Skip to content

Commit

Permalink
Try fixing Return to Lobby returning to wrong spawn point when Save a…
Browse files Browse the repository at this point in the history
…nd Return to Lobby is involved
  • Loading branch information
maddie480 committed Dec 20, 2022
1 parent 6c58112 commit 9f0740e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions UI/ReturnToLobbyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ReturnToLobbyHelper {
private static bool temporarySaveAllowedHolder;
private static string temporaryGymExitMapSIDHolder;
private static bool temporaryGymExitSaveAllowedHolder;
private static bool forceInitializeModSession;

internal static void Load() {
On.Celeste.OuiChapterPanel.StartRoutine += modChapterPanelStartRoutine;
Expand Down Expand Up @@ -110,6 +111,13 @@ private static IEnumerator modChapterPanelStartRoutine(On.Celeste.OuiChapterPane
temporarySaveAllowedHolder = false;
temporaryGymExitMapSIDHolder = null;
temporaryGymExitSaveAllowedHolder = false;
forceInitializeModSession = false;
}

if (forceInitializeModSession) {
// we want to initialize the session even when we selected "Continue", since we want to use the chapter panel settings
// instead of whatever we had in our session last time.
OnSessionCreated();
}
}

Expand All @@ -128,6 +136,7 @@ public static void OnSessionCreated() {
temporarySaveAllowedHolder = false;
temporaryGymExitMapSIDHolder = null;
temporaryGymExitSaveAllowedHolder = false;
forceInitializeModSession = false;

if (CollabModule.Instance.Session.LobbySID == null) {
Session session = SaveData.Instance.CurrentSession_Safe;
Expand Down Expand Up @@ -174,6 +183,8 @@ private static void onLevelExitConstructor(On.Celeste.LevelExit.orig_ctor orig,
temporaryRoomHolder = CollabModule.Instance.Session.LobbyRoom;
temporarySpawnPointHolder = new Vector2(CollabModule.Instance.Session.LobbySpawnPointX, CollabModule.Instance.Session.LobbySpawnPointY);
temporarySaveAllowedHolder = CollabModule.Instance.Session.SaveAndReturnToLobbyAllowed;
temporaryGymExitMapSIDHolder = CollabModule.Instance.Session.GymExitMapSID;
temporaryGymExitSaveAllowedHolder = CollabModule.Instance.Session.GymExitSaveAllowed;
}
if ((mode == LevelExit.Mode.GiveUp || mode == LevelExit.Mode.Completed) && CollabModule.Instance.Session.LobbySID != null) {
// be sure that Return to Map and such from a collab entry returns to the lobby, not to the collab entry...
Expand Down Expand Up @@ -220,6 +231,11 @@ private static void openReturnToLobbyConfirmMenu(Level level, int returnIndex) {
Dictionary<string, string> modSessions = new Dictionary<string, string>();
Dictionary<string, string> modSessionsBinary = new Dictionary<string, string>();
foreach (EverestModule mod in Everest.Modules) {
if (mod == CollabModule.Instance) {
// we do NOT want to mess with our own session!
continue;
}

if (mod.SaveDataAsync) {
// new save data API: session is serialized into a byte array.
byte[] sessionBinary = mod.SerializeSession(SaveData.Instance.FileSlot);
Expand Down Expand Up @@ -300,6 +316,8 @@ private static void onLevelLoaderConstructor(On.Celeste.LevelLoader.orig_ctor or
temporaryRoomHolder = CollabModule.Instance.Session.LobbyRoom;
temporarySpawnPointHolder = new Vector2(CollabModule.Instance.Session.LobbySpawnPointX, CollabModule.Instance.Session.LobbySpawnPointY);
temporarySaveAllowedHolder = CollabModule.Instance.Session.SaveAndReturnToLobbyAllowed;
temporaryGymExitMapSIDHolder = CollabModule.Instance.Session.GymExitMapSID;
temporaryGymExitSaveAllowedHolder = CollabModule.Instance.Session.GymExitSaveAllowed;
}

orig(self, session, startPosition);
Expand Down Expand Up @@ -344,6 +362,12 @@ private static bool loadModSessions(Session session) {

// restore all mod sessions we can restore.
foreach (EverestModule mod in Everest.Modules) {
if (mod == CollabModule.Instance) {
// it is too early to load the Collab Utils mod session, but we should do that when the start routine is over.
forceInitializeModSession = true;
continue;
}

if (mod.SaveDataAsync && sessionsBinary != null && sessionsBinary.TryGetValue(mod.Metadata.Name, out string savedSessionBinary)) {
// new save data API: session is deserialized by passing the byte array as is.
mod.DeserializeSession(SaveData.Instance.FileSlot, Convert.FromBase64String(savedSessionBinary));
Expand Down Expand Up @@ -374,6 +398,9 @@ private static void onLevelLoaderStartLevel(On.Celeste.LevelLoader.orig_StartLev
temporaryRoomHolder = null;
temporarySpawnPointHolder = Vector2.Zero;
temporarySaveAllowedHolder = false;
temporaryGymExitMapSIDHolder = null;
temporaryGymExitSaveAllowedHolder = false;
forceInitializeModSession = false;

orig(self);
}
Expand Down
2 changes: 1 addition & 1 deletion everest.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- Name: CollabUtils2
Version: 1.7.1
Version: 1.7.2
DLL: bin/Release/net452/CollabUtils2.dll
Dependencies:
- Name: Everest
Expand Down

0 comments on commit 9f0740e

Please sign in to comment.