using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using DynamicDeadlineMod.Patches;
using HarmonyLib;
using Unity.Netcode;
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("DynamicDeadlineMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DynamicDeadlineMod")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("78c98d39-8b11-4a2c-bccb-e32338f5bacf")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace DynamicDeadlineMod
{
[BepInPlugin("Haha.DynamicDeadline", "Dynamic Deadline", "1.2.2")]
public class DynamicDeadlineMod : BaseUnityPlugin
{
private const string modGUID = "Haha.DynamicDeadline";
private const string modName = "Dynamic Deadline";
private const string modVersion = "1.2.2";
private readonly Harmony harmony = new Harmony("Haha.DynamicDeadline");
public static DynamicDeadlineMod Instance;
internal static ConfigEntry<float> MinScrapValuePerDay;
internal static ConfigEntry<bool> legacyCal;
internal static ConfigEntry<float> legacyDailyValue;
internal static ConfigEntry<bool> useMinMax;
internal static ConfigEntry<float> setMinimumDays;
internal static ConfigEntry<float> setMaximumDays;
internal ManualLogSource mls;
internal void Awake()
{
if ((Object)(object)Instance == (Object)null)
{
Instance = this;
}
mls = Logger.CreateLogSource("Dynamic Deadline");
mls.LogInfo((object)"No more short deadlines for excessive quotas.");
MinScrapValuePerDay = ((BaseUnityPlugin)this).Config.Bind<float>("Customizable Values", "Minimum Daily ScrapValue", 200f, "Set this value to the minimum scrap value you should achieve per day. This will ignore the calculation for daily scrap if it's below this number.");
useMinMax = ((BaseUnityPlugin)this).Config.Bind<bool>("Customizable Values", "Use Custom Deadline Range", false, "Set to true if you want to use the custom minimum/maximum deadline range.");
setMinimumDays = ((BaseUnityPlugin)this).Config.Bind<float>("Customizable Values", "Minimum Deadline", 3f, "If Use Custom Deadline Range is enabled, this is the minimum deadline you will have.");
setMaximumDays = ((BaseUnityPlugin)this).Config.Bind<float>("Customizable Values", "Maximum Deadline", float.MaxValue, "If use Custom Deadline Range is enabled, this is the maximum deadline you will have.");
legacyCal = ((BaseUnityPlugin)this).Config.Bind<bool>("Customizeable Values - Legacy", "Legacy Calculations", false, "Set to true if you want to use the deadline calculation from 1.1.0 prior.");
legacyDailyValue = ((BaseUnityPlugin)this).Config.Bind<float>("Customizeable Values - Legacy", "Daily Scrap Value", 200f, "Set this number to the value of scrap you can reasonably achieve in a single day.");
harmony.PatchAll(typeof(DynamicDeadlineMod));
harmony.PatchAll(typeof(ProfitQuotaPatch));
}
}
}
namespace DynamicDeadlineMod.Patches
{
[HarmonyPatch(typeof(TimeOfDay), "SyncTimeClientRpc")]
public class FixTheDeadline
{
[HarmonyPrefix]
public static void DeadlineBroke()
{
string currentSaveFileName = GameNetworkManager.Instance.currentSaveFileName;
float num;
if (ES3.KeyExists("totalOfAverage"))
{
num = ES3.Load<float>("totalOfAverage", currentSaveFileName, 0f);
}
else
{
ES3.Save<float>("totalOfAverage", 0f, currentSaveFileName);
num = ES3.Load<float>("totalOfAverage", currentSaveFileName, 0f);
}
if (num < DynamicDeadlineMod.MinScrapValuePerDay.Value * (float)TimeOfDay.Instance.timesFulfilledQuota)
{
num = DynamicDeadlineMod.MinScrapValuePerDay.Value * (float)TimeOfDay.Instance.timesFulfilledQuota;
float num2 = num / (float)TimeOfDay.Instance.timesFulfilledQuota;
float num3 = Mathf.Clamp(Mathf.Ceil((float)TimeOfDay.Instance.profitQuota / num2), 3f, float.MaxValue);
ES3.Save<float>("totalOfAverage", num, currentSaveFileName);
TimeOfDay.Instance.timeUntilDeadline = 700f * num3;
ES3.Save<float>("previousDeadline", num3, currentSaveFileName);
}
}
}
[HarmonyPatch(typeof(GameNetworkManager), "ResetSavedGameValues")]
public class ResetSavedValuesPatch
{
[HarmonyPrefix]
public static void ResetSavedValues()
{
string currentSaveFileName = GameNetworkManager.Instance.currentSaveFileName;
ES3.Save<float>("previousDeadline", 3f, currentSaveFileName);
ES3.Save<float>("totalOfAverage", 0f, currentSaveFileName);
}
}
[HarmonyPatch(typeof(TimeOfDay), "SetNewProfitQuota")]
public class ProfitQuotaPatch
{
private static float quotaFulfilled;
[HarmonyPrefix]
public static void GetQuotaFulfilled()
{
quotaFulfilled = TimeOfDay.Instance.quotaFulfilled;
}
[HarmonyPostfix]
private static void DynamicDeadline(TimeOfDay __instance)
{
string currentSaveFileName = GameNetworkManager.Instance.currentSaveFileName;
bool isHost = ((NetworkBehaviour)RoundManager.Instance).NetworkManager.IsHost;
float num = TimeOfDay.Instance.timesFulfilledQuota;
float num2 = ((!DynamicDeadlineMod.useMinMax.Value) ? 3f : DynamicDeadlineMod.setMinimumDays.Value);
float num3 = ((!DynamicDeadlineMod.useMinMax.Value) ? float.MaxValue : DynamicDeadlineMod.setMaximumDays.Value);
float num4;
if (ES3.KeyExists("previousDeadline"))
{
num4 = ES3.Load<float>("previousDeadline", currentSaveFileName, 3f);
DynamicDeadlineMod.Instance.mls.LogInfo((object)$"Successfully loaded the previous totalOfAverage variable! totalofAverage is: {num4}");
}
else
{
ES3.Save<float>("previousDeadline", 3f, currentSaveFileName);
num4 = ES3.Load<float>("previousDeadline", currentSaveFileName, 3f);
DynamicDeadlineMod.Instance.mls.LogInfo((object)"Could not load previousDeadline variable as it does not exist! Creating now!");
}
float num5;
if (ES3.KeyExists("totalOfAverage"))
{
num5 = ES3.Load<float>("totalOfAverage", currentSaveFileName, 0f);
DynamicDeadlineMod.Instance.mls.LogInfo((object)$"Successfully loaded the previous totalOfAverage variable! totalofAverage is: {num5}");
}
else
{
ES3.Save<float>("totalOfAverage", 0f, currentSaveFileName);
num5 = ES3.Load<float>("totalOfAverage", currentSaveFileName, 0f);
DynamicDeadlineMod.Instance.mls.LogInfo((object)"Could not load totalOfAverage variable as it does not exist! Creating now!");
}
if (isHost && !DynamicDeadlineMod.legacyCal.Value)
{
float num6 = Mathf.Clamp(Mathf.Ceil(quotaFulfilled / num4), DynamicDeadlineMod.MinScrapValuePerDay.Value, 1000f);
if (num5 == 0f && num != 0f)
{
num5 = DynamicDeadlineMod.MinScrapValuePerDay.Value * num - 1f;
}
float num7 = num5 / num;
float num8 = Mathf.Clamp(Mathf.Ceil((float)__instance.profitQuota / num7), num2, num3);
num5 += num6;
__instance.timeUntilDeadline = __instance.totalTime * num8;
DynamicDeadlineMod.Instance.mls.LogInfo((object)$"This person is the host, changing deadline. DailyValue registered as {num6}, new average is {num7}, and host is currently on their {num} run!");
TimeOfDay.Instance.SyncTimeClientRpc(__instance.globalTime, (int)__instance.timeUntilDeadline);
DynamicDeadlineMod.Instance.mls.LogInfo((object)$"The new deadline is {num8} days.");
num4 = num8;
DynamicDeadlineMod.Instance.mls.LogInfo((object)$"Did the value get assigned properly? Previous deadline is {num4}");
ES3.Save<float>("previousDeadline", num4, currentSaveFileName);
ES3.Save<float>("totalOfAverage", num5, currentSaveFileName);
}
else if (isHost && DynamicDeadlineMod.legacyCal.Value)
{
__instance.timeUntilDeadline = __instance.totalTime * Mathf.Clamp(Mathf.Ceil((float)__instance.profitQuota / DynamicDeadlineMod.legacyDailyValue.Value), num2, num3);
DynamicDeadlineMod.Instance.mls.LogInfo((object)"This person is the host and using the legacy difficulty calculations. Changing deadline.");
TimeOfDay.Instance.SyncTimeClientRpc(__instance.globalTime, (int)__instance.timeUntilDeadline);
}
else
{
DynamicDeadlineMod.Instance.mls.LogInfo((object)"This person is not the host. Will not change deadline or send rpc.");
}
}
}
}