Test Tool C # execute decryption as a script language,

Source: Internet
Author: User

Test Tool C # execute decryption as a script language,
Background

We know that Unity3d uses the C # scripting language to write the logic code of the game. Similarly, the SCOTT server also sets the C # script to implement the game logic, however, this article does not really want to analyze and decrypt their operating mechanisms, but just wants to discuss and summarize the principles of this article based on your own needs.

Next, my own requirements are relatively simple. Because some gadgets are often deployed in non-development environments for system maintenance, I am too lazy to bring my notebook and compilation environment to the site every time, however, the system data is always so strange, and there are often bugs. It is easy to make adjustments (simply modify the script) on the site, so there is no need to go back and forth to solve the problem on the site, not very Happy.

Statement: This article was first published in manniu, the blog posts park, and I am original.

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

 

Principle

The Mono used by Unity3d knows, Scott? I have analyzed the source code before and probably know that it must be compatible with the IconPyhton and C # scripts. Therefore, it implements a layer of encapsulation, but the DLL lib referenced by it has the shadow of Mono, so it is estimated that it is also Mono (it doesn't matter if you don't make a brick ). The actual principle is very simple, that is, dynamic compilation of scripts (a bit like interpretation and execution, should not be strictly, I think Mr. Li's hot update script should not be dynamically compiled because many platforms do not support such as IOS at all. I checked the information below. Currently, c # has three methods of dynamic compilation: CodeDom, Mono, roslyn, these three courses should be a bunch of courses, but there are not many Mono And Roslyn. After all, this demand is relatively small, and Roslyn is the latest, the example is "Hello world", which makes people very uncomfortable. Finally, we found a library CS-Script. The website is https://csscriptsource.codeplex.com:

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 lines 1, 2, and 3 of the Code, I smiled. In fact, CS-Script is an upper-layer encapsulation of the three methods. You can choose the one you want to use, this solution is enough for me. The following is a test and then implementation.
Test
I don't want to talk about the detailed steps. I have my own detailed tutorial on CS-Script. Because I need to use my own code to call the script, I will not talk about using commands to execute the script here.
Step 1: Create a console project, use nuget to install CS-Script, and run
Install-Package CS-Script 

Nuget automatically downloads related DLL and Example scripts to the project. The result is as follows:

The execution result is as follows. For details, you can see that the methods in Test are some simple examples of dynamic execution classes, methods, static methods, and interfaces, our simple requirements are enough. (If you have complex requirements such as Host, context, and advanced dynamic compiling, please refer to the document. We can't explain it here)

Now, a simple test environment has been created.

Implementation

Perform a slight transformation based on the official example. Modify the Main function as follows:

Write your own Hello. cs script in bin:

Create a batch processing execution to pass in the file name as a dynamic parameter. The execution result is as follows:

Note that the file format of the script should not be forgotten as UTF-8, Otherwise Chinese characters will be garbled.

Application

The above script execution template is basically completed, so you only need to put the tool code you need to execute into a separate class, and then start the function as Run, in this way, as long as the corresponding name of the c # code file is modified in the Dos batch processing script, it can be executed according to the script, the following is my tool script code (C # source code of a compressed second-level directory ):

I'm so happy, but the result is a shameful failure.

The main reason is that I used another related function in the c # class file in the Myscript script, that is, the Code marked in the red box, in fact, this is a lack of context (or host problem). One solution here is to dynamically add Assembly; the other is to use DLL. For simplicity, I chose the second method. Assume that ZipUtil is a public class library. I encapsulate it into a separate DLL and reference it as a class library to the project. The running result is successfully executed as expected (So happy)

This also shows that CS-Script is powerful, and the DLL of a third-party application is managed dynamically without any processing. I like it!

Summary

So far, it is Easy to use the CS-Script tool to implement dynamic calling and execution of the C # Script, C # is also introduced as the principle and process of dynamic script compilation and execution. Of course, there are many complicated applications, such as execution efficiency, hot swapping and updating, and advanced dynamic compilation, the context is for your own research.

As my own needs have been met, I also have a deeper understanding of dynamic execution of C # scripts, especially through troubleshooting, I am more deeply aware of the prerequisites and environment for using scripts: the script should be a separate C # file (if it is too much, it will lose the meaning of the script, and it is not necessary ); the script is used to implement variable logic through scripts on the premise of a complete class library, if the class library is not mature and stable enough, do not use scripts. (For details, refer to Scott. At least it provides Framework-level libraries)

 

Source code download

Refer:

C # Script engine CS-Script (I) -- first knowledge of http://www.cnblogs.com/strinkbug/p/4697034.html

Related Article

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.