Decompiled source of InfiniteMoney v1.0.0

InfiniteMoney.dll

Decompiled an hour ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using HarmonyLib;
using InfiniteMoney;
using MelonLoader;
using MelonLoader.Preferences;

[assembly: MelonInfo(typeof(Core), "InfiniteMoney", "1.0.0", "MimesisModder", null)]
[assembly: MelonGame("ReLUGames", "MIMESIS")]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace InfiniteMoney;

public class Core : MelonMod
{
	private MelonPreferences_Entry<bool> _enabled;

	private MelonPreferences_Entry<int> _amount;

	private MelonPreferences_Entry<bool> _verbose;

	private readonly Stopwatch _clock = Stopwatch.StartNew();

	private long _nextCheck;

	private bool _roomAnnounced;

	private bool _errorLogged;

	public override void OnInitializeMelon()
	{
		MelonPreferences_Category val = MelonPreferences.CreateCategory("InfiniteMoney");
		_enabled = val.CreateEntry<bool>("Enabled", true, (string)null, "Turn the mod on or off.", false, false, (ValueValidator)null, (string)null);
		_amount = val.CreateEntry<int>("Amount", 999999999, (string)null, "Currency value the mod keeps you at (never lowers your money).", false, false, (ValueValidator)null, (string)null);
		_verbose = val.CreateEntry<bool>("Verbose", false, (string)null, "Log every top-up (for troubleshooting).", false, false, (ValueValidator)null, (string)null);
		MoneyLogic.IsEnabled = () => _enabled.Value;
		MoneyLogic.GetAmount = () => _amount.Value;
		int num = 0;
		try
		{
			num = Hooks.Install(((MelonBase)this).HarmonyInstance);
		}
		catch (Exception ex)
		{
			((MelonBase)this).LoggerInstance.Warning("Could not install purchase hooks: " + ex.Message);
		}
		((MelonBase)this).LoggerInstance.Msg("InfiniteMoney loaded (" + num + " hooks). Host-only effect: the lobby host owns the money.");
	}

	public override void OnUpdate()
	{
		if (!_enabled.Value)
		{
			return;
		}
		long elapsedMilliseconds = _clock.ElapsedMilliseconds;
		if (elapsedMilliseconds < _nextCheck)
		{
			return;
		}
		_nextCheck = elapsedMilliseconds + 1000;
		try
		{
			object obj = MoneyLogic.FindMaintenanceRoom(MoneyLogic.FindUnityObject);
			if (obj == null)
			{
				_roomAnnounced = false;
				return;
			}
			bool flag = MoneyLogic.TopUp(obj, _amount.Value);
			if (!_roomAnnounced)
			{
				_roomAnnounced = true;
				((MelonBase)this).LoggerInstance.Msg("Money found. Keeping currency at " + _amount.Value + ".");
			}
			else if (flag && _verbose.Value)
			{
				((MelonBase)this).LoggerInstance.Msg("Topped up currency to " + _amount.Value + ".");
			}
		}
		catch (Exception ex)
		{
			if (!_errorLogged)
			{
				_errorLogged = true;
				((MelonBase)this).LoggerInstance.Warning("Top-up failed (will keep retrying quietly): " + ex.Message);
			}
		}
	}
}
internal static class MoneyLogic
{
	private const BindingFlags Any = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

	public static Func<bool> IsEnabled = () => true;

	public static Func<long> GetAmount = () => 999999999L;

	private static readonly Dictionary<string, Type> TypeCache = new Dictionary<string, Type>();

	private static MethodInfo _unityFind;

	private static bool _unityFindResolved;

	public static Type FindType(string name)
	{
		if (TypeCache.TryGetValue(name, out var value) && value != null)
		{
			return value;
		}
		Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault((Assembly a) => a.GetName().Name == "Assembly-CSharp");
		if (assembly == null)
		{
			return null;
		}
		Type[] source;
		try
		{
			source = assembly.GetTypes();
		}
		catch (ReflectionTypeLoadException ex)
		{
			source = ex.Types.Where((Type t) => t != null).ToArray();
		}
		Type type = source.FirstOrDefault((Type t) => t.FullName == name) ?? source.FirstOrDefault((Type t) => t.Name == name);
		TypeCache[name] = type;
		return type;
	}

	public static IEnumerable<MethodInfo> FindMethods(Type type, string name)
	{
		Type t = type;
		while (t != null)
		{
			MethodInfo[] methods = t.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (MethodInfo m in methods)
			{
				if (m.Name == name)
				{
					yield return m;
				}
			}
			t = t.BaseType;
		}
	}

