]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Add basic map loader support for entity renaming (#15862)
authorLeon Friedrich <60421075+ElectroJr@users.noreply.github.com>
Mon, 1 May 2023 03:41:57 +0000 (15:41 +1200)
committerGitHub <noreply@github.com>
Mon, 1 May 2023 03:41:57 +0000 (13:41 +1000)
Content.IntegrationTests/Tests/Chemistry/DispenserTest.cs
Content.Server/Maps/MapMigrationSystem.cs [new file with mode: 0644]
Resources/Prototypes/Entities/Objects/Devices/Circuitboards/Machine/production.yml
Resources/Prototypes/Entities/Structures/Dispensers/chem.yml
Resources/migration.yml [new file with mode: 0644]

index 3978dfb1004bbd2bfe6e07d22a2daf5f805ab164..eebb3358dfe3dc761ac68a9ee5b39d8f176e0035 100644 (file)
@@ -15,7 +15,7 @@ public sealed class DispenserTest : InteractionTest
     [Test]
     public async Task InsertEjectBuiTest()
     {
-        await SpawnTarget("chem_dispenser");
+        await SpawnTarget("ChemDispenser");
         ToggleNeedPower();
 
         // Insert beaker
diff --git a/Content.Server/Maps/MapMigrationSystem.cs b/Content.Server/Maps/MapMigrationSystem.cs
new file mode 100644 (file)
index 0000000..ce11f15
--- /dev/null
@@ -0,0 +1,76 @@
+using System.Diagnostics.CodeAnalysis;
+using System.IO;
+using System.Linq;
+using Robust.Server.GameObjects;
+using Robust.Server.Maps;
+using Robust.Shared.ContentPack;
+using Robust.Shared.Prototypes;
+using Robust.Shared.Serialization.Markdown;
+using Robust.Shared.Serialization.Markdown.Mapping;
+using Robust.Shared.Serialization.Markdown.Value;
+using Robust.Shared.Utility;
+
+namespace Content.Server.Maps;
+
+/// <summary>
+///     Performs basic map migration operations by listening for engine <see cref="MapLoaderSystem"/> events.
+/// </summary>
+public sealed class MapMigrationSystem : EntitySystem
+{
+    [Dependency] private readonly IPrototypeManager _protoMan = default!;
+    [Dependency] private readonly IResourceManager _resMan = default!;
+
+    private const string MigrationFile = "/migration.yml";
+
+    public override void Initialize()
+    {
+        base.Initialize();
+        SubscribeLocalEvent<BeforeEntityReadEvent>(OnBeforeReadEvent);
+
+#if DEBUG
+        if (!TryReadFile(out var mappings))
+            return;
+
+        foreach (var node in mappings.Values)
+        {
+            var newId = ((ValueDataNode) node).Value;
+            if (!string.IsNullOrEmpty(newId) && newId != "null")
+                DebugTools.Assert(_protoMan.HasIndex<EntityPrototype>(newId));
+        }
+#endif
+    }
+
+    private bool TryReadFile([NotNullWhen(true)] out MappingDataNode? mappings)
+    {
+        mappings = null;
+        var path = new ResPath(MigrationFile);
+        if (!_resMan.TryContentFileRead(path, out var stream))
+            return false;
+
+        using var reader = new StreamReader(stream, EncodingHelpers.UTF8);
+        var documents = DataNodeParser.ParseYamlStream(reader).FirstOrDefault();
+
+        if (documents == null)
+            return false;
+
+        mappings = (MappingDataNode) documents.Root;
+        return true;
+    }
+
+    private void OnBeforeReadEvent(BeforeEntityReadEvent ev)
+    {
+        if (!TryReadFile(out var mappings))
+            return;
+
+        foreach (var (key, value) in mappings)
+        {
+            if (key is not ValueDataNode keyNode || value is not ValueDataNode valueNode)
+                continue;
+
+            if (string.IsNullOrWhiteSpace(valueNode.Value) || valueNode.Value == "null")
+                ev.DeletedPrototypes.Add(keyNode.Value);
+            else
+                ev.RenamedPrototypes.Add(keyNode.Value, valueNode.Value);
+        }
+    }
+}
index 33da740127b7ac5b7c965d92674597cb0e18c966..a2b8313d3a13767dcad65efa0806eb0fe1c3518c 100644 (file)
     - type: Sprite
       state: medical
     - type: MachineBoard
-      prototype: chem_dispenser
+      prototype: ChemDispenser
       requirements:
         Capacitor: 1
       materialRequirements:
index af3c39192d4834d716af5802b9db6b97a890c7c9..44babe2411a4398d50ff751a1e92ba3292fc1a28 100644 (file)
@@ -1,5 +1,5 @@
 - type: entity
-  id: chem_dispenser
+  id: ChemDispenser
   name: chemical dispenser
   parent: ReagentDispenserBase
   description: An industrial grade chemical dispenser with a sizeable chemical supply.
@@ -27,8 +27,8 @@
   - type: Machine
     board: ChemDispenserMachineCircuitboard
   - type: Wires
-    BoardName: "chem_dispenser"
-    LayoutId: chem_dispenser
+    BoardName: "ChemDispenser"
+    LayoutId: ChemDispenser
   - type: UpgradePowerDraw
     powerDrawMultiplier: 0.75
     scaling: Exponential
diff --git a/Resources/migration.yml b/Resources/migration.yml
new file mode 100644 (file)
index 0000000..99e6b3b
--- /dev/null
@@ -0,0 +1,11 @@
+# basic dictionary that maps old entity ids to new entity ids.
+# an empty or "null" string results in the entity getting deleted.
+
+# e.g., you can swap all walls with windows and delete all tables by adding lines like:
+#
+# Window: WallSolid
+# WallSolid: Window
+# Table: null
+
+chem_dispenser: ChemDispenser
+