Exemplo n.º 1
0
 /// <summary>
 /// Dispose
 /// </summary>
 public static void Dispose()
 {
     _changeWatchingTimer.Dispose();
     _runtimeDomain.Dispose();
 }
Exemplo n.º 2
0
        private static ScriptRuntimeScope InitScriptRuntimeScope()
        {
            //star compile
            if (Interlocked.Exchange(ref _isCompiling, 1) == 0)
            {
                ScriptRuntimeDomain runtimeDomain = null;
                try
                {
                    string runtimePath = MathUtils.RuntimePath ?? MathUtils.RuntimeBinPath;
                    AppDomain.CurrentDomain.AppendPrivatePath(ScriptCompiler.ScriptPath);
                    runtimeDomain = new ScriptRuntimeDomain(typeof(ScriptRuntimeDomain).Name, new[] { _settupInfo.RuntimePrivateBinPath, ScriptCompiler.ScriptPath });
                    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);
                    }
                    var scope = runtimeDomain.CreateScope(_settupInfo);
                    //ignore error, allow model is empty.
                    if (scope == null)
                    {
                        if (_runtimeDomain == null)
                        {
                            _runtimeDomain = runtimeDomain;
                        }
                        return(scope);
                    }

                    //update befor
                    bool isFirstRun = _runtimeDomain == null;
                    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;
                    if (_runtimeDomain != null)
                    {
                        //unload pre-domain
                        _runtimeDomain.Dispose();
                    }
                    _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);
                        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);
        }