l# script language, direct DLL as script execution (illustrated)

Source: Internet
Author: User

What is l#: Run DLL as a Script.

A Pure C # IL Runner, Direct Parse Execution IL scripting engine.

in principle, it's a simulated execution. CLR the work, from the performance is to DLL executes as a resource directly loaded.

is not many classmates dream of hot more DLL ? No, No.

load directly as a reflection symbolDLL,DLLthe interpretation is performed or by theCLRcompleted. will also involveJITengine.
butl#is a"Simulationthe CLR's work. Do not use reflection to load symbols, do not useJIT
in theIOS(Unity),WP8such a platform,CLRit does not reflect the ability to load symbols. IOSit's closed.Jit
l#not subject to this restriction

But other constraints have been brought : some trouble with the interaction

remember this article mainly: l# Loaded by DLL code in is script code, and programs in system code cannot inherit from each other

l# What is the syntax of:

l# is a DLL that runs dotnet directly, you can use C # vb.net F # and so on. As long as the compilation can be done.
Unlike C#light's own implementation of the grammatical interpretation problem, l# does not have a grammatical interpretation level bug, only the implementation level of the bug.
It is precisely c#light in the maintenance of the grammar interpreter into the mire, deep reflection, only l# this idea.

About the code:

The latest code can be obtained at http://svn.cltri.com
or Https://github.com/lightszero/lsharp.
is still in the alpha stage, the bug is indispensable, this stage only recommended the ability to do more than the strong students try.
Find the bug scold me is sure to scold, if after the scold, can push a wrong use case on GitHub
Will not be very grateful.

?

The following describes the use of l# in Unity3d

Step by step is like this, is the devil's step

L#forunity3d Hell#oworld Environment Building diagram

1. Create a new project

2.Copy L#foru3d class Library to DLL

or directly into the L#foru3d source code can also, optional

3. Create a code for l# module access)

Stole the lyrics from Pommeron, but I'm not going to apologize.

4. Create a l# module

5. Add a reference to the l# module

Because we just created in the Plugins directory code Interface.cs, want to l# module access to him, the corresponding Assembly-csharp-firstpass this module

6. Writing the l# module

You can see that the l# module makes a rough call to our program.

And then F7, you'll get the DLL file for the l# module

7. Put the module file as a resource into the Unity3d

Rename the module file to

HotFixCode.Dll.bytes

HotFixCode.Pdb.bytes

PDB this file is not required, but when the script is out of the ordinary, you need to check which file is the wrong one, and no PDB is found. Can only give you a dry address.

Of course you are familiar with IL, you use other tools, according to this address can also find code location.

He should be identified as two textasset in unity.

8. Write the test procedure Test001

Create a new scene and save Test001,

Create a new Test001 script to attach directly to the camera

Write the following code:

Using Unityengine;

Using System.Collections;

?

public class Test001:monobehaviour {

?

???? Clrsharp.clrsharp_environment env;

???? Use this for initialization

???? void Start () {

???????? Create a CLRSHARP environment

???????? Env=new clrsharp.clrsharp_environment (New Logger ());

????}

????

???? Update is called once per frame

???? void Update () {

????

????}

???? The public class logger:clrsharp.iclrsharp_logger//implements the l# log interface

???? {

???????? public void Log (String str)

???????? {

???????????? Debug.Log (str);

????????}

????????

???????? public void Log_error (String str)

???????? {

???????????? Debug.logerror (str);

????????}

????????

???????? public void log_warning (String str)

???????? {

???????????? Debug.logwarning (str);

????????}

????}

}

9. Executive HelloWorld

Go to Unity and run the project

This is the only result, there is a log l# is initialized

L#helloworld second step, call the l# module diagram

Continue to add code

?

Add code after the start function

0. Loading the module

???????? Loading the l# module
???????? Textasset dll = resources.load ("HoxFixCode.dll") as Textasset;
???????? Textasset pdb = resources.load ("hoxfixcode.pdb") as Textasset;
???????? System.IO.MemoryStream Msdll = new System.IO.MemoryStream (dll.bytes);
???????? System.IO.MemoryStream mspdb = new System.IO.MemoryStream (pdb.bytes);
???????? Env. LoadModule (Msdll, NULL);//If the PDB is not required, the second parameter is passed NULL
???????? Env. LoadModule (Msdll, mspdb);

1. Creating a Thread context

2. Get the type of preparation call

3. Calling static functions

4. Calling member functions

The Complete Start function code

???? void Start () {

???????? Create a CLRSHARP environment

???????? Env=new clrsharp.clrsharp_environment (New Logger ());

?

???????? Loading the l# module

???????? Textasset dll = resources.load ("HoxFixCode.dll") as Textasset;

???????? Textasset pdb = resources.load ("hoxfixcode.pdb") as Textasset;

???????? System.IO.MemoryStream Msdll = new System.IO.MemoryStream (dll.bytes);

???????? System.IO.MemoryStream mspdb = new System.IO.MemoryStream (pdb.bytes);

???????? Env. LoadModule (Msdll, NULL);//If the PDB is not required, the second parameter is passed NULL

???????? Env. LoadModule (Msdll, mspdb);

???????? Debug.Log ("LoadModule HotFixCode.dll done.");

?

???????? STEP01 establishes a thread context that simulates the threading model of l# and creates one per thread.

???????? Clrsharp.threadcontext context = new Clrsharp.threadcontext (env);

???????? Debug.Log ("Create threadcontext for l#.");

?

???????? STEP02 get the type of l# you want to call

???????? Clrsharp.iclrtype Wanttype = env. GetType ("Hoxfixcode.testclass", null);//with full name, including namespace

???????? Debug.Log ("GetType:" +wanttype.name);

???????? Corresponds to the Type.GetType in the Reflection code

?

???????? STEP03 Static Calls

???????? Get a function on the type, the first argument is the function name, the second argument is the function's parameter table, this is a function without parameters

???????? Clrsharp.imethod method01 = Wanttype.getmethod ("Test1", CLRSharp.MethodParamList.MakeEmpty ());

???????? Method01. Invoke (context, NULL, NULL);//The third parameter is the object[] parameter table, and this example does not require a parameter

???????? This is a static function call, corresponding to the code he is HotFixCode.TestClass.Test1 ();

?

???????? STEP04 member invocation

???????? The second Test program is a member variable, so create an instance first

???????? Clrsharp.clrsharp_instance typeobj = new Clrsharp.clrsharp_instance (wanttype as Clrsharp.iclrtype_sharp);//Create Instance

???????? Clrsharp.imethod methodctor = Wanttype.getmethod (". ctor", CLRSharp.MethodParamList.MakeEmpty ());//Get constructor

???????? Methodctor. Invoke (context, typeobj, NULL);//Execute constructor

???????? The function of these lines corresponds to the code about equal to Hotfixcode.testclass typeobj =new hotfixcode.testclass ();

???????? Clrsharp.imethod method02 = Wanttype.getmethod ("Test2", CLRSharp.MethodParamList.MakeEmpty ());

???????? Method02. Invoke (context, typeobj, null);

???????? The effect of these two lines is equivalent to Typeobj.test2 ();

????}

Execution results

l# script language, direct DLL as script execution (illustrated)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.