Skip to content

Commit e898b11

Browse files
committed
Replaced MapObjectData w/ MapObjectDB
Adding components to each GameObject takes 100-200ms on map load while this only takes 5ms.
1 parent 023ccc8 commit e898b11

21 files changed

Lines changed: 113 additions & 115 deletions

LevelImposter/Core/Components/LIExileController.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@ public class LIExileController(IntPtr intPtr) : ExileController(intPtr)
4040
exileVisorPosition = new Vector3(-0.148f, 0.647f, -0.002f);
4141

4242
// Get Element Data
43-
var elementData = EjectBuilder.EjectController?.gameObject.GetLIData();
44-
if (elementData == null)
43+
var element = MapObjectDB.Get(EjectBuilder.EjectController?.gameObject);
44+
if (element == null)
4545
throw new Exception("Failed to get LIElementData from EjectController");
4646

4747
// Get Element Properties
48-
_x = elementData.Element.x;
49-
_y = elementData.Element.y;
50-
_preTextDuration = elementData.Properties.ejectPreTextDuration ?? 2.0f;
51-
_textDuration = elementData.Properties.ejectTextDuration ?? 2.0f;
52-
_postTextDuration = elementData.Properties.ejectPostTextDuration ?? 2.0f;
53-
_cameraXOffset = elementData.Properties.camXOffset ?? 0.0f;
54-
_cameraYOffset = elementData.Properties.camYOffset ?? 0.0f;
55-
_cameraZoom = elementData.Properties.camZoom ?? 3.0f;
48+
_x = element.x;
49+
_y = element.y;
50+
_preTextDuration = element.properties.ejectPreTextDuration ?? 2.0f;
51+
_textDuration = element.properties.ejectTextDuration ?? 2.0f;
52+
_postTextDuration = element.properties.ejectPostTextDuration ?? 2.0f;
53+
_cameraXOffset = element.properties.camXOffset ?? 0.0f;
54+
_cameraYOffset = element.properties.camYOffset ?? 0.0f;
55+
_cameraZoom = element.properties.camZoom ?? 3.0f;
5656

5757
// Set Base Duration (Just in case)
5858
Duration = _preTextDuration + _textDuration + _postTextDuration + DURATION_OFFSET;

LevelImposter/Core/Components/LIFloat.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public class LIFloat(IntPtr intPtr) : MonoBehaviour(intPtr)
1616
public void Awake()
1717
{
1818
// Get LI data
19-
var objectData = gameObject.GetLIData();
20-
if (objectData == null)
19+
var element = MapObjectDB.Get(gameObject);
20+
if (element == null)
2121
throw new Exception("LIFloat is missing LI data");
2222

23-
_height = objectData.Element.properties.floatingHeight ?? _height;
24-
_speed = objectData.Element.properties.floatingSpeed ?? _speed;
25-
_yScale = objectData.Element.yScale;
23+
_height = element.properties.floatingHeight ?? _height;
24+
_speed = element.properties.floatingSpeed ?? _speed;
25+
_yScale = element.yScale;
2626
}
2727

2828
public void Update()

LevelImposter/Core/Components/LIPhysicsObject.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ public class LIPhysicsObject(IntPtr intPtr) : MonoBehaviour(intPtr)
2525
private static uint _objectCounter;
2626
private uint _objectID;
2727

28-
public Rigidbody2D? rb;
29-
public MapObjectData? liObject;
28+
[HideFromIl2Cpp] public LIElement? Element { get; private set; }
29+
[HideFromIl2Cpp] public Rigidbody2D? Rigidbody { get; private set; }
3030

3131
public void Awake()
3232
{
3333
_objectID = _objectCounter++;
3434

3535
AllObjects.Add(_objectID, this);
36-
37-
rb = GetComponent<Rigidbody2D>();
38-
liObject = gameObject.GetLIData();
36+
37+
Element = MapObjectDB.Get(gameObject);
38+
Rigidbody = GetComponent<Rigidbody2D>();
3939
}
4040

4141
public void Start()
@@ -83,7 +83,7 @@ private IEnumerator CoUpdatePosAsHost()
8383

8484
private void UpdateObjectPosOverRPC()
8585
{
86-
if (rb == null)
86+
if (Rigidbody == null)
8787
throw new Exception("Rigidbody2D is null");
8888

8989
Rpc<PhysicsObjectRPC>.Instance.Send(PlayerControl.LocalPlayer, new RPCPhysicsObjectPacket
@@ -92,9 +92,9 @@ private void UpdateObjectPosOverRPC()
9292
X = transform.position.x,
9393
Y = transform.position.y,
9494
Rotation = transform.rotation.eulerAngles.z,
95-
VelocityX = rb.velocity.x,
96-
VelocityY = rb.velocity.y,
97-
AngularVelocity = rb.angularVelocity
95+
VelocityX = Rigidbody.velocity.x,
96+
VelocityY = Rigidbody.velocity.y,
97+
AngularVelocity = Rigidbody.angularVelocity
9898
});
9999
}
100100
}

