1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2025-12-08 22:13:30 +08:00
This commit is contained in:
2024-09-18 01:41:08 +08:00
parent fb916b3813
commit bd512a6b78
17 changed files with 232 additions and 207 deletions

View File

@@ -3,24 +3,12 @@ using HarmonyLib;
namespace UXAssist.Common;
public static class GameLogic
public class GameLogic: PatchImpl<GameLogic>
{
private static Harmony _harmony;
public static Action OnDataLoaded;
public static Action OnGameBegin;
public static Action OnGameEnd;
public static void Init()
{
_harmony ??= Harmony.CreateAndPatchAll(typeof(GameLogic));
}
public static void Uninit()
{
_harmony?.UnpatchSelf();
_harmony = null;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(VFPreload), nameof(VFPreload.InvokeOnLoadWorkEnded))]
public static void VFPreload_InvokeOnLoadWorkEnded_Postfix()

View File

@@ -1,7 +1,15 @@
using HarmonyLib;
using System;
using System.Reflection;
using HarmonyLib;
namespace UXAssist.Common;
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class PatchImplGuidAttribute(string guid) : Attribute
{
public string Guid { get; } = guid;
}
public class PatchImpl<T> where T : new()
{
private static T Instance { get; } = new();
@@ -17,7 +25,8 @@ public class PatchImpl<T> where T : new()
}
if (enable)
{
thisInstance._patch ??= Harmony.CreateAndPatchAll(typeof(T));
var guid = typeof(T).GetCustomAttribute<PatchImplGuidAttribute>()?.Guid;
thisInstance._patch ??= Harmony.CreateAndPatchAll(typeof(T), guid);
thisInstance.OnEnable();
return;
}
@@ -26,7 +35,7 @@ public class PatchImpl<T> where T : new()
thisInstance._patch = null;
}
protected static Harmony GetPatch() => (Instance as PatchImpl<T>)?._patch;
public static Harmony GetHarmony() => (Instance as PatchImpl<T>)?._patch;
protected virtual void OnEnable() { }
protected virtual void OnDisable() { }

View File

@@ -8,11 +8,13 @@ namespace UXAssist.Common;
public static class Util
{
public static Type[] GetTypesInNamespace(Assembly assembly, string nameSpace)
public static Type[] GetTypesFiltered(Assembly assembly, Func<Type, bool> predicate)
{
return assembly.GetTypes().Where(t => string.Equals(t.Namespace, nameSpace, StringComparison.Ordinal)).ToArray();
return assembly.GetTypes().Where(predicate).ToArray();
}
public static Type[] GetTypesInNamespace(Assembly assembly, string nameSpace) => GetTypesFiltered(assembly, t => string.Equals(t.Namespace, nameSpace, StringComparison.Ordinal));
public static byte[] LoadEmbeddedResource(string path, Assembly assembly = null)
{
if (assembly == null)

View File

@@ -35,7 +35,6 @@ public static class BulletTimeWrapper
matcher.MatchForward(false,
new CodeMatch(OpCodes.Ldstr, "Increase game speed (max 4x)")
).Set(OpCodes.Ldstr, "Increase game speed (max 10x)");
UXAssist.Logger.LogDebug($"Patched IngameUI.Init @ {matcher.Pos}");
return matcher.InstructionEnumeration();
}
@@ -45,7 +44,6 @@ public static class BulletTimeWrapper
matcher.MatchForward(false,
new CodeMatch(OpCodes.Ldc_R8, 240.0)
).Set(OpCodes.Ldc_R8, 600.0);
UXAssist.Logger.LogDebug($"Patched IngameUI.OnSpeedButtonClick @ {matcher.Pos}");
return matcher.InstructionEnumeration();
}

View File

