@ -11,15 +11,15 @@ public static class SyncRegistry
static SyncRegistry ( )
static SyncRegistry ( )
{
{
foreach ( var type in typeof ( SyncRegistry ) . Assembly . GetTypes ( ) ) {
foreach ( var type in typeof ( SyncRegistry ) . Assembly . GetTypes ( ) ) {
var objAttr = type . GetCustomAttribute < SyncObject Attribute > ( ) ;
var objAttr = type . GetCustomAttribute < SyncAttribute > ( ) ;
if ( objAttr = = null ) continue ;
if ( objAttr = = null ) continue ;
if ( ! typeof ( Node ) . IsAssignableFrom ( type ) ) throw new Exception (
if ( ! typeof ( Node ) . IsAssignableFrom ( type ) ) throw new Exception (
$"Type {type} with {nameof(SyncObject Attribute)} must be a subclass of {nameof(Node)}" ) ;
$"Type {type} with {nameof(SyncAttribute)} must be a subclass of {nameof(Node)}" ) ;
var objInfo = new SyncObjectInfo ( ( ushort ) _ byID . Count , type ) ;
var objInfo = new SyncObjectInfo ( ( ushort ) _ byID . Count , type ) ;
foreach ( var property in type . GetProperties ( ) ) {
foreach ( var property in type . GetProperties ( ) ) {
if ( property . GetCustomAttribute < SyncProperty Attribute > ( ) = = null ) continue ;
if ( property . GetCustomAttribute < SyncAttribute > ( ) = = null ) continue ;
var propType = typeof ( SyncPropertyInfo < , > ) . MakeGenericType ( type , property . PropertyType ) ;
var propType = typeof ( SyncPropertyInfo < , > ) . MakeGenericType ( type , property . PropertyType ) ;
var propInfo = ( SyncPropertyInfo ) Activator . CreateInstance ( propType , ( byte ) objInfo . PropertiesByID . Count , property ) ;
var propInfo = ( SyncPropertyInfo ) Activator . CreateInstance ( propType , ( byte ) objInfo . PropertiesByID . Count , property ) ;
objInfo . PropertiesByID . Add ( propInfo ) ;
objInfo . PropertiesByID . Add ( propInfo ) ;
@ -41,7 +41,7 @@ public static class SyncRegistry
= > Get ( typeof ( T ) ) ;
= > Get ( typeof ( T ) ) ;
public static SyncObjectInfo Get ( Type type )
public static SyncObjectInfo Get ( Type type )
= > _ byType . TryGetValue ( type , out var value ) ? value : throw new Exception (
= > _ byType . TryGetValue ( type , out var value ) ? value : throw new Exception (
$"No {nameof(SyncObjectInfo)} found for type {type} (missing {nameof(SyncObject Attribute)}?)" ) ;
$"No {nameof(SyncObjectInfo)} found for type {type} (missing {nameof(SyncAttribute)}?)" ) ;
}
}
@ -49,22 +49,17 @@ public class SyncObjectInfo
{
{
public ushort ID { get ; }
public ushort ID { get ; }
public Type Type { get ; }
public Type Type { get ; }
public PackedScene Scene { get ; }
public string Name = > Type . Name ;
public string Name = > Type . Name ;
public PackedScene InstanceScene { get ; }
public string ContainerNodePath { get ; }
public List < SyncPropertyInfo > PropertiesByID { get ; } = new List < SyncPropertyInfo > ( ) ;
public List < SyncPropertyInfo > PropertiesByID { get ; } = new List < SyncPropertyInfo > ( ) ;
public Dictionary < string , SyncPropertyInfo > PropertiesByName { get ; } = new Dictionary < string , SyncPropertyInfo > ( ) ;
public Dictionary < string , SyncPropertyInfo > PropertiesByName { get ; } = new Dictionary < string , SyncPropertyInfo > ( ) ;
public SyncObjectInfo ( ushort id , Type type )
public SyncObjectInfo ( ushort id , Type type )
{
{
ID = id ;
ID = id ;
Type = type ;
Type = type ;
Scene = GD . Load < PackedScene > ( $"res://scene/{type.Name}.tscn" ) ;
var attr = type . GetCustomAttribute < SyncObjectAttribute > ( ) ;
InstanceScene = GD . Load < PackedScene > ( $"res://scene/{attr.Scene}.tscn" ) ;
ContainerNodePath = attr . Container ;
}
}
}
}
@ -95,16 +90,7 @@ public class SyncPropertyInfo<TObject, TValue> : SyncPropertyInfo
}
}
[AttributeUsage(AttributeTargets.Class)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
public class SyncObjectAttribute : Attribute
public class SyncAttribute : Attribute
{
public string Scene { get ; }
public string Container { get ; }
public SyncObjectAttribute ( string scene , string container )
{ Scene = scene ; Container = container ; }
}
[AttributeUsage(AttributeTargets.Property)]
public class SyncPropertyAttribute : Attribute
{
{
}
}