Files
RobustToolbox/Robust.Shared/Analyzers/ComponentNetworkGeneratorAuxiliary.cs
Kara 85547a9be7 Auto-componentstate source generator (#3684)
* dog what am i doing

* finish gen source part from class symbol

* we are dangerously close to things happening

* generation fixes

* oh? on god?

* stop autogenerating the attribute for no reason + diagnostics

* testing diagnostics

* proper type name handling + clonedata bool

* thank you material storage for making me realize this

* forgot to commit

* p

* fixes for afterautohandlestate

* make it work with access
2023-04-06 12:32:57 -05:00

58 lines
2.4 KiB
C#

using System;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
namespace Robust.Shared.Analyzers;
/// <summary>
/// When a component is marked with this attribute, any members it has marked with <see cref="AutoNetworkedFieldAttribute"/>
/// will automatically be replicated using component states to clients. Systems which need to have more intelligent
/// component state replication beyond just directly setting variables should not use this attribute.
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
[BaseTypeRequired(typeof(Component))]
public sealed class AutoGenerateComponentStateAttribute : Attribute
{
/// <summary>
/// If this is true, the autogenerated code will raise a <see cref="AfterAutoHandleStateEvent"/> component event
/// so that user-defined systems can have effects after handling state without redefining all replication.
/// </summary>
public bool RaiseAfterAutoHandleState;
public AutoGenerateComponentStateAttribute(bool raiseAfterAutoHandleState = false)
{
RaiseAfterAutoHandleState = raiseAfterAutoHandleState;
}
}
/// <summary>
/// Used to mark component members which should be automatically replicated, assuming the component is marked with
/// <see cref="AutoGenerateComponentStateAttribute"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public sealed class AutoNetworkedFieldAttribute : Attribute
{
/// <summary>
/// Determines whether the data should be wrapped in a new() when setting in get/handlestate
/// e.g. for cloning collections like dictionaries or hashsets which is sometimes necessary.
/// </summary>
/// <remarks>
/// This should only be true if the type actually has a constructor that takes in itself.
/// </remarks>
[UsedImplicitly]
public bool CloneData;
public AutoNetworkedFieldAttribute(bool cloneData=false)
{
CloneData = cloneData;
}
}
/// <summary>
/// Raised as a component event after auto handling state is done, if
/// <see cref="AutoGenerateComponentStateAttribute.RaiseAfterAutoHandleState"/> is true, so that other systems
/// can have effects after handling state without having to redefine all replication.
/// </summary>
[ByRefEvent]
public record struct AfterAutoHandleStateEvent(ComponentState State);