Files
crystall-punk-14/Content.Shared/Construction/ConstructionGraphEdge.cs
Visne 9b94d5c195 Added nullable to most Content.Shared files (#3238)
* Add nullable to some Content.Shared files.

* Use [NotNullWhen(true)]

* Undo adding now redundant !'s

* Forgot one

* Add a ton more nullable

* You can guess

* Fix some issues

* It actually compiles now

* Auto stash before merge of "null2" and "origin/master"

* I lied

* enable annotations -> enable

* Revert ActionBlockerSystem.cs to original

* Fix ActionBlockerSystem.cs

* More nullable

* Undo some added exclamation marks

* Fix issues

* Update Content.Shared/Maps/ContentTileDefinition.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Resolve some issues

* Remove unused method

* Fix more issues

* Fix more issues

* Fix more issues

* Fix more issues

* Fix issue, rollback SharedGhostComponent.cs

* Update submodule

* Fix issue, invert some if-statements to reduce nesting

* Revert RobustToolbox

* FIx things broken by merge

* Some fixes

- Replaced with string.Empty
- Remove some exclamation marks
- Revert file

* Some fixes

* Trivial #nullable enable

* Fix null ables

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
2021-02-27 14:12:09 +11:00

119 lines
3.9 KiB
C#

#nullable enable
using System;
using System.Collections.Generic;
using Content.Shared.Interfaces;
using Robust.Shared.IoC;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
using YamlDotNet.RepresentationModel;
namespace Content.Shared.Construction
{
[Serializable]
public class ConstructionGraphEdge : IExposeData
{
private List<ConstructionGraphStep> _steps = new();
private List<IEdgeCondition> _conditions = new();
private List<IGraphAction> _completed = new();
[ViewVariables]
public string Target { get; private set; } = string.Empty;
[ViewVariables]
public IReadOnlyList<IEdgeCondition> Conditions => _conditions;
[ViewVariables]
public IReadOnlyList<IGraphAction> Completed => _completed;
[ViewVariables]
public IReadOnlyList<ConstructionGraphStep> Steps => _steps;
void IExposeData.ExposeData(ObjectSerializer serializer)
{
InternalExposeData(serializer);
}
private void InternalExposeData(ObjectSerializer serializer)
{
var moduleManager = IoCManager.Resolve<IModuleManager>();
serializer.DataField(this, x => x.Target, "to", string.Empty);
if (!moduleManager.IsServerModule) return;
serializer.DataField(ref _conditions, "conditions", new List<IEdgeCondition>());
serializer.DataField(ref _completed, "completed", new List<IGraphAction>());
}
public void LoadFrom(YamlMappingNode mapping)
{
var serializer = YamlObjectSerializer.NewReader(mapping);
InternalExposeData(serializer);
if (!mapping.TryGetNode("steps", out YamlSequenceNode? stepsMapping)) return;
foreach (var yamlNode in stepsMapping)
{
var stepMapping = (YamlMappingNode) yamlNode;
_steps.Add(LoadStep(stepMapping));
}
}
public static ConstructionGraphStep LoadStep(YamlMappingNode mapping)
{
var stepSerializer = YamlObjectSerializer.NewReader(mapping);
if (mapping.TryGetNode("material", out _))
{
var material = new MaterialConstructionGraphStep();
material.ExposeData(stepSerializer);
return material;
}
if (mapping.TryGetNode("tool", out _))
{
var tool = new ToolConstructionGraphStep();
tool.ExposeData(stepSerializer);
return tool;
}
if (mapping.TryGetNode("prototype", out _))
{
var prototype = new PrototypeConstructionGraphStep();
prototype.ExposeData(stepSerializer);
return prototype;
}
if (mapping.TryGetNode("component", out _))
{
var component = new ComponentConstructionGraphStep();
component.ExposeData(stepSerializer);
return component;
}
if (mapping.TryGetNode("tag", out _))
{
var tags = new TagConstructionGraphStep();
tags.ExposeData(stepSerializer);
return tags;
}
if (mapping.TryGetNode("allTags", out _) || mapping.TryGetNode("anyTags", out _))
{
var tags = new MultipleTagsConstructionGraphStep();
tags.ExposeData(stepSerializer);
return tags;
}
if(mapping.TryGetNode("steps", out _))
{
var nested = new NestedConstructionGraphStep();
nested.ExposeData(stepSerializer);
nested.LoadFrom(mapping);
return nested;
}
throw new ArgumentException("Tried to convert invalid YAML node mapping to ConstructionGraphStep!");
}
}
}