using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Mirror;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInPlugin("com.buhe.gwyfcrashcontrolbuhe", "GWYF-CrashControl-BUHE", "0.1.1")]
public sealed class GwyfCrashControlBuhePlugin : BaseUnityPlugin
{
internal static GwyfCrashControlBuhePlugin Instance;
internal static ManualLogSource Log;
internal static CrashControlSettings Settings = new CrashControlSettings();
private Harmony _harmony;
internal static string PluginFolder
{
get
{
string location = Assembly.GetExecutingAssembly().Location;
return Path.GetDirectoryName(location) ?? Paths.PluginPath;
}
}
private void Awake()
{
//IL_0026: Unknown result type (might be due to invalid IL or missing references)
//IL_0030: Expected O, but got Unknown
Instance = this;
Log = ((BaseUnityPlugin)this).Logger;
Settings = CrashControlSettings.Load(GetSettingsPath());
_harmony = new Harmony("com.buhe.gwyfcrashcontrolbuhe");
_harmony.PatchAll(typeof(GwyfCrashControlBuhePlugin).Assembly);
((BaseUnityPlugin)this).Logger.LogInfo((object)("GWYF-CrashControl-BUHE loaded. Enabled=" + Settings.Enabled + ", CrashPoint=" + Settings.CrashPoint.ToString("0.##")));
}
private void OnDestroy()
{
if (_harmony != null)
{
_harmony.UnpatchSelf();
_harmony = null;
}
}
internal static string GetSettingsPath()
{
return Path.Combine(PluginFolder, "GWYF-CrashControl-BUHE.settings.txt");
}
internal void LogInfo(string message)
{
((BaseUnityPlugin)this).Logger.LogInfo((object)message);
}
}
internal sealed class CrashControlSettings
{
private const string DefaultSettingsText = "# GWYF-CrashControl-BUHE\n# Put this file next to GWYF-CrashControl-BUHE.dll.\n# Enabled:\n# true = enable Crash point control\n# false = use the original random Crash point\n# CrashPoint:\n# Fixed Crash multiplier. 100 means the machine crashes at 100x.\nEnabled=true\nCrashPoint=100\n";
internal bool Enabled = true;
internal float CrashPoint = 100f;
internal static CrashControlSettings Load(string path)
{
CrashControlSettings crashControlSettings = new CrashControlSettings();
if (!File.Exists(path))
{
File.WriteAllText(path, "# GWYF-CrashControl-BUHE\n# Put this file next to GWYF-CrashControl-BUHE.dll.\n# Enabled:\n# true = enable Crash point control\n# false = use the original random Crash point\n# CrashPoint:\n# Fixed Crash multiplier. 100 means the machine crashes at 100x.\nEnabled=true\nCrashPoint=100\n", Encoding.UTF8);
return crashControlSettings;
}
string[] array = File.ReadAllLines(path, Encoding.UTF8);
foreach (string text in array)
{
string text2 = text.Trim();
if (text2.Length == 0 || text2.StartsWith("#") || text2.StartsWith("//"))
{
continue;
}
int num = text2.IndexOf('=');
if (num > 0)
{
string text3 = text2.Substring(0, num).Trim();
string text4 = text2.Substring(num + 1).Trim();
float result;
if (text3.Equals("Enabled", StringComparison.OrdinalIgnoreCase))
{
crashControlSettings.Enabled = !text4.Equals("false", StringComparison.OrdinalIgnoreCase);
}
else if (text3.Equals("CrashPoint", StringComparison.OrdinalIgnoreCase) && float.TryParse(text4, NumberStyles.Float, CultureInfo.InvariantCulture, out result))
{
crashControlSettings.CrashPoint = Mathf.Clamp(result, 1.01f, 1000000f);
}
}
}
return crashControlSettings;
}
}
[HarmonyPatch(typeof(Crash), "SetCrashPoint")]
internal static class CrashSetCrashPointPatch
{
private static readonly MethodInfo RaiseRoutineMethod = AccessTools.Method(typeof(Crash), "RaiseRoutine", (Type[])null, (Type[])null);
private static bool Prefix(Crash __instance)
{
CrashControlSettings settings = GwyfCrashControlBuhePlugin.Settings;
if (settings == null || !settings.Enabled)
{
return true;
}
if (!NetworkServer.active)
{
return true;
}
if (RaiseRoutineMethod == null)
{
Log("RaiseRoutine method not found; using original crash logic.");
return true;
}
float num = Mathf.Clamp(settings.CrashPoint, 1.01f, 1000000f);
IEnumerator enumerator = (IEnumerator)RaiseRoutineMethod.Invoke(__instance, new object[1] { num });
((MonoBehaviour)__instance).StartCoroutine(enumerator);
__instance.Network_hasStarted = true;
Log("Forced Crash point to " + num.ToString("0.##") + "x.");
return false;
}
private static void Log(string message)
{
if ((Object)(object)GwyfCrashControlBuhePlugin.Instance != (Object)null)
{
GwyfCrashControlBuhePlugin.Instance.LogInfo(message);
}
else
{
Debug.Log((object)("[GWYF-CrashControl-BUHE] " + message));
}
}
}