1
1
using System . Runtime . InteropServices ;
2
2
using Flecs . NET . Core ;
3
+ using Flecs . NET . Core . Hooks ;
3
4
4
5
// Components
5
- file struct NativeString ( string str )
6
+ file struct NativeString ( string str ) :
7
+ // Resource management hooks. These hooks should primarily be used for
8
+ // managing memory used by the component.
9
+ ICtorHook < NativeString > ,
10
+ IDtorHook < NativeString > ,
11
+ ICopyHook < NativeString > ,
12
+ IMoveHook < NativeString > ,
13
+ // Lifecycle hooks. These hooks should be used for application logic.
14
+ // Note that the signature is the same as query callbacks.
15
+ IOnAddHook < NativeString > ,
16
+ IOnRemoveHook < NativeString > ,
17
+ IOnSetHook < NativeString >
6
18
{
7
19
public IntPtr Value = Marshal . StringToHGlobalAnsi ( str ) ;
20
+
21
+ // The constructor should initialize the component value.
22
+ public static void Ctor ( ref NativeString data , TypeInfo _ )
23
+ {
24
+ Ecs . Log . Trace ( "Ctor" ) ;
25
+ data . Value = IntPtr . Zero ;
26
+ }
27
+
28
+ // The destructor should free resources.
29
+ public static void Dtor ( ref NativeString data , TypeInfo _ )
30
+ {
31
+ Ecs . Log . Trace ( "Dtor" ) ;
32
+ Marshal . FreeHGlobal ( data . Value ) ;
33
+ }
34
+
35
+ // The move hook should move resources from one location to another.
36
+ public static void Move ( ref NativeString dst , ref NativeString src , TypeInfo _ )
37
+ {
38
+ Ecs . Log . Trace ( "Move" ) ;
39
+ Marshal . FreeHGlobal ( dst . Value ) ;
40
+ dst . Value = src . Value ;
41
+ src . Value = IntPtr . Zero ; // This makes sure the value doesn't get deleted twice
42
+ // as the destructor is still invoked after a move.
43
+ }
44
+
45
+ // The copy hook should copy resources from one location to another.
46
+ public static void Copy ( ref NativeString dst , ref NativeString src , TypeInfo _ )
47
+ {
48
+ Ecs . Log . Trace ( "Copy" ) ;
49
+ Marshal . FreeHGlobal ( dst . Value ) ;
50
+ dst = new NativeString ( Marshal . PtrToStringAnsi ( src . Value ) ! ) ; // Allocate new copy of the string.
51
+ }
52
+
53
+ // The on add hook gets called when the component is added.
54
+ public static void OnAdd ( Iter it , int i , ref NativeString _ )
55
+ {
56
+ Ecs . Log . Trace ( $ "{ it . Event ( ) } : { it . Entity ( i ) } ") ;
57
+ }
58
+
59
+ // The on remove hook gets called when the component is removed.
60
+ public static void OnRemove ( Iter it , int i , ref NativeString _ )
61
+ {
62
+ Ecs . Log . Trace ( $ "{ it . Event ( ) } : { it . Entity ( i ) } ") ;
63
+ }
64
+
65
+ // The on set hook gets called when the component is set.
66
+ public static void OnSet ( Iter it , int i , ref NativeString _ )
67
+ {
68
+ Ecs . Log . Trace ( $ "{ it . Event ( ) } : { it . Entity ( i ) } ") ;
69
+ }
8
70
}
9
71
10
72
public static class Entities_Hooks
@@ -13,57 +75,6 @@ public static void Main()
13
75
{
14
76
World world = World . Create ( ) ;
15
77
16
- world . Component < NativeString > ( )
17
- // Resource management hooks. These hooks should primarily be used for
18
- // managing memory used by the component.
19
-
20
- // The constructor should initialize the component value.
21
- . Ctor ( ( ref NativeString data , TypeInfo typeInfo ) =>
22
- {
23
- Ecs . Log . Trace ( "Ctor" ) ;
24
- data . Value = IntPtr . Zero ;
25
- } )
26
-
27
- // The destructor should free resources.
28
- . Dtor ( ( ref NativeString data , TypeInfo typeInfo ) =>
29
- {
30
- Ecs . Log . Trace ( "Dtor" ) ;
31
- Marshal . FreeHGlobal ( data . Value ) ;
32
- } )
33
-
34
- // The move hook should move resources from one location to another.
35
- . Move ( ( ref NativeString dst , ref NativeString src , TypeInfo typeInfo ) =>
36
- {
37
- Ecs . Log . Trace ( "Move" ) ;
38
- Marshal . FreeHGlobal ( dst . Value ) ;
39
- dst . Value = src . Value ;
40
- src . Value = IntPtr . Zero ; // This makes sure the value doesn't get deleted twice
41
- // as the destructor is still invoked after a move.
42
- } )
43
-
44
- // The copy hook should copy resources from one location to another.
45
- . Copy ( ( ref NativeString dst , ref NativeString src , TypeInfo typeInfo ) =>
46
- {
47
- Ecs . Log . Trace ( "Copy" ) ;
48
- Marshal . FreeHGlobal ( dst . Value ) ;
49
- dst = new NativeString ( Marshal . PtrToStringAnsi ( src . Value ) ! ) ; // Allocate new copy of the string.
50
- } )
51
-
52
- // Lifecycle hooks. These hooks should be used for application logic.
53
- // Note that the signature is the same as query callbacks.
54
- . OnAdd ( ( Iter it , int i , ref NativeString str ) =>
55
- {
56
- Ecs . Log . Trace ( $ "{ it . Event ( ) } : { it . Entity ( i ) } ") ;
57
- } )
58
- . OnSet ( ( Iter it , int i , ref NativeString str ) =>
59
- {
60
- Ecs . Log . Trace ( $ "{ it . Event ( ) } : { it . Entity ( i ) } ") ;
61
- } )
62
- . OnRemove ( ( Iter it , int i , ref NativeString str ) =>
63
- {
64
- Ecs . Log . Trace ( $ "{ it . Event ( ) } : { it . Entity ( i ) } ") ;
65
- } ) ;
66
-
67
78
Ecs . Log . SetLevel ( 0 ) ;
68
79
69
80
Entity e = world . Entity ( "Entity" ) ;
0 commit comments