This changeset is motivated by a simple concept - get rid of the MiniYaml.Clone and MiniYamlNode.Clone methods to avoid deep copying yaml trees during merging. MiniYaml becoming immutable allows the merge function to reuse existing yaml trees rather than cloning them, saving on memory and improving merge performance. On initial loading the YAML for all maps is processed, so this provides a small reduction in initial loading time. The rest of the changeset is dealing with the change in the exposed API surface. Some With* helper methods are introduced to allow creating new YAML from existing YAML. Areas of code that generated small amounts of YAML are able to transition directly to the immutable model without too much ceremony. Some use cases are far less ergonomic even with these helper methods and so a MiniYamlBuilder is introduced to retain mutable creation functionality. This allows those areas to continue to use the old mutable structures. The main users are the update rules and linting capabilities.
68 lines
2.4 KiB
C#
68 lines
2.4 KiB
C#
#region Copyright & License Information
|
|
/*
|
|
* Copyright (c) The OpenRA Developers and Contributors
|
|
* 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.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
using System.Linq;
|
|
using OpenRA.Graphics;
|
|
using OpenRA.Traits;
|
|
|
|
namespace OpenRA.Mods.Common.Traits
|
|
{
|
|
[TraitLocation(SystemActors.Player)]
|
|
public class GameSaveViewportManagerInfo : TraitInfo
|
|
{
|
|
public override object Create(ActorInitializer init) { return new GameSaveViewportManager(); }
|
|
}
|
|
|
|
public class GameSaveViewportManager : IWorldLoaded, IGameSaveTraitData
|
|
{
|
|
WorldRenderer worldRenderer;
|
|
|
|
void IWorldLoaded.WorldLoaded(World w, WorldRenderer wr) { worldRenderer = wr; }
|
|
|
|
List<MiniYamlNode> IGameSaveTraitData.IssueTraitData(Actor self)
|
|
{
|
|
// HACK: Store the viewport state for the skirmish observer on the first bot's trait
|
|
// TODO: This won't make sense for MP saves
|
|
var localPlayer = worldRenderer.World.LocalPlayer;
|
|
if ((localPlayer != null && localPlayer.PlayerActor != self) ||
|
|
(localPlayer == null && self.Owner != self.World.Players.FirstOrDefault(p => p.IsBot)))
|
|
return null;
|
|
|
|
var nodes = new List<MiniYamlNode>()
|
|
{
|
|
new MiniYamlNode("Viewport", FieldSaver.FormatValue(worldRenderer.Viewport.CenterPosition))
|
|
};
|
|
|
|
var renderPlayer = worldRenderer.World.RenderPlayer;
|
|
if (localPlayer == null && renderPlayer != null)
|
|
nodes.Add(new MiniYamlNode("RenderPlayer", FieldSaver.FormatValue(renderPlayer.PlayerActor.ActorID)));
|
|
|
|
return nodes;
|
|
}
|
|
|
|
void IGameSaveTraitData.ResolveTraitData(Actor self, ImmutableArray<MiniYamlNode> data)
|
|
{
|
|
var viewportNode = data.FirstOrDefault(n => n.Key == "Viewport");
|
|
if (viewportNode != null)
|
|
worldRenderer.Viewport.Center(FieldLoader.GetValue<WPos>("Viewport", viewportNode.Value.Value));
|
|
|
|
var renderPlayerNode = data.FirstOrDefault(n => n.Key == "RenderPlayer");
|
|
if (renderPlayerNode != null)
|
|
{
|
|
var renderPlayerActorID = FieldLoader.GetValue<uint>("RenderPlayer", renderPlayerNode.Value.Value);
|
|
worldRenderer.World.RenderPlayer = worldRenderer.World.GetActorById(renderPlayerActorID).Owner;
|
|
}
|
|
}
|
|
}
|
|
}
|