-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f2778d
commit cbaa4f9
Showing
10 changed files
with
184 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Reflection; | ||
using UnityEngine; | ||
|
||
namespace ContentLibrary.API; | ||
|
||
/// <summary> | ||
/// Utility methods for working with asset bundles. | ||
/// </summary> | ||
public static class BundleUtils { | ||
private static MemoryStream CopyToMemoryStream(Stream stream) { | ||
using var reader = new StreamReader(stream); | ||
var ms = new MemoryStream(); | ||
|
||
reader.BaseStream.CopyTo(ms); | ||
|
||
return ms; | ||
} | ||
|
||
/// <summary> | ||
/// Loads an asset bundle from a file path. | ||
/// </summary> | ||
/// <param name="path">The path to load the asset bundle from.</param> | ||
/// <param name="gzipped">Whether the stream is gzipped.</param> | ||
/// <returns>The loaded asset bundle.</returns> | ||
public static AssetBundle? LoadAssetBundle(string path, bool gzipped = false) { | ||
return LoadAssetBundle(File.OpenRead(path), gzipped); | ||
} | ||
|
||
/// <summary> | ||
/// Loads an asset bundle from an embedded resource. | ||
/// </summary> | ||
/// <param name="assembly">The assembly to load the asset bundle from.</param> | ||
/// <param name="path">The path to load the asset bundle from.</param> | ||
/// <param name="gzipped">Whether the stream is gzipped.</param> | ||
/// <returns>The loaded asset bundle.</returns> | ||
public static AssetBundle? LoadEmbeddedAssetBundle(Assembly assembly, string path, bool gzipped = false) { | ||
var stream = assembly.GetManifestResourceStream(path) ?? | ||
throw new Exception($"Failed to load embedded resource '{path}'."); | ||
|
||
return LoadAssetBundle(stream, gzipped); | ||
} | ||
|
||
/// <summary> | ||
/// Loads an asset bundle from a stream. | ||
/// </summary> | ||
/// <param name="stream">The stream to load the asset bundle from.</param> | ||
/// <param name="gzipped">Whether the stream is gzipped.</param> | ||
/// <returns>The loaded asset bundle.</returns> | ||
public static AssetBundle? LoadAssetBundle(Stream stream, bool gzipped = false) { | ||
if (!gzipped) return AssetBundle.LoadFromStream(CopyToMemoryStream(stream)); | ||
|
||
using var gzStream = new GZipStream(stream, CompressionMode.Decompress); | ||
using var ms = CopyToMemoryStream(gzStream); | ||
|
||
return AssetBundle.LoadFromStream(ms); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
ContentLibrary/Source/ContentLibrary/API/Enemies/EnemyLoader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Linq; | ||
using UnityEngine; | ||
|
||
namespace ContentLibrary.API.Enemies; | ||
|
||
/// <summary> | ||
/// Contains methods for loading enemies. | ||
/// </summary> | ||
public class EnemyLoader { | ||
/// <summary> | ||
/// Registers an enemy. | ||
/// </summary> | ||
/// <param name="bundle">The asset bundle to load the enemy from.</param> | ||
/// <param name="asset">The asset name to load the enemy from.</param> | ||
public static void LoadEnemyFromBundle(AssetBundle bundle, string asset) { | ||
var enemy = bundle.LoadAsset<GameObject>(asset); | ||
|
||
RegisterEnemy(enemy); | ||
} | ||
|
||
/// <summary> | ||
/// Registers an enemy. | ||
/// </summary> | ||
/// <param name="asset">The enemy asset.</param> | ||
public static void RegisterEnemy(GameObject asset) { | ||
EnemyManager.RegisterEnemy(asset); | ||
} | ||
|
||
/// <summary> | ||
/// Registers all enemies in an asset bundle. | ||
/// </summary> | ||
/// <param name="bundle">The asset bundle to load enemies from.</param> | ||
public static void LoadAllEnemiesFromBundle(AssetBundle bundle) { | ||
var assets = bundle.LoadAllAssets<GameObject>().Where(v => v.GetComponent<BudgetCost>() != null); | ||
|
||
foreach (var asset in assets) RegisterEnemy(asset); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
ContentLibrary/Source/ContentLibrary/API/Enemies/EnemyManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using HarmonyLib; | ||
using UnityEngine; | ||
|
||
namespace ContentLibrary.API.Enemies; | ||
|
||
/// <summary> | ||
/// Contains methods for managing enemies. | ||
/// </summary> | ||
public static class EnemyManager { | ||
private static List<GameObject> Enemies { get; } = []; | ||
private static List<GameObject> AddedEnemies { get; } = []; | ||
|
||
/// <summary> | ||
/// Injects all registered enemies into a round spawner. | ||
/// </summary> | ||
/// <param name="spawner">The round spawner to inject enemies into.</param> | ||
public static void InjectEnemies(RoundSpawner spawner) { | ||
foreach (var enemy in Enemies.Where(enemy => !AddedEnemies.Contains(enemy))) | ||
{ | ||
spawner.possibleSpawns.AddItem(enemy); | ||
spawner.spawnBudgetCosts.AddItem(enemy.GetComponent<BudgetCost>()); | ||
AddedEnemies.Add(enemy); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Registers an enemy. | ||
/// </summary> | ||
/// <param name="enemy">The enemy to register.</param> | ||
public static void RegisterEnemy(GameObject enemy) { | ||
Enemies.Add(enemy); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
ContentLibrary/Source/ContentLibrary/Patches/RoundSpawnerPatch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using ContentLibrary.API.Enemies; | ||
using HarmonyLib; | ||
|
||
namespace ContentLibrary.Patches; | ||
|
||
/// <summary> | ||
/// Contains Harmony patches for the round spawner. | ||
/// </summary> | ||
[HarmonyPatch] | ||
public class RoundSpawnerPatch { | ||
/// <summary> | ||
/// Patches the RoundSpawner to inject custom enemies. | ||
/// </summary> | ||
/// <param name="__instance">The instance.</param> | ||
/// <returns>Whether to continue.</returns> | ||
[HarmonyPatch(typeof(RoundSpawner), nameof(RoundSpawner.Start))] | ||
[HarmonyPrefix] | ||
public static bool Start(RoundSpawner __instance) { | ||
EnemyManager.InjectEnemies(__instance); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters