Files
BepInEx/Projects/TerraTech/TerraTech/Class1.cs
2023-04-10 17:52:17 +02:00

176 lines
7.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using HarmonyLib.Tools;
using UnityEngine;
// TODO: Make magnet better
// TODO: Make more energy generate
// TODO: Make attractor more range
// TODO: Make battery bigger
// TODO: Make shield and repair bigger
namespace TerraTech {
[BepInPlugin(pluginGuid, pluginName, pluginVersion)]
public class Main : BaseUnityPlugin {
private const string pluginGuid = "CykaMod";
private const string pluginName = "CykaMod";
private const string pluginVersion = "1.0.0";
public static ConfigEntry<int> xpMultiplier;
public static ConfigEntry<int> moneyMultiplier;
public static ConfigEntry<float> energyGenMultiplier;
public static ConfigEntry<bool> allProjectilesHoming;
public static ConfigEntry<float> shootingSpeedMultiplier;
public static ConfigEntry<float> muzzleVelocityMultiplier;
public void Awake() {
xpMultiplier = Config.Bind("General", "XP Multiplier", 1);
moneyMultiplier = Config.Bind("General", "Money Multiplier", 1);
energyGenMultiplier = Config.Bind("General", "Energy Generation Multiplier", 1f);
shootingSpeedMultiplier = Config.Bind("General", "Shooting Speed Multiplier", 1f);
muzzleVelocityMultiplier = Config.Bind("General", "Muzzle Velocity Multiplier", 1f);
allProjectilesHoming = Config.Bind("General", "Make All Projectiles Home", false);
shootingSpeedMultiplier.SettingChanged += (sender, args) => WeaponPropertiesManager.DoPatch();
energyGenMultiplier.SettingChanged += (sender, args) => GeneratorPropertiesManager.DoPatch();
Logger.LogInfo("Cyka mod loaded");
HarmonyFileLog.Enabled = true;
Harmony harmony = new Harmony(pluginGuid);
harmony.PatchAll();
var originalMethods = harmony.GetPatchedMethods();
Logger.LogInfo("Patched " + originalMethods.Count() + " methods");
}
}
[HarmonyPatch]
public class Patches {
[HarmonyPatch(typeof(ManLicenses), "AddXP")]
static void XPMulti(FactionSubTypes corporation, ref int xp, bool showUI = true) {
xp *= Main.xpMultiplier.Value;
}
[HarmonyPatch(typeof(ManPlayer), "AddMoney")]
static void MoneyMulti(ref int amount) {
amount *= Main.moneyMultiplier.Value;
}
}
[HarmonyPatch(typeof(Projectile), "Fire")]
public class HomingProjectilePatch {
private static Dictionary<FireData, bool> patchedObjects = new Dictionary<FireData, bool>();
static void Prefix(Vector3 fireDirection, ref FireData fireData, ref ModuleWeaponGun weapon, Tank shooter, ref bool seekingRounds, bool replayRounds) {
if (Main.allProjectilesHoming.Value) {
seekingRounds = true;
}
if (Main.muzzleVelocityMultiplier.Value != -1f && !patchedObjects.ContainsKey(fireData)) {
fireData.m_MuzzleVelocity *= Main.muzzleVelocityMultiplier.Value;
patchedObjects.Add(fireData, true);
}
}
}
[HarmonyPatch]
public class WeaponPropertiesManager {
private static Dictionary<ModuleWeaponGun, float> weapons = new Dictionary<ModuleWeaponGun, float>();
[HarmonyPostfix]
[HarmonyPatch(typeof(ModuleWeaponGun), "OnAttached")]
static void PostfixCreate(ModuleWeaponGun __instance) {
if (!weapons.ContainsKey(__instance)) {
weapons.Add(__instance, __instance.m_ShotCooldown);
DoPatchSingle(__instance);
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(ModuleWeaponGun), "OnDetaching")]
static void PostfixDestroy(ModuleWeaponGun __instance) {
DoRestoreSingle(__instance);
weapons.Remove(__instance);
}
public static void DoPatch() {
Console.WriteLine("Modifying " + weapons.Count + " weapons");
foreach (KeyValuePair<ModuleWeaponGun, float> keyValuePair in weapons) {
DoRestoreSingle(keyValuePair.Key);
DoPatchSingle(keyValuePair.Key);
}
}
static void DoPatchSingle(ModuleWeaponGun weapon) {
Console.WriteLine("Patching " + weapon.name);
Console.WriteLine("Old value " + weapon.m_ShotCooldown);
weapon.m_ShotCooldown /= Main.shootingSpeedMultiplier.Value;
Console.WriteLine("New value " + weapon.m_ShotCooldown);
}
static void DoRestoreSingle(ModuleWeaponGun weapon) {
if (weapons.ContainsKey(weapon)) {
weapon.m_ShotCooldown = weapons[weapon];
}
}
}
[HarmonyPatch]
public class GeneratorPropertiesManager {
private static Dictionary<ModuleEnergy, float> generators = new Dictionary<ModuleEnergy, float>();
[HarmonyPostfix]
[HarmonyPatch(typeof(ModuleEnergy), "OnAttached")]
static void PostfixCreate(ModuleEnergy __instance) {
if (!generators.ContainsKey(__instance)) {
generators.Add(__instance, GetValue(__instance));
if (ShouldPatch()) {
DoPatchSingle(__instance);
}
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(ModuleEnergy), "OnDetaching")]
static void PostfixDestroy(ModuleEnergy __instance) {
DoRestoreSingle(__instance);
generators.Remove(__instance);
}
public static void DoPatch() {
Console.WriteLine("Modifying " + generators.Count + " generators");
Console.WriteLine("Should patch: " + ShouldPatch());
foreach (KeyValuePair<ModuleEnergy, float> keyValuePair in generators) {
DoRestoreSingle(keyValuePair.Key);
DoPatchSingle(keyValuePair.Key);
}
}
static void DoPatchSingle(ModuleEnergy moduleEnergy) {
Console.WriteLine("Patching " + moduleEnergy.name);
Console.WriteLine("Old value " + GetValue(moduleEnergy));
SetValue(moduleEnergy, GetValue(moduleEnergy) * Main.energyGenMultiplier.Value);
Console.WriteLine("New value " + GetValue(moduleEnergy));
}
static void DoRestoreSingle(ModuleEnergy moduleEnergy) {
if (generators.ContainsKey(moduleEnergy)) {
SetValue(moduleEnergy, generators[moduleEnergy]);
}
}
static bool ShouldPatch() {
return Math.Abs(Main.energyGenMultiplier.Value - 1f) > 0.0001f;
}
private static float GetValue(ModuleEnergy moduleEnergy) {
return Traverse.Create(moduleEnergy).Field("m_OutputPerSecond").GetValue() as float? ?? 0f;
}
private static void SetValue(ModuleEnergy moduleEnergy, float value) {
Traverse.Create(moduleEnergy).Field("m_OutputPerSecond").SetValue(value);
}
}
}