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

Prefab cache now resets #544

Merged
merged 3 commits into from
Apr 25, 2024
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
43 changes: 39 additions & 4 deletions Nautilus/Assets/ModPrefabCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static bool IsPrefabCached(string classId)
/// Any prefab with the matching <paramref name="classId"/> will be removed from the cache.
/// </summary>
/// <param name="classId">The class id of the prefab that will be removed.</param>
/// <remarks>This operation is extremely dangerous on custom prefabs that are directly registering an asset bundle prefab as it may make the prefab unusable in the current session.<br/>Avoid using this method unless you know what you're doing.</remarks>
public static void RemovePrefabFromCache(string classId)
{
if(_cacheInstance == null)
Expand Down Expand Up @@ -85,7 +86,9 @@ private void Awake()

gameObject.AddComponent<SceneCleanerPreserve>();
DontDestroyOnLoad(gameObject);

SaveUtils.RegisterOnQuitEvent(ModPrefabCache.RunningPrefabs.Clear);
SaveUtils.RegisterOnQuitEvent(RemoveFakePrefabs);
}

public void EnterPrefabIntoCache(GameObject prefab)
Expand Down Expand Up @@ -122,12 +125,44 @@ public void EnterPrefabIntoCache(GameObject prefab)

public void RemoveCachedPrefab(string classId)
{
if (Entries.TryGetValue(classId, out var prefab))
if (!Entries.TryGetValue(classId, out var prefab))
{
if(!prefab.IsPrefab())
Destroy(prefab);
InternalLogger.Debug($"ModPrefabCache: removed prefab {classId}");
return;
}

if (!prefab)
{
InternalLogger.Debug($"ModPrefabCache: Prefab for '{classId}' is null; removing entry.");
Entries.Remove(classId);
return;
}

if (!prefab.IsPrefab())
{
Destroy(prefab);
}

InternalLogger.Debug($"ModPrefabCache: removing prefab '{classId}'");
Entries.Remove(classId);
}

private void RemoveFakePrefabs()
{
foreach (var prefab in new Dictionary<string, GameObject>(Entries))
{
if (prefab.Value.Exists() is null)
{
Entries.Remove(prefab.Key);
continue;
}

if (prefab.Value.IsPrefab())
{
continue;
}

Destroy(prefab.Value);
Entries.Remove(prefab.Key);
}
}

Expand Down
4 changes: 3 additions & 1 deletion Nautilus/Extensions/GameObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Nautilus.Utility;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;

namespace Nautilus.Extensions;
Expand Down Expand Up @@ -136,9 +137,10 @@ public static Transform SearchChild(this Transform transform, string name)
/// </summary>
/// <param name="gameObject">The game object to check.</param>
/// <returns>True if this game object is a proper prefab, otherwise false.</returns>
/// <exception cref="System.NullReferenceException"><paramref name="gameObject"/> is null.</exception>
public static bool IsPrefab(this GameObject gameObject)
{
return gameObject.transform.parent == null && !gameObject.activeInHierarchy && gameObject.activeSelf;
return gameObject.scene.name is null && gameObject.scene.loadingState is Scene.LoadingState.NotLoaded;
}

/// <summary>
Expand Down
Loading