Skip to content

Commit 7be627c

Browse files
committed
Small refactors
1 parent a267029 commit 7be627c

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

Runtime/Code/Luau/AirshipComponentV2.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,35 @@
55
using Assets.Code.Luau;
66
using Luau;
77
using UnityEngine;
8+
using UnityEngine.SceneManagement;
9+
using UnityEngine.Serialization;
810

11+
[AddComponentMenu("Airship/Airship Component")]
12+
[LuauAPI(LuauContext.Protected)]
913
public class AirshipComponentV2 : MonoBehaviour {
14+
private const bool ElevateToProtectedWithinCoreScene = true;
15+
1016
public static LuauScript.AwakeData QueuedAwakeData = null;
1117
private static int _airshipComponentIdGen = 10000000;
18+
private static bool _validatedSceneInGameConfig = false;
1219

1320
public AirshipScript script;
1421

1522
public IntPtr thread;
1623
[HideInInspector] public LuauContext context = LuauContext.Game;
17-
18-
[HideInInspector] public LuauMetadata metadata = new();
24+
[HideInInspector] public bool forceContext = false;
25+
[FormerlySerializedAs("m_metadata")] [HideInInspector] public LuauMetadata metadata = new();
1926

2027
private readonly int _airshipComponentId = _airshipComponentIdGen++;
2128
private readonly Dictionary<AirshipComponentUpdateType, bool> _hasAirshipUpdateMethods = new();
29+
30+
public string TypescriptFilePath => script.m_path.Replace(".lua", ".ts");
2231

2332
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
2433
private static void OnReload() {
2534
_airshipComponentIdGen = 10000000;
2635
QueuedAwakeData = null;
36+
_validatedSceneInGameConfig = false;
2737
}
2838

2939
private void Awake() {
@@ -34,12 +44,26 @@ private void Awake() {
3444
}
3545

3646
ScriptingEntryPoint.InvokeOnLuauStartup();
47+
48+
#if UNITY_EDITOR && !AIRSHIP_PLAYER
49+
if (!_validatedSceneInGameConfig) {
50+
var scene = gameObject.scene;
51+
if (!LuauCore.IsProtectedScene(scene)) {
52+
var sceneName = scene.name;
53+
var gameConfig = GameConfig.Load();
54+
if (gameConfig.gameScenes.ToList().Find((s) => s.name == sceneName) == null) {
55+
throw new Exception(
56+
$"Tried to load AirshipComponent ({name}) on GameObject ({gameObject.name}) in a scene not found in GameConfig.scenes. Please add \"{sceneName}\" to your Assets/GameConfig.asset");
57+
}
58+
}
59+
_validatedSceneInGameConfig = true;
60+
}
61+
#endif
3762

38-
// TODO:
3963
// Assume protected context for bindings within CoreScene
40-
// if (!this.contextOverwritten && ((gameObject.scene.name is "CoreScene" or "MainMenu") || (SceneManager.GetActiveScene().name is "CoreScene" or "MainMenu")) && ElevateToProtectedWithinCoreScene) {
41-
// context = LuauContext.Protected;
42-
// }
64+
if (!forceContext && ((gameObject.scene.name is "CoreScene" or "MainMenu") || (SceneManager.GetActiveScene().name is "CoreScene" or "MainMenu")) && ElevateToProtectedWithinCoreScene) {
65+
context = LuauContext.Protected;
66+
}
4367

4468
// Load the component onto the thread:
4569
thread = LuauScript.LoadAndExecuteScript(gameObject, context, LuauScriptCacheMode.Cached, script, true);

Runtime/Code/Luau/LuauScript.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using Luau;
44
using UnityEngine;
5+
using UnityEngine.SceneManagement;
56
#if UNITY_EDITOR
67
using UnityEditor;
78
#endif
@@ -13,6 +14,8 @@ public enum LuauScriptCacheMode {
1314

1415
[LuauAPI(LuauContext.Protected)]
1516
public class LuauScript : MonoBehaviour {
17+
private const bool ElevateToProtectedWithinCoreScene = true;
18+
1619
public static AwakeData QueuedAwakeData = null;
1720

1821
// Injected from LuauHelper
@@ -22,6 +25,7 @@ public class LuauScript : MonoBehaviour {
2225

2326
public IntPtr thread;
2427
[HideInInspector] public LuauContext context = LuauContext.Game;
28+
[HideInInspector] public bool forceContext = false;
2529

2630
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
2731
private static void OnReload() {
@@ -56,16 +60,16 @@ private static AirshipScript LoadBinaryFileFromPath(string fullFilePath) {
5660
#endif
5761
}
5862

59-
public static LuauScript AddNew(GameObject go, string scriptPath, LuauContext context) {
63+
public static LuauScript Create(GameObject go, string scriptPath, LuauContext context) {
6064
var script = LoadBinaryFileFromPath(scriptPath);
6165
if (script == null) {
6266
throw new Exception($"Failed to load script from file: {scriptPath}");
6367
}
6468

65-
return AddNew(go, script, context);
69+
return Create(go, script, context);
6670
}
6771

68-
public static LuauScript AddNew(GameObject go, AirshipScript script, LuauContext context) {
72+
public static LuauScript Create(GameObject go, AirshipScript script, LuauContext context) {
6973
var awakeData = new AwakeData() {
7074
Script = script,
7175
Context = context,
@@ -92,6 +96,11 @@ private void Awake() {
9296
context = QueuedAwakeData.Context;
9397
QueuedAwakeData = null;
9498
}
99+
100+
// Assume protected context for bindings within CoreScene
101+
if (!forceContext && ((gameObject.scene.name is "CoreScene" or "MainMenu") || (SceneManager.GetActiveScene().name is "CoreScene" or "MainMenu")) && ElevateToProtectedWithinCoreScene) {
102+
context = LuauContext.Protected;
103+
}
95104

96105
LoadAndExecuteScript(gameObject, context, LuauScriptCacheMode.NotCached, script, false);
97106
}
@@ -171,5 +180,6 @@ public static IntPtr LoadAndExecuteScript(GameObject obj, LuauContext context, L
171180
public class AwakeData {
172181
public AirshipScript Script;
173182
public LuauContext Context;
183+
public bool ForceContext = false;
174184
}
175185
}

Runtime/Code/Luau/ScriptingEntryPoint.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ private void StartCoreScripts() {
4343
}
4444

4545
// Main Menu
46-
LuauScript.AddNew(new GameObject("MainMenuInGame"), MainMenuEntryScript, LuauContext.Protected);
46+
LuauScript.Create(new GameObject("MainMenuInGame"), MainMenuEntryScript, LuauContext.Protected);
4747

4848
// Core
49-
LuauScript.AddNew(new GameObject("@Easy/Core"), CoreEntryScript, LuauContext.Game);
49+
LuauScript.Create(new GameObject("@Easy/Core"), CoreEntryScript, LuauContext.Game);
5050
}
5151
}
5252
}

0 commit comments

Comments
 (0)