mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-03-12 04:14:08 +00:00
added post-init event for world objects, fixes quantum state issue
This commit is contained in:
parent
7c4372dca7
commit
50efe668d3
@ -246,12 +246,12 @@ namespace QSB.QuantumSync.Patches
|
||||
|
||||
if (____sector == null)
|
||||
{
|
||||
__instance.GetType().GetMethod("CheckEnabled", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, null);
|
||||
__instance.CheckEnabled();
|
||||
}
|
||||
|
||||
if (____collapseOnStart)
|
||||
{
|
||||
__instance.GetType().GetMethod("Collapse", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { true });
|
||||
__instance.Collapse(true);
|
||||
}
|
||||
|
||||
return false;
|
||||
@ -292,7 +292,13 @@ namespace QSB.QuantumSync.Patches
|
||||
|
||||
var allMultiStates = QSBWorldSync.GetWorldObjects<QSBMultiStateQuantumObject>();
|
||||
var stateObject = QSBWorldSync.GetWorldFromUnity<QSBQuantumState, QuantumState>(__instance);
|
||||
var owner = allMultiStates.First(x => x.QuantumStates.Contains(stateObject));
|
||||
var owner = allMultiStates.FirstOrDefault(x => x.QuantumStates.Contains(stateObject));
|
||||
if (owner == default)
|
||||
{
|
||||
DebugLog.DebugWrite($"Error - Could not find QSBMultiStateQuantumObject for state {__instance.name}", MessageType.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (owner.ControllingPlayer != QSBPlayerManager.LocalPlayerId)
|
||||
{
|
||||
return;
|
||||
|
@ -11,7 +11,7 @@ namespace QSB.QuantumSync.WorldObjects
|
||||
{
|
||||
public List<QSBQuantumState> QuantumStates { get; private set; }
|
||||
public Text DebugBoxText;
|
||||
public int CurrentState => AttachedObject.GetValue<int>("_stateIndex");
|
||||
public int CurrentState => AttachedObject._stateIndex;
|
||||
|
||||
public override void OnRemoval()
|
||||
{
|
||||
@ -26,15 +26,25 @@ namespace QSB.QuantumSync.WorldObjects
|
||||
{
|
||||
ObjectId = id;
|
||||
AttachedObject = attachedObject;
|
||||
QuantumStates = AttachedObject.GetValue<QuantumState[]>("_states").ToList().Select(x => QSBWorldSync.GetWorldFromUnity<QSBQuantumState, QuantumState>(x)).ToList();
|
||||
|
||||
if (QSBCore.DebugMode)
|
||||
{
|
||||
DebugBoxText = DebugBoxManager.CreateBox(AttachedObject.transform, 0, CurrentState.ToString()).GetComponent<Text>();
|
||||
DebugBoxText = DebugBoxManager.CreateBox(AttachedObject.transform, 0, $"Multistate\r\nid:{id}\r\nstate:{CurrentState}").GetComponent<Text>();
|
||||
}
|
||||
|
||||
base.Init(attachedObject, id);
|
||||
}
|
||||
|
||||
public override void PostInit()
|
||||
{
|
||||
QuantumStates = AttachedObject._states.ToList().Select(x => QSBWorldSync.GetWorldFromUnity<QSBQuantumState, QuantumState>(x)).ToList();
|
||||
|
||||
if (QuantumStates.Any(x => x == null))
|
||||
{
|
||||
DebugLog.ToConsole($"Error - {AttachedObject.name} has one or more null QSBQuantumStates assigned!", OWML.Common.MessageType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
public void ChangeState(int newStateIndex)
|
||||
{
|
||||
if (CurrentState != -1)
|
||||
@ -43,10 +53,10 @@ namespace QSB.QuantumSync.WorldObjects
|
||||
}
|
||||
|
||||
QuantumStates[newStateIndex].SetVisible(true);
|
||||
AttachedObject.SetValue("_stateIndex", newStateIndex);
|
||||
AttachedObject._stateIndex = newStateIndex;
|
||||
if (QSBCore.DebugMode)
|
||||
{
|
||||
DebugBoxText.text = newStateIndex.ToString();
|
||||
DebugBoxText.text = $"Multistate\r\nid:{ObjectId}\r\nstate:{CurrentState}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ namespace QSB.QuantumSync.WorldObjects
|
||||
base.Init(quantumObject, id);
|
||||
if (QSBCore.DebugMode)
|
||||
{
|
||||
DebugBoxText = DebugBoxManager.CreateBox(AttachedObject.transform, 0, ObjectId.ToString()).GetComponent<Text>();
|
||||
DebugBoxText = DebugBoxManager.CreateBox(AttachedObject.transform, 0, $"Socketed\r\nid:{ObjectId}").GetComponent<Text>();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,7 @@ namespace QSB.WorldSync
|
||||
int ObjectId { get; }
|
||||
string Name { get; }
|
||||
|
||||
void PostInit();
|
||||
void OnRemoval();
|
||||
MonoBehaviour ReturnObject();
|
||||
}
|
||||
|
@ -45,23 +45,31 @@ namespace QSB.WorldSync
|
||||
|
||||
if (unityObject == null)
|
||||
{
|
||||
DebugLog.ToConsole($"Error - Trying to run GetWorldFromUnity with a null unity object! TWorldObject:{typeof(TWorldObject).Name}, TUnityObject:{typeof(TUnityObject).Name}.", MessageType.Error);
|
||||
DebugLog.ToConsole($"Error - Trying to run GetWorldFromUnity with a null unity object! TWorldObject:{typeof(TWorldObject).Name}, TUnityObject:{typeof(TUnityObject).Name}", MessageType.Error);
|
||||
return default;
|
||||
}
|
||||
|
||||
if (!QSBCore.IsInMultiplayer)
|
||||
{
|
||||
DebugLog.ToConsole($"Warning - Trying to run GetWorldFromUnity while not in multiplayer!");
|
||||
DebugLog.ToConsole($"Warning - Trying to run GetWorldFromUnity while not in multiplayer! TWorldObject:{typeof(TWorldObject).Name}, TUnityObject:{typeof(TUnityObject).Name}", MessageType.Warning);
|
||||
return default;
|
||||
}
|
||||
|
||||
if (!WorldObjectsToUnityObjects.ContainsKey(unityObject))
|
||||
{
|
||||
DebugLog.ToConsole($"Error - WorldObjectsToUnityObjects does not contain \"{unityObject.name}\"! Called from {new StackTrace().GetFrame(1).GetMethod().Name}", MessageType.Error);
|
||||
DebugLog.ToConsole($"Error - WorldObjectsToUnityObjects does not contain \"{unityObject.name}\"! TWorldObject:{typeof(TWorldObject).Name}, TUnityObject:{typeof(TUnityObject).Name}", MessageType.Error);
|
||||
return default;
|
||||
}
|
||||
|
||||
return WorldObjectsToUnityObjects[unityObject] as TWorldObject;
|
||||
var returnObject = WorldObjectsToUnityObjects[unityObject] as TWorldObject;
|
||||
|
||||
if (returnObject == default || returnObject == null)
|
||||
{
|
||||
DebugLog.ToConsole($"Error - World object for unity object {unityObject.name} is null! TWorldObject:{typeof(TWorldObject).Name}, TUnityObject:{typeof(TUnityObject).Name}", MessageType.Error);
|
||||
return default;
|
||||
}
|
||||
|
||||
return returnObject;
|
||||
}
|
||||
|
||||
public static int GetIdFromUnity<TWorldObject, TUnityObject>(TUnityObject unityObject)
|
||||
@ -121,6 +129,12 @@ namespace QSB.WorldSync
|
||||
{
|
||||
var worldObject = (TWorldObject)Activator.CreateInstance(typeof(TWorldObject));
|
||||
WorldObjects.Add(worldObject);
|
||||
if (worldObject == null)
|
||||
{
|
||||
// if this happens, god help you
|
||||
DebugLog.ToConsole($"Error - CreateWorldObject is returning a null value! This is very bad!", MessageType.Error);
|
||||
}
|
||||
|
||||
return worldObject;
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@ namespace QSB.WorldSync
|
||||
public string Name => AttachedObject == null ? "<NullObject!>" : AttachedObject.name;
|
||||
|
||||
public abstract void Init(T attachedObject, int id);
|
||||
public virtual void PostInit() { }
|
||||
public virtual void OnRemoval() { }
|
||||
public MonoBehaviour ReturnObject() => AttachedObject;
|
||||
}
|
||||
|
@ -67,9 +67,19 @@ namespace QSB.WorldSync
|
||||
}
|
||||
}
|
||||
|
||||
QSBCore.UnityEvents.FireInNUpdates(() => AllReady = true, 1);
|
||||
QSBCore.UnityEvents.FireInNUpdates(DoPostInit, 1);
|
||||
}
|
||||
|
||||
private static void DoPostInit()
|
||||
{
|
||||
AllReady = true;
|
||||
var allWorldObjects = QSBWorldSync.GetWorldObjects<IWorldObject>();
|
||||
foreach (var worldObject in allWorldObjects)
|
||||
{
|
||||
worldObject.PostInit();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void RebuildWorldObjects(OWScene scene);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user