forked from rwmt/Multiplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Server Bootstrap: add client bootstrap configurator flow #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MhaWay
wants to merge
4
commits into
bootstrap-protocol-state-1.6
Choose a base branch
from
bootstrap-client-flow-1.6
base: bootstrap-protocol-state-1.6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| using Verse; | ||
|
|
||
| namespace Multiplayer.Client.Comp | ||
| { | ||
| /// <summary> | ||
| /// Runs during bootstrap to detect when the new game has fully entered Playing and a map exists. | ||
| /// Keeps the save trigger logic reliable even when the bootstrap window may not receive regular updates. | ||
| /// </summary> | ||
| public class BootstrapCoordinator : GameComponent | ||
| { | ||
| private int nextCheckTick; | ||
| private const int CheckIntervalTicks = 60; // ~1s | ||
|
|
||
| public BootstrapCoordinator(Game game) | ||
| { | ||
| } | ||
|
|
||
| public override void GameComponentTick() | ||
| { | ||
| base.GameComponentTick(); | ||
|
|
||
| var win = BootstrapConfiguratorWindow.Instance; | ||
| if (win == null) | ||
| return; | ||
|
|
||
| // Throttle checks | ||
| if (Find.TickManager != null && Find.TickManager.TicksGame < nextCheckTick) | ||
| return; | ||
|
|
||
| if (Find.TickManager != null) | ||
| nextCheckTick = Find.TickManager.TicksGame + CheckIntervalTicks; | ||
|
|
||
| win.BootstrapCoordinatorTick(); | ||
| } | ||
|
|
||
| public override void ExposeData() | ||
| { | ||
| base.ExposeData(); | ||
| Scribe_Values.Look(ref nextCheckTick, "mp_bootstrap_nextCheckTick", 0); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| using Multiplayer.Common; | ||
| using Multiplayer.Common.Networking.Packet; | ||
| using RimWorld; | ||
| using Verse; | ||
|
|
||
| namespace Multiplayer.Client; | ||
|
|
||
| /// <summary> | ||
| /// Client connection state used while configuring a bootstrap server. | ||
| /// The server is in ServerBootstrap and expects upload packets; the client must keep the connection alive | ||
| /// and handle bootstrap completion / disconnect packets. | ||
| /// </summary> | ||
| [PacketHandlerClass] | ||
| public class ClientBootstrapState(ConnectionBase connection) : ClientBaseState(connection) | ||
| { | ||
| [TypedPacketHandler] | ||
| public void HandleBootstrap(ServerBootstrapPacket packet) | ||
| { | ||
| // The server sends this again when entering ServerBootstrapState.StartState(). | ||
| // We already have the bootstrap info from ClientJoiningState; just ignore it. | ||
| } | ||
|
|
||
| [TypedPacketHandler] | ||
| public override void HandleDisconnected(ServerDisconnectPacket packet) | ||
| { | ||
| // If bootstrap completed successfully, show success message before closing the window | ||
| if (packet.reason == MpDisconnectReason.BootstrapCompleted) | ||
| { | ||
| OnMainThread.Enqueue(() => Messages.Message( | ||
| "Bootstrap configuration completed. The server will now shut down; please restart it manually to start normally.", | ||
| MessageTypeDefOf.PositiveEvent, false)); | ||
| } | ||
|
|
||
| // Close the bootstrap configurator window now that the process is complete | ||
| OnMainThread.Enqueue(() => | ||
| { | ||
| var window = Find.WindowStack.WindowOfType<BootstrapConfiguratorWindow>(); | ||
| if (window != null) | ||
| Find.WindowStack.TryRemove(window); | ||
| }); | ||
|
|
||
| // Let the base class handle the disconnect | ||
| base.HandleDisconnected(packet); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| using HarmonyLib; | ||
| using Verse; | ||
|
|
||
| namespace Multiplayer.Client | ||
| { | ||
| [HarmonyPatch(typeof(MapComponentUtility), nameof(MapComponentUtility.FinalizeInit))] | ||
| static class BootstrapMapInitPatch | ||
| { | ||
| static void Postfix(Map map) | ||
| { | ||
| if (BootstrapConfiguratorWindow.AwaitingBootstrapMapInit && | ||
| BootstrapConfiguratorWindow.Instance != null) | ||
| { | ||
| OnMainThread.Enqueue(() => | ||
| { | ||
| BootstrapConfiguratorWindow.Instance.OnBootstrapMapInitialized(); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using HarmonyLib; | ||
| using Verse; | ||
|
|
||
| namespace Multiplayer.Client | ||
| { | ||
| /// <summary> | ||
| /// Root_Play.Start is called when the game fully transitions into Playing. | ||
| /// This is a reliable signal for arming the bootstrap map init detection. | ||
| /// </summary> | ||
| [HarmonyPatch(typeof(Root_Play), nameof(Root_Play.Start))] | ||
| static class BootstrapRootPlayPatch | ||
| { | ||
| static void Postfix() | ||
| { | ||
| var inst = BootstrapConfiguratorWindow.Instance; | ||
| if (inst == null) | ||
| return; | ||
|
|
||
| inst.TryArmAwaitingBootstrapMapInit_FromRootPlay(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| using HarmonyLib; | ||
| using Verse; | ||
|
|
||
| namespace Multiplayer.Client | ||
| { | ||
| /// <summary> | ||
| /// Root_Play.Update runs through the whole transition from MapInitializing to Playing. | ||
| /// Arms the map-init trigger as soon as a map exists and ProgramState is Playing. | ||
| /// </summary> | ||
| [HarmonyPatch(typeof(Root_Play), nameof(Root_Play.Update))] | ||
| static class BootstrapRootPlayUpdatePatch | ||
| { | ||
| private static int nextCheckFrame; | ||
| private const int CheckEveryFrames = 10; | ||
|
|
||
| static void Postfix() | ||
| { | ||
| var win = BootstrapConfiguratorWindow.Instance; | ||
| if (win == null) | ||
| return; | ||
|
|
||
| if (UnityEngine.Time.frameCount < nextCheckFrame) | ||
| return; | ||
| nextCheckFrame = UnityEngine.Time.frameCount + CheckEveryFrames; | ||
|
|
||
| win.TryArmAwaitingBootstrapMapInit_FromRootPlayUpdate(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| using HarmonyLib; | ||
| using RimWorld; | ||
| using Verse; | ||
|
|
||
| namespace Multiplayer.Client | ||
| { | ||
| /// <summary> | ||
| /// When the vanilla flow finishes generating the new game, this fires once the map and pawns are ready. | ||
| /// Backup signal to kick the bootstrap save pipeline in case FinalizeInit was missed or delayed. | ||
| /// </summary> | ||
| [HarmonyPatch(typeof(GameComponentUtility), nameof(GameComponentUtility.StartedNewGame))] | ||
| public static class BootstrapStartedNewGamePatch | ||
| { | ||
| static void Postfix() | ||
| { | ||
| var window = BootstrapConfiguratorWindow.Instance; | ||
| if (window == null) | ||
| return; | ||
|
|
||
| BootstrapConfiguratorWindow.AwaitingBootstrapMapInit = true; | ||
|
|
||
| OnMainThread.Enqueue(() => | ||
| { | ||
| window.OnBootstrapMapInitialized(); | ||
| }); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
InvalidOperationExceptionfilter relies on exact, localized exception messages (only English/Italian are handled). On other locales the same LiteNetLib queue-race can still crash, and message matching is generally brittle. Consider centralizing this check (there is alreadyLiteNetManager.IsQueueEmptyRace) and/or using a more robust condition (e.g. catching the specific LiteNetLib call site) instead of comparing localized strings.