Decompiled source of PlayerMoneyController v0.2.0

PlayerMoneyController/PlayerMoneyController.dll

Decompiled 7 months ago
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using Colossal.Json;
using Colossal.Logging;
using Colossal.PSI.Environment;
using Colossal.Serialization.Entities;
using Game;
using Game.City;
using Game.Common;
using Game.Simulation;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Entities;
using UnityEngine.InputSystem;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Mbyron26")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyCopyright("Copyright © 2023 Mbyron26")]
[assembly: AssemblyFileVersion("0.2.0.0")]
[assembly: AssemblyInformationalVersion("0.2")]
[assembly: AssemblyProduct("Player Money Controller")]
[assembly: AssemblyTitle("PlayerMoneyController")]
[assembly: AssemblyVersion("0.2.0.0")]
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;
		}
	}
}
namespace PlayerMoneyController
{
	[BepInPlugin("PlayerMoneyController", "Player Money Controller", "0.2.0")]
	public class Mod : BaseUnityPlugin
	{
		public const string RAWMODNAME = "PlayerMoneyController";

		public const string MODNAME = "Player Money Controller";

		public const string MODVERSION = "0.2.0";

		public static readonly ILog Log = LogManager.GetLogger("PlayerMoneyController", true);

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin Player Money Controller is loaded!");
			Harmony val = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "PlayerMoneyController_Cities2Harmony");
			MethodBase[] array = val.GetPatchedMethods().ToArray();
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Plugin Player Money Controller made patches! Patched methods: " + array.Length));
			MethodBase[] array2 = array;
			foreach (MethodBase methodBase in array2)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Patched method: " + methodBase.Module.Name + ":" + methodBase.Name));
			}
		}
	}
	public class ModSettings
	{
		private static readonly string ModSettingsFileName = "PlayerMoneyControllerSettings";

		private static readonly string JSONExtension = ".json";

		private static readonly string ModSettingsPath = EnvPath.kUserDataPath + "/" + ModSettingsFileName + JSONExtension;

		public static ModSettings Instance
		{
			get
			{
				if (!TryGetSettings(out ModSettings settings))
				{
					SaveSettings(settings);
				}
				return settings;
			}
		}

		public int ManuallyMoneyAmount { get; set; } = 1000000;


		public bool InitialMoneyEnabled { get; set; } = false;


		public int InitialMoneyAmount { get; set; } = 500000;


		public static void SaveSettings(ModSettings settings)
		{
			try
			{
				string contents = JSON.Dump((object)settings, (EncodeOptions)0);
				File.WriteAllText(ModSettingsPath, contents);
				Mod.Log.Info((object)"Settings saved successfully");
			}
			catch (Exception ex)
			{
				Mod.Log.InfoFormat("Saving settings failed: {0}", (object)ex);
			}
		}

		private static bool TryGetSettings(out ModSettings settings)
		{
			if (File.Exists(ModSettingsPath))
			{
				try
				{
					Variant val = JSON.Load(File.ReadAllText(ModSettingsPath));
					settings = val.Make<ModSettings>();
					Mod.Log.Info((object)"Loaded settings successfully");
					SaveSettings(settings);
					return true;
				}
				catch (Exception ex)
				{
					Mod.Log.InfoFormat("Loading settings failed: {0}", (object)ex);
				}
			}
			Mod.Log.Info((object)"Settings not present");
			settings = new ModSettings();
			return false;
		}
	}
	[HarmonyPatch(typeof(SystemOrder), "Initialize")]
	public class Patch1
	{
		public static void Postfix(UpdateSystem updateSystem)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			((ComponentSystemBase)updateSystem).World.GetOrCreateSystem<PlayerMoneyControllerSystem>();
			updateSystem.UpdateAt<CitySystem>((SystemUpdatePhase)21);
		}
	}
	[HarmonyPatch(typeof(CitySystem), "PostDeserialize")]
	public class Patch2
	{
		public static void Postfix(CitySystem __instance, Context context)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Invalid comparison between Unknown and I4
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			if (ModSettings.Instance.InitialMoneyEnabled && (int)((Context)(ref context)).purpose == 1)
			{
				EntityManager entityManager = ((ComponentSystemBase)__instance).EntityManager;
				((EntityManager)(ref entityManager)).SetComponentData<PlayerMoney>(__instance.City, new PlayerMoney(ModSettings.Instance.InitialMoneyAmount));
			}
		}
	}
	public class PlayerMoneyControllerSystem : GameSystemBase
	{
		private CitySystem? citySystem;

		protected override void OnCreate()
		{
			((GameSystemBase)this).OnCreate();
			citySystem = ((ComponentSystemBase)this).World.GetExistingSystemManaged<CitySystem>();
			if (citySystem == null)
			{
				Mod.Log.Error((object)"[CitySystem] is null");
				return;
			}
			CreateKeyBinding();
			Mod.Log.Info((object)"PlayerMoneyControllerSystem: OnCreate");
		}

		private void CreateKeyBinding()
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Expected O, but got Unknown
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			InputAction val = new InputAction("AddMoney", (InputActionType)0, (string)null, (string)null, (string)null, (string)null);
			CompositeSyntax val2 = InputActionSetupExtensions.AddCompositeBinding(val, "ButtonWithTwoModifiers", (string)null, (string)null);
			val2 = ((CompositeSyntax)(ref val2)).With("Modifier1", "<Keyboard>/ctrl", (string)null, (string)null);
			val2 = ((CompositeSyntax)(ref val2)).With("Modifier2", "<Keyboard>/shift", (string)null, (string)null);
			((CompositeSyntax)(ref val2)).With("Button", "<Keyboard>/equals", (string)null, (string)null);
			val.performed += OnToggleAddMoney;
			val.Enable();
			val = new InputAction("SubtractMoney", (InputActionType)0, (string)null, (string)null, (string)null, (string)null);
			val2 = InputActionSetupExtensions.AddCompositeBinding(val, "ButtonWithTwoModifiers", (string)null, (string)null);
			val2 = ((CompositeSyntax)(ref val2)).With("Modifier1", "<Keyboard>/ctrl", (string)null, (string)null);
			val2 = ((CompositeSyntax)(ref val2)).With("Modifier2", "<Keyboard>/shift", (string)null, (string)null);
			((CompositeSyntax)(ref val2)).With("Button", "<Keyboard>/minus", (string)null, (string)null);
			val.performed += OnToggleSubtractMoney;
			val.Enable();
		}

		private void OnToggleAddMoney(CallbackContext obj)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			if (citySystem != null)
			{
				EntityManager entityManager = ((ComponentSystemBase)this).EntityManager;
				PlayerMoney componentData = ((EntityManager)(ref entityManager)).GetComponentData<PlayerMoney>(citySystem.City);
				((PlayerMoney)(ref componentData)).Add(ModSettings.Instance.ManuallyMoneyAmount);
				entityManager = ((ComponentSystemBase)this).EntityManager;
				((EntityManager)(ref entityManager)).SetComponentData<PlayerMoney>(citySystem.City, componentData);
			}
		}

		private void OnToggleSubtractMoney(CallbackContext obj)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			if (citySystem != null)
			{
				EntityManager entityManager = ((ComponentSystemBase)this).EntityManager;
				PlayerMoney componentData = ((EntityManager)(ref entityManager)).GetComponentData<PlayerMoney>(citySystem.City);
				((PlayerMoney)(ref componentData)).Subtract(ModSettings.Instance.ManuallyMoneyAmount);
				entityManager = ((ComponentSystemBase)this).EntityManager;
				((EntityManager)(ref entityManager)).SetComponentData<PlayerMoney>(citySystem.City, componentData);
			}
		}

		protected override void OnUpdate()
		{
			Mod.Log.Info((object)"PlayerMoneyControllerSystem: OnUpdate");
		}
	}
}