Decompiled source of SturdyFloors v1.0.0

SturdyFloors.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: AssemblyTitle("SturdyFloors")]
[assembly: AssemblyDescription("Unbreakable floors for R.E.P.O.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyMetadata("AI_Assisted_Creation", "This assembly was fully created with the assistance of Generative AI (code generation, reverse engineering of game code, testing and documentation).")]
[assembly: AssemblyMetadata("AI_Model_Vendor", "Anthropic (Claude)")]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SturdyFloors;

[BepInPlugin("delish.SturdyFloors", "SturdyFloors", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string Guid = "delish.SturdyFloors";

	public const string Name = "SturdyFloors";

	public const string Version = "1.0.0";

	internal static ManualLogSource Log;

	internal static Plugin Instance;

	internal static ConfigEntry<bool> UnbreakableFloors;

	internal static ConfigEntry<bool> BreakTumblesPlayers;

	internal static ConfigEntry<bool> BreakDamagesPlayers;

	internal static ConfigEntry<bool> SunstoneOnly;

	internal static ConfigEntry<int> TestStartLevel;

	internal static ConfigEntry<bool> NoMonsters;

	internal static ConfigEntry<bool> LogFloors;

	private void Awake()
	{
		//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fc: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		Instance = this;
		UnbreakableFloors = ((BaseUnityPlugin)this).Config.Bind<bool>("Floors", "UnbreakableFloors", true, "When true, breakable floors (the wooden planks in Sunstone Valley Resort) never break. Set to false to let floors break, and use the [Floor Breaks] settings to soften what happens.");
		BreakTumblesPlayers = ((BaseUnityPlugin)this).Config.Bind<bool>("Floor Breaks", "BreakTumblesPlayers", true, "Only used when UnbreakableFloors = false. When false, players on a breaking floor just drop through instead of being knocked into a tumble (which also means no landing damage).");
		BreakDamagesPlayers = ((BaseUnityPlugin)this).Config.Bind<bool>("Floor Breaks", "BreakDamagesPlayers", true, "Only used when UnbreakableFloors = false. When false, the tumble from a breaking floor does no damage when you land (normally 10 HP).");
		SunstoneOnly = ((BaseUnityPlugin)this).Config.Bind<bool>("Levels", "SunstoneOnly", false, "When true, every run level is Sunstone Valley Resort (the shop and arena are unchanged).");
		TestStartLevel = ((BaseUnityPlugin)this).Config.Bind<int>("Testing", "TestStartLevel", 0, "For testing: if above 0, the run jumps straight to at least this level number. Later levels are bigger and have more of the rooms with breakable floors (10-20 is good). Monsters and loot scale up too, and the save keeps the higher level, so use a spare save. Set back to 0 for normal play.");
		NoMonsters = ((BaseUnityPlugin)this).Config.Bind<bool>("Testing", "NoMonsters", false, "For testing: if true, no monsters spawn in run levels. Applies from the next level that loads.");
		LogFloors = ((BaseUnityPlugin)this).Config.Bind<bool>("Testing", "LogFloors", false, "Write a line to the BepInEx log for every breakable floor in a level (with its room name), each time one is stopped from breaking, and when one breaks under a player.");
		Harmony val = new Harmony("delish.SturdyFloors");
		Type[] types = typeof(Plugin).Assembly.GetTypes();
		foreach (Type type in types)
		{
			if (type.GetCustomAttributes(typeof(HarmonyPatch), inherit: false).Length != 0)
			{
				try
				{
					val.CreateClassProcessor(type).Patch();
				}
				catch (Exception ex)
				{
					Log.LogError((object)("Patch " + type.Name + " failed; that feature is off. " + ex.GetBaseException().Message));
				}
			}
		}
		Log.LogInfo((object)("SturdyFloors 1.0.0 loaded. Unbreakable floors: " + UnbreakableFloors.Value + ", Sunstone only: " + SunstoneOnly.Value + ", test start level: " + TestStartLevel.Value + ", no monsters: " + NoMonsters.Value));
	}

	internal static void ReloadConfig()
	{
		try
		{
			((BaseUnityPlugin)Instance).Config.Reload();
		}
		catch (Exception ex)
		{
			Log.LogWarning((object)("Could not re-read config: " + ex.Message));
		}
	}
}
[HarmonyPatch(typeof(BreakableFloor), "TriggerImpulse")]
internal static class TriggerImpulsePatch
{
	private static readonly HashSet<int> Reported = new HashSet<int>();

	private static bool Prefix(BreakableFloor __instance)
	{
		if (!Plugin.UnbreakableFloors.Value)
		{
			return true;
		}
		if (Plugin.LogFloors.Value && Reported.Add(((Object)__instance).GetInstanceID()))
		{
			Plugin.Log.LogInfo((object)("BLOCKED a floor from breaking in room '" + FloorSpawnPatch.RoomName((Component)(object)__instance) + "'."));
		}
		return false;
	}
}
[HarmonyPatch(typeof(BreakableFloor), "StateSet")]
internal static class StateSetPatch
{
	private static bool Prefix(object[] __args)
	{
		if (!Plugin.UnbreakableFloors.Value)
		{
			return true;
		}
		return Convert.ToInt32(__args[0]) == 0;
	}
}
[HarmonyPatch(typeof(BreakableFloor), "StunColliderLogic")]
internal static class FloorBreakEffectsPatch
{
	private static bool Tumble => Plugin.BreakTumblesPlayers.Value;

	private static bool Damage => Plugin.BreakTumblesPlayers.Value && Plugin.BreakDamagesPlayers.Value;

	private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
	{
		int replaced = 0;
		foreach (CodeInstruction ci in instructions)
		{
			MethodInfo m = ci.operand as MethodInfo;
			if ((ci.opcode == OpCodes.Call || ci.opcode == OpCodes.Callvirt) && m != null && m.DeclaringType == typeof(PlayerTumble))
			{
				MethodInfo methodInfo = AccessTools.Method(typeof(FloorBreakEffectsPatch), m.Name + "Hook", (Type[])null, (Type[])null);
				if (methodInfo != null)
				{
					ci.opcode = OpCodes.Call;
					ci.operand = methodInfo;
					replaced++;
				}
			}
			yield return ci;
		}
		if (replaced != 5)
		{
			Plugin.Log.LogWarning((object)("Floor break tumble/damage options: expected 5 game calls, found " + replaced + ". The game may have changed."));
		}
	}

	internal static void TumbleRequestHook(PlayerTumble t, bool isTumbling, bool playerInput)
	{
		if (Plugin.LogFloors.Value)
		{
			Plugin.Log.LogInfo((object)("A floor broke under a player. Tumble: " + ((!Tumble) ? "off" : "on") + ", landing damage: " + ((!Damage) ? "off" : "on") + "."));
		}
		if (Tumble)
		{
			t.TumbleRequest(isTumbling, playerInput);
		}
	}

	internal static void TumbleForceHook(PlayerTumble t, Vector3 force)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		if (Tumble)
		{
			t.TumbleForce(force);
		}
	}

	internal static void TumbleTorqueHook(PlayerTumble t, Vector3 torque)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		if (Tumble)
		{
			t.TumbleTorque(torque);
		}
	}

	internal static void TumbleOverrideTimeHook(PlayerTumble t, float time)
	{
		if (Tumble)
		{
			t.TumbleOverrideTime(time);
		}
	}

	internal static void ImpactHurtSetHook(PlayerTumble t, float time, int damage)
	{
		if (Damage)
		{
			t.ImpactHurtSet(time, damage);
		}
	}
}
[HarmonyPatch(typeof(RunManager), "SetRunLevel")]
internal static class SetRunLevelPatch
{
	private static void Prefix()
	{
		Plugin.ReloadConfig();
	}

	private static void Postfix(RunManager __instance)
	{
		int value = Plugin.TestStartLevel.Value;
		if (value > 0 && __instance.levelsCompleted < value - 1)
		{
			Plugin.Log.LogInfo((object)("Testing: jumping from level " + (__instance.levelsCompleted + 1) + " to level " + value + "."));
			__instance.levelsCompleted = value - 1;
		}
		if (Plugin.SunstoneOnly.Value)
		{
			Level val = FindSunstone(__instance.levels);
			if (object.ReferenceEquals(val, null))
			{
				Plugin.Log.LogWarning((object)"Sunstone Valley Resort not found in the level list; leaving the game's pick.");
			}
			else
			{
				__instance.levelCurrent = val;
			}
		}
	}

	internal static Level FindSunstone(List<Level> levels)
	{
		if (levels == null)
		{
			return null;
		}
		foreach (Level level in levels)
		{
			if (object.ReferenceEquals(level, null) || level.NarrativeName == null || level.NarrativeName.IndexOf("Sunstone", StringComparison.OrdinalIgnoreCase) < 0)
			{
				continue;
			}
			return level;
		}
		foreach (Level level2 in levels)
		{
			if (object.ReferenceEquals(level2, null) || !(((Object)level2).name == "Level - Desert"))
			{
				continue;
			}
			return level2;
		}
		return null;
	}
}
[HarmonyPatch(typeof(BreakableFloor), "Awake")]
internal static class FloorSpawnPatch
{
	private static void Postfix(BreakableFloor __instance)
	{
		if (Plugin.LogFloors.Value)
		{
			int num = Object.FindObjectsByType<BreakableFloor>((FindObjectsSortMode)0).Length;
			Plugin.Log.LogInfo((object)("Breakable floor #" + num + " on this level, in room '" + RoomName((Component)(object)__instance) + "'."));
		}
	}

	internal static string RoomName(Component c)
	{
		Module componentInParent = c.GetComponentInParent<Module>();
		string text = ((!((Object)(object)componentInParent != (Object)null)) ? ((Object)((Component)c.transform.root).gameObject).name : ((Object)((Component)componentInParent).gameObject).name);
		return text.Replace("(Clone)", string.Empty).Trim();
	}
}
[HarmonyPatch(typeof(LevelGenerator), "Generate")]
internal static class NoMonstersPatch
{
	private static void Prefix(ref bool ___DebugNoEnemy)
	{
		Plugin.ReloadConfig();
		if (Plugin.NoMonsters.Value)
		{
			___DebugNoEnemy = true;
			Level val = ((!object.ReferenceEquals(RunManager.instance, null)) ? RunManager.instance.levelCurrent : null);
			if (!object.ReferenceEquals(val, null) && val.HasEnemies)
			{
				Plugin.Log.LogInfo((object)"Testing: no monsters will spawn on this level.");
			}
		}
	}
}