-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a fix for Yellow-Dog-Man/Resonite-Issues#737
- Loading branch information
Showing
2 changed files
with
100 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using Elements.Assets; | ||
using Elements.Core; | ||
using FrooxEngine; | ||
using FrooxEngine.Store; | ||
using FrooxEngine.Undo; | ||
using HarmonyLib; | ||
using MonkeyLoader.Resonite; | ||
using SkyFrost.Base; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CommunityBugFixCollection | ||
{ | ||
[HarmonyPatchCategory(nameof(ImportMultipleAudioFiles))] | ||
[HarmonyPatch(typeof(UniversalImporter), nameof(UniversalImporter.ImportTask))] | ||
internal sealed class ImportMultipleAudioFiles : ResoniteMonkey<ImportMultipleAudioFiles> | ||
{ | ||
public override bool CanBeDisabled => true; | ||
|
||
private static async Task ImportAudioAsync(IEnumerable<string> files, World world, float3 position, floatQ rotation, float3 scale) | ||
{ | ||
await default(ToWorld); | ||
|
||
if (!world.CanSpawnObjects()) | ||
return; | ||
|
||
var list = files.Where(s => s is not null) | ||
.Select(s => s.Trim()) | ||
.ToList(); | ||
|
||
var count = list.Count; | ||
var rowSize = MathX.Max(1, MathX.CeilToInt(MathX.Sqrt(count))); | ||
var index = 0; | ||
|
||
UniLog.Log($"Importing files with Asset Class {AssetClass.Audio}:\n" + string.Join("\n", list)); | ||
|
||
foreach (var file in list) | ||
{ | ||
await default(ToWorld); | ||
|
||
var slot = world.LocalUserSpace.AddSlot(Path.GetFileName(file)); | ||
slot.CreateSpawnUndoPoint(); | ||
|
||
var gridOffset = UniversalImporter.GridOffset(ref index, rowSize); | ||
var direction = rotation * gridOffset; | ||
var offset = direction * scale; | ||
|
||
slot.GlobalPosition = position + offset; | ||
slot.GlobalRotation = rotation; | ||
slot.GlobalScale = scale; | ||
|
||
await default(ToBackground); | ||
|
||
Uri uri; | ||
if (File.Exists(file)) | ||
uri = await world.Engine.LocalDB.ImportLocalAssetAsync(file, LocalDB.ImportLocation.Original); | ||
else if (!Uri.TryCreate(file, UriKind.Absolute, out uri)) | ||
continue; | ||
|
||
await default(ToWorld); | ||
|
||
var audioPlayerInterface = await slot.SpawnEntity<AudioPlayerInterface, LegacyAudioPlayer>(FavoriteEntity.AudioPlayer); | ||
audioPlayerInterface.InitializeEntity(Path.GetFileName(file)); | ||
audioPlayerInterface.SetSource(uri); | ||
|
||
if (".wav".Equals(Path.GetExtension(file), StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
audioPlayerInterface.SetType(AudioTypeGroup.SoundEffect, spatialize: true, 1f); | ||
continue; | ||
} | ||
|
||
var audioTypeGroup = await UniversalImporter.DetectAudioTypeGroup(audioPlayerInterface.Clip.Target.Target); | ||
|
||
var isMultimedia = audioTypeGroup == AudioTypeGroup.Multimedia; | ||
audioPlayerInterface.SetType(audioTypeGroup, isMultimedia, isMultimedia ? 1 : 0); | ||
} | ||
} | ||
|
||
private static bool Prefix(ref Task? __result, AssetClass assetClass, IEnumerable<string> files, World world, float3 position, floatQ rotation, float3 scale) | ||
{ | ||
if (assetClass != AssetClass.Audio) | ||
return true; | ||
|
||
__result = ImportAudioAsync(files, world, position, rotation, scale); | ||
return false; | ||
} | ||
|
||
private static bool Prepare() => Enabled; | ||
} | ||
} |