1
0
Fork 0

Refactor: Fix up visibility and static modifiers, and other minor things

This commit is contained in:
ivan tkachenko 2025-08-02 16:25:01 +03:00
parent 76e9ca3595
commit 72adb9e713
3 changed files with 45 additions and 42 deletions

View File

@ -1,4 +1,5 @@
using DunGen;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@ -7,22 +8,24 @@ using UnityEngine;
namespace MuzikaGromche
{
public class DiscoBallManager : MonoBehaviour
public static class DiscoBallManager
{
// A struct holding a disco ball container object and the name of a tile for which it was designed.
public readonly record struct Data(string TileName, GameObject DiscoBallContainer)
private readonly record struct Data(string TileName, GameObject DiscoBallContainer)
{
// We are specifically looking for cloned tiles, not the original prototypes.
public readonly string TileCloneName = $"{TileName}(Clone)";
}
public static readonly List<Data> Containers = [];
private static readonly List<Data> Containers = [];
private static readonly List<GameObject> InstantiatedContainers = [];
public static void Initialize()
{
string bundlePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "muzikagromche_discoball");
var bundle = AssetBundle.LoadFromFile(bundlePath);
const string BundleFileName = "muzikagromche_discoball";
string bundlePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), BundleFileName);
var assetBundle = AssetBundle.LoadFromFile(bundlePath)
?? throw new NullReferenceException("Failed to load bundle");
foreach ((string prefabPath, string tileName) in new[] {
("Assets/LethalCompany/Mods/MuzikaGromche/DiscoBallContainerManor.prefab", "ManorStartRoomSmall"),
@ -33,7 +36,7 @@ namespace MuzikaGromche
("Assets/LethalCompany/Mods/MuzikaGromche/DiscoBallContainerBirthdayRoomTile.prefab", "BirthdayRoomTile"),
})
{
var container = bundle.LoadAsset<GameObject>(prefabPath);
var container = assetBundle.LoadAsset<GameObject>(prefabPath);
Containers.Add(new(tileName, container));
}
}
@ -66,7 +69,7 @@ namespace MuzikaGromche
private static void Enable(Tile tile, Data container)
{
Debug.Log($"{nameof(MuzikaGromche)} {nameof(DiscoBallManager)} Enabling at '{tile.gameObject.name}'");
var discoBall = Instantiate(container.DiscoBallContainer, tile.transform);
var discoBall = UnityEngine.Object.Instantiate(container.DiscoBallContainer, tile.transform);
InstantiatedContainers.Add(discoBall);
foreach (var animatorName in animatorNames)
@ -83,7 +86,7 @@ namespace MuzikaGromche
foreach (var discoBall in InstantiatedContainers)
{
Debug.Log($"{nameof(MuzikaGromche)} {nameof(DiscoBallManager)}: Disabling {discoBall.name}");
Destroy(discoBall);
UnityEngine.Object.Destroy(discoBall);
}
InstantiatedContainers.Clear();
}

View File

