Decompiled source of MorePopulation v1.6.0

BepInEx/plugins/cs.HoLMod.MorePopulation.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("cs.HoLMod.MorePopulation")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("cs.HoLMod.MorePopulation")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("d1e8ebb6-3e94-4ac1-a0a1-bcbc5bdc0ff2")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7", FrameworkDisplayName = ".NET Framework 4.7")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace cs.HoLMod.MorePopulation;

public static class PluginInfo
{
	public const string PLUGIN_GUID = "cs.HoLMod.MorePopulation.AnZhi20";

	public const string PLUGIN_NAME = "HoLMod.MorePopulation";

	public const string PLUGIN_VERSION = "1.5.0";
}
[BepInPlugin("cs.HoLMod.MorePopulation.AnZhi20", "HoLMod.MorePopulation", "1.5.0")]
public class Main : BaseUnityPlugin
{
	public static int PopulationCheatRate;

	public static float PopulationCorrectionRate;

	private int lastCorrectionYearMonth = 0;

	public static Main Instance { get; private set; }

	public static ManualLogSource Log
	{
		get
		{
			Main instance = Instance;
			return (instance != null) ? ((BaseUnityPlugin)instance).Logger : null;
		}
	}

	private void Awake()
	{
		Instance = this;
		PopulationCheatRate = ((BaseUnityPlugin)this).Config.Bind<int>("倍率调整(Adjustment)", "自然增加人口修正(Population Correction)", 1, "每月自然增加人口修正倍数,填1为默认修正值。(Monthly natural population growth correction factor, set to 1 for default value).").Value;
	}

	private void Update()
	{
		HandlePopulationCorrection();
	}

	private void HandlePopulationCorrection()
	{
		try
		{
			if ((Object)(object)Instance == (Object)null || string.IsNullOrEmpty(Mainload.SceneID) || Mainload.Time_now == null || Mainload.Time_now.Count < 3)
			{
				return;
			}
			string[] array = Mainload.SceneID.Split(new char[1] { '|' });
			if (array == null || array.Length == 0)
			{
				ManualLogSource log = Log;
				if (log != null)
				{
					log.LogWarning((object)"场景ID分割后为空,无法确定场景类型");
				}
				return;
			}
			string text = array[0];
			if (text != "M" && text != "Z" && text != "S" && text != "H" && text != "F" && text != "L")
			{
				ManualLogSource log2 = Log;
				if (log2 != null)
				{
					log2.LogWarning((object)("当前场景类型[" + text + "]不是有效游戏场景,无法执行人口修正"));
				}
				return;
			}
			int num = Mainload.Time_now[0];
			int num2 = Mainload.Time_now[1];
			int num3 = Mainload.Time_now[2];
			switch (num)
			{
			case 1:
				PopulationCorrectionRate = 0.1f;
				break;
			case 2:
				PopulationCorrectionRate = 0.2f;
				break;
			case 3:
				PopulationCorrectionRate = 0.3f;
				break;
			case 4:
				PopulationCorrectionRate = 0.4f;
				break;
			case 5:
				PopulationCorrectionRate = 0.5f;
				break;
			case 6:
				PopulationCorrectionRate = 0.6f;
				break;
			case 7:
				PopulationCorrectionRate = 0.7f;
				break;
			case 8:
				PopulationCorrectionRate = 0.8f;
				break;
			case 9:
				PopulationCorrectionRate = 0.9f;
				break;
			case 10:
				PopulationCorrectionRate = 1f;
				break;
			case 11:
				PopulationCorrectionRate = 1.2f;
				break;
			case 12:
				PopulationCorrectionRate = 1.4f;
				break;
			case 13:
				PopulationCorrectionRate = 1.6f;
				break;
			case 14:
				PopulationCorrectionRate = 1.8f;
				break;
			case 15:
				PopulationCorrectionRate = 2f;
				break;
			case 16:
				PopulationCorrectionRate = 2.2f;
				break;
			case 17:
				PopulationCorrectionRate = 2.4f;
				break;
			case 18:
				PopulationCorrectionRate = 2.6f;
				break;
			case 19:
				PopulationCorrectionRate = 2.8f;
				break;
			case 20:
				PopulationCorrectionRate = 3f;
				break;
			default:
				PopulationCorrectionRate = 4f;
				break;
			}
			if (num3 != 1)
			{
				return;
			}
			int num4 = num * 100 + num2;
			if (num4 != lastCorrectionYearMonth)
			{
				PluginCommonInfo.PopulationCorrectionRate = (int)PopulationCorrectionRate;
				PluginCommonInfo.PopulationCheatRate = PopulationCheatRate;
				ManualLogSource log3 = Log;
				if (log3 != null)
				{
					log3.LogInfo((object)$"已修正{num}年{num2}月的人口,修正倍率为{PopulationCheatRate},修正比例为{PopulationCorrectionRate}");
				}
				CorrectPopulation();
				lastCorrectionYearMonth = num4;
			}
		}
		catch (Exception ex)
		{
			ManualLogSource log4 = Log;
			if (log4 != null)
			{
				log4.LogError((object)("处理人口修正时发生错误:" + ex.Message));
			}
		}
	}

