Implement game save/load backend.
This commit is contained in:
313
OpenRA.Game/Network/GameSave.cs
Normal file
313
OpenRA.Game/Network/GameSave.cs
Normal file
@@ -0,0 +1,313 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2019 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Server;
|
||||
|
||||
namespace OpenRA.Network
|
||||
{
|
||||
public class SlotClient
|
||||
{
|
||||
public readonly Color Color;
|
||||
public readonly string Faction;
|
||||
public readonly int SpawnPoint;
|
||||
public readonly int Team;
|
||||
public readonly string Slot;
|
||||
public readonly string Bot;
|
||||
public readonly bool IsAdmin;
|
||||
|
||||
public readonly string BotName;
|
||||
|
||||
public SlotClient() { }
|
||||
|
||||
public SlotClient(Session.Client client)
|
||||
{
|
||||
Color = client.Color;
|
||||
Faction = client.Faction;
|
||||
SpawnPoint = client.SpawnPoint;
|
||||
Team = client.Team;
|
||||
Slot = client.Slot;
|
||||
Bot = client.Bot;
|
||||
IsAdmin = client.IsAdmin;
|
||||
|
||||
if (client.Bot != null)
|
||||
BotName = client.Name;
|
||||
}
|
||||
|
||||
public void ApplyTo(Session.Client client)
|
||||
{
|
||||
client.Color = Color;
|
||||
client.Faction = Faction;
|
||||
client.SpawnPoint = SpawnPoint;
|
||||
client.Team = Team;
|
||||
client.Slot = Slot;
|
||||
client.Bot = Bot;
|
||||
client.IsAdmin = IsAdmin;
|
||||
|
||||
if (Bot != null)
|
||||
client.Name = BotName;
|
||||
}
|
||||
|
||||
public static SlotClient Deserialize(MiniYaml data)
|
||||
{
|
||||
return FieldLoader.Load<SlotClient>(data);
|
||||
}
|
||||
|
||||
public MiniYamlNode Serialize(string key)
|
||||
{
|
||||
return new MiniYamlNode("SlotClient@{0}".F(key), FieldSaver.Save(this));
|
||||
}
|
||||
}
|
||||
|
||||
public class GameSave
|
||||
{
|
||||
public const int EOFMarker = -2;
|
||||
public const int MetadataMarker = -1;
|
||||
public const int TraitDataMarker = -3;
|
||||
|
||||
readonly MemoryStream ordersStream = new MemoryStream();
|
||||
|
||||
// Loaded from file and updated during gameplay
|
||||
public int LastOrdersFrame { get; private set; }
|
||||
public int LastSyncFrame { get; private set; }
|
||||
byte[] lastSyncPacket = new byte[0];
|
||||
|
||||
// Loaded from file or set on game start
|
||||
public Session.Global GlobalSettings { get; private set; }
|
||||
public Dictionary<string, Session.Slot> Slots { get; private set; }
|
||||
public Dictionary<string, SlotClient> SlotClients { get; private set; }
|
||||
public Dictionary<int, MiniYaml> TraitData = new Dictionary<int, MiniYaml>();
|
||||
|
||||
// Set on game start
|
||||
int[] clientsBySlotIndex = { };
|
||||
int firstBotSlotIndex = -1;
|
||||
|
||||
public GameSave()
|
||||
{
|
||||
LastOrdersFrame = -1;
|
||||
Slots = new Dictionary<string, Session.Slot>();
|
||||
}
|
||||
|
||||
public GameSave(string filepath)
|
||||
{
|
||||
using (var rs = File.OpenRead(filepath))
|
||||
{
|
||||
rs.Seek(-12, SeekOrigin.End);
|
||||
var metadataOffset = rs.ReadInt32();
|
||||
var traitDataOffset = rs.ReadInt32();
|
||||
if (rs.ReadInt32() != EOFMarker)
|
||||
throw new InvalidDataException("Invalid orasav file");
|
||||
|
||||
rs.Seek(metadataOffset, SeekOrigin.Begin);
|
||||
if (rs.ReadInt32() != MetadataMarker)
|
||||
throw new InvalidDataException("Invalid orasav file");
|
||||
|
||||
LastOrdersFrame = rs.ReadInt32();
|
||||
LastSyncFrame = rs.ReadInt32();
|
||||
lastSyncPacket = rs.ReadBytes(5);
|
||||
|
||||
var globalSettings = MiniYaml.FromString(rs.ReadString(Encoding.UTF8, Connection.MaxOrderLength));
|
||||
GlobalSettings = Session.Global.Deserialize(globalSettings[0].Value);
|
||||
|
||||
var slots = MiniYaml.FromString(rs.ReadString(Encoding.UTF8, Connection.MaxOrderLength));
|
||||
Slots = new Dictionary<string, Session.Slot>();
|
||||
foreach (var s in slots)
|
||||
{
|
||||
var slot = Session.Slot.Deserialize(s.Value);
|
||||
Slots.Add(slot.PlayerReference, slot);
|
||||
}
|
||||
|
||||
var slotClients = MiniYaml.FromString(rs.ReadString(Encoding.UTF8, Connection.MaxOrderLength));
|
||||
SlotClients = new Dictionary<string, SlotClient>();
|
||||
foreach (var s in slotClients)
|
||||
{
|
||||
var slotClient = SlotClient.Deserialize(s.Value);
|
||||
SlotClients.Add(slotClient.Slot, slotClient);
|
||||
}
|
||||
|
||||
if (rs.Position != traitDataOffset || rs.ReadInt32() != TraitDataMarker)
|
||||
throw new InvalidDataException("Invalid orasav file");
|
||||
|
||||
var traitData = MiniYaml.FromString(rs.ReadString(Encoding.UTF8, Connection.MaxOrderLength));
|
||||
foreach (var td in traitData)
|
||||
TraitData.Add(int.Parse(td.Key), td.Value);
|
||||
|
||||
rs.Seek(0, SeekOrigin.Begin);
|
||||
ordersStream.Write(rs.ReadBytes(metadataOffset), 0, metadataOffset);
|
||||
}
|
||||
}
|
||||
|
||||
public void StartGame(Session lobbyInfo, MapPreview map)
|
||||
{
|
||||
// Game orders are mapped from a client index to the slot that they occupy
|
||||
// Orders from spectators are ignored, which is not a problem in practice
|
||||
// because all immediate orders are also ignored
|
||||
clientsBySlotIndex = lobbyInfo.Slots.Keys.Select(s =>
|
||||
{
|
||||
var client = lobbyInfo.ClientInSlot(s);
|
||||
return client != null ? client.Index : -1;
|
||||
}).ToArray();
|
||||
|
||||
// Perform a deep clone by round-tripping the data
|
||||
GlobalSettings = Session.Global.Deserialize(lobbyInfo.GlobalSettings.Serialize().Value);
|
||||
Slots = new Dictionary<string, Session.Slot>();
|
||||
SlotClients = new Dictionary<string, SlotClient>();
|
||||
foreach (var s in lobbyInfo.Slots)
|
||||
{
|
||||
Slots[s.Key] = Session.Slot.Deserialize(s.Value.Serialize().Value);
|
||||
|
||||
var playerReference = map.Players.Players[s.Value.PlayerReference];
|
||||
var client = lobbyInfo.ClientInSlot(s.Key);
|
||||
|
||||
// Only save the client state relevant to the game (faction, team, etc).
|
||||
// Admin and bot controller state is inherited and/or reassigned by the server at load time
|
||||
if (playerReference.Playable && client != null)
|
||||
{
|
||||
SlotClients[s.Key] = new SlotClient(client);
|
||||
|
||||
// See HACK comment in DispatchOrders about reassigning bot orders
|
||||
if (client.Bot != null && firstBotSlotIndex < 0)
|
||||
firstBotSlotIndex = clientsBySlotIndex.IndexOf(client.Index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void DispatchOrders(Connection conn, int frame, byte[] data)
|
||||
{
|
||||
// Sync packet - we only care about the last value
|
||||
if (data.Length > 0 && data[0] == 0x65 && frame > LastSyncFrame)
|
||||
{
|
||||
LastSyncFrame = frame;
|
||||
lastSyncPacket = data;
|
||||
}
|
||||
|
||||
if (frame <= LastOrdersFrame)
|
||||
return;
|
||||
|
||||
// Ignore immediate orders
|
||||
if (data.Length > 0 && data[0] == 0xFE)
|
||||
return;
|
||||
|
||||
var clientSlot = clientsBySlotIndex.IndexOf(conn.PlayerIndex);
|
||||
|
||||
// Handle orders that were sent by spectators
|
||||
if (clientSlot == -1)
|
||||
{
|
||||
// HACK: Assume that this is a bot order sent by its controller client
|
||||
// who is a spectator. The network data doesn't contain enough information
|
||||
// for us to confirm this, or to know which bot this is supposed to belong to...
|
||||
//
|
||||
// For skirmish games it is sufficient to map everything to the first bot,
|
||||
// because even if the bot choice is wrong, the bot-to-client remapping in ParseOrders
|
||||
// will give the right client as there is only one human client to choose from!
|
||||
// TODO: This will need to be fixed properly before implementing multiplayer saves
|
||||
clientSlot = firstBotSlotIndex;
|
||||
}
|
||||
|
||||
ordersStream.WriteArray(BitConverter.GetBytes(data.Length + 8));
|
||||
ordersStream.WriteArray(BitConverter.GetBytes(frame));
|
||||
ordersStream.WriteArray(BitConverter.GetBytes(clientSlot));
|
||||
ordersStream.WriteArray(data);
|
||||
LastOrdersFrame = frame;
|
||||
}
|
||||
|
||||
public void ParseOrders(Session lobbyInfo, Action<int, int, byte[]> packetFn)
|
||||
{
|
||||
// Send the trait data first to guarantee that it is available when needed
|
||||
foreach (var kv in TraitData)
|
||||
{
|
||||
var data = new List<MiniYamlNode>() { new MiniYamlNode(kv.Key.ToString(), kv.Value) }.WriteToString();
|
||||
packetFn(0, 0, new ServerOrder("SaveTraitData", data).Serialize());
|
||||
}
|
||||
|
||||
ordersStream.Seek(0, SeekOrigin.Begin);
|
||||
while (ordersStream.Position < ordersStream.Length)
|
||||
{
|
||||
var dataLength = ordersStream.ReadInt32() - 8;
|
||||
var frame = ordersStream.ReadInt32();
|
||||
var slot = ordersStream.ReadInt32();
|
||||
var data = ordersStream.ReadBytes(dataLength);
|
||||
|
||||
// Remap bot orders to their controller client
|
||||
var clientIndex = clientsBySlotIndex[slot];
|
||||
var client = lobbyInfo.ClientWithIndex(clientIndex);
|
||||
if (client.Bot != null)
|
||||
clientIndex = client.BotControllerClientIndex;
|
||||
|
||||
packetFn(frame, clientIndex, data);
|
||||
}
|
||||
|
||||
// Send sync hash to validate restore
|
||||
packetFn(LastSyncFrame, 0, lastSyncPacket);
|
||||
}
|
||||
|
||||
public void AddTraitData(int traitIndex, MiniYaml data)
|
||||
{
|
||||
TraitData[traitIndex] = data;
|
||||
}
|
||||
|
||||
public void Save(string path)
|
||||
{
|
||||
// File format:
|
||||
// - List of orders in network frame format
|
||||
// - Metadata start marker
|
||||
// - Last frame number containing orders (int32)
|
||||
// - Last frame number containing sync hash (int32)
|
||||
// - Last sync packet (5 x byte)
|
||||
// - Lobby global settings (yaml)
|
||||
// - Lobby slots (yaml)
|
||||
// - Lobby slot-client data (yaml)
|
||||
// - Trait data start marker
|
||||
// - Custom trait yaml
|
||||
// - File offset of metadata start marker
|
||||
// - File offset of custom trait data
|
||||
// - Metadata end marker
|
||||
var file = File.Create(path);
|
||||
|
||||
ordersStream.Seek(0, SeekOrigin.Begin);
|
||||
ordersStream.CopyTo(file);
|
||||
file.Write(BitConverter.GetBytes(MetadataMarker), 0, 4);
|
||||
file.Write(BitConverter.GetBytes(LastOrdersFrame), 0, 4);
|
||||
file.Write(BitConverter.GetBytes(LastSyncFrame), 0, 4);
|
||||
file.Write(lastSyncPacket, 0, 5);
|
||||
|
||||
var globalSettingsNodes = new List<MiniYamlNode>() { GlobalSettings.Serialize() };
|
||||
file.WriteString(Encoding.UTF8, globalSettingsNodes.WriteToString());
|
||||
|
||||
var slotNodes = Slots
|
||||
.Select(s => s.Value.Serialize())
|
||||
.ToList();
|
||||
file.WriteString(Encoding.UTF8, slotNodes.WriteToString());
|
||||
|
||||
var slotClientNodes = SlotClients
|
||||
.Select(s => s.Value.Serialize(s.Key))
|
||||
.ToList();
|
||||
file.WriteString(Encoding.UTF8, slotClientNodes.WriteToString());
|
||||
|
||||
var traitDataOffset = file.Length;
|
||||
file.Write(BitConverter.GetBytes(TraitDataMarker), 0, 4);
|
||||
|
||||
var traitDataNodes = TraitData
|
||||
.Select(kv => new MiniYamlNode(kv.Key.ToString(), kv.Value))
|
||||
.ToList();
|
||||
file.WriteString(Encoding.UTF8, traitDataNodes.WriteToString());
|
||||
|
||||
file.Write(BitConverter.GetBytes(ordersStream.Length), 0, 4);
|
||||
file.Write(BitConverter.GetBytes(traitDataOffset), 0, 4);
|
||||
file.Write(BitConverter.GetBytes(EOFMarker), 0, 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,9 @@ namespace OpenRA.Network
|
||||
public bool GameStarted { get { return NetFrameNumber != 0; } }
|
||||
public IConnection Connection { get; private set; }
|
||||
|
||||
internal int GameSaveLastFrame = -1;
|
||||
internal int GameSaveLastSyncFrame = -1;
|
||||
|
||||
List<Order> localOrders = new List<Order>();
|
||||
|
||||
List<ChatLine> chatCache = new List<ChatLine>();
|
||||
@@ -70,8 +73,10 @@ namespace OpenRA.Network
|
||||
generateSyncReport = !(Connection is ReplayConnection) && LobbyInfo.GlobalSettings.EnableSyncReports;
|
||||
|
||||
NetFrameNumber = 1;
|
||||
for (var i = NetFrameNumber; i <= FramesAhead; i++)
|
||||
Connection.Send(i, new List<byte[]>());
|
||||
|
||||
if (GameSaveLastFrame < 0)
|
||||
for (var i = NetFrameNumber; i <= FramesAhead; i++)
|
||||
Connection.Send(i, new List<byte[]>());
|
||||
}
|
||||
|
||||
public OrderManager(string host, int port, string password, IConnection conn)
|
||||
@@ -105,7 +110,7 @@ namespace OpenRA.Network
|
||||
public void TickImmediate()
|
||||
{
|
||||
var immediateOrders = localOrders.Where(o => o.IsImmediate).ToList();
|
||||
if (immediateOrders.Count != 0)
|
||||
if (immediateOrders.Count != 0 && GameSaveLastFrame < NetFrameNumber + FramesAhead)
|
||||
Connection.SendImmediate(immediateOrders.Select(o => o.Serialize()).ToList());
|
||||
localOrders.RemoveAll(o => o.IsImmediate);
|
||||
|
||||
@@ -178,13 +183,18 @@ namespace OpenRA.Network
|
||||
if (!IsReadyForNextFrame)
|
||||
throw new InvalidOperationException();
|
||||
|
||||
Connection.Send(NetFrameNumber + FramesAhead, localOrders.Select(o => o.Serialize()).ToList());
|
||||
if (GameSaveLastFrame < NetFrameNumber + FramesAhead)
|
||||
Connection.Send(NetFrameNumber + FramesAhead, localOrders.Select(o => o.Serialize()).ToList());
|
||||
|
||||
localOrders.Clear();
|
||||
|
||||
foreach (var order in frameData.OrdersForFrame(World, NetFrameNumber))
|
||||
UnitOrders.ProcessOrder(this, World, order.Client, order.Order);
|
||||
|
||||
Connection.SendSync(NetFrameNumber, OrderIO.SerializeSync(World.SyncHash()));
|
||||
if (NetFrameNumber + FramesAhead >= GameSaveLastSyncFrame)
|
||||
Connection.SendSync(NetFrameNumber, OrderIO.SerializeSync(World.SyncHash()));
|
||||
else
|
||||
Connection.SendSync(NetFrameNumber, OrderIO.SerializeSync(0));
|
||||
|
||||
if (generateSyncReport)
|
||||
using (new PerfSample("sync_report"))
|
||||
|
||||
@@ -202,6 +202,7 @@ namespace OpenRA.Network
|
||||
public bool EnableSingleplayer;
|
||||
public bool EnableSyncReports;
|
||||
public bool Dedicated;
|
||||
public bool GameSavesEnabled;
|
||||
|
||||
[FieldLoader.Ignore]
|
||||
public Dictionary<string, LobbyOptionState> LobbyOptions = new Dictionary<string, LobbyOptionState>();
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Primitives;
|
||||
@@ -114,11 +115,45 @@ namespace OpenRA.Network
|
||||
break;
|
||||
}
|
||||
|
||||
Game.AddChatLine(Color.White, ServerChatName, "The game has started.");
|
||||
if (!string.IsNullOrEmpty(order.TargetString))
|
||||
{
|
||||
var data = MiniYaml.FromString(order.TargetString);
|
||||
var saveLastOrdersFrame = data.FirstOrDefault(n => n.Key == "SaveLastOrdersFrame");
|
||||
if (saveLastOrdersFrame != null)
|
||||
orderManager.GameSaveLastFrame =
|
||||
FieldLoader.GetValue<int>("saveLastOrdersFrame", saveLastOrdersFrame.Value.Value);
|
||||
|
||||
var saveSyncFrame = data.FirstOrDefault(n => n.Key == "SaveSyncFrame");
|
||||
if (saveSyncFrame != null)
|
||||
orderManager.GameSaveLastSyncFrame =
|
||||
FieldLoader.GetValue<int>("SaveSyncFrame", saveSyncFrame.Value.Value);
|
||||
}
|
||||
else
|
||||
Game.AddChatLine(Color.White, ServerChatName, "The game has started.");
|
||||
|
||||
Game.StartGame(orderManager.LobbyInfo.GlobalSettings.Map, WorldType.Regular);
|
||||
break;
|
||||
}
|
||||
|
||||
case "SaveTraitData":
|
||||
{
|
||||
var data = MiniYaml.FromString(order.TargetString)[0];
|
||||
var traitIndex = int.Parse(data.Key);
|
||||
|
||||
if (world != null)
|
||||
world.AddGameSaveTraitData(traitIndex, data.Value);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case "GameSaved":
|
||||
if (!orderManager.World.IsReplay)
|
||||
Game.AddChatLine(Color.White, ServerChatName, "Game saved");
|
||||
|
||||
foreach (var nsr in orderManager.World.WorldActor.TraitsImplementing<INotifyGameSaved>())
|
||||
nsr.GameSaved(orderManager.World);
|
||||
break;
|
||||
|
||||
case "PauseGame":
|
||||
{
|
||||
var client = orderManager.LobbyInfo.ClientWithIndex(clientId);
|
||||
|
||||
Reference in New Issue
Block a user