Skip to content

Commit

Permalink
Merge branch 'master' into docs
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeTwentyThree committed May 6, 2024
2 parents 4247854 + a36127f commit a1d3154
Show file tree
Hide file tree
Showing 17 changed files with 12,462 additions and 37 deletions.
32 changes: 32 additions & 0 deletions Nautilus/Assets/CustomPrefabExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Nautilus.Utility;

namespace Nautilus.Assets;

/// <summary>
/// Represents extension methods for the <see cref="CustomPrefab"/> class.
/// </summary>
public static class CustomPrefabExtensions
{
/// <summary>
/// Removes the current prefab from the prefab cache and doesn't allow it to get cached later.
/// </summary>
/// <param name="customPrefab">The custom prefab to remove from the prefab cache.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static ICustomPrefab RemoveFromCache(this CustomPrefab customPrefab)
{
if (customPrefab.Info == default)
{
return customPrefab;
}

if (string.IsNullOrWhiteSpace(customPrefab.Info.ClassID))
{
InternalLogger.Error($"Couldn't remove prefab '{customPrefab.Info}' from cache because the class ID is null.");
return customPrefab;
}

ModPrefabCache.RemovePrefabFromCache(customPrefab.Info.ClassID);

return customPrefab;
}
}
64 changes: 57 additions & 7 deletions Nautilus/Assets/ModPrefabCache.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Nautilus.Utility;
using System.Collections.Generic;
using Nautilus.Extensions;
using Nautilus.Handlers;
using UnityEngine;

namespace Nautilus.Assets;
Expand All @@ -16,7 +17,7 @@ public static class ModPrefabCache
private static ModPrefabCacheInstance _cacheInstance;

/// <summary> Adds the given prefab to the cache. </summary>
/// <param name="prefab"> The prefab object that is disabled and cached. </param>
/// <param name="prefab"> The prefab object that is disabled and cached.</param>
public static void AddPrefab(GameObject prefab)
{
EnsureCacheExists();
Expand All @@ -38,10 +39,15 @@ 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)
if (_cacheInstance == null)
{
InternalLogger.Debug($"Removed '{classId}' from prefab cache.");
ModPrefabCacheInstance.BannedPrefabs.Add(classId);
return;
}

_cacheInstance.RemoveCachedPrefab(classId);
}
Expand Down Expand Up @@ -73,7 +79,10 @@ private static void EnsureCacheExists()

internal class ModPrefabCacheInstance: MonoBehaviour
{
public Dictionary<string, GameObject> Entries { get; } = new Dictionary<string, GameObject>();
public Dictionary<string, GameObject> Entries { get; } = new();

// Prefabs that are banned from getting cached
internal static readonly HashSet<string> BannedPrefabs = new();

private Transform _prefabRoot;

Expand All @@ -85,7 +94,9 @@ private void Awake()

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

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

public void EnterPrefabIntoCache(GameObject prefab)
Expand All @@ -98,6 +109,11 @@ public void EnterPrefabIntoCache(GameObject prefab)
return;
}

if (BannedPrefabs.Contains(prefabIdentifier.classId))
{
return;
}

