Skip to content

Commit 631eca3

Browse files
authored
Add instance static class (#41)
1 parent 2a86873 commit 631eca3

17 files changed

+312
-319
lines changed

EcsactCsharpSystemImpl/Runtime/DefaultCsharpSystemImplsLoader.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,25 @@ public static bool Enabled() {
4949

5050
[RuntimeInitializeOnLoadMethod]
5151
internal static void Load() {
52+
5253
if(!Enabled()) {
5354
return;
5455
}
5556

56-
var runtimeSettings = EcsactRuntimeSettings.Get();
57-
var runtime = EcsactRuntime.GetOrLoadDefault();
58-
var implsAssembly = Assembly.Load(
59-
runtimeSettings.defaultCsharpSystemImplsAssemblyName
60-
);
57+
Ecsact.Defaults.WhenReady(() => {
58+
var runtimeSettings = EcsactRuntimeSettings.Get();
59+
var runtime = Ecsact.Defaults.Runtime;
60+
var implsAssembly = Assembly.Load(
61+
runtimeSettings.defaultCsharpSystemImplsAssemblyName
62+
);
6163

62-
foreach(var type in implsAssembly.GetTypes()) {
63-
foreach(var method in type.GetMethods()) {
64-
var defaultSystemImplAttr =
65-
method.GetCustomAttribute<Ecsact.DefaultSystemImplAttribute>();
66-
if(defaultSystemImplAttr == null) continue;
64+
foreach(var type in implsAssembly.GetTypes()) {
65+
foreach(var method in type.GetMethods()) {
66+
var defaultSystemImplAttr =
67+
method.GetCustomAttribute<Ecsact.DefaultSystemImplAttribute>();
68+
if(defaultSystemImplAttr == null) continue;
6769

68-
var systemLikeId = defaultSystemImplAttr.systemLikeId;
70+
var systemLikeId = defaultSystemImplAttr.systemLikeId;
6971

7072
#if UNITY_EDITOR
7173
var errors = ValidateImplMethodInfo(method);
@@ -85,14 +87,15 @@ internal static void Load() {
8587
}
8688
#endif // UNITY_EDITOR
8789

88-
var implDelegate = Delegate.CreateDelegate(
89-
type: typeof(EcsactRuntime.SystemExecutionImpl),
90-
method: method
91-
) as EcsactRuntime.SystemExecutionImpl;
92-
Debug.Assert(implDelegate != null);
93-
runtime.dynamic.SetSystemExecutionImpl(systemLikeId, implDelegate!);
94-
}
95-
}
90+
var implDelegate = Delegate.CreateDelegate(
91+
type: typeof(EcsactRuntime.SystemExecutionImpl),
92+
method: method
93+
) as EcsactRuntime.SystemExecutionImpl;
94+
Debug.Assert(implDelegate != null);
95+
runtime.dynamic.SetSystemExecutionImpl(systemLikeId, implDelegate!);
96+
}
97+
}
98+
});
9699
}
97100
}
98101
}

EcsactWasm/Runtime/EcsactWasmRuntimeLoader.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ public static void OnLoad() {
2020
return;
2121
}
2222

23-
var settings = EcsactWasmRuntimeSettings.Get();
24-
var defaultRuntime = EcsactRuntime.GetOrLoadDefault();
23+
Ecsact.Defaults.WhenReady(Load());
24+
}
2525

