From 3be337bab9efe3ce37c1ec673220802eeb190c2d Mon Sep 17 00:00:00 2001 From: Govorunb Date: Fri, 22 Dec 2023 02:17:16 +1100 Subject: [PATCH] better prefab registering for jukebox tracks add console command to manually unlock tracks make the tracks sound like they're coming from the jukebox instead of just magically going full stereo once you're close enough to it --- ...{d401bd90-2cc7-4357-94fd-85ac0b808c72}.xml | 3 + SCHIZO/ConsoleCommands/ConsoleCommands.cs | 20 +++++- SCHIZO/Jukebox/CustomJukeboxDisk.BelowZero.cs | 64 +++++++++++++++++-- .../Jukebox/CustomJukeboxTrack.BelowZero.cs | 39 +---------- .../CustomJukeboxTrackPatches.BelowZero.cs | 16 ----- 5 files changed, 82 insertions(+), 60 deletions(-) diff --git a/FMOD Project/Metadata/Event/{d401bd90-2cc7-4357-94fd-85ac0b808c72}.xml b/FMOD Project/Metadata/Event/{d401bd90-2cc7-4357-94fd-85ac0b808c72}.xml index 9889d5af..e7734f09 100644 --- a/FMOD Project/Metadata/Event/{d401bd90-2cc7-4357-94fd-85ac0b808c72}.xml +++ b/FMOD Project/Metadata/Event/{d401bd90-2cc7-4357-94fd-85ac0b808c72}.xml @@ -117,6 +117,9 @@ + + 180 + -2 diff --git a/SCHIZO/ConsoleCommands/ConsoleCommands.cs b/SCHIZO/ConsoleCommands/ConsoleCommands.cs index cd5808cb..7611424b 100644 --- a/SCHIZO/ConsoleCommands/ConsoleCommands.cs +++ b/SCHIZO/ConsoleCommands/ConsoleCommands.cs @@ -6,9 +6,11 @@ using FMODUnity; using JetBrains.Annotations; using Nautilus.Commands; +using Nautilus.Handlers; using SCHIZO.Helpers; using UnityEngine; using Object = UnityEngine.Object; +using TrackId = global::Jukebox.UnlockableTrack; namespace SCHIZO.ConsoleCommands; @@ -46,7 +48,7 @@ public static void OnConsoleCommand_say(params string[] args) // so we don't have to needlessly split and join ErrorMessage.AddMessage(string.Join(" ", args)); } - +#region FMOD public static string OnConsoleCommand_banks() { RuntimeManager.StudioSystem.getBankList(out Bank[] banks); @@ -161,4 +163,20 @@ public static string OnConsoleCommand_fmod(params string[] args) _ => null, }; } + #endregion FMOD + + [ConsoleCommand("unlocktrack"), UsedImplicitly] + public static string OnConsoleCommand_unlocktrack(params string[] trackArgs) + { + string track = string.Join(" ", trackArgs); + if (string.IsNullOrEmpty(track)) + return "Usage: unlocktrack \nTrack ID can be specified by enum name or value (e.g. `Track1` or just `1`)"; + // Nautilus only patches Enum.Parse... + if (!Enum.TryParse(track, true, out TrackId trackId) && !EnumHandler.TryGetValue(track, out trackId)) + return $"No such track '{track}'"; + + if (!Player.main) return "Jukebox tracks can only be unlocked in-game"; + global::Jukebox.Unlock(trackId, true); + return null; + } } diff --git a/SCHIZO/Jukebox/CustomJukeboxDisk.BelowZero.cs b/SCHIZO/Jukebox/CustomJukeboxDisk.BelowZero.cs index 49c651ac..de7b16e9 100644 --- a/SCHIZO/Jukebox/CustomJukeboxDisk.BelowZero.cs +++ b/SCHIZO/Jukebox/CustomJukeboxDisk.BelowZero.cs @@ -1,15 +1,65 @@ +using System.Collections.Generic; +using Nautilus.Assets; +using Nautilus.Assets.Gadgets; +using Nautilus.Assets.PrefabTemplates; +using Nautilus.Utility; +using SCHIZO.Creatures.Components; +using SCHIZO.Helpers; +using UnityEngine; + namespace SCHIZO.Jukebox; -public sealed class CustomJukeboxDisk : JukeboxDisk +public sealed class JukeboxDiskPrefab { - public new void Start() + // prefab path "Misc/JukeboxDisk8.prefab" + private const string DISK_CLASSID = "5108080f-242b-49e8-9b91-d01d6bbe138c"; + + internal static Dictionary Prefabs = []; + + public CustomPrefab NautilusPrefab { get; } + private JukeboxDiskPrefab(CustomJukeboxTrack track) { - if (track == default) + string prefabName = $"{nameof(CustomJukeboxTrack)}_{track.identifier}"; + + NautilusPrefab = new CustomPrefab(prefabName, null, null); + + NautilusPrefab.SetSpawns(new SpawnLocation(track.diskSpawnLocation.position, track.diskSpawnLocation.rotation)); + + NautilusPrefab.SetGameObject(new CloneTemplate(NautilusPrefab.Info, DISK_CLASSID) { - LOGGER.LogError($"Jukebox disk {name} at {transform.position} was not assigned a track, self-destructing"); - Destroy(this); - } + ModifyPrefab = prefab => + { + prefab.SetActive(false); + if (track.diskPrefab) + { + Renderer[] renderers = prefab.GetComponentsInChildren(); + renderers.ForEach(r => r.gameObject.SetActive(false)); + + GameObject customModel = GameObject.Instantiate(track.diskPrefab, prefab.transform, false); + customModel.transform.localPosition = Vector3.zero; - base.Start(); + MaterialUtils.ApplySNShaders(customModel, 1); + } + + JukeboxDisk diskComp = prefab.EnsureComponent(); + diskComp.track = track; + if (!string.IsNullOrEmpty(track.unlockFmodEvent)) + diskComp.acquireSound = AudioUtils.GetFmodAsset(track.unlockFmodEvent, FMODHelpers.GetId(track.unlockFmodEvent)); + + DisableUntilStoryGoal storyGate = prefab.EnsureComponent(); + storyGate.storyGoalBZ = "SanctuaryCompleted"; + } + }); + NautilusPrefab.Register(); + } + + public static void Register(CustomJukeboxTrack track) + { + if (Prefabs.ContainsKey(track.identifier)) + { + LOGGER.LogWarning($"Track {track.identifier} already registered, skipping prefab registration"); + return; + } + Prefabs[track.identifier] = new JukeboxDiskPrefab(track); } } diff --git a/SCHIZO/Jukebox/CustomJukeboxTrack.BelowZero.cs b/SCHIZO/Jukebox/CustomJukeboxTrack.BelowZero.cs index ff8d071c..11c8d751 100644 --- a/SCHIZO/Jukebox/CustomJukeboxTrack.BelowZero.cs +++ b/SCHIZO/Jukebox/CustomJukeboxTrack.BelowZero.cs @@ -71,6 +71,7 @@ protected override void Register() } CustomJukeboxTrackPatches.customTracks[trackId] = this; RegisterInJukebox(BZJukebox._main); + JukeboxDiskPrefab.Register(this); if (!Player.main) return; @@ -106,50 +107,16 @@ public static bool IsTrackCustom(BZJukebox.UnlockableTrack trackId) internal void SetupUnlock() { - BZJukebox.UnlockableTrack trackId = this; - if (!Player.main || !GameModeManager.HaveGameOptionsSet) { - LOGGER.LogError($"Can't set up unlock for {trackId} with no {(!Player.main ? "player" : "game options")}!"); + LOGGER.LogError($"Can't set up unlock for {identifier} with no {(!Player.main ? "player" : "game options")}!"); return; } if (unlockedOnStart || !GameModeManager.GetOption(GameOption.Story)) { - BZJukebox.Unlock(trackId, false); + BZJukebox.Unlock(this, false); BZJukebox.main.SetInfo(JukeboxIdentifier, ToTrackInfo(false)); } - else - { - SpawnDisk(trackId, diskSpawnLocation.position, diskSpawnLocation.rotation); - } - } - - private void SpawnDisk(BZJukebox.UnlockableTrack trackId, Vector3 position, Vector3 rotation) - { - GameObject disk = Instantiate(CustomJukeboxTrackPatches.defaultDiskPrefab); - disk.transform.position = position; - disk.transform.eulerAngles = rotation; - - if (diskPrefab) - { - Renderer[] renderers = disk.GetComponentsInChildren(); - renderers.ForEach(r => r.gameObject.SetActive(false)); - - GameObject customModel = Instantiate(diskPrefab, disk.transform, false); - customModel.transform.localPosition = Vector3.zero; - - MaterialUtils.ApplySNShaders(disk, 1); - } - - Destroy(disk.GetComponent()); - - CustomJukeboxDisk diskComp = disk.EnsureComponent(); - diskComp.track = trackId; - diskComp.acquireSound = AudioUtils.GetFmodAsset(unlockFmodEvent, FMODHelpers.GetId(unlockFmodEvent)); - - disk.GetComponent().enabled = false; // don't save - - LOGGER.LogDebug($"Spawned disk for {trackId} at {position}"); } } diff --git a/SCHIZO/Jukebox/CustomJukeboxTrackPatches.BelowZero.cs b/SCHIZO/Jukebox/CustomJukeboxTrackPatches.BelowZero.cs index db22beb5..d0458a9a 100644 --- a/SCHIZO/Jukebox/CustomJukeboxTrackPatches.BelowZero.cs +++ b/SCHIZO/Jukebox/CustomJukeboxTrackPatches.BelowZero.cs @@ -17,26 +17,11 @@ public static class CustomJukeboxTrackPatches { internal static readonly SelfCheckingDictionary customTracks = new("customTracks"); - internal static GameObject defaultDiskPrefab; - static CustomJukeboxTrackPatches() { - CoroutineHost.StartCoroutine(GetJukeboxDiskPrefab()); CoroutineHost.StartCoroutine(InitJukebox()); SaveUtils.RegisterOnQuitEvent(() => CoroutineHost.StartCoroutine(InitJukebox())); } - private static IEnumerator GetJukeboxDiskPrefab() - { - if (defaultDiskPrefab) yield break; - // const string diskClassId = "5108080f-242b-49e8-9b91-d01d6bbe138c"; - const string diskPrefabPath = "Misc/JukeboxDisk8.prefab"; - IPrefabRequest request = PrefabDatabase.GetPrefabForFilenameAsync(diskPrefabPath); - yield return request; - - if (!request.TryGetPrefab(out defaultDiskPrefab)) - LOGGER.LogError("Could not get prefab for jukebox disk!"); - LOGGER.LogDebug("Loaded default prefab for custom tracks"); - } private static IEnumerator InitJukebox() { @@ -87,7 +72,6 @@ public static void EnableRichText(object __instance) [HarmonyPostfix] public static void SetupUnlocksForCustomTracks() { - // duplicate disks are not a problem - they self-destruct on Start if already unlocked customTracks.ForEach(pair => pair.Value.SetupUnlock()); }