@@ -7,19 +7,18 @@ using UXAssist.Common;
namespace UXAssist.Patches;
public static class DysonSpherePatch
public class DysonSpherePatch: PatchImpl<DysonSpherePatch>
{
public static ConfigEntry<bool> StopEjectOnNodeCompleteEnabled;
public static ConfigEntry<bool> OnlyConstructNodesEnabled;
public static ConfigEntry<int> AutoConstructMultiplier;
private static Harmony _dysonSpherePatch;
private static FieldInfo _totalNodeSpInfo, _totalFrameSpInfo, _totalCpInfo;
public static void Init()
{
I18N.Add("[UXAssist] No node to fill", "[UXAssist] No node to fill", "[UXAssist] 无可建造节点");
_dysonSpherePatch ??= Harmony.CreateAndPatchAll(typeof(DysonSpherePatch));
Enable(true);
StopEjectOnNodeCompleteEnabled.SettingChanged += (_, _) => StopEjectOnNodeComplete.Enable(StopEjectOnNodeCompleteEnabled.Value);
OnlyConstructNodesEnabled.SettingChanged += (_, _) => OnlyConstructNodes.Enable(OnlyConstructNodesEnabled.Value);
StopEjectOnNodeComplete.Enable(StopEjectOnNodeCompleteEnabled.Value);
@@ -33,8 +32,7 @@ public static class DysonSpherePatch
{
StopEjectOnNodeComplete.Enable(false);
OnlyConstructNodes.Enable(false);
_dysonSpherePatch?.UnpatchSelf();
_dysonSpherePatch = null;
Enable(false);
}
public static void InitCurrentDysonSphere(int index)

View File

@@ -12,7 +12,7 @@ using UXAssist.Common;
namespace UXAssist.Patches;
public static class FactoryPatch
public class FactoryPatch: PatchImpl<FactoryPatch>
{
public static ConfigEntry<bool> UnlimitInteractiveEnabled;
public static ConfigEntry<bool> RemoveSomeConditionEnabled;
@@ -29,8 +29,6 @@ public static class FactoryPatch
public static ConfigEntry<bool> BeltSignalsForBuyOutEnabled;
private static PressKeyBind _doNotRenderEntitiesKey;
private static Harmony _factoryPatch;
public static void Init()
{
_doNotRenderEntitiesKey = KeyBindings.RegisterKeyBinding(new BuiltinKey
@@ -71,11 +69,13 @@ public static class FactoryPatch
DragBuildPowerPoles.Enable(DragBuildPowerPolesEnabled.Value);
BeltSignalsForBuyOut.Enable(BeltSignalsForBuyOutEnabled.Value);
_factoryPatch ??= Harmony.CreateAndPatchAll(typeof(FactoryPatch));
Enable(true);
}
public static void Uninit()
{
Enable(false);
RemoveSomeConditionBuild.Enable(false);
UnlimitInteractive.Enable(false);
NightLight.Enable(false);
@@ -90,9 +90,6 @@ public static class FactoryPatch
DragBuildPowerPoles.Enable(false);
BeltSignalsForBuyOut.Enable(false);
BeltSignalsForBuyOut.UninitPersist();
_factoryPatch?.UnpatchSelf();
_factoryPatch = null;
}
public static void OnUpdate()
@@ -1324,7 +1321,7 @@ public static class FactoryPatch
private static void UnfixProto()
{
if (GetPatch() == null || OldDragBuild.Count < 3 || DSPGame.IsMenuDemo) return;
if (GetHarmony() == null || OldDragBuild.Count < 3 || DSPGame.IsMenuDemo) return;
var i = 0;
foreach (var id in PowerPoleIds)
{

View File

@@ -9,7 +9,7 @@ using UXAssist.Common;
namespace UXAssist.Patches;
public static class GamePatch
public class GamePatch: PatchImpl<GamePatch>
{
private const string GameWindowClass = "UnityWndClass";
private static string _gameWindowTitle = "Dyson Sphere Program";
@@ -50,8 +50,6 @@ public static class GamePatch
}
}
private static Harmony _gamePatch;
public static void Init()
{
// Get profile name from command line arguments, and set window title accordingly
@@ -104,19 +102,18 @@ public static class GamePatch
MouseCursorScaleUp.Enable(MouseCursorScaleUpMultiplier.Value > 1);
// AutoSaveOpt.Enable(AutoSaveOptEnabled.Value);
ConvertSavesFromPeace.Enable(ConvertSavesFromPeaceEnabled.Value);
_gamePatch ??= Harmony.CreateAndPatchAll(typeof(GamePatch));
Enable(true);
}
public static void Uninit()
{
Enable(false);
LoadLastWindowRect.Enable(false);
EnableWindowResize.Enable(false);
MouseCursorScaleUp.reload = false;
MouseCursorScaleUp.Enable(false);
// AutoSaveOpt.Enable(false);
ConvertSavesFromPeace.Enable(false);
_gamePatch?.UnpatchSelf();
_gamePatch = null;
}
private static void RefreshSavePath()
@@ -341,21 +338,11 @@ public static class GamePatch
}
/*
private static class AutoSaveOpt
private class AutoSaveOpt: PatchImpl<AutoSaveOpt>
{
private static Harmony _patch;
public static void Enable(bool on)
protected override void OnEnable()
{
if (on)
{
Directory.CreateDirectory(GameConfig.gameSaveFolder + "AutoSaves/");
_patch ??= Harmony.CreateAndPatchAll(typeof(AutoSaveOpt));
return;
}
_patch?.UnpatchSelf();
_patch = null;
Directory.CreateDirectory(GameConfig.gameSaveFolder + "AutoSaves/");
}
[HarmonyPrefix]

View File

@@ -29,11 +29,10 @@ public class UXAssist : BaseUnityPlugin, IModCanSave
private static bool _configWinInitialized;
private static MyConfigWindow _configWin;
private static Harmony _patch;
private static Harmony _persistPatch;
private static bool _initialized;
private static PressKeyBind _toggleKey;
private static ConfigFile _dummyConfig;
private Type[] _patches;
#region IModCanSave
private const ushort ModSaveVersion = 1;
@@ -152,47 +151,46 @@ public class UXAssist : BaseUnityPlugin, IModCanSave
DysonSpherePatch.AutoConstructMultiplier = Config.Bind("DysonSphere", "AutoConstructMultiplier", 1, "Dyson Sphere auto-construct speed multiplier");
I18N.Init();
}
private void Start()
{
I18N.Add("UXAssist Config", "UXAssist Config", "UX助手设置");
I18N.Add("KEYOpenUXAssistConfigWindow", "Open UXAssist Config Window", "打开UX助手设置面板");
I18N.Add("KEYToggleAutoCruise", "Toggle auto-cruise", "切换自动巡航");
// UI Patch
_patch ??= Harmony.CreateAndPatchAll(typeof(UXAssist), PluginInfo.PLUGIN_GUID);
_persistPatch ??= Harmony.CreateAndPatchAll(typeof(Persist));
// UI Patches
UIPatch.Enable(true);
GameLogic.Init();
// Persistant Patches
Persist.Enable(true);
GameLogic.Enable(true);
MyWindowManager.Init();
UIConfigWindow.Init();
Common.Util.GetTypesInNamespace(Assembly.GetExecutingAssembly(), "UXAssist.Patches")
.Do(type => type.GetMethod("Init")?.Invoke(null, null));
_patches = Common.Util.GetTypesInNamespace(Assembly.GetExecutingAssembly(), "UXAssist.Patches");
_patches?.Do(type => type.GetMethod("Init")?.Invoke(null, null));
ModsCompat.AuxilaryfunctionWrapper.Init(_patch);
ModsCompat.BulletTimeWrapper.Init(_patch);
var patch = UIPatch.GetHarmony();
ModsCompat.AuxilaryfunctionWrapper.Init(patch);
ModsCompat.BulletTimeWrapper.Init(patch);
I18N.Apply();
I18N.OnInitialized += RecreateConfigWindow;
}
private void Start()
{
_patches?.Do(type => type.GetMethod("Start")?.Invoke(null, null));
LogisticsPatch.Start();
}
private void OnDestroy()
{
Common.Util.GetTypesInNamespace(Assembly.GetExecutingAssembly(), "UXAssist.Patches")
.Do(type => type.GetMethod("Uninit")?.Invoke(null, null));
_patches?.Do(type => type.GetMethod("Uninit")?.Invoke(null, null));
MyWindowManager.Uninit();
GameLogic.Uninit();
_patch?.UnpatchSelf();
_patch = null;
_persistPatch?.UnpatchSelf();
_persistPatch = null;
GameLogic.Enable(false);
Persist.Enable(false);
UIPatch.Enable(false);
}
private void Update()
@@ -245,66 +243,90 @@ public class UXAssist : BaseUnityPlugin, IModCanSave
if (wasActive) ToggleConfigWindow();
}
// Add config button to main menu
[HarmonyPostfix, HarmonyPatch(typeof(UIRoot), nameof(UIRoot.OpenMainMenuUI))]
public static void UIRoot_OpenMainMenuUI_Postfix()
[PatchImplGuid(PluginInfo.PLUGIN_GUID)]
private class UIPatch: PatchImpl<UIPatch>
{
if (_initialized) return;
private static GameObject _buttonOnPlanetGlobe;
// Add config button to main menu
[HarmonyPostfix, HarmonyPatch(typeof(UIRoot), nameof(UIRoot.OpenMainMenuUI))]
public static void UIRoot_OpenMainMenuUI_Postfix()
{
var mainMenu = UIRoot.instance.uiMainMenu;
var src = mainMenu.newGameButton;
var parent = src.transform.parent;
var btn = Instantiate(src, parent);
btn.name = "button-cheatenabler-config";
var l = btn.text.GetComponent<Localizer>();
if (l != null)
if (_initialized) return;
{
l.stringKey = "UXAssist Config";
l.translation = "UXAssist Config".Translate();
var mainMenu = UIRoot.instance.uiMainMenu;
var src = mainMenu.newGameButton;
var parent = src.transform.parent;
var btn = Instantiate(src, parent);
btn.name = "button-cheatenabler-config";
var l = btn.text.GetComponent<Localizer>();
if (l != null)
{
l.stringKey = "UXAssist Config";
l.translation = "UXAssist Config".Translate();
}
btn.text.text = "UXAssist Config".Translate();
btn.text.fontSize = btn.text.fontSize * 7 / 8;
I18N.OnInitialized += () => { btn.text.text = "UXAssist Config".Translate(); };
var vec = ((RectTransform)mainMenu.exitButton.transform).anchoredPosition3D;
var vec2 = ((RectTransform)mainMenu.creditsButton.transform).anchoredPosition3D;
var transform1 = (RectTransform)btn.transform;
transform1.anchoredPosition3D = new Vector3(vec.x, vec.y + (vec.y - vec2.y) * 2, vec.z);
btn.button.onClick.RemoveAllListeners();
btn.button.onClick.AddListener(ToggleConfigWindow);
}
btn.text.text = "UXAssist Config".Translate();
btn.text.fontSize = btn.text.fontSize * 7 / 8;
I18N.OnInitialized += () => { btn.text.text = "UXAssist Config".Translate(); };
var vec = ((RectTransform)mainMenu.exitButton.transform).anchoredPosition3D;
var vec2 = ((RectTransform)mainMenu.creditsButton.transform).anchoredPosition3D;
var transform1 = (RectTransform)btn.transform;
transform1.anchoredPosition3D = new Vector3(vec.x, vec.y + (vec.y - vec2.y) * 2, vec.z);
btn.button.onClick.RemoveAllListeners();
btn.button.onClick.AddListener(ToggleConfigWindow);
{
var panel = UIRoot.instance.uiGame.planetGlobe;
var src = panel.button2;
var sandboxMenu = UIRoot.instance.uiGame.sandboxMenu;
var icon = sandboxMenu.categoryButtons[6].transform.Find("icon")?.GetComponent<Image>()?.sprite;
var b = Instantiate(src, src.transform.parent);
_buttonOnPlanetGlobe = b.gameObject;
var rect = (RectTransform)_buttonOnPlanetGlobe.transform;
var btn = _buttonOnPlanetGlobe.GetComponent<UIButton>();
var img = _buttonOnPlanetGlobe.transform.Find("button-2/icon")?.GetComponent<Image>();
if (img != null)
{
img.sprite = icon;
}
if (_buttonOnPlanetGlobe != null && btn != null)
{
_buttonOnPlanetGlobe.name = "open-uxassist-config";
rect.localScale = new Vector3(0.6f, 0.6f, 0.6f);
rect.anchoredPosition3D = new Vector3(64f, -5f, 0f);
b.onClick.RemoveAllListeners();
btn.onClick += _ => { ToggleConfigWindow(); };
btn.tips.tipTitle = "UXAssist Config";
I18N.OnInitialized += () => { btn.tips.tipTitle = "UXAssist Config".Translate(); };
btn.tips.tipText = null;
btn.tips.corner = 9;
btn.tips.offset = new Vector2(-20f, -20f);
_buttonOnPlanetGlobe.SetActive(true);
}
}
_initialized = true;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(UIPlanetGlobe), nameof(UIPlanetGlobe.DistributeButtons))]
private static void UIPlanetGlobe_DistributeButtons_Postfix(UIPlanetGlobe __instance)
{
var panel = UIRoot.instance.uiGame.planetGlobe;
var src = panel.button2;
var sandboxMenu = UIRoot.instance.uiGame.sandboxMenu;
var icon = sandboxMenu.categoryButtons[6].transform.Find("icon")?.GetComponent<Image>()?.sprite;
var b = Instantiate(src, src.transform.parent);
var panelButtonGo = b.gameObject;
var rect = (RectTransform)panelButtonGo.transform;
var btn = panelButtonGo.GetComponent<UIButton>();
var img = panelButtonGo.transform.Find("button-2/icon")?.GetComponent<Image>();
if (img != null)
if (_buttonOnPlanetGlobe == null) return;
var rect = (RectTransform)_buttonOnPlanetGlobe.transform;
if (__instance.dysonSphereSystemUnlocked || __instance.logisticsSystemUnlocked)
{
img.sprite = icon;
}
if (panelButtonGo != null && btn != null)
{
panelButtonGo.name = "open-uxassist-config";
rect.localScale = new Vector3(0.6f, 0.6f, 0.6f);
rect.anchoredPosition3D = new Vector3(64f, -5f, 0f);
b.onClick.RemoveAllListeners();
btn.onClick += _ => { ToggleConfigWindow(); };
btn.tips.tipTitle = "UXAssist Config";
I18N.OnInitialized += () => { btn.tips.tipTitle = "UXAssist Config".Translate(); };
btn.tips.tipText = null;
btn.tips.corner = 9;
btn.tips.offset = new Vector2(-20f, -20f);
panelButtonGo.SetActive(true);
}
else
{
rect.anchoredPosition3D = new Vector3(128f, -100f, 0f);
}
}
_initialized = true;
}
private static class Persist
private class Persist: PatchImpl<Persist>
{
// Check for noModifier while pressing hotkeys on build bar
[HarmonyTranspiler]