Make PrototypeManager.TryIndex log errors when using invalid id structs (#5203)

* Make `PrototypeManager.TryIndex` log errors when using id structs

* A
This commit is contained in:
Leon Friedrich
2024-06-09 00:15:21 +12:00
committed by GitHub
parent 5aa9378de0
commit 94f98073b0
4 changed files with 32 additions and 19 deletions

View File

@@ -39,7 +39,7 @@ END TEMPLATE-->
### New features
*None yet*
* `IPrototypeManager.TryIndex` will now default to logging errors if passed an invalid prototype id struct (i,e., `EntProtoId` or `ProtoId<T>`). There is a new optional bool argument to disable logging errors.
### Bugfixes

View File

@@ -143,17 +143,20 @@ public interface IPrototypeManager
/// </summary>
FrozenDictionary<string, T> GetInstances<T>() where T : IPrototype;
/// <inheritdoc cref="TryIndex{T}(string, out T)"/>
bool TryIndex(EntProtoId id, [NotNullWhen(true)] out EntityPrototype? prototype);
/// <inheritdoc cref="TryIndex{T}(ProtoId{T}, out T, bool)"/>
bool TryIndex(EntProtoId id, [NotNullWhen(true)] out EntityPrototype? prototype, bool logError = true);
/// <inheritdoc cref="TryIndex{T}(string, out T)"/>
bool TryIndex<T>(ProtoId<T> id, [NotNullWhen(true)] out T? prototype) where T : class, IPrototype;
/// <summary>
/// Attempt to retrieve the prototype corresponding to the given prototype id.
/// Unless otherwise specified, this will log an error if the id does not match any known prototype.
/// </summary>
bool TryIndex<T>(ProtoId<T> id, [NotNullWhen(true)] out T? prototype, bool logError = true) where T : class, IPrototype;
/// <inheritdoc cref="TryIndex{T}(string, out T)"/>
bool TryIndex(EntProtoId? id, [NotNullWhen(true)] out EntityPrototype? prototype);
/// <inheritdoc cref="TryIndex{T}(ProtoId{T}, out T, bool)"/>
bool TryIndex(EntProtoId? id, [NotNullWhen(true)] out EntityPrototype? prototype, bool logError = true);
/// <inheritdoc cref="TryIndex{T}(string, out T)"/>
bool TryIndex<T>(ProtoId<T>? id, [NotNullWhen(true)] out T? prototype) where T : class, IPrototype;
/// <inheritdoc cref="TryIndex{T}(ProtoId{T}, out T, bool)"/>
bool TryIndex<T>(ProtoId<T>? id, [NotNullWhen(true)] out T? prototype, bool logError = true) where T : class, IPrototype;
bool HasMapping<T>(string id);
bool TryGetMapping(Type kind, string id, [NotNullWhen(true)] out MappingDataNode? mappings);

View File

@@ -108,7 +108,7 @@ public abstract partial class PrototypeManager : IPrototypeManagerInternal
}
}
DebugTools.Assert(!TryIndex(id, out var instance)
DebugTools.Assert(!TryIndex(id, out var instance, false)
|| instance.CategoriesInternal == null
|| instance.CategoriesInternal.All(x =>
set.Any(y => y.ID == x)));
@@ -124,7 +124,7 @@ public abstract partial class PrototypeManager : IPrototypeManagerInternal
}
}
if (!TryIndex(id, out var protoInstance))
if (!TryIndex(id, out var protoInstance, false))
{
// Prototype is abstract
cache.Add(id, set);

View File

@@ -744,19 +744,29 @@ namespace Robust.Shared.Prototypes
}
/// <inheritdoc />
public bool TryIndex(EntProtoId id, [NotNullWhen(true)] out EntityPrototype? prototype)
public bool TryIndex(EntProtoId id, [NotNullWhen(true)] out EntityPrototype? prototype, bool logError = true)
{
return TryIndex(id.Id, out prototype);
if (TryIndex(id.Id, out prototype))
return true;
if (logError)
Sawmill.Error($"Attempted to resolve invalid {nameof(EntProtoId)}: {id.Id}");
return false;
}
/// <inheritdoc />
public bool TryIndex<T>(ProtoId<T> id, [NotNullWhen(true)] out T? prototype) where T : class, IPrototype
public bool TryIndex<T>(ProtoId<T> id, [NotNullWhen(true)] out T? prototype, bool logError = true) where T : class, IPrototype
{
return TryIndex(id.Id, out prototype);
if (TryIndex(id.Id, out prototype))
return true;
if (logError)
Sawmill.Error($"Attempted to resolve invalid ProtoId<{typeof(T).Name}>: {id.Id}");
return false;
}
/// <inheritdoc />
public bool TryIndex(EntProtoId? id, [NotNullWhen(true)] out EntityPrototype? prototype)
public bool TryIndex(EntProtoId? id, [NotNullWhen(true)] out EntityPrototype? prototype, bool logError = true)
{
if (id == null)
{
@@ -764,11 +774,11 @@ namespace Robust.Shared.Prototypes
return false;
}
return TryIndex(id.Value, out prototype);
return TryIndex(id.Value, out prototype, logError);
}
/// <inheritdoc />
public bool TryIndex<T>(ProtoId<T>? id, [NotNullWhen(true)] out T? prototype) where T : class, IPrototype
public bool TryIndex<T>(ProtoId<T>? id, [NotNullWhen(true)] out T? prototype, bool logError = true) where T : class, IPrototype
{
if (id == null)
{
@@ -776,7 +786,7 @@ namespace Robust.Shared.Prototypes
return false;
}
return TryIndex(id.Value, out prototype);
return TryIndex(id.Value, out prototype, logError);
}
/// <inheritdoc />