Skip to content

Commit

Permalink
serialize sound collection instances in unity
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexejhero committed Nov 2, 2023
1 parent 00f9c6d commit 34540ea
Show file tree
Hide file tree
Showing 83 changed files with 745 additions and 187 deletions.
6 changes: 1 addition & 5 deletions SCHIZO/Creatures/Components/GetCarried.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using Nautilus.Utility;
using SCHIZO.Sounds.Players;
using UnityEngine;
using Random = UnityEngine.Random;
Expand All @@ -20,18 +19,15 @@ partial class GetCarried
typeof(Rigidbody),
};

private const string BUS = AudioUtils.BusPaths.UnderwaterCreatures;
public override void Awake()
{
base.Awake();
// contrary to the name, this is actually the max possible priority
// full explanation here <see cref="Events.Ermcon.ErmconAttendee.Awake"/>
evaluatePriority = 99f;
pickupSounds = pickupSounds!?.Initialize(BUS);
carrySounds = carrySounds!?.Initialize(BUS);
releaseSounds = releaseSounds!?.Initialize(BUS);
_disabledComponents = new List<(MonoBehaviour component, bool wasEnabled)>();
}

public override float Evaluate(float time) => isCarried ? 99f : -99f; // manual start/end

private void DisableComponents()
Expand Down
6 changes: 0 additions & 6 deletions SCHIZO/Creatures/Ermshark/ErmsharkAttack.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
using Nautilus.Utility;
using SCHIZO.Creatures.Components;
using UnityEngine;

namespace SCHIZO.Creatures.Ermshark;

partial class ErmsharkAttack
{
private void Awake()
{
attackSounds = attackSounds!?.Initialize(AudioUtils.BusPaths.UnderwaterCreatures);
}

public override void OnTouch(Collider collider)
{
if (!enabled) return;
Expand Down
2 changes: 0 additions & 2 deletions SCHIZO/Events/Ermcon/ErmconAttendee.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Nautilus.Utility;
using UnityEngine;

namespace SCHIZO.Events.Ermcon;
Expand Down Expand Up @@ -50,7 +49,6 @@ public override void Awake()
// "meta"-priority - this number determines the order in which actions get *evaluated*
// and the priority obtained from Evaluate() actually determines which action gets *performed*
evaluatePriority = 99f;
pickupDeniedSounds = pickupDeniedSounds!?.Initialize(AudioUtils.BusPaths.UnderwaterCreatures);
}
public void OnHandHover(GUIHand hand)
{
Expand Down
3 changes: 3 additions & 0 deletions SCHIZO/Interop/Subnautica/_FMODAsset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace SCHIZO.Interop.Subnautica;

partial class _FMODAsset : FMODAsset;
7 changes: 4 additions & 3 deletions SCHIZO/Jukebox/CustomJukeboxDisk.BelowZero.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FMODUnity;
using Nautilus.Handlers;
using Nautilus.Utility;
using SCHIZO.Sounds;
using UnityEngine;

namespace SCHIZO.Jukebox;
Expand All @@ -24,9 +25,9 @@ public sealed class CustomJukeboxDisk : JukeboxDisk

if (!CustomSoundHandler.TryGetCustomSound(guid, out _))
{
const string BUS = "bus:/master/SFX_for_pause/PDA_pause/all";
CustomSoundHandler.RegisterCustomSound(guid, unlockSound, BUS, AudioUtils.StandardSoundModes_2D);
RuntimeManager.GetBus(BUS).unlockChannelGroup();
string bus = BusPaths.SFX.GetBusName();
CustomSoundHandler.RegisterCustomSound(guid, unlockSound, bus, AudioUtils.StandardSoundModes_2D);
RuntimeManager.GetBus(bus).unlockChannelGroup();
}

acquireSound = AudioUtils.GetFmodAsset(guid);
Expand Down
2 changes: 1 addition & 1 deletion SCHIZO/Resources/AssetBundles/Assets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace SCHIZO.Resources;

public static class Assets
{
private const int _rnd = -112038177;
private const int _rnd = 228879590;

private static readonly UnityEngine.AssetBundle _a = ResourceManager.GetAssetBundle("assets");

Expand Down
Binary file modified SCHIZO/Resources/AssetBundles/assets
Binary file not shown.
19 changes: 19 additions & 0 deletions SCHIZO/Sounds/BusPaths.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using Nautilus.Utility;

namespace SCHIZO.Sounds;

public static class BusPathsExtensions
{
public static string GetBusName(this BusPaths bus)
{
return bus switch
{
BusPaths.PDAVoice => AudioUtils.BusPaths.PDAVoice,
BusPaths.UnderwaterCreatures => AudioUtils.BusPaths.UnderwaterCreatures,
BusPaths.IndoorSounds => "bus:/master/SFX_for_pause/PDA_pause/all/indoorsounds",
BusPaths.SFX => "bus:/master/SFX_for_pause/PDA_pause/all/SFX",
_ => throw new ArgumentOutOfRangeException(nameof(bus), bus, null)
};
}
}
22 changes: 0 additions & 22 deletions SCHIZO/Sounds/Collections/SoundCollection.cs

This file was deleted.

32 changes: 10 additions & 22 deletions SCHIZO/Sounds/Collections/SoundCollectionInstance.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,32 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using Nautilus.Utility;

namespace SCHIZO.Sounds.Collections;

public sealed class SoundCollectionInstance : SoundCollection
partial class SoundCollectionInstance
{
private FMODSoundCollection _fmodSounds;
private string _bus;

public static SoundCollectionInstance Create(SoundCollection soundCollection, string bus)
private void OnEnable()
{
SoundCollectionInstance instance = CreateInstance<SoundCollectionInstance>();
instance._fmodSounds = FMODSoundCollection.For(soundCollection, bus);
instance._bus = bus;
return instance;
_fmodSounds = FMODSoundCollection.For(collection, bus.GetBusName());
path = id = new Guid().ToString();
}

[Obsolete("SoundCollectionInstance does not need to be initialized", true)]
public override SoundCollectionInstance Initialize(string bus)
{
if (_bus != bus) throw new InvalidOperationException($"SoundCollection {this} is already initialized with a different bus ({_bus} != {bus})");
return this;
}

public override float LastPlay => _fmodSounds.LastPlay;
public float LastPlay => _fmodSounds.LastPlay;

public override void Play2D(float delay = 0)
public void Play2D(float delay = 0)
{
_fmodSounds.Play2D(delay);
}

public override void Play(FMOD_CustomEmitter emitter, float delay = 0)
public void Play(FMOD_CustomEmitter emitter, float delay = 0)
{
if (!emitter) throw new ArgumentNullException(nameof(emitter));
_fmodSounds.Play(emitter, delay);
}

public override void CancelAllDelayed() => _fmodSounds.CancelAllDelayed();
public void CancelAllDelayed() => _fmodSounds.CancelAllDelayed();

[Obsolete("SoundCollectionInstance does not support enumerating sounds", true)]
public override IEnumerable<AudioClip> GetSounds() => throw new InvalidOperationException("SoundCollectionInstance does not support enumerating sounds");
public void Stop() => _fmodSounds.Stop();
}
27 changes: 27 additions & 0 deletions SCHIZO/Sounds/FMODSoundCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using FMOD;
using FMOD.Studio;
using FMODUnity;
using Nautilus.FMod.Interfaces;
using Nautilus.Handlers;
using Nautilus.Utility;
using SCHIZO.DataStructures;
Expand All @@ -23,10 +24,12 @@ private enum VCA
Voice,
Ambient
}

private static readonly Dictionary<string, FMODSoundCollection> _cache = new();

private readonly string _busName;
private readonly List<string> _sounds = new();
private readonly List<Channel> _channels = new();

private bool _ready;
private RandomList<string> _randomSounds;
Expand Down Expand Up @@ -123,6 +126,18 @@ IEnumerator PlayWithDelay(float del)
}
}

