Skip to content

Commit f580aa5

Browse files
committed
Attempt early init
1 parent 989eff9 commit f580aa5

File tree

2 files changed

+65
-29
lines changed

2 files changed

+65
-29
lines changed

Runtime/Code/Luau/AirshipComponent.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class AirshipComponent : MonoBehaviour {
2525
public static readonly Dictionary<int, string> ComponentIdToScriptName = new();
2626
private static int _airshipComponentIdGen = 10000000;
2727
private static bool _validatedSceneInGameConfig = false;
28+
29+
private bool _init = false;
2830

2931
public AirshipScript script;
3032

@@ -78,8 +80,15 @@ public static AirshipComponent Create(GameObject go, AirshipScript script, LuauC
7880

7981
return component;
8082
}
81-
83+
8284
private void Awake() {
85+
Init();
86+
}
87+
88+
public void Init() {
89+
if (_init) return;
90+
_init = true;
91+
8392
if (QueuedAwakeData != null) {
8493
script = QueuedAwakeData.Script;
8594
context = QueuedAwakeData.Context;
@@ -130,6 +139,10 @@ private void Awake() {
130139

131140
private void AwakeAirshipComponent() {
132141
InitializeAirshipReference();
142+
143+
foreach (var dependency in GetDependencies()) {
144+
dependency.Init();
145+
}
133146

134147
var properties = new List<LuauMetadataProperty>(metadata.properties);
135148

@@ -201,6 +214,27 @@ private void InvokeAirshipLifecycle(AirshipComponentUpdateType updateType) {
201214
LuauPlugin.LuauUpdateIndividualAirshipComponent(context, thread, AirshipBehaviourRootV2.GetId(gameObject), _airshipComponentId, updateType, 0, true);
202215
}
203216

217+
private IReadOnlyList<AirshipComponent> GetDependencies() {
218+
List<AirshipComponent> dependencies = new();
219+
220+
foreach (var property in metadata.properties) {
221+
if (property.ComponentType == AirshipComponentPropertyType.AirshipComponent) {
222+
var obj = property.serializedObject;
223+
if (obj == null) continue;
224+
dependencies.Add(obj as AirshipComponent);
225+
} else if (property.ComponentType == AirshipComponentPropertyType.AirshipArray && property.ArrayElementComponentType == AirshipComponentPropertyType.AirshipComponent) {
226+
if (property.items.objectRefs == null) continue;
227+
foreach (var arrayItem in property.items.objectRefs) {
228+
if (arrayItem != null) {
229+
dependencies.Add(arrayItem as AirshipComponent);
230+
}
231+
}
232+
}
233+
}
234+
235+
return dependencies;
236+
}
237+
204238
private bool HasAirshipMethod(AirshipComponentUpdateType updateType) {
205239
if (_hasAirshipUpdateMethods.TryGetValue(updateType, out var has)) {
206240
return has;

Runtime/Code/LuauAPI/AirshipBehaviourHelper.cs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Runtime.CompilerServices;
4+
using System.Windows.Forms;
45
using Luau;
56
using UnityEngine;
67

@@ -25,12 +26,12 @@ private static int PushEmptyTable(IntPtr thread) {
2526
}
2627

2728
private static int GetAirshipBehaviourRootId(GameObject gameObject) {
28-
// if (!AirshipBehaviourRootV2.HasId(gameObject)) {
29-
// // See if it just needs to be started first:
30-
// foreach (var binding in gameObject.GetComponents<AirshipComponent>()) {
31-
// binding.InitEarly();
32-
// }
33-
// }
29+
if (!AirshipBehaviourRootV2.HasId(gameObject)) {
30+
// See if it just needs to be started first:
31+
foreach (var component in gameObject.GetComponents<AirshipComponent>()) {
32+
component.Init();
33+
}
34+
}
3435

3536
if (!AirshipBehaviourRootV2.HasId(gameObject)) {
3637
return -1;
@@ -39,8 +40,8 @@ private static int GetAirshipBehaviourRootId(GameObject gameObject) {
3940
return AirshipBehaviourRootV2.GetId(gameObject);
4041
}
4142

42-
private static bool IsTypeOrInheritingType(AirshipComponent binding, string typeName, string targetTypeScriptPath) {
43-
var componentName = binding.GetAirshipComponentName();
43+
private static bool IsTypeOrInheritingType(AirshipComponent airshipComponent, string typeName, string targetTypeScriptPath) {
44+
var componentName = airshipComponent.GetAirshipComponentName();
4445

4546
if (componentName == typeName) {
4647
return true;
@@ -50,7 +51,7 @@ private static bool IsTypeOrInheritingType(AirshipComponent binding, string type
5051
if (!buildInfo) return false;
5152

5253
// Check inheritance if possible
53-
return targetTypeScriptPath != null && buildInfo.Inherits(binding.script, targetTypeScriptPath);
54+
return targetTypeScriptPath != null && buildInfo.Inherits(airshipComponent.script, targetTypeScriptPath);
5455
}
5556

5657
public static int GetAirshipComponent(LuauContext context, IntPtr thread, GameObject gameObject, string typeName) {
@@ -62,10 +63,11 @@ public static int GetAirshipComponent(LuauContext context, IntPtr thread, GameOb
6263
var buildInfo = AirshipBuildInfo.Instance;
6364
var targetTypeScriptPath = buildInfo ? buildInfo.GetScriptPathByTypeName(typeName) : null;
6465

65-
foreach (var binding in gameObject.GetComponents<AirshipComponent>()) {
66-
if (!IsTypeOrInheritingType(binding, typeName, targetTypeScriptPath)) continue;
66+
foreach (var airshipComponent in gameObject.GetComponents<AirshipComponent>()) {
67+
airshipComponent.Init();
68+
if (!IsTypeOrInheritingType(airshipComponent, typeName, targetTypeScriptPath)) continue;
6769

68-
var componentId = binding.GetAirshipComponentId();
70+
var componentId = airshipComponent.GetAirshipComponentId();
6971

7072
LuauPlugin.LuauPushAirshipComponent(context, thread, unityInstanceId, componentId);
7173
return 1;
@@ -82,10 +84,11 @@ public static int GetAirshipComponents(LuauContext context, IntPtr thread, GameO
8284
var targetTypeScriptPath = buildInfo ? buildInfo.GetScriptPathByTypeName(typeName) : null;
8385

8486
var hasAny = false;
85-
foreach (var binding in gameObject.GetComponents<AirshipComponent>()) {
86-
if (!IsTypeOrInheritingType(binding, typeName, targetTypeScriptPath)) continue;
87+
foreach (var airshipComponent in gameObject.GetComponents<AirshipComponent>()) {
88+
airshipComponent.Init();
89+
if (!IsTypeOrInheritingType(airshipComponent, typeName, targetTypeScriptPath)) continue;
8790

88-
var componentId = binding.GetAirshipComponentId();
91+
var componentId = airshipComponent.GetAirshipComponentId();
8992

9093
if (!hasAny) {
9194
hasAny = true;
@@ -105,18 +108,17 @@ public static int GetAirshipComponents(LuauContext context, IntPtr thread, GameO
105108

106109
public static int GetAirshipComponentInChildren(LuauContext context, IntPtr thread, GameObject gameObject, string typeName, bool includeInactive) {
107110
// Attempt to initialize any uninitialized bindings first:
108-
var scriptBindings = gameObject.GetComponentsInChildren<AirshipComponent>();
109-
foreach (var binding in scriptBindings) {
111+
foreach (var airshipComponent in gameObject.GetComponentsInChildren<AirshipComponent>()) {
110112
// Side effect loads the components if found. No need for its return result here.
111-
GetAirshipBehaviourRootId(binding.gameObject);
113+
GetAirshipBehaviourRootId(airshipComponent.gameObject);
112114
}
113115

114116
var airshipComponents = gameObject.GetComponentsInChildren<AirshipComponent>(includeInactive);
115117
var buildInfo = AirshipBuildInfo.Instance;
116118
var targetTypeScriptPath = buildInfo ? buildInfo.GetScriptPathByTypeName(typeName) : null;
117119

118120
foreach (var airshipComponent in airshipComponents) {
119-
var componentName = airshipComponent.GetAirshipComponentName();
121+
airshipComponent.Init();
120122
if (!IsTypeOrInheritingType(airshipComponent, typeName, targetTypeScriptPath)) continue;
121123

122124
var componentId = airshipComponent.GetAirshipComponentId();
@@ -130,18 +132,18 @@ public static int GetAirshipComponentInChildren(LuauContext context, IntPtr thre
130132

131133
public static int GetAirshipComponentInParent(LuauContext context, IntPtr thread, GameObject gameObject,
132134
string typeName, bool includeInactive) {
133-
var scriptBindings = gameObject.GetComponentsInParent<AirshipComponent>();
134135

135-
foreach (var binding in scriptBindings) {
136+
foreach (var airshipComponent in gameObject.GetComponentsInParent<AirshipComponent>()) {
136137
// Side effect loads the components if found. No need for its return result here.
137-
GetAirshipBehaviourRootId(binding.gameObject);
138+
GetAirshipBehaviourRootId(airshipComponent.gameObject);
138139
}
139140

140141
var airshipComponents = gameObject.GetComponentsInParent<AirshipComponent>(includeInactive);
141142
var buildInfo = AirshipBuildInfo.Instance;
142143
var targetTypeScriptPath = buildInfo ? buildInfo.GetScriptPathByTypeName(typeName) : null;
143144

144145
foreach (var airshipComponent in airshipComponents) {
146+
airshipComponent.Init();
145147
if (!IsTypeOrInheritingType(airshipComponent, typeName, targetTypeScriptPath)) continue;
146148

147149
var componentId = airshipComponent.GetAirshipComponentId();
@@ -155,10 +157,9 @@ public static int GetAirshipComponentInParent(LuauContext context, IntPtr thread
155157

156158
public static int GetAirshipComponentsInChildren(LuauContext context, IntPtr thread, GameObject gameObject, string typeName, bool includeInactive) {
157159
// Attempt to initialize any uninitialized bindings first:
158-
var scriptBindings = gameObject.GetComponentsInChildren<AirshipComponent>();
159-
foreach (var binding in scriptBindings) {
160+
foreach (var airshipComponent in gameObject.GetComponentsInChildren<AirshipComponent>()) {
160161
// Side effect loads the components if found. No need for its return result here.
161-
GetAirshipBehaviourRootId(binding.gameObject);
162+
GetAirshipBehaviourRootId(airshipComponent.gameObject);
162163
}
163164

164165
var airshipComponents = gameObject.GetComponentsInChildren<AirshipComponent>(includeInactive);
@@ -168,6 +169,7 @@ public static int GetAirshipComponentsInChildren(LuauContext context, IntPtr thr
168169
var componentIdsByUnityInstanceIds = new Dictionary<int, List<int>>();
169170

170171
foreach (var airshipComponent in airshipComponents) {
172+
airshipComponent.Init();
171173
if (!IsTypeOrInheritingType(airshipComponent, typeName, targetTypeScriptPath)) continue;
172174

173175
var componentId = airshipComponent.GetAirshipComponentId();
@@ -195,10 +197,9 @@ public static int GetAirshipComponentsInParent(LuauContext context, IntPtr threa
195197
var foundComponents = false;
196198

197199
// Attempt to initialize any uninitialized bindings first:
198-
var scriptBindings = gameObject.GetComponentsInParent<AirshipComponent>();
199-
foreach (var binding in scriptBindings) {
200+
foreach (var airshipComponent in gameObject.GetComponentsInParent<AirshipComponent>()) {
200201
// Side effect loads the components if found. No need for its return result here.
201-
GetAirshipBehaviourRootId(binding.gameObject);
202+
GetAirshipBehaviourRootId(airshipComponent.gameObject);
202203
}
203204

204205
var airshipComponents = gameObject.GetComponentsInParent<AirshipComponent>(includeInactive);
@@ -208,6 +209,7 @@ public static int GetAirshipComponentsInParent(LuauContext context, IntPtr threa
208209
var componentIdsByUnityInstanceIds = new Dictionary<int, List<int>>();
209210

210211
foreach (var airshipComponent in airshipComponents) {
212+
airshipComponent.Init();
211213
if (!IsTypeOrInheritingType(airshipComponent, typeName, targetTypeScriptPath)) continue;
212214

213215
var componentId = airshipComponent.GetAirshipComponentId();

0 commit comments

Comments
 (0)