Skip to content

Commit

Permalink
Overhaul ModPrefabRequest and PrefabHandler to fix multi request bug (#…
Browse files Browse the repository at this point in the history
…508)

* Overhaul ModPrefabRequest and PrefabHandler to fix multi request bug
  • Loading branch information
MrPurple6411 authored Dec 17, 2023
1 parent f006d4e commit 62fc980
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
1 change: 1 addition & 0 deletions Nautilus/Assets/ModPrefabCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public void RemoveCachedPrefab(string classId)
{
if(!prefab.IsPrefab())
Destroy(prefab);
InternalLogger.Debug($"ModPrefabCache: removed prefab {classId}");
Entries.Remove(classId);
}
}
Expand Down
29 changes: 16 additions & 13 deletions Nautilus/Assets/ModPrefabRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ModPrefabRequest(PrefabInfo prefabInfo)

private void Init()
{
if (task != null)
if ((task != null && !Done) || (Done && TryGetPrefab(out _)))
{
return;
}
Expand All @@ -45,34 +45,37 @@ public object Current
get
{
Init();
return task;
return task.Current;
}
}

public bool TryGetPrefab(out GameObject result)
{
result = taskResult.Get();
if (!Done)
{
Done = result;
}
return result != null;
return ModPrefabCache.TryGetPrefabFromCache(prefabInfo.ClassID, out result) && result != null;
}

public bool MoveNext()
{
Init();
if (task == null)
if (!task.MoveNext())
{
return false;
Done = true;
}

return !TryGetPrefab(out _);
return !Done;
}

public void Reset() {}
public void Reset()
{
Init();
task.Reset();
Done = false;
}

public void Release()
{
ModPrefabCache.RemovePrefabFromCache(prefabInfo.ClassID);
taskResult = null;
task = null;
Done = false;
}
}
12 changes: 6 additions & 6 deletions Nautilus/Handlers/PrefabHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ internal static IEnumerator GetPrefabAsync(TaskResult<GameObject> gameObject, Pr
yield break;
}

yield return prefabFactory(gameObject);

yield return ProcessPrefabAsync(gameObject, info, prefabFactory);
yield return InitPrefabAsync(gameObject, info, prefabFactory);
}

private static IEnumerator ProcessPrefabAsync(TaskResult<GameObject> gameObject, PrefabInfo info, PrefabFactoryAsync prefabFactory)
private static IEnumerator InitPrefabAsync(TaskResult<GameObject> gameObject, PrefabInfo info, PrefabFactoryAsync prefabFactory)
{
yield return prefabFactory(gameObject);

var obj = gameObject.Get();
if(obj == null)
{
InternalLogger.Error($"PrefabHandler: Tried to process a null prefab.");
InternalLogger.Error($"PrefabHandler: PrefabFactory returned null for {info.ClassID}");
yield break;
}
obj.SetActive(false);

var techType = info.TechType;
var classId = info.ClassID;
Expand Down Expand Up @@ -76,7 +77,6 @@ private static IEnumerator ProcessPrefabAsync(TaskResult<GameObject> gameObject,
if (Prefabs.TryGetPostProcessorForInfo(info, out var postProcessor))
yield return postProcessor?.Invoke(obj);


gameObject.Set(obj);
ModPrefabCache.AddPrefab(obj);
}
Expand Down
7 changes: 1 addition & 6 deletions Nautilus/Patchers/PrefabDatabasePatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,7 @@ private static IPrefabRequest GetModPrefabAsync(string classId)
}

if(ModPrefabCache.Requests.TryGetValue(prefabInfo.ClassID, out var request))
{
if (request.Done && !request.TryGetPrefab(out _))
{
return new ModPrefabRequest(prefabInfo);
}

{
return request;
}

Expand Down

0 comments on commit 62fc980

Please sign in to comment.