private static void TimeChange(TrafficLights.ChangeMode mode) { var numberOfTests = 1000; TrafficLights.ChangeAll(mode); var sw = new Stopwatch(); sw.Start(); for (var i = 0; i < numberOfTests; i++) { TrafficLights.ChangeAll(mode); } sw.Stop(); var stats = sw.Elapsed; sw.Reset(); sw.Start(); for (var i = 0; i < numberOfTests; i++) { TrafficLights.ChangeAllFast(mode); } sw.Stop(); var fast = sw.Elapsed; DebugLog.Info($"Stats: {stats}, Fast: {fast}"); //Add: (small map, 1000 iterations) // Stats: 00:00:47.1062748 // Fast : 00:00:02.5574887 }
public void Change(TrafficLights.ChangeMode changeMode) { switch (changeMode) { case TrafficLights.ChangeMode.Remove: case TrafficLights.ChangeMode.Add: case TrafficLights.ChangeMode.Reset: Patch(changeMode); break; default: throw new ArgumentOutOfRangeException(nameof(changeMode), changeMode, null); } }
private void Patch(TrafficLights.ChangeMode changeMode) { if (_patch != null) { DebugLog.Info($"{nameof(Patch)}: Already patched with {ChangeMode}"); if (ChangeMode != changeMode) { DebugLog.Info($"{nameof(Patch)}: Update mode to {changeMode}"); _changeMode = changeMode; } return; } var src = typeof(RoadBaseAI).GetMethod(nameof(RoadBaseAI.UpdateNode), BindingFlags.Public | BindingFlags.Instance); var prefix = typeof(TrafficLightsHandlingChanger).GetMethod(nameof(AfterRoadBaseAiUpdateNode), BindingFlags.Public | BindingFlags.Static); DebugLog.Info($"{nameof(Patch)}: Patch to {changeMode}; src={src}; prefix={prefix}"); _changeMode = changeMode; _patch = Harmony.Patch.Apply(src, prefix); }
private static void HandleChangeTrafficLightsClick(UIMouseEventParameter evt, TrafficLights.ChangeMode mode, bool requiresControl) { if (evt.buttons != UIMouseButton.Left) { return; } if (requiresControl && !(Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))) { return; } var currentTime = Time.time; if (currentTime - _lastChangeTime >= MinTimeBetweenChanges) { _lastChangeTime = currentTime; } else { //not enough time passed DebugLog.Info("Not enough time passed for another update"); return; } DebugLog.Info($"Change all intersections: {mode} all"); TrafficLights.ChangeAllFast(mode); // var stats = TrafficLights.ChangeAll(mode); //todo: handle stats? }