Storage Standardization [Take 2] (#21270)

This commit is contained in:
Nemanja
2023-10-30 23:55:55 -04:00
committed by GitHub
parent 2c5ea97d9a
commit 0c329ed661
240 changed files with 1365 additions and 1275 deletions

View File

@@ -26,7 +26,7 @@ namespace Content.IntegrationTests.Tests
- type: Clothing
slots: [innerclothing]
- type: Item
size: 5
size: Tiny
- type: entity
name: IDCardDummy
@@ -36,7 +36,7 @@ namespace Content.IntegrationTests.Tests
slots:
- idcard
- type: Item
size: 5
size: Tiny
- type: IdCard
- type: entity
@@ -44,14 +44,14 @@ namespace Content.IntegrationTests.Tests
id: FlashlightDummy
components:
- type: Item
size: 5
size: Tiny
- type: entity
name: ToolboxDummy
id: ToolboxDummy
components:
- type: Item
size: 9999
size: Huge
";
[Test]
public async Task Test()

View File

@@ -1,39 +0,0 @@
using Content.Shared.Item;
using Content.Shared.Stacks;
using Robust.Shared.GameObjects;
using Robust.Shared.Prototypes;
namespace Content.IntegrationTests.Tests;
[TestFixture]
public sealed class StackTest
{
[Test]
public async Task StackCorrectItemSize()
{
await using var pair = await PoolManager.GetServerClient();
var server = pair.Server;
var protoManager = server.ResolveDependency<IPrototypeManager>();
var compFact = server.ResolveDependency<IComponentFactory>();
Assert.Multiple(() =>
{
foreach (var entity in PoolManager.GetPrototypesWithComponent<StackComponent>(server))
{
if (!entity.TryGetComponent<StackComponent>(out var stackComponent, compFact) ||
!entity.TryGetComponent<ItemComponent>(out var itemComponent, compFact))
continue;
if (!protoManager.TryIndex<StackPrototype>(stackComponent.StackTypeId, out var stackProto) ||
stackProto.ItemSize == null)
continue;
var expectedSize = stackProto.ItemSize * stackComponent.Count;
Assert.That(itemComponent.Size, Is.EqualTo(expectedSize), $"Prototype id: {entity.ID} has an item size of {itemComponent.Size} but expected size of {expectedSize}.");
}
});
await pair.CleanReturnAsync();
}
}

View File

@@ -3,11 +3,12 @@ using System.Collections.Generic;
using System.Linq;
using Content.Server.Storage.Components;
using Content.Shared.Item;
using Content.Shared.Prototypes;
using Content.Shared.Storage;
using Content.Shared.Storage.Components;
using Content.Shared.Storage.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.Prototypes;
using Robust.UnitTesting;
namespace Content.IntegrationTests.Tests
{
@@ -32,9 +33,11 @@ namespace Content.IntegrationTests.Tests
{
if (!proto.TryGetComponent<StorageComponent>("Storage", out var storage) ||
storage.Whitelist != null ||
!proto.TryGetComponent<ItemComponent>("Item", out var item)) continue;
storage.MaxItemSize == null ||
!proto.TryGetComponent<ItemComponent>("Item", out var item))
continue;
Assert.That(storage.StorageCapacityMax, Is.LessThanOrEqualTo(item.Size), $"Found storage arbitrage on {proto.ID}");
Assert.That(storage.MaxItemSize.Value, Is.LessThanOrEqualTo(item.Size), $"Found storage arbitrage on {proto.ID}");
}
});
await pair.CleanReturnAsync();
@@ -82,69 +85,122 @@ namespace Content.IntegrationTests.Tests
{
foreach (var proto in PoolManager.GetPrototypesWithComponent<StorageFillComponent>(server))
{
int capacity;
var isEntStorage = false;
if (proto.HasComponent<EntityStorageComponent>(compFact))
continue;
if (proto.TryGetComponent<StorageComponent>("Storage", out var storage))
if (!proto.TryGetComponent<StorageComponent>("Storage", out var storage))
{
capacity = storage.StorageCapacityMax;
Assert.Fail($"Entity {proto.ID} has storage-fill without a storage component!");
continue;
}
else if (proto.TryGetComponent<EntityStorageComponent>("EntityStorage", out var entStorage))
proto.TryGetComponent<ItemComponent>("Item", out var item);
var fill = (StorageFillComponent) proto.Components[id].Component;
var size = GetFillSize(fill, false, protoMan);
var maxSize = storage.MaxItemSize ??
(item?.Size == null
? SharedStorageSystem.DefaultStorageMaxItemSize
: (ItemSize) Math.Max(0, (int) item.Size - 1));
if (storage.MaxSlots != null)
{
capacity = entStorage.Capacity;
isEntStorage = true;
Assert.That(GetFillSize(fill, true, protoMan), Is.LessThanOrEqualTo(storage.MaxSlots),
$"{proto.ID} storage fill has too many items.");
}
else
{
Assert.That(size, Is.LessThanOrEqualTo(storage.MaxTotalWeight), $"{proto.ID} storage fill is too large.");
}
foreach (var entry in fill.Contents)
{
if (entry.PrototypeId == null)
continue;
if (!protoMan.TryIndex<EntityPrototype>(entry.PrototypeId, out var fillItem))
continue;
if (!fillItem.TryGetComponent<ItemComponent>("Item", out var entryItem))
continue;
Assert.That(entryItem.Size, Is.LessThanOrEqualTo(maxSize),
$"Entity {proto.ID} has storage-fill item, {entry.PrototypeId}, that is too large");
}
}
});
await pair.CleanReturnAsync();
}
[Test]
public async Task TestSufficientSpaceForEntityStorageFill()
{
await using var pair = await PoolManager.GetServerClient();
var server = pair.Server;
var protoMan = server.ResolveDependency<IPrototypeManager>();
var compFact = server.ResolveDependency<IComponentFactory>();
var id = compFact.GetComponentName(typeof(StorageFillComponent));
Assert.Multiple(() =>
{
foreach (var proto in PoolManager.GetPrototypesWithComponent<StorageFillComponent>(server))
{
if (proto.HasComponent<StorageComponent>(compFact))
continue;
if (!proto.TryGetComponent<EntityStorageComponent>("EntityStorage", out var entStorage))
{
Assert.Fail($"Entity {proto.ID} has storage-fill without a storage component!");
continue;
}
var fill = (StorageFillComponent) proto.Components[id].Component;
var size = GetFillSize(fill, isEntStorage);
Assert.That(size, Is.LessThanOrEqualTo(capacity), $"{proto.ID} storage fill is too large.");
var size = GetFillSize(fill, true, protoMan);
Assert.That(size, Is.LessThanOrEqualTo(entStorage.Capacity),
$"{proto.ID} storage fill is too large.");
}
});
await pair.CleanReturnAsync();
}
int GetEntrySize(EntitySpawnEntry entry, bool isEntStorage)
private int GetEntrySize(EntitySpawnEntry entry, bool getCount, IPrototypeManager protoMan)
{
if (entry.PrototypeId == null)
return 0;
if (!protoMan.TryIndex<EntityPrototype>(entry.PrototypeId, out var proto))
{
if (entry.PrototypeId == null)
return 0;
if (!protoMan.TryIndex<EntityPrototype>(entry.PrototypeId, out var proto))
{
Assert.Fail($"Unknown prototype: {entry.PrototypeId}");
return 0;
}
if (isEntStorage)
return entry.Amount;
if (proto.TryGetComponent<ItemComponent>("Item", out var item))
return item.Size * entry.Amount;
Assert.Fail($"Prototype is missing item comp: {entry.PrototypeId}");
Assert.Fail($"Unknown prototype: {entry.PrototypeId}");
return 0;
}
int GetFillSize(StorageFillComponent fill, bool isEntStorage)
if (getCount)
return entry.Amount;
if (proto.TryGetComponent<ItemComponent>("Item", out var item))
return SharedItemSystem.GetItemSizeWeight(item.Size) * entry.Amount;
Assert.Fail($"Prototype is missing item comp: {entry.PrototypeId}");
return 0;
}
private int GetFillSize(StorageFillComponent fill, bool getCount, IPrototypeManager protoMan)
{
var totalSize = 0;
var groups = new Dictionary<string, int>();
foreach (var entry in fill.Contents)
{
var totalSize = 0;
var groups = new Dictionary<string, int>();
foreach (var entry in fill.Contents)
{
var size = GetEntrySize(entry, isEntStorage);
var size = GetEntrySize(entry, getCount, protoMan);
if (entry.GroupId == null)
totalSize += size;
else
groups[entry.GroupId] = Math.Max(size, groups.GetValueOrDefault(entry.GroupId));
}
return totalSize + groups.Values.Sum();
if (entry.GroupId == null)
totalSize += size;
else
groups[entry.GroupId] = Math.Max(size, groups.GetValueOrDefault(entry.GroupId));
}
await pair.CleanReturnAsync();
return totalSize + groups.Values.Sum();
}
}
}