Skip to content

Commit

Permalink
* updated fielddaycore with critical audio bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
BeauchesneFieldDay committed Jan 24, 2025
1 parent a892265 commit f65023f
Show file tree
Hide file tree
Showing 26 changed files with 456 additions and 91 deletions.
3 changes: 3 additions & 0 deletions Assets/FieldDay/Assets/AssetCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public void Deregister(StringHash32 id) {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Il2CppSetOption(Option.NullChecks, false)]
public void Clear() {
foreach(var asset in m_Lookup.Values) {
RegistrationCallbacks.InvokeDeregister(asset);
}
m_Lookup.Clear();
}

Expand Down
9 changes: 9 additions & 0 deletions Assets/FieldDay/Assets/AssetMgr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,15 @@ public NamedAssetIterator<T> GetAllNamed<T>() where T : class, INamedAsset {
return new NamedAssetIterator<T>(typedCollection.GetAll());
}

/// <summary>
/// Returns if a named asset with the given name and type is loaded.
/// </summary>
[Il2CppSetOption(Option.NullChecks, false)]
public bool HasNamed<T>(StringHash32 id) where T : class, INamedAsset {
NamedAssetCollection typedCollection = GetNamedCollection<T>(true);
return typedCollection.TryLookup(id, out var _);
}

#endregion // Named

#region Lite
Expand Down
12 changes: 12 additions & 0 deletions Assets/FieldDay/Audio/AudioMgr.Cmd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ private void FlushCommandPipe() {
break;
}

case AudioCommandType.SetTagWithHandle: {
Cmd_SetTagForHandle(cmd.SetTag);
break;
}