26+
private static void Load() {
27+
var settings = EcsactWasmRuntimeSettings.Get();
2628
foreach(var entry in settings.wasmSystemEntries) {
2729
if(string.IsNullOrWhiteSpace(entry.wasmExportName)) continue;
2830

29-
var loadError = defaultRuntime.wasm.Load(
31+
var loadError = Ecsact.Defaults.Runtime.wasm.Load(
3032
entry.wasmAsset.bytes,
3133
entry.systemId,
3234
entry.wasmExportName

Runtime/AsyncRunner.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ namespace Ecsact {
66
[AddComponentMenu("")]
77
public class AsyncRunner : MonoBehaviour {
88
private static AsyncRunner? instance = null;
9-
private EcsactRuntime? runtimeInstance = null;
109

11-
[RuntimeInitializeOnLoadMethod]
1210
private static void OnRuntimeLoad() {
1311
if(instance != null) {
1412
return;
@@ -24,12 +22,8 @@ private static void OnRuntimeLoad() {
2422
DontDestroyOnLoad(gameObject);
2523
}
2624

27-
void Awake() {
28-
runtimeInstance = EcsactRuntime.GetOrLoadDefault();
29-
}
30-
3125
void Update() {
32-
runtimeInstance!.async.FlushEvents();
26+
Ecsact.Defaults.Runtime.async.FlushEvents();
3327
}
3428

3529
void OnDestroy() {

Runtime/DefaultFixedRunner.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ namespace Ecsact {
99
[AddComponentMenu("")]
1010
public class DefaultFixedRunner : EcsactRunner {
1111

12-
[RuntimeInitializeOnLoadMethod]
13-
private static void OnRuntimeLoad() {
14-
EcsactRunner.OnRuntimeLoad<DefaultFixedRunner>(
15-
EcsactRuntimeDefaultRegistry.RunnerType.FixedUpdate,
16-
"Default Fixed Runner"
17-
);
18-
}
19-
2012
void FixedUpdate() {
2113
Execute();
2214
}

Runtime/DefaultRunner.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ namespace Ecsact {
99
[AddComponentMenu("")]
1010
public class DefaultRunner : EcsactRunner {
1111

12-
[RuntimeInitializeOnLoadMethod]
13-
private static void OnRuntimeLoad() {
14-
EcsactRunner.OnRuntimeLoad<DefaultRunner>(
15-
EcsactRuntimeDefaultRegistry.RunnerType.Update,
16-
"Default Runner"
17-
);
18-
}
19-
2012
void Update() {
2113
Execute();
2214
}

Runtime/DynamicEntity.cs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using UnityEngine;
22
using System.Collections.Generic;
3+
using System;
34

45
#nullable enable
56

@@ -55,17 +56,13 @@ public void OnBeforeSerialize() {
5556
public class DynamicEntity : MonoBehaviour {
5657
public global::System.Int32 entityId { get; private set; } = -1;
5758
public List<SerializableEcsactComponent> ecsactComponents = new();
58-
59-
private EcsactRuntime? runtime;
60-
private EcsactRuntimeSettings? settings;
61-
private EcsactRuntimeDefaultRegistry? defReg;
62-
59+
6360
public void AddEcsactCompnent<C>
6461
( C component
6562
) where C : Ecsact.Component
6663
{
6764
if(Application.isPlaying) {
68-
runtime!.core.AddComponent(defReg!.registryId, entityId, component);
65+
Ecsact.Defaults.Registry.AddComponent(entityId, component);
6966
}
7067

7168
ecsactComponents.Add(new SerializableEcsactComponent{
@@ -80,8 +77,7 @@ public void AddEcsactComponent
8077
)
8178
{
8279
if(Application.isPlaying) {
83-
runtime!.core.AddComponent(
84-
defReg!.registryId,
80+
Ecsact.Defaults.Registry.AddComponent(
8581
entityId,
8682
componentId,
8783
componentData
@@ -96,12 +92,12 @@ public void AddEcsactComponent
9692

9793
private void CreateEntityIfNeeded() {
9894
if(entityId == -1) {
99-
runtime = EcsactRuntime.GetOrLoadDefault();
100-
settings = EcsactRuntimeSettings.Get();
101-
defReg = settings.defaultRegistries[0];
102-
entityId = runtime.core.CreateEntity(defReg.registryId);
103-
if(defReg.pool != null) {
104-
defReg.pool.SetPreferredEntityGameObject(entityId, gameObject);
95+
entityId = Ecsact.Defaults.Registry.CreateEntity();
96+
if(Ecsact.Defaults.Pool != null) {
97+
Ecsact.Defaults.Pool.SetPreferredEntityGameObject(
98+
entityId,
99+
gameObject
100+
);
105101
}
106102
}
107103
}
@@ -122,34 +118,37 @@ private void AddInitialEcsactComponents() {
122118
}
123119

124120
foreach(var ecsactComponent in ecsactComponents) {
125-
runtime!.core.AddComponent(
126-
defReg!.registryId,
121+
Ecsact.Defaults.Registry.AddComponent(
127122
entityId,
128123
ecsactComponent.id,
129124
ecsactComponent.data!
130125
);
131126
}
132127
}
133128

134-
void OnEnable() {
135-
CreateEntityIfNeeded();
136-
AddInitialEcsactComponents();
129+
void OnEnable() {
130+
Ecsact.Defaults.WhenReady(() => {
131+
CreateEntityIfNeeded();
132+
AddInitialEcsactComponents();
133+
});
137134
}
138135

139136
void OnDisable() {
140-
foreach(var ecsactComponent in ecsactComponents) {
141-
var hasComponent = runtime!.core.HasComponent(
142-
defReg!.registryId,
143-
entityId,
144-
ecsactComponent.id
145-
);
146-
if(hasComponent) {
147-
runtime!.core.RemoveComponent(
148-
defReg!.registryId,
137+
if(entityId != -1) {
138+
foreach(var ecsactComponent in ecsactComponents) {
139+
var hasComponent = Ecsact.Defaults.Registry.HasComponent(
149140
entityId,
150141
ecsactComponent.id
151142
);
143+
if(hasComponent) {
144+
Ecsact.Defaults.Registry.RemoveComponent(
145+
entityId,
146+
ecsactComponent.id
147+
);
148+
}
152149
}
150+
} else {
151+
throw new Exception("Uninitialized entityID");
153152
}
154153
}
155154
};

Runtime/EcsactDefaults.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Runtime.CompilerServices;
2+
using System;
3+
4+
#nullable enable
5+
6+
[assembly: InternalsVisibleTo("EcsactRuntimeDefaults")]
7+
8+
namespace Ecsact {
9+
static public class Defaults {
10+
11+
internal static EcsactRuntime? _Runtime;
12+
public static EcsactRuntime Runtime => _Runtime ?? throw new Exception(
13+
"Runtime is null, if you want to access it as early as possible " +
14+
"use Ecsact.Defaults.WhenReady"
15+
);
16+
internal static Ecsact.Registry? _Registry;
17+
public static Ecsact.Registry Registry => _Registry ?? throw new Exception(
18+
"Registry is null, if you want to access it as early as possible " +
19+
"use Ecsact.Defaults.WhenReady"
20+
);
21+
public static Ecsact.UnitySync.EntityGameObjectPool? Pool;
22+
public static EcsactRunner? Runner;
23+
24+
private static event global::System.Action? onReady;
25+
26+
public static void WhenReady
27+
( global::System.Action callback
28+
)
29+
{
30+
if(!UnityEngine.Application.isPlaying) {
31+
throw new Exception(
32+
"Ecsact.Defaults.WhenReady may only be used during play mode"
33+
);
34+
}
35+
if(_Runtime != null) {
36+
callback();
37+
} else {
38+
onReady += callback;
39+
}
40+
}
41+
42+
internal static void NotifyReady() {
43+
if(_Runtime != null) {
44+
onReady?.Invoke();
45+
onReady = null;
46+
} else {
47+
throw new Exception(
48+
"Cannot notify ready until the Ecsact Runtime is ready"
49+
);
50+
}
51+
}
52+
53+
internal static void ClearDefaults() {
54+
if(_Runtime != null) {
55+
EcsactRuntime.Free(_Runtime);
56+
Runner = null;
57+
_Runtime = null;
58+
_Registry = null;
59+
Pool = null;
60+
}
61+
}
62+
}
63+
64+
}

Runtime/VisualScriptingEvents.cs.meta renamed to Runtime/EcsactDefaults.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/EcsactExecutionOptions.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Collections.Generic;
2+
using System.Runtime.InteropServices;
3+
4+
namespace Ecsact {
5+
public class ExecutionOptions {
6+
7+
public EcsactRuntime.CExecutionOptions executionOptions;
8+
private List<EcsactRuntime.EcsactAction> actions;
9+
10+
internal ExecutionOptions() {
11+
actions = new();
12+
executionOptions = new();
13+
}
14+
15+
public void AddActions() {
16+
var actionsArray = actions.ToArray();
17+
18+
executionOptions.actions = actionsArray;
19+
executionOptions.actionsLength = actionsArray.Length;
20+
}
21+
22+
public int actionCount() {
23+
return actions.Count;
24+
}
25+
26+
public void PushAction<T>
27+
( T action
28+
) where T : Ecsact.Action
29+
{
30+
var actionId = Ecsact.Util.GetActionID<T>();
31+
var actionPtr = Marshal.AllocHGlobal(Marshal.SizeOf(action));
32+
Marshal.StructureToPtr(action, actionPtr, false);
33+
var ecsAction = new EcsactRuntime.EcsactAction {
34+
actionId = actionId,
35+
actionData = actionPtr
36+
};
37+
actions.Add(ecsAction);
38+
}
39+
40+
public EcsactRuntime.CExecutionOptions C() {
41+
return executionOptions;
42+
}
43+
44+
public void FreeActions() {
45+
foreach(var ecsAction in actions) {
46+
Marshal.FreeHGlobal(ecsAction.actionData);
47+
}
48+
actions.Clear();
49+
}
50+
51+
};
52+
}

Runtime/EcsactExecutionOptions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)