if (!Entries.ContainsKey(prefabIdentifier.classId))
{
Entries.Add(prefabIdentifier.classId, prefab);
Expand All @@ -122,12 +138,46 @@ public void EnterPrefabIntoCache(GameObject prefab)

public void RemoveCachedPrefab(string classId)
{
if (Entries.TryGetValue(classId, out var prefab))
BannedPrefabs.Add(classId);

if (!Entries.TryGetValue(classId, out var prefab))
{
return;
}

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

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

Entries.Remove(classId);
InternalLogger.Debug($"ModPrefabCache: removing prefab '{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
3 changes: 2 additions & 1 deletion Nautilus/Assets/ModPrefabRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public object Current

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

public bool MoveNext()
Expand All @@ -66,6 +66,7 @@ public bool MoveNext()
public void Reset()
{
task.Reset();
taskResult.Set(null);
Done = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public AtmosphereVolumeTemplate(PrefabInfo info, VolumeShape shape, string overr
public override IEnumerator GetPrefabAsync(TaskResult<GameObject> gameObject)
{
var prefab = new GameObject(info.ClassID);
prefab.SetActive(false);
prefab.layer = AtmosphereVolumesLayer;

Collider collider = Shape switch
Expand Down
12 changes: 7 additions & 5 deletions Nautilus/Assets/PrefabTemplates/CloneTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public override IEnumerator GetPrefabAsync(TaskResult<GameObject> gameObject)
GameObject obj = gameObject.Get();
if (obj)
{
obj.SetActive(false);
ApplySkin(obj);
ModifyPrefab?.Invoke(obj);
if(ModifyPrefabAsync is { })
Expand All @@ -89,12 +90,13 @@ public override IEnumerator GetPrefabAsync(TaskResult<GameObject> gameObject)

if (_spawnType == SpawnType.TechType)
{
yield return CraftData.InstantiateFromPrefabAsync(_techTypeToClone, gameObject);
obj = gameObject.Get();
var task = CraftData.GetPrefabForTechTypeAsync(_techTypeToClone);
yield return task;
obj = Utils.InstantiateDeactivated(task.GetResult());
}
else if(_spawnType == SpawnType.Prefab)
{
var task = _prefabToClone.InstantiateAsync();
var task = _prefabToClone.LoadAssetAsync();
yield return task;

if (task.Status != UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationStatus.Succeeded)
Expand All @@ -103,7 +105,7 @@ public override IEnumerator GetPrefabAsync(TaskResult<GameObject> gameObject)
yield break;
}

obj = task.Result;
obj = Utils.InstantiateDeactivated(task.Result);
}
else if(_spawnType == SpawnType.ClassId)
{
Expand All @@ -116,7 +118,7 @@ public override IEnumerator GetPrefabAsync(TaskResult<GameObject> gameObject)
yield break;
}

obj = Object.Instantiate(prefab);
obj = Utils.InstantiateDeactivated(prefab);
}

ApplySkin(obj);
Expand Down
2 changes: 2 additions & 0 deletions Nautilus/Assets/PrefabTemplates/EggTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public override IEnumerator GetPrefabAsync(TaskResult<GameObject> gameObject)
var obj = gameObject.Get();
if (obj)
{
obj.SetActive(false);
yield return ProcessEgg(obj);
yield break;
}
Expand All @@ -231,6 +232,7 @@ public override IEnumerator GetPrefabAsync(TaskResult<GameObject> gameObject)
""");
yield break;
}
obj.SetActive(false);
yield return ProcessEgg(obj);

gameObject.Set(obj);
Expand Down
3 changes: 2 additions & 1 deletion Nautilus/Assets/PrefabTemplates/EnergySourceTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public override IEnumerator GetPrefabAsync(TaskResult<GameObject> gameObject)
var obj = gameObject.Get();
if (obj)
{
obj.SetActive(false);
ApplyModifications(obj);
yield break;
}
Expand All @@ -68,7 +69,7 @@ private IEnumerator CreateEnergySource(IOut<GameObject> gameObject)
var task = CraftData.GetPrefabForTechTypeAsync(tt, false);
yield return task;

var obj = GameObject.Instantiate(task.GetResult());
var obj = UWE.Utils.InstantiateDeactivated(task.GetResult());

yield return ApplyModifications(obj);

Expand Down
2 changes: 1 addition & 1 deletion Nautilus/Assets/PrefabTemplates/FabricatorTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private IEnumerator CreateFabricator(IOut<GameObject> gameObject)
InternalLogger.Error($"Failed to get prefab for {FabricatorModel}!!!!!!!! PLEASE REPORT THIS BUG TO THE NAUTILUS TEAM!");
}

var obj = GameObject.Instantiate(prefab);
var obj = Utils.InstantiateDeactivated(prefab);
yield return ApplyCrafterPrefab(obj);
gameObject.Set(obj);
}
Expand Down
21 changes: 16 additions & 5 deletions Nautilus/Crafting/TabNode.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace Nautilus.Crafting;

using Nautilus.Assets;
using Nautilus.Patchers;
using Assets;
using Handlers;
using Utility;

#if SUBNAUTICA
using Sprite = Atlas.Sprite;
Expand All @@ -15,15 +16,25 @@ internal class TabNode : Node
internal Sprite Sprite { get; set; }
internal string DisplayName { get; set; }
internal string Name { get; set; }
internal string Id { get; }

internal TabNode(string[] path, CraftTree.Type scheme, Sprite sprite, string name, string displayName, string language = "English") : base(path, scheme)
internal TabNode(string[] path, CraftTree.Type scheme, Sprite sprite, string name, string displayName) : base(path, scheme)
{
Sprite = sprite;
DisplayName = displayName;
Name = name;
Id = $"{Scheme.ToString()}_{Name}";

ModSprite.Add(new ModSprite(SpriteManager.Group.Category, $"{Scheme.ToString()}_{Name}", Sprite));
LanguagePatcher.AddCustomLanguageLine($"{Scheme.ToString()}Menu_{Name}", DisplayName, language);
ModSprite.Add(new ModSprite(SpriteManager.Group.Category, Id, Sprite));

if (!string.IsNullOrEmpty(displayName))
{
LanguageHandler.SetLanguageLine(Id, displayName);
}
else if (string.IsNullOrEmpty(Language.main.Get(name)))
{
InternalLogger.Warn($"Display name was not specified and no existing language line has been found for Tab node '{name}'.");
}
}

}
Loading

0 comments on commit a1d3154

Please sign in to comment.