private void Application_OnPlayfieldUnloading(IPlayfield playfield) { ScriptingModScriptsInfoData.TryRemove(playfield.Name, out _); if (!PlayfieldData.TryRemove(playfield.Name, out var data)) { return; } data.Playfield.OnEntityLoaded -= data.Playfield_OnEntityLoaded; data.Playfield.OnEntityUnloaded -= data.Playfield_OnEntityUnloaded; ModApi.Log($"PauseScripts for {playfield.Name} {(data.PauseScripts ? "always stopped" : "scripts running")}"); data.PauseScripts = true; DisplayScriptInfos(); data.ScriptExecQueue?.Clear(); data.LcdCompileCache?.Clear(); data.PersistendData?.Clear(); var stores = data.EventStore?.Values.ToArray(); data.EventStore?.Clear(); stores?.ForEach(S => ((EventStore)S).Dispose()); }
private void Log(string text, LogLevel level) { if (Configuration?.Current.LogLevel <= level) { ModApi.Log(text); } }
private void Application_OnPlayfieldLoaded(IPlayfield playfield) { PlayfieldScriptData data = null; InitGameDependedData(ModApi.Application.Mode == ApplicationMode.SinglePlayer); PlayfieldData.TryAdd(playfield.Name, data = new PlayfieldScriptData(this) { PlayfieldName = playfield.Name, Playfield = playfield, }); UpdateScriptingModInfoData(); ModApi.Log($"StartScripts for {playfield.Name} pending"); TaskTools.Delay(Configuration.Current.DelayStartForNSecondsOnPlayfieldLoad, () => { ModApi.Log($"StartScripts for {playfield.Name}"); data.PauseScripts = false; if (ModApi.Application.Mode == ApplicationMode.SinglePlayer) { ModApi.Log(playfield.Entities?.Aggregate($"Player:{playfield.Players.FirstOrDefault().Value?.Name} PlayerDriving:{playfield.Players.FirstOrDefault().Value?.DrivingEntity?.Name}", (L, E) => L + $" {E.Key}:{E.Value.Name}")); data.AddEntity(playfield.Players.FirstOrDefault().Value?.DrivingEntity); playfield.Entities?.ForEach(E => data.AddEntity(E.Value)); } }); data.Playfield.OnEntityLoaded += data.Playfield_OnEntityLoaded; data.Playfield.OnEntityUnloaded += data.Playfield_OnEntityUnloaded; }
private void Application_OnPlayfieldLoaded(string playfieldName) { ModApi.Log($"StartScripts for {playfieldName} pending"); TaskTools.Delay(Configuration.Current.DelayStartForNSecondsOnPlayfieldLoad, () => { ModApi.Log($"StartScripts for {playfieldName}"); PauseScripts = false; }); }
public static void RestartAllScriptsForPlayfieldServer() { StopScriptsEvent?.Invoke(EmpyrionScriptingInstance, EventArgs.Empty); EmpyrionScriptingInstance.PauseScripts = false; Configuration.Load(); ModApi?.Log($"EmpyrionScripting Mod.Restart Threads: {EmpyrionScriptingInstance.LastAlive} <-> {DateTime.Now} : {Configuration.Current.LogLevel}"); EmpyrionScriptingInstance.StartAllScriptsForPlayfieldServer(); }
private void Application_GameEntered(bool hasEntered) { ModApi.Log($"Application_GameEntered {hasEntered}"); if (hasEntered) { InitGameDependedData(false); } ModApi.Log("Application_GameEntered init finish"); }
private void Application_OnPlayfieldUnloaded(string playfieldName) { ModApi.Log($"PauseScripts for {playfieldName} {(PauseScripts ? "always stopped" : "scripts running")}"); PauseScripts = true; DisplayScriptInfos(); ScriptExecQueue.Clear(); LcdCompileCache.Clear(); PersistendData.Clear(); }
private void CsCompiler_ConfigurationChanged(object sender, EventArgs e) { PlayfieldData.ForEach(P => { ModApi.Log($"CsCompiler_ConfigurationChanged: {P.Key} {(P.Value == null ? "null" : (P.Value.PauseScripts ? "always stopped" : "scripts running"))}"); DisplayScriptInfos(); P.Value?.ScriptExecQueue?.Clear(); P.Value?.LcdCompileCache?.Clear(); }); }
private int UpdateScripts(PlayfieldScriptData playfieldData, Func <PlayfieldScriptData, IEntity, int> process, string name) { try { if (playfieldData.Playfield == null) { ModApi.Log($"UpdateScripts no Playfield"); return(0); } if (playfieldData.Playfield.Entities == null) { ModApi.Log($"UpdateScripts no Entities"); return(0); } var timer = new Stopwatch(); timer.Start(); playfieldData.AllEntities = playfieldData.Playfield.Entities.Values.ToArray(); playfieldData.CurrentEntities = playfieldData.AllEntities .Where(E => E.Type == EntityType.BA || E.Type == EntityType.CV || E.Type == EntityType.SV || E.Type == EntityType.HV) .ToArray(); Log($"CurrentEntities: {playfieldData.CurrentEntities.Length}", LogLevel.Debug); playfieldData.EntityCultureInfo.Clear(); int count = 0; playfieldData.CurrentEntities.ForEach(E => count += process(playfieldData, E)); timer.Stop(); if (timer.Elapsed.TotalSeconds > 30) { Log($"UpdateScripts: {name} RUNS {timer.Elapsed} !!!!", LogLevel.Message); } else { Log($"UpdateScripts: {name} take {timer.Elapsed}", LogLevel.Debug); } return(count); } catch (Exception error) { ModApi.LogWarning("Next try because: " + ErrorFilter(error)); return(0); } }
public void StartAllScriptsForPlayfieldServer() { ModApi.Log($"StartAllScriptsForPlayfieldServer: InGame:{Configuration.Current.InGameScriptsIntervallMS}ms SaveGame:{Configuration.Current.SaveGameScriptsIntervallMS}ms "); StartScriptIntervall(Configuration.Current.InGameScriptsIntervallMS, () => { PlayfieldData.Values.ForEach(PF => { Log($"InGameScript: {PF.PauseScripts}", LogLevel.Debug); LastAlive = DateTime.Now; if (PF.PauseScripts) { return; } InGameScriptsCount = UpdateScripts(PF, ProcessAllInGameScripts, "InGameScript"); PF.ScriptExecQueue.ScriptsCount = InGameScriptsCount + SaveGameScriptsCount; }); }, "InGameScript"); StartScriptIntervall(Configuration.Current.SaveGameScriptsIntervallMS, () => { PlayfieldData.Values.ForEach(PF => { Log($"SaveGameScript: {PF.PauseScripts}", LogLevel.Debug); LastAlive = DateTime.Now; if (PF.PauseScripts) { return; } SaveGameScriptsCount = UpdateScripts(PF, ProcessAllSaveGameScripts, "SaveGameScript"); PF.ScriptExecQueue.ScriptsCount = InGameScriptsCount + SaveGameScriptsCount; }); }, "SaveGameScript"); StartScriptIntervall(60000, () => { PlayfieldData.Values.ForEach(PF => { Log($"ScriptInfos: Pause:{PF.PauseScripts} {PF.ScriptExecQueue.ScriptRunInfo.Count} ExecQueue:{PF.ScriptExecQueue.ExecQueue.Count} WaitForExec:{PF.ScriptExecQueue.WaitForExec.Count}", LogLevel.Debug); LastAlive = DateTime.Now; if (PF.PauseScripts || Configuration.Current.LogLevel > LogLevel.Message) { return; } DisplayScriptInfos(); PF.ScriptExecQueue.CheckForEmergencyRestart(PF); }); }, "ScriptInfos"); }
public static void Log(string text, LogLevel level) { if (Configuration?.Current.LogLevel <= level) { switch (level) { case LogLevel.Debug: ModApi?.Log(text); break; case LogLevel.Message: ModApi?.Log(text); break; case LogLevel.Error: ModApi?.LogError(text); break; default: ModApi?.Log(text); break; } } }
public void StartAllScriptsForPlayfieldServer() { ModApi.Log($"StartAllScriptsForPlayfieldServer: InGame:{Configuration.Current.InGameScriptsIntervallMS}ms SaveGame:{Configuration.Current.SaveGameScriptsIntervallMS}ms "); StartScriptIntervall(Configuration.Current.InGameScriptsIntervallMS, () => { Log($"InGameScript: {PauseScripts} #{CycleCounter}", LogLevel.Debug); LastAlive = DateTime.Now; if (PauseScripts) { return; } Interlocked.Increment(ref CycleCounter); InGameScriptsCount = UpdateScripts(ProcessAllInGameScripts, "InGameScript"); ScriptExecQueue.ScriptsCount = InGameScriptsCount + SaveGameScriptsCount; }, "InGameScript"); StartScriptIntervall(Configuration.Current.SaveGameScriptsIntervallMS, () => { Log($"SaveGameScript: {PauseScripts}", LogLevel.Debug); LastAlive = DateTime.Now; if (PauseScripts) { return; } SaveGameScriptsCount = UpdateScripts(ProcessAllSaveGameScripts, "SaveGameScript"); ScriptExecQueue.ScriptsCount = InGameScriptsCount + SaveGameScriptsCount; }, "SaveGameScript"); StartScriptIntervall(60000, () => { Log($"ScriptInfos: {ScriptExecQueue.ScriptRunInfo.Count} ExecQueue:{ScriptExecQueue.ExecQueue.Count} WaitForExec:{ScriptExecQueue.WaitForExec.Count}", LogLevel.Debug); LastAlive = DateTime.Now; if (PauseScripts || Configuration.Current.LogLevel > LogLevel.Message) { return; } DisplayScriptInfos(); }, "ScriptInfos"); }
private static void InitGameDependedData(bool forceInit) { if (forceInit || !AppFoldersLogged) { AppFoldersLogged = true; ModApi.Log($"InitGameDependedData [ForceInit:{forceInit}]:\n" + $"AppFolder.Content:{ModApi.Application?.GetPathFor(AppFolder.Content)}\n" + $"AppFolder.Mod:{ModApi.Application?.GetPathFor(AppFolder.Mod)}\n" + $"AppFolder.Root:{ModApi.Application?.GetPathFor(AppFolder.Root)}\n" + $"AppFolder.SaveGame:{ModApi.Application?.GetPathFor(AppFolder.SaveGame)}\n" + $"AppFolder.Dedicated:{ModApi.Application?.GetPathFor(AppFolder.Dedicated)}\n" + $"AppFolder.Cache:{ModApi.Application?.GetPathFor(AppFolder.Cache)}\n" + $"AppFolder.ActiveScenario:{ModApi.Application?.GetPathFor(AppFolder.ActiveScenario)} -> CurrentScenario:{CurrentScenario}"); } if (forceInit || Localization == null) { Localization = new Localization(ModApi.Application?.GetPathFor(AppFolder.Content), CurrentScenario); } if (forceInit || ConfigEcfAccess == null) { InitEcfConfigData(); } }
public void Game_Exit() { ModApi.Log("Mod exited:Game_Exit"); StopScriptsEvent?.Invoke(this, EventArgs.Empty); }
private int ProcessAllInGameScripts(IEntity entity) { Log($"ProcessAllInGameScripts: {entity.Name}:{entity.Type} Pause:{PauseScripts}", LogLevel.Debug); if (entity.Type == EntityType.Proxy || PauseScripts) { return(0); } try { var entityScriptData = new ScriptRootData(CurrentEntities, ModApi.Playfield, entity, DeviceLockAllowed, PersistendData); var deviceNames = entityScriptData.E.S.AllCustomDeviceNames.Where(N => N.StartsWith(ScriptKeyword)).ToArray(); Log($"ProcessAllInGameScripts: #{deviceNames.Length}", LogLevel.Debug); int count = 0; Parallel.ForEach(deviceNames, N => { if (PauseScripts) { return; } var lcd = entity.Structure.GetDevice <ILcd>(N); if (lcd == null) { return; } try { Log($"ProcessAllInGameScripts: {N}", LogLevel.Debug); var data = new ScriptRootData(entityScriptData) { Script = lcd.GetText(), Error = L, }; AddTargetsAndDisplayType(data, N.Substring(ScriptKeyword.Length)); if (Configuration.Current.ScriptTracking) { var trackfile = GetTrackingFileName(entity, data.Script.GetHashCode().ToString()); if (!File.Exists(trackfile)) { File.WriteAllText(trackfile, data.Script); } } data.ScriptId = entity.Id + "/" + N; ScriptExecQueue.Add(data); Interlocked.Increment(ref count); } catch (Exception lcdError) { if (Configuration.Current.LogLevel >= EmpyrionNetAPIDefinitions.LogLevel.Debug) { ModApi.Log($"UpdateLCDs ({entity.Id}/{entity.Name}):LCD: {lcdError}"); } } }); return(count); } catch (Exception error) { File.WriteAllText(GetTrackingFileName(entity, string.Empty) + ".error", error.ToString()); return(0); } }
public void Dispose() { ModApi?.Log("EmpyrionScripting Mod: Dispose"); }
public void Shutdown() { ModApi.Log("Mod exited:Shutdown"); StopScriptsEvent.Invoke(this, EventArgs.Empty); }