public void Stop()
{
if (!Initialize()) return;
if (Assets.Options_DisableAllSounds.Value) return;

foreach (Channel channel in _channels)
{
channel.stop();
}
ClearChannelsCache();
}

private void PlaySound(FMOD_CustomEmitter emitter = null)
{
LastPlay = Time.time;
Expand All @@ -139,9 +154,21 @@ private void PlaySound(FMOD_CustomEmitter emitter = null)
CustomSoundHandler.TryPlayCustomSound(sound, out Channel channel);
channel.setVolume(GetVolumeFor(GetVCAForBus(_busName)));
channel.set3DLevel(0);

ClearChannelsCache();
_channels.Add(channel);
}
}

private void ClearChannelsCache()
{
_channels.RemoveAll(c =>
{
RESULT result = c.isPlaying(out bool isPlaying);
return result != RESULT.OK || !isPlaying;
});
}

private static VCA GetVCAForBus(string bus)
{
if (bus.StartsWith("bus:/master/Music")) return VCA.Music;
Expand Down
14 changes: 0 additions & 14 deletions SCHIZO/Sounds/ItemSounds.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using Nautilus.Utility;

namespace SCHIZO.Sounds;

Expand All @@ -21,18 +20,5 @@ public void Register(TechType techType)
}

_registeredItemSounds[techType] = this;

// If multiple items share the same sounds, Initialize will return the same SoundCollectionInstance as long as the bus is the same, so we don't need to worry about not calling it.

pickupSounds = pickupSounds!?.Initialize(AudioUtils.BusPaths.PDAVoice);
dropSounds = dropSounds!?.Initialize(AudioUtils.BusPaths.UnderwaterCreatures);

drawSounds = drawSounds!?.Initialize(AudioUtils.BusPaths.PDAVoice);
holsterSounds = holsterSounds!?.Initialize(AudioUtils.BusPaths.PDAVoice);

cookSounds = cookSounds!?.Initialize(AudioUtils.BusPaths.PDAVoice);
eatSounds = eatSounds!?.Initialize(AudioUtils.BusPaths.PDAVoice);

playerDeathSounds = playerDeathSounds!?.Initialize(AudioUtils.BusPaths.PDAVoice);
}
}
3 changes: 3 additions & 0 deletions SCHIZO/Sounds/Patches/ItemSoundsPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using HarmonyLib;
using JetBrains.Annotations;
using SCHIZO.Helpers;
using SCHIZO.Sounds.Players;
using UnityEngine;

namespace SCHIZO.Sounds.Patches;
Expand All @@ -28,6 +29,8 @@ public static void PlayCustomDropSound(Pickupable __instance)

sounds.holsterSounds!?.CancelAllDelayed();
sounds.dropSounds.Play(__instance.GetComponent<FMOD_CustomEmitter>());

__instance.GetComponentsInChildren<InventoryAmbientSoundPlayer>().ForEach(p => p.Stop());
}

[HarmonyPatch(typeof(PlayerTool), nameof(PlayerTool.OnDraw))]
Expand Down
3 changes: 1 addition & 2 deletions SCHIZO/Sounds/Players/InventoryAmbientSoundPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ partial class InventoryAmbientSoundPlayer
{
private float _timer = -1;

protected override void Awake()
private void Awake()
{
base.Awake();
ResetTimer();
}

Expand Down
21 changes: 6 additions & 15 deletions SCHIZO/Sounds/Players/SoundPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
using System;
using SCHIZO.Helpers;

namespace SCHIZO.Sounds.Players;

partial class SoundPlayer
{
protected virtual void Awake()
public void Play(float delay = 0)
{
string busOrFieldPath = string.IsNullOrEmpty(bus) ? DefaultBus : bus;
if (string.IsNullOrEmpty(busOrFieldPath)) throw new InvalidOperationException($"No bus assigned to {this}");
// the string is most likely a field path, but we can also directly accept bus paths (and guids)
string actualBus = busOrFieldPath.StartsWith("bus:/") || Guid.TryParse(busOrFieldPath, out _)
? busOrFieldPath
: StaticHelpers.GetValue<string>(busOrFieldPath);

soundCollection = soundCollection.Initialize(actualBus);
if (Is3D) sounds.Play(emitter, delay);
else sounds.Play2D(delay);
}

public void Play(float delay = 0)
public void Stop()
{
if (Is3D) soundCollection.Play(emitter, delay);
else soundCollection.Play2D(delay);
if (Is3D) emitter.Stop();
else sounds.Stop();
}
}
3 changes: 1 addition & 2 deletions SCHIZO/Sounds/Players/WorldAmbientSoundPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ partial class WorldAmbientSoundPlayer
{
private float _timer = -1;

protected override void Awake()
private void Awake()
{
base.Awake();
ResetTimer();
}

Expand Down
5 changes: 2 additions & 3 deletions SCHIZO/Sounds/ScanSoundHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using HarmonyLib;
using Nautilus.Utility;
using SCHIZO.Sounds.Collections;

namespace SCHIZO.Sounds;
Expand All @@ -10,9 +9,9 @@ public static class ScanSoundHandler
{
private static readonly Dictionary<TechType, SoundCollectionInstance> _scanSounds = new();

public static void Register(TechType techType, SoundCollection soundCollection)
public static void Register(TechType techType, SoundCollectionInstance soundCollection)
{
_scanSounds.Add(techType, soundCollection.Initialize(AudioUtils.BusPaths.PDAVoice));
_scanSounds.Add(techType, soundCollection);
}

[HarmonyPatch]
Expand Down
5 changes: 0 additions & 5 deletions SCHIZO/_old/Creatures/Ermfish/ErmfishLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ public sealed class ErmfishLoader : PickupableCreatureLoader<PickupableCreatureD
public static readonly SoundPlayer PlayerDeathSounds = new(Assets.Erm_Sounds_PlayerDeath_ErmfishPlayerDeath,
IS_BELOWZERO ? "bus:/master/SFX_for_pause" : "bus:/master/SFX_for_pause/nofilter");

public ErmfishLoader() : base(Assets.Erm_ErmfishData)
{
VFXFabricatingData = new VFXFabricatingData("VM/model", -0.255f, 0.67275f, new Vector3(0, 0.22425f), 0.1f, new Vector3(0, -180, 0));
}

protected override IEnumerable<LootDistributionData.BiomeData> GetLootDistributionData()
{
foreach (BiomeType biome in BiomeHelpers.GetOpenWaterBiomes())
Expand Down
1 change: 1 addition & 0 deletions SCHIZO/_old/Credits/EasterEggPatches.Subnautica.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Linq;
using HarmonyLib;
using Nautilus.FMod;
using Nautilus.Handlers;
using SCHIZO.Resources;
using SCHIZO.Sounds;
Expand Down
Loading

0 comments on commit 34540ea

Please sign in to comment.