Scut:脚本引擎

  Scut 可以执行 C#、Python、Lua 三种类型的脚步,Scut 是如何加载并传递参数的呢?

  首先值得注意的是:Scut 在编译时就会将逻辑层脚本源码复制到bin/Script的目录下。

1. ScriptRuntimeDomain、ScriptRuntimeScope、ScriptDomainContext

    public abstract class ScriptBaseScope : IDisposable
    {
        protected readonly ScriptSettupInfo SettupInfo;   //脚本配置信息
        protected readonly List<string> WatcherPathList;  //被监控文件列表
        private string[] _rootPathArr;                    //根路径拆解
        protected string _modelAssemblyPath;           //model程序集路径
        protected Assembly _modelAssembly;                //model程序集
        protected string _csharpAssemblyPath;          //C#程序集路径
        protected Assembly _csharpAssembly;           //C#程序集
    }

  疑问:这里为什么将程序集拆成C#Assembly与ModelAssemble?

  LuaRuntimeScope 包含了 ScriptBaseScope;

  PythonRuntimeScope 包含了 LuaRuntimeScope;

  CSharpRuntimeScope 包含了 PythonRuntimeScope;

     public override void Init()
        {
            _modelCodeCache = new DictionaryExtend<string, ScriptFileInfo>();  //model 文件夹下脚本加载所需的代码缓存
            _csharpCodeCache = new DictionaryExtend<string, ScriptFileInfo>();  //C# 文件夹下脚本加载所需的代码缓存
            _modelScriptPath = Path.Combine(SettupInfo.RuntimePath, SettupInfo.ScriptRelativePath, SettupInfo.ModelScriptPath);
            AddWatchPath(_modelScriptPath, FileFilter);  //model 文件夹监视

            _csharpScriptPath = Path.Combine(SettupInfo.RuntimePath, SettupInfo.ScriptRelativePath, SettupInfo.CSharpScriptPath);
            AddWatchPath(_csharpScriptPath, FileFilter)   //CSharp 文件夹监视
        Load(); 
        private void Load()
        {
            var pathList = new String[] { _modelScriptPath, _csharpScriptPath };

            foreach (var path in pathList)
            {
                if (Directory.Exists(path))
                {
                    var files = Directory.GetFiles(path, FileFilter, SearchOption.AllDirectories);  //过滤出 bin/Script 目录下的所有 .cs 文件
                    if (files.Length > 0)
                    {
                        LoadScriptAssemblyInfo(path);  //创建该程序集的属性文件,用于生成配置清单
                    }
                    foreach (var fileName in files)
                    {
                        LoadScript(path, fileName);    //将文件内容动态加载到 CSharpRuntimeScope 结构中
        private ScriptFileInfo LoadScript(string scriptPath, string fileName)
        {
            ScriptFileInfo scriptFileInfo = null;
            string scriptCode = GetScriptCode(fileName);    //以相对地址+文件前缀作为 key;
            scriptFileInfo = CreateScriptFile(fileName);   //将文件源码作为 value;
            if (scriptFileInfo != null)
            {
                if (scriptPath == _modelScriptPath)
                {
                    _modelCodeCache[scriptCode] = scriptFileInfo;
                }
                else
                {
                    _csharpCodeCache[scriptCode] = scriptFileInfo;
                }
            }
            return scriptFileInfo;
        }
                    }
                }
            }
            Compile();  //在CSharpRuntimeScope结构中找到代码进行编译,并将编译后的程序集加载到 BaseRuntimeScope 中了
            BuildPythonReferenceFile();
        }

        base.Init();   //执行C#、python、lua runtimescope 的初始化API
     }

  ScriptRuntimeScope 管理了具体的程序集,ScriptRuntimeDomain 则是负责将程序集运行在具体的运行域中。

  ScriptDomainContext 的作用暂时不是很清楚。

 

