IRobustCloneable and generator support (#5692)

* Add IRobustCloneable and check for it in compnet generator.

* Redo compnetgenerator support; add test

* Disconnect client at end of test

* Actually test for client entities

* Cleanup

* Cleanup 2
This commit is contained in:
Tayrtahn
2025-06-27 14:37:43 -04:00
committed by GitHub
parent bd0579ed6d
commit a45b72a1c5
3 changed files with 199 additions and 1 deletions

View File

@@ -35,6 +35,7 @@ namespace Robust.Shared.CompNetworkGenerator
private const string GlobalDictionaryName = "global::System.Collections.Generic.Dictionary<TKey, TValue>";
private const string GlobalHashSetName = "global::System.Collections.Generic.HashSet<T>";
private const string GlobalListName = "global::System.Collections.Generic.List<T>";
private const string GlobalIRobustCloneableName = "global::Robust.Shared.Serialization.IRobustCloneable";
private static readonly SymbolDisplayFormat FullNullableFormat =
FullyQualifiedFormat.WithMiscellaneousOptions(IncludeNullableReferenceTypeModifier);
@@ -375,7 +376,39 @@ namespace Robust.Shared.CompNetworkGenerator
stateFields.Append($@"
public {networkedType} {name} = default!;");
if (IsCloneType(type))
if (ImplementsInterface(type, GlobalIRobustCloneableName))
{
getField = $"component.{name}";
cast = $"({castString})";
var nullCast = nullable ? castString.Substring(0, castString.Length - 1) : castString;
if (nullable)
{
handleStateSetters.Append($@"
component.{name} = state.{name} == null ? null! : state.{name}.Clone();");
deltaHandleFields.Append($@"
var {name}Value = {cast} {fieldHandleValue};
if ({name}Value == null)
component.{name} = null!;
else
component.{name} = {nullCast}({name}Value.Clone());");
shallowClone.Append($@"
{name} = this.{name},");
deltaApply.Add($"fullState.{name} = {name} == null ? null! : {name}.Clone();");
}
else
{
handleStateSetters.Append($@"
component.{name} = state.{name}.Clone();");
deltaHandleFields.Append($@"
component.{name} = {cast}({fieldHandleValue}.Clone());");
shallowClone.Append($@"
{name} = this.{name},");
deltaApply.Add($"fullState.{name} = {name}.Clone();");
}
}
else if (IsCloneType(type))
{
getField = $"component.{name}";
cast = $"({castString})";
@@ -758,5 +791,19 @@ public partial class {componentName}{deltaInterface}
_ => false
};
}
private static bool ImplementsInterface(ITypeSymbol type, string interfaceName)
{
foreach (var interfaceType in type.AllInterfaces)
{
if (interfaceType.ToDisplayString(FullyQualifiedFormat).Contains(interfaceName)
|| interfaceType.ConstructedFrom.ToDisplayString(FullyQualifiedFormat).Contains(interfaceName))
{
return true;
}
}
return false;
}
}
}