Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul ModPrefabRequest and PrefabHandler to fix multi request bug #508

Merged
merged 2 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading