Try to Sledgehammer C # as a scripting language to perform decryption

Source: Internet
Author: User

Background

We know that Unity3d is in the form of C # scripting language to implement the game's logical code writing, the same Scott server also set up through C # script to implement game logic, but this article is not to really analyze and decrypt their operating mechanism, just want to through their own needs, to explore the principles of the summary.

Below to say my own needs, relatively simple, because often in the non-development environment to deploy some small tools, do system maintenance, but every time and do not bother to bring notebook and build environment to the scene, but the system data is always so wonderful, often have bugs appear, whether the whim is to make tools into script, So the scene is easy to adjust (simple to modify the script), on-site to do not have to toss back and forth, it is very happy.

Statement: This article started in the Bull, the second blog park, I original.

Http://www.manew.com/thread-96136-1-1.html

Principle

Unity3d used mono This earth man knows, where's Scott? I have analyzed its source code, probably know that it needs to be compatible with Iconpyhton and C # Two of scripts, so it did a layer of encapsulation, but see it referenced dll Lib has mono shadow, so it is also mono (not to shoot bricks, in fact, it does not matter what). The actual principle is very simple, that is, the script dynamic compilation (a bit like the interpretation of execution, strictly not, I think the General Lee's Hot update script should not be dynamically compiled because many platforms do not support such as iOS), checked the information currently C # Dynamic compilation of three ways Codedom,mono, Roslyn, these three kinds of tutorials should be a bunch of, but mono and Roslyn are not many, after all, this demand is relatively small, and Roslyn is the latest, examples are "Hello world" level of people very uncomfortable, and finally found a library cs-script, The URL is https://csscriptsource.codeplex.com/, read the following document, and glance at the code:

CSScript.EvaluatorConfig.Engine = Evaluatorengine.roslyn;                                Evaluatorengine.mono;                                Evaluatorengine.codedom;var Sqr = Csscript.evaluator                  . CreateDelegate (@ "int Sqr (int a)                                    {                                        return a * A;                                    }"); var r = Sqr (3);
When I saw the lines of code, I smiled, actually cs-script is for three ways of the upper package, you can customize the choice with that one, this scheme for my already enough. The following is a test, then the implementation can be.
Test
The detailed steps taught, I don't want to say more cs-script have their own detailed tutorials. Since my requirement is to use my own code to invoke the script, here is not the command to execute the script.
The first step installs, creates a console project, and then uses NuGet for the Cs-script installation, which executes in the Program management console
 
 

NuGet will automatically download the relevant DLLs and example scripts into the project very simply, the results are as follows

The following script is automatically added to the example program, open to see very simple, here is not the table

In the main function, add the following code, test (the compiler found there are errors, may be missing Roslyn reference, no OVA, direct culling or comment)

static void Main (string[] args)
{
Hostapp.test ();
}

The implementation of the results are as follows, the concrete can be seen in the test method is some how to dynamically execute classes, methods, static methods, interfaces, and so simple example, for our simple needs are enough (if there are complex requirements such as host, context, advanced dynamic compilation of what to look at the document, it is not expected to say)

The simple test environment is now available

Implement

According to the official example a little bit of modification, modify the main function as follows:

Write your own Hello.cs script under the bin:

Create a batch execution to pass the file name as a dynamic parameter, performing the following effect

Note Here is the script file format do not forget the utf-8 format, otherwise Chinese will be garbled.

Application

The script that runs the template above is basically done, so just put the tool code that you need to execute in a separate class, then start the function as run, so that you can follow the script as long as you modify the corresponding name of the C # code file in a DOS batch script. Here is my tool script code (a C # source that compresses a level two directory):

Full of joy, but the result of a shameful failure

The main reason is that I used another C # class file in the MyScript script related functions, that is, the red box in the code, in fact, this is a context is missing (or host problem), the solution here is to dynamically add assembly, one is to use a DLL. For the sake of simplicity here, I choose the second method. This assumes that Ziputil is a common class library that I encapsulate into a standalone DLL and reference it to the project as a class library. The results of the operation are as successful as expected (so happy)

Here also embodies the strong cs-script, dynamic on the management of the third-party application of the DLL did not need to do any processing, praise one!

Summarize

At this point, the small test sledgehammer use cs-script This tool, very easy to implement the C # script dynamic invocation and execution, but also introduced C # as a script dynamic compilation of the principle and process of execution, of course, there are many complex applications, such as execution efficiency, hot plug Update, advanced dynamic compilation, The context is left to everyone to study it.

For me as my own needs have been achieved, for the dynamic implementation of C # scripts also have a deeper understanding, especially through the process of troubleshooting, more profound understanding of the premise of using the script and what the environment is: The script is better than a single C # file (the meaning of the script is lost, there is no need) The use of scripts is in the premise of a perfect class library, through the script to implement the variable logic, if the class library is not mature and stable or do not use the script (here can refer to Scott at least it provides the framework level of the library)

SOURCE download

Reference reading:

C # scripting engine Cs-script (i)--a preliminary knowledge of http://www.cnblogs.com/strinkbug/p/4697034.html

Try to Sledgehammer C # as a scripting language to perform decryption

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.