	public void CorrectPopulation()
	{
		try
		{
			if (Mainload.CityData_now == null || Mainload.CityData_now.Count == 0)
			{
				ManualLogSource log = Log;
				if (log != null)
				{
					log.LogWarning((object)"CityData_now数据结构不存在或为空,无法执行人口修正,可能是未进入相应的游戏存档");
				}
				return;
			}
			for (int i = 0; i < 12; i++)
			{
				if (i >= Mainload.CityData_now.Count || Mainload.CityData_now[i] == null || Mainload.CityData_now[i].Count < 1 || Mainload.CityData_now[i][0] == null || Mainload.CityData_now[i][0].Count < 8)
				{
					ManualLogSource log2 = Log;
					if (log2 != null)
					{
						log2.LogWarning((object)$"郡索引{i}的数据结构不完整,跳过该郡的人口修正");
					}
					continue;
				}
				string s = Mainload.CityData_now[i][0][7];
				int num = (int)((float)int.Parse(s) + 300f * PopulationCorrectionRate * (float)PopulationCheatRate);
				int num2 = num - int.Parse(s);
				Mainload.CityData_now[i][0][7] = num.ToString();
				s = null;
				ManualLogSource log3 = Log;
				if (log3 != null)
				{
					log3.LogInfo((object)$"已修正{PluginCommonInfo.JunList[i]}的人口,修正值为{num2},修正后人口为{num}");
				}
			}
			for (int j = 0; j < 12; j++)
			{
				if (j >= Mainload.CityData_now.Count || Mainload.CityData_now[j] == null)
				{
					ManualLogSource log4 = Log;
					if (log4 != null)
					{
						log4.LogWarning((object)$"郡索引{j}的数据结构不存在,跳过该县的人口修正");
					}
					continue;
				}
				int num3 = PluginCommonInfo.XianList[j].Length;
				for (int k = 0; k < num3; k++)
				{
					if (k + 1 >= Mainload.CityData_now[j].Count || Mainload.CityData_now[j][k + 1] == null || Mainload.CityData_now[j][k + 1].Count < 4)
					{
						ManualLogSource log5 = Log;
						if (log5 != null)
						{
							log5.LogWarning((object)$"郡索引{j}、县索引{k}的数据结构不完整,跳过该县的人口修正");
						}
						continue;
					}
					string s2 = Mainload.CityData_now[j][k + 1][3];
					int num4 = (int)((float)int.Parse(s2) + 100f * PopulationCorrectionRate * (float)PopulationCheatRate);
					int num5 = num4 - int.Parse(s2);
					Mainload.CityData_now[j][k + 1][3] = num4.ToString();
					s2 = null;
					ManualLogSource log6 = Log;
					if (log6 != null)
					{
						log6.LogInfo((object)$"已修正{PluginCommonInfo.XianList[j][k]}的人口,修正值为{num5},修正后人口为{num4}");
					}
				}
			}
		}
		catch (Exception ex)
		{
			ManualLogSource log7 = Log;
			if (log7 != null)
			{
				log7.LogError((object)("执行人口修正时发生错误:" + ex.Message));
			}
		}
		finally
		{
			PopulationCorrectionRate = 0f;
			PluginCommonInfo.PopulationCorrectionRate = 0;
			PluginCommonInfo.PopulationCheatRate = 0;
		}
	}
}
public static class PluginCommonInfo
{
	public const int PopulationCorrection = 100;

