GA好望角|魔力宝贝私服|魔力私服|魔力私服|GA反外挂

 找回密码
 立即注册
凤凰(QQ:715837)
售前咨询
GA白皮书(客户端)
GA白皮书(服务端)
cgdev.me开发者论坛
放映地(炫斗魔力公益PK服)
新服发布站
欢迎来到GA好望角~
价格公示表
回首页
查看: 3476|回复: 1

GA framework 4.0 加入C#语言支持

 关闭 [复制链接]

131

主题

137

帖子

137

积分

管理员

Rank: 9Rank: 9Rank: 9

金钱
2130
号角币
466
发表于 2019-12-8 22:32:32 | 显示全部楼层 |阅读模式
GA framework 4.0 将同时支持Lua、C#语言进行编写客户端或服务端脚本


C#语言在GA的特征:
1 支持C#7.0 最新所有语法
2 不需要 .net framework 框架,可以运行在centos或者windows任意一个版本上运行
3 支持反射、委托、泛型
4 支持 linq 表达式
5 自动GC垃圾回收


C#语言在GA不支持的特征:
1 不支持抢占式多线程,如创建新线程,将以Lua同等的协同式多线程创建
2 不支持unsafe关键字,意味着将无法使用任何指针类
3 不支持JIT(即时编译器),意味着CSharpCodeProvider无法使用


使用C#的好处:
1 大量算法,既定网络存在大量算法,如加密,压缩,传输等可以直接编译成库给Lua使用
2 强类型,意味着编译时期就做类型检查,避免隐式的错误
3 优质的IDE VS2019,强大的代码补全与检查


原理:
GA没有使用 .net framework 或 .net Core框架,而是使用了Lua 重写了 .net CLR
意味着C#可以运行在Lua的虚拟机之上


执行过程:
C#源码 --> IL --> IL AST --> Lua AST --> Lua

      
using System;

namespace HelloLua {
  public static class Program {
    public static void Main() {
      Console.WriteLine("hello lua!");
    }
  }
}

GA会将您的C#代码转换成以下等价Lua代码

      
local System = System
System.namespace("HelloLua", function (namespace) 
  namespace.class("Program", function (namespace) 
    local Main
    Main = function () 
      System.Console.WriteLine("hello lua!")
    end
    return {
      Main = Main
    }
  end)
end)



互相调用规则:
Lua调用C#的类
请将需要调用的类方法设置为public static (公开且静态)
如上代码Lua的调用方式为 命名空间.类名.方法名
HelloLua.Program.Main()


C#中直接写入Lua:
可以使用 /*[[ ]]*/ 写入 如下图
      
using System;

namespace HelloLua {
  public static class Program {
    public static void Main() {
      /*
         [[
              print("Lua Script") 
         ]]
      */
      Console.WriteLine("hello lua!");
    }
  }
}






回复

使用道具 举报

131

主题

137

帖子

137

积分

管理员

Rank: 9Rank: 9Rank: 9

金钱
2130
号角币
466
 楼主| 发表于 2019-12-8 22:52:11 | 显示全部楼层
例程:RC4算法在C#中编写 自动生成的等价Lua代码
      
using System;
using System.Text;

namespace Libraries
{
    /// <summary>
    /// Description of CryptoGraphy.
    /// </summary>
    public class RC4Crypt{
        byte[] S;
        byte[] T;
        byte[] K;
        byte[] k;
        public RC4Crypt() { }
        public RC4Crypt(byte[] key){
            this.K=key;
        }
        public byte[] Key
        {
            get
            {
                return K;
            }
            set
            {
                K = value;
            }
        }
        //初始化状态向量S和临时向量T,供keyStream方法调用
        void initial(){
            if (S == null || T == null)
            {
                S = new byte[256];
                T = new byte[256];
                K = new byte[256];
                k = new byte[256];
            }
            for (int i = 0; i < 256; ++i) {
                S[i]=(byte)i;
                T[i] = K[i % K.Length];
            }
        }
        //初始排列状态向量S,供keyStream方法调用
        void ranges(){
            int j=0;
            for (int i = 0; i < 256; ++i) {
                j=(j+S[i]+T[i])&0xff;
                S[i]=(byte)((S[i]+S[j])&0xff);
                S[j]=(byte)((S[i]-S[j])&0xff);
                S[i]=(byte)((S[i]-S[j])&0xff);
            }
        }
        //生成密钥流
        //len:明文为len个字节
        void keyStream(int len){
            initial();
            ranges();
            int i=0,j=0,t=0;
            k=new byte[len];
            for (int r = 0; r < len; r++) {
                i=(i+1)&0xff;
                j=(j+S[i])&0xff;

                S[i]=(byte)((S[i]+S[j])&0xff);
                S[j]=(byte)((S[i]-S[j])&0xff);
                S[i]=(byte)((S[i]-S[j])&0xff);

                t=(S[i]+S[j])&0xff;
                k[r]=S[t];
            }
        }

        public byte[] EncryptByte(byte[] data){
            //生产密匙流
            keyStream(data.Length);
            for (int i = 0; i < data.Length; i++) {
                k[i]=(byte)(data[i]^k[i]);
            }
            return k;
        }

        public byte[] DecryptByte(byte[] data){
            return EncryptByte(data);
        }

        //是否回收完毕
        bool _disposed;
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        ~RC4Crypt()
        {
            Dispose(false);
        }
        //这里的参数表示示是否需要释放那些实现IDisposable接口的托管对象
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed) return;//如果已经被回收,就中断执行
            if (disposing)
            {
                //TODO:释放那些实现IDisposable接口的托管对象

            }
            //TODO:释放非托管资源,设置对象为null
            S = null;
            T = null;
            K = null;
            k = null;
            _disposed = true;
        }
    }
        
        public class Pro
    {
                public static char[] b2c(byte[] b)                
                {
                        char[] ret = {'0'};
                        /*
                        [[
                                ret = b
                                
                        ]] 
                        */
                        return ret;
                }
                public static string Main(string[] args)
                {        
                        char[] s999  = ("abcde").ToCharArray();
                        StringBuilder sb = new StringBuilder("ABC", 50);        
                        char[] bb  = Pro.b2c(new byte[]{67,66,67});
                                                
                        string str = new string(bb);
                        /*
                        [[
                                
                                print(str)
                        ]] 
                        */
                
                        RC4Crypt cb = new RC4Crypt();
                        char[] s  = ("123").ToCharArray();
                        byte[] s2 = new byte[s.Length];
                        for (int i = 0; i < s.Length; i++)  
                        {
                                s2[i] = (byte)s[i];
                        }
                        
                        byte[] s3  = cb.EncryptByte(s2);
                        char[] s4 = new char[s3.Length];
                        for (int i = 0; i < s3.Length; i++)  
                        {
                                s4[i] = (char)s3[i];
                        }
                
                        return  new string(s4);
                }
                                
        }
        
}[i]