2. ScriptEngine

        public static void Initialize()
        {
            try
            {
                ScriptCompiler.ClearScriptRuntimeTemp();                   //删除运行目录下的ScriptRuntimeDomain目录
                ConfigManager.ConfigReloaded += OnScriptSettingReLoad;     //配置文件重载时的钩子API
                var scope = InitScriptRuntimeScope();
        private static ScriptRuntimeScope InitScriptRuntimeScope()
        {
            //star compile
            if (Interlocked.Exchange(ref _isCompiling, 1) == 0)   //将_isCompiling设为1,并返回原值
            {
                ScriptRuntimeDomain runtimeDomain = null;
                try
                {
                    string runtimePath = MathUtils.RuntimePath ?? MathUtils.RuntimeBinPath;   //当前者为null时,取后者的值
                    AppDomain.CurrentDomain.AppendPrivatePath(ScriptCompiler.ScriptPath);     //增加专用路径:运行时路径/temp
                    runtimeDomain = new ScriptRuntimeDomain(typeof(ScriptRuntimeDomain).Name, new[] { _settupInfo.RuntimePrivateBinPath, ScriptCompiler.ScriptPath });  //为脚本创建新的运行域
                    foreach (var assemblyName in _settupInfo.ReferencedAssemblyNames)         //_setupInfor 是脚本的安装信息,通过配置读取获得,包括C#、Python、Lua脚本所在的路径、模块的路径等参数--加载脚本运行所需的依赖库
                    {
                        //排除System的dll
                        if (string.IsNullOrEmpty(assemblyName) ||     
                            !Path.IsPathRooted(assemblyName)) continue;
                        string key = Path.GetFileNameWithoutExtension(assemblyName);
                        runtimeDomain.LoadAssembly(key, assemblyName);   //从这里可以发现ScriptDomainContext主要是管理依赖库的,也就是脚本运行的上下文环境
                    }
                    var scope = runtimeDomain.CreateScope(_settupInfo);  //在运行域上创建并初始化管理程序集的结构,并动态加载程序集至 _modelAssemble _csharpAssemble
                    //ignore error, allow model is empty.
                    if (scope == null)
                    {
                        if (_runtimeDomain == null) _runtimeDomain = runtimeDomain;
                        return scope;
                    }

                    //update befor
                    bool isFirstRun = _runtimeDomain == null;  //在下面才对 _runtimeDomian 赋值,而这里做一个开关?
                    if (!isFirstRun && _settupInfo.ModelChangedBefore != null)
                    {
                        if (_runtimeDomain.Scope.ModelAssembly != null) _settupInfo.ModelChangedBefore(_runtimeDomain.Scope.ModelAssembly);
                        TimeListener.Clear();
                        if (_runtimeDomain.MainInstance != null) _runtimeDomain.MainInstance.Stop();
                    }
                    runtimeDomain.MainInstance = runtimeDomain.Scope.Execute(_settupInfo.ScriptMainProgram, _settupInfo.ScriptMainTypeName) as IMainScript;  //从 _csharpAssembly 处获取 MainClass 的句柄
                    if (_runtimeDomain != null)
                    {
                        //unload pre-domain
                        _runtimeDomain.Dispose();
                    }
                    _runtimeDomain = runtimeDomain;  //在这里才对 _runtimeDomain 赋值是何意?
                    EntitySchemaSet.EntityAssembly = scope.ModelAssembly;
                    //update after
                    if (!isFirstRun && _settupInfo.ModelChangedAfter != null && scope.ModelAssembly != null)
                    {
                        _settupInfo.ModelChangedAfter(scope.ModelAssembly);
                    }
                    else if (scope.ModelAssembly != null)
                    {
                        ProtoBufUtils.LoadProtobufType(scope.ModelAssembly);  //比较重要的代码,Model文件夹下的代码也支持protobuf的继承
                        EntitySchemaSet.LoadAssembly(scope.ModelAssembly);
                    }
                    PrintCompiledMessage();
                    //replace runtime 
                    if (!isFirstRun && runtimeDomain.MainInstance != null)
                    {
                        runtimeDomain.MainInstance.ReStart();
                    }
                    return scope;
                }
                finally
                {
                    Interlocked.Exchange(ref _isCompiling, 0);
                }
            }
            else
            {
                TraceLog.WriteLine("{1} {0} has not compiled in other thread.", "model", DateTime.Now.ToString("HH:mm:ss"));
            }
            return null;
        }
       if (scope != null)
                {
                    InitScriptListener(scope.WatcherPaths);  //对脚本文件目录进行监控
                }
            }
            catch (Exception er)
            {
                IsError = true;
                TraceLog.WriteError("Script init error:{0}.", er);
                throw er;
            }
        }

   在之前使用 protobuf 结构的类中,是不能有继承的,否则反序列化会出问题,这里有所解决?到时候试验一下。

   单独来看一下 LoadAssembly:

        public static void LoadAssembly(Assembly assembly)
        {
            TraceLog.WriteLine("{0} Start checking table schema, please wait.", DateTime.Now.ToString("HH:mm:ss"));

            EntityAssembly = assembly;
            var types = assembly.GetTypes().Where(p => p.GetCustomAttributes(typeof(EntityTableAttribute), false).Count() > 0).ToList();  //实体必须包含 EntityTable
            foreach (var type in types)  //遍历所有实体类型
            {
                InitSchema(type);        //为每个类型构建数据库映射表
            }
            TraceLog.WriteLine("{0} Check table schema successfully.", DateTime.Now.ToString("HH:mm:ss"));
        }

  再看 InitSchema:

        public static SchemaTable InitSchema(Type type, bool isReset = false)
        {
            SchemaTable schema;
            if (!isReset && TryGet(type, out schema))
            {
                return schema;
            }
            schema = new SchemaTable();
            try
            {
                schema.IsEntitySync = type.GetCustomAttributes(typeof(EntitySyncAttribute), false).Length > 0;
                schema.EntityType = type;
                //加载表
                var entityTable = FindAttribute<EntityTableAttribute>(type.GetCustomAttributes(false));  //获取该实体类型的 EntityTable 的具体属性定义
                if (entityTable != null)  //并逐一复制给该实体类型对应 的 数据库映射结构
                {
                    schema.AccessLevel = entityTable.AccessLevel;    
                    schema.StorageType |= entityTable.StorageType;
                    schema.CacheType = entityTable.CacheType;
                    schema.IncreaseStartNo = entityTable.IncreaseStartNo;
                    schema.IsExpired = entityTable.IsExpired;
                    schema.EntityName = string.IsNullOrEmpty(entityTable.TableName) ? type.Name : entityTable.TableName;
                    schema.ConnectKey = string.IsNullOrEmpty(entityTable.ConnectKey) ? "" : entityTable.ConnectKey;
                    schema.Indexs = entityTable.Indexs;
                    schema.Condition = entityTable.Condition;
                    schema.OrderColumn = entityTable.OrderColumn;
                    //schema.DelayLoad = entityTable.DelayLoad;
                    schema.NameFormat = entityTable.TableNameFormat;
                    SetPeriodTime(schema);
                    //model other set
                    if (entityTable.IsExpired && entityTable.PeriodTime > 0)
                    {
                        schema.PeriodTime = entityTable.PeriodTime;
                    }
                    schema.Capacity = entityTable.Capacity;
                    schema.PersonalName = entityTable.PersonalName;

                    if (string.IsNullOrEmpty(schema.ConnectKey)
                        && type == typeof(EntityHistory))
                    {
                        schema.IsInternal = true;
                        var dbPair = DbConnectionProvider.Find(DbLevel.Game);
                        if (dbPair.Value == null)
                        {
                            dbPair = DbConnectionProvider.FindFirst();
                        }
                        if (dbPair.Value != null)
                        {
                            schema.ConnectKey = dbPair.Key;
                            schema.ConnectionProviderType = dbPair.Value.ProviderTypeName;
                            schema.ConnectionString = dbPair.Value.ConnectionString;
                        }
                        //else
                        //{
                        //    TraceLog.WriteWarn("Not found Redis's history record of db connect config.");
                        //}
                    }
                }
                InitSchema(type, schema);  //进一步分析该实体类型的成员属性, 由 EntityField 属性控制,来决定数据库表头每个字段的属性,将表结构加入全局静态 EntitySchemaSet 进行管理
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("InitSchema type:{0} error:\r\n{1}", type.FullName, ex);
            }
            //check cachetype
            if ((schema.CacheType == CacheType.Entity && !type.IsSubclassOf(typeof(ShareEntity))) ||
                (schema.CacheType == CacheType.Dictionary &&
                    schema.AccessLevel == AccessLevel.ReadWrite &&
                    !type.IsSubclassOf(typeof(BaseEntity)))
                )
            {
                throw new ArgumentException(string.Format("\"EntityTable.CacheType:{1}\" attribute of {0} class is error", type.FullName, schema.CacheType), "CacheType");
            }
            return schema;
        }

 

