From 29ee32b11b144738b72202049daf6180496b7ba6 Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Sun, 30 Nov 2025 18:08:13 +0800 Subject: [PATCH] work in progress --- CheatEnabler/Functions/PlanetFunctions.cs | 1 + CheatEnabler/Patches/PlanetPatch.cs | 1 + UXAssist/ModsCompat/BlueprintTweaks.cs | 3 +- UXAssist/Patches/PersistPatch.cs | 155 ++++++++++++++++++++++ UXAssist/Patches/PlanetPatch.cs | 1 + UXAssist/UIConfigWindow.cs | 2 +- UXAssist/UXAssist.cs | 7 +- UniverseGenTweaks/BirthPlanetPatch.cs | 1 + 8 files changed, 168 insertions(+), 3 deletions(-) diff --git a/CheatEnabler/Functions/PlanetFunctions.cs b/CheatEnabler/Functions/PlanetFunctions.cs index 61e036d..05b1481 100644 --- a/CheatEnabler/Functions/PlanetFunctions.cs +++ b/CheatEnabler/Functions/PlanetFunctions.cs @@ -1,6 +1,7 @@ using Random = UnityEngine.Random; namespace CheatEnabler.Functions; + public static class PlanetFunctions { public static void BuryAllVeins(bool bury) diff --git a/CheatEnabler/Patches/PlanetPatch.cs b/CheatEnabler/Patches/PlanetPatch.cs index aadb198..85773df 100644 --- a/CheatEnabler/Patches/PlanetPatch.cs +++ b/CheatEnabler/Patches/PlanetPatch.cs @@ -6,6 +6,7 @@ using HarmonyLib; using UXAssist.Common; namespace CheatEnabler.Patches; + public static class PlanetPatch { public static ConfigEntry WaterPumpAnywhereEnabled; diff --git a/UXAssist/ModsCompat/BlueprintTweaks.cs b/UXAssist/ModsCompat/BlueprintTweaks.cs index 1212ba2..c460372 100644 --- a/UXAssist/ModsCompat/BlueprintTweaks.cs +++ b/UXAssist/ModsCompat/BlueprintTweaks.cs @@ -82,7 +82,8 @@ class BlueprintTweaks new CodeMatch(OpCodes.Callvirt) ).RemoveInstructions(5); matcher.InsertAndAdvance(new CodeInstruction(OpCodes.Ldarg_0), - Transpilers.EmitDelegate((UIBuildingGrid grid) => { + Transpilers.EmitDelegate((UIBuildingGrid grid) => + { grid.material.SetFloat(reformMode, 0f); grid.material.SetFloat(zMin, -0.5f); }) diff --git a/UXAssist/Patches/PersistPatch.cs b/UXAssist/Patches/PersistPatch.cs index 76ffb5e..f80a537 100644 --- a/UXAssist/Patches/PersistPatch.cs +++ b/UXAssist/Patches/PersistPatch.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; +using System.IO; using System.Reflection.Emit; using HarmonyLib; using UnityEngine; +using UnityEngine.Networking; using UXAssist.Common; namespace UXAssist.Patches; @@ -170,4 +172,157 @@ public class PersistPatch : PatchImpl { return false; } + + #region Cluster Upload Result + + const int ClusterUploadResultKeepCount = 100; + private static readonly ClusterUploadResult[] _clusterUploadResults = new ClusterUploadResult[ClusterUploadResultKeepCount]; + private static readonly object _clusterUploadResultsLock = new(); + private static int _clusterUploadResultsHead = 0; + private static int _clusterUploadResultsCount = 0; + private static ClusterPlayerData[] _topTenPlayerData = null; + + private struct ClusterUploadResult + { + public DateTime UploadTime; + public int Result; + public float RequestTime; + } + + private static void AddClusterUploadResult(int result, float requestTime) + { + lock (_clusterUploadResultsLock) + { + if (_clusterUploadResultsCount >= ClusterUploadResultKeepCount) + { + _clusterUploadResults[_clusterUploadResultsHead] = new ClusterUploadResult { UploadTime = DateTime.Now, Result = result, RequestTime = requestTime }; + _clusterUploadResultsHead = (_clusterUploadResultsHead + 1) % ClusterUploadResultKeepCount; + } + else + { + _clusterUploadResults[(_clusterUploadResultsHead + _clusterUploadResultsCount) % ClusterUploadResultKeepCount] = new ClusterUploadResult { UploadTime = DateTime.Now, Result = result, RequestTime = requestTime }; + _clusterUploadResultsCount++; + } + } + } + + public static void ExportClusterUploadResults(BinaryWriter w) + { + lock (_clusterUploadResultsLock) + { + w.Write(_clusterUploadResultsCount); + w.Write(_clusterUploadResultsHead); + for (var i = 0; i < _clusterUploadResultsCount; i++) + { + ref var result = ref _clusterUploadResults[(i + _clusterUploadResultsHead) % ClusterUploadResultKeepCount]; + w.Write(result.UploadTime.ToBinary()); + w.Write(result.Result); + w.Write(result.RequestTime); + } + } + } + + public static void ImportClusterUploadResults(BinaryReader r) + { + lock (_clusterUploadResultsLock) + { + _clusterUploadResultsCount = r.ReadInt32(); + _clusterUploadResultsHead = r.ReadInt32(); + for (var i = 0; i < _clusterUploadResultsCount; i++) + { + ref var result = ref _clusterUploadResults[(i + _clusterUploadResultsHead) % ClusterUploadResultKeepCount]; + result.UploadTime = DateTime.FromBinary(r.ReadInt64()); + result.Result = r.ReadInt32(); + result.RequestTime = r.ReadSingle(); + } + } + } + + [HarmonyPostfix] + [HarmonyPatch(typeof(MilkyWayWebClient), nameof(MilkyWayWebClient.OnUploadLoginErrored))] + private static void MilkyWayWebClient_OnUploadLoginErrored_Postfix(MilkyWayWebClient __instance, DSPWeb.HTTP_ERROR_TYPE errorType, string errorInfo, int errorCode) + { + switch (errorType) + { + case DSPWeb.HTTP_ERROR_TYPE.NETWORK_ERROR: + AddClusterUploadResult(-10001, (float)__instance.uploadRequest.reqTime); + break; + case DSPWeb.HTTP_ERROR_TYPE.HTTP_ERROR: + AddClusterUploadResult(-10010 - errorCode, (float)__instance.uploadRequest.reqTime); + break; + case DSPWeb.HTTP_ERROR_TYPE.USER_ABORT: + AddClusterUploadResult(-10003, (float)__instance.uploadRequest.reqTime); + break; + case DSPWeb.HTTP_ERROR_TYPE.UNEXPECTED_ERROR: + AddClusterUploadResult(-10004, (float)__instance.uploadRequest.reqTime); + break; + } + } + + [HarmonyPostfix] + [HarmonyPatch(typeof(MilkyWayWebClient), nameof(MilkyWayWebClient.OnUploadErrored))] + private static void MilkyWayWebClient_OnUploadErrored_Postfix(MilkyWayWebClient __instance, DSPWeb.HTTP_ERROR_TYPE errorType, string errorInfo, int errorCode) + { + switch (errorType) + { + case DSPWeb.HTTP_ERROR_TYPE.NETWORK_ERROR: + AddClusterUploadResult(-101, (float)__instance.uploadRequest.reqTime); + break; + case DSPWeb.HTTP_ERROR_TYPE.HTTP_ERROR: + AddClusterUploadResult(-110 - errorCode, (float)__instance.uploadRequest.reqTime); + break; + case DSPWeb.HTTP_ERROR_TYPE.USER_ABORT: + AddClusterUploadResult(-103, (float)__instance.uploadRequest.reqTime); + break; + case DSPWeb.HTTP_ERROR_TYPE.UNEXPECTED_ERROR: + AddClusterUploadResult(-104, (float)__instance.uploadRequest.reqTime); + break; + } + } + + [HarmonyPostfix] + [HarmonyPatch(typeof(MilkyWayWebClient), nameof(MilkyWayWebClient.OnUploadSucceed))] + private static void MilkyWayWebClient_OnUploadSucceed_Postfix(MilkyWayWebClient __instance, DownloadHandler handler) + { + if (!int.TryParse(handler.text, out var rcode)) + rcode = -1; + AddClusterUploadResult(rcode, (float)__instance.uploadRequest.reqTime); + } + + [HarmonyTranspiler] + [HarmonyPatch(typeof(MilkyWayCache), nameof(MilkyWayCache.LoadTopTenPlayerData))] + private static IEnumerable MilkyWayCache_LoadTopTenPlayerData_Transpiler(IEnumerable instructions, ILGenerator generator) + { + var matcher = new CodeMatcher(instructions, generator); + + matcher.MatchForward(false, + new CodeMatch(OpCodes.Ldarg_1), + new CodeMatch(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(BinaryReader), nameof(BinaryReader.ReadInt32))) + ).Advance(2).InsertAndAdvance( + new CodeInstruction(OpCodes.Dup), + Transpilers.EmitDelegate((int count) => _topTenPlayerData = new ClusterPlayerData[count]) + ); + + matcher.MatchForward(false, + new CodeMatch(OpCodes.Ldloca_S), + new CodeMatch(OpCodes.Ldarg_1), + new CodeMatch(OpCodes.Callvirt), + new CodeMatch(OpCodes.Stfld, AccessTools.Field(typeof(ClusterPlayerData), nameof(ClusterPlayerData.isAnon))) + ); + var objloc = matcher.Operand; + matcher.Advance(4); + var iinstr = matcher.Instruction.Clone(); + matcher.InsertAndAdvance( + iinstr, + new CodeInstruction(OpCodes.Ldloca_S, objloc), + Transpilers.EmitDelegate((int index, ref ClusterPlayerData playerData) => + { + if (index < _topTenPlayerData.Length) _topTenPlayerData[index] = playerData; + }) + ); + + return matcher.InstructionEnumeration(); + } + + #endregion } diff --git a/UXAssist/Patches/PlanetPatch.cs b/UXAssist/Patches/PlanetPatch.cs index c57b1c5..45df5bd 100644 --- a/UXAssist/Patches/PlanetPatch.cs +++ b/UXAssist/Patches/PlanetPatch.cs @@ -5,6 +5,7 @@ using HarmonyLib; using UXAssist.Common; namespace UXAssist.Patches; + public static class PlanetPatch { public static ConfigEntry PlayerActionsInGlobeViewEnabled; diff --git a/UXAssist/UIConfigWindow.cs b/UXAssist/UIConfigWindow.cs index abe51b7..ee70ff5 100644 --- a/UXAssist/UIConfigWindow.cs +++ b/UXAssist/UIConfigWindow.cs @@ -419,7 +419,7 @@ public static class UIConfigWindow PressShiftToTakeWholeBeltItemsEnabledChanged(null, null); void PressShiftToTakeWholeBeltItemsEnabledChanged(object o, EventArgs e) - { + { includeBranches.SetEnable(FactoryPatch.PressShiftToTakeWholeBeltItemsEnabled.Value); includeInserters.SetEnable(FactoryPatch.PressShiftToTakeWholeBeltItemsEnabled.Value); } diff --git a/UXAssist/UXAssist.cs b/UXAssist/UXAssist.cs index 803635e..8b0c08f 100644 --- a/UXAssist/UXAssist.cs +++ b/UXAssist/UXAssist.cs @@ -33,12 +33,13 @@ public class UXAssist : BaseUnityPlugin, IModCanSave private readonly Harmony _harmony = new(PluginInfo.PLUGIN_GUID); #region IModCanSave - private const ushort ModSaveVersion = 1; + private const ushort ModSaveVersion = 2; public void Export(BinaryWriter w) { w.Write(ModSaveVersion); FactoryPatch.Export(w); + PersistPatch.ExportClusterUploadResults(w); } public void Import(BinaryReader r) @@ -46,6 +47,10 @@ public class UXAssist : BaseUnityPlugin, IModCanSave var version = r.ReadUInt16(); if (version <= 0) return; FactoryPatch.Import(r); + if (version > 1) + { + PersistPatch.ImportClusterUploadResults(r); + } } public void IntoOtherSave() diff --git a/UniverseGenTweaks/BirthPlanetPatch.cs b/UniverseGenTweaks/BirthPlanetPatch.cs index 381a382..9f38942 100644 --- a/UniverseGenTweaks/BirthPlanetPatch.cs +++ b/UniverseGenTweaks/BirthPlanetPatch.cs @@ -4,6 +4,7 @@ using HarmonyLib; using GameLogicProc = UXAssist.Common.GameLogic; namespace UniverseGenTweaks; + public static class BirthPlanetPatch { public static ConfigEntry SitiVeinsOnBirthPlanet;