Skip to content

Commit

Permalink
Pre Second ContentRestorer Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmBatby committed Sep 2, 2024
1 parent 3d766ca commit 7c80b3f
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 12 deletions.
6 changes: 3 additions & 3 deletions LethalLevelLoader/LethalLevelLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@

<MakeDir Directories="$(ProjectDir)../assets/bundles" Condition="!Exists('$(ProjectDir)../assets/bundles')" />

<Exec Command="dotnet tcli build --config-path $(ProjectDir)../assets/thunderstore.toml --package-version $(PlainVersion)" />
<Exec Command="dotnet tcli build --config-path &quot;$(ProjectDir)../assets/thunderstore.toml&quot; --package-version $(PlainVersion)" />

</Target>

<!-- thunderstore publish -->
<Target Name="PublishThunderstore" DependsOnTargets="SetPluginVersion;SaveVersionAndNameToFiles">
<Exec Command="dotnet tcli publish --config-path $(ProjectDir)../assets/thunderstore.toml --file $(ProjectDir)dist/*-$(MinVerVersion).zip" />
<Exec Command="dotnet tcli publish --config-path &quot;$(ProjectDir)../assets/thunderstore.toml&quot; --file &quot;$(ProjectDir)dist/*-$(MinVerVersion).zip&quot;" />
</Target>

<!-- zipping the .dll alone for curseforge release -->
<Target Name="ZipCurseForge" AfterTargets="PackThunderstore">
<Target Name="ZipCurseForge" DependsOnTargets="PackThunderstore">
<MakeDir Directories="$(ProjectDir)dist/output/" Condition="!Exists('$(ProjectDir)dist/output/')" />
<Copy SourceFiles="$(ProjectDir)bin/Release/netstandard2.1/$(AssemblyName).dll" DestinationFiles="$(ProjectDir)dist/output/$(AssemblyName).dll" />

