Skip to content

Commit

Permalink
fix: Fixed issue when yield returning same request (#498)
Browse files Browse the repository at this point in the history
* Fixed issue when yield returning same request

The issue occurs because when you yield return the same request multiple times, the state machine advances but it will not wait until the code is completely executed.

* Fixed missing reference when a prefab unloads

* Better access modifiers
  • Loading branch information
Metious committed Dec 2, 2023
1 parent 75d926f commit 8f266ca
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Nautilus/Assets/ModPrefabCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void EnterPrefabIntoCache(GameObject prefab)
if(!Entries.ContainsKey(prefabIdentifier.classId))
{
Entries.Add(prefabIdentifier.classId, prefab);
InternalLogger.Debug($"ModPrefabCache: adding prefab {prefab}");
InternalLogger.Debug($"ModPrefabCache: added prefab {prefab}");
}
else // this should never happen
{
Expand Down
19 changes: 15 additions & 4 deletions Nautilus/Assets/ModPrefabRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
namespace Nautilus.Assets;

// request for getting ModPrefab asynchronously
internal class ModPrefabRequest: IPrefabRequest, IEnumerator
internal class ModPrefabRequest: IPrefabRequest
{
internal bool Done { get; private set; }

private readonly PrefabInfo prefabInfo;

private int state = 0;

private CoroutineTask<GameObject> task;

private TaskResult<GameObject> taskResult;

public ModPrefabRequest(PrefabInfo prefabInfo)
Expand Down Expand Up @@ -50,13 +52,22 @@ public object Current
public bool TryGetPrefab(out GameObject result)
{
result = taskResult.Get();
if (!Done)
{
Done = result;
}
return result != null;
}

public bool MoveNext()
{
Init();
return state++ == 0;
if (task == null)
{
return false;
}

return !TryGetPrefab(out _);
}

public void Reset() {}
Expand Down
7 changes: 7 additions & 0 deletions Nautilus/Patchers/PrefabDatabasePatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ 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;
}

return new ModPrefabRequest(prefabInfo);
}
Expand Down

0 comments on commit 8f266ca

Please sign in to comment.