Decompiled source of SchoolCapacityBalancer v0.1.2

SchoolCapacityBalancer/SchoolCapacityBalancer.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using Game.Prefabs;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Wayzware")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.2.0")]
[assembly: AssemblyInformationalVersion("0.1.2+877b24a14fd57276418073fade3781e732da00b4")]
[assembly: AssemblyProduct("SchoolCapacityBalancer")]
[assembly: AssemblyTitle("SchoolCapacityBalancer")]
[assembly: AssemblyVersion("0.1.2.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace Wayz.CS2
{
	public static class WayzSettingsManager
	{
		public static T? GetSettings<T>(string modIdentifier, string settingName)
		{
			string text = Path.Combine(Application.persistentDataPath, "ModSettings", modIdentifier, settingName + ".json");
			if (!File.Exists(text))
			{
				throw new FileNotFoundException("Settings file not found at " + text);
			}
			return JsonConvert.DeserializeObject<T>(File.ReadAllText(text, Encoding.UTF8));
		}

		public static void SaveSettings<T>(string modIdentifier, string settingName, T settings)
		{
			if (!Directory.Exists(Path.Combine(Application.persistentDataPath, "ModSettings", modIdentifier)))
			{
				Directory.CreateDirectory(Path.Combine(Application.persistentDataPath, "ModSettings", modIdentifier));
			}
			string path = Path.Combine(Application.persistentDataPath, "ModSettings", modIdentifier, settingName + ".json");
			string contents = JsonConvert.SerializeObject((object)settings, (Formatting)1);
			File.WriteAllText(path, contents, Encoding.UTF8);
		}

		public static bool TryGetSettings<T>(string modIdentifier, string settingName, out T settings)
		{
			try
			{
				settings = GetSettings<T>(modIdentifier, settingName);
				return settings != null;
			}
			catch (FileNotFoundException)
			{
				settings = default(T);
				return false;
			}
		}

		public static T GetOrInitializeSettings<T>(string modIdentifier, string settingName) where T : new()
		{
			if (TryGetSettings<T>(modIdentifier, settingName, out var settings))
			{
				return settings;
			}
			T val = new T();
			SaveSettings(modIdentifier, settingName, val);
			return val;
		}
	}
}
namespace Wayz.CS2.SchoolCapacityBalancer
{
	public class SchoolCapacityBalancerOptions
	{
		public static SchoolCapacityBalancerOptions Default => new SchoolCapacityBalancerOptions
		{
			Version = 1,
			Options = new List<SchoolOptions>
			{
				new SchoolOptions
				{
					Name = "ElementarySchool01",
					UpkeepCost = 50000,
					StudentCapacity = 2000
				},
				new SchoolOptions
				{
					Name = "ElementarySchool01 Extension Wing",
					UpkeepCost = 20000,
					StudentCapacity = 1000
				},
				new SchoolOptions
				{
					Name = "HighSchool01",
					UpkeepCost = -1,
					StudentCapacity = -1
				},
				new SchoolOptions
				{
					Name = "HighSchool01 Extension Wing",
					UpkeepCost = -1,
					StudentCapacity = -1
				},
				new SchoolOptions
				{
					Name = "College01",
					UpkeepCost = -1,
					StudentCapacity = -1
				},
				new SchoolOptions
				{
					Name = "College01 Extension Wing",
					UpkeepCost = 110000,
					StudentCapacity = 1000
				},
				new SchoolOptions
				{
					Name = "University01",
					UpkeepCost = 375000,
					StudentCapacity = 2500
				},
				new SchoolOptions
				{
					Name = "University01 Extension Wing",
					UpkeepCost = 125000,
					StudentCapacity = 1000
				}
			}
		};

		public int Version { get; set; }

		public IEnumerable<SchoolOptions> Options { get; set; } = new List<SchoolOptions>();


		public IReadOnlyDictionary<string, SchoolOptions> ToDictionary()
		{
			Dictionary<string, SchoolOptions> dictionary = new Dictionary<string, SchoolOptions>();
			foreach (SchoolOptions option in Options)
			{
				dictionary.Add(option.Name, option);
			}
			return dictionary;
		}

		public int RemoveBadEntires()
		{
			int num = Options.Count();
			Options = Options.Where((SchoolOptions x) => !string.IsNullOrEmpty(x.Name) || (x.UpkeepCost < 0 && x.StudentCapacity < 0));
			return num - Options.Count();
		}

		public int UpdateToLatestVersion()
		{
			int version = Version;
			if (Version == 0)
			{
				string[] array = new string[7] { "HighSchool01", "HighSchool01 Extension Wing", "College01", "TechnicalUniversity01", "TechnicalUniversity01 Extension Wing", "MedicalUniversity01", "MedicalUniversity01 Extension Wing" };
				foreach (string school in array)
				{
					if (!Options.Any((SchoolOptions o) => o.Name == school))
					{
						Options = Options.Append(new SchoolOptions
						{
							Name = school,
							UpkeepCost = -1,
							StudentCapacity = -1
						});
					}
				}
				Options = Options.OrderBy((SchoolOptions x) => x.Name);
				Version = 1;
			}
			return Version - version;
		}
	}
	public class SchoolOptions
	{
		public string Name { get; set; }

		public int UpkeepCost { get; set; }

		public int StudentCapacity { get; set; }
	}
	[BepInPlugin("Wayz.CS2.SchoolCapacityBalancer", "SchoolCapacityBalancer", "0.1.2")]
	public class SchoolCapacityBalancer : BaseUnityPlugin
	{
		public static ManualLogSource GameLogger;

		public static IReadOnlyDictionary<string, SchoolOptions> SchoolOptions { get; private set; }

		private void Awake()
		{
			GameLogger = ((BaseUnityPlugin)this).Logger;
			if (!WayzSettingsManager.TryGetSettings<SchoolCapacityBalancerOptions>("SchoolCapacityBalancer_Wayz", "settings", out var settings))
			{
				settings = SchoolCapacityBalancerOptions.Default;
				try
				{
					WayzSettingsManager.SaveSettings("SchoolCapacityBalancer_Wayz", "settings", settings);
				}
				catch
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)"Failed to save default config to settings file, using in-memory default config.");
				}
			}
			if (settings.UpdateToLatestVersion() > 0)
			{
				try
				{
					WayzSettingsManager.SaveSettings("SchoolCapacityBalancer_Wayz", "settings", settings);
				}
				catch
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)"Failed to save updated config to settings file, using in-memory updated config.");
				}
			}
			int num = settings.RemoveBadEntires();
			if (num != 0)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)$"Removed {num} bad entries loaded from settings file!");
			}
			SchoolOptions = settings.ToDictionary();
			IEnumerable<MethodBase> patchedMethods = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "SchoolCapacityBalancer_Cities2Harmony").GetPatchedMethods();
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Plugin SchoolCapacityBalancer 0.1.2 is loaded! Patched methods: " + patchedMethods.Count()));
			foreach (MethodBase item in patchedMethods)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Patched method: " + item.Module.Name + ":" + item.Name));
			}
		}
	}
	[HarmonyPatch(typeof(PrefabSystem), "AddPrefab")]
	public static class PrefabPatcher
	{
		[HarmonyPrefix]
		public static bool Prefix(object __instance, PrefabBase prefab)
		{
			if (SchoolCapacityBalancer.SchoolOptions.TryGetValue(((Object)prefab).name, out SchoolOptions value))
			{
				if (value.UpkeepCost >= 0)
				{
					((ComponentBase)prefab).GetComponent<ServiceConsumption>().m_Upkeep = value.UpkeepCost;
				}
				if (value.StudentCapacity >= 0)
				{
					((ComponentBase)prefab).GetComponent<School>().m_StudentCapacity = value.StudentCapacity;
				}
			}
			return true;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "SchoolCapacityBalancer";

		public const string PLUGIN_NAME = "SchoolCapacityBalancer";

		public const string PLUGIN_VERSION = "0.1.2";
	}
}