patches (i hate this)

This commit is contained in:
JohnCorby 2022-05-28 14:14:25 -07:00
parent 814fb5cc20
commit fb92861ed2
4 changed files with 90 additions and 59 deletions

View File

@ -12,11 +12,13 @@ internal class LightSensorManager : WorldObjectManager
public override WorldObjectScene WorldObjectScene => WorldObjectScene.Both;
public override bool DlcOnly => true;
public static bool IsPlayerLightSensor(LightSensor lightSensor) => lightSensor.name is "CameraDetector" or "REMOTE_CameraDetector";
public override async UniTask BuildWorldObjects(OWScene scene, CancellationToken ct)
{
// ignore player light sensors
var list = QSBWorldSync.GetUnityObjects<SingleLightSensor>()
.Where(x => x.name is not ("CameraDetector" or "REMOTE_CameraDetector"))
.Where(x => !IsPlayerLightSensor(x))
.SortDeterministic();
QSBWorldSync.Init<QSBLightSensor, SingleLightSensor>(list);
}

View File

@ -24,10 +24,8 @@ internal class LightSensorPatches : QSBPatch
return true;
}
if (!__instance.TryGetWorldObject(out QSBLightSensor qsbLightSensor))
{
return true;
}
var isPlayerLightSensor = LightSensorManager.IsPlayerLightSensor(__instance);
var qsbLightSensor = isPlayerLightSensor ? null : __instance.GetWorldObject<QSBLightSensor>();
if (__instance._lightDetector != null)
{
@ -67,9 +65,19 @@ internal class LightSensorPatches : QSBPatch
__instance._lightDetector.GetShape().enabled = false;
if (__instance._startIlluminated)
{
qsbLightSensor._locallyIlluminated = true;
qsbLightSensor.OnDetectLocalLight?.Invoke();
qsbLightSensor.SendMessage(new SetIlluminatedMessage(true));
if (isPlayerLightSensor)
{
var qsbPlayerLightSensor = __instance.GetComponent<QSBPlayerLightSensor>();
qsbPlayerLightSensor._locallyIlluminated = true;
new PlayerSetIlluminatedMessage(qsbPlayerLightSensor.PlayerId, true).Send();
}
else
{
qsbLightSensor._locallyIlluminated = true;
qsbLightSensor.OnDetectLocalLight?.Invoke();
qsbLightSensor.SendMessage(new SetIlluminatedMessage(true));
}
}
}
@ -85,10 +93,8 @@ internal class LightSensorPatches : QSBPatch
return true;
}
if (!__instance.TryGetWorldObject(out QSBLightSensor qsbLightSensor))
{
return true;
}
var isPlayerLightSensor = LightSensorManager.IsPlayerLightSensor(__instance);
var qsbLightSensor = isPlayerLightSensor ? null : __instance.GetWorldObject<QSBLightSensor>();
var containsAnyOccupants = __instance._sector.ContainsAnyOccupants(DynamicOccupant.Player | DynamicOccupant.Probe);
if (containsAnyOccupants && !__instance.enabled)
@ -106,11 +112,24 @@ internal class LightSensorPatches : QSBPatch
__instance._lightDetector.GetShape().enabled = false;
if (!__instance._preserveStateWhileDisabled)
{
if (qsbLightSensor._locallyIlluminated)
if (isPlayerLightSensor)
{
qsbLightSensor._locallyIlluminated = false;
qsbLightSensor.OnDetectLocalDarkness?.Invoke();
qsbLightSensor.SendMessage(new SetIlluminatedMessage(false));
var qsbPlayerLightSensor = __instance.GetComponent<QSBPlayerLightSensor>();
if (qsbPlayerLightSensor._locallyIlluminated)
{
qsbPlayerLightSensor._locallyIlluminated = false;
new PlayerSetIlluminatedMessage(qsbPlayerLightSensor.PlayerId, false).Send();
}
}
else
{
if (qsbLightSensor._locallyIlluminated)
{
qsbLightSensor._locallyIlluminated = false;
qsbLightSensor.OnDetectLocalDarkness?.Invoke();
qsbLightSensor.SendMessage(new SetIlluminatedMessage(false));
}
}
}
}
@ -132,10 +151,8 @@ internal class LightSensorPatches : QSBPatch
return true;
}
if (!__instance.TryGetWorldObject(out QSBLightSensor qsbLightSensor))
{
return true;
}
var isPlayerLightSensor = LightSensorManager.IsPlayerLightSensor(__instance);
var qsbLightSensor = isPlayerLightSensor ? null : __instance.GetWorldObject<QSBLightSensor>();
if (__instance._fixedUpdateFrameDelayCount > 0)
{
@ -150,27 +167,63 @@ internal class LightSensorPatches : QSBPatch
var illuminated = __instance._illuminated;
__instance.UpdateIllumination();
var locallyIlluminated = qsbLightSensor._locallyIlluminated;
qsbLightSensor._locallyIlluminated = __instance._illuminated;
bool locallyIlluminated;
QSBPlayerLightSensor qsbPlayerLightSensor = null;
if (isPlayerLightSensor)
{
qsbPlayerLightSensor = __instance.GetComponent<QSBPlayerLightSensor>();
locallyIlluminated = qsbPlayerLightSensor._locallyIlluminated;
qsbPlayerLightSensor._locallyIlluminated = __instance._illuminated;
}
else
{
locallyIlluminated = qsbLightSensor._locallyIlluminated;
qsbLightSensor._locallyIlluminated = __instance._illuminated;
}
__instance._illuminated = illuminated;
if (!locallyIlluminated && qsbLightSensor._locallyIlluminated)
if (isPlayerLightSensor)
{
qsbLightSensor._locallyIlluminated = true;
qsbLightSensor.OnDetectLocalLight?.Invoke();
qsbLightSensor.SendMessage(new SetIlluminatedMessage(true));
if (!locallyIlluminated && qsbPlayerLightSensor._locallyIlluminated)
{
qsbPlayerLightSensor._locallyIlluminated = true;
new PlayerSetIlluminatedMessage(qsbPlayerLightSensor.PlayerId, true).Send();
}
else if (locallyIlluminated && !qsbPlayerLightSensor._locallyIlluminated)
{
qsbPlayerLightSensor._locallyIlluminated = false;
new PlayerSetIlluminatedMessage(qsbPlayerLightSensor.PlayerId, false).Send();
}
}
else if (locallyIlluminated && !qsbLightSensor._locallyIlluminated)
else
{
qsbLightSensor._locallyIlluminated = false;
qsbLightSensor.OnDetectLocalDarkness?.Invoke();
qsbLightSensor.SendMessage(new SetIlluminatedMessage(false));
if (!locallyIlluminated && qsbLightSensor._locallyIlluminated)
{
qsbLightSensor._locallyIlluminated = true;
qsbLightSensor.OnDetectLocalLight?.Invoke();
qsbLightSensor.SendMessage(new SetIlluminatedMessage(true));
}
else if (locallyIlluminated && !qsbLightSensor._locallyIlluminated)
{
qsbLightSensor._locallyIlluminated = false;
qsbLightSensor.OnDetectLocalDarkness?.Invoke();
qsbLightSensor.SendMessage(new SetIlluminatedMessage(false));
}
}
if (__instance._illuminatingDreamLanternList != null
&& !__instance._illuminatingDreamLanternList.SequenceEqual(_prevIlluminatingDreamLanternList))
{
qsbLightSensor.SendMessage(new IlluminatingLanternsMessage(__instance._illuminatingDreamLanternList));
if (isPlayerLightSensor)
{
new PlayerIlluminatingLanternsMessage(qsbPlayerLightSensor.PlayerId, __instance._illuminatingDreamLanternList).Send();
}
else
{
qsbLightSensor.SendMessage(new IlluminatingLanternsMessage(__instance._illuminatingDreamLanternList));
}
}
return false;

View File

@ -10,17 +10,13 @@ namespace QSB.EchoesOfTheEye.LightSensorSync;
/// <summary>
/// stores a bit of extra data needed for player light sensor sync
///
///
///
///
/// todo you might be able to remove when you simplify light sensor after the fake sector thingy
/// </summary>
[RequireComponent(typeof(SingleLightSensor))]
public class QSBPlayerLightSensor : MonoBehaviour
{
public uint PlayerId;
private SingleLightSensor _lightSensor;
private PlayerInfo _player;
internal bool _locallyIlluminated;
internal readonly List<uint> _illuminatedBy = new();
@ -28,7 +24,7 @@ public class QSBPlayerLightSensor : MonoBehaviour
private void Awake()
{
_lightSensor = GetComponent<SingleLightSensor>();
_player = QSBPlayerManager.PlayerList.First(x => x.LightSensor == _lightSensor);
PlayerId = QSBPlayerManager.PlayerList.First(x => x.LightSensor == _lightSensor).PlayerId;
RequestInitialStatesMessage.SendInitialState += SendInitialState;
QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
@ -42,10 +38,10 @@ public class QSBPlayerLightSensor : MonoBehaviour
private void SendInitialState(uint to)
{
new PlayerIlluminatedByMessage(_player.PlayerId, _illuminatedBy.ToArray()) { To = to }.Send();
new PlayerIlluminatedByMessage(PlayerId, _illuminatedBy.ToArray()) { To = to }.Send();
if (_lightSensor._illuminatingDreamLanternList != null)
{
new PlayerIlluminatingLanternsMessage(_player.PlayerId, _lightSensor._illuminatingDreamLanternList) { To = to }.Send();
new PlayerIlluminatingLanternsMessage(PlayerId, _lightSensor._illuminatingDreamLanternList) { To = to }.Send();
}
}

View File

@ -269,26 +269,6 @@ public static class QSBWorldSync
return (TWorldObject)worldObject;
}
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.
/// iterates thru all objects and throws error if there isn't exactly 1.