LevelImposter/Core/Components/LIScroll.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public class LIScroll(IntPtr intPtr) : MonoBehaviour(intPtr)
1818
public void Awake()
1919
{
2020
// Get Object Data
21-
var objData = gameObject.GetLIData();
22-
if (objData == null)
23-
throw new Exception("Missing MapObjectData");
21+
var element = MapObjectDB.Get(gameObject);
22+
if (element == null)
23+
throw new Exception("LIScroll is missing LI data");
2424

2525
// Get Scrolling Speeds
26-
_xSpeed = objData.Element.properties.scrollingXSpeed ?? _xSpeed;
27-
_ySpeed = objData.Element.properties.scrollingYSpeed ?? _ySpeed;
26+
_xSpeed = element.properties.scrollingXSpeed ?? _xSpeed;
27+
_ySpeed = element.properties.scrollingYSpeed ?? _ySpeed;
2828

2929
// Set Layer
3030
//gameObject.layer = (int)Layer.Ship;
@@ -34,7 +34,7 @@ public void Awake()
3434
// Unity doesn't support scrolling textures on sprites
3535
SpriteBuilder.OnSpriteLoad += (loadedElem, _) =>
3636
{
37-
if (loadedElem.id != objData.ID)
37+
if (loadedElem.id != element.id)
3838
return;
3939
ReplaceRenderer();
4040
};

LevelImposter/Core/Components/LITeleporter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public class LITeleporter(IntPtr intPtr) : MonoBehaviour(intPtr)
1818

1919
public void Awake()
2020
{
21-
_element = gameObject.GetLIData().Element;
22-
_preserveOffset = _element.properties.preserveOffset ?? true;
23-
_clientSide = _element.properties.triggerClientSide ?? false;
21+
_element = MapObjectDB.Get(gameObject);
22+
_preserveOffset = _element?.properties.preserveOffset ?? true;
23+
_clientSide = _element?.properties.triggerClientSide ?? false;
2424
}
2525

2626
public void OnDestroy()

LevelImposter/Core/Components/MapObjectData.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

LevelImposter/Core/Components/TriggerAnim.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ public void OnDestroy()
4040
private void Init()
4141
{
4242
// Get Object Data
43-
var elementData = gameObject.GetLIData();
44-
if (elementData == null)
45-
throw new Exception("No LIElement data found on object");
43+
var element = MapObjectDB.Get(gameObject);
44+
if (element == null)
45+
throw new Exception("TriggerAnim is missing LI data");
4646

4747
// Get Animation Data
48-
_animTargets = elementData.Properties.animTargets ?? Array.Empty<LIAnimTarget>();
49-
_loop = elementData.Properties.triggerLoop ?? false;
48+
_animTargets = element.properties.animTargets ?? Array.Empty<LIAnimTarget>();
49+
_loop = element.properties.triggerLoop ?? false;
5050

5151
// Sort All Keyframes by Time
5252
// Also calculate the duration of the animation

LevelImposter/Core/LILogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace LevelImposter.Core;
1111
public static class LILogger
1212
{
1313
// Set to true to log Unity Stack traces to the BepInEx console. Useful when debugging.
14-
private const bool LOG_UNITY_STACK_TRACE = true;
14+
private const bool LOG_UNITY_STACK_TRACE = false;
1515

1616
private static ManualLogSource? _logger;
1717

LevelImposter/Core/Patches/Triggers/ConsolePatch.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public static bool Prefix(MonoBehaviour __instance)
3939
MinigamePatch.LastConsole = __instance.gameObject;
4040

4141
// Get Object Data
42-
var objectData = __instance.gameObject.GetComponent<MapObjectData>();
43-
if (objectData == null)
42+
var element = MapObjectDB.Get(__instance.gameObject);
43+
if (element == null)
4444
return true;
4545

4646
// Create Trigger
47-
var isClientSide = objectData.Properties.triggerClientSide ?? true;
47+
var isClientSide = element.properties.triggerClientSide ?? true;
4848
TriggerSignal signal = new(__instance.gameObject, "onUse", PlayerControl.LocalPlayer);
4949

5050
// Fire Trigger

LevelImposter/Core/Utils/GameObjectExtensions.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,6 @@ namespace LevelImposter.Core;
55

66
public static class GameObjectExtensions
77
{
8-
/// <summary>
9-
/// <c>MapObjectData</c> is appended to GameObjects that are built from <c>LIElement</c>s.
10-
/// This pulls that object data or throws an Exception if the object was not an <c>LIElement</c>.
11-
/// </summary>
12-
/// <param name="gameObject">GameObject to pull data from</param>
13-
/// <returns>The cooresponding map object data</returns>
14-
/// <exception cref="Exception">If the object was not an <c>LIElement</c></exception>
15-
public static MapObjectData GetLIData(this GameObject gameObject)
16-
{
17-
var mapObjectData = gameObject.GetComponent<MapObjectData>();
18-
if (mapObjectData == null)
19-
throw new Exception($"{gameObject} is missing LI data");
20-
return mapObjectData;
21-
}
22-
238
/// <summary>
249
/// Equivelent of <c>GameObject.GetComponent</c> but throws an exception if the component is null or missing.
2510
/// </summary>

0 commit comments

Comments
 (0)