	public static int PopulationCorrectionRate;

	public static int PopulationCheatRate;

	public static string[] JunList = new string[12]
	{
		"南郡", "三川郡", "蜀郡", "丹阳郡", "陈留郡", "长沙郡", "会稽郡", "广陵郡", "太原郡", "益州郡",
		"南海郡", "云南郡"
	};

	public static string[][] XianList = new string[12][]
	{
		new string[8] { "临沮", "襄樊", "宜城", "麦城", "华容", "郢亭", "江陵", "夷陵" },
		new string[6] { "平阳", "荥阳", "原武", "阳武", "新郑", "宜阳" },
		new string[6] { "邛崃", "郫县", "什邡", "绵竹", "新都", "成都" },
		new string[6] { "秣陵", "江乘", "江宁", "溧阳", "建邺", "永世" },
		new string[6] { "长垣", "济阳", "成武", "襄邑", "宁陵", "封丘" },
		new string[8] { "零陵", "益阳", "湘县", "袁州", "庐陵", "衡山", "建宁", "桂阳" },
		new string[4] { "曲阿", "松江", "山阴", "余暨" },
		new string[4] { "平安", "射阳", "海陵", "江都" },
		new string[8] { "大陵", "晋阳", "九原", "石城", "阳曲", "魏榆", "孟县", "中都" },
		new string[8] { "连然", "谷昌", "同劳", "昆泽", "滇池", "俞元", "胜休", "南安" },
		new string[6] { "四会", "阳山", "龙川", "揭岭", "罗阳", "善禺" },
		new string[8] { "云平", "叶榆", "永宁", "遂久", "姑复", "蜻陵", "弄栋", "邪龙" }
	};
}
[HarmonyPatch(typeof(SaveData))]
[HarmonyPatch("ReadGameData")]
public static class SaveDataReadGameDataPatch
{
	private static void Postfix()
	{
		try
		{
			if (Mainload.Time_now == null || Mainload.Time_now.Count < 3)
			{
				ManualLogSource log = Main.Log;
				if (log != null)
				{
					log.LogWarning((object)"Mainload.Time_now数据不完整,无法执行加载存档后的人口修正");
				}
				return;
			}
			int num = Mainload.Time_now[0];
			int num2 = Mainload.Time_now[1];
			float num3 = 4f;
			num3 = num switch
			{
				1 => 0.1f, 
				2 => 0.2f, 
				3 => 0.3f, 
				4 => 0.4f, 
				5 => 0.5f, 
				6 => 0.6f, 
				7 => 0.7f, 
				8 => 0.8f, 
				9 => 0.9f, 
				10 => 1f, 
				11 => 1.2f, 
				12 => 1.4f, 
				13 => 1.6f, 
				14 => 1.8f, 
				15 => 2f, 
				16 => 2.2f, 
				17 => 2.4f, 
				18 => 2.6f, 
				19 => 2.8f, 
				20 => 3f, 
				_ => 4f, 
			};
			if ((Object)(object)Main.Instance != (Object)null)
			{
				Main.PopulationCorrectionRate = num3;
				PluginCommonInfo.PopulationCorrectionRate = (int)num3;
				PluginCommonInfo.PopulationCheatRate = Main.PopulationCheatRate;
				ManualLogSource log2 = Main.Log;
				if (log2 != null)
				{
					log2.LogInfo((object)$"加载存档后修正人口,当前年份:{num}年{num2}月,修正倍率:{Main.PopulationCheatRate},修正比例:{num3}");
				}
				Main.Instance.CorrectPopulation();
			}
		}
		catch (Exception ex)
		{
			if (Main.Log != null)
			{
				ManualLogSource log3 = Main.Log;
				if (log3 != null)
				{
					log3.LogError((object)("读取存档数据后修正人口时发生错误:" + ex.Message));
				}
			}
		}
	}
}