Skip to content

Commit

Permalink
Audio
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaPiggy committed Sep 9, 2024
1 parent 50ddbac commit b72f631
Show file tree
Hide file tree
Showing 7 changed files with 478 additions and 40 deletions.
18 changes: 18 additions & 0 deletions Winch/Core/AssetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private static void LoadAssetFolder(string path)
string bundlesFolderpath = Path.Combine(path, "Bundles");
string localizationFolderPath = Path.Combine(path, "Localization");
string textureFolderPath = Path.Combine(path, "Textures");
string audioFolderPath = Path.Combine(path, "Audio");
string gridConfigFolderpath = Path.Combine(path, "GridConfigs");
string itemFolderPath = Path.Combine(path, "Items");
string poiFolderpath = Path.Combine(path, "POI");
Expand All @@ -70,6 +71,7 @@ private static void LoadAssetFolder(string path)
if(Directory.Exists(bundlesFolderpath)) LoadAssetBundleFiles(bundlesFolderpath);
if(Directory.Exists(localizationFolderPath)) LoadLocalizationFiles(localizationFolderPath);
if(Directory.Exists(textureFolderPath)) LoadTextureFiles(textureFolderPath);
if(Directory.Exists(audioFolderPath)) LoadAudioFiles(audioFolderPath);
if(Directory.Exists(gridConfigFolderpath)) LoadGridConfigFiles(gridConfigFolderpath);
if(Directory.Exists(itemFolderPath)) LoadItemFiles(itemFolderPath);
if(Directory.Exists(vibrationFolderpath)) LoadVibrationFiles(vibrationFolderpath);
Expand Down Expand Up @@ -290,6 +292,22 @@ private static void LoadTextureFiles(string textureFolderPath)
}
}

private static void LoadAudioFiles(string audioFolderPath)
{
string[] audioFiles = Directory.GetFiles(audioFolderPath);
foreach (string file in audioFiles)
{
try
{
AudioClipUtil.LoadAudioFromFile(file);
}
catch (Exception ex)
{
WinchCore.Log.Error($"Failed to load audio file {file}: {ex}");
}
}
}

private static void LoadHarvestZoneFiles(string harvestZoneFolderPath)
{
string[] harvestZoneFiles = Directory.GetFiles(harvestZoneFolderPath);
Expand Down
33 changes: 23 additions & 10 deletions Winch/Data/Abilities/ModdedAbilityData.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
using Sirenix.Utilities;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.Localization;
using Winch.Components;
using Winch.Core;
using Winch.Data.Item;
using System.Collections.Generic;
using Winch.Util;

namespace Winch.Data.Abilities;
Expand All @@ -26,6 +18,15 @@ public class ModdedAbilityData : AbilityData
/// </summary>
public bool autoUnlock = true;

/// <summary>
/// IDs of advanced ability data linked to this ability
/// </summary>
public new string linkedAdvancedVersion = string.Empty;

public new string primaryVibration = string.Empty;

public new string secondaryVibration = string.Empty;

/// <summary>
/// IDs of item datas linked to this ability
/// </summary>
Expand All @@ -35,6 +36,18 @@ public class ModdedAbilityData : AbilityData

internal void Populate()
{
if (!string.IsNullOrWhiteSpace(linkedAdvancedVersion))
{
base.linkedAdvancedVersion = AbilityUtil.GetAbilityData(linkedAdvancedVersion);
}
if (!string.IsNullOrWhiteSpace(primaryVibration))
{
base.primaryVibration = VibrationUtil.GetVibrationData(primaryVibration);
}
if (!string.IsNullOrWhiteSpace(secondaryVibration))
{
base.secondaryVibration = VibrationUtil.GetVibrationData(secondaryVibration);
}
if (linkedItems != null)
{
List<ItemData> items = new List<ItemData>();
Expand All @@ -48,4 +61,4 @@ internal void Populate()
base.linkedItems = items.ToArray();
}
}
}
}
10 changes: 8 additions & 2 deletions Winch/Serialization/Ability/AbilityDataConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using UnityEngine.AddressableAssets;
using UnityEngine.Localization;
using Winch.Data.Abilities;
using Winch.Util;
Expand Down Expand Up @@ -33,7 +34,12 @@ public class AbilityDataConverter : DredgeTypeConverter<ModdedAbilityData>
{ "persistAbilityToggle", new(false, o=> bool.Parse(o.ToString())) },
{ "requiresAbilityFocus", new(false, o=> bool.Parse(o.ToString())) },
{ "sfxRepeatThreshold", new(0f, o => float.Parse(o.ToString())) },
{ "showsCounter", new(false, o=> bool.Parse(o.ToString())) }
{ "showsCounter", new(false, o=> bool.Parse(o.ToString())) },
{ "linkedAdvancedVersion", new(string.Empty, null) },
{ "primaryVibration", new(string.Empty, null) },
{ "secondaryVibration", new(string.Empty, null) },
{ "castSFX", new(new AssetReference(), o=>DredgeTypeHelpers.ParseAudioReference(o.ToString())) },
{ "deactivateSFX", new(new AssetReference(), o=>DredgeTypeHelpers.ParseAudioReference(o.ToString())) }
};