Expand Down
6 changes: 3 additions & 3 deletions LethalLevelLoader/Patches/DungeonManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static List<ExtendedDungeonFlowWithRarity> GetValidExtendedDungeonFlows(E
debugString += "Info For ExtendedLevel: " + extendedLevel.name + " | Planet Name: " + extendedLevel.NumberlessPlanetName + " | Content Tags: ";
foreach (ContentTag tag in extendedLevel.ContentTags)
debugString += tag.contentTagName + ", ";
debugString = debugString.Remove(debugString.LastIndexOf(", "));
debugString = debugString.TrimEnd([',', ' ']);
debugString += " | Route Price: " + extendedLevel.RoutePrice + " | Current Weather: " + extendedLevel.SelectableLevel.currentWeather.ToString();
debugString += "\n";

Expand All @@ -74,15 +74,15 @@ public static List<ExtendedDungeonFlowWithRarity> GetValidExtendedDungeonFlows(E
foreach (ExtendedDungeonFlowWithRarity extendedDungeonFlowWithRarity in potentialExtendedDungeonFlowsList)
if (!viableDungeonFlows.Contains(extendedDungeonFlowWithRarity.extendedDungeonFlow))
debugString += extendedDungeonFlowWithRarity.extendedDungeonFlow.DungeonName + ", ";
debugString = debugString.Remove(debugString.LastIndexOf(", "));
debugString = debugString.TrimEnd([',', ' ']);
debugString += "\n";

returnExtendedDungeonFlowsList = returnExtendedDungeonFlowsList.OrderBy(e => e.rarity).Reverse().ToList();

debugString += "Viable ExtendedDungeonFlows: ";
foreach (ExtendedDungeonFlowWithRarity extendedDungeonFlowWithRarity in returnExtendedDungeonFlowsList)
debugString += extendedDungeonFlowWithRarity.extendedDungeonFlow.DungeonName + " (" + extendedDungeonFlowWithRarity.rarity + ")" + ", ";
debugString = debugString.Remove(debugString.LastIndexOf(", "));
debugString = debugString.TrimEnd([',', ' ']);

DebugHelper.Log(debugString + "\n", DebugType.User);
}
Expand Down
13 changes: 8 additions & 5 deletions LethalLevelLoader/Tools/ConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static List<StringWithRarity> ConvertToStringWithRarityList(string newInp
rarity = value;

if (clampRarity != Vector2.zero)
Math.Clamp(rarity, Mathf.RoundToInt(clampRarity.x), Mathf.RoundToInt(clampRarity.y));
rarity = Math.Clamp(rarity, Mathf.RoundToInt(clampRarity.x), Mathf.RoundToInt(clampRarity.y));

returnList.Add(new StringWithRarity(levelName, rarity));
}
Expand Down Expand Up @@ -62,7 +62,7 @@ public static List<Vector2WithRarity> ConvertToVector2WithRarityList(string newI
rarity = value;

if (clampRarity != Vector2.zero)
Math.Clamp(rarity, Mathf.RoundToInt(clampRarity.x), Mathf.RoundToInt(clampRarity.y));
rarity = Math.Clamp(rarity, Mathf.RoundToInt(clampRarity.x), Mathf.RoundToInt(clampRarity.y));

returnList.Add(new Vector2WithRarity(new Vector2(x,y), rarity));
}
Expand All @@ -74,8 +74,9 @@ public static List<SpawnableEnemyWithRarity> ConvertToSpawnableEnemyWithRarityLi
List<StringWithRarity> stringList = ConvertToStringWithRarityList(newInputString, clampRarity);
List<SpawnableEnemyWithRarity> returnList = new List<SpawnableEnemyWithRarity>();

foreach (EnemyType enemyType in OriginalContent.Enemies.Concat(PatchedContent.Enemies))
foreach (ExtendedEnemyType extendedEnemyType in PatchedContent.ExtendedEnemyTypes)
{
EnemyType enemyType = extendedEnemyType.EnemyType;
foreach (StringWithRarity stringString in new List<StringWithRarity>(stringList))
{
if (enemyType.enemyName.ToLower().Contains(stringString.Name.ToLower()))
Expand All @@ -90,8 +91,9 @@ public static List<SpawnableEnemyWithRarity> ConvertToSpawnableEnemyWithRarityLi
}

//Incase the user put in the real name (eg. Bracken) instead of the internal name (Flowerman) we go through the scannode texts which has the more updated name.
foreach (EnemyType enemyType in OriginalContent.Enemies.Concat(PatchedContent.Enemies))
foreach (ExtendedEnemyType extendedEnemyType in PatchedContent.ExtendedEnemyTypes)
{
EnemyType enemyType = extendedEnemyType.EnemyType;
foreach (StringWithRarity stringString in new List<StringWithRarity>(stringList))
{
if (enemyType.enemyPrefab != null)
Expand Down Expand Up @@ -119,8 +121,9 @@ public static List<SpawnableItemWithRarity> ConvertToSpawnableItemWithRarityList
List<StringWithRarity> stringList = ConvertToStringWithRarityList(newInputString, clampRarity);
List<SpawnableItemWithRarity> returnList = new List<SpawnableItemWithRarity>();

foreach (Item item in OriginalContent.Items.Concat(PatchedContent.Items))
foreach (ExtendedItem extendedItem in PatchedContent.ExtendedItems)
{
Item item = extendedItem.Item;
foreach (StringWithRarity stringString in new List<StringWithRarity>(stringList))
{
if (SanitizeString(item.itemName).Contains(SanitizeString(stringString.Name)) || SanitizeString(stringString.Name).Contains(SanitizeString(item.itemName)))
Expand Down
163 changes: 163 additions & 0 deletions LethalLevelLoader/Tools/ContentRestore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.Rendering;
using Object = UnityEngine.Object;

namespace LethalLevelLoader.Tools
{
public abstract class ContentRestore
{
public abstract void Flush();
}
public abstract class ContentRestore<T> : ContentRestore
{
protected List<T> restoredContentDestroyList = new List<T>();

// List Of New Content, List Of Original Comparisons
public virtual void TryRestoreContents(List<T> originalContents, ref List<T> newContents, bool debugAction = false, bool destroyOnRestore = true)
{
for (int i = 0; i < newContents.Count; i++)
newContents[i] = TryRestoreContent(newContents[i], originalContents, debugAction, destroyOnRestore);
}

// 1 New Content : List Of Orignal Comparisons
public virtual T TryRestoreContent(T newContent, List<T> originalContents, bool debugAction = false, bool destroyOnRestore = true)
{
if (newContent == null) return newContent;

for (int i = 0; i < originalContents.Count; i++)
if (originalContents[i] != null && CompareContent(originalContents[i], newContent))
return (RestoreContent(originalContents[i], newContent, debugAction, destroyOnRestore));

return (newContent);
}

// 1 New Content : 1 Original Content Comparison
public virtual T TryRestoreContent(T originalContent, T newContent, bool debugAction = false, bool destroyOnReplace = true)
{
if (originalContent == null || newContent == null) return originalContent;

if (CompareContent(originalContent, newContent))
return (RestoreContent(originalContent, newContent, debugAction, destroyOnReplace));
else
return (originalContent);
}

public abstract bool CompareContent(T originalContent, T newContent);

protected virtual T RestoreContent(T originalContent, T newContent, bool debugAction = false, bool destroyOnReplace = true)
{
if (originalContent != null && newContent != null)
{
if (debugAction == true && originalContent.ToString() != null)
DebugHelper.Log("Restoring " + originalContent.GetType().ToString() + ": Old Asset Name: " + originalContent + " , New Asset Name: ", DebugType.Developer);

if (destroyOnReplace == true)
if (!restoredContentDestroyList.Contains(originalContent))
restoredContentDestroyList.Add(originalContent);
}
else
DebugHelper.LogWarning("Asset Restoration Failed, Null Reference Found!", DebugType.Developer);
return (newContent);
}
}

public class UnityContentRestore<T> : ContentRestore<T> where T : Object
{
public override bool CompareContent(T originalContent, T newContent)
{
if (originalContent.name != null && newContent.name != null)
return (originalContent.name == newContent.name);
else
return (false);
}

public override void Flush()
{
for (int i = 0; i < restoredContentDestroyList.Count; i++)
Object.Destroy(restoredContentDestroyList[i]);
restoredContentDestroyList.Clear();
}
}

public class ItemRestore : UnityContentRestore<Item>
{
public void TryRestoreContents(List<SpawnableItemWithRarity> newContents, List<Item> originalContents, bool debugAction = false, bool destroyOnRestore = true)
{
for (int i = 0; i < newContents.Count; i++)
newContents[i].spawnableItem = TryRestoreContent(newContents[i].spawnableItem, originalContents, debugAction, destroyOnRestore);
}
}

public class EnemyRestore : UnityContentRestore<EnemyType>
{
public void TryRestoreContents(List<SpawnableEnemyWithRarity> newContents, List<EnemyType> originalContents, bool debugAction = false, bool destroyOnRestore = true)
{
for (int i = 0; i < newContents.Count; i++)
newContents[i].enemyType = TryRestoreContent(newContents[i].enemyType, originalContents, debugAction, destroyOnRestore);
}
}

public class SpawnableMapObjectRestore : UnityContentRestore<GameObject>
{
public void TryRestoreContents(SpawnableMapObject[] newContents, List<GameObject> originalContents, bool debugAction = false, bool destroyOnRestore = true)
{
for (int i = 0; i < newContents.Length; i++)
newContents[i].prefabToSpawn = TryRestoreContent(newContents[i].prefabToSpawn, originalContents, debugAction, destroyOnRestore);
}

public void TryRestoreContents(List<RandomMapObject> newContents, List<GameObject> originalContents, bool debugAction = false, bool destroyOnRestore = true)
{
for (int i = 0; i < newContents.Count; i++)
for (int j = 0; j < newContents[i].spawnablePrefabs.Count; j++)
newContents[i].spawnablePrefabs[j] = TryRestoreContent(newContents[i].spawnablePrefabs[j], originalContents, debugAction, destroyOnRestore);
}

public void TryRestoreContents(RandomMapObject newContent, List<GameObject> originalContents, bool debugAction = false, bool destroyOnRestore = true)
{
for (int j = 0; j < newContent.spawnablePrefabs.Count; j++)
newContent.spawnablePrefabs[j] = TryRestoreContent(newContent.spawnablePrefabs[j], originalContents, debugAction, destroyOnRestore);
}
}

public class SpawnableOutsideObjectRestore : UnityContentRestore<GameObject>
{
public void TryRestoreContents(SpawnableOutsideObjectWithRarity[] newContents, List<SpawnableOutsideObject> originalContents, bool debugAction = false, bool destroyOnRestore = true)
{
List<GameObject> prefabs = originalContents.Select(p => p.prefabToSpawn).ToList();
for (int i = 0; i < newContents.Length; i++)
newContents[i].spawnableObject.prefabToSpawn = TryRestoreContent(newContents[i].spawnableObject.prefabToSpawn, prefabs, debugAction, destroyOnRestore);
}
}

public class ItemGroupRestore : UnityContentRestore<ItemGroup>
{
public void TryRestoreContents(List<RandomScrapSpawn> newContents, List<ItemGroup> originalContents, bool debugAction = false, bool destroyOnRestore = true)
{
for (int i = 0; i < newContents.Count; i++)
newContents[i].spawnableItems = TryRestoreContent(newContents[i].spawnableItems, originalContents, debugAction, destroyOnRestore);
}
}

public class ReverbPresetRestore : UnityContentRestore<ReverbPreset>
{
public void TryRestoreContents(AudioReverbTrigger[] newContents, List<ReverbPreset> originalContents, bool debugAction = false, bool destroyOnRestore = true )
{
for (int i = 0; i < newContents.Length; i++)
newContents[i].reverbPreset = TryRestoreContent(newContents[i].reverbPreset, originalContents, debugAction, destroyOnRestore);
}
}

public class AudioMixerGroupRestore : UnityContentRestore<AudioMixerGroup>
{
public void TryRestoreContents(AudioSource[] newContents, List<AudioMixerGroup> originalContents, bool debugAction = false, bool destroyOnRestore = true)
{
for (int i = 0; i < newContents.Length; i++)
newContents[i].outputAudioMixerGroup = TryRestoreContent(newContents[i].outputAudioMixerGroup, originalContents, debugAction, destroyOnRestore);
}
}
}
81 changes: 81 additions & 0 deletions LethalLevelLoader/Tools/NewContentRestorer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using DunGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Audio;
using Object = UnityEngine.Object;

namespace LethalLevelLoader.Tools
{
public static class NewContentRestorer
{
public static ItemRestore ItemRestore { get; private set; } = new ItemRestore();
public static EnemyRestore EnemyRestore { get; private set; } = new EnemyRestore();
public static SpawnableMapObjectRestore SpawnableMapObjectRestore { get; private set; } = new SpawnableMapObjectRestore();
public static SpawnableOutsideObjectRestore SpawnableOutsideObjectRestore { get; private set; } = new SpawnableOutsideObjectRestore();
public static UnityContentRestore<LevelAmbienceLibrary> LevelAmbienceRestore { get; private set; } = new UnityContentRestore<LevelAmbienceLibrary>();
public static ItemGroupRestore ItemGroupRestore { get; private set; } = new ItemGroupRestore();

public static AudioMixerGroupRestore AudioMixerGroupRestore { get; private set; } = new AudioMixerGroupRestore();
public static ReverbPresetRestore ReverbPresetRestore { get; private set; } = new ReverbPresetRestore();

internal static List<ContentRestore> ContentRestores = new List<Tools.ContentRestore>()
{
ItemRestore, EnemyRestore, SpawnableMapObjectRestore, SpawnableOutsideObjectRestore, LevelAmbienceRestore,
ItemGroupRestore, AudioMixerGroupRestore, ReverbPresetRestore
};

internal static void RestoreVanillaLevelAssetReferences(ExtendedLevel extendedLevel)
{
SelectableLevel selectableLevel = extendedLevel.SelectableLevel;

foreach (SpawnableItemWithRarity spawnableItem in new List<SpawnableItemWithRarity>(selectableLevel.spawnableScrap))
if (spawnableItem.spawnableItem == null)
selectableLevel.spawnableScrap.Remove(spawnableItem);

ItemRestore.TryRestoreContents(selectableLevel.spawnableScrap, OriginalContent.Items);

EnemyRestore.TryRestoreContents(selectableLevel.Enemies, OriginalContent.Enemies);
EnemyRestore.TryRestoreContents(selectableLevel.DaytimeEnemies, OriginalContent.Enemies);
EnemyRestore.TryRestoreContents(selectableLevel.OutsideEnemies, OriginalContent.Enemies);

SpawnableMapObjectRestore.TryRestoreContents(selectableLevel.spawnableMapObjects, OriginalContent.SpawnableMapObjects);

SpawnableOutsideObjectRestore.TryRestoreContents(selectableLevel.spawnableOutsideObjects, OriginalContent.SpawnableOutsideObjects);

selectableLevel.levelAmbienceClips = LevelAmbienceRestore.TryRestoreContent(selectableLevel.levelAmbienceClips, OriginalContent.LevelAmbienceLibraries);
}

internal static void RestoreVanillaInteriorAssetReferences(ExtendedDungeonFlow extendedDungeonFlow)
{
foreach (Tile tile in extendedDungeonFlow.DungeonFlow.GetTiles())
RestoreVanillaTileAssetReferences(tile);
}

internal static void RestoreVanillaTileAssetReferences(Tile tile)
{
foreach (RandomScrapSpawn spawn in tile.GetComponentsInChildren<RandomScrapSpawn>())
spawn.spawnableItems = ItemGroupRestore.TryRestoreContent(spawn.spawnableItems, OriginalContent.ItemGroups);

foreach (RandomMapObject spawn in tile.GetComponentsInChildren<RandomMapObject>())
SpawnableMapObjectRestore.TryRestoreContents(spawn, OriginalContent.SpawnableMapObjects);
}

internal static void RestoreVanillaParentAudioAssetReferences(GameObject parent)
{
AudioSource[] allAudioSources = parent.GetComponentsInChildren<AudioSource>(true);
AudioReverbTrigger[] allReverbTriggers = parent.GetComponentsInChildren<AudioReverbTrigger>(true);

AudioMixerGroupRestore.TryRestoreContents(allAudioSources, OriginalContent.AudioMixerGroups, destroyOnRestore: false);
ReverbPresetRestore.TryRestoreContents(allReverbTriggers, OriginalContent.ReverbPresets);
}

internal static void FlushAll()
{
foreach (ContentRestore contentRestore in ContentRestores)
contentRestore.Flush();
}
}
}
1 change: 0 additions & 1 deletion assets/thunderstore.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ containsNsfwContent = false

[package.dependencies]
BepInEx-BepInExPack = "5.4.2100"
Evaisa-LethalLib = "0.16.0"
Evaisa-FixPluginTypesSerialization = "1.1.1"
MaxWasUnavailable-LethalModDataLib = "1.2.2"

Expand Down

0 comments on commit 7c80b3f

Please sign in to comment.