public void Start() { try { var reload = PackageManager.Packages != null && PackageManager.Packages.Count > 0; if (!reload) { Console.WriteLine($"[{nameof(PluginManager)}] START"); } else { Console.WriteLine($"[{nameof(PluginManager)}] RESTART"); } loadPackages(); var packages = PackageManager.Packages.ToArray(); var paths = PackageManager.Packages.ConvertAll(package => package.Path.Split('\\').Last()).ToArray(); var engine = Python.CreateEngine(); var searchPaths = engine.GetSearchPaths(); searchPaths.Add(PackageManager.RootDir); engine.SetSearchPaths(searchPaths); Console.WriteLine($"[{nameof(PluginManager)}] Executing packages"); var scope = engine.Runtime.CreateScope(); scope.SetVariable("GameDir", PackageManager.GameDir); scope.SetVariable("Globals", engine.Runtime.Globals.GetVariableNames()); scope.SetVariable("Packages", packages); scope.SetVariable("PluginManager", this); scope.SetVariable("RootDir", PackageManager.RootDir); scope.SetVariable("Versions", typeof(Versions)); CSharpCodeProvider csProvider = new CSharpCodeProvider(); CompilerParameters csParams = new CompilerParameters(); csParams.GenerateInMemory = true; csParams.GenerateExecutable = false; csParams.ReferencedAssemblies.Add(Assembly.GetAssembly(typeof(Game.GameStates.GameShipEditor)).Location); csParams.ReferencedAssemblies.Add(Assembly.GetAssembly(typeof(Game.Framework.IUpdatable)).Location); csParams.ReferencedAssemblies.Add(Assembly.GetAssembly(typeof(PluginClass)).Location); PluginGlobal csGlobal = new PluginGlobal(PackageManager.GameDir, packages, this, PackageManager.RootDir); PackageManager.Packages.ForEach((Package p) => { try { if (p.Metadata.PackageType == PackageType.Python) { InitPythonPlugin(p, ref engine, ref scope); } else if (p.Metadata.PackageType == PackageType.CScript) { InitCScriptPlugin(p, ref csProvider, ref csParams, ref csGlobal); } else { throw new InvalidOperationException("Missing or invalid PackageType in plugin.json"); } } catch (Exception ex) { Console.WriteLine($"[{nameof(PluginManager)}] Error while executing {p.Path.Split('\\').Last()}"); Console.WriteLine(ex); } }); Console.WriteLine($"[{nameof(PluginManager)}] All packages invoked"); if (reload && gameClient != null) { OnClientHotReload?.Invoke(gameClient); } if (reload && gameEditor != null) { OnEditorHotReload?.Invoke(gameEditor); } if (reload && gameMenu != null) { OnMenuHotReload?.Invoke(gameMenu); } if (reload && gameServer != null) { OnServerHotReload?.Invoke(gameServer); } } catch (Exception ex) { Console.WriteLine($"[{nameof(PluginManager)}] Error while starting plugins"); Console.WriteLine(ex); } }
public void Start() { try { var alreadyStarted = PackageManager.Packages != null && PackageManager.Packages.Any(); if (!alreadyStarted) { Log.Info("Starting"); } else { Log.Info("Restarting"); } loadPackages(); var packages = PackageManager.Packages.ToArray(); Log.Info("Executing packages"); var executionError = false; PackageManager.Packages .ForEach(package => { ScriptRunner scriptRunner = null; if (package.Metadata.Type == PackageType.Python) { scriptRunner = new PYScriptRunner(); } else if (package.Metadata.Type == PackageType.Lua) { scriptRunner = new LuaScriptRunner(); } else { throw new NotImplementedException($"Package type not supported: {package.Metadata.Type.ToString()}"); } var scriptGlobals = new ScriptGlobal { GameDirectory = PackageManager.GameDirectory, PackageDirectory = package.Path, Packages = packages, PluginManager = this, Versions = typeof(Versions) }; try { scriptRunner.CreateScope(scriptGlobals); scriptRunner.ExecuteScript(System.IO.Path.Combine(package.Path, package.Metadata.EntryPoint)); if (scriptRunner.Warnings.Any()) { scriptRunner.Warnings.ToList().ForEach(warning => Log.Warning(warning)); } } catch (Exception exception) { executionError = true; Log.Error($"Error while executing {package.Metadata.ToString()}"); if (scriptRunner.Errors.Any()) { scriptRunner.Errors.ToList().ForEach(error => Log.Error(error)); } else { Log.Exception(exception); } } }); if (executionError) { Log.Error("Please resolve issues and restart the game"); Console.ReadLine(); } else { Log.Info("Packages executed"); } if (alreadyStarted && gameClient != null) { OnClientHotReload?.Invoke(gameClient); } if (alreadyStarted && gameEditor != null) { OnEditorHotReload?.Invoke(gameEditor); } if (alreadyStarted && gameMenu != null) { OnMenuHotReload?.Invoke(gameMenu); } if (alreadyStarted && gameServer != null) { OnServerHotReload?.Invoke(gameServer); } } catch (Exception exception) { Log.Error($"Error while starting {nameof(PluginLoader)}"); Sentry.Exception(exception); } }