疑问:

  通篇代码阅读下来,一个疑问是“获取 MainClass 句柄后,通过 MainClass 调用其他脚本是一个怎么样的过程?”,因为好像并没有将它们同时加载在一个运行域上?

  看首次启动的代码:

        protected virtual async System.Threading.Tasks.Task RunAsync()
        {
        ...if (ScriptEngines.RunMainProgram())
        ...  await RunWait();
        }

  进一步加重了疑惑,这个疑问简而言之就是 ScriptRuntimeDomain 里的 AppDomain(运行域) 与 ScriptRuntimeScoe(程序集)并没有联系在一起?

  还是要回头检查代码或者知识储备。

 

8.18 补充:

在新建的 ScriptRuntimeDomain 中:

AppDomain 包含了 新的运行域;

runtimeDomain = new ScriptRuntimeDomain(typeof(ScriptRuntimeDomain).Name, new[] { _settupInfo.RuntimePrivateBinPath, ScriptCompiler.ScriptPath });
        public ScriptRuntimeDomain(string name, string[] privateBinPaths)
        {
            AppDomainSetup setup = new AppDomainSetup();
            setup.ApplicationName = name;
            setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            setup.PrivateBinPath = string.Join(";", privateBinPaths);
            setup.CachePath = setup.ApplicationBase;
            setup.ShadowCopyFiles = "true";
            setup.ShadowCopyDirectories = setup.ApplicationBase;
            InitDomain(name, setup);
        }

        private void InitDomain(string name, AppDomainSetup setup)
        {
#if STATIC

#else
            _currDomain = AppDomain.CreateDomain(name, null, setup);  //创建应用程序域
            var type = typeof(ScriptDomainContext);
            _context = (ScriptDomainContext)_currDomain.CreateInstanceFromAndUnwrap(type.Assembly.GetName().CodeBase, type.FullName);  //在新应用程序域里创建上下文的实例
#endif
        }

 

