Map Loading (#1110)

This commit is contained in:
Acruid
2020-06-07 15:56:55 -07:00
committed by GitHub
parent d631048c1e
commit 12ea903c98
4 changed files with 57 additions and 43 deletions

View File

@@ -198,14 +198,22 @@ namespace Robust.Server.Console.Commands
// no loading null space
if (mapID == MapId.Nullspace)
{
shell.SendText(player, "You cannot load into map 0.");
return;
}
var mapManager = IoCManager.Resolve<IMapManager>();
if (mapManager.MapExists(mapID))
{
shell.SendText(player, $"Map {mapID} already exists.");
return;
}
// TODO: Parse path
IoCManager.Resolve<IMapLoader>().LoadMap(mapID, "Maps/Demo/DemoMap.yaml");
var mapPath = "Maps/Demo/DemoMap.yaml";
IoCManager.Resolve<IMapLoader>().LoadMap(mapID, mapPath);
shell.SendText(player, $"Map {mapID} has been loaded from {mapPath}.");
}
}

View File

@@ -132,6 +132,7 @@ namespace Robust.Server.Maps
/// <inheritdoc />
public void SaveMap(MapId mapId, string yamlPath)
{
Logger.InfoS("map", $"Saving map {mapId} to {yamlPath}");
var context = new MapContext(_mapManager, _tileDefinitionManager, _serverEntityManager, _pauseManager, _componentManager, _prototypeManager);
foreach (var grid in _mapManager.GetAllMapGrids(mapId))
{
@@ -153,6 +154,7 @@ namespace Robust.Server.Maps
stream.Save(new YamlMappingFix(new Emitter(writer)), false);
}
}
Logger.InfoS("map", "Save completed!");
}
/// <inheritdoc />
@@ -164,7 +166,7 @@ namespace Robust.Server.Maps
// try user
if (!_resMan.UserData.Exists(resPath))
{
Logger.InfoS("map", $"No user blueprint found: {resPath}");
Logger.InfoS("map", $"No user map found: {resPath}");
// fallback to content
if (_resMan.TryContentFileRead(resPath, out var contentReader))
@@ -173,7 +175,7 @@ namespace Robust.Server.Maps
}
else
{
Logger.ErrorS("map", $"No blueprint found: {resPath}");
Logger.ErrorS("map", $"No map found: {resPath}");
return;
}
}
@@ -189,11 +191,6 @@ namespace Robust.Server.Maps
var data = new MapData(reader);
if (data.GridCount != 1)
{
throw new InvalidDataException("Cannot instance map with multiple grids as blueprint.");
}
var context = new MapContext(_mapManager, _tileDefinitionManager, _serverEntityManager, _pauseManager, _componentManager, _prototypeManager, (YamlMappingNode)data.RootNode, mapId);
context.Deserialize();
@@ -280,6 +277,9 @@ namespace Robust.Server.Maps
// First we load map meta data like version.
ReadMetaSection();
// Create the new map.
AllocMap();
// Load grids.
ReadTileMapSection();
ReadGridSection();
@@ -454,6 +454,23 @@ namespace Robust.Server.Maps
}
}
private void AllocMap()
{
// Both blueprint and map deserialization use this,
// so we need to ensure the map exists (and the map entity)
// before allocating entities.
if (!_mapManager.MapExists(TargetMap))
{
_mapManager.CreateMap(TargetMap);
if (!MapIsPostInit)
{
_pauseManager.AddUninitializedMap(TargetMap);
}
}
}
private void AllocEntities()
{
var entities = RootNode.GetNode<YamlSequenceNode>("entities");

View File

@@ -278,6 +278,7 @@ namespace Robust.Shared.Map
MapCreated?.Invoke(this, new MapEventArgs(actualID));
var newDefaultGrid = CreateGrid(actualID, defaultGridID);
Logger.DebugS("map", $"Grid {newDefaultGrid.Index} is the default grid for map {actualID}");
_defaultGrids.Add(actualID, newDefaultGrid.Index);
return actualID;

View File

@@ -430,8 +430,7 @@ namespace Robust.Shared.GameObjects
}
}
internal static void LoadEntity(EntityPrototype prototype, Entity entity, IComponentFactory factory,
IEntityLoadContext context)
internal static void LoadEntity(EntityPrototype prototype, Entity entity, IComponentFactory factory, IEntityLoadContext context)
{
YamlObjectSerializer.Context defaultContext = null;
if (context == null)
@@ -443,20 +442,6 @@ namespace Robust.Shared.GameObjects
{
foreach (var (name, data) in prototype.Components)
{
var compRegistration = factory.GetRegistration(name);
Component component;
if (entity.TryGetComponent(compRegistration.Type, out var existingComp))
{
component = (Component) existingComp;
}
else
{
component = (Component) factory.GetComponent(name);
component.Owner = entity;
entity.AddComponent(component);
}
ObjectSerializer ser;
if (context != null)
{
@@ -468,33 +453,36 @@ namespace Robust.Shared.GameObjects
ser = YamlObjectSerializer.NewReader(data, defaultContext);
}
component.ExposeData(ser);
EnsureCompExistsAndDeserialize(entity, factory, name, ser);
}
}
if (context == null)
if (context != null)
{
return;
}
// This is for map loading.
// Components that have been ADDED NEW need to be handled too.
foreach (var name in context.GetExtraComponentTypes())
{
if ((prototype != null && prototype.Components.ContainsKey(name)) || name == "Transform" || name == "Metadata")
foreach (var name in context.GetExtraComponentTypes())
{
continue;
var ser = context.GetComponentSerializer(name, null);
EnsureCompExistsAndDeserialize(entity, factory, name, ser);
}
var component = (Component) factory.GetComponent(name);
component.Owner = entity;
var ser = context.GetComponentSerializer(name, null);
component.ExposeData(ser);
entity.AddComponent(component);
}
}
private static void EnsureCompExistsAndDeserialize(Entity entity, IComponentFactory factory, string compName, ObjectSerializer ser)
{
var compType = factory.GetRegistration(compName).Type;
if (!entity.TryGetComponent(compType, out var component))
{
var newComponent = (Component) factory.GetComponent(compName);
newComponent.Owner = entity;
entity.AddComponent(newComponent);
component = newComponent;
}
component.ExposeData(ser);
}
private void ReadComponent(YamlMappingNode mapping, IComponentFactory factory)
{
string type = mapping.GetNode("type").AsString();
@@ -532,7 +520,7 @@ namespace Robust.Shared.GameObjects
return $"EntityPrototype({ID})";
}
class PrototypeSerializationContext : YamlObjectSerializer.Context
private class PrototypeSerializationContext : YamlObjectSerializer.Context
{
readonly EntityPrototype prototype;