/// <summary> /// Initializes if needed, issues updates. /// </summary> public override void UpdateAfterSimulation() { MainLock.MainThread_ReleaseExclusive(); try { switch (ManagerStatus) { case Status.Not_Initialized: Init(); return; case Status.Initialized: if (ServerSettings.ServerSettingsLoaded) { Log.DebugLog("Server settings loaded"); Start(); } return; case Status.Terminated: return; } Dictionary <Action, uint> Unregister = null; if (AddRemoveActions.Count != 0) { try { AddRemoveActions.PopHeadInvokeAll(); } catch (Exception ex) { Log.AlwaysLog("Exception in AddRemoveActions: " + ex, Logger.severity.ERROR); } } if (ExternalRegistrations.Count != 0) { try { ExternalRegistrations.PopHeadInvokeAll(); } catch (Exception ex) { Log.AlwaysLog("Exception in ExternalRegistrations: " + ex, Logger.severity.ERROR); } } foreach (KeyValuePair <uint, List <Action> > pair in UpdateRegistrar) { if (Globals.UpdateCount % pair.Key == 0) { foreach (Action item in pair.Value) { try { Profiler.StartProfileBlock(item); item.Invoke(); Profiler.EndProfileBlock(); } catch (Exception ex2) { if (Unregister == null) { Unregister = new Dictionary <Action, uint>(); } if (!Unregister.ContainsKey(item)) { Log.AlwaysLog("Script threw exception, unregistering: " + ex2, Logger.severity.ERROR); Logger.DebugNotify("A script has been terminated", 10000, Logger.severity.ERROR); Unregister.Add(item, pair.Key); } } } } } if (Unregister != null) { foreach (KeyValuePair <Action, uint> pair in Unregister) { UnRegisterForUpdates(pair.Value, pair.Key); } } } catch (Exception ex) { Log.AlwaysLog("Exception: " + ex, Logger.severity.FATAL); ManagerStatus = Status.Terminated; } finally { Globals.UpdateCount++; float instantSimSpeed = Globals.UpdateDuration / (float)(DateTime.UtcNow - m_lastUpdate).TotalSeconds; if (instantSimSpeed > 0.01f && instantSimSpeed < 1.1f) { Globals.SimSpeed = Globals.SimSpeed * 0.9f + instantSimSpeed * 0.1f; } //Log.DebugLog("instantSimSpeed: " + instantSimSpeed + ", SimSpeed: " + Globals.SimSpeed); m_lastUpdate = DateTime.UtcNow; MainLock.MainThread_AcquireExclusive(); } }
public UpdateManager() { ThreadTracker.SetGameThread(); Instance = this; MainLock.MainThread_AcquireExclusive(); }