Decompiled source of Hospital Fix v1.0.0

FS_HospitalFix.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Script.LevelApplication.Events;
using Script.LevelApplication.LoadGame;
using Script.LevelApplication.SaveGame;
using Script.Players;
using Script.Players.Construction;
using Script.Players.Construction.Bonuses;
using Script.Players.Construction.Buildings;
using TMPro;
using UnityEngine;
using UnityEngine.AzureSky;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("FS_HospitalFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+45c8950f9ed256048ebcfa650138597e2103caed")]
[assembly: AssemblyProduct("FS_HospitalFix")]
[assembly: AssemblyTitle("FS_HospitalFix")]
[assembly: AssemblyVersion("1.0.0.0")]
internal static class ModLog
{
	private static ManualLogSource _bepInExLogger;

	public static string LogPrefix { get; set; }

	public static void Init()
	{
		if (_bepInExLogger == null)
		{
			_bepInExLogger = Logger.CreateLogSource(LogPrefix ?? "");
		}
	}

	internal static void Info(string message, string tag = "")
	{
		_bepInExLogger.LogInfo((object)FormatMessage(message, tag));
	}

	internal static void Log(string message, string tag = "")
	{
		_bepInExLogger.LogMessage((object)FormatMessage(message, tag));
	}

	internal static void Debug(string message, string tag = "")
	{
	}

	internal static void Warn(string message, string tag = "")
	{
		_bepInExLogger.LogWarning((object)FormatMessage(message, tag));
	}

	internal static void Error(string message, string tag = "")
	{
		_bepInExLogger.LogError((object)FormatMessage(message, tag));
	}

	private static string FormatMessage(string message, string tag)
	{
		if (string.IsNullOrEmpty(tag))
		{
			return message;
		}
		return "[" + tag + "] " + message;
	}
}
internal static class ModInfo
{
	internal const string PLUGIN_GUID = "com.maxgerman.hospitalfix";

	internal const string PLUGIN_NAME = "Hospital Fix";

	internal const string PLUGIN_VERSION = "1.0.0";

	internal const string ASSEMBLY_VERSION = "1.0.0";

	internal const string FILE_VERSION = "1.0.0";

	internal const string INFORMATIONAL_VERSION = "1.0.0";

	internal const string HARMONY_ID = "com.maxgerman.hospitalfix";

	internal const string LOG_PREFIX = "Hospital Fix";

	internal const string DISPLAY_NAME_SHORT = "Hospital Fix";

	internal const string DISPLAY_NAME_LONG = "Hospital Fix";
}
public static class GameTimeHelper
{
	public static int CachedCurrentDay;

	public static void FastReadDayFromLiveUI()
	{
		try
		{
			GameObject val = GameObject.Find("Canvas/Ship/ShipScreen/ResourcePanel/DateTimePanel/DateTimeLayoutGroup/DayText (TMP)");
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			TMP_Text component = val.GetComponent<TMP_Text>();
			if ((Object)(object)component == (Object)null || string.IsNullOrWhiteSpace(component.text))
			{
				return;
			}
			string text = component.text;
			string text2 = string.Empty;
			string text3 = text;
			for (int i = 0; i < text3.Length; i++)
			{
				char c = text3[i];
				if (c >= '0' && c <= '9')
				{
					text2 += c;
				}
			}
			if (int.TryParse(text2, out var result) && result > 0 && result < 500)
			{
				CachedCurrentDay = result;
			}
		}
		catch (Exception ex)
		{
			ModLog.Debug("Day read error: " + ex.Message);
		}
	}

	public static int GetDay()
	{
		return CachedCurrentDay;
	}

	public static string GetTime()
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		AzureTimeController instance = AzureTimeController.Instance;
		if ((Object)(object)instance == (Object)null)
		{
			return "--:--";
		}
		Vector2 timeOfDay = instance.GetTimeOfDay();
		int value = Mathf.Clamp(Mathf.FloorToInt(timeOfDay.x), 0, 23);
		int value2 = Mathf.Clamp(Mathf.FloorToInt(timeOfDay.y), 0, 59);
		return $"{value:00}:{value2:00}";
	}
}
namespace HospitalFixMod;

public static class HospitalPatches
{
	public static Ship DirectShipInstance;