	private static PropertyInfo FindProperty(Type type, string name)
	{
		Type type2 = type;
		while (type2 != null)
		{
			PropertyInfo property = type2.GetProperty(name, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			if (property != null)
			{
				return property;
			}
			type2 = type2.BaseType;
		}
		return null;
	}

	public static object GetMemberValue(object obj, string name)
	{
		if (obj == null)
		{
			return null;
		}
		Type type = obj.GetType();
		while (type != null)
		{
			FieldInfo field = type.GetField(name, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			if (field != null)
			{
				return field.GetValue(obj);
			}
			PropertyInfo property = type.GetProperty(name, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			if (property != null && property.GetIndexParameters().Length == 0 && property.GetGetMethod(nonPublic: true) != null)
			{
				return property.GetValue(obj, null);
			}
			type = type.BaseType;
		}
		return null;
	}

	public static object FindUnityObject(Type type)
	{
		if (!_unityFindResolved)
		{
			_unityFindResolved = true;
			Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault((Assembly a) => a.GetName().Name == "UnityEngine.CoreModule");
			Type type2 = ((!(assembly == null)) ? assembly.GetType("UnityEngine.Object") : null);
			if (type2 != null)
			{
				Type[] types = new Type[1] { typeof(Type) };
				BindingFlags bindingAttr = BindingFlags.Static | BindingFlags.Public;
				_unityFind = type2.GetMethod("FindFirstObjectByType", bindingAttr, null, types, null) ?? type2.GetMethod("FindObjectOfType", bindingAttr, null, types, null);
			}
		}
		return (!(_unityFind == null)) ? _unityFind.Invoke(null, new object[1] { type }) : null;
	}

	private static bool IsMaintenanceRoom(object o)
	{
		Type type = o.GetType();
		while (type != null)
		{
			if (type.Name == "MaintenanceRoom")
			{
				return true;
			}
			type = type.BaseType;
		}
		return false;
	}

	public static object FindMaintenanceRoom(Func<Type, object> findUnityObject)
	{
		Type type = FindType("Hub");
		if (type == null)
		{
			return null;
		}
		object obj = findUnityObject(type);
		object obj2 = GetMemberValue(obj, "vworld") ?? GetMemberValue(obj, "VWorld");
		object obj3 = GetMemberValue(obj2, "_vRoomManager") ?? GetMemberValue(obj2, "VRoomManager");
		if (!((GetMemberValue(obj3, "_vrooms") ?? GetMemberValue(obj3, "VRooms")) is IDictionary dictionary))
		{
			return null;
		}
		foreach (DictionaryEntry item in dictionary)
		{
			if (item.Value != null && IsMaintenanceRoom(item.Value))
			{
				return item.Value;
			}
		}
		return null;
	}

	public static bool TopUp(object room, long target)
	{
		if (room == null)
		{
			return false;
		}
		Type type = room.GetType();
		PropertyInfo propertyInfo = FindProperty(type, "Currency");
		if (propertyInfo == null || propertyInfo.GetGetMethod(nonPublic: true) == null)
		{
			return false;
		}
		long num = Convert.ToInt64(propertyInfo.GetValue(room, null));
		if (num >= target)
		{
			return false;
		}
		MethodInfo methodInfo = FindMethods(type, "AddCurrency").FirstOrDefault((MethodInfo m) => m.GetParameters().Length == 1);
		if (methodInfo != null)
		{
			object obj = Convert.ChangeType(target - num, methodInfo.GetParameters()[0].ParameterType);
			methodInfo.Invoke(room, new object[1] { obj });
			return true;
		}
		MethodInfo setMethod = propertyInfo.GetSetMethod(nonPublic: true);
		if (setMethod != null)
		{
			setMethod.Invoke(room, new object[1] { Convert.ChangeType(target, propertyInfo.PropertyType) });
			return true;
		}
		return false;
	}
}
internal static class Hooks
{
	public static void BeforeBuy(object __instance)
	{
		try
		{
			if (MoneyLogic.IsEnabled())
			{
				MoneyLogic.TopUp(__instance, MoneyLogic.GetAmount());
			}
		}
		catch
		{
		}
	}

	public static void BeforeRepair(object __instance)
	{
		try
		{
			if (MoneyLogic.IsEnabled())
			{
				object memberValue = MoneyLogic.GetMemberValue(__instance, "VRoom");
				if (memberValue != null)
				{
					MoneyLogic.TopUp(memberValue, MoneyLogic.GetAmount());
				}
			}
		}
		catch
		{
		}
	}

	public static int Install(Harmony harmony)
	{
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Expected O, but got Unknown
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Expected O, but got Unknown
		int num = 0;
		Type type = MoneyLogic.FindType("MaintenanceRoom");
		if (type != null)
		{
			HarmonyMethod val = new HarmonyMethod(typeof(Hooks).GetMethod("BeforeBuy"));
			MethodInfo[] methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (MethodInfo methodInfo in methods)
			{
				if (!(methodInfo.Name != "BuyItem"))
				{
					harmony.Patch((MethodBase)methodInfo, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					num++;
				}
			}
		}
		Type type2 = MoneyLogic.FindType("VPlayer");
		if (type2 != null)
		{
			HarmonyMethod val2 = new HarmonyMethod(typeof(Hooks).GetMethod("BeforeRepair"));
			MethodInfo[] methods2 = type2.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (MethodInfo methodInfo2 in methods2)
			{
				if (!(methodInfo2.Name != "HandleRepairTrain"))
				{
					harmony.Patch((MethodBase)methodInfo2, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					num++;
				}
			}
		}
		return num;
	}
}