TryGetWorldObject

This commit is contained in:
JohnCorby 2022-05-26 21:27:55 -07:00
parent 423a554f7a
commit 256a0e350b
2 changed files with 21 additions and 4 deletions

View File

@ -282,12 +282,12 @@ internal class ShipPatches : QSBPatch
return;
}
if (!__instance.HasWorldObject())
if (!__instance.TryGetWorldObject(out QSBShipLight qsbShipLight))
{
return;
}
__instance.GetWorldObject<QSBShipLight>().SendMessage(new ShipLightMessage(on));
qsbShipLight.SendMessage(new ShipLightMessage(on));
}
[HarmonyPrefix]

View File

@ -269,8 +269,25 @@ public static class QSBWorldSync
return (TWorldObject)worldObject;
}
public static bool HasWorldObject(this MonoBehaviour unityObject)
=> unityObject && UnityObjectsToWorldObjects.ContainsKey(unityObject);
public static bool TryGetWorldObject<TWorldObject>(this MonoBehaviour unityObject, out TWorldObject worldObject)
where TWorldObject : IWorldObject
{
if (!unityObject)
{
DebugLog.ToConsole($"Error - Trying to run GetWorldFromUnity with a null unity object! TWorldObject:{typeof(TWorldObject).Name}, TUnityObject:NULL, Stacktrace:\r\n{Environment.StackTrace}", MessageType.Error);
worldObject = default;
return false;
}
if (!UnityObjectsToWorldObjects.TryGetValue(unityObject, out var iWorldObject))
{
worldObject = default;
return false;
}
worldObject = (TWorldObject)iWorldObject;
return true;
}
/// <summary>
/// not deterministic across platforms.