using Mirror; using OWML.Common; using QSB.Menus; using QSB.Messaging; using QSB.Utility; using System.Collections.Generic; using QSB.TimeSync.Patches; using UnityEngine; namespace QSB.SaveSync.Messages; /// /// always sent by host /// public class GameStateMessage : QSBMessage { private bool WarpedToTheEye; private float SecondsRemainingOnWarp; private bool LaunchCodesGiven; private int LoopCount; private bool[] KnownFrequencies; private Dictionary KnownSignals; private bool ReducedFrights; private bool IsLoopAfterStatue; private Quaternion StatueRotation; private Vector3 PlayerPosition; private Quaternion PlayerRotation; public GameStateMessage(uint toId) { To = toId; var gameSave = PlayerData._currentGameSave; WarpedToTheEye = gameSave.warpedToTheEye; SecondsRemainingOnWarp = gameSave.secondsRemainingOnWarp; LaunchCodesGiven = PlayerData.KnowsLaunchCodes(); LoopCount = gameSave.loopCount; KnownFrequencies = gameSave.knownFrequencies; KnownSignals = gameSave.knownSignals; ReducedFrights = PlayerData.GetReducedFrights(); IsLoopAfterStatue = TimeLoopPatches.IsLoopAfterStatue; StatueRotation = MemoryUplinkTrigger._savedStatueRotation; PlayerPosition = PlayerSpawner._localResetPos; PlayerRotation = PlayerSpawner._localResetRotation; } public override void Serialize(NetworkWriter writer) { base.Serialize(writer); writer.Write(WarpedToTheEye); writer.Write(SecondsRemainingOnWarp); writer.Write(LaunchCodesGiven); writer.Write(LoopCount); writer.Write(KnownFrequencies); writer.Write(KnownSignals.Count); foreach (var (name, discovered) in KnownSignals) { writer.Write(name); writer.Write(discovered); } writer.Write(ReducedFrights); writer.Write(IsLoopAfterStatue); writer.Write(StatueRotation); writer.Write(PlayerPosition); writer.Write(PlayerRotation); } public override void Deserialize(NetworkReader reader) { base.Deserialize(reader); WarpedToTheEye = reader.Read(); SecondsRemainingOnWarp = reader.Read(); LaunchCodesGiven = reader.Read(); LoopCount = reader.Read(); KnownFrequencies = reader.Read(); var signalsLength = reader.Read(); KnownSignals = new Dictionary(signalsLength); for (var i = 0; i < signalsLength; i++) { var key = reader.Read(); var value = reader.Read(); KnownSignals.Add(key, value); } ReducedFrights = reader.Read(); IsLoopAfterStatue = reader.Read(); StatueRotation = reader.ReadQuaternion(); PlayerPosition = reader.ReadVector3(); PlayerRotation = reader.ReadQuaternion(); } public override void OnReceiveRemote() { if (QSBSceneManager.CurrentScene != OWScene.TitleScreen) { DebugLog.ToConsole($"Error - Tried to handle GameStateEvent when not in TitleScreen!", MessageType.Error); return; } PlayerData.ResetGame(); var gameSave = PlayerData._currentGameSave; gameSave.loopCount = LoopCount; gameSave.knownFrequencies = KnownFrequencies; gameSave.knownSignals = KnownSignals; gameSave.warpedToTheEye = WarpedToTheEye; gameSave.secondsRemainingOnWarp = SecondsRemainingOnWarp; PlayerData.SetPersistentCondition("LAUNCH_CODES_GIVEN", LaunchCodesGiven); PlayerData._settingsSave.reducedFrights = ReducedFrights; TimeLoop._startTimeLoopOnReload = IsLoopAfterStatue; MemoryUplinkTrigger._savedStatueRotation = StatueRotation; PlayerSpawner._localResetPos = PlayerPosition; PlayerSpawner._localResetRotation = PlayerRotation; PlayerData.SaveCurrentGame(); MenuManager.Instance.LoadGame(WarpedToTheEye); } }