case AudioCommandType.SetVoiceBoolParameter: {
Cmd_SetVoiceBoolParameter(cmd.BoolParam);
break;
Expand Down Expand Up @@ -150,6 +155,13 @@ private unsafe void Cmd_StopWithTag(StringHash32 tag, float delay, Curve curve)

#region Params

private unsafe void Cmd_SetTagForHandle(OverwriteTagCommandData tagChange) {
VoiceData voice = FindVoiceForId(tagChange.Handle);
if (voice != null) {
voice.Tag = tagChange.Tag;
}
}

private unsafe void Cmd_SetVoiceBoolParameter(BoolParamChangeCommandData paramChange) {
VoiceData voice = FindVoiceForId(paramChange.Handle.Handle);
if (voice != null) {
Expand Down
9 changes: 7 additions & 2 deletions Assets/FieldDay/Audio/AudioMgr.Voice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,15 @@ private unsafe void UpdateTweens(float deltaTime) {
tween.Source->SetFloat(tween.Property, newVal);

if (finalProgress >= 1) {
if (tween.Linked != UniqueId16.Invalid && tween.KillOnFinish) {
if (tween.Linked != UniqueId16.Invalid) {
VoiceData voice = FindVoiceForId(tween.Linked);
if (voice != null) {
RequestImmediateStop(voice);
if (tween.KillOnFinish) {
voice.KillTweenIndex = -1;
RequestImmediateStop(voice);
} else {
voice.FloatTweens.Indices[(int) tween.Property] = -1;
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion Assets/FieldDay/Audio/AudioMgr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ internal unsafe AudioMgr(Config config) {

if (config.DefaultEmitterProfile) {
m_DefaultEmitterConfig = config.DefaultEmitterProfile.Config;
Game.Assets.AddNamed(config.DefaultEmitterProfile.name, config.DefaultEmitterProfile);
if (!Game.Assets.HasNamed<AudioEmitterProfile>(config.DefaultEmitterProfile.AssetId)) {
Game.Assets.AddNamed(config.DefaultEmitterProfile.AssetId, config.DefaultEmitterProfile);
}
} else {
m_DefaultEmitterConfig = config.Is3D ? AudioEmitterConfig.Default3D : AudioEmitterConfig.Default2D;
}
Expand Down
10 changes: 10 additions & 0 deletions Assets/FieldDay/Audio/Playback/AudioCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ internal enum AudioCommandType : ushort {
PlayClipFromName,
PlayClipFromAssetRef,
PlayFromHandle,
SetTagWithHandle,
StopWithHandle,
StopWithAudioSource,
StopWithTag,
Expand Down Expand Up @@ -124,6 +125,14 @@ internal struct PlayExistingCommandData {
public UniqueId16 Handle;
}

/// <summary>
/// Data for SetTagForHandle
/// </summary>
internal struct OverwriteTagCommandData {
public UniqueId16 Handle;
public StringHash32 Tag;
}

/// <summary>
/// Data for StopWithHandle and StopWithTag
/// </summary>
Expand Down Expand Up @@ -168,6 +177,7 @@ internal struct AudioCommand {
[FieldOffset(0)] public AudioCommandType Type;
//[FieldOffset(4)] public PlayCommandData Play;
[FieldOffset(4)] public PlayExistingCommandData Resume;
[FieldOffset(4)] public OverwriteTagCommandData SetTag;
[FieldOffset(4)] public StopCommandData Stop;
[FieldOffset(4)] public FloatParamChangeCommandData FloatParam;
[FieldOffset(4)] public BoolParamChangeCommandData BoolParam;
Expand Down
33 changes: 33 additions & 0 deletions Assets/FieldDay/Audio/Sfx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,29 @@ static public void Stop(AudioSource source, float fadeDuration) {
});
}

static public void StopAllWithTag(StringHash32 tag) {
Game.Audio?.QueueAudioCommand(new AudioCommand() {
Type = AudioCommandType.StopWithTag,
Stop = new StopCommandData() {
Id = new AudioIdRef() {
Id = tag
}
}
});
}

static public void StopAllWithTag(StringHash32 tag, float fadeDuration) {
Game.Audio?.QueueAudioCommand(new AudioCommand() {
Type = AudioCommandType.StopWithTag,
Stop = new StopCommandData() {
Id = new AudioIdRef() {
Id = tag
},
FadeOut = fadeDuration
}
});
}

static public void StopAll() {
Game.Audio?.QueueAudioCommand(new AudioCommand() {
Type = AudioCommandType.StopAll
Expand All @@ -155,6 +178,16 @@ static public bool IsActive(AudioHandle handle) {

#region Properties

static public void OverrideTag(AudioHandle handle, StringHash32 tag) {
Game.Audio.QueueAudioCommand(new AudioCommand() {
Type = AudioCommandType.SetTagWithHandle,
SetTag = new OverwriteTagCommandData() {
Handle = handle.m_Id,
Tag = tag
}
});
}

static public void SetVolume(AudioHandle handle, float volume, float transitionTime = 0, Curve transitionCurve = Curve.Linear) {
Game.Audio.QueueAudioCommand(new AudioCommand() {
Type = AudioCommandType.SetVoiceFloatParameter,
Expand Down
4 changes: 2 additions & 2 deletions Assets/FieldDay/ECS/Components/ComponentMgr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ public void Unlock()
Assert.True(m_ModificationLock > 0, "Unbalanced Lock/Unlock calls");
if (m_ModificationLock-- == 1)
{
while (m_RemovalQueue.TryPopBack(out IComponentData component))
while (m_RemovalQueue.TryPopFront(out IComponentData component))
{
DeregisterImpl(component);
}
while (m_AddQueue.TryPopBack(out IComponentData component))
while (m_AddQueue.TryPopFront(out IComponentData component))
{
RegisterImpl(component);
}
Expand Down
8 changes: 8 additions & 0 deletions Assets/FieldDay/HID/Cursor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

159 changes: 159 additions & 0 deletions Assets/FieldDay/HID/Cursor/CursorHint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
using System;
using BeauUtil;
using BeauUtil.Debugger;
using BeauUtil.UI;
using FieldDay.Assets;
using UnityEngine;
using UnityEngine.EventSystems;

namespace FieldDay.HID {
[DisallowMultipleComponent]
public class CursorHint : PointerListener {
#region Inspector

[Header("Tooltip")]
[AssetName(typeof(CursorType))] public StringHash32 CursorType;
public string Tooltip;

#endregion // Inspector

/// <summary>
/// Invoked when hovering starts or ends.
/// </summary>
public readonly CastableEvent<CursorHint, bool> OnHover = new CastableEvent<CursorHint, bool>();

#region Unity Events

protected virtual void Awake() {
onPointerEnter.AddListener(OnEnter);
onPointerExit.AddListener(OnExit);
}

protected virtual void OnDisable() {
if (ReferenceEquals(this, s_Pointer)) {
s_Pointer = null;
}
if (ReferenceEquals(this, s_Locked)) {
s_Locked = null;
}
if (ReferenceEquals(this, s_Effective)) {
UpdateEffectiveCursor();
}
}

private void OnEnter(PointerEventData evtData) {
if (!ReferenceEquals(this, s_Pointer)) {
s_Pointer = this;
UpdateEffectiveCursor();
}
}

private void OnExit(PointerEventData evtData) {
if (ReferenceEquals(this, s_Pointer)) {
s_Pointer = null;
UpdateEffectiveCursor();
}
}

#endregion // Unity Events

#region Current Tracking

static private CursorHint s_Pointer;
static private CursorHint s_Locked;
static private CursorHint s_Effective;

static public CursorHint Current {
get { return s_Effective; }
}

static private void UpdateEffectiveCursor() {
CursorHint desiredEffective = s_Locked ? s_Locked : s_Pointer;
CursorHint prev = s_Effective;
if (desiredEffective != prev) {
if (prev) {
prev.OnHover.Invoke(prev, false);
OnHoverStop.Invoke(prev);
}

s_Effective = desiredEffective;
if (desiredEffective) {
desiredEffective.OnHover.Invoke(desiredEffective, true);
OnHoverStart.Invoke(desiredEffective);
}

Log.Msg("[CursorHint] Updated effective focus from '{0}' to '{1}'", prev, desiredEffective);
}
}

static public readonly CastableEvent<CursorHint> OnHoverStart = new CastableEvent<CursorHint>();
static public readonly CastableEvent<CursorHint> OnHoverStop = new CastableEvent<CursorHint>();

#endregion // Current Tracking

#region Locks

/// <summary>
/// Returns if the given hint is the currently locked hint.
/// </summary>
static public bool IsLocked(CursorHint hint) {
return hint == s_Locked;
}

/// <summary>
/// Attempts to lock focus on the given cursor hint.
/// </summary>
static public bool TryLock(CursorHint hint) {
if (hint) {
if (hint.isActiveAndEnabled) {
if (s_Locked != hint) {
if (s_Locked != null) {
Log.Warn("[CursorHint] Attempting to switch lock while '{0}' is currently locked", s_Locked.name);
}
s_Locked = hint;
Log.Debug("[CursorHint] Locked focus on '{0}'", hint.name);
UpdateEffectiveCursor();
return true;
}
} else {
Log.Warn("[CursorHint] Hint '{0}' is currently inactive, and cannot be locked", hint.name);
}
}

return false;
}

/// <summary>
/// Releases the given cursor hint from being locked.
/// </summary>
static public bool Unlock(CursorHint hint) {
if (hint) {
if (s_Locked == hint) {
s_Locked = null;
Log.Debug("[CursorHint] Unlocked focus from '{0}'", hint.name);
UpdateEffectiveCursor();
return true;
}
}

return false;
}

/// <summary>
/// Releases the current cursor hint from being locked.
/// </summary>
static public bool Unlock() {
CursorHint prev = s_Locked;
if (prev != null) {
s_Locked = null;
Log.Debug("[CursorHint] Unlocked focus from '{0}'", prev.name);
UpdateEffectiveCursor();
return true;
}

return false;
}

#endregion // Locks
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Assets/FieldDay/HID/Cursor/CursorType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using BeauUtil;
using FieldDay.Assets;
using UnityEngine;

namespace FieldDay.HID {
[CreateAssetMenu(menuName = "Field Day/Cursor/Cursor Type")]
public class CursorType : NamedAsset {
[Required] public Sprite DefaultImage;
public Sprite HeldImage;
}
}
11 changes: 11 additions & 0 deletions Assets/FieldDay/HID/Cursor/CursorType.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions Assets/FieldDay/HID/InputMgr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ public bool IsMousePressed(int mouseButton) {
return !m_InputConsumed && Input.GetMouseButtonDown(mouseButton);
}

/// <summary>
/// Returns if a mouse button was pressed this frame.
/// </summary>
public bool IsMouseUp(MouseButton mouseButton) {
return !m_InputConsumed && Input.GetMouseButtonUp((int)mouseButton);
}

/// <summary>
/// Returns if a mouse button was pressed this frame.
/// </summary>
public bool IsMouseUp(int mouseButton) {
return !m_InputConsumed && Input.GetMouseButtonUp(mouseButton);
}

#endregion // Clicks

#region Keys
Expand Down
Loading

0 comments on commit f65023f

Please sign in to comment.