Git Product home page Git Product logo

scorpio-csharp's Introduction

Scorpio-CSharp

此脚本为纯c#实现的脚本系统,最低支持 .net framework 3.5和 .net standard 2.0, 语法类似 javascript

基础介绍

安装 sco 命令行

  • windows 7, windows 8, MacOS, Linux 需要预先安装 PowerShell Core, 然后运行命令行
pwsh -Command "Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://qingfeng346.gitee.io/installsco.ps1'))"
  • windows 10 直接运行命令行, 也可以安装 PowerShell Core 使用上面的方法安装
powershell.exe -Command "Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://qingfeng346.gitee.io/installsco.ps1'))"

兼容大部分 .net 平台

  • .net framework 3.5 及以上
  • .net standard 2.0 及以上
  • .net core 2.0 及以上
  • unity3d
  • asp.net
  • asp.net core
  • mono
  • xamarin

Unity3d平台支持:

  • PC, Mac & Linux Standalone(包括IL2CPP)
  • iOS(包括IL2CPP)
  • Android(包括IL2CPP)
  • WebGL

注意事项

  • 脚本文本文件编码要改成 utf8 without bom (无签名的utf8格式)

  • 使用 importType 函数引入一个c#类, 参数字符串请参考 Type.GetType, 类似

    • TopNamespace.SubNameSpace.ContainingClass+NestedClass,MyAssembly
    • TopNamespace.Sub+Namespace.ContainingClass+NestedClass,MyAssembly
  • 脚本内所有c#实例(除了bool,number,string,enum等基础类型) 均为引用, struct 变量也一样

  • event 对象 += -= 操作可以使用函数 add_[event变量名] remove_[event变量名] 代替

  • c# 扩展函数, 请使用静态函数方式调用

  • Unity3d 使用 IL2CPP 后, 部分Unity3D的类或函数不能反射获取,请自行包一层函数,或者使用快速反射功能

  • UWP平台master配置下 genericMethod, genericType 函数会出问题, 其他平台均无问题 (android&il2cpp ios&il2cpp webgl等), 报错: PlatformNotSupported_NoTypeHandleForOpenTypes. For more information, visit http://go.microsoft.com/fwlink/?LinkId=623485

反射调用c#运算符重载函数

运算符号 反射名称 脚本是否支持直接调用
+ op_Addition 支持
- op_Subtraction 支持
* op_Multiply 支持
/ op_Division 支持
% op_Modulus 支持
| op_BitwiseOr 支持
& op_BitwiseAnd 支持
^ op_ExclusiveOr 支持
> op_GreaterThan 支持
>= op_GreaterThanOrEqual 支持
< op_LessThan 支持
<= op_LessThanOrEqual 支持
== op_Equality 支持
!= op_Inequality 不支持, 脚本 != 会直接取反 ==
[] get_Item(获取变量) 支持 key 不为string的情况
[] set_Item(设置变量) 支持 key 不为string的情况

快速反射

快速反射类生成

  • 使用命令行可以生成快速反射类文件,例子
sco fast -dll [dll文件路径] -class [class完整名] -output [输出目录]

快速反射类使用

  • 例如使用快速反射的类为 UnityEngine.GameObject , 生成的快速反射类则为ScorpioClass_UnityEngine_GameObject, 然后 c# 调用
Scorpio.Userdata.TypeManager.SetFastReflectClass(typeof(UnityEngine.GameObject), new ScorpioClass_UnityEngine_GameObject(script))

源码目录说明

  • Scorpio 脚本引擎源码
  • ScorpioExec 命令行 sco
  • ScorpioReflect 快速反射机制的实现
  • ScorpioTest Unity3D内使用sco脚本示例

Unity导入Scorpio-CSharp:

Scorpio脚本Hello World函数 (c# console项目):

using System;
using Scorpio;
using Scorpio.Userdata;
namespace HelloWorld {
    public delegate void TestDelegate1(int a, int b);
    public class Test {
        public static TestDelegate1 dele;
        private int a = 100;
        public Test(int a) {
            this.a = a;
        }
        public void Func() {
            Console.WriteLine("Func " + a);
        }
        public static void StaticFunc() {
            Console.WriteLine("StaticFunc");
        }
        public static void Call() {
            if (dele != null) dele(100, 200);
        }
    }
    public enum TestEnum {
        Test1,
        Test2,
        Test3,
    }
    class MainClass {
        public static void Main(string[] args) {
            TypeManager.PushAssembly(typeof(MainClass).Assembly);            //添加当前程序的程序集
            Script script = new Script();                           //new一个Script对象
            script.LoadLibrary();                                   //加载所有Scorpio的库,源码在Library目录下
            script.SetGlobal("CTest", ScriptValue.CreateObject(new Test(300)));  //SetObject可以设置一个c#对象到脚本里
                                                                            //LoadString 解析一段字符串,LoadString传入的参数就是热更新的文本文件内容
            script.LoadString(@"
print(""hello world"")
");
            //Scorpio脚本调用c#函数
            script.LoadString(@"
MyTest = import_type('HelloWorld.Test')                     //import_type 要写入类的全路径 要加上命名空间 否则找不到此类,然后赋值给 MyTest 对象
TestDelegate1 = import_type('HelloWorld.TestDelegate1')     //委托类型
MyTest.StaticFunc()                                         //调用c#类的静态函数
var t = MyTest(200)                                         //new 一个Test对象, 括号里面是构造函数的参数
t.Func()                                                    //调用c#的内部函数
CTest.Func()                                                //调用c#的内部函数 CTest是通过 script.SetObject 函数设置
MyTest.dele = function(a, b) {                              //设置委托类型, c# 委托类型可以直接传入 脚本 function
    print('script function : ' + a + '   ' + b)
}
var func = TestDelegate1(function(a, b) {                  //也可以直接申请一个委托类型
    print('delegate : ' + a + '   ' + b)
})
MyTest.dele += func                                         //只有使用 委托构造函数申请的 function 才可以 -=
MyTest.dele(1111,2222)
MyTest.dele -= func
MyTest.dele(11111,22222)

MyTest.Call()
TestEnum = import_type('HelloWorld.TestEnum')       //引入枚举
print(TestEnum.Test1)                               //直接使用枚举
");
            Console.ReadKey();
        }
    }
}

捐助作者

如果此项目对你有所帮助,可以请作者喝杯咖啡

scorpio-csharp's People

Contributors

fengyuezhu avatar qingfeng346 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.