108 lines
2.9 KiB
C#
Raw Normal View History

2021-12-23 17:07:29 -08:00
using QSB.Menus;
2021-12-22 22:44:31 -08:00
using QSB.Messaging;
using QSB.Utility;
using QuantumUNET.Transport;
2021-12-23 17:07:29 -08:00
using System;
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
{
2021-12-22 22:44:31 -08:00
// only to be sent from host
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 readonly Dictionary<int, bool> KnownSignals = new();
2021-12-05 14:06:43 +00:00
2021-12-25 18:57:20 -08:00
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
writer.Write(WarpedToTheEye);
writer.Write(SecondsRemainingOnWarp);
writer.Write(LaunchCodesGiven);
writer.Write(LoopCount);
writer.Write(KnownFrequencies.Length);
foreach (var item in KnownFrequencies)
{
writer.Write(item);
}
writer.Write(KnownSignals.Count);
foreach (var (key, value) in KnownSignals)
{
writer.Write(key);
writer.Write(value);
}
}
2021-12-05 14:06:43 +00:00
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
WarpedToTheEye = reader.ReadBoolean();
SecondsRemainingOnWarp = reader.ReadSingle();
2021-12-12 00:00:46 +00:00
LaunchCodesGiven = reader.ReadBoolean();
2021-12-05 14:06:43 +00:00
LoopCount = reader.ReadInt32();
var frequenciesLength = reader.ReadInt32();
var knownFrequencies = KnownFrequencies;
Array.Resize(ref knownFrequencies, frequenciesLength);
KnownFrequencies = knownFrequencies;
for (var i = 0; i < frequenciesLength; i++)
{
KnownFrequencies[i] = reader.ReadBoolean();
}
2021-12-05 23:35:33 +00:00
var signalsLength = reader.ReadInt32();
KnownSignals.Clear();
for (var i = 0; i < signalsLength; i++)
{
var key = reader.ReadInt32();
var value = reader.ReadBoolean();
KnownSignals.Add(key, value);
}
2021-12-05 14:06:43 +00:00
}
2021-12-22 22:44:31 -08:00
public GameStateMessage(uint toId)
{
To = toId;
var gameSave = StandaloneProfileManager.SharedInstance.currentProfileGameSave;
WarpedToTheEye = gameSave.warpedToTheEye;
SecondsRemainingOnWarp = gameSave.secondsRemainingOnWarp;
LaunchCodesGiven = PlayerData.KnowsLaunchCodes();
LoopCount = gameSave.loopCount;
KnownFrequencies = gameSave.knownFrequencies;
KnownSignals = gameSave.knownSignals;
}
public GameStateMessage() { }
public override void OnReceiveRemote()
{
if (QSBSceneManager.CurrentScene != OWScene.TitleScreen)
{
2021-12-24 21:42:14 -08:00
DebugLog.ToConsole($"Error - Tried to handle GameStateEvent when not in TitleScreen!", OWML.Common.MessageType.Error);
2021-12-22 22:44:31 -08:00
return;
}
PlayerData.ResetGame();
var gameSave = StandaloneProfileManager.SharedInstance.currentProfileGameSave;
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
}