97 lines
2.5 KiB
C#
Raw Normal View History

using Mirror;
using OWML.Common;
2021-12-25 22:53:36 -08:00
using QSB.Menus;
2021-12-22 22:44:31 -08:00
using QSB.Messaging;
using QSB.Utility;
2021-12-23 17:07:29 -08:00
using System.Collections.Generic;
2021-12-05 14:06:43 +00:00
2021-12-23 02:17:56 -08:00
namespace QSB.SaveSync.Messages
2021-12-05 14:06:43 +00:00
{
/// <summary>
/// always sent by host
/// </summary>
2021-12-22 22:44:31 -08:00
internal class GameStateMessage : QSBMessage
2021-12-05 14:06:43 +00:00
{
2021-12-22 22:44:31 -08:00
private bool WarpedToTheEye;
private float SecondsRemainingOnWarp;
private bool LaunchCodesGiven;
private int LoopCount;
private bool[] KnownFrequencies;
private Dictionary<int, bool> KnownSignals;
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;
}
2021-12-05 14:06:43 +00:00
2022-01-08 11:41:55 +00:00
public override void Serialize(NetworkWriter writer)
2021-12-25 18:57:20 -08:00
{
base.Serialize(writer);
writer.Write(WarpedToTheEye);
writer.Write(SecondsRemainingOnWarp);
writer.Write(LaunchCodesGiven);
writer.Write(LoopCount);
writer.Write(KnownFrequencies);
2021-12-25 18:57:20 -08:00
writer.Write(KnownSignals.Count);
2022-01-08 11:41:55 +00:00
foreach (var (name, discovered) in KnownSignals)
2021-12-25 18:57:20 -08:00
{
2022-01-08 11:41:55 +00:00
writer.Write(name);
writer.Write(discovered);
2021-12-25 18:57:20 -08:00
}
}
public override void Deserialize(NetworkReader reader)
2021-12-05 14:06:43 +00:00
{
base.Deserialize(reader);
WarpedToTheEye = reader.Read<bool>();
SecondsRemainingOnWarp = reader.Read<float>();
LaunchCodesGiven = reader.Read<bool>();
LoopCount = reader.Read<int>();
2021-12-05 14:06:43 +00:00
KnownFrequencies = reader.Read<bool[]>();
2021-12-05 23:35:33 +00:00
var signalsLength = reader.Read<int>();
KnownSignals = new Dictionary<int, bool>(signalsLength);
2021-12-05 23:35:33 +00:00
for (var i = 0; i < signalsLength; i++)
{
var key = reader.Read<int>();
var value = reader.Read<bool>();
2021-12-05 23:35:33 +00:00
KnownSignals.Add(key, value);
}
2021-12-05 14:06:43 +00:00
}
2021-12-22 22:44:31 -08:00
public override void OnReceiveRemote()
{
if (QSBSceneManager.CurrentScene != OWScene.TitleScreen)
{
2021-12-25 22:53:36 -08:00
DebugLog.ToConsole($"Error - Tried to handle GameStateEvent when not in TitleScreen!", MessageType.Error);
2021-12-22 22:44:31 -08:00
return;
}
PlayerData.ResetGame();
var gameSave = PlayerData._currentGameSave;
2021-12-22 22:44:31 -08:00
gameSave.loopCount = LoopCount;
gameSave.knownFrequencies = KnownFrequencies;
gameSave.knownSignals = KnownSignals;
gameSave.warpedToTheEye = WarpedToTheEye;
gameSave.secondsRemainingOnWarp = SecondsRemainingOnWarp;
PlayerData.SetPersistentCondition("LAUNCH_CODES_GIVEN", LaunchCodesGiven);
PlayerData.SaveCurrentGame();
MenuManager.Instance.JoinGame(WarpedToTheEye);
}
2021-12-05 14:06:43 +00:00
}
2021-12-22 22:44:31 -08:00
}