Skip to content

Commit

Permalink
i am so tired
Browse files Browse the repository at this point in the history
  • Loading branch information
RedstoneWizard08 committed Sep 24, 2024
1 parent 4f2778d commit cbaa4f9
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class KeyCodeOption : KeyCodeSetting, IOption<KeyCode> {
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="doClamp">Whether to clamp the value when changed.</param>
protected KeyCodeOption(string name, KeyCode defaultValue, string displayName) : this(name, defaultValue, displayName, []) {
protected KeyCodeOption(string name, KeyCode defaultValue, string displayName) : this(name, defaultValue,
displayName, []) {
}

/// <summary>
Expand Down Expand Up @@ -97,7 +98,7 @@ public string GetDisplayName() {
public IUntypedOption AsUntyped() {
return this;
}

/// <summary>
/// Get this option's default value.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static class States {
/// String states.
/// </summary>
public static readonly StateHolder<string> Strings = new();

/// <summary>
/// KeyCode states.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,29 @@ public class CustomizationPatches {
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> RunTerminalTranspiler(IEnumerable<CodeInstruction> instructions) {
List<CodeInstruction> list = [..instructions];

foreach (var t in list.Where(t => t.opcode == OpCodes.Ldc_I4_3)) {
t.opcode = OpCodes.Ldc_I4;
t.operand = States.Ints[SettingKeys.FaceCharLimit];

break;
}

return list.AsEnumerable();
}

[HarmonyPatch(typeof(PlayerVisor), nameof(PlayerVisor.SafetyCheckVisorText))]
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> RunSafetyCheckTranspiler(IEnumerable<CodeInstruction> instructions) {
List<CodeInstruction> list = [..instructions];

foreach (var t in list.Where(t => t.opcode == OpCodes.Ldc_I4_3)) {
t.opcode = OpCodes.Ldc_I4;
t.operand = States.Ints[SettingKeys.FaceCharLimit];

break;
}

return list.AsEnumerable();
}

Expand All @@ -50,7 +50,8 @@ private static IEnumerable<CodeInstruction> RunSafetyCheckTranspiler(IEnumerable
private static void UpdatePostfix(PlayerCustomizer __instance, TextMeshProUGUI ___faceText, PhotonView ___view_g) {
if (Input.GetKey(KeyCode.Delete)) {
___view_g.RPC("RCP_SetFaceText", RpcTarget.All, "");
} else if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.V)) {
} else if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) &&
Input.GetKeyDown(KeyCode.V)) {
var systemCopyBuffer = GUIUtility.systemCopyBuffer;
___view_g.RPC("RCP_SetFaceText", RpcTarget.All, systemCopyBuffer);
} else if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter)) {
Expand All @@ -74,14 +75,14 @@ private static void PostfixRunTerminal(PlayerCustomizer __instance) {
[HarmonyPostfix]
private static void PostfixSetFaceText(ref PlayerCustomizer __instance, string text) {
if (!__instance.playerInTerminal) return;

Debug.Log("Patching SetFaceText with full text: " + text);

__instance.faceText.text = text;
__instance.playerInTerminal.refs.visor.visorFaceText.text = text;

if (__instance.faceText == null) return;

__instance.faceText.enableAutoSizing = States.Bools[SettingKeys.FaceAutoSizing];
__instance.faceText.fontSizeMin = States.Floats[SettingKeys.FaceMinFont];
__instance.faceText.fontSizeMax = States.Floats[SettingKeys.FaceMaxFont];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class IntroScreenPatch {
[HarmonyPrefix]
private static void StartPatch(IntroScreenAnimator __instance) {
if (!States.Bools[SettingKeys.SkipIntroScreen]) return;

__instance.skipping = true;
__instance.m_animator.enabled = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ private static class Settings {
private class DiveKey()
: KeyCodeOption(HellDiversSettingKeys.DiveKey, KeyCode.F, "Dive Keybind", [ApplySettings]);
}
}
}
60 changes: 60 additions & 0 deletions ContentLibrary/Source/ContentLibrary/API/BundleUtils.cs
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 ContentLibrary/Source/ContentLibrary/API/Enemies/EnemyLoader.cs
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 ContentLibrary/Source/ContentLibrary/API/Enemies/EnemyManager.cs
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 ContentLibrary/Source/ContentLibrary/Patches/RoundSpawnerPatch.cs
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;
}
}
39 changes: 12 additions & 27 deletions ContentLibrary/Source/ContentSettings/Internal/SettingsAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.IO.Compression;
using ContentLibrary;
using ContentLibrary.API;
using UnityEngine;
using Object = UnityEngine.Object;

Expand Down Expand Up @@ -83,36 +84,20 @@ internal static void LoadAssets() {
/// <typeparam name="T">The type of asset to load.</typeparam>
/// <returns>The loaded asset.</returns>
/// <exception cref="System.Exception">Thrown if the asset bundle or asset could not be loaded.</exception>
private static T LoadAsset<T>(string bundleName, string assetName)
where T : Object {
private static T LoadAsset<T>(string bundleName, string assetName) where T : Object {
ContentSettingsEntry.Logger.LogDebug($"Loading asset '{assetName}' from asset bundle '{bundleName}'.");

if (AssetBundles.TryGetValue(bundleName, out var bundle)) return bundle.LoadAsset<T>(assetName);

var assetBundleStream = typeof(ContentSettingsEntry)
.Assembly
.GetManifestResourceStream(
typeof(Plugin).Namespace + "." +
bundleName)
?? throw new Exception(
$"Failed to load asset bundle '{bundleName}' from embedded resource.");

using (assetBundleStream)
using (var gzStream = new GZipStream(assetBundleStream, CompressionMode.Decompress))
using (var reader = new StreamReader(gzStream))
using (var ms = new MemoryStream()) {
reader.BaseStream.CopyTo(ms);

var assetBundle = AssetBundle.LoadFromStream(ms) ??
throw new Exception($"Failed to load asset bundle '{bundleName}' from stream.");

var asset = assetBundle.LoadAsset<T>(assetName) ??
throw new Exception($"Failed to load asset '{assetName}' from asset bundle '{bundleName}'.");

ContentSettingsEntry.Logger.LogDebug($"Loaded asset '{assetName}' from asset bundle '{bundleName}'.");
AssetBundles.Add(bundleName, assetBundle);

return asset;
}
var assetBundle = BundleUtils.LoadEmbeddedAssetBundle(typeof(ContentSettingsEntry).Assembly,
typeof(Plugin).Namespace + "." + bundleName, true);

var asset = assetBundle?.LoadAsset<T>(assetName) ??
throw new Exception($"Failed to load asset '{assetName}' from asset bundle '{bundleName}'.");

ContentSettingsEntry.Logger.LogDebug($"Loaded asset '{assetName}' from asset bundle '{bundleName}'.");
AssetBundles.Add(bundleName, assetBundle);

return asset;
}
}

0 comments on commit cbaa4f9

Please sign in to comment.