Decompiled source of ConfigurableMurderCooldown v1.0.1

ConfigurableMurderCooldown.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using SOD.Common;
using SOD.Common.BepInEx;
using SOD.Common.Helpers;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("ConfigurableMurderCooldown")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+bb944b530a0598a3bc6c00858aedda2e4fdff308")]
[assembly: AssemblyProduct("ConfigurableMurderCooldown")]
[assembly: AssemblyTitle("ConfigurableMurderCooldown")]
[assembly: AssemblyVersion("1.0.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 ConfigurableMurderCooldown
{
	public static class CooldownController
	{
		private static Random r = new Random();

		public static void SetCooldown(bool newMurderer)
		{
			int minValue = GetMinValue(newMurderer);
			int maxValue = GetMaxValue(newMurderer);
			int num = r.Next(minValue, maxValue + 1);
			MurderController.Instance.pauseBetweenMurders = num;
			MurderController.Instance.murderPreset.minimumTimeBetweenMurders = num;
			Plugin.ModLog("Set cooldown to " + (int)MurderController.Instance.murderPreset.minimumTimeBetweenMurders);
		}

		private static int GetMinValue(bool newMurderer)
		{
			if (!newMurderer)
			{
				return Settings.ExistingMurdererMinCooldown.Value;
			}
			return Settings.NewMurdererMinCooldown.Value;
		}

		private static int GetMaxValue(bool newMurderer)
		{
			if (!newMurderer)
			{
				return Settings.ExistingMurdererMaxCooldown.Value;
			}
			return Settings.NewMurdererMaxCooldown.Value;
		}
	}
	[HarmonyPatch(typeof(MurderController), "PickNewMurderer")]
	public static class PatchPickNewMurderer
	{
		public static bool IsNewGame { get; set; }

		[HarmonyPostfix]
		private static void Postfix(MurderController __instance)
		{
			if (IsNewGame && !Settings.ApplyToNewGames.Value)
			{
				IsNewGame = false;
				return;
			}
			Plugin.ModLog("New Murderer. Resetting cooldown");
			CooldownController.SetCooldown(newMurderer: true);
			IsNewGame = false;
		}
	}
	[HarmonyPatch(typeof(MurderController), "PickNewVictim")]
	public class PatchPickNewVictim
	{
		[HarmonyPostfix]
		internal static void Postfix()
		{
			CooldownController.SetCooldown(newMurderer: false);
			MurderController.Instance.pauseBetweenMurders = 0f;
			Plugin.ModLog("New Victim. Resetting cooldown");
		}
	}
	[BepInPlugin("Severedsolo.SOD.ConfigurableMurderCooldown", "ConfigurableMurderCooldown", "1.0.0")]
	public class Plugin : PluginController<Plugin>
	{
		public const string PLUGIN_GUID = "Severedsolo.SOD.ConfigurableMurderCooldown";

		public const string PLUGIN_NAME = "ConfigurableMurderCooldown";

		public const string PLUGIN_VERSION = "1.0.0";

		public override void Load()
		{
			((PluginController<Plugin, IEmptyBindings>)(object)this).Harmony.PatchAll(Assembly.GetExecutingAssembly());
			BindConfigs();
			Lib.SaveGame.OnAfterNewGame += OnNewGame;
			Lib.SaveGame.OnAfterLoad += OnLoadGame;
			Lib.SaveGame.OnAfterSave += OnSaveGame;
			Lib.SaveGame.OnAfterDelete += OnDeleteGame;
			ModLog("Initialised and patched");
		}

		private static string GetSavePath(string savePath)
		{
			string uniqueString = Lib.SaveGame.GetUniqueString(savePath);
			return Lib.SaveGame.GetSavestoreDirectoryPath(Assembly.GetExecutingAssembly(), "CMC_" + uniqueString + ".txt");
		}

		private void OnDeleteGame(object? sender, SaveGameArgs e)
		{
			string savePath = GetSavePath(e.FilePath);
			if (File.Exists(savePath))
			{
				File.Delete(savePath);
				ModLog("Save deleted");
			}
		}

		private void OnSaveGame(object? sender, SaveGameArgs e)
		{
			if ((Object)(object)MurderController.Instance == (Object)null)
			{
				return;
			}
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.AppendLine(MurderController.Instance.pauseBetweenMurders.ToString());
			stringBuilder.AppendLine(MurderController.Instance.murderPreset.minimumTimeBetweenMurders.ToString());
			using StreamWriter streamWriter = new StreamWriter(GetSavePath(e.FilePath));
			streamWriter.Write(stringBuilder.ToString());
			ModLog("Data saved");
		}

		private void OnLoadGame(object? sender, SaveGameArgs e)
		{
			PatchPickNewMurderer.IsNewGame = false;
			string savePath = GetSavePath(e.FilePath);
			if (!File.Exists(savePath))
			{
				return;
			}
			List<string> list = new List<string>();
			using StreamReader streamReader = new StreamReader(savePath);
			while (true)
			{
				string text = streamReader.ReadLine();
				if (text == null)
				{
					break;
				}
				list.Add(text);
			}
			if (list.Count != 0)
			{
				float.TryParse(list[0], out var result);
				MurderController.Instance.pauseBetweenMurders = result;
				float.TryParse(list[1], out result);
				MurderController.Instance.murderPreset.minimumTimeBetweenMurders = result;
				ModLog("Data Restored");
			}
		}

		public static void ModLog(string output)
		{
			PluginController<Plugin, IEmptyBindings>.Log.LogInfo((object)output);
		}

		private void OnNewGame(object? sender, EventArgs e)
		{
			PatchPickNewMurderer.IsNewGame = true;
			MurderController.Instance.pauseBetweenMurders = 0f;
			ModLog("New Game: - Resetting to Default");
		}

		private void BindConfigs()
		{
			Settings.ApplyToNewGames = base.Config.Bind<bool>("General", "Severedsolo.CMC.applyToNewGames", false, "Should the first murder of a new game also have a cooldown?");
			Settings.NewMurdererMinCooldown = base.Config.Bind<int>("Murderer First Kill", "Severedsolo.CMC.newKillerMinCooldown", 12, "Minimum time (in hours) before murderer makes their first kill");
			Settings.NewMurdererMaxCooldown = base.Config.Bind<int>("Murderer First Kill", "Severedsolo.CMC.newKillerMaxCooldown", 48, "Maximum time (in hours) before murderer makes their first kill");
			Settings.ExistingMurdererMinCooldown = base.Config.Bind<int>("Murderer Second+ Kill", "Severedsolo.CMC.existingKillerMinCooldown", 1, "Minimum time (in hours) before murderer makes their second (or more) kill");
			Settings.ExistingMurdererMaxCooldown = base.Config.Bind<int>("Murderer Second+ Kill", "Severedsolo.CMC.existingKillerMaxCooldown", 12, "Minimum time (in hours) before murderer makes their second (or more) kill");
			ModLog("Configs Bound");
		}
	}
	public static class Settings
	{
		public static ConfigEntry<bool> ApplyToNewGames { get; set; }

		public static ConfigEntry<int> NewMurdererMinCooldown { get; set; }

		public static ConfigEntry<int> NewMurdererMaxCooldown { get; set; }

		public static ConfigEntry<int> ExistingMurdererMinCooldown { get; set; }

		public static ConfigEntry<int> ExistingMurdererMaxCooldown { get; set; }
	}
}