Example #1
0
        private static void UpdateAssembly(string name, string[] filenames, FileWatcherInfo watcherInfo)
        {
            string assemblyName = string.Format("DynamicScripts.{0}", name);
            var    assembly     = GenerateCsharpScriptAssembly(name, filenames, assemblyName, watcherInfo);
            var    obj          = _watcherDict[name];

            if (obj != null && assembly != null)
            {
                obj.Assembly = assembly;
                var pairs = _watcherDict.Where(pair => !pair.Value.IsPython && pair.Value.ReferenceKeys.Contains(name)).ToList();
                foreach (var pair in pairs)
                {
                    string refName = pair.Key;
                    var    files   = _scriptCodeCache.Where(p => p.Value.GroupName == refName).Select(t => t.Value.FileName).ToArray();
                    UpdateAssembly(refName, files, pair.Value);
                }
            }
        }
Example #2
0
        static ScriptEngines()
        {
            //init object.
            _pythonOptions = new Dictionary<string, object>();
            _watcherDict = new DictionaryExtend<string, FileWatcherInfo>();
            _scriptCodeCache = new DictionaryExtend<string, ScriptFileInfo>();
            _changedFiles = new HashSet<string>();

            //init runtime path.
            _runtimePath = MathUtils.RuntimePath;
            _runtimeBinPath = MathUtils.RuntimeBinPath;
            if (string.IsNullOrEmpty(_runtimeBinPath))
            {
                _runtimeBinPath = _runtimePath;
            }
            _relativeDirName = ConfigUtils.GetSetting("ScriptRelativePath", "");
            _disablePython = ConfigUtils.GetSetting("Python_Disable", false);
            _scriptIsDebug = ConfigUtils.GetSetting("Script_IsDebug", false);
            SetPythonDebug = ConfigUtils.GetSetting("Python_IsDebug", _scriptIsDebug);
            CSharpDirName = ConfigUtils.GetSetting("CSharpRootPath", "Script");
            ScriptMainClass = ConfigUtils.GetSetting("ScriptMainClass", Path.Combine(CSharpDirName, "MainClass.cs"));
            ScriptMainTypeName = ConfigUtils.GetSetting("ScriptMainTypeName", "");

            //init script dir.
            var directorys = new[]
            {
                string.Format("{0};{1}", ModelDirName, false),
                string.Format("{0};{1};{2}", CSharpDirName, false, ModelDirName)
            };
            foreach (string temp in directorys)
            {
                var arr = temp.Split(';');
                string dirName = arr[0];
                bool isMemory = arr.Length > 1 ? arr[1].ToBool() : false;
                string[] refArr = arr.Length > 2 ? arr[2].Split(',') : new string[0];
                _watcherDict[dirName] = new FileWatcherInfo()
                {
                    Path = Path.Combine(_runtimePath, _relativeDirName, dirName),
                    Filter = "*.cs",
                    CompileLevel = dirName == ModelDirName ? 9 : 3,
                    IsInMemory = isMemory,
                    ReferenceKeys = refArr
                };
            }
            if (!_disablePython)
            {
                PythonDirName = ConfigUtils.GetSetting("PythonRootPath", "PyScript");
                ReferenceLibFile = Path.Combine(_runtimePath, _relativeDirName, PythonDirName, ConfigUtils.GetSetting("ReferenceLibFile", "Lib/ReferenceLib.py"));
                string path = Path.GetDirectoryName(ReferenceLibFile);
                if (!string.IsNullOrEmpty(path) && !Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                _watcherDict[PythonDirName] = new FileWatcherInfo()
                {
                    IsPython = true,
                    CompileLevel = 0,
                    Path = Path.Combine(_runtimePath, _relativeDirName, PythonDirName),
                    Filter = "*.py"
                };
            }

            //init Assemblies
            _referencedAssemblies = new HashSet<string>(new[]
            {
                Path.Combine(_runtimeBinPath, "NLog.dll"),
                Path.Combine(_runtimeBinPath, "Newtonsoft.Json.dll"),
                Path.Combine(_runtimeBinPath, "protobuf-net.dll"),
                Path.Combine(_runtimeBinPath, "ServiceStack.Redis.dll"),
                Path.Combine(_runtimeBinPath, "ZyGames.Framework.Common.dll"),
                Path.Combine(_runtimeBinPath, "ZyGames.Framework.dll")
            });
        }
Example #3
0
 private static void UpdateAssembly(string name, string[] filenames, FileWatcherInfo watcherInfo)
 {
     string assemblyName = string.Format("DynamicScripts.{0}", name);
     var assembly = GenerateCsharpScriptAssembly(name, filenames, assemblyName, watcherInfo);
     var obj = _watcherDict[name];
     if (obj != null && assembly != null)
     {
         obj.Assembly = assembly;
         var pairs = _watcherDict.Where(pair => !pair.Value.IsPython && pair.Value.ReferenceKeys.Contains(name)).ToList();
         foreach (var pair in pairs)
         {
             string refName = pair.Key;
             var files = _scriptCodeCache.Where(p => p.Value.GroupName == refName).Select(t => t.Value.FileName).ToArray();
             UpdateAssembly(refName, files, pair.Value);
         }
     }
 }
Example #4
0
 /// <summary>
 /// ����CSharp�ű�����
 /// </summary>
 private static Assembly GenerateCsharpScriptAssembly(string name, string[] fileNames, string assemblyName, FileWatcherInfo watcherInfo)
 {
     if (fileNames.Length > 0)
     {
         var refAssemblies = _referencedAssemblies.ToList();
         foreach (var refKey in watcherInfo.ReferenceKeys)
         {
             string assmPath = _watcherDict[refKey].AssemblyOutPath;
             if (!string.IsNullOrEmpty(assmPath))
             {
                 refAssemblies.Add(assmPath);
             }
         }
         bool inMemory = watcherInfo.IsInMemory;
         if (name == ModelDirName)
         {
             string pathToAssembly;
             var assm = ScriptCompiler.InjectionCompile(fileNames, refAssemblies.ToArray(), assemblyName, _scriptIsDebug, inMemory, out pathToAssembly);
             watcherInfo.AssemblyOutPath = pathToAssembly;
             //load parent class propertys.
             if (assm != null)
             {
                 ProtoBufUtils.LoadProtobufType(assm);
                 if (watcherInfo.Assembly == null)
                 {
                     //first
                     EntitySchemaSet.LoadAssembly(assm);
                 }
             }
             return assm;
         }
         var result = ScriptCompiler.Compile(fileNames, refAssemblies.ToArray(), assemblyName, _scriptIsDebug, inMemory, ScriptCompiler.ScriptAssemblyTemp);
         if (result != null)
         {
             watcherInfo.AssemblyOutPath = result.PathToAssembly;
             return result.CompiledAssembly;
         }
     }
     return null;
 }
Example #5
0
 /// <summary>
 /// ����CSharp�ű�����
 /// </summary>
 private static Assembly GenerateCsharpScriptAssembly(string name, string[] fileNames, string assemblyName, FileWatcherInfo watcherInfo)
 {
     if (fileNames.Length > 0)
     {
         var refAssemblies = _referencedAssemblies.ToList();
         foreach (var refKey in watcherInfo.ReferenceKeys)
         {
             string assmPath = _watcherDict[refKey].AssemblyOutPath;
             if (!string.IsNullOrEmpty(assmPath))
             {
                 refAssemblies.Add(assmPath);
             }
         }
         bool inMemory = watcherInfo.IsInMemory;
         if (name == ModelDirName)
         {
             string pathToAssembly;
             var assm = ScriptCompiler.InjectionCompile(fileNames, refAssemblies.ToArray(), assemblyName, _scriptIsDebug, inMemory, out pathToAssembly);
             watcherInfo.AssemblyOutPath = pathToAssembly;
             return assm;
         }
         var result = ScriptCompiler.Compile(fileNames, refAssemblies.ToArray(), assemblyName, _scriptIsDebug, inMemory);
         if (result != null)
         {
             watcherInfo.AssemblyOutPath = result.PathToAssembly;
             return result.CompiledAssembly;
         }
     }
     return null;
 }
Example #6
0
        static ScriptEngines()
        {
            //init object.
            _pythonOptions   = new Dictionary <string, object>();
            _watcherDict     = new DictionaryExtend <string, FileWatcherInfo>();
            _scriptCodeCache = new DictionaryExtend <string, ScriptFileInfo>();
            _changedFiles    = new HashSet <string>();

            //init runtime path.
            _runtimePath    = MathUtils.RuntimePath;
            _runtimeBinPath = MathUtils.RuntimeBinPath;
            if (string.IsNullOrEmpty(_runtimeBinPath))
            {
                _runtimeBinPath = _runtimePath;
            }
            _relativeDirName   = ConfigUtils.GetSetting("ScriptRelativePath", "");
            _disablePython     = ConfigUtils.GetSetting("Python_Disable", false);
            _scriptIsDebug     = ConfigUtils.GetSetting("Script_IsDebug", false);
            SetPythonDebug     = ConfigUtils.GetSetting("Python_IsDebug", _scriptIsDebug);
            CSharpDirName      = ConfigUtils.GetSetting("CSharpRootPath", "Script");
            ScriptMainClass    = ConfigUtils.GetSetting("ScriptMainClass", Path.Combine(CSharpDirName, "MainClass.cs"));
            ScriptMainTypeName = ConfigUtils.GetSetting("ScriptMainTypeName", "");

            //init script dir.
            var directorys = new[]
            {
                string.Format("{0};{1}", ModelDirName, false),
                string.Format("{0};{1};{2}", CSharpDirName, false, ModelDirName)
            };

            foreach (string temp in directorys)
            {
                var      arr      = temp.Split(';');
                string   dirName  = arr[0];
                bool     isMemory = arr.Length > 1 ? arr[1].ToBool() : false;
                string[] refArr   = arr.Length > 2 ? arr[2].Split(',') : new string[0];
                _watcherDict[dirName] = new FileWatcherInfo()
                {
                    Path          = Path.Combine(_runtimePath, _relativeDirName, dirName),
                    Filter        = "*.cs",
                    CompileLevel  = dirName == ModelDirName ? 9 : 3,
                    IsInMemory    = isMemory,
                    ReferenceKeys = refArr
                };
            }
            if (!_disablePython)
            {
                PythonDirName    = ConfigUtils.GetSetting("PythonRootPath", "PyScript");
                ReferenceLibFile = Path.Combine(_runtimePath, _relativeDirName, PythonDirName, ConfigUtils.GetSetting("ReferenceLibFile", "Lib/ReferenceLib.py"));
                string path = Path.GetDirectoryName(ReferenceLibFile);
                if (!string.IsNullOrEmpty(path) && !Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                _watcherDict[PythonDirName] = new FileWatcherInfo()
                {
                    IsPython     = true,
                    CompileLevel = 0,
                    Path         = Path.Combine(_runtimePath, _relativeDirName, PythonDirName),
                    Filter       = "*.py"
                };
            }

            //init Assemblies
            _referencedAssemblies = new HashSet <string>(new[]
            {
                Path.Combine(_runtimeBinPath, "NLog.dll"),
                Path.Combine(_runtimeBinPath, "Newtonsoft.Json.dll"),
                Path.Combine(_runtimeBinPath, "protobuf-net.dll"),
                Path.Combine(_runtimeBinPath, "ServiceStack.Redis.dll"),
                Path.Combine(_runtimeBinPath, "ZyGames.Framework.Common.dll"),
                Path.Combine(_runtimeBinPath, "ZyGames.Framework.dll")
            });
        }
Example #7
0
 /// <summary>
 /// 生成CSharp脚本程序集
 /// </summary>
 private static Assembly GenerateCsharpScriptAssembly(string name, string[] fileNames, string assemblyName, FileWatcherInfo watcherInfo)
 {
     if (fileNames.Length > 0)
     {
         var refAssemblies = _referencedAssemblies.ToList();
         foreach (var refKey in watcherInfo.ReferenceKeys)
         {
             string assmPath = _watcherDict[refKey].AssemblyOutPath;
             if (!string.IsNullOrEmpty(assmPath))
             {
                 refAssemblies.Add(assmPath);
             }
         }
         bool inMemory = watcherInfo.IsInMemory;
         if (name == ModelDirName)
         {
             string pathToAssembly;
             var    assm = ScriptCompiler.InjectionCompile(fileNames, refAssemblies.ToArray(), assemblyName, _scriptIsDebug, inMemory, out pathToAssembly);
             watcherInfo.AssemblyOutPath = pathToAssembly;
             //load parent class propertys.
             if (assm != null)
             {
                 ProtoBufUtils.LoadProtobufType(assm);
             }
             return(assm);
         }
         var result = ScriptCompiler.Compile(fileNames, refAssemblies.ToArray(), assemblyName, _scriptIsDebug, inMemory, ScriptCompiler.ScriptAssemblyTemp);
         if (result != null)
         {
             watcherInfo.AssemblyOutPath = result.PathToAssembly;
             return(result.CompiledAssembly);
         }
     }
     return(null);
 }