Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of VargheimGate v0.3.3
VargheimGate.dll
Decompiled a month agousing System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; using System.Text; using BepInEx; using BepInEx.Configuration; using HarmonyLib; using Microsoft.CodeAnalysis; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: AssemblyVersion("0.0.0.0")] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [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 VargheimGate { [BepInPlugin("digi.vargheim.gate", "VargheimGate", "0.3.3")] public class Plugin : BaseUnityPlugin { [Serializable] [CompilerGenerated] private sealed class <>c { public static readonly <>c <>9 = new <>c(); public static ConsoleEvent <>9__21_1; public static PopupButtonCallback <>9__39_0; public static PopupButtonCallback <>9__39_1; internal void <Awake>b__21_1(ConsoleEventArgs args) { ClientSeasonCommand(args); } internal void <Update>b__39_0() { UnifiedPopup.Pop(); Accept(); } internal void <Update>b__39_1() { UnifiedPopup.Pop(); Decline("chose No"); } } public const string PluginGuid = "digi.vargheim.gate"; public const string PluginName = "VargheimGate"; public const string PluginVersion = "0.3.3"; private const string LogFile = "vargheim-gate-log.txt"; internal static Plugin Instance; private ConfigEntry<bool> _enabled; private ConfigEntry<string> _seasonId; private ConfigEntry<int> _skillCap; private ConfigEntry<int> _nemesisScore; private ConfigEntry<bool> _requireMod; private ConfigEntry<string> _exempt; private static readonly ConditionalWeakTable<ZRpc, object> s_hello = new ConditionalWeakTable<ZRpc, object>(); private static bool s_active; private static string s_season = string.Empty; private static int s_cap = 25; private static float s_nemesis = 5000f; private static ZRpc s_serverRpc; private static bool s_checked; private static bool s_pending; private static float s_pendingSince; private const float AnswerSeconds = 180f; private static readonly string[] s_clearFields = new string[6] { "m_knownRecipes", "m_knownStations", "m_knownMaterial", "m_trophies", "m_uniques", "m_knownBiome" }; private const string NemesisScoreKey = "SLS_NEM_SCORE"; private const string NemesisDataKey = "SLS_NEM_SCOREDATA"; private void Awake() { //IL_0071: Unknown result type (might be due to invalid IL or missing references) //IL_007b: Expected O, but got Unknown //IL_00cc: Unknown result type (might be due to invalid IL or missing references) //IL_00d6: Expected O, but got Unknown //IL_0105: Unknown result type (might be due to invalid IL or missing references) //IL_0126: Expected O, but got Unknown //IL_015a: Unknown result type (might be due to invalid IL or missing references) //IL_0145: Unknown result type (might be due to invalid IL or missing references) //IL_014a: Unknown result type (might be due to invalid IL or missing references) //IL_0150: Expected O, but got Unknown Instance = this; try { _enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Server", "Enabled", true, "Server only. Master switch for the season gate."); _seasonId = ((BaseUnityPlugin)this).Config.Bind<string>("Server", "SeasonId", "S2", "Server only. Short label for this season. A character is reset once per label; changing the label makes every character go through the gate again."); _skillCap = ((BaseUnityPlugin)this).Config.Bind<int>("Server", "SkillCap", 25, new ConfigDescription("Server only. Skills above this level are lowered to it when a character goes through the gate.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), Array.Empty<object>())); _requireMod = ((BaseUnityPlugin)this).Config.Bind<bool>("Server", "RequireMod", true, "Server only. Turn away players who join without this mod (they see the game's 'incompatible version' message). When false, players without the mod can join and are NOT gated."); _nemesisScore = ((BaseUnityPlugin)this).Config.Bind<int>("Server", "NemesisScore", 5000, new ConfigDescription("Server only. The Star Level System Nemesis score every character is set back to when it goes through the gate (its fight history from earlier seasons is forgotten too). Use the NeutralScore from NemesisSettings.yaml.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100000), Array.Empty<object>())); _exempt = ((BaseUnityPlugin)this).Config.Bind<string>("Server", "ExemptIds", "", "Server only. Steam ids, separated by commas, that are never gated (for an admin test character)."); SafePatch.Apply(new Harmony("digi.vargheim.gate"), typeof(Patches), delegate(string msg) { ((BaseUnityPlugin)this).Logger.LogWarning((object)msg); }); object obj = <>c.<>9__21_1; if (obj == null) { ConsoleEvent val = delegate(ConsoleEventArgs args) { ClientSeasonCommand(args); }; <>c.<>9__21_1 = val; obj = (object)val; } new ConsoleCommand("vg_season", "Vargheim season gate: `vg_season` shows the armed season, `vg_season arm` (admin) arms the world for the config's SeasonId.", (ConsoleEvent)obj, false, false, false, false, false, false, (ConsoleOptionsFetcher)null, false, false, false); ((BaseUnityPlugin)this).Logger.LogInfo((object)"VargheimGate 0.3.3 loaded."); } catch (Exception ex) { ((BaseUnityPlugin)this).Logger.LogError((object)("VargheimGate: startup failed, mod inactive: " + ex)); } } internal static void Note(string s) { if ((Object)(object)Instance != (Object)null) { ((BaseUnityPlugin)Instance).Logger.LogInfo((object)("VargheimGate: " + s)); } } internal static void Warn(string s) { if ((Object)(object)Instance != (Object)null) { ((BaseUnityPlugin)Instance).Logger.LogWarning((object)("VargheimGate: " + s)); } } internal static void OnNewConnection(ZNet znet, ZNetPeer peer) { if ((Object)(object)Instance == (Object)null || peer == null || peer.m_rpc == null) { return; } if (znet.IsServer()) { if (znet.IsDedicated()) { ZRpc rpc = peer.m_rpc; rpc.Register<string>("VG_Hello", (Action<ZRpc, string>)Instance.Server_Hello); rpc.Register<string, string>("VG_Report", (Action<ZRpc, string, string>)Instance.Server_Report); rpc.Register<string>("VG_Season", (Action<ZRpc, string>)Instance.Server_Season); } } else { ClearClient(); s_serverRpc = peer.m_rpc; peer.m_rpc.Register<string, int, int>("VG_Settings", (Action<ZRpc, string, int, int>)Client_Settings); peer.m_rpc.Register<string>("VG_Msg", (Action<ZRpc, string>)Client_Msg); peer.m_rpc.Invoke("VG_Hello", new object[1] { "0.3.3" }); } } private static void ClearClient() { s_active = false; s_season = string.Empty; s_serverRpc = null; s_checked = false; s_pending = false; } internal static void OnZNetGone() { ClearClient(); } private static string MajorMinor(string v) { string[] array = (v ?? string.Empty).Split('.'); if (array.Length < 2) { return v; } return array[0] + "." + array[1]; } private static string HostOf(ZRpc rpc) { try { return rpc.GetSocket().GetHostName() ?? string.Empty; } catch { return string.Empty; } } private void Server_Hello(ZRpc rpc, string version) { if (!_enabled.Value) { return; } string text = HostOf(rpc); if (MajorMinor(version) != MajorMinor("0.3.3")) { Warn("player " + text + " has gate version '" + Rules.LogSafe(version) + "', server has 0.3.3."); return; } if (!s_hello.TryGetValue(rpc, out var _)) { s_hello.Add(rpc, new object()); } if (Rules.Ids(_exempt.Value).Contains(text)) { Note("player " + text + " is exempt; not gated."); return; } string text2; try { text2 = Season.Armed(Rules.CleanSeason(_seasonId.Value)); } catch (Exception ex) { Warn("could not read the armed season, player " + text + " is not gated: " + ex.Message); return; } if (!string.IsNullOrEmpty(text2)) { rpc.Invoke("VG_Settings", new object[3] { text2, _skillCap.Value, _nemesisScore.Value }); } } private void Server_Season(ZRpc rpc, string what) { string text = HostOf(rpc); string cfg = Rules.CleanSeason(_seasonId.Value); string text2; try { text2 = ((!(what == "arm")) ? Season.Status(cfg) : (Season.IsAdmin(text) ? Season.Arm(cfg) : "only an admin can arm a season.")); } catch (Exception ex) { text2 = "season: " + ex.Message; } Note("vg_season " + Rules.LogSafe(what) + " from " + text + ": " + text2); rpc.Invoke("VG_Msg", new object[1] { text2 }); } internal static void LogLine(string host, string what, string details) { string text = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) + "|" + Rules.LogSafe(host) + "|" + Rules.LogSafe(what) + "|" + Rules.LogSafe(details); try { File.AppendAllText(Path.Combine(Paths.ConfigPath, "vargheim-gate-log.txt"), text + "\n"); } catch (Exception ex) { Warn("could not write vargheim-gate-log.txt: " + ex.Message); } } internal static bool Server_AllowPeerNote(ZNet znet, ZRpc rpc) { if ((Object)(object)Instance == (Object)null || !znet.IsServer() || !znet.IsDedicated() || !Instance._enabled.Value || !Instance._requireMod.Value) { return true; } if (s_hello.TryGetValue(rpc, out var _)) { return true; } Warn("refused " + HostOf(rpc) + ": joined without VargheimGate 0.3.3."); rpc.Invoke("Error", new object[1] { 3 }); return false; } private void Server_Report(ZRpc rpc, string what, string details) { string text = HostOf(rpc); if (what == "forget") { ForgetProfessions(text, details); } Note(Rules.LogSafe(text) + "|" + Rules.LogSafe(what) + "|" + Rules.LogSafe(details)); LogLine(text, what, details); } private static void ForgetProfessions(string host, string name) { try { Type type = AccessTools.TypeByName("VargheimProfessions.Plugin"); MethodInfo methodInfo = ((type != null) ? type.GetMethod("Forget", BindingFlags.Static | BindingFlags.Public) : null); if (methodInfo == null) { Note("professions: Vargheim Professions 0.5.15+ not found, " + Rules.LogSafe(name) + " keeps its record."); return; } object obj = methodInfo.Invoke(null, new object[2] { host, name }); Note("professions: " + Rules.LogSafe(name) + " (" + host + ") " + ((obj is bool && (bool)obj) ? "record cleared." : "had no record.")); } catch (Exception ex) { Warn("professions: could not clear " + Rules.LogSafe(name) + ": " + ex.Message); } } private static void Client_Settings(ZRpc rpc, string season, int cap, int nemesis) { if (rpc == s_serverRpc && !((Object)(object)ZNet.instance == (Object)null) && !ZNet.instance.IsServer()) { s_season = Rules.CleanSeason(season); s_cap = Mathf.Clamp(cap, 0, 100); s_nemesis = Mathf.Clamp(nemesis, 0, 100000); s_active = true; s_checked = false; Note("this server runs the season gate (season " + s_season + ", skill cap " + s_cap + ")."); } } private static void Client_Msg(ZRpc rpc, string text) { if (rpc == s_serverRpc) { Note(text); if ((Object)(object)Console.instance != (Object)null) { Console.instance.Print("[gate] " + text); } } } private static void ClientSeasonCommand(ConsoleEventArgs args) { string text = ((args.Length > 1) ? args[1].ToLowerInvariant() : "status"); if ((Object)(object)ZNet.instance != (Object)null && ZNet.instance.IsServer()) { string cfg = (((Object)(object)Instance != (Object)null) ? Rules.CleanSeason(Instance._seasonId.Value) : string.Empty); try { args.Context.AddString("[gate] " + ((text == "arm") ? Season.Arm(cfg) : Season.Status(cfg))); return; } catch (Exception ex) { args.Context.AddString("[gate] " + ex.Message); return; } } if (s_serverRpc == null || !s_serverRpc.IsConnected()) { args.Context.AddString("[gate] not connected to a gated server."); return; } s_serverRpc.Invoke("VG_Season", new object[1] { text }); args.Context.AddString("[gate] asked the server (" + text + ")..."); } internal static bool BlockInput() { return s_pending; } private void Update() { //IL_01cd: Unknown result type (might be due to invalid IL or missing references) //IL_01d2: Unknown result type (might be due to invalid IL or missing references) //IL_01d8: Expected O, but got Unknown //IL_01f9: Unknown result type (might be due to invalid IL or missing references) //IL_0203: Expected O, but got Unknown //IL_01ec: Unknown result type (might be due to invalid IL or missing references) //IL_01f1: Unknown result type (might be due to invalid IL or missing references) //IL_01f7: Expected O, but got Unknown if (!s_active || (Object)(object)ZNet.instance == (Object)null || ZNet.instance.IsServer()) { return; } Player localPlayer = Player.m_localPlayer; if ((Object)(object)localPlayer == (Object)null) { return; } try { if (s_pending) { if (Time.unscaledTime - s_pendingSince > 180f) { Decline("no answer"); } } else { if (s_checked) { return; } s_checked = true; if (localPlayer.m_customData.TryGetValue("vargheim.gate", out var value) && value == s_season) { Report("returning", localPlayer.GetPlayerName()); return; } float num = 0f; foreach (Skill skill in ((Character)localPlayer).GetSkills().GetSkillList()) { num += skill.m_level; } int count = ((ICollection)Need(localPlayer, "m_knownStations")).Count; if (Rules.LooksNew(num, count)) { Apply(localPlayer, "new character"); return; } string[] array = s_clearFields; foreach (string field in array) { Need(localPlayer, field); } if (!UnifiedPopup.IsAvailable()) { throw new InvalidOperationException("the game's question window is not available"); } s_pending = true; s_pendingSince = Time.unscaledTime; string text = "This character has not entered Vargheim season " + s_season + " yet.\n\nIf you continue, it is changed for good, on this PC and in every world:\n- everything it carries and wears is removed\n- all recipes, build pieces and other unlocks are forgotten\n- skills above " + s_cap + " are lowered to " + s_cap + "\n- its professions and its Nemesis record on this server start over\n\nThis cannot be undone. To keep this character as it is, choose No and join with a new or copied character.\n\nEnter the season with this character?"; object obj = <>c.<>9__39_0; if (obj == null) { PopupButtonCallback val = delegate { UnifiedPopup.Pop(); Accept(); }; <>c.<>9__39_0 = val; obj = (object)val; } object obj2 = <>c.<>9__39_1; if (obj2 == null) { PopupButtonCallback val2 = delegate { UnifiedPopup.Pop(); Decline("chose No"); }; <>c.<>9__39_1 = val2; obj2 = (object)val2; } UnifiedPopup.Push((PopupBase)new YesNoPopup("Vargheim season gate", text, (PopupButtonCallback)obj, (PopupButtonCallback)obj2, false, false)); } } catch (Exception ex) { ((BaseUnityPlugin)this).Logger.LogError((object)("VargheimGate: gate check failed, nothing was changed, leaving the server: " + ex)); Decline("gate error: " + ex.GetType().Name); } } private static void Accept() { s_pending = false; Player localPlayer = Player.m_localPlayer; if ((Object)(object)localPlayer == (Object)null || !s_active) { return; } try { Apply(localPlayer, "existing character"); } catch (Exception ex) { if ((Object)(object)Instance != (Object)null) { ((BaseUnityPlugin)Instance).Logger.LogError((object)("VargheimGate: reset failed part way: " + ex)); } } } private static void Decline(string why) { s_pending = false; s_active = false; Report("declined", why); try { if (UnifiedPopup.IsVisible()) { UnifiedPopup.Pop(); } } catch { } if ((Object)(object)Game.instance != (Object)null) { Game.instance.Logout(true, true); } } private static void Report(string what, string details) { try { if (s_serverRpc != null && s_serverRpc.IsConnected()) { s_serverRpc.Invoke("VG_Report", new object[2] { what, details }); } } catch { } } private static object Need(Player p, string field) { FieldInfo fieldInfo = AccessTools.Field(typeof(Player), field); object obj = ((fieldInfo != null) ? fieldInfo.GetValue(p) : null); if (obj == null || (!(obj is ICollection) && !(obj.GetType().GetMethod("Clear", Type.EmptyTypes) != null)) || obj.GetType().GetMethod("Clear", Type.EmptyTypes) == null) { throw new MissingFieldException("Player." + field); } return obj; } private static void Apply(Player p, string kind) { List<object> list = new List<object>(); string[] array = s_clearFields; foreach (string field in array) { list.Add(Need(p, field)); } List<Skill> skillList = ((Character)p).GetSkills().GetSkillList(); int count = ((Humanoid)p).GetInventory().GetAllItems().Count; ((Humanoid)p).UnequipAllItems(); ((Humanoid)p).GetInventory().RemoveAll(); foreach (object item in list) { item.GetType().GetMethod("Clear", Type.EmptyTypes).Invoke(item, null); } p.SetGuardianPower(string.Empty); p.m_guardianPowerCooldown = 0f; p.ClearFood(); int num = 0; foreach (Skill item2 in skillList) { if (Rules.Cap(item2.m_level, s_cap, out var capped)) { item2.m_level = capped; item2.m_accumulator = 0f; num++; } } string text = ResetNemesis(p); ((Humanoid)p).GiveDefaultItems(); p.m_customData["vargheim.gate"] = s_season; ((Character)p).Message((MessageType)2, "You enter Vargheim season " + s_season + " with empty hands.", 0, (Sprite)null, false); Note("gate applied (" + kind + "): " + count + " item stacks removed, " + num + " skills lowered to " + s_cap + ", " + text + "."); Report("reset", p.GetPlayerName() + ", " + kind + ", " + count + " item stacks removed, " + num + " skills lowered to " + s_cap + ", " + text); Report("forget", p.GetPlayerName()); } private static string ResetNemesis(Player p) { try { bool flag = p.m_customData.Remove("SLS_NEM_SCOREDATA"); ZNetView component = ((Component)p).GetComponent<ZNetView>(); ZDO val = (((Object)(object)component != (Object)null && component.IsValid()) ? component.GetZDO() : null); if (val != null) { val.Set("SLS_NEM_SCORE", s_nemesis); } Type type = AccessTools.TypeByName("StarLevelSystem.modules.NemesisSystem.NemesisSystem"); if (type != null) { FieldInfo fieldInfo = AccessTools.Field(type, "PlayerScore"); if (fieldInfo != null) { Type type2 = AccessTools.TypeByName("StarLevelSystem.common.DataObjects+ScoreData"); fieldInfo.SetValue(null, (type2 != null) ? Activator.CreateInstance(type2) : null); } FieldInfo fieldInfo2 = AccessTools.Field(type, "CachedPlayerScore"); if (fieldInfo2 != null && fieldInfo2.FieldType == typeof(float)) { fieldInfo2.SetValue(null, s_nemesis); } } return "nemesis score set to " + s_nemesis + (flag ? " (history cleared)" : ""); } catch (Exception ex) { Warn("nemesis reset: " + ex.Message); return "nemesis reset failed"; } } } [HarmonyPatch] internal static class Patches { [HarmonyPatch(typeof(ZNet), "OnNewConnection")] [HarmonyPostfix] private static void ZNet_OnNewConnection(ZNet __instance, ZNetPeer peer) { try { Plugin.OnNewConnection(__instance, peer); } catch (Exception ex) { Plugin.Warn("connection hook failed: " + ex.Message); } } [HarmonyPatch(typeof(ZNet), "RPC_PeerInfo")] [HarmonyPrefix] [HarmonyPriority(800)] private static bool ZNet_RPC_PeerNote(ZNet __instance, ZRpc rpc) { try { return Plugin.Server_AllowPeerNote(__instance, rpc); } catch (Exception ex) { Plugin.Warn("join check failed, player let in: " + ex.Message); return true; } } [HarmonyPatch(typeof(ZNet), "OnDestroy")] [HarmonyPostfix] private static void ZNet_OnDestroy() { Plugin.OnZNetGone(); } [HarmonyPatch(typeof(Player), "TakeInput")] [HarmonyPostfix] private static void Player_TakeInput(ref bool __result) { if (Plugin.BlockInput()) { __result = false; } } [HarmonyPatch(typeof(InventoryGui), "Show")] [HarmonyPrefix] private static bool InventoryGui_Show() { return !Plugin.BlockInput(); } } public static class Rules { public const string MarkerKey = "vargheim.gate"; public static bool Cap(float level, float cap, out float capped) { if (cap < 0f) { cap = 0f; } if (level > cap) { capped = cap; return true; } capped = level; return false; } public static HashSet<string> Ids(string text) { HashSet<string> hashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase); StringBuilder stringBuilder = new StringBuilder(); string text2 = (text ?? string.Empty) + ","; foreach (char c in text2) { if (char.IsLetterOrDigit(c) || c == '_') { stringBuilder.Append(c); } else if (stringBuilder.Length > 0) { hashSet.Add(stringBuilder.ToString()); stringBuilder.Length = 0; } } return hashSet; } public static string CleanSeason(string s) { StringBuilder stringBuilder = new StringBuilder(); string text = (s ?? string.Empty).Trim(); foreach (char c in text) { if ((char.IsLetterOrDigit(c) && c < '\u0080') || c == '-' || c == '_') { stringBuilder.Append(c); } } string text2 = stringBuilder.ToString(); if (text2.Length > 24) { text2 = text2.Substring(0, 24); } if (text2.Length != 0) { return text2; } return "S2"; } public static bool LooksNew(float totalSkillLevels, int knownStations) { if (totalSkillLevels < 1f) { return knownStations == 0; } return false; } public static string LogSafe(string s) { s = (s ?? string.Empty).Replace('\r', ' ').Replace('\n', ' ').Replace('|', '/'); if (s.Length > 120) { return s.Substring(0, 120); } return s; } } public static class SafePatch { public delegate void Logger(string message); public static int Apply(Harmony harmony, Type patchClass, Logger warn, string name = null) { //IL_0117: Unknown result type (might be due to invalid IL or missing references) //IL_011c: Unknown result type (might be due to invalid IL or missing references) //IL_0129: Unknown result type (might be due to invalid IL or missing references) //IL_0136: Unknown result type (might be due to invalid IL or missing references) //IL_0145: Expected O, but got Unknown name = name ?? patchClass.Name; int num = 0; MethodInfo[] methods = patchClass.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); foreach (MethodInfo methodInfo in methods) { List<HarmonyMethod> fromMethod = HarmonyMethodExtensions.GetFromMethod((MethodBase)methodInfo); if (fromMethod == null || fromMethod.Count == 0) { continue; } HarmonyMethod val = HarmonyMethod.Merge(fromMethod); bool flag = ((MemberInfo)methodInfo).GetCustomAttribute<HarmonyPrefix>() != null; bool flag2 = ((MemberInfo)methodInfo).GetCustomAttribute<HarmonyPostfix>() != null; bool flag3 = ((MemberInfo)methodInfo).GetCustomAttribute<HarmonyFinalizer>() != null; if (!flag && !flag2 && !flag3) { continue; } string text = ((val.declaringType != null) ? val.declaringType.Name : "?") + "." + (val.methodName ?? "?"); try { MethodBase methodBase = Resolve(val); if (methodBase == null) { num++; warn(name + ": " + text + " not found (game update?) - " + methodInfo.Name + " is OFF."); continue; } PatchProcessor val2 = harmony.CreateProcessor(methodBase); HarmonyMethod val3 = new HarmonyMethod(methodInfo) { priority = val.priority, before = val.before, after = val.after }; if (flag) { val2.AddPrefix(val3); } if (flag2) { val2.AddPostfix(val3); } if (flag3) { val2.AddFinalizer(val3); } val2.Patch(); } catch (Exception ex) { num++; warn(name + ": " + text + " could not be patched (" + ex.GetType().Name + ": " + ex.Message + ") - " + methodInfo.Name + " is OFF."); } } return num; } public static FieldRef<T, F> FieldRef<T, F>(string field, Logger warn) { try { return AccessTools.FieldRefAccess<T, F>(field); } catch (Exception ex) { warn?.Invoke(typeof(T).Name + "." + field + " not found (game update?): " + ex.Message); return null; } } public static FieldRef<F> StaticFieldRef<F>(Type type, string field, Logger warn) { try { FieldInfo fieldInfo = AccessTools.Field(type, field); if (fieldInfo == null) { warn?.Invoke(type.Name + "." + field + " not found (game update?)."); return null; } return AccessTools.StaticFieldRefAccess<F>(fieldInfo); } catch (Exception ex) { warn?.Invoke(type.Name + "." + field + ": " + ex.Message); return null; } } private static MethodBase Resolve(HarmonyMethod info) { //IL_0016: Unknown result type (might be due to invalid IL or missing references) //IL_001b: Unknown result type (might be due to invalid IL or missing references) //IL_001c: 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_0034: Expected I4, but got Unknown if (info.declaringType == null) { return null; } MethodType valueOrDefault = info.methodType.GetValueOrDefault(); switch (valueOrDefault - 1) { case 2: return AccessTools.Constructor(info.declaringType, info.argumentTypes, false); case 3: return AccessTools.Constructor(info.declaringType, (Type[])null, true); case 0: if (info.methodName != null) { return AccessTools.PropertyGetter(info.declaringType, info.methodName); } return null; case 1: if (info.methodName != null) { return AccessTools.PropertySetter(info.declaringType, info.methodName); } return null; default: if (info.methodName == null) { return null; } if (info.argumentTypes != null) { return AccessTools.Method(info.declaringType, info.methodName, info.argumentTypes, (Type[])null); } try { return AccessTools.Method(info.declaringType, info.methodName, (Type[])null, (Type[])null); } catch (AmbiguousMatchException) { return null; } } } } internal static class Season { internal const string Key = "vg_season"; private static string s_warnedFor; internal static string Armed(string cfg) { string text = Read(); if (string.IsNullOrEmpty(text)) { if (string.IsNullOrEmpty(cfg)) { return cfg; } Write(cfg); Plugin.Note("season '" + cfg + "' armed from the config: this world had no armed season yet."); Plugin.LogLine("server", "armed", cfg + " (first run)"); return cfg; } if (Same(text, cfg)) { return cfg; } if (s_warnedFor != cfg) { s_warnedFor = cfg; Plugin.Warn("SeasonId in the config is '" + cfg + "' but this world is armed for '" + text + "'. The gate keeps using '" + text + "'. If a new season is really meant to start, an admin runs `vg_season arm` in game."); } return text; } internal static string Status(string cfg) { string text = Read(); return "armed: " + (string.IsNullOrEmpty(text) ? "(none)" : text) + ", config: " + cfg + (Same(text, cfg) ? " (same)" : " (DIFFERENT - run `vg_season arm` to switch the world to the config value)"); } internal static string Arm(string cfg) { if (string.IsNullOrEmpty(cfg)) { return "config SeasonId is empty, nothing armed."; } string text = Read(); if (Same(text, cfg)) { return "world is already armed for '" + cfg + "'."; } Write(cfg); s_warnedFor = null; Plugin.Note("season armed by admin: '" + text + "' -> '" + cfg + "'. Every character that does not carry this mark is asked to reset on its next join."); Plugin.LogLine("server", "armed", cfg + " (was " + (text ?? "none") + ")"); return "world armed for season '" + cfg + "' (was " + (text ?? "none") + "). Characters without this mark are asked to reset on their next join."; } internal static bool Same(string a, string b) { if (!string.IsNullOrEmpty(a)) { return string.Equals(a, b, StringComparison.OrdinalIgnoreCase); } return false; } private static IDictionary Values() { ZoneSystem instance = ZoneSystem.instance; if ((Object)(object)instance == (Object)null) { throw new InvalidOperationException("ZoneSystem is not up yet"); } FieldInfo fieldInfo = AccessTools.Field(typeof(ZoneSystem), "m_globalKeysValues"); return ((fieldInfo != null) ? (fieldInfo.GetValue(instance) as IDictionary) : null) ?? throw new MissingFieldException("ZoneSystem.m_globalKeysValues"); } private static string Read() { foreach (DictionaryEntry item in Values()) { if (item.Key is string a && string.Equals(a, "vg_season", StringComparison.OrdinalIgnoreCase)) { return Rules.CleanSeason(item.Value as string); } } return null; } private static void Write(string label) { ZoneSystem instance = ZoneSystem.instance; MethodInfo methodInfo = AccessTools.Method(typeof(ZoneSystem), "SetGlobalKey", new Type[2] { typeof(string), typeof(string) }, (Type[])null); if (methodInfo != null) { methodInfo.Invoke(instance, new object[2] { "vg_season", label }); } else { MethodInfo methodInfo2 = AccessTools.Method(typeof(ZoneSystem), "SetGlobalKey", new Type[1] { typeof(string) }, (Type[])null); if (methodInfo2 == null) { throw new MissingMethodException("ZoneSystem.SetGlobalKey"); } methodInfo2.Invoke(instance, new object[1] { "vg_season " + label }); } string text = Read(); if (!Same(text, label)) { MethodInfo methodInfo3 = AccessTools.Method(typeof(ZoneSystem), "SetGlobalKey", new Type[1] { typeof(string) }, (Type[])null); if (methodInfo3 != null) { methodInfo3.Invoke(instance, new object[1] { "vg_season " + label }); text = Read(); } if (!Same(text, label)) { throw new InvalidOperationException("global key did not stick (read back '" + text + "')"); } } } internal static bool IsAdmin(string host) { ZNet instance = ZNet.instance; if ((Object)(object)instance == (Object)null || string.IsNullOrEmpty(host)) { return false; } FieldInfo fieldInfo = AccessTools.Field(typeof(ZNet), "m_adminList"); object obj = ((fieldInfo != null) ? fieldInfo.GetValue(instance) : null); if (obj == null) { return false; } MethodInfo methodInfo = AccessTools.Method(typeof(ZNet), "ListContainsId", (Type[])null, (Type[])null); if (methodInfo != null) { try { return (bool)methodInfo.Invoke(instance, new object[2] { obj, host }); } catch { } } MethodInfo method = obj.GetType().GetMethod("Contains", new Type[1] { typeof(string) }); if (method != null) { return (bool)method.Invoke(obj, new object[1] { host }); } return false; } } }