|
1 |
| -using HarmonyLib; |
2 |
| -using System.Collections.Generic; |
3 |
| -using UnityEngine; |
| 1 | +using System.Diagnostics; |
4 | 2 |
|
5 | 3 | namespace KSPCommunityFixes.Performance
|
6 | 4 | {
|
7 | 5 | class CommNetThrottling : BasePatch
|
8 | 6 | {
|
9 |
| - private static double packedInterval = 0.5; |
10 |
| - private static double unpackedInterval = 5.0; |
| 7 | + private static double maxGameTimeInterval = 2.5; |
| 8 | + private static long minRealTimeInterval = 20; |
| 9 | + private static Stopwatch realTimeUpdateTimer = new Stopwatch(); |
11 | 10 |
|
12 | 11 | protected override void ApplyPatches()
|
13 | 12 | {
|
14 | 13 | ConfigNode settingsNode = KSPCommunityFixes.SettingsNode.GetNode("COMMNET_THROTTLING_SETTINGS");
|
15 | 14 |
|
16 | 15 | if (settingsNode != null)
|
17 | 16 | {
|
18 |
| - settingsNode.TryGetValue("packedInterval", ref packedInterval); |
19 |
| - settingsNode.TryGetValue("unpackedInterval", ref unpackedInterval); |
| 17 | + settingsNode.TryGetValue("maxGameTimeInterval", ref maxGameTimeInterval); |
| 18 | + settingsNode.TryGetValue("minRealTimeInterval", ref minRealTimeInterval); |
20 | 19 | }
|
21 | 20 |
|
22 |
| - AddPatch(PatchType.Prefix, typeof(CommNet.CommNetNetwork), nameof(CommNet.CommNetNetwork.Update)); |
| 21 | + AddPatch(PatchType.Override, typeof(CommNet.CommNetNetwork), nameof(CommNet.CommNetNetwork.Update)); |
23 | 22 | }
|
24 | 23 |
|
25 |
| - static bool CommNetNetwork_Update_Prefix(CommNet.CommNetNetwork __instance) |
| 24 | + static void CommNetNetwork_Update_Override(CommNet.CommNetNetwork commNetNetwork) |
26 | 25 | {
|
27 |
| - if (!__instance.queueRebuild && !__instance.commNet.IsDirty) |
| 26 | + double currentGameTime = Planetarium.GetUniversalTime(); |
| 27 | + double currentGameTimeInterval = currentGameTime - commNetNetwork.prevUpdate; |
| 28 | + if (!commNetNetwork.queueRebuild && !commNetNetwork.commNet.IsDirty |
| 29 | + && currentGameTimeInterval >= 0.0 |
| 30 | + && (currentGameTimeInterval < maxGameTimeInterval || realTimeUpdateTimer.ElapsedMilliseconds < minRealTimeInterval)) |
28 | 31 | {
|
29 |
| - double timeSinceLastUpdate = Time.timeSinceLevelLoad - __instance.prevUpdate; |
30 |
| - |
31 |
| - if (FlightGlobals.ActiveVessel != null) |
32 |
| - { |
33 |
| - double interval = FlightGlobals.ActiveVessel.packed ? packedInterval : unpackedInterval; |
34 |
| - if (timeSinceLastUpdate < interval) |
35 |
| - { |
36 |
| - __instance.graphDirty = true; |
37 |
| - return false; |
38 |
| - } |
39 |
| - } |
| 32 | + commNetNetwork.graphDirty = true; |
| 33 | + return; |
40 | 34 | }
|
41 | 35 |
|
42 |
| - return true; |
| 36 | + // UnityEngine.Debug.Log($"CommNet update interval : {realTimeUpdateTimer.Elapsed.TotalMilliseconds:F0}ms"); |
| 37 | + |
| 38 | + commNetNetwork.commNet.Rebuild(); |
| 39 | + realTimeUpdateTimer.Restart(); |
| 40 | + commNetNetwork.prevUpdate = currentGameTime; |
| 41 | + commNetNetwork.graphDirty = false; |
| 42 | + commNetNetwork.queueRebuild = false; |
43 | 43 | }
|
44 | 44 | }
|
45 | 45 | }
|
0 commit comments