	public static void ConvertInjuredToSick(Ship ship)
	{
		if (ship == null)
		{
			return;
		}
		try
		{
			int cachedCurrentDay = GameTimeHelper.CachedCurrentDay;
			int value = ModMain.DaysToConvertConfig.Value;
			int injuryPeople = ship.InjuryPeople;
			int sickPeople = ship.SickPeople;
			if (injuryPeople <= 0)
			{
				ModLog.Info("Fortunately, there are no injuries patients.");
				return;
			}
			if (cachedCurrentDay % value != 0)
			{
				ModLog.Info("This isn't the day; maybe tomorrow.");
				return;
			}
			int maxHospitalBeds = GetMaxHospitalBeds(ship);
			ModLog.Info($"Maximum number of hospital beds: {maxHospitalBeds}");
			if (maxHospitalBeds <= 0)
			{
				ModLog.Info("Hospital capacity is set to 0 or less.");
				return;
			}
			int num = maxHospitalBeds - sickPeople;
			if (num <= 0)
			{
				ModLog.Info($"Hospital is full: {sickPeople}/{maxHospitalBeds}. " + "No injured patients will be transferred.");
				return;
			}
			int value2 = ModMain.ConvertBatchSizeConfig.Value;
			if (value2 <= 0)
			{
				ModLog.Info("Conversion batch size is set to 0 or less.");
				return;
			}
			int val = Math.Min(value2, injuryPeople);
			val = Math.Min(val, num);
			ModLog.Info($"Daily treatment: Injured={injuryPeople}, Sick={sickPeople}/{maxHospitalBeds}, Transfering={val}.");
			ship.RemoveInjuryPeople(val);
			ship.AddSickPeople(val);
			ship.SyncSickPeopleWithHospital();
			ModLog.Info($"Success: Injured={ship.InjuryPeople}, Sick={ship.SickPeople}/{maxHospitalBeds}.");
		}
		catch (Exception value3)
		{
			ModLog.Warn($"Error while transferring injured patients: {value3}");
		}
	}

	public static int GetMaxHospitalBeds(Ship ship)
	{
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		object obj;
		if (ship == null)
		{
			obj = null;
		}
		else
		{
			ShipConstructions buildings = ship.Buildings;
			obj = ((buildings != null) ? buildings.GetHospital() : null);
		}
		Hospital val = (Hospital)obj;
		if (val == null)
		{
			return 0;
		}
		BuildingLevel val2 = default(BuildingLevel);
		if (!((AbstractBuilding)val).Levels.TryGetValue(((AbstractBuilding)val).CurrentLevel, ref val2) || ((val2 != null) ? val2.Bonuses : null) == null)
		{
			return 0;
		}
		IntPtr nativeClassPtr = Il2CppClassPointerStore<HospitalSettings>.NativeClassPtr;
		foreach (AbstractBuildingBonus item in (Il2CppArrayBase<AbstractBuildingBonus>)(object)val2.Bonuses)
		{
			if (item != null && ((Il2CppObjectBase)item).ObjectClass == nativeClassPtr)
			{
				return new HospitalSettings(((Il2CppObjectBase)item).Pointer).NumberOfHospitalBeds;
			}
		}
		return 0;
	}
}
[HarmonyPatch(typeof(Ship), "RecalculateWorkPeople")]
public static class RecalculateWorkPeople_Patch
{
	[HarmonyPatch(typeof(LoadGameManager), "LoadSave")]
	public static class LoadSave_Patch
	{
		[HarmonyPostfix]
		public static void Postfix(SaveData __0)
		{
			if (__0.Level == null)
			{
				ModLog.Info("[LoadSave] working but SaveData = null");
			}
			else
			{
				GameTimeHelper.CachedCurrentDay = __0.Level.DayNumber;
			}
		}
	}

	[HarmonyPatch(typeof(NewDayEvent), "Invoke")]
	public static class NewDayEvent_Patch
	{
		[HarmonyPostfix]
		public static void Postfix()
		{
			GameTimeHelper.FastReadDayFromLiveUI();
			HospitalPatches.ConvertInjuredToSick(HospitalPatches.DirectShipInstance);
		}
	}

	[HarmonyPostfix]
	public static void Postfix(Ship __instance)
	{
		if (__instance != null)
		{
			HospitalPatches.DirectShipInstance = __instance;
		}
	}
}
[BepInPlugin("com.maxgerman.hospitalfix", "Hospital Fix", "1.0.0")]
public class ModMain : BasePlugin
{
	public static ModMain Instance;

	public static ConfigEntry<int> DaysToConvertConfig;

	public static ConfigEntry<int> ConvertBatchSizeConfig;

	public override void Load()
	{
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		ModLog.LogPrefix = "Hospital Fix";
		ModLog.Init();
		ModLog.Info("Starting Hospital Fix Mod...");
		DaysToConvertConfig = ((BasePlugin)this).Config.Bind<int>("Settings", "ConversionInterval", 3, "How many days apart should injured patients be transferred to the hospital.\nThe transfer will take place on an in-game day divisible by this number.");
		ConvertBatchSizeConfig = ((BasePlugin)this).Config.Bind<int>("Settings", "ConversionBatchSize", 1, "Number of injured patients transferred from ICU to hospital wards per conversion day.");
		try
		{
			new Harmony("com.maxgerman.hospitalfix").PatchAll();
			ModLog.Info("Harmony patches applied successfully!");
		}
		catch (Exception ex)
		{
			ModLog.Error("Harmony patching failed: " + ex);
		}
	}
}