using System; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Versioning; using BepInEx; using BepInEx.Logging; using HarmonyLib; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: AssemblyTitle("IronLabs.DirectPlay")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("IronLabs.DirectPlay")] [assembly: AssemblyCopyright("Copyright © 2026")] [assembly: AssemblyTrademark("")] [assembly: ComVisible(false)] [assembly: Guid("046ED613-45E5-48D3-9B7B-72703870DA05")] [assembly: AssemblyFileVersion("1.0.4")] [assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")] [assembly: AssemblyVersion("1.0.4.0")] namespace IronLabs.DirectPlay; [BepInPlugin("Ironlabs.Directplay", "IronLabs.DirectPlay", "1.0.4")] public sealed class DirectPlayPlugin : BaseUnityPlugin { [HarmonyPatch(typeof(FejdStartup), "Start")] private static class StartPatch { private static void Postfix(FejdStartup __instance) { ModLogger.LogDebug("DirectPlay FejdStartup.Start postfix invoked."); StartPostfix(__instance); } } [HarmonyPatch(typeof(FejdStartup), "OnWorldStart")] private static class LocalSessionPatch { private static void Prefix() { ModLogger.LogDebug("DirectPlay FejdStartup.OnWorldStart prefix invoked."); PlatformPrefs.SetString("IronLabs.DirectPlay.LastSession", "local"); } } [HarmonyPatch(typeof(FejdStartup), "JoinServer")] private static class MultiplayerSessionPatch { private static void Prefix(FejdStartup __instance) { ModLogger.LogDebug("DirectPlay FejdStartup.JoinServer prefix invoked."); if (__instance.HasServerToJoin()) { PlatformPrefs.SetString("IronLabs.DirectPlay.LastSession", "multiplayer"); } } } private const string PluginGuid = "Ironlabs.Directplay"; private const string PluginName = "IronLabs.DirectPlay"; private const string PluginVersion = "1.0.4"; private const string LastSessionPreference = "IronLabs.DirectPlay.LastSession"; private const string LocalSession = "local"; private const string MultiplayerSession = "multiplayer"; private static readonly ModLog ModLogger = new ModLog(Logger.CreateLogSource("IronLabs.DirectPlay"), "IronLabs.DirectPlay"); private static bool _attemptedAutoStart; private readonly Harmony _harmony = new Harmony("Ironlabs.Directplay"); private static bool _patchesApplied; private void Awake() { PatchOwnNamespace(); ModLogger.LogInfo("IronLabs.DirectPlay 1.0.4 is loaded."); } private void PatchOwnNamespace() { if (_patchesApplied) { ModLogger.LogDebug("Harmony patches are already active; skipping registration."); return; } string text = typeof(DirectPlayPlugin).Namespace; Type[] types = Assembly.GetExecutingAssembly().GetTypes(); foreach (Type type in types) { if (type.Namespace == text) { _harmony.CreateClassProcessor(type).Patch(); } } _patchesApplied = true; ModLogger.LogDebug("Harmony patches were applied for the plugin namespace."); } private void OnDestroy() { if (_patchesApplied) { _harmony.UnpatchSelf(); _patchesApplied = false; } } private static void StartPostfix(FejdStartup startup) { if (_attemptedAutoStart || HasDisabledArgument()) { return; } _attemptedAutoStart = true; string profileFilename = PlatformPrefs.GetString("profile", ""); if (!RememberedProfileExists(profileFilename)) { return; } if (PlatformPrefs.GetString("IronLabs.DirectPlay.LastSession", "") == "multiplayer") { JoinLastMultiplayerSession(startup, profileFilename); return; } string worldName = PlatformPrefs.GetString("world", ""); if (RememberedWorldExists(worldName)) { startup.OnCharacterStart(); if (IsRememberedWorldSelected(startup, worldName)) { StartWorld(startup, profileFilename, worldName); } } } private static bool HasDisabledArgument() { string[] commandLineArgs = Environment.GetCommandLineArgs(); for (int i = 0; i < commandLineArgs.Length - 1; i++) { if (MatchesDisabledArgument(commandLineArgs, i)) { ModLogger.LogDebug("Received command-line switch: --directplay false."); return true; } } return false; } private static bool MatchesDisabledArgument(string[] arguments, int index) { if (string.Equals(arguments[index], "--directplay", StringComparison.OrdinalIgnoreCase)) { return string.Equals(arguments[index + 1], "false", StringComparison.OrdinalIgnoreCase); } return false; } private static bool RememberedProfileExists(string profileFilename) { if (string.IsNullOrEmpty(profileFilename)) { return false; } if (!SaveSystem.GetAllPlayerProfiles().Exists((PlayerProfile profile) => profile.GetFilename() == profileFilename)) { return false; } return true; } private static bool RememberedWorldExists(string worldName) { if (string.IsNullOrEmpty(worldName)) { return false; } if (SaveSystem.GetWorldList().Exists((World world) => world.m_name == worldName)) { return true; } return false; } private static void JoinLastMultiplayerSession(FejdStartup startup, string profileFilename) { //IL_000b: Unknown result type (might be due to invalid IL or missing references) //IL_0011: Expected O, but got Unknown //IL_001b: Unknown result type (might be due to invalid IL or missing references) //IL_0020: 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_0044: Unknown result type (might be due to invalid IL or missing references) //IL_0060: Unknown result type (might be due to invalid IL or missing references) LocalServerList val = new LocalServerList((string)null, ServerListGui.GetServerListLocations("recent")); ServerJoinData val3; try { if (val.Count == 0) { goto IL_002a; } ServerJoinData val2 = val[0]; if (!((ServerJoinData)(ref val2)).IsValid) { goto IL_002a; } val3 = val[0]; startup.SetServerToJoin(val3); goto end_IL_0011; IL_002a: ModLogger.LogError("DirectPlay could not find a valid recent multiplayer server."); return; end_IL_0011:; } finally { ((IDisposable)val)?.Dispose(); } ModLogger.LogDebug($"DirectPlay is joining multiplayer server '{val3}' with character '{profileFilename}'."); startup.OnCharacterStart(); startup.JoinServer(); } private static bool IsRememberedWorldSelected(FejdStartup startup, string worldName) { World value = Traverse.Create((object)startup).Field("m_world").GetValue(); if (value != null && value.m_name == worldName) { return true; } return false; } private static void StartWorld(FejdStartup startup, string profileFilename, string worldName) { ModLogger.LogDebug("DirectPlay is joining local world '" + worldName + "' with character '" + profileFilename + "'."); startup.OnWorldStart(); } } internal sealed class ModLog { private readonly ManualLogSource _logger; private readonly string _pluginName; internal ModLog(ManualLogSource logger, string pluginName) { _logger = logger; _pluginName = pluginName; } internal void LogFatal(object message) { Write((LogLevel)1, message); } internal void LogError(object message) { Write((LogLevel)2, message); } internal void LogWarning(object message) { Write((LogLevel)4, message); } internal void LogMessage(object message) { Write((LogLevel)8, message); } internal void LogInfo(object message) { Write((LogLevel)16, message); } internal void LogDebug(object message) { Write((LogLevel)32, message); } private void Write(LogLevel level, object message) { //IL_0006: Unknown result type (might be due to invalid IL or missing references) _logger.Log(level, message); } }