mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2026-11-19 17:05:20 +08:00
Rework `Unlock Tech with Key-Modifiers` into a full upgrade/downgrade feature, ported from the standalone TechTweaks plugin. - Holding Shift / Ctrl / Ctrl+Shift / Alt while clicking a tech adds 1 / 10 / 100 / all levels and unlocks the direct prerequisites, as before. - `Caps Lock` toggles a session downgrade mode; the same modifiers then subtract 1 / 10 / 100 levels or revert the tech to locked. Downgrading walks the dependent techs so no "prerequisite locked, dependent unlocked" state is left behind, and it reverts the upgrade values the tech had applied (`TechFuncRevert`), including the multiplicative, assignment and boolean `UnlockTechFunction` cases, so repeated up/down cycles do not drift. - Toggling downgrade mode reports itself like the other toggles: a realtime tip popup plus the critical warning banner. The banner now shows on its own instead of requiring `Build without condition` or `No collision` to be on, and outranks both in the shared priority chains. - The `?` button in the tech tree shows the full key reference, using the same title and text as the config panel's tip button. While the option is off, the button is absent and `Caps Lock` is inert, so the key cannot collide with other bindings. - The option keeps its original `General/UnlockTech` config entry, so existing configurations carry over. The old unlock-only handler in `Patches/GamePatch.cs` is replaced by `Patches/TechTweaksPatch.cs`, which follows the repository's mod feature lifecycle (per-frame input through the registry's `OnInputUpdate`).
213 lines
8.1 KiB
C#
213 lines
8.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection.Emit;
|
|
using BepInEx.Configuration;
|
|
using HarmonyLib;
|
|
using UnityEngine.Bindings;
|
|
using UXAssist.Common;
|
|
using UXAssist.Common.ModFeatures;
|
|
using GameLogicProc = UXAssist.Common.GameLogic;
|
|
|
|
namespace CheatEnabler.Patches;
|
|
|
|
[ModFeature("Game")]
|
|
public static class GamePatch
|
|
{
|
|
public static ConfigEntry<bool> DevShortcutsEnabled;
|
|
public static ConfigEntry<bool> AbnormalDisablerEnabled;
|
|
|
|
public static void Init()
|
|
{
|
|
DevShortcutsEnabled.SettingChanged += (_, _) => DevShortcuts.Enable(DevShortcutsEnabled.Value);
|
|
AbnormalDisablerEnabled.SettingChanged += (_, _) => AbnormalDisabler.Enable(AbnormalDisablerEnabled.Value);
|
|
GameLogicProc.OnGameEnd += ResetState;
|
|
}
|
|
|
|
public static void Start()
|
|
{
|
|
DevShortcuts.Enable(DevShortcutsEnabled.Value);
|
|
AbnormalDisabler.Enable(AbnormalDisablerEnabled.Value);
|
|
}
|
|
|
|
public static void Uninit()
|
|
{
|
|
GameLogicProc.OnGameEnd -= ResetState;
|
|
AbnormalDisabler.Enable(false);
|
|
DevShortcuts.Enable(false);
|
|
}
|
|
|
|
private static void ResetState()
|
|
{
|
|
AbnormalDisabler.ResetState();
|
|
DevShortcuts.ResetState();
|
|
}
|
|
|
|
public class AbnormalDisabler : PatchImpl<AbnormalDisabler>
|
|
{
|
|
private static Dictionary<int, AbnormalityDeterminator> _savedDeterminators;
|
|
|
|
protected override void OnEnable()
|
|
{
|
|
if (_savedDeterminators == null) return;
|
|
var abnormalLogic = GameMain.gameScenario?.abnormalityLogic;
|
|
if (abnormalLogic == null) return;
|
|
foreach (var p in _savedDeterminators)
|
|
{
|
|
p.Value.OnUnregEvent();
|
|
}
|
|
|
|
abnormalLogic.determinators = [];
|
|
}
|
|
|
|
protected override void OnDisable()
|
|
{
|
|
if (_savedDeterminators == null) return;
|
|
var abnormalLogic = GameMain.gameScenario?.abnormalityLogic;
|
|
if (abnormalLogic?.determinators == null) return;
|
|
abnormalLogic.determinators = _savedDeterminators;
|
|
foreach (var p in _savedDeterminators)
|
|
{
|
|
p.Value.OnRegEvent();
|
|
}
|
|
}
|
|
|
|
internal static void ResetState()
|
|
{
|
|
_savedDeterminators = null;
|
|
}
|
|
|
|
[HarmonyPrefix]
|
|
[HarmonyPatch(typeof(AbnormalityLogic), nameof(AbnormalityLogic.NotifyBeforeGameSave))]
|
|
[HarmonyPatch(typeof(AbnormalityLogic), nameof(AbnormalityLogic.NotifyOnAssemblerRecipePick))]
|
|
[HarmonyPatch(typeof(AbnormalityLogic), nameof(AbnormalityLogic.NotifyOnGameBegin))]
|
|
[HarmonyPatch(typeof(AbnormalityLogic), nameof(AbnormalityLogic.NotifyOnMechaForgeTaskComplete))]
|
|
[HarmonyPatch(typeof(AbnormalityLogic), nameof(AbnormalityLogic.NotifyOnUnlockTech))]
|
|
[HarmonyPatch(typeof(AbnormalityLogic), nameof(AbnormalityLogic.NotifyOnUseConsole))]
|
|
private static bool DisableAbnormalLogic()
|
|
{
|
|
return false;
|
|
}
|
|
|
|
[HarmonyPostfix]
|
|
[HarmonyPatch(typeof(AbnormalityLogic), nameof(AbnormalityLogic.InitDeterminators))]
|
|
private static void DisableAbnormalDeterminators(AbnormalityLogic __instance)
|
|
{
|
|
_savedDeterminators = __instance.determinators;
|
|
if (!AbnormalDisablerEnabled.Value) return;
|
|
__instance.determinators = [];
|
|
foreach (var p in _savedDeterminators)
|
|
{
|
|
p.Value.OnUnregEvent();
|
|
}
|
|
}
|
|
}
|
|
|
|
public class DevShortcuts : PatchImpl<DevShortcuts>
|
|
{
|
|
private static PlayerAction_Test _test;
|
|
|
|
protected override void OnEnable()
|
|
{
|
|
if (_test != null) _test.active = true;
|
|
}
|
|
|
|
protected override void OnDisable()
|
|
{
|
|
if (_test != null) _test.active = false;
|
|
}
|
|
|
|
internal static void ResetState()
|
|
{
|
|
_test = null;
|
|
}
|
|
|
|
[HarmonyPostfix]
|
|
[HarmonyPatch(typeof(PlayerController), nameof(PlayerController.Init))]
|
|
private static void PlayerController_Init_Postfix(PlayerController __instance)
|
|
{
|
|
var cnt = __instance.actions.Length;
|
|
var newActions = new PlayerAction[cnt + 1];
|
|
for (var i = 0; i < cnt; i++)
|
|
{
|
|
newActions[i] = __instance.actions[i];
|
|
}
|
|
|
|
_test = new PlayerAction_Test();
|
|
_test.Init(__instance.player);
|
|
_test.active = DevShortcutsEnabled.Value;
|
|
newActions[cnt] = _test;
|
|
__instance.actions = newActions;
|
|
}
|
|
|
|
[HarmonyPostfix]
|
|
[HarmonyPatch(typeof(PlayerAction_Test), nameof(PlayerAction_Test.GameTick))]
|
|
private static void PlayerAction_Test_GameTick_Postfix(PlayerAction_Test __instance)
|
|
{
|
|
__instance.Update();
|
|
}
|
|
// Harmony transpiler: PlayerAction_Test_Update_Transpiler
|
|
// Target: PlayerAction_Test.Update
|
|
// Fallback: None — patch will fail loudly if the target method body changes.
|
|
[HarmonyTranspiler]
|
|
[HarmonyPatch(typeof(PlayerAction_Test), nameof(PlayerAction_Test.Update))]
|
|
private static IEnumerable<CodeInstruction> PlayerAction_Test_Update_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
|
|
{
|
|
var matcher = new CodeMatcher(instructions, generator);
|
|
matcher.End().MatchBack(false,
|
|
new CodeMatch(OpCodes.Ldarg_0),
|
|
new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(PlayerAction_Test), nameof(PlayerAction_Test.active)))
|
|
);
|
|
var pos = matcher.Pos;
|
|
/* Remove Shift+F4 part of the method */
|
|
matcher.Start().RemoveInstructions(pos).MatchForward(false,
|
|
new CodeMatch(OpCodes.Call, AccessTools.PropertyGetter(typeof(GameMain), nameof(GameMain.sandboxToolsEnabled))),
|
|
new CodeMatch(OpCodes.Ldc_I4_0),
|
|
new CodeMatch(OpCodes.Ceq)
|
|
);
|
|
var labels = matcher.Labels;
|
|
matcher.SetInstructionAndAdvance(
|
|
new CodeInstruction(OpCodes.Ldc_I4_1).WithLabels(labels)
|
|
).RemoveInstructions(2);
|
|
/* Remove Ctrl+A */
|
|
matcher.Start().MatchForward(false,
|
|
new CodeMatch(instr => (instr.opcode == OpCodes.Ldc_I4_S || instr.opcode == OpCodes.Ldc_I4) && instr.OperandIs(0x61)),
|
|
new CodeMatch(OpCodes.Call, AccessTools.Method(typeof(UnityEngine.Input), nameof(UnityEngine.Input.GetKeyDown), [typeof(UnityEngine.KeyCode)]))
|
|
);
|
|
labels = matcher.Labels;
|
|
matcher.Labels = null;
|
|
matcher.RemoveInstructions(2);
|
|
matcher.Opcode = OpCodes.Br;
|
|
matcher.Labels = labels;
|
|
return matcher.InstructionEnumeration();
|
|
}
|
|
// Harmony transpiler: GameCamera_Logic_Transpiler
|
|
// Target: GameCamera.FrameLogic
|
|
// Fallback: None — patch will fail loudly if the target method body changes.
|
|
[HarmonyTranspiler]
|
|
[HarmonyPatch(typeof(GameCamera), nameof(GameCamera.FrameLogic))]
|
|
private static IEnumerable<CodeInstruction> GameCamera_Logic_Transpiler(IEnumerable<CodeInstruction> instructions)
|
|
{
|
|
var matcher = new CodeMatcher(instructions);
|
|
matcher.MatchForward(false,
|
|
new CodeMatch(OpCodes.Ldarg_0),
|
|
new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(GameCamera), nameof(GameCamera.finalPoser))),
|
|
new CodeMatch(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(CameraPoser), nameof(CameraPoser.cameraPose)))
|
|
);
|
|
var labels = matcher.Labels;
|
|
matcher.Labels = null;
|
|
matcher.Insert(
|
|
new CodeInstruction(OpCodes.Ldarg_0).WithLabels(labels),
|
|
Transpilers.EmitDelegate((GameCamera camera) =>
|
|
{
|
|
if (PlayerAction_Test.lockCam)
|
|
{
|
|
camera.finalPoser.cameraPose = PlayerAction_Test.camPose;
|
|
}
|
|
})
|
|
);
|
|
return matcher.InstructionEnumeration();
|
|
}
|
|
}
|
|
|
|
}
|