From 88a03d102ab0232b19e4e4a1951f93be4523b893 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Mon, 11 Jul 2022 22:56:01 -0400 Subject: [PATCH 01/34] Recover mod after disconnect Tested (once so far) and it worked. Keeping as draft until further test runs. --- HouseRules_Core/LifecycleDirector.cs | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 74e096a9..e2d3adaa 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -18,6 +18,7 @@ internal static class LifecycleDirector private static GameContext _gameContext; private static bool _isCreatingGame; private static bool _isLoadingGame; + private static long _gameId; internal static bool IsRulesetActive { get; private set; } @@ -39,6 +40,12 @@ internal static void Patch(Harmony harmony) .GetDeclaredMethod("OnJoinedRoom"), prefix: new HarmonyMethod(typeof(LifecycleDirector), nameof(CreatingGameState_OnJoinedRoom_Prefix))); + harmony.Patch( + original: AccessTools + .Inner(typeof(GameStateMachine), "PlayingState").GetTypeInfo() + .GetDeclaredMethod("OnMasterClientChanged"), + prefix: new HarmonyMethod(typeof(LifecycleDirector), nameof(CreatingGameState_OnMasterClientChanged_Prefix))); + harmony.Patch( original: AccessTools.Method(typeof(GameStateMachine), "GoToPlayingState"), postfix: new HarmonyMethod(typeof(LifecycleDirector), nameof(GameStateMachine_GoToPlayingState_Postfix))); @@ -133,6 +140,40 @@ private static void CreatingGameState_OnJoinedRoom_Prefix() OnPreGameCreated(); } + private static void CreatingGameState_OnMasterClientChanged_Prefix() + { + MelonLoader.MelonLogger.Warning("Master Client changed..."); + if (!GameStateMachine.IsMasterClient) + { + MelonLoader.MelonLogger.Warning("I'm *NOT* the Master Client yet..."); + return; + } + + MelonLoader.MelonLogger.Warning("I'm the Master Client now!"); + if (HR.SelectedRuleset == Ruleset.None) + { + return; + } + + if (_gameContext.gameStateMachine.goBackToMenuState) + { + return; + } + + if (_gameId != GameHub.GameID) + { + MelonLoader.MelonLogger.Warning($"Old game {_gameId} doesn't match new game {GameHub.GameID}"); + return; + } + + var levelSequence = Traverse.Create(_gameContext.gameStateMachine).Field("levelSequence").Value; + MotherbrainGlobalVars.CurrentConfig = levelSequence.gameConfig; + + CoreMod.Logger.Warning($"<--- Resuming ruleset after disconnection from game {_gameId} --->"); + ActivateRuleset(); + OnPreGameCreated(); + } + private static void GameStateMachine_GoToPlayingState_Postfix() { if (!_isCreatingGame) @@ -166,11 +207,14 @@ private static void PostGameControllerBase_OnPlayAgainClicked_Postfix() private static void GameStateMachine_EndGame_Prefix() { + _gameId = 0; DeactivateRuleset(); } private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix() { + _gameId = GameHub.GameID; + MelonLoader.MelonLogger.Warning($"<--- Disconnected from game {_gameId} --->"); DeactivateRuleset(); } From dd7f525bb0f1171dc26db9ebe34924b2a0b7caa9 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Mon, 11 Jul 2022 23:09:19 -0400 Subject: [PATCH 02/34] Fix typo wrong gamestate name in method --- HouseRules_Core/LifecycleDirector.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index e2d3adaa..f14a6110 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -44,7 +44,7 @@ internal static void Patch(Harmony harmony) original: AccessTools .Inner(typeof(GameStateMachine), "PlayingState").GetTypeInfo() .GetDeclaredMethod("OnMasterClientChanged"), - prefix: new HarmonyMethod(typeof(LifecycleDirector), nameof(CreatingGameState_OnMasterClientChanged_Prefix))); + prefix: new HarmonyMethod(typeof(LifecycleDirector), nameof(PlayingGameState_OnMasterClientChanged_Prefix))); harmony.Patch( original: AccessTools.Method(typeof(GameStateMachine), "GoToPlayingState"), @@ -140,7 +140,7 @@ private static void CreatingGameState_OnJoinedRoom_Prefix() OnPreGameCreated(); } - private static void CreatingGameState_OnMasterClientChanged_Prefix() + private static void PlayingGameState_OnMasterClientChanged_Prefix() { MelonLoader.MelonLogger.Warning("Master Client changed..."); if (!GameStateMachine.IsMasterClient) From 0f471f71790ff0a18ddcf11fced76d3ab367ee3f Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Tue, 12 Jul 2022 02:42:42 -0400 Subject: [PATCH 03/34] Oops! Totally forgot to load Post with the Pre. --- HouseRules_Core/LifecycleDirector.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index f14a6110..33e2cff2 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -172,6 +172,7 @@ private static void PlayingGameState_OnMasterClientChanged_Prefix() CoreMod.Logger.Warning($"<--- Resuming ruleset after disconnection from game {_gameId} --->"); ActivateRuleset(); OnPreGameCreated(); + OnPostGameCreated(); } private static void GameStateMachine_GoToPlayingState_Postfix() From 3cc307745eb1c4bd32de83d56be5f5c1aafbb928 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Tue, 12 Jul 2022 22:12:01 -0400 Subject: [PATCH 04/34] Fix multiple players using HR This should prevent a second HR using player from activating their ruleset if they gain host. --- HouseRules_Core/LifecycleDirector.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 33e2cff2..220ef37d 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -136,6 +136,8 @@ private static void CreatingGameState_OnJoinedRoom_Prefix() var levelSequence = Traverse.Create(_gameContext.gameStateMachine).Field("levelSequence").Value; MotherbrainGlobalVars.CurrentConfig = levelSequence.gameConfig; + _gameId = GameHub.GameID; + CoreMod.Logger.Warning($"Game: {_gameId} loaded"); ActivateRuleset(); OnPreGameCreated(); } @@ -214,7 +216,6 @@ private static void GameStateMachine_EndGame_Prefix() private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix() { - _gameId = GameHub.GameID; MelonLoader.MelonLogger.Warning($"<--- Disconnected from game {_gameId} --->"); DeactivateRuleset(); } From f44951e209de6164d9599bea9f861137d1a8cf15 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Tue, 12 Jul 2022 22:23:50 -0400 Subject: [PATCH 05/34] Notify on ruleset re-activation Notify Host with a shorter timed welcome message when ruleset is restored. --- HouseRules_Core/LifecycleDirector.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 220ef37d..46fb6f03 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -12,7 +12,7 @@ internal static class LifecycleDirector { - private const float WelcomeMessageDurationSeconds = 30f; + private static float WelcomeMessageDurationSeconds = 30f; private const string ModdedRoomPropertyKey = "modded"; private static GameContext _gameContext; @@ -175,6 +175,9 @@ private static void PlayingGameState_OnMasterClientChanged_Prefix() ActivateRuleset(); OnPreGameCreated(); OnPostGameCreated(); + WelcomeMessageDurationSeconds = 10f; + ShowWelcomeMessage(); + WelcomeMessageDurationSeconds = 30f; } private static void GameStateMachine_GoToPlayingState_Postfix() From 6cfe995a807c9449589b470b8423908f567df11a Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Wed, 13 Jul 2022 01:15:55 -0400 Subject: [PATCH 06/34] Clarifying log reports N/A --- HouseRules_Core/LifecycleDirector.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 46fb6f03..2d8476bd 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -137,7 +137,7 @@ private static void CreatingGameState_OnJoinedRoom_Prefix() MotherbrainGlobalVars.CurrentConfig = levelSequence.gameConfig; _gameId = GameHub.GameID; - CoreMod.Logger.Warning($"Game: {_gameId} loaded"); + CoreMod.Logger.Warning($"New game with gameId {_gameId} loaded"); ActivateRuleset(); OnPreGameCreated(); } @@ -164,7 +164,7 @@ private static void PlayingGameState_OnMasterClientChanged_Prefix() if (_gameId != GameHub.GameID) { - MelonLoader.MelonLogger.Warning($"Old game {_gameId} doesn't match new game {GameHub.GameID}"); + MelonLoader.MelonLogger.Warning($"Previous gameId {_gameId} doesn't match this gameId {GameHub.GameID}"); return; } From 26a88c8cc41252a0976b8620ccc6030e9c270c7a Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Fri, 15 Jul 2022 22:05:59 -0400 Subject: [PATCH 07/34] Fixed freeze Level sequence already loads properly without redefining it. Freezes game otherwise. --- HouseRules_Core/LifecycleDirector.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 2d8476bd..4b17abfa 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -12,9 +12,8 @@ internal static class LifecycleDirector { - private static float WelcomeMessageDurationSeconds = 30f; private const string ModdedRoomPropertyKey = "modded"; - + private static float WelcomeMessageDurationSeconds = 30f; private static GameContext _gameContext; private static bool _isCreatingGame; private static bool _isLoadingGame; @@ -137,7 +136,7 @@ private static void CreatingGameState_OnJoinedRoom_Prefix() MotherbrainGlobalVars.CurrentConfig = levelSequence.gameConfig; _gameId = GameHub.GameID; - CoreMod.Logger.Warning($"New game with gameId {_gameId} loaded"); + CoreMod.Logger.Warning($"New game with gameId {_gameId} started"); ActivateRuleset(); OnPreGameCreated(); } @@ -168,9 +167,6 @@ private static void PlayingGameState_OnMasterClientChanged_Prefix() return; } - var levelSequence = Traverse.Create(_gameContext.gameStateMachine).Field("levelSequence").Value; - MotherbrainGlobalVars.CurrentConfig = levelSequence.gameConfig; - CoreMod.Logger.Warning($"<--- Resuming ruleset after disconnection from game {_gameId} --->"); ActivateRuleset(); OnPreGameCreated(); From f4eb92f44c27353b004a8a65b109b6536054496a Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Fri, 15 Jul 2022 22:14:29 -0400 Subject: [PATCH 08/34] Skip this rule when loading OnPreGameCreated Prevents error message when loading ruleset after reconnect. --- HouseRules_Core/LifecycleDirector.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 4b17abfa..9cfa599b 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -326,7 +326,10 @@ private static void OnPreGameCreated() try { CoreMod.Logger.Msg($"Calling OnPreGameCreated for rule type: {rule.GetType()}"); - rule.OnPreGameCreated(_gameContext); + if (rule.Description != "LevelSequence is overridden") + { + rule.OnPreGameCreated(_gameContext); + } } catch (Exception e) { From 4ad8190c3926062ef38f21fb35e4b0c1798f274b Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Sat, 16 Jul 2022 05:55:56 -0400 Subject: [PATCH 09/34] Fix LevelSequence loading Fix level sequence not loading on new games since last update. --- HouseRules_Core/LifecycleDirector.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 9cfa599b..8048b12a 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -18,6 +18,7 @@ internal static class LifecycleDirector private static bool _isCreatingGame; private static bool _isLoadingGame; private static long _gameId; + private static bool _isReconnect = false; internal static bool IsRulesetActive { get; private set; } @@ -168,12 +169,14 @@ private static void PlayingGameState_OnMasterClientChanged_Prefix() } CoreMod.Logger.Warning($"<--- Resuming ruleset after disconnection from game {_gameId} --->"); + _isReconnect = true; ActivateRuleset(); OnPreGameCreated(); OnPostGameCreated(); WelcomeMessageDurationSeconds = 10f; ShowWelcomeMessage(); WelcomeMessageDurationSeconds = 30f; + _isReconnect = false; } private static void GameStateMachine_GoToPlayingState_Postfix() @@ -326,7 +329,7 @@ private static void OnPreGameCreated() try { CoreMod.Logger.Msg($"Calling OnPreGameCreated for rule type: {rule.GetType()}"); - if (rule.Description != "LevelSequence is overridden") + if (!_isReconnect || (_isReconnect && rule.Description != "LevelSequence is overridden")) { rule.OnPreGameCreated(_gameContext); } From 6da44404833d7392cc69ef6ef09fb8bbe829fefa Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Sat, 16 Jul 2022 07:59:37 -0400 Subject: [PATCH 10/34] Piece adjustments must remain loaded Piece adjustments get lost if unloaded so keep them activated for possible reconnect. Don't reload them if reconnect as they are already still loaded. If it's not your game and you become host it will deactivate ALL rules. --- HouseRules_Core/LifecycleDirector.cs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 8048b12a..fadb1778 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -165,11 +165,12 @@ private static void PlayingGameState_OnMasterClientChanged_Prefix() if (_gameId != GameHub.GameID) { MelonLoader.MelonLogger.Warning($"Previous gameId {_gameId} doesn't match this gameId {GameHub.GameID}"); + _isReconnect = false; + DeactivateRuleset(); return; } CoreMod.Logger.Warning($"<--- Resuming ruleset after disconnection from game {_gameId} --->"); - _isReconnect = true; ActivateRuleset(); OnPreGameCreated(); OnPostGameCreated(); @@ -218,7 +219,8 @@ private static void GameStateMachine_EndGame_Prefix() private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix() { - MelonLoader.MelonLogger.Warning($"<--- Disconnected from game {_gameId} --->"); + MelonLoader.MelonLogger.Warning($"<--- Disconnected from game {GameHub.GameID} --->"); + _isReconnect = true; DeactivateRuleset(); } @@ -301,8 +303,11 @@ private static void DeactivateRuleset() { try { - CoreMod.Logger.Msg($"Deactivating rule type: {rule.GetType()}"); - rule.OnDeactivate(_gameContext); + if (!_isReconnect || (_isReconnect && !rule.Description.Contains("Piece "))) + { + CoreMod.Logger.Msg($"Deactivating rule type: {rule.GetType()}"); + rule.OnDeactivate(_gameContext); + } } catch (Exception e) { @@ -329,7 +334,7 @@ private static void OnPreGameCreated() try { CoreMod.Logger.Msg($"Calling OnPreGameCreated for rule type: {rule.GetType()}"); - if (!_isReconnect || (_isReconnect && rule.Description != "LevelSequence is overridden")) + if (!_isReconnect || (_isReconnect && (rule.Description != "LevelSequence is overridden" && !rule.Description.Contains("Piece ")))) { rule.OnPreGameCreated(_gameContext); } @@ -358,8 +363,11 @@ private static void OnPostGameCreated() { try { - CoreMod.Logger.Msg($"Calling OnPostGameCreated for rule type: {rule.GetType()}"); - rule.OnPostGameCreated(_gameContext); + if (!_isReconnect || (_isReconnect && (rule.Description != "LevelSequence is overridden" && !rule.Description.Contains("Piece ")))) + { + CoreMod.Logger.Msg($"Calling OnPostGameCreated for rule type: {rule.GetType()}"); + rule.OnPostGameCreated(_gameContext); + } } catch (Exception e) { From f84ab00375ba2574001548039bd7051ba190433d Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Sat, 16 Jul 2022 20:05:21 -0400 Subject: [PATCH 11/34] Fix Play Again Set gameId when hitting play again in case of disconnect in next match --- HouseRules_Core/LifecycleDirector.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index fadb1778..037d72ea 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -206,6 +206,7 @@ private static void GameStateMachine_GoToShoppingState_Postfix() private static void PostGameControllerBase_OnPlayAgainClicked_Postfix() { + _gameId = GameHub.GameID; ActivateRuleset(); _isCreatingGame = true; OnPreGameCreated(); From 429b6a4912a4f3c95a9c66f0e75676a60ab0f0c4 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Sun, 17 Jul 2022 04:03:51 -0400 Subject: [PATCH 12/34] Fix missing variable set Some rules were prevented from loading when new room initialized after a disconnect. --- HouseRules_Core/LifecycleDirector.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 037d72ea..db363a73 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -138,6 +138,7 @@ private static void CreatingGameState_OnJoinedRoom_Prefix() _gameId = GameHub.GameID; CoreMod.Logger.Warning($"New game with gameId {_gameId} started"); + _isReconnect = false; ActivateRuleset(); OnPreGameCreated(); } From 084b0a9fb94ab8bca6eed4e08de10b78b131a086 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Sun, 17 Jul 2022 04:36:56 -0400 Subject: [PATCH 13/34] Detect manual disconnects Prevent manual disconnects from being seen as forced disconnects. --- HouseRules_Core/LifecycleDirector.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index db363a73..ce356617 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -172,12 +172,10 @@ private static void PlayingGameState_OnMasterClientChanged_Prefix() } CoreMod.Logger.Warning($"<--- Resuming ruleset after disconnection from game {_gameId} --->"); + GameUI.ShowCameraMessage(RulesetActiveMessage(), 10f); ActivateRuleset(); OnPreGameCreated(); OnPostGameCreated(); - WelcomeMessageDurationSeconds = 10f; - ShowWelcomeMessage(); - WelcomeMessageDurationSeconds = 30f; _isReconnect = false; } @@ -221,9 +219,18 @@ private static void GameStateMachine_EndGame_Prefix() private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix() { - MelonLoader.MelonLogger.Warning($"<--- Disconnected from game {GameHub.GameID} --->"); - _isReconnect = true; - DeactivateRuleset(); + if (context == BoardgameActionOnLocalPlayerDisconnect.DisconnectContext.ReconnectState) + { + MelonLoader.MelonLogger.Warning($"<--- Disconnected from game {GameHub.GameID} --->"); + _isReconnect = true; + DeactivateRuleset(); + } + else + { + MelonLoader.MelonLogger.Warning($"<- Manually disconnected from game {GameHub.GameID} ->"); + _isReconnect = false; + DeactivateRuleset(); + } } From 87c99ad88565f8ab0d00b0aa856e62abc1970cbe Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Sun, 17 Jul 2022 05:34:22 -0400 Subject: [PATCH 14/34] Detect manual disconnects v2 Only try to reconnect mod on unintentional disconnects User CoreMod logger for consistency --- HouseRules_Core/LifecycleDirector.cs | 41 ++++++++++++++++++---------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index ce356617..e7576f18 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -5,6 +5,7 @@ using System.Reflection; using System.Text; using Boardgame; + using Boardgame.BoardgameActions; using Boardgame.Networking; using HarmonyLib; using HouseRules.Types; @@ -13,7 +14,7 @@ internal static class LifecycleDirector { private const string ModdedRoomPropertyKey = "modded"; - private static float WelcomeMessageDurationSeconds = 30f; + private const float WelcomeMessageDurationSeconds = 30f; private static GameContext _gameContext; private static bool _isCreatingGame; private static bool _isLoadingGame; @@ -145,14 +146,14 @@ private static void CreatingGameState_OnJoinedRoom_Prefix() private static void PlayingGameState_OnMasterClientChanged_Prefix() { - MelonLoader.MelonLogger.Warning("Master Client changed..."); + CoreMod.Logger.Warning("Master Client changed..."); if (!GameStateMachine.IsMasterClient) { - MelonLoader.MelonLogger.Warning("I'm *NOT* the Master Client yet..."); + CoreMod.Logger.Warning("I'm *NOT* the Master Client yet..."); return; } - MelonLoader.MelonLogger.Warning("I'm the Master Client now!"); + CoreMod.Logger.Warning("I'm the Master Client now!"); if (HR.SelectedRuleset == Ruleset.None) { return; @@ -165,7 +166,7 @@ private static void PlayingGameState_OnMasterClientChanged_Prefix() if (_gameId != GameHub.GameID) { - MelonLoader.MelonLogger.Warning($"Previous gameId {_gameId} doesn't match this gameId {GameHub.GameID}"); + CoreMod.Logger.Warning($"Previous gameId {_gameId} doesn't match this gameId {GameHub.GameID}"); _isReconnect = false; DeactivateRuleset(); return; @@ -217,23 +218,22 @@ private static void GameStateMachine_EndGame_Prefix() DeactivateRuleset(); } - private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix() + private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix(BoardgameActionOnLocalPlayerDisconnect.DisconnectContext context) { if (context == BoardgameActionOnLocalPlayerDisconnect.DisconnectContext.ReconnectState) { - MelonLoader.MelonLogger.Warning($"<--- Disconnected from game {GameHub.GameID} --->"); + CoreMod.Logger.Warning($"<--- Disconnected from game {GameHub.GameID} --->"); _isReconnect = true; DeactivateRuleset(); } else { - MelonLoader.MelonLogger.Warning($"<- Manually disconnected from game {GameHub.GameID} ->"); + CoreMod.Logger.Warning($"<- MANUALLY disconnected from game {GameHub.GameID} ->"); _isReconnect = false; DeactivateRuleset(); } } - /// /// Add properties to the room to indicate its modded nature. /// @@ -257,7 +257,6 @@ private static void AddModdedRoomProperties(RoomOptions roomOptions) newOptions[0] = ModdedRoomPropertyKey; roomOptions.CustomRoomPropertiesForLobby.CopyTo(newOptions, 1); roomOptions.CustomRoomPropertiesForLobby = newOptions; - roomOptions.CustomRoomProperties.Add(ModdedRoomPropertyKey, true); } @@ -282,7 +281,7 @@ private static void ActivateRuleset() IsRulesetActive = true; - CoreMod.Logger.Msg($"Activating ruleset: {HR.SelectedRuleset.Name} (with {HR.SelectedRuleset.Rules.Count} rules)"); + CoreMod.Logger.Warning($"Activating ruleset: {HR.SelectedRuleset.Name} (with {HR.SelectedRuleset.Rules.Count} rules)"); foreach (var rule in HR.SelectedRuleset.Rules) { try @@ -312,7 +311,11 @@ private static void DeactivateRuleset() { try { - if (!_isReconnect || (_isReconnect && !rule.Description.Contains("Piece "))) + if (_isReconnect && (rule.Description.StartsWith("Piece ") && !rule.Description.Contains(" is "))) + { + continue; + } + else { CoreMod.Logger.Msg($"Deactivating rule type: {rule.GetType()}"); rule.OnDeactivate(_gameContext); @@ -342,9 +345,13 @@ private static void OnPreGameCreated() { try { - CoreMod.Logger.Msg($"Calling OnPreGameCreated for rule type: {rule.GetType()}"); - if (!_isReconnect || (_isReconnect && (rule.Description != "LevelSequence is overridden" && !rule.Description.Contains("Piece ")))) + if (_isReconnect && (rule.Description.StartsWith("LevelSequence ") || (rule.Description.StartsWith("Piece ") && !rule.Description.Contains(" is ")))) { + continue; + } + else + { + CoreMod.Logger.Msg($"Calling OnPreGameCreated for rule type: {rule.GetType()}"); rule.OnPreGameCreated(_gameContext); } } @@ -372,7 +379,11 @@ private static void OnPostGameCreated() { try { - if (!_isReconnect || (_isReconnect && (rule.Description != "LevelSequence is overridden" && !rule.Description.Contains("Piece ")))) + if (_isReconnect && (rule.Description.StartsWith("LevelSequence ") || (rule.Description.StartsWith("Piece ") && !rule.Description.Contains(" is ")))) + { + continue; + } + else { CoreMod.Logger.Msg($"Calling OnPostGameCreated for rule type: {rule.GetType()}"); rule.OnPostGameCreated(_gameContext); From 46871107986c54ee95a5091c6d8db5244d7407db Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Sun, 17 Jul 2022 14:27:45 -0400 Subject: [PATCH 15/34] Set variable at game over Just in case --- HouseRules_Core/LifecycleDirector.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index e7576f18..7f0e38da 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -207,6 +207,7 @@ private static void GameStateMachine_GoToShoppingState_Postfix() private static void PostGameControllerBase_OnPlayAgainClicked_Postfix() { _gameId = GameHub.GameID; + _isReconnect = false; ActivateRuleset(); _isCreatingGame = true; OnPreGameCreated(); @@ -215,6 +216,7 @@ private static void PostGameControllerBase_OnPlayAgainClicked_Postfix() private static void GameStateMachine_EndGame_Prefix() { _gameId = 0; + _isReconnect = false; DeactivateRuleset(); } From 1389e918af0ba867cc7d4a09558c50a3672850a4 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Sun, 17 Jul 2022 15:19:44 -0400 Subject: [PATCH 16/34] Update functionality Fully deactivate ruleset if joining a different game Only report disconnects and set _isReconnect if you are hosting and ruleset is active --- HouseRules_Core/LifecycleDirector.cs | 53 ++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 7f0e38da..59f93cb9 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -41,6 +41,12 @@ internal static void Patch(Harmony harmony) .GetDeclaredMethod("OnJoinedRoom"), prefix: new HarmonyMethod(typeof(LifecycleDirector), nameof(CreatingGameState_OnJoinedRoom_Prefix))); + harmony.Patch( + original: AccessTools + .Inner(typeof(GameStateMachine), "JoiningGameState").GetTypeInfo() + .GetDeclaredMethod("OnJoinedRoom"), + prefix: new HarmonyMethod(typeof(LifecycleDirector), nameof(JoiningGameState_OnJoinedRoom_Prefix))); + harmony.Patch( original: AccessTools .Inner(typeof(GameStateMachine), "PlayingState").GetTypeInfo() @@ -144,16 +150,33 @@ private static void CreatingGameState_OnJoinedRoom_Prefix() OnPreGameCreated(); } + private static void JoiningGameState_OnJoinedRoom_Prefix() + { + if (HR.SelectedRuleset == Ruleset.None) + { + return; + } + + if (_gameContext.gameStateMachine.goBackToMenuState) + { + return; + } + + if (_isReconnect && _gameId != GameHub.GameID) + { + _isReconnect = false; + IsRulesetActive = true; + DeactivateRuleset(); + } + } + private static void PlayingGameState_OnMasterClientChanged_Prefix() { - CoreMod.Logger.Warning("Master Client changed..."); if (!GameStateMachine.IsMasterClient) { - CoreMod.Logger.Warning("I'm *NOT* the Master Client yet..."); return; } - CoreMod.Logger.Warning("I'm the Master Client now!"); if (HR.SelectedRuleset == Ruleset.None) { return; @@ -222,17 +245,25 @@ private static void GameStateMachine_EndGame_Prefix() private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix(BoardgameActionOnLocalPlayerDisconnect.DisconnectContext context) { - if (context == BoardgameActionOnLocalPlayerDisconnect.DisconnectContext.ReconnectState) + if (HR.SelectedRuleset == Ruleset.None) { - CoreMod.Logger.Warning($"<--- Disconnected from game {GameHub.GameID} --->"); - _isReconnect = true; - DeactivateRuleset(); + return; } - else + + if (GameStateMachine.IsMasterClient) { - CoreMod.Logger.Warning($"<- MANUALLY disconnected from game {GameHub.GameID} ->"); - _isReconnect = false; - DeactivateRuleset(); + if (context == BoardgameActionOnLocalPlayerDisconnect.DisconnectContext.ReconnectState) + { + CoreMod.Logger.Warning($"<--- Disconnected from game {GameHub.GameID} --->"); + _isReconnect = true; + DeactivateRuleset(); + } + else + { + CoreMod.Logger.Warning($"<- MANUALLY disconnected from game {GameHub.GameID} ->"); + _isReconnect = true; // Maybe to rejoin because Host character died? + DeactivateRuleset(); + } } } From 32497bcf667251e4a35523c5b1024b61beb10f3d Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Sun, 17 Jul 2022 20:25:22 -0400 Subject: [PATCH 17/34] Implemented Type IDisableOnReconnect Type prevents unload/load of rule on disconnect Added Type to (so far) discovered problematic rules --- HouseRules_Core/HouseRules_Core.csproj | 1 + HouseRules_Core/LifecycleDirector.cs | 18 +++--------------- HouseRules_Core/Types/IDisableOnReconnect.cs | 9 +++++++++ .../Rules/LevelSequenceOverriddenRule.cs | 2 +- .../Rules/PieceAbilityListOverriddenRule.cs | 2 +- .../Rules/PieceBehavioursListOverriddenRule.cs | 2 +- .../Rules/PieceImmunityListAdjustedRule.cs | 4 ++-- .../Rules/PiecePieceTypeListOverriddenRule.cs | 2 +- .../Rules/PieceUseWhenKilledOverriddenRule.cs | 2 +- 9 files changed, 20 insertions(+), 22 deletions(-) create mode 100644 HouseRules_Core/Types/IDisableOnReconnect.cs diff --git a/HouseRules_Core/HouseRules_Core.csproj b/HouseRules_Core/HouseRules_Core.csproj index bee9c864..72732aab 100644 --- a/HouseRules_Core/HouseRules_Core.csproj +++ b/HouseRules_Core/HouseRules_Core.csproj @@ -68,6 +68,7 @@ + diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 59f93cb9..75497020 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -344,11 +344,7 @@ private static void DeactivateRuleset() { try { - if (_isReconnect && (rule.Description.StartsWith("Piece ") && !rule.Description.Contains(" is "))) - { - continue; - } - else + if (_isReconnect && !(rule is IDisableOnReconnect)) { CoreMod.Logger.Msg($"Deactivating rule type: {rule.GetType()}"); rule.OnDeactivate(_gameContext); @@ -378,11 +374,7 @@ private static void OnPreGameCreated() { try { - if (_isReconnect && (rule.Description.StartsWith("LevelSequence ") || (rule.Description.StartsWith("Piece ") && !rule.Description.Contains(" is ")))) - { - continue; - } - else + if (_isReconnect && !(rule is IDisableOnReconnect)) { CoreMod.Logger.Msg($"Calling OnPreGameCreated for rule type: {rule.GetType()}"); rule.OnPreGameCreated(_gameContext); @@ -412,11 +404,7 @@ private static void OnPostGameCreated() { try { - if (_isReconnect && (rule.Description.StartsWith("LevelSequence ") || (rule.Description.StartsWith("Piece ") && !rule.Description.Contains(" is ")))) - { - continue; - } - else + if (_isReconnect && !(rule is IDisableOnReconnect)) { CoreMod.Logger.Msg($"Calling OnPostGameCreated for rule type: {rule.GetType()}"); rule.OnPostGameCreated(_gameContext); diff --git a/HouseRules_Core/Types/IDisableOnReconnect.cs b/HouseRules_Core/Types/IDisableOnReconnect.cs new file mode 100644 index 00000000..4c82359c --- /dev/null +++ b/HouseRules_Core/Types/IDisableOnReconnect.cs @@ -0,0 +1,9 @@ +namespace HouseRules.Types +{ + /// + /// Represents a rule that is safe to apply in a multiplayer environment. + /// + public interface IDisableOnReconnect + { + } +} diff --git a/HouseRules_Essentials/Rules/LevelSequenceOverriddenRule.cs b/HouseRules_Essentials/Rules/LevelSequenceOverriddenRule.cs index c3abc8c6..5481b5ef 100644 --- a/HouseRules_Essentials/Rules/LevelSequenceOverriddenRule.cs +++ b/HouseRules_Essentials/Rules/LevelSequenceOverriddenRule.cs @@ -8,7 +8,7 @@ using HarmonyLib; using HouseRules.Types; - public sealed class LevelSequenceOverriddenRule : Rule, IConfigWritable>, IPatchable, IMultiplayerSafe + public sealed class LevelSequenceOverriddenRule : Rule, IConfigWritable>, IPatchable, IMultiplayerSafe, IDisableOnReconnect { public override string Description => "LevelSequence is overridden"; diff --git a/HouseRules_Essentials/Rules/PieceAbilityListOverriddenRule.cs b/HouseRules_Essentials/Rules/PieceAbilityListOverriddenRule.cs index 67f2e9ec..ea94c7da 100644 --- a/HouseRules_Essentials/Rules/PieceAbilityListOverriddenRule.cs +++ b/HouseRules_Essentials/Rules/PieceAbilityListOverriddenRule.cs @@ -9,7 +9,7 @@ using HouseRules.Types; public sealed class PieceAbilityListOverriddenRule : Rule, - IConfigWritable>>, IMultiplayerSafe + IConfigWritable>>, IMultiplayerSafe, IDisableOnReconnect { public override string Description => "Piece abilities are adjusted"; diff --git a/HouseRules_Essentials/Rules/PieceBehavioursListOverriddenRule.cs b/HouseRules_Essentials/Rules/PieceBehavioursListOverriddenRule.cs index 974f7c4b..fb6527b4 100644 --- a/HouseRules_Essentials/Rules/PieceBehavioursListOverriddenRule.cs +++ b/HouseRules_Essentials/Rules/PieceBehavioursListOverriddenRule.cs @@ -10,7 +10,7 @@ using Behaviour = DataKeys.Behaviour; public sealed class PieceBehavioursListOverriddenRule : Rule, - IConfigWritable>>, IMultiplayerSafe + IConfigWritable>>, IMultiplayerSafe, IDisableOnReconnect { public override string Description => "Piece behaviours are adjusted"; diff --git a/HouseRules_Essentials/Rules/PieceImmunityListAdjustedRule.cs b/HouseRules_Essentials/Rules/PieceImmunityListAdjustedRule.cs index 6c41c193..09498143 100644 --- a/HouseRules_Essentials/Rules/PieceImmunityListAdjustedRule.cs +++ b/HouseRules_Essentials/Rules/PieceImmunityListAdjustedRule.cs @@ -9,7 +9,7 @@ using HouseRules.Types; public sealed class PieceImmunityListAdjustedRule : Rule, - IConfigWritable>>, IMultiplayerSafe + IConfigWritable>>, IMultiplayerSafe, IDisableOnReconnect { public override string Description => "Piece immunities are adjusted"; @@ -21,7 +21,7 @@ public sealed class PieceImmunityListAdjustedRule : Rule, /// /// Initializes a new instance of the class. /// - /// Dict of piece name and List + /// Dict of piece name and List. /// Replaces original settings with new list. public PieceImmunityListAdjustedRule(Dictionary> adjustments) { diff --git a/HouseRules_Essentials/Rules/PiecePieceTypeListOverriddenRule.cs b/HouseRules_Essentials/Rules/PiecePieceTypeListOverriddenRule.cs index 8de4ba1b..624562aa 100644 --- a/HouseRules_Essentials/Rules/PiecePieceTypeListOverriddenRule.cs +++ b/HouseRules_Essentials/Rules/PiecePieceTypeListOverriddenRule.cs @@ -9,7 +9,7 @@ using HouseRules.Types; public sealed class PiecePieceTypeListOverriddenRule : Rule, - IConfigWritable>>, IMultiplayerSafe + IConfigWritable>>, IMultiplayerSafe, IDisableOnReconnect { public override string Description => "Piece piece types are adjusted"; diff --git a/HouseRules_Essentials/Rules/PieceUseWhenKilledOverriddenRule.cs b/HouseRules_Essentials/Rules/PieceUseWhenKilledOverriddenRule.cs index 913e4f3c..43a24b7c 100644 --- a/HouseRules_Essentials/Rules/PieceUseWhenKilledOverriddenRule.cs +++ b/HouseRules_Essentials/Rules/PieceUseWhenKilledOverriddenRule.cs @@ -9,7 +9,7 @@ using HouseRules.Types; public sealed class PieceUseWhenKilledOverriddenRule : Rule, - IConfigWritable>>, IMultiplayerSafe + IConfigWritable>>, IMultiplayerSafe, IDisableOnReconnect { public override string Description => "Piece UseWhenKilled lists are overridden"; From 15f3ac8ad51a7b6a60239ec1033a52ac3177b5c9 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Sun, 17 Jul 2022 21:00:48 -0400 Subject: [PATCH 18/34] Fix pregame and postgame loading Whoops! --- HouseRules_Core/LifecycleDirector.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 75497020..679d74cc 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -344,7 +344,11 @@ private static void DeactivateRuleset() { try { - if (_isReconnect && !(rule is IDisableOnReconnect)) + if (_isReconnect && rule is IDisableOnReconnect) + { + continue; + } + else { CoreMod.Logger.Msg($"Deactivating rule type: {rule.GetType()}"); rule.OnDeactivate(_gameContext); @@ -374,7 +378,11 @@ private static void OnPreGameCreated() { try { - if (_isReconnect && !(rule is IDisableOnReconnect)) + if (_isReconnect && rule is IDisableOnReconnect) + { + continue; + } + else { CoreMod.Logger.Msg($"Calling OnPreGameCreated for rule type: {rule.GetType()}"); rule.OnPreGameCreated(_gameContext); @@ -404,7 +412,11 @@ private static void OnPostGameCreated() { try { - if (_isReconnect && !(rule is IDisableOnReconnect)) + if (_isReconnect && rule is IDisableOnReconnect) + { + continue; + } + else { CoreMod.Logger.Msg($"Calling OnPostGameCreated for rule type: {rule.GetType()}"); rule.OnPostGameCreated(_gameContext); From ef194ae9a9b6a0a72257bc637b77c98fae40067c Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Mon, 18 Jul 2022 01:17:10 -0400 Subject: [PATCH 19/34] Fix OnPostGameCreated Skipping (might) not (be) necessary? Will test. --- HouseRules_Core/LifecycleDirector.cs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 679d74cc..8612448e 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -344,7 +344,8 @@ private static void DeactivateRuleset() { try { - if (_isReconnect && rule is IDisableOnReconnect) + var isDisabled = rule is IDisableOnReconnect; + if (_isReconnect && isDisabled) { continue; } @@ -378,7 +379,8 @@ private static void OnPreGameCreated() { try { - if (_isReconnect && rule is IDisableOnReconnect) + var isDisabled = rule is IDisableOnReconnect; + if (_isReconnect && isDisabled) { continue; } @@ -412,15 +414,8 @@ private static void OnPostGameCreated() { try { - if (_isReconnect && rule is IDisableOnReconnect) - { - continue; - } - else - { - CoreMod.Logger.Msg($"Calling OnPostGameCreated for rule type: {rule.GetType()}"); - rule.OnPostGameCreated(_gameContext); - } + CoreMod.Logger.Msg($"Calling OnPostGameCreated for rule type: {rule.GetType()}"); + rule.OnPostGameCreated(_gameContext); } catch (Exception e) { From 585d25ae42d9d003889bc36f2b3eabbbc359f7fe Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Mon, 18 Jul 2022 04:58:31 -0400 Subject: [PATCH 20/34] Trying again... Try skip deactivation and reactivation of non-working rules. Untested. PieceConfig wasn't working so added to IDisableOnReconnect list --- HouseRules_Core/LifecycleDirector.cs | 32 +++++++++++++++---- .../Rules/PieceConfigAdjustedRule.cs | 2 +- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 8612448e..b83b261f 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -196,7 +196,7 @@ private static void PlayingGameState_OnMasterClientChanged_Prefix() } CoreMod.Logger.Warning($"<--- Resuming ruleset after disconnection from game {_gameId} --->"); - GameUI.ShowCameraMessage(RulesetActiveMessage(), 10f); + GameUI.ShowCameraMessage("Reconnected: Mod and RuleSet are resuming...", 10f); ActivateRuleset(); OnPreGameCreated(); OnPostGameCreated(); @@ -261,7 +261,7 @@ private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix(Boardgam else { CoreMod.Logger.Warning($"<- MANUALLY disconnected from game {GameHub.GameID} ->"); - _isReconnect = true; // Maybe to rejoin because Host character died? + _isReconnect = true; // Change this to false once things are confirmed working... DeactivateRuleset(); } } @@ -319,8 +319,17 @@ private static void ActivateRuleset() { try { - CoreMod.Logger.Msg($"Activating rule type: {rule.GetType()}"); - rule.OnActivate(_gameContext); + var isDisabled = rule is IDisableOnReconnect; + if (_isReconnect && isDisabled) + { + CoreMod.Logger.Warning($"Skip activating rule type: {rule.GetType()}"); + continue; + } + else + { + CoreMod.Logger.Msg($"Activating rule type: {rule.GetType()}"); + rule.OnActivate(_gameContext); + } } catch (Exception e) { @@ -347,6 +356,7 @@ private static void DeactivateRuleset() var isDisabled = rule is IDisableOnReconnect; if (_isReconnect && isDisabled) { + CoreMod.Logger.Warning($"Skip deactivating rule type: {rule.GetType()}"); continue; } else @@ -382,6 +392,7 @@ private static void OnPreGameCreated() var isDisabled = rule is IDisableOnReconnect; if (_isReconnect && isDisabled) { + CoreMod.Logger.Warning($"Skip calling OnPreGameCreated for rule type: {rule.GetType()}"); continue; } else @@ -414,8 +425,17 @@ private static void OnPostGameCreated() { try { - CoreMod.Logger.Msg($"Calling OnPostGameCreated for rule type: {rule.GetType()}"); - rule.OnPostGameCreated(_gameContext); + var isDisabled = rule is IDisableOnReconnect; + if (_isReconnect && isDisabled) + { + CoreMod.Logger.Warning($"Skip calling OnPostGameCreated for rule type: {rule.GetType()}"); + continue; + } + else + { + CoreMod.Logger.Msg($"Calling OnPostGameCreated for rule type: {rule.GetType()}"); + rule.OnPostGameCreated(_gameContext); + } } catch (Exception e) { diff --git a/HouseRules_Essentials/Rules/PieceConfigAdjustedRule.cs b/HouseRules_Essentials/Rules/PieceConfigAdjustedRule.cs index 3f213d54..77059a29 100644 --- a/HouseRules_Essentials/Rules/PieceConfigAdjustedRule.cs +++ b/HouseRules_Essentials/Rules/PieceConfigAdjustedRule.cs @@ -9,7 +9,7 @@ using HouseRules.Types; public sealed class PieceConfigAdjustedRule : Rule, IConfigWritable>, - IMultiplayerSafe + IMultiplayerSafe, IDisableOnReconnect { public override string Description => "Piece configuration is adjusted"; From 948ccb6f4f3a395be64c9c720809998ed77d1c7c Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Mon, 18 Jul 2022 05:30:47 -0400 Subject: [PATCH 21/34] Cleanups n/a --- HouseRules_Core/LifecycleDirector.cs | 47 ++++++++++++++++++---------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index b83b261f..dd3aeae5 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -152,6 +152,11 @@ private static void CreatingGameState_OnJoinedRoom_Prefix() private static void JoiningGameState_OnJoinedRoom_Prefix() { + if (!_isReconnect) + { + return; + } + if (HR.SelectedRuleset == Ruleset.None) { return; @@ -162,8 +167,9 @@ private static void JoiningGameState_OnJoinedRoom_Prefix() return; } - if (_isReconnect && _gameId != GameHub.GameID) + if (_gameId != GameHub.GameID) { + CoreMod.Logger.Warning($"Previous disconnected gameId {_gameId} doesn't match this gameId {GameHub.GameID}"); _isReconnect = false; IsRulesetActive = true; DeactivateRuleset(); @@ -172,6 +178,11 @@ private static void JoiningGameState_OnJoinedRoom_Prefix() private static void PlayingGameState_OnMasterClientChanged_Prefix() { + if (!_isReconnect) + { + return; + } + if (!GameStateMachine.IsMasterClient) { return; @@ -189,14 +200,15 @@ private static void PlayingGameState_OnMasterClientChanged_Prefix() if (_gameId != GameHub.GameID) { - CoreMod.Logger.Warning($"Previous gameId {_gameId} doesn't match this gameId {GameHub.GameID}"); + CoreMod.Logger.Warning("This message should never be seen..."); _isReconnect = false; + IsRulesetActive = true; DeactivateRuleset(); return; } CoreMod.Logger.Warning($"<--- Resuming ruleset after disconnection from game {_gameId} --->"); - GameUI.ShowCameraMessage("Reconnected: Mod and RuleSet are resuming...", 10f); + GameUI.ShowCameraMessage("Reconnected as Host! Mod and RuleSet are resuming...", 10f); ActivateRuleset(); OnPreGameCreated(); OnPostGameCreated(); @@ -230,7 +242,6 @@ private static void GameStateMachine_GoToShoppingState_Postfix() private static void PostGameControllerBase_OnPlayAgainClicked_Postfix() { _gameId = GameHub.GameID; - _isReconnect = false; ActivateRuleset(); _isCreatingGame = true; OnPreGameCreated(); @@ -250,20 +261,22 @@ private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix(Boardgam return; } - if (GameStateMachine.IsMasterClient) + if (!GameStateMachine.IsMasterClient) { - if (context == BoardgameActionOnLocalPlayerDisconnect.DisconnectContext.ReconnectState) - { - CoreMod.Logger.Warning($"<--- Disconnected from game {GameHub.GameID} --->"); - _isReconnect = true; - DeactivateRuleset(); - } - else - { - CoreMod.Logger.Warning($"<- MANUALLY disconnected from game {GameHub.GameID} ->"); - _isReconnect = true; // Change this to false once things are confirmed working... - DeactivateRuleset(); - } + return; + } + + if (context == BoardgameActionOnLocalPlayerDisconnect.DisconnectContext.ReconnectState) + { + CoreMod.Logger.Warning($"<--- Disconnected from game {GameHub.GameID} --->"); + _isReconnect = true; + DeactivateRuleset(); + } + else + { + CoreMod.Logger.Warning($"<- MANUALLY disconnected from game {GameHub.GameID} ->"); + _isReconnect = true; // Change this to false once things are confirmed working... + DeactivateRuleset(); } } From c5b4dee432b17b84392176e7844f950118648955 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Mon, 18 Jul 2022 18:49:58 -0400 Subject: [PATCH 22/34] Fix problematic GameHub.GameID Use original game creation room code as reference instead. --- HouseRules_Core/LifecycleDirector.cs | 82 +++++++++++----------------- 1 file changed, 32 insertions(+), 50 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index dd3aeae5..16716d9e 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -9,6 +9,7 @@ using Boardgame.Networking; using HarmonyLib; using HouseRules.Types; + using Photon.Pun; using Photon.Realtime; internal static class LifecycleDirector @@ -18,13 +19,18 @@ internal static class LifecycleDirector private static GameContext _gameContext; private static bool _isCreatingGame; private static bool _isLoadingGame; - private static long _gameId; private static bool _isReconnect = false; + private static string roomCode; + private static string lastCode; internal static bool IsRulesetActive { get; private set; } internal static void Patch(Harmony harmony) { + harmony.Patch( + original: AccessTools.Method(typeof(GameStateMachine), "OnRoomJoined"), + postfix: new HarmonyMethod(typeof(LifecycleDirector), nameof(GameStateMachine_OnRoomJoined_Postfix))); + harmony.Patch( original: AccessTools.Method(typeof(GameStartup), "InitializeGame"), postfix: new HarmonyMethod(typeof(LifecycleDirector), nameof(GameStartup_InitializeGame_Postfix))); @@ -41,12 +47,6 @@ internal static void Patch(Harmony harmony) .GetDeclaredMethod("OnJoinedRoom"), prefix: new HarmonyMethod(typeof(LifecycleDirector), nameof(CreatingGameState_OnJoinedRoom_Prefix))); - harmony.Patch( - original: AccessTools - .Inner(typeof(GameStateMachine), "JoiningGameState").GetTypeInfo() - .GetDeclaredMethod("OnJoinedRoom"), - prefix: new HarmonyMethod(typeof(LifecycleDirector), nameof(JoiningGameState_OnJoinedRoom_Prefix))); - harmony.Patch( original: AccessTools .Inner(typeof(GameStateMachine), "PlayingState").GetTypeInfo() @@ -84,6 +84,22 @@ private static void GameStartup_InitializeGame_Postfix(GameStartup __instance) _gameContext = gameContext; } + private static void GameStateMachine_OnRoomJoined_Postfix() + { + if (!_isReconnect) + { + return; + } + + lastCode = PhotonNetwork.CurrentRoom.Name; + if (lastCode != roomCode) + { + CoreMod.Logger.Warning($"Room {lastCode} doesn't match original room {roomCode}. Deactivating ruleset reconnection!"); + _isReconnect = false; + DeactivateRuleset(); + } + } + private static void CreatingGameState_TryCreateRoom_Prefix() { if (HR.SelectedRuleset == Ruleset.None) @@ -143,39 +159,13 @@ private static void CreatingGameState_OnJoinedRoom_Prefix() var levelSequence = Traverse.Create(_gameContext.gameStateMachine).Field("levelSequence").Value; MotherbrainGlobalVars.CurrentConfig = levelSequence.gameConfig; - _gameId = GameHub.GameID; - CoreMod.Logger.Warning($"New game with gameId {_gameId} started"); _isReconnect = false; + roomCode = PhotonNetwork.CurrentRoom.Name; + CoreMod.Logger.Warning($"New game in room {roomCode} started"); ActivateRuleset(); OnPreGameCreated(); } - private static void JoiningGameState_OnJoinedRoom_Prefix() - { - if (!_isReconnect) - { - return; - } - - if (HR.SelectedRuleset == Ruleset.None) - { - return; - } - - if (_gameContext.gameStateMachine.goBackToMenuState) - { - return; - } - - if (_gameId != GameHub.GameID) - { - CoreMod.Logger.Warning($"Previous disconnected gameId {_gameId} doesn't match this gameId {GameHub.GameID}"); - _isReconnect = false; - IsRulesetActive = true; - DeactivateRuleset(); - } - } - private static void PlayingGameState_OnMasterClientChanged_Prefix() { if (!_isReconnect) @@ -198,17 +188,8 @@ private static void PlayingGameState_OnMasterClientChanged_Prefix() return; } - if (_gameId != GameHub.GameID) - { - CoreMod.Logger.Warning("This message should never be seen..."); - _isReconnect = false; - IsRulesetActive = true; - DeactivateRuleset(); - return; - } + CoreMod.Logger.Warning($"<--- Resuming ruleset after disconnection from room {roomCode} --->"); - CoreMod.Logger.Warning($"<--- Resuming ruleset after disconnection from game {_gameId} --->"); - GameUI.ShowCameraMessage("Reconnected as Host! Mod and RuleSet are resuming...", 10f); ActivateRuleset(); OnPreGameCreated(); OnPostGameCreated(); @@ -241,7 +222,6 @@ private static void GameStateMachine_GoToShoppingState_Postfix() private static void PostGameControllerBase_OnPlayAgainClicked_Postfix() { - _gameId = GameHub.GameID; ActivateRuleset(); _isCreatingGame = true; OnPreGameCreated(); @@ -249,7 +229,6 @@ private static void PostGameControllerBase_OnPlayAgainClicked_Postfix() private static void GameStateMachine_EndGame_Prefix() { - _gameId = 0; _isReconnect = false; DeactivateRuleset(); } @@ -268,13 +247,13 @@ private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix(Boardgam if (context == BoardgameActionOnLocalPlayerDisconnect.DisconnectContext.ReconnectState) { - CoreMod.Logger.Warning($"<--- Disconnected from game {GameHub.GameID} --->"); + CoreMod.Logger.Warning($"<--- Disconnected from room {roomCode} --->"); _isReconnect = true; DeactivateRuleset(); } else { - CoreMod.Logger.Warning($"<- MANUALLY disconnected from game {GameHub.GameID} ->"); + CoreMod.Logger.Warning($"<- MANUALLY disconnected from room {roomCode} ->"); _isReconnect = true; // Change this to false once things are confirmed working... DeactivateRuleset(); } @@ -359,7 +338,10 @@ private static void DeactivateRuleset() return; } - IsRulesetActive = false; + if (!_isReconnect) + { + IsRulesetActive = false; + } CoreMod.Logger.Msg($"Deactivating ruleset: {HR.SelectedRuleset.Name} (with {HR.SelectedRuleset.Rules.Count} rules)"); foreach (var rule in HR.SelectedRuleset.Rules) From 469cca5325e414e28f46ed11b834cbf727d5f1bc Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Mon, 18 Jul 2022 20:56:44 -0400 Subject: [PATCH 23/34] Fix active ruleset check Let ruleset load when active if reconnect possible Don't try to reconnect if quitting when game is ended --- HouseRules_Core/LifecycleDirector.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 16716d9e..14ed7a1d 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -22,6 +22,7 @@ internal static class LifecycleDirector private static bool _isReconnect = false; private static string roomCode; private static string lastCode; + private static bool gameOver = false; internal static bool IsRulesetActive { get; private set; } @@ -222,6 +223,7 @@ private static void GameStateMachine_GoToShoppingState_Postfix() private static void PostGameControllerBase_OnPlayAgainClicked_Postfix() { + gameOver = false; ActivateRuleset(); _isCreatingGame = true; OnPreGameCreated(); @@ -229,12 +231,19 @@ private static void PostGameControllerBase_OnPlayAgainClicked_Postfix() private static void GameStateMachine_EndGame_Prefix() { + gameOver = true; _isReconnect = false; DeactivateRuleset(); } private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix(BoardgameActionOnLocalPlayerDisconnect.DisconnectContext context) { + if (gameOver) + { + gameOver = false; + return; + } + if (HR.SelectedRuleset == Ruleset.None) { return; @@ -287,7 +296,7 @@ private static void AddModdedRoomProperties(RoomOptions roomOptions) private static void ActivateRuleset() { - if (IsRulesetActive) + if (IsRulesetActive && !_isReconnect) { CoreMod.Logger.Warning("Ruleset activation was attempted whilst a ruleset was already activated. This should not happen. Please report this to HouseRules developers."); return; From 7fba3a51ba17d813b2fb52ba7e9fb2062eca0028 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Mon, 18 Jul 2022 21:07:25 -0400 Subject: [PATCH 24/34] Fix summary for new Type n/a --- HouseRules_Core/Types/IDisableOnReconnect.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HouseRules_Core/Types/IDisableOnReconnect.cs b/HouseRules_Core/Types/IDisableOnReconnect.cs index 4c82359c..b714f086 100644 --- a/HouseRules_Core/Types/IDisableOnReconnect.cs +++ b/HouseRules_Core/Types/IDisableOnReconnect.cs @@ -1,7 +1,7 @@ namespace HouseRules.Types { /// - /// Represents a rule that is safe to apply in a multiplayer environment. + /// Represents a rule that is not safe to apply after a disconnect in a multiplayer environment. /// public interface IDisableOnReconnect { From 20b5b36b5ebdecf831e77621495ce0c9c4abf3b4 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Mon, 18 Jul 2022 21:10:14 -0400 Subject: [PATCH 25/34] Keep up to date with main n/a --- HouseRules_Essentials/Rulesets/BeatTheClockRuleset.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/HouseRules_Essentials/Rulesets/BeatTheClockRuleset.cs b/HouseRules_Essentials/Rulesets/BeatTheClockRuleset.cs index c51831c0..5e13ce1b 100644 --- a/HouseRules_Essentials/Rulesets/BeatTheClockRuleset.cs +++ b/HouseRules_Essentials/Rulesets/BeatTheClockRuleset.cs @@ -19,6 +19,7 @@ internal static Ruleset Create() new PieceConfigAdjustedRule.PieceProperty { Piece = BoardPieceId.HeroHunter, Property = "StartHealth", Value = 200 }, new PieceConfigAdjustedRule.PieceProperty { Piece = BoardPieceId.HeroRogue, Property = "StartHealth", Value = 200 }, new PieceConfigAdjustedRule.PieceProperty { Piece = BoardPieceId.HeroSorcerer, Property = "StartHealth", Value = 200 }, + new PieceConfigAdjustedRule.PieceProperty { Piece = BoardPieceId.HeroWarlock, Property = "StartHealth", Value = 200 }, }); var recyclingRule = new CardEnergyFromRecyclingMultipliedRule(5); var roundLimitRule = new RoundCountLimitedRule(15); From 90a3bd1ad0a793ebb04b8a8a17438a9d92aac211 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Tue, 19 Jul 2022 00:58:11 -0400 Subject: [PATCH 26/34] Remove previous commit Variable prevented reconnect after finished game and was unnecessary. --- HouseRules_Core/LifecycleDirector.cs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 14ed7a1d..3ac98140 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -22,7 +22,6 @@ internal static class LifecycleDirector private static bool _isReconnect = false; private static string roomCode; private static string lastCode; - private static bool gameOver = false; internal static bool IsRulesetActive { get; private set; } @@ -162,7 +161,7 @@ private static void CreatingGameState_OnJoinedRoom_Prefix() _isReconnect = false; roomCode = PhotonNetwork.CurrentRoom.Name; - CoreMod.Logger.Warning($"New game in room {roomCode} started"); + CoreMod.Logger.Msg($"New game in room {roomCode} started"); ActivateRuleset(); OnPreGameCreated(); } @@ -223,7 +222,6 @@ private static void GameStateMachine_GoToShoppingState_Postfix() private static void PostGameControllerBase_OnPlayAgainClicked_Postfix() { - gameOver = false; ActivateRuleset(); _isCreatingGame = true; OnPreGameCreated(); @@ -231,19 +229,12 @@ private static void PostGameControllerBase_OnPlayAgainClicked_Postfix() private static void GameStateMachine_EndGame_Prefix() { - gameOver = true; _isReconnect = false; DeactivateRuleset(); } private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix(BoardgameActionOnLocalPlayerDisconnect.DisconnectContext context) { - if (gameOver) - { - gameOver = false; - return; - } - if (HR.SelectedRuleset == Ruleset.None) { return; @@ -263,7 +254,7 @@ private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix(Boardgam else { CoreMod.Logger.Warning($"<- MANUALLY disconnected from room {roomCode} ->"); - _isReconnect = true; // Change this to false once things are confirmed working... + _isReconnect = false; // Change this to true only for testing purposes DeactivateRuleset(); } } From cd89eab0f1b5b0a27de0e0c2dc1df7a9e05dd0bd Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Tue, 19 Jul 2022 01:20:14 -0400 Subject: [PATCH 27/34] Unnecessary - already in main n/a --- HouseRules_Essentials/Rulesets/BeatTheClockRuleset.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/HouseRules_Essentials/Rulesets/BeatTheClockRuleset.cs b/HouseRules_Essentials/Rulesets/BeatTheClockRuleset.cs index 5e13ce1b..c51831c0 100644 --- a/HouseRules_Essentials/Rulesets/BeatTheClockRuleset.cs +++ b/HouseRules_Essentials/Rulesets/BeatTheClockRuleset.cs @@ -19,7 +19,6 @@ internal static Ruleset Create() new PieceConfigAdjustedRule.PieceProperty { Piece = BoardPieceId.HeroHunter, Property = "StartHealth", Value = 200 }, new PieceConfigAdjustedRule.PieceProperty { Piece = BoardPieceId.HeroRogue, Property = "StartHealth", Value = 200 }, new PieceConfigAdjustedRule.PieceProperty { Piece = BoardPieceId.HeroSorcerer, Property = "StartHealth", Value = 200 }, - new PieceConfigAdjustedRule.PieceProperty { Piece = BoardPieceId.HeroWarlock, Property = "StartHealth", Value = 200 }, }); var recyclingRule = new CardEnergyFromRecyclingMultipliedRule(5); var roundLimitRule = new RoundCountLimitedRule(15); From e1bbbd8e83377a5fbab740258f52476bb8180b3e Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Tue, 19 Jul 2022 12:33:02 -0400 Subject: [PATCH 28/34] Disable if Host chooses not to reconnect Untested --- HouseRules_Core/LifecycleDirector.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 3ac98140..18c725e4 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -76,6 +76,12 @@ internal static void Patch(Harmony harmony) prefix: new HarmonyMethod( typeof(LifecycleDirector), nameof(SerializableEventQueue_DisconnectLocalPlayer_Prefix))); + + harmony.Patch( + original: AccessTools + .Inner(typeof(GameStateMachine), "ReconnectState").GetTypeInfo() + .GetDeclaredMethod("OnClickLeaveGameAfterReconnect"), + postfix: new HarmonyMethod(typeof(LifecycleDirector), nameof(ReconnectState_OnClickLeaveGameAfterReconnect_Postfix))); } private static void GameStartup_InitializeGame_Postfix(GameStartup __instance) @@ -84,6 +90,14 @@ private static void GameStartup_InitializeGame_Postfix(GameStartup __instance) _gameContext = gameContext; } + private static void ReconnectState_OnClickLeaveGameAfterReconnect_Postfix() + { + // (UNTESTED): Host chose not to reconnect so deactivate all rules + _isReconnect = false; + DeactivateRuleset(); + CoreMod.Logger.Warning("Reconnect disabled by Host!"); + } + private static void GameStateMachine_OnRoomJoined_Postfix() { if (!_isReconnect) From 887ddfd75fa4f1043ac43167000c5dbb4a15d765 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Tue, 19 Jul 2022 21:57:01 -0400 Subject: [PATCH 29/34] Added new Type to another rule This rule also doesn't work when new players enter if not held in memory after reconnect. --- HouseRules_Essentials/Rules/StartCardsModifiedRule.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/HouseRules_Essentials/Rules/StartCardsModifiedRule.cs b/HouseRules_Essentials/Rules/StartCardsModifiedRule.cs index 9f952e24..0a24dac4 100644 --- a/HouseRules_Essentials/Rules/StartCardsModifiedRule.cs +++ b/HouseRules_Essentials/Rules/StartCardsModifiedRule.cs @@ -9,7 +9,8 @@ using HarmonyLib; using HouseRules.Types; - public sealed class StartCardsModifiedRule : Rule, IConfigWritable>>, IPatchable, IMultiplayerSafe + public sealed class StartCardsModifiedRule : Rule, IConfigWritable>>, + IPatchable, IMultiplayerSafe, IDisableOnReconnect { public override string Description => "Hero start cards are modified"; From b3ea715012c27a7a29bb4edcfe566bb5b65982e0 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Wed, 20 Jul 2022 15:36:55 -0400 Subject: [PATCH 30/34] Added DeactivateReconnect function Deactivates only reconnection rules as needed. --- HouseRules_Core/LifecycleDirector.cs | 50 ++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 18c725e4..bfb26429 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -92,9 +92,9 @@ private static void GameStartup_InitializeGame_Postfix(GameStartup __instance) private static void ReconnectState_OnClickLeaveGameAfterReconnect_Postfix() { - // (UNTESTED): Host chose not to reconnect so deactivate all rules + // (UNTESTED): Host chose not to reconnect so deactivate all reconnection rules _isReconnect = false; - DeactivateRuleset(); + DeactivateReconnect(); CoreMod.Logger.Warning("Reconnect disabled by Host!"); } @@ -108,9 +108,9 @@ private static void GameStateMachine_OnRoomJoined_Postfix() lastCode = PhotonNetwork.CurrentRoom.Name; if (lastCode != roomCode) { - CoreMod.Logger.Warning($"Room {lastCode} doesn't match original room {roomCode}. Deactivating ruleset reconnection!"); + CoreMod.Logger.Warning($"Room {lastCode} doesn't match original room {roomCode}. Deactivating reconnection rules!"); _isReconnect = false; - DeactivateRuleset(); + DeactivateReconnect(); } } @@ -173,7 +173,12 @@ private static void CreatingGameState_OnJoinedRoom_Prefix() var levelSequence = Traverse.Create(_gameContext.gameStateMachine).Field("levelSequence").Value; MotherbrainGlobalVars.CurrentConfig = levelSequence.gameConfig; - _isReconnect = false; + if (_isReconnect) + { + _isReconnect = false; + DeactivateReconnect(); + } + roomCode = PhotonNetwork.CurrentRoom.Name; CoreMod.Logger.Msg($"New game in room {roomCode} started"); ActivateRuleset(); @@ -243,8 +248,15 @@ private static void PostGameControllerBase_OnPlayAgainClicked_Postfix() private static void GameStateMachine_EndGame_Prefix() { - _isReconnect = false; - DeactivateRuleset(); + if (_isReconnect) + { + _isReconnect = false; + DeactivateReconnect(); + } + else + { + DeactivateRuleset(); + } } private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix(BoardgameActionOnLocalPlayerDisconnect.DisconnectContext context) @@ -382,6 +394,30 @@ private static void DeactivateRuleset() } } + private static void DeactivateReconnect() + { + IsRulesetActive = false; + + CoreMod.Logger.Msg($"Deactivating reconnection: {HR.SelectedRuleset.Name} (with {HR.SelectedRuleset.Rules.Count} rules)"); + foreach (var rule in HR.SelectedRuleset.Rules) + { + try + { + var isDisabled = rule is IDisableOnReconnect; + if (isDisabled) + { + CoreMod.Logger.Msg($"Deactivating reconnection for rule type: {rule.GetType()}"); + rule.OnDeactivate(_gameContext); + } + } + catch (Exception e) + { + // TODO(orendain): Consider rolling back or disable rule. + CoreMod.Logger.Warning($"Failed to deactivate reconnection for rule [{rule.GetType()}]: {e}"); + } + } + } + private static void OnPreGameCreated() { if (HR.SelectedRuleset == Ruleset.None) From 7cd639210401d95d9da655fb79822d6a12d71155 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Wed, 20 Jul 2022 16:57:06 -0400 Subject: [PATCH 31/34] Proper deactivation fix Only deactivate active reconnection rules when manually disconnecting. --- HouseRules_Core/LifecycleDirector.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index bfb26429..a0e812f3 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -280,8 +280,15 @@ private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix(Boardgam else { CoreMod.Logger.Warning($"<- MANUALLY disconnected from room {roomCode} ->"); - _isReconnect = false; // Change this to true only for testing purposes - DeactivateRuleset(); + if (_isReconnect) + { + _isReconnect = false; + DeactivateReconnect(); + } + else + { + DeactivateRuleset(); + } } } From cd53eb5e71023d5b7ab20e98a69f4e2388a95e96 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Fri, 29 Jul 2022 16:15:59 -0400 Subject: [PATCH 32/34] Code cleanup Changes some log warnings to messages and vice versa Consolidate some code --- HouseRules_Core/LifecycleDirector.cs | 38 +++++++--------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index a0e812f3..56c64181 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -92,10 +92,7 @@ private static void GameStartup_InitializeGame_Postfix(GameStartup __instance) private static void ReconnectState_OnClickLeaveGameAfterReconnect_Postfix() { - // (UNTESTED): Host chose not to reconnect so deactivate all reconnection rules - _isReconnect = false; DeactivateReconnect(); - CoreMod.Logger.Warning("Reconnect disabled by Host!"); } private static void GameStateMachine_OnRoomJoined_Postfix() @@ -109,7 +106,6 @@ private static void GameStateMachine_OnRoomJoined_Postfix() if (lastCode != roomCode) { CoreMod.Logger.Warning($"Room {lastCode} doesn't match original room {roomCode}. Deactivating reconnection rules!"); - _isReconnect = false; DeactivateReconnect(); } } @@ -175,7 +171,6 @@ private static void CreatingGameState_OnJoinedRoom_Prefix() if (_isReconnect) { - _isReconnect = false; DeactivateReconnect(); } @@ -248,15 +243,7 @@ private static void PostGameControllerBase_OnPlayAgainClicked_Postfix() private static void GameStateMachine_EndGame_Prefix() { - if (_isReconnect) - { - _isReconnect = false; - DeactivateReconnect(); - } - else - { - DeactivateRuleset(); - } + DeactivateRuleset(); } private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix(BoardgameActionOnLocalPlayerDisconnect.DisconnectContext context) @@ -279,16 +266,8 @@ private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix(Boardgam } else { - CoreMod.Logger.Warning($"<- MANUALLY disconnected from room {roomCode} ->"); - if (_isReconnect) - { - _isReconnect = false; - DeactivateReconnect(); - } - else - { - DeactivateRuleset(); - } + CoreMod.Logger.Msg($"<- MANUALLY disconnected from room {roomCode} ->"); + DeactivateReconnect(); } } @@ -347,7 +326,7 @@ private static void ActivateRuleset() var isDisabled = rule is IDisableOnReconnect; if (_isReconnect && isDisabled) { - CoreMod.Logger.Warning($"Skip activating rule type: {rule.GetType()}"); + CoreMod.Logger.Msg($"Skip activating rule type: {rule.GetType()}"); continue; } else @@ -384,7 +363,7 @@ private static void DeactivateRuleset() var isDisabled = rule is IDisableOnReconnect; if (_isReconnect && isDisabled) { - CoreMod.Logger.Warning($"Skip deactivating rule type: {rule.GetType()}"); + CoreMod.Logger.Msg($"Skip deactivating rule type: {rule.GetType()}"); continue; } else @@ -403,9 +382,10 @@ private static void DeactivateRuleset() private static void DeactivateReconnect() { + _isReconnect = false; IsRulesetActive = false; - CoreMod.Logger.Msg($"Deactivating reconnection: {HR.SelectedRuleset.Name} (with {HR.SelectedRuleset.Rules.Count} rules)"); + CoreMod.Logger.Warning($"Deactivating reconnection: {HR.SelectedRuleset.Name} (with {HR.SelectedRuleset.Rules.Count} rules)"); foreach (var rule in HR.SelectedRuleset.Rules) { try @@ -444,7 +424,7 @@ private static void OnPreGameCreated() var isDisabled = rule is IDisableOnReconnect; if (_isReconnect && isDisabled) { - CoreMod.Logger.Warning($"Skip calling OnPreGameCreated for rule type: {rule.GetType()}"); + CoreMod.Logger.Msg($"Skip OnPreGameCreated for rule type: {rule.GetType()}"); continue; } else @@ -480,7 +460,7 @@ private static void OnPostGameCreated() var isDisabled = rule is IDisableOnReconnect; if (_isReconnect && isDisabled) { - CoreMod.Logger.Warning($"Skip calling OnPostGameCreated for rule type: {rule.GetType()}"); + CoreMod.Logger.Msg($"Skip OnPostGameCreated for rule type: {rule.GetType()}"); continue; } else From a508d4ef71059525fb1160a223464a8cb547a4c0 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Mon, 19 Dec 2022 12:09:17 -0500 Subject: [PATCH 33/34] Allow ruleset re-selection Fixes not being able to change rulesets after selecting and playing one --- HouseRules_Core/HR.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/HouseRules_Core/HR.cs b/HouseRules_Core/HR.cs index 387ddaeb..9dc91779 100644 --- a/HouseRules_Core/HR.cs +++ b/HouseRules_Core/HR.cs @@ -18,7 +18,8 @@ public static void SelectRuleset(string ruleset) { if (IsRulesetActive) { - throw new InvalidOperationException("May not select a new ruleset while one is currently active."); + LifecycleDirector.DeactivateReconnect(); + // throw new InvalidOperationException("May not select a new ruleset while one is currently active."); } if (Ruleset.None.Name.Equals(ruleset, StringComparison.OrdinalIgnoreCase)) From bdc38c1abd05bcf2f9827bb3b9ee2577cc65cf55 Mon Sep 17 00:00:00 2001 From: The Gray Alien Date: Mon, 19 Dec 2022 12:29:17 -0500 Subject: [PATCH 34/34] Fixes for latest updates to main n/a --- HouseRules_Core/LifecycleDirector.cs | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/HouseRules_Core/LifecycleDirector.cs b/HouseRules_Core/LifecycleDirector.cs index 7706f31e..67865442 100644 --- a/HouseRules_Core/LifecycleDirector.cs +++ b/HouseRules_Core/LifecycleDirector.cs @@ -2,7 +2,6 @@ { using System; using System.Linq; - using System.Reflection; using System.Text; using Boardgame; using Boardgame.BoardgameActions; @@ -44,9 +43,7 @@ internal static void Patch(Harmony harmony) prefix: new HarmonyMethod(typeof(LifecycleDirector), nameof(CreatingGameState_OnJoinedRoom_Prefix))); harmony.Patch( - original: AccessTools - .Inner(typeof(GameStateMachine), "PlayingState").GetTypeInfo() - .GetDeclaredMethod("OnMasterClientChanged"), + original: AccessTools.Method(typeof(PlayingState), "OnMasterClientChanged"), prefix: new HarmonyMethod(typeof(LifecycleDirector), nameof(PlayingGameState_OnMasterClientChanged_Prefix))); harmony.Patch( @@ -59,9 +56,7 @@ internal static void Patch(Harmony harmony) harmony.Patch( original: AccessTools.Method(typeof(PostGameControllerBase), "OnPlayAgainClicked"), - postfix: new HarmonyMethod( - typeof(LifecycleDirector), - nameof(PostGameControllerBase_OnPlayAgainClicked_Postfix))); + postfix: new HarmonyMethod(typeof(LifecycleDirector), nameof(PostGameControllerBase_OnPlayAgainClicked_Postfix))); harmony.Patch( original: AccessTools.Method(typeof(GameStateMachine), "EndGame"), @@ -69,14 +64,10 @@ internal static void Patch(Harmony harmony) harmony.Patch( original: AccessTools.Method(typeof(SerializableEventQueue), "DisconnectLocalPlayer"), - prefix: new HarmonyMethod( - typeof(LifecycleDirector), - nameof(SerializableEventQueue_DisconnectLocalPlayer_Prefix))); + prefix: new HarmonyMethod(typeof(LifecycleDirector), nameof(SerializableEventQueue_DisconnectLocalPlayer_Prefix))); harmony.Patch( - original: AccessTools - .Inner(typeof(GameStateMachine), "ReconnectState").GetTypeInfo() - .GetDeclaredMethod("OnClickLeaveGameAfterReconnect"), + original: AccessTools.Method(typeof(ReconnectState), "OnClickLeaveGameAfterReconnect"), postfix: new HarmonyMethod(typeof(LifecycleDirector), nameof(ReconnectState_OnClickLeaveGameAfterReconnect_Postfix))); } @@ -263,14 +254,15 @@ private static void SerializableEventQueue_DisconnectLocalPlayer_Prefix(Boardgam if (context == BoardgameActionOnLocalPlayerDisconnect.DisconnectContext.ReconnectState) { - CoreMod.Logger.Warning($"<--- Disconnected from room {roomCode} --->"); + CoreMod.Logger.Warning($"<- Disconnected from room {roomCode} ->"); _isReconnect = true; DeactivateRuleset(); } else { CoreMod.Logger.Msg($"<- MANUALLY disconnected from room {roomCode} ->"); - DeactivateReconnect(); + _isReconnect = true; + DeactivateRuleset(); } } @@ -383,12 +375,13 @@ private static void DeactivateRuleset() } } - private static void DeactivateReconnect() + public static void DeactivateReconnect() { _isReconnect = false; IsRulesetActive = false; CoreMod.Logger.Warning($"Deactivating reconnection: {HR.SelectedRuleset.Name} (with {HR.SelectedRuleset.Rules.Count} rules)"); + foreach (var rule in HR.SelectedRuleset.Rules) { try