Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
02d8ef4
Entity Relations
Roudenn Aug 14, 2026
d9ec76c
Part 2
Roudenn Aug 15, 2026
feb6d23
Clean components on manual relation remove
Roudenn Aug 15, 2026
d97e4fc
Update RobustUnitTest.cs
Roudenn Aug 15, 2026
b36a612
More API methods to clear relations
Roudenn Aug 15, 2026
12f0760
Fix API methods
Roudenn Aug 15, 2026
f9a89fa
New ExperimentalTM generator for relations
Roudenn Aug 15, 2026
46ba8c6
Update EntityRelationsSystem.cs
Roudenn Aug 15, 2026
ef033eb
Make shutdown event codegen a bool parameter
Roudenn Aug 15, 2026
2d2cfcb
Update RobustServerSimulation.cs
Roudenn Aug 15, 2026
97117b7
Update EntityDeserializer.cs
Roudenn Aug 15, 2026
66ff141
Docs + analyzer fixes
Roudenn Aug 15, 2026
8b559a6
Review fixes
Roudenn Aug 16, 2026
3bdb07b
Add debug asserts if you make a self-reference
Roudenn Aug 16, 2026
d8e913e
Add a basic test for EntityRelations
Roudenn Aug 16, 2026
9565847
Fix test. fix dictionaries and nullable relations
Roudenn Aug 16, 2026
6733d74
EntityRelationGenerator -> EntityRelationAnalyzer
Roudenn Aug 16, 2026
b45a476
small doc fix
Roudenn Aug 16, 2026
ed70d47
EntityRelationsGeneratorTest
Roudenn Aug 16, 2026
27c1256
Update EntityRelationsGeneratorTest.cs
Roudenn Aug 16, 2026
b9c8ebd
SetRelation `dirty` parameter
Roudenn Aug 16, 2026
b3f49d6
Fix dictionary EntityRelation networking
Roudenn Aug 16, 2026
dea8fb5
Improve API and tests
Roudenn Aug 17, 2026
2f71711
Update EntityRelationsSystemTests.Networking.cs
Roudenn Aug 17, 2026
97186d4
Content API improvements (no I won't make tests)
Roudenn Aug 17, 2026
ed15bd6
Fix swapping Relations by setting them 2 in a row
Roudenn Aug 18, 2026
889823f
Improve collection Relations API even more
Roudenn Aug 19, 2026
01f5dcf
refactor: whitespaces, xml-doc indent, explicit braces and use patter…
Fildrance Aug 23, 2026
84708e3
refactor: extract methods
Fildrance Aug 23, 2026
6d1f358
Review changes
Roudenn Aug 26, 2026
49b1028
Fix network not networking
Roudenn Aug 26, 2026
46fa0cc
Simplify HasRelations method API
Roudenn Aug 26, 2026
acb9115
Merge remote-tracking branch 'upstream/master' into entity-relations
Roudenn Sep 26, 2026
bddcf8d
Merge remote-tracking branch 'upstream/master' into entity-relations
Roudenn Nov 1, 2026
1b378dc
Clear entity relations on `ComponentRemove`
Roudenn Nov 2, 2026
34adfa4
Remove explicit dirtying and fix component adds
Roudenn Nov 2, 2026
ac458c7
Move EntityRelation to separate serializer
Roudenn Nov 2, 2026
ca4c1ac
Merge remote-tracking branch 'upstream/master' into entity-relations
Roudenn Nov 4, 2026
4ce05cf
Local entity relations
Roudenn Nov 4, 2026
cd885ed
Fix local relations + add tests
Roudenn Nov 5, 2026
be4f1d8
Merge remote-tracking branch 'upstream/master' into entity-relations
Roudenn Nov 20, 2026
013c198
SetRemoveOnEmpty ensures the component when false
Roudenn Nov 20, 2026
f393e83
Fix codegen not working
Roudenn Nov 21, 2026
c63bb59
Throw away buggy EntityRelationSerializer
Roudenn Nov 21, 2026
81c5f01
Collection options for relations
Roudenn Nov 22, 2026
cac8740
Fix `ClearRelations` API shenanigans
Roudenn Nov 22, 2026
9cedcc3
Update EntitySystem.Proxy.cs
Roudenn Nov 22, 2026
dbb2e5e
Fix test
Roudenn Nov 22, 2026
1507492
Fix networking able to make invalid relations
Roudenn Nov 22, 2026
dabb4ec
Entity relation public constructor
Roudenn Nov 26, 2026
d6f0e05
start refactoring stuff
Roudenn Nov 29, 2026
a07d0c3
api
Roudenn Nov 29, 2026
5011852
Move API to EntityRelationsSystem and simplify it
Roudenn Nov 29, 2026
1110c11
Update EntityRelationsSystemTests.cs
Roudenn Nov 29, 2026
7aa2f9e
Remove collection settings
Roudenn Nov 29, 2026
b3aa028
Remove EntityRelation wrapper struct
Roudenn Nov 29, 2026
4ad6280
Reconstruct relations on deserialization
Roudenn Nov 29, 2026
e3ec9e8
Oops I did it wrong
Roudenn Nov 29, 2026
50b350d
prevent client from doing anything with relations
Roudenn Nov 29, 2026
1fe7468
Fix tests
Roudenn Nov 29, 2026
e20134d
Update EntityRelationsSystem.API.cs
Roudenn Nov 29, 2026
1ee7fd0
Merge remote-tracking branch 'upstream/master' into entity-relations
Roudenn Nov 29, 2026
d820b45
EntityQuery ensure comps
Roudenn Nov 29, 2026
221a6fb
I am likely doing something wrong...
Roudenn Nov 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
334 changes: 334 additions & 0 deletions Robust.Serialization.Generator/EntityRelationsGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Robust.Roslyn.Shared;
using Robust.Roslyn.Shared.Helpers;

namespace Robust.Serialization.Generator;

/// <summary>
/// Automatically generates implementations for handling timer unpausing.
/// </summary>
Comment thread
Roudenn marked this conversation as resolved.
Resolved
[Generator(LanguageNames.CSharp)]
public sealed class EntityRelationsGenerator : IIncrementalGenerator
{
private const string AutoGenerateComponentPauseAttributeName = "Robust.Shared.Analyzers.AutoGenerateEntityRelationsAttribute";
private const string AutoPausedFieldAttributeName = "Robust.Shared.Analyzers.AutoRelationFieldAttribute";
private const string AutoNetworkFieldAttributeName = "Robust.Shared.Analyzers.AutoNetworkedFieldAttribute";
// ReSharper disable once InconsistentNaming
private const string IComponentTypeName = "Robust.Shared.GameObjects.IComponent";

private static readonly DiagnosticDescriptor NotComponentDiagnostic = new(
Diagnostics.IdComponentPauseNotComponent,

Check warning on line 23 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (freebsd-x64)

Diagnostic Id 'RA0021' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 23 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (osx-arm64)

Diagnostic Id 'RA0021' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 23 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Diagnostic Id 'RA0021' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 23 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (win-x64)

Diagnostic Id 'RA0021' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 23 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (freebsd-arm64)

Diagnostic Id 'RA0021' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 23 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (linux-x64)

Diagnostic Id 'RA0021' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 23 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (win-arm64)

Diagnostic Id 'RA0021' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 23 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (osx-x64)

Diagnostic Id 'RA0021' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 23 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Diagnostic Id 'RA0021' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 23 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (linux-arm64)

Diagnostic Id 'RA0021' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 23 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (MacOS)

Diagnostic Id 'RA0021' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 23 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (MacOS)

Diagnostic Id 'RA0021' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 23 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (Linux)

Diagnostic Id 'RA0021' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 23 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Diagnostic Id 'RA0021' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.
"Class must be an IComponent to use AutoGenerateEntityRelations",
"Class '{0}' must implement IComponent to be used with [AutoGenerateEntityRelations]",
"Usage",
DiagnosticSeverity.Error,
true);

private static readonly DiagnosticDescriptor NoFieldsDiagnostic = new(
Diagnostics.IdComponentPauseNoFields,

Check warning on line 31 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (freebsd-x64)

Diagnostic Id 'RA0022' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 31 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (osx-arm64)

Diagnostic Id 'RA0022' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 31 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Diagnostic Id 'RA0022' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 31 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (win-x64)

Diagnostic Id 'RA0022' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 31 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (freebsd-arm64)

Diagnostic Id 'RA0022' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 31 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (linux-x64)

Diagnostic Id 'RA0022' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 31 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (win-arm64)

Diagnostic Id 'RA0022' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 31 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (osx-x64)

Diagnostic Id 'RA0022' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 31 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Diagnostic Id 'RA0022' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 31 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (linux-arm64)

Diagnostic Id 'RA0022' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 31 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (MacOS)

Diagnostic Id 'RA0022' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 31 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (MacOS)

Diagnostic Id 'RA0022' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 31 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (Linux)

Diagnostic Id 'RA0022' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 31 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Diagnostic Id 'RA0022' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.
"AutoGenerateEntityRelations has no fields",
"Class '{0}' has [AutoGenerateEntityRelations] but has no fields or properties with [AutoRelationField]",
"Usage",
DiagnosticSeverity.Warning,
true);

private static readonly DiagnosticDescriptor NoParentAttributeDiagnostic = new(
Diagnostics.IdComponentPauseNoParentAttribute,

Check warning on line 39 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (freebsd-x64)

Diagnostic Id 'RA0023' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 39 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (osx-arm64)

Diagnostic Id 'RA0023' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 39 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Diagnostic Id 'RA0023' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 39 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (win-x64)

Diagnostic Id 'RA0023' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 39 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (freebsd-arm64)

Diagnostic Id 'RA0023' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 39 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (linux-x64)

Diagnostic Id 'RA0023' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 39 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (win-arm64)

Diagnostic Id 'RA0023' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 39 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (osx-x64)

Diagnostic Id 'RA0023' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 39 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Diagnostic Id 'RA0023' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 39 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (linux-arm64)

Diagnostic Id 'RA0023' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 39 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (MacOS)

Diagnostic Id 'RA0023' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 39 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (MacOS)

Diagnostic Id 'RA0023' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 39 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (Linux)

Diagnostic Id 'RA0023' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 39 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Diagnostic Id 'RA0023' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.
"AutoRelationField on type of field without AutoGenerateEntityRelations",
"Field '{0}' has [AutoRelationField] but its containing type does not have [AutoGenerateEntityRelations]",
"Usage",
DiagnosticSeverity.Error,
true);

private static readonly DiagnosticDescriptor WrongTypeAttributeDiagnostic = new(
Diagnostics.IdComponentPauseWrongTypeAttribute,

Check warning on line 47 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (freebsd-x64)

Diagnostic Id 'RA0024' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 47 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (osx-arm64)

Diagnostic Id 'RA0024' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 47 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Diagnostic Id 'RA0024' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 47 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (win-x64)

Diagnostic Id 'RA0024' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 47 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (freebsd-arm64)

Diagnostic Id 'RA0024' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 47 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (linux-x64)

Diagnostic Id 'RA0024' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 47 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (win-arm64)

Diagnostic Id 'RA0024' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 47 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (osx-x64)

Diagnostic Id 'RA0024' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 47 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Diagnostic Id 'RA0024' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 47 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / Publish check (linux-arm64)

Diagnostic Id 'RA0024' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 47 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (MacOS)

Diagnostic Id 'RA0024' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 47 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (MacOS)

Diagnostic Id 'RA0024' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 47 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (Linux)

Diagnostic Id 'RA0024' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.

Check warning on line 47 in Robust.Serialization.Generator/EntityRelationsGenerator.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Diagnostic Id 'RA0024' is already used by analyzer 'ComponentPauseGenerator'. Please use a different diagnostic ID.
"AutoRelationField has wrong type",
"Field '{0}' has [AutoRelationField] but is not of type EntityRelation",
"Usage",
DiagnosticSeverity.Error,
true);

public void Initialize(IncrementalGeneratorInitializationContext context)
{
var componentInfos = context.SyntaxProvider.ForAttributeWithMetadataName(
AutoGenerateComponentPauseAttributeName,
(syntaxNode, _) => syntaxNode is TypeDeclarationSyntax,
(syntaxContext, _) =>
{
var symbol = (INamedTypeSymbol)syntaxContext.TargetSymbol;

var typeDeclarationSyntax = (TypeDeclarationSyntax) syntaxContext.TargetNode;
var partialTypeInfo = PartialTypeInfo.FromSymbol(
symbol,
typeDeclarationSyntax);

if (syntaxContext.Attributes[0].ConstructorArguments.Length > 0)
AttributeHelper.GetNamedArgumentBool(syntaxContext.Attributes[0], "Dirty", false);

var dirty = false;
if (syntaxContext.Attributes[0].ConstructorArguments[0].Value is bool dirtyBool)
dirty = dirtyBool;

var shutdownSub = false;
if (syntaxContext.Attributes[0].ConstructorArguments[1].Value is bool shutdownSubBool)
shutdownSub = shutdownSubBool;

var fieldBuilder = ImmutableArray.CreateBuilder<FieldInfo>();
foreach (var member in symbol.GetMembers())
{
if (!AttributeHelper.HasAttribute(member, AutoPausedFieldAttributeName, out var _))
Comment thread
Roudenn marked this conversation as resolved.
Resolved
continue;

var type = member switch
{
IPropertySymbol property => property.Type,
IFieldSymbol field => field.Type,
_ => null
};

if (type is not INamedTypeSymbol namedType)
continue;

var invalid = false;
var nullable = false;
var dictionaryKey = false;
var dictionaryValue = false;
var collection = false;
if (namedType.Name != "EntityRelation")
{
if (namedType is { Name: "Nullable", TypeArguments: [{Name: "EntityRelation"}] })
{
nullable = true;
}
else if (namedType is { Name: "Dictionary", TypeArguments: [{Name: "EntityRelation"}, {}]})
{
dictionaryKey = true;
}
else if (namedType is { Name: "Dictionary", TypeArguments: [{}, {Name: "EntityRelation"}]})
{
dictionaryValue = true;
}
if (namedType.Name == "List" || namedType.Name == "HashSet" && namedType is { TypeArguments: [{ Name: "EntityRelation" }]})
{
collection = true;
}
else
{
invalid = true;
}
}

// If any pause field has [AutoNetworkedField], automatically mark it to dirty on unpause.
Comment thread
Roudenn marked this conversation as resolved.
Resolved
if (AttributeHelper.HasAttribute(member, AutoNetworkFieldAttributeName, out var _))
dirty = true;

fieldBuilder.Add(new FieldInfo(member.Name, nullable, invalid, dictionaryKey, dictionaryValue, collection, member.Locations[0]));
}

return new ComponentInfo(
partialTypeInfo,
EquatableArray<FieldInfo>.FromImmutableArray(fieldBuilder.ToImmutable()),
dirty,
shutdownSub,
!TypeSymbolHelper.ImplementsInterface(symbol, IComponentTypeName),
typeDeclarationSyntax.Identifier.GetLocation());
});

context.RegisterImplementationSourceOutput(componentInfos, static (productionContext, info) =>
{
if (info.NotComponent)
{
productionContext.ReportDiagnostic(Diagnostic.Create(
NotComponentDiagnostic,
info.Location,
info.PartialTypeInfo.Name));
return;
}

// Component always have to be partial anyways due to the serialization generator.
// So I can't be arsed to define a diagnostic for this.
if (!info.PartialTypeInfo.IsValid)
return;

if (info.Fields.AsImmutableArray().Length == 0)
{
productionContext.ReportDiagnostic(Diagnostic.Create(
NoFieldsDiagnostic,
info.Location,
info.PartialTypeInfo.Name));
return;
}

var relationBuilder = new StringBuilder();
var shutdownBuilder = new StringBuilder();

var anyValidField = false;
foreach (var field in info.Fields)
{
if (field.Invalid)
{
productionContext.ReportDiagnostic(Diagnostic.Create(WrongTypeAttributeDiagnostic, field.Location));
continue;
}

if (field.Nullable)
{
relationBuilder.AppendLine($"""
if (ent.Comp.{field.Name}.HasValue && ent.Comp.{field.Name}.Value == args.Relation)
ent.Comp.{field.Name} = null;
""");

shutdownBuilder.AppendLine($" entMan.ClearRelation(ent.Owner, ref ent.Comp.{field.Name});");
}
else if (field.DictionaryKey)
{
relationBuilder.AppendLine($" ent.Comp.{field.Name}.Remove(args.Relation);");

shutdownBuilder.AppendLine($" entMan.ClearRelation(ent.Owner, ent.Comp.{field.Name});");
}
else if (field.DictionaryValue)
{
relationBuilder.AppendLine($$"""
Comment thread
Roudenn marked this conversation as resolved.
Resolved
foreach (var (key, value) in ent.Comp.{{field.Name}})
{
if (ent.Comp.{{field.Name}}[key] == args.Relation)
Comment thread
Roudenn marked this conversation as resolved.
Resolved
ent.Comp.{{field.Name}}[key] = EntityRelation.Null;
}
""");

shutdownBuilder.AppendLine($" entMan.ClearRelation(ent.Owner, ent.Comp.{field.Name});");
}
else if (field.Collection)
{
relationBuilder.AppendLine($" ent.Comp.{field.Name}.Remove(args.Relation);");
shutdownBuilder.AppendLine($" entMan.ClearRelation(ent.Owner, ent.Comp.{field.Name});");
}
else
{
relationBuilder.AppendLine($"""
if (ent.Comp.{field.Name} == args.Relation)
ent.Comp.{field.Name} = EntityRelation.Null;
""");

shutdownBuilder.AppendLine($" entMan.ClearRelation(ent.Owner, ref ent.Comp.{field.Name});");
}

anyValidField = true;
}

if (!anyValidField)
return;

if (info.Dirty)
relationBuilder.AppendLine(" Dirty(ent);");

var shutdownSub = info.ShutdownEvent
? $" SubscribeLocalEvent<{info.PartialTypeInfo.Name}, ComponentShutdown>(OnRelationShutdown);"
: string.Empty;

var shutdownSubMethod = info.ShutdownEvent
? $$"""
private void OnRelationShutdown(Entity<{{info.PartialTypeInfo.Name}}> ent, ref ComponentShutdown args)
{
{{info.PartialTypeInfo.Name}}.ClearComponentRelations(ent, EntityManager);
}
"""
: string.Empty;

var result = new StringBuilder();

result.AppendLine("""
// <auto-generated />

using Robust.Shared.GameObjects;

""");

info.PartialTypeInfo.WriteHeader(result);

result.AppendLine($$"""

{
[RobustAutoGenerated]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public sealed partial class {{info.PartialTypeInfo.Name}}_AutoRelationsSystem : EntitySystem
Comment thread
Roudenn marked this conversation as resolved.
Resolved
{
public override void Initialize()
{
base.Initialize();
{{shutdownSub}}
SubscribeLocalEvent<{{info.PartialTypeInfo.Name}}, EntityRelationDeleteEvent>(OnRelationDeleted);
}

private void OnRelationDeleted(Entity<{{info.PartialTypeInfo.Name}}> ent, ref EntityRelationDeleteEvent args)
{
{{relationBuilder}}
}

{{shutdownSubMethod}}
}

/// <summary>
/// Auto-generated method that clears all relations in a certain entity.
/// This has to be called on component shutdown to keep all relations correct.
/// </summary>
public static void ClearComponentRelations(Entity<{{info.PartialTypeInfo.Name}}> ent, IEntityManager entMan)
{
{{shutdownBuilder}}
}
}
""");

info.PartialTypeInfo.WriteFooter(result);

productionContext.AddSource(info.PartialTypeInfo.GetGeneratedFileName(), result.ToString());
});

// Code to report diagnostic for fields that have it but don't have the attribute on the parent.
var allFields = context.SyntaxProvider.ForAttributeWithMetadataName(
AutoPausedFieldAttributeName,
(syntaxNode, _) => syntaxNode is VariableDeclaratorSyntax or PropertyDeclarationSyntax,
(syntaxContext, _) =>
{
var errorTarget = syntaxContext.TargetNode is PropertyDeclarationSyntax prop
? prop.Identifier.GetLocation()
: syntaxContext.TargetNode.GetLocation();
return new AllFieldInfo(
syntaxContext.TargetSymbol.Name,
syntaxContext.TargetSymbol.ContainingType.ToDisplayString(),
errorTarget);
});

var allComponentsTogether = componentInfos.Collect();
var allFieldsTogether = allFields.Collect();
var componentFieldJoin = allFieldsTogether.Combine(allComponentsTogether);

context.RegisterImplementationSourceOutput(componentFieldJoin, (productionContext, info) =>
{
var componentsByName = new HashSet<string>(info.Right.Select(x => x.PartialTypeInfo.DisplayName));
foreach (var field in info.Left)
{
if (!componentsByName.Contains(field.ParentDisplayName))
{
productionContext.ReportDiagnostic(
Diagnostic.Create(NoParentAttributeDiagnostic, field.Location, field.Name));
}
}
});
}

public sealed record ComponentInfo(
PartialTypeInfo PartialTypeInfo,
EquatableArray<FieldInfo> Fields,
bool Dirty,
bool ShutdownEvent,
bool NotComponent,
Location Location);

public sealed record FieldInfo(string Name, bool Nullable, bool Invalid, bool DictionaryKey, bool DictionaryValue, bool Collection, Location Location);

public sealed record AllFieldInfo(string Name, string ParentDisplayName, Location Location);
Comment thread
Roudenn marked this conversation as resolved.
Resolved
}
1 change: 1 addition & 0 deletions Robust.Server.Testing/RobustServerSimulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ public ISimulation InitializeInstance()
compFactory.RegisterClass<GridSplitNodeComponent>();
compFactory.RegisterClass<ChunkEntityComponent>();
compFactory.RegisterClass<ChunkContainerComponent>();
compFactory.RegisterClass<EntityRelationsComponent>();

_regDelegate?.Invoke(compFactory);

Expand Down
Loading
Loading