自动生成等价的Lua代码

      
local System = System
local ArrayByte = System.Array(System.Byte)
local ArrayChar = System.Array(System.Char)
local Libraries
System.import(function (out)
  Libraries = out.Libraries
end)
System.namespace("Libraries", function (namespace)
  -- <summary>
  -- Description of CryptoGraphy.
  -- </summary>
  namespace.class("RC4Crypt", function (namespace)
    local getKey, setKey, initial, ranges, keyStream, EncryptByte, DecryptByte, Dispose, 
    __gc, Dispose1, __ctor1__, __ctor2__
    __ctor1__ = function (this)
    end
    __ctor2__ = function (this, key)
      this.K = key
    end
    getKey = function (this)
      return this.K
    end
    setKey = function (this, value)
      this.K = value
    end
    initial = function (this)
      if this.S == nil or this.T == nil then
        this.S = ArrayByte:new(256)
        this.T = ArrayByte:new(256)
        this.K = ArrayByte:new(256)
        this.k = ArrayByte:new(256)
      end
      for i = 0, 255 do
        this.S:set(i, System.toByte(i))
        this.T:set(i, this.K:get(System.mod(i, #this.K)))
      end
    end
    ranges = function (this)
      local j = 0
      for i = 0, 255 do
        j = (j + this.S:get(i) + this.T:get(i)) & 0xff
        this.S:set(i, System.toByte((this.S:get(i) + this.S:get(j)) & 0xff))
        this.S:set(j, System.toByte((this.S:get(i) - this.S:get(j)) & 0xff))
        this.S:set(i, System.toByte((this.S:get(i) - this.S:get(j)) & 0xff))
      end
    end
    keyStream = function (this, len)
      initial(this)
      ranges(this)
      local i = 0 local j = 0 local t = 0
      this.k = ArrayByte:new(len)
      for r = 0, len - 1 do
        i = (i + 1) & 0xff
        j = (j + this.S:get(i)) & 0xff

        this.S:set(i, System.toByte((this.S:get(i) + this.S:get(j)) & 0xff))
        this.S:set(j, System.toByte((this.S:get(i) - this.S:get(j)) & 0xff))
        this.S:set(i, System.toByte((this.S:get(i) - this.S:get(j)) & 0xff))

        t = (this.S:get(i) + this.S:get(j)) & 0xff
        this.k:set(r, this.S:get(t))
      end
    end
    EncryptByte = function (this, data)
      --生产密匙流
      keyStream(this, #data)
      for i = 0, #data - 1 do
        this.k:set(i, System.toByte(data:get(i) ~ this.k:get(i)))
      end
      return this.k
    end
    DecryptByte = function (this, data)
      return EncryptByte(this, data)
    end
    Dispose = function (this)
      Dispose1(this, true)
      System.GC.SuppressFinalize(this)
    end
    __gc = function (this)
      Dispose1(this, false)
    end
    Dispose1 = function (this, disposing)
      if this._disposed then
        return
      end
      --如果已经被回收,就中断执行
      if disposing then
        --TODO:释放那些实现IDisposable接口的托管对象
      end
      --TODO:释放非托管资源,设置对象为null
      this.S = nil
      this.T = nil
      this.K = nil
      this.k = nil
      this._disposed = true
    end
    return {
      getKey = getKey,
      setKey = setKey,
      EncryptByte = EncryptByte,
      DecryptByte = DecryptByte,
      _disposed = false,
      Dispose = Dispose,
      __gc = __gc,
      Dispose1 = Dispose1,
      __ctor__ = {
        __ctor1__,
        __ctor2__
      }
    }
  end)

  namespace.class("Pro", function (namespace)
    local b2c, Main
    b2c = function (b)
      local ret = ArrayChar(48 --[['0']])
      ret = b
      return ret
    end
    Main = function (args)
      local s999 = ("abcde"):ToCharArray()
      local sb = System.StringBuilder("ABC", 50)
      local bb = b2c(ArrayByte(67, 66, 67))
      local str = System.String(bb)
      print(str)

      local cb = Libraries.RC4Crypt()
      local s = ("123"):ToCharArray()
      local s2 = ArrayByte:new(#s)
      for i = 0, #s - 1 do
        s2:set(i, System.toByte(s:get(i)))
      end

      local s3 = cb:EncryptByte(s2)
      local s4 = ArrayChar:new(#s3)
      for i = 0, #s3 - 1 do
        s4:set(i, System.toUInt16(s3:get(i)))
      end


      return System.String(s4)
    end
    return {
      b2c = b2c,
      Main = Main
    }
  end)
end)

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|GA好望角

GMT+8, 2024-4-30 11:49 , Processed in 0.054483 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表