@ -41,7 +41,7 @@ namespace MuzikaGromche
.Select(a => $" Trying... {a}")
];
public static Track[] Tracks = [
public static readonly Track[] Tracks = [
new Track
{
Name = "MuzikaGromche",
@ -482,8 +482,8 @@ namespace MuzikaGromche
return tracks[trackId];
}
public static Track? CurrentTrack;
public static BeatTimeState? BeatTimeState;
internal static Track? CurrentTrack;
internal static BeatTimeState? BeatTimeState;
public static void SetLightColor(Color color)
{
@ -520,7 +520,7 @@ namespace MuzikaGromche
HUDManager.Instance.UIAudio.Stop();
}
private void Awake()
void Awake()
{
string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
UnityWebRequest[] requests = new UnityWebRequest[Tracks.Length * 2];
@ -560,7 +560,7 @@ namespace MuzikaGromche
}
};
public record Language(string Short, string Full)
public readonly record struct Language(string Short, string Full)
{
public static readonly Language ENGLISH = new("EN", "English");
public static readonly Language RUSSIAN = new("RU", "Russian");
@ -585,9 +585,9 @@ namespace MuzikaGromche
? Mathf.Pow(2f, 20f * x - 10f) / 2f
: (2f - Mathf.Pow(2f, -20f * x + 10f)) / 2f);
public static Easing[] All = [Linear, InCubic, OutCubic, InOutCubic, InExpo, OutExpo, InOutExpo];
public static readonly Easing[] All = [Linear, InCubic, OutCubic, InOutCubic, InExpo, OutExpo, InOutExpo];
public static string[] AllNames => [.. All.Select(easing => easing.Name)];
public static readonly string[] AllNames = [.. All.Select(easing => easing.Name)];
public static Easing FindByName(string Name)
{
@ -600,9 +600,9 @@ namespace MuzikaGromche
}
}
public record Palette(Color[] Colors)
public readonly record struct Palette(Color[] Colors)
{
public static Palette DEFAULT = new([Color.magenta, Color.cyan, Color.green, Color.yellow]);
public static readonly Palette DEFAULT = new([Color.magenta, Color.cyan, Color.green, Color.yellow]);
public static Palette Parse(string[] hexColors)
{
@ -784,7 +784,7 @@ namespace MuzikaGromche
}
}
public readonly record struct BeatTimestamp
readonly record struct BeatTimestamp
{
// Number of beats in the loop audio segment.
public readonly int LoopBeats;
@ -834,7 +834,7 @@ namespace MuzikaGromche
}
}
public readonly record struct BeatTimeSpan
readonly record struct BeatTimeSpan
{
public readonly int LoopBeats;
public readonly float HalfLoopBeats => LoopBeats / 2f;
@ -984,7 +984,7 @@ namespace MuzikaGromche
}
}
public class BeatTimeState
class BeatTimeState
{
// The object is newly created, the Start audio began to play but its time hasn't adjanced from 0.0f yet.
private bool hasStarted = false;
@ -1251,9 +1251,9 @@ namespace MuzikaGromche
}
}
public abstract class BaseEvent;
abstract class BaseEvent;
public class SetLightsColorEvent(Color color) : BaseEvent
class SetLightsColorEvent(Color color) : BaseEvent
{
public readonly Color Color = color;
public override string ToString()
@ -1262,7 +1262,7 @@ namespace MuzikaGromche
}
}
public class SetLightsColorTransitionEvent(Color from, Color to, Easing easing, float t)
class SetLightsColorTransitionEvent(Color from, Color to, Easing easing, float t)
: SetLightsColorEvent(Color.Lerp(from, to, Mathf.Clamp(easing.Eval(t), 0f, 1f)))
{
// Additional context for debugging
@ -1276,12 +1276,12 @@ namespace MuzikaGromche
}
}
public class FlickerLightsEvent : BaseEvent
class FlickerLightsEvent : BaseEvent
{
public override string ToString() => "Flicker";
}
public class LyricsEvent(string text) : BaseEvent
class LyricsEvent(string text) : BaseEvent
{
public readonly string Text = text;
public override string ToString()
@ -1290,14 +1290,14 @@ namespace MuzikaGromche
}
}
public class WindUpZeroBeatEvent : BaseEvent
class WindUpZeroBeatEvent : BaseEvent
{
public override string ToString() => "WindUp";
}
// Default C#/.NET remainder operator % returns negative result for negative input
// which is unsuitable as an index for an array.
public static class Mod
static class Mod
{
public static int Positive(int x, int m)
{
@ -1317,7 +1317,7 @@ namespace MuzikaGromche
}
}
public readonly struct RandomWeightedIndex
readonly struct RandomWeightedIndex
{
public RandomWeightedIndex(int[] weights)
{
@ -1404,7 +1404,7 @@ namespace MuzikaGromche
readonly public int TotalWeights { get; }
}
public static class SyncedEntryExtensions
static class SyncedEntryExtensions
{
// Update local values on clients. Even though the clients couldn't
// edit them, they could at least see the new values.
@ -1417,7 +1417,7 @@ namespace MuzikaGromche
}
}
public class Config : SyncedConfig2<Config>
class Config : SyncedConfig2<Config>
{
public static ConfigEntry<bool> DisplayLyrics { get; private set; } = null!;
@ -1438,7 +1438,7 @@ namespace MuzikaGromche
public static float? ColorTransitionOutOverride { get; private set; } = null;
public static string? ColorTransitionEasingOverride { get; private set; } = null;
public Config(ConfigFile configFile) : base(PluginInfo.PLUGIN_GUID)
internal Config(ConfigFile configFile) : base(PluginInfo.PLUGIN_GUID)
{
DisplayLyrics = configFile.Bind("General", "Display Lyrics", true,
new ConfigDescription("Display lyrics in the HUD tooltip when you hear the music."));
@ -1767,7 +1767,7 @@ namespace MuzikaGromche
// farAudio is during windup, Start overrides popGoesTheWeaselTheme
// creatureVoice is when popped, Loop overrides screamingSFX
[HarmonyPatch(typeof(JesterAI))]
internal class JesterPatch
static class JesterPatch
{
#if DEBUG
[HarmonyPatch(nameof(JesterAI.SetJesterInitialValues))]
@ -1786,7 +1786,7 @@ namespace MuzikaGromche
[HarmonyPatch(nameof(JesterAI.Update))]
[HarmonyPrefix]
static void DoNotStopTheMusicPrefix(JesterAI __instance, out State __state)
static void JesterUpdatePrefix(JesterAI __instance, out State __state)
{
__state = new State
{
@ -1808,7 +1808,7 @@ namespace MuzikaGromche
[HarmonyPatch(nameof(JesterAI.Update))]
[HarmonyPostfix]
static void DoNotStopTheMusic(JesterAI __instance, State __state)
static void JesterUpdatePostfix(JesterAI __instance, State __state)
{
if (__instance.previousState == 1 && __state.previousState != 1)
{
@ -1900,13 +1900,13 @@ namespace MuzikaGromche
}
[HarmonyPatch(typeof(EnemyAI))]
internal class EnemyAIPatch
static class EnemyAIPatch
{
// JesterAI class does not override abstract method OnDestroy,
// so we have to patch its superclass directly.
[HarmonyPatch(nameof(EnemyAI.OnDestroy))]
[HarmonyPrefix]
public static void CleanUpOnDestroy(EnemyAI __instance)
static void CleanUpOnDestroy(EnemyAI __instance)
{
if (__instance is JesterAI)
{

View File

@ -9,7 +9,7 @@ using UnityEngine;
namespace MuzikaGromche
{
internal class PoweredLightsAnimators
static class PoweredLightsAnimators
{
private delegate void ManualPatch(GameObject animatorContainer);
@ -167,11 +167,11 @@ namespace MuzikaGromche
}
[HarmonyPatch(typeof(Tile))]
internal class PoweredLightsAnimatorsPatch
static class PoweredLightsAnimatorsPatch
{
[HarmonyPatch("AddTriggerVolume")]
[HarmonyPatch(nameof(Tile.AddTriggerVolume))]
[HarmonyPostfix]
public static void OnAddTriggerVolume(Tile __instance)
static void OnAddTriggerVolume(Tile __instance)
{
PoweredLightsAnimators.Patch(__instance);
}
@ -188,14 +188,14 @@ namespace MuzikaGromche
// In order to fix that, replace singular GetComponentInChildren<Light> with plural GetComponentsInChildren<Light> version.
[HarmonyPatch(nameof(RoundManager.RefreshLightsList))]
[HarmonyPrefix]
private static bool OnRefreshLightsList(RoundManager __instance)
static bool OnRefreshLightsList(RoundManager __instance)
{
RefreshLightsListPatched(__instance);
// Skip the original method
return false;
}
private static void RefreshLightsListPatched(RoundManager self)
static void RefreshLightsListPatched(RoundManager self)
{
// Reusable list to reduce allocations
List<Light> lights = [];