ScriptDomainContext 包含了 动态编译的脚本程序集 执行所依赖的程序集;

foreach (var assemblyName in _settupInfo.ReferencedAssemblyNames)
{
  //排除System的dll
  if (string.IsNullOrEmpty(assemblyName) ||
  !Path.IsPathRooted(assemblyName)) continue;
  string key = Path.GetFileNameWithoutExtension(assemblyName);
  runtimeDomain.LoadAssembly(key, assemblyName);  //在新的应用程序域里加载“脚本程序集”运行所需的依赖程序集
}

internal void LoadAssembly(string key, string assemblyName)
{
  Assembly.LoadFrom(assemblyName);
  _context.LoadAssembly(key, assemblyName);
}

 

ScriptRuntimeScope 包含了 动态编译的脚本程序集;

var scope = runtimeDomain.CreateScope(_settupInfo);

private ScriptRuntimeScope CreateRuntimeScope(ScriptSettupInfo settupInfo, string amsKey, Type type)
{
#if STATIC
  return type.CreateInstance<ScriptRuntimeScope>(settupInfo);
#else
  return _context.GetInstance(amsKey, type.FullName, settupInfo) as ScriptRuntimeScope;  //使用在新应用程序域里的“context实例”创建“脚本程序集容器Scope”
#endif
}
        private void Load()
        {
            var pathList = new String[] { _modelScriptPath, _csharpScriptPath };

            foreach (var path in pathList)
            {
                if (Directory.Exists(path))
                {
                    var files = Directory.GetFiles(path, FileFilter, SearchOption.AllDirectories);
                    if (files.Length > 0)
                    {
                        LoadScriptAssemblyInfo(path);  
                    }
                    foreach (var fileName in files)
                    {
                        LoadScript(path, fileName);  加载脚本文件
                    }
                }
            }
            Compile();  //新应用程序域:编译脚本文件生成程序集 存在新应用程序域中 ScriptRunTimeScope 的内存中
            BuildPythonReferenceFile();
        }

 

脚本入口句柄:

runtimeDomain.MainInstance = runtimeDomain.Scope.Execute(_settupInfo.ScriptMainProgram, _settupInfo.ScriptMainTypeName) as IMainScript;

最后调用到:

        private bool CreateInstance(string scriptCode, string typeName, object[] args, out object result)
        {
            result = null;
            Type type;
            if (TryParseType(scriptCode, typeName, out type))
            {
                result = type.CreateInstance(args);  //从scope入口进入的是新的应用程序域,反射出获取“MainClass”脚本入口
                return true;
            }
            return false;
        }

 

产生了新的关于应用程序域的疑问?

  函数应该是执行了线程中,线程中一部分执行的是A应用程序域的程序集,一部分执行的是B应用程序域的程序集,但是从线程概念来说,一个函数用的是同一份内存。

  但不是说应用程序域之间使用的内存是独立的吗?-- 一个A应用程序域的对象,里面包含了一个B应用程序域的对象是可以的吗?

 

  继续来看一下现象:

Console.WriteLine("Current domain name = {0}.", AppDomain.CurrentDomain.FriendlyName);
runtimeDomain = new ScriptRuntimeDomain(typeof(ScriptRuntimeDomain).Name, new[] { _settupInfo.RuntimePrivateBinPath, ScriptCompiler.ScriptPath });
public override object Execute(string scriptCode, string typeName, params object[] args)
{
  string code = FormatScriptCode(SettupInfo.CSharpScriptPath, scriptCode, ".cs");
  object result;
  if (CreateInstance(code, typeName, args, out result)) return result;
  return base.Execute(scriptCode, typeName, args);
}

  可以看到 Scope 里运行的代码的应用程序域名 是新域名,还是验证了我的猜想。

  总体可以理解为,可执行文件运行的是主应用程序域,在主程序域创建其他程序域,并加载程序集,需要使用“透明代理”才能在主程序域运行

  而应用程序域应该有自己的 “堆栈上下文”,应用程序域之间的“代理”,可用“值传递”或“引用传递”的方式传递参数,这就能解决 “一个A应用程序域的对象,里面包含了一个B应用程序域的对象” 这个问题。

 

正如改图所示(两种上下文的概念有空再补齐):

 

posted on 2016-08-16 22:32  青墨淡潋  阅读(1219)  评论(0)    收藏  举报