public AbilityDataConverter()
Expand All @@ -42,4 +48,4 @@ public AbilityDataConverter()
}

protected static LocalizedString CreateLocalizedString(string value) => CreateLocalizedString(TableDefinition, value);
}
}
12 changes: 6 additions & 6 deletions Winch/Serialization/Dock/DockDataConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public class DockDataConverter : DredgeTypeConverter<DeferredDockData>
{
{ "id", new(string.Empty, null) },
{ "dockNameKey", new(null, o=> CreateLocalizedString(o.ToString())) },
{ "musicAssetReference", new(new AssetReference(), null) },
{ "musicAssetOverrides", new(new List<AssetReferenceOverride>(), null) },
{ "ambienceDayAssetReference", new(new AssetReference(), null) },
{ "ambienceNightAssetReference", new(new AssetReference(), null) },
{ "ambienceDayAssetOverrides", new(new List<AssetReferenceOverride>(), null) },
{ "ambienceNightAssetOverrides", new(new List<AssetReferenceOverride>(), null) },
{ "musicAssetReference", new(new AssetReference(), o=>DredgeTypeHelpers.ParseAudioReference(o.ToString())) },
{ "musicAssetOverrides", new(new List<AssetReferenceOverride>(), o=>DredgeTypeHelpers.ParseAudioReferenceOverrides((JArray)o)) },
{ "ambienceDayAssetReference", new(new AssetReference(), o=>DredgeTypeHelpers.ParseAudioReference(o.ToString())) },
{ "ambienceNightAssetReference", new(new AssetReference(), o=>DredgeTypeHelpers.ParseAudioReference(o.ToString())) },
{ "ambienceDayAssetOverrides", new(new List<AssetReferenceOverride>(), o=>DredgeTypeHelpers.ParseAudioReferenceOverrides((JArray)o)) },
{ "ambienceNightAssetOverrides", new(new List<AssetReferenceOverride>(), o=>DredgeTypeHelpers.ParseAudioReferenceOverrides((JArray)o)) },
{ "yarnRootNode", new(string.Empty, null) },
{ "progressTitleLocalizationKey", new(string.Empty, null) },
{ "progressValueLocalizationKey", new(string.Empty, null) },
Expand Down
122 changes: 121 additions & 1 deletion Winch/Serialization/DredgeTypeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;
using UnityEngine.AddressableAssets;
using Winch.Data;
using Winch.Data.GridConfig;
using Winch.Data.Item.Prerequisites;
Expand Down Expand Up @@ -158,7 +159,7 @@ public static List<Vector2Int> ParseItemDimensions(JArray dimensions)
{
throw new InvalidOperationException("Failed to create item. Dimensions require the upper-left cell to be filled.");
}

return parsed;
}

Expand Down Expand Up @@ -380,4 +381,123 @@ public static DockSafeZone ParseDockSafeZone(JToken o)
radius = radius != null ? float.Parse(radius.ToString()) : 15f,
};
}

public static AssetReference ParseAssetReference(string s)
{
if (string.IsNullOrWhiteSpace(s)) return AddressablesUtil.EmptyAssetReference;

if (Guid.TryParse(s, out _)) return new AssetReference(s);

if (AddressablesUtil.TryGetAssetGUID(s, out string guid))
return new AssetReference(guid);

if (AddressablesUtil.TryGetIdenticalLocationKey(s, out string identical))
return new AssetReference(identical);

return AddressablesUtil.EmptyAssetReference;
}

public static List<AssetReference> ParseAssetReferences(JArray o)
{
var parsed = new List<AssetReference>();
foreach (var s in ParseStringList(o))
{
parsed.Add(ParseAssetReference(s));
}
return parsed;
}

public static AssetReferenceT<TObject> ParseAssetReference<TObject>(string s) where TObject : UnityEngine.Object
{
if (string.IsNullOrWhiteSpace(s)) return new AssetReferenceT<TObject>(string.Empty);

if (Guid.TryParse(s, out _)) return new AssetReferenceT<TObject>(s);

if (AddressablesUtil.TryGetAssetGUID(s, out string guid))
return new AssetReferenceT<TObject>(guid);

if (AddressablesUtil.TryGetIdenticalLocationKey<TObject>(s, out string identical))
return new AssetReferenceT<TObject>(identical);

return new AssetReferenceT<TObject>(string.Empty);
}

public static List<AssetReference> ParseAssetReferences<TObject>(JArray o) where TObject : UnityEngine.Object
{
var parsed = new List<AssetReference>();
foreach (var s in ParseStringList(o))
{
parsed.Add(ParseAssetReference<TObject>(s));
}
return parsed;
}

public static AssetReferenceOverride ParseAssetReferenceOverride(JToken o)
{
if (o.IsNullOrEmpty()) return new AssetReferenceOverride();

var assetReference = o["assetReference"];
var nodesVisited = o["nodesVisited"];
var boolValues = o["boolValues"];
var tirWorldPhase = o["tirWorldPhase"];
return new AssetReferenceOverride
{
assetReference = assetReference != null ? ParseAssetReference(assetReference.ToString()) : AddressablesUtil.EmptyAssetReference,
nodesVisited = nodesVisited != null ? ParseStringList((JArray)nodesVisited) : new List<string>(),
boolValues = boolValues != null ? ParseStringList((JArray)boolValues) : new List<string>(),
tirWorldPhase = tirWorldPhase != null ? int.Parse(tirWorldPhase.ToString()) : 0,
};
}

public static List<AssetReferenceOverride> ParseAssetReferenceOverrides(JArray o)
{
var parsed = new List<AssetReferenceOverride>();
foreach (var refOverride in o)
{
parsed.Add(ParseAssetReferenceOverride(refOverride));
}
return parsed;
}

public static AssetReference ParseAudioReference(string o)
{
return ParseAssetReference<AudioClip>(o);
}

public static List<AssetReference> ParseAudioReferences(JArray o)
{
var parsed = new List<AssetReference>();
foreach (var s in ParseStringList(o))
{
parsed.Add(ParseAudioReference(s));
}
return parsed;
}

public static AssetReferenceOverride ParseAudioReferenceOverride(JToken o)
{
if (o.IsNullOrEmpty()) return new AssetReferenceOverride();

var assetReference = o["assetReference"];
var nodesVisited = o["nodesVisited"];
var boolValues = o["boolValues"];
var tirWorldPhase = o["tirWorldPhase"];
return new AssetReferenceOverride
{
assetReference = assetReference != null ? ParseAudioReference(assetReference.ToString()) : AddressablesUtil.EmptyAssetReference,
nodesVisited = nodesVisited != null ? ParseStringList((JArray)nodesVisited) : new List<string>(),
boolValues = boolValues != null ? ParseStringList((JArray)boolValues) : new List<string>(),
tirWorldPhase = tirWorldPhase != null ? int.Parse(tirWorldPhase.ToString()) : 0,
};
}

public static List<AssetReferenceOverride> ParseAudioReferenceOverrides(JArray o)
{
var parsed = new List<AssetReferenceOverride>();
foreach (var refOverride in o)
{
parsed.Add(ParseAudioReferenceOverride(refOverride));
}
return parsed;
}
}
Loading

0 comments on commit b72f631

Please sign in to comment.