Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom FMOD support for PlayableDirector #554

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added attaching functionality for FMOD channel
  • Loading branch information
Metious committed Jul 22, 2024
commit 09d1d4e45411e2aee681b2910a52ee77fdabd5fc
37 changes: 37 additions & 0 deletions Nautilus/Handlers/CustomSoundHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,41 @@ public static bool TryGetCustomSoundChannel(int id, out Channel channel)
{
return CustomSoundPatcher.EmitterPlayedChannels.TryGetValue(id, out channel);
}


/// <summary>
/// Attaches the specified channel to the given transform. This results in the sound position following the <paramref name="transform"/>.
/// </summary>
/// <param name="channel">The channel to attach.</param>
/// <param name="transform">The transform which the channel will follow.</param>
public static void AttachChannelToGameObject(Channel channel, Transform transform)
{
var index = CustomSoundPatcher.AttachedChannels.FindIndex(x => x.Channel.handle == channel.handle);
var attachedChannel = new CustomSoundPatcher.AttachedChannel(channel, transform);
if (index == -1)
{
CustomSoundPatcher.AttachedChannels.Add(attachedChannel);
}
else
{
CustomSoundPatcher.AttachedChannels[index] = attachedChannel;
}

CustomSoundPatcher.SetChannel3DAttributes(channel, transform);
}

/// <summary>
/// Detaches the specified channel from any game object.
/// </summary>
/// <param name="channel">The channel to detach.</param>
public static void DetachChannelFromGameObject(Channel channel)
{
var index = CustomSoundPatcher.AttachedChannels.FindIndex(x => x.Channel.handle == channel.handle);
if (index == -1)
{
InternalLogger.Warn($"{nameof(CustomSoundHandler)}: The specified channel is not attached to any game object.");
}

CustomSoundPatcher.AttachedChannels.RemoveAt(index);
}
}
42 changes: 40 additions & 2 deletions Nautilus/Patchers/CustomSoundPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@
using FMODUnity;
using HarmonyLib;
using Nautilus.FMod.Interfaces;
using Nautilus.Handlers;
using Nautilus.Utility;
using UnityEngine;
using UnityEngine.Playables;

namespace Nautilus.Patchers;

internal class CustomSoundPatcher
{
internal record struct AttachedChannel(Channel Channel, Transform Transform);

internal static readonly SelfCheckingDictionary<string, Sound> CustomSounds = new("CustomSounds");
internal static readonly SelfCheckingDictionary<string, Bus> CustomSoundBuses = new("CustomSoundBuses");
internal static readonly SelfCheckingDictionary<string, IFModSound> CustomFModSounds = new("CustoomFModSounds");
internal static readonly Dictionary<int, Channel> EmitterPlayedChannels = new();
internal static List<AttachedChannel> AttachedChannels = new();

private static readonly Dictionary<string, Channel> PlayedChannels = new();
private static readonly List<AttachedChannel> _attachedChannelsToRemove = new();

internal static void Patch(Harmony harmony)
{
Expand Down Expand Up @@ -69,6 +75,38 @@ public static bool FMODExtension_GetLength_Prefix(string path, ref int __result)
return false;
}


[HarmonyPatch(typeof(RuntimeManager), nameof(RuntimeManager.Update))]
[HarmonyPostfix]
public static void RuntimeManager_Update_Postfix(RuntimeManager __instance)
{
if (!__instance.studioSystem.isValid())
{
return;
}

foreach (var attachedChannel in AttachedChannels)
{
attachedChannel.Channel.isPlaying(out var isPlaying);
if (!isPlaying || !attachedChannel.Transform)
{
_attachedChannelsToRemove.Add(attachedChannel);
continue;
}

SetChannel3DAttributes(attachedChannel.Channel, attachedChannel.Transform);
}

if (_attachedChannelsToRemove.Count > 0)
{
foreach (var toRemove in _attachedChannelsToRemove)
{
AttachedChannels.Remove(toRemove);
}
_attachedChannelsToRemove.Clear();
}
}

#if SUBNAUTICA

[HarmonyPatch(typeof(FMODUWE), nameof(FMODUWE.PlayOneShotImpl))]
Expand Down Expand Up @@ -700,13 +738,13 @@ public static bool FMOD_CustomLoopingEmitter_OnPlay_Prefix(FMOD_CustomLoopingEmi
}
#endif

private static void SetChannel3DAttributes(Channel channel, Transform transform)
internal static void SetChannel3DAttributes(Channel channel, Transform transform)
{
ATTRIBUTES_3D attributes = transform.To3DAttributes();
channel.set3DAttributes(ref attributes.position, ref attributes.velocity);
}

private static void SetChannel3DAttributes(Channel channel, Vector3 position)
internal static void SetChannel3DAttributes(Channel channel, Vector3 position)
{
ATTRIBUTES_3D attributes = position.To3DAttributes();
channel.set3DAttributes(ref attributes.position, ref attributes.velocity);
Expand Down