mirror of
https://github.com/DeltaV-Station/Delta-v.git
synced 2026-09-29 23:57:29 +00:00
fine-grained mapping prevention (#3076)
* add all code for mapping categories * add some mapping categories * add categories to a few things * ignore prototypes on client * gaming * shoukou ops * chibi ops * cc real * sid meiers pirates! * old ai sat * pen * make test errors better, listening post * fix remaining tests, remove turret toolbox from a wreck * john gaming * reduce diff on stamps file * update test * :trollface: * :trollface: * :trollface: * :trollface: * :trollface: * aaaa Signed-off-by: deltanedas <39013340+deltanedas@users.noreply.github.com> * resave centcomm * orphan is good?? * gnu world order * ok good * update test to skip old files, not merge for maps --------- Signed-off-by: deltanedas <39013340+deltanedas@users.noreply.github.com> Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
using Content.Server._DV.Mapping;
|
||||
using Robust.Shared.ContentPack;
|
||||
using Robust.Shared.EntitySerialization;
|
||||
using Robust.Shared.EntitySerialization.Systems;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.Markdown.Mapping;
|
||||
using Robust.Shared.Serialization.Markdown.Value;
|
||||
using Robust.Shared.Utility;
|
||||
using System.Linq;
|
||||
|
||||
namespace Content.IntegrationTests.Tests._DV;
|
||||
|
||||
/// <summary>
|
||||
/// Checks that every mapped entity with <see cref="MappingCategoriesComponent"/> is allowed to be mapped.
|
||||
/// </summary>
|
||||
public sealed class MappingCategoryTest
|
||||
{
|
||||
private const string MapsPath = "/Maps";
|
||||
// dev map doesn't matter and don't want to change it
|
||||
private const string TestMapsPath = "/Maps/Test/";
|
||||
|
||||
[Test]
|
||||
public async Task NonGameMapsLoadableTest()
|
||||
{
|
||||
await using var pair = await PoolManager.GetServerClient();
|
||||
var server = pair.Server;
|
||||
var entMan = server.ResolveDependency<IEntityManager>();
|
||||
var resMan = server.ResolveDependency<IResourceManager>();
|
||||
var mapLoader = entMan.System<MapLoaderSystem>();
|
||||
var catSys = entMan.System<MappingCategoriesSystem>(); // meow
|
||||
var mapSys = entMan.System<SharedMapSystem>();
|
||||
var sawmill = server.ResolveDependency<ILogManager>().GetSawmill("mapping_categories");
|
||||
|
||||
await server.WaitPost(() =>
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
var mapFolder = new ResPath(MapsPath);
|
||||
foreach (var map in resMan.ContentFindFiles(mapFolder))
|
||||
{
|
||||
if (map.Extension != "yml" || map.Filename.StartsWith(".", StringComparison.Ordinal))
|
||||
continue;
|
||||
|
||||
var rootedPath = map.ToRootedPath().ToString();
|
||||
if (rootedPath.StartsWith(TestMapsPath, StringComparison.Ordinal))
|
||||
continue;
|
||||
|
||||
if (GetCategory(map, mapLoader) is not {} category)
|
||||
{
|
||||
sawmill.Warning($"Map {map} is missing a category, skipping it.");
|
||||
continue;
|
||||
}
|
||||
|
||||
var mapUid = mapSys.CreateMap(out var mapId);
|
||||
var opts = new MapLoadOptions
|
||||
{
|
||||
MergeMap = category == FileCategory.Map
|
||||
? null // don't try to reparent maps
|
||||
: mapId // needed or else grids will be de-orphaned which is bad
|
||||
};
|
||||
Assert.That(mapLoader.TryLoadGeneric(map, out var maps, out _, opts), $"Failed to load map {rootedPath}");
|
||||
maps.Add(mapUid);
|
||||
|
||||
var allowed = catSys.GetAllowedCategories(rootedPath);
|
||||
var query = entMan.EntityQueryEnumerator<MappingCategoriesComponent>();
|
||||
while (query.MoveNext(out var uid, out var comp))
|
||||
{
|
||||
var ent = (uid, comp);
|
||||
Assert.That(catSys.CanMap(ent, allowed), $"Entity {entMan.ToPrettyString(uid)} cannot be mapped on {rootedPath}");
|
||||
}
|
||||
|
||||
foreach (var uid in maps)
|
||||
{
|
||||
entMan.DeleteEntity(uid);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
await server.WaitRunTicks(1);
|
||||
|
||||
await pair.CleanReturnAsync();
|
||||
}
|
||||
|
||||
// me when engine doesnt have this
|
||||
private FileCategory? GetCategory(ResPath path, MapLoaderSystem mapLoader)
|
||||
{
|
||||
Assert.That(mapLoader.TryReadFile(path, out var data), $"Failed to read map file {path}");
|
||||
var meta = data.Get<MappingDataNode>("meta");
|
||||
if (!meta.TryGet<ValueDataNode>("category", out var node))
|
||||
return null;
|
||||
|
||||
var valid = Enum.TryParse<FileCategory>(node.Value, out var cat);
|
||||
Assert.That(valid, $"Category for {path} is invalid");
|
||||
return cat;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user