SamSWAT.CustomMusic update
This commit is contained in:
parent
bc0b0cd4fb
commit
6414137976
@ -1,6 +1,5 @@
|
||||
using Aki.Reflection.Patching;
|
||||
using EFT.UI;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@ -8,93 +7,100 @@ using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using BepInEx;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace SamSWAT.CustomMusic
|
||||
{
|
||||
public class CustomMusicPatch : ModulePatch
|
||||
{
|
||||
private static Dictionary<string, bool> ReplaceOriginal;
|
||||
private static string CurrDir { get; set; }
|
||||
|
||||
private static List<AudioClip> musicClips = new List<AudioClip>();
|
||||
private static bool _replaceOriginal;
|
||||
private static List<AudioClip> _musicClips = new List<AudioClip>();
|
||||
|
||||
protected override MethodBase GetTargetMethod()
|
||||
{
|
||||
return typeof(GUISounds).GetMethod("method_1",BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
}
|
||||
|
||||
public CustomMusicPatch()
|
||||
{
|
||||
CurrDir = BepInEx.Paths.PluginPath + "/SamSWAT.CustomMusic";
|
||||
string json = new StreamReader(Path.Combine(CurrDir, "config.json")).ReadToEnd();
|
||||
ReplaceOriginal = JsonConvert.DeserializeObject<Dictionary<string, bool>>(json);
|
||||
var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||
var json = File.ReadAllText($"{directory}/config.json");
|
||||
_replaceOriginal = (bool) JObject.Parse(json)["ReplaceOriginalMusic"];
|
||||
|
||||
string[] files = Directory.GetFiles(CurrDir + "/music");
|
||||
var files = Directory.GetFiles($"{directory}/music");
|
||||
|
||||
foreach (var song in files)
|
||||
foreach (var file in files)
|
||||
{
|
||||
Logger.LogInfo(song);
|
||||
string url = "file:///" + song.Replace("\\", "/");
|
||||
if (song.Contains(".mp3"))
|
||||
LoadAudio(url, AudioType.MPEG);
|
||||
else if (song.Contains(".wav"))
|
||||
LoadAudio(url, AudioType.WAV);
|
||||
else if (song.Contains(".ogg"))
|
||||
LoadAudio(url, AudioType.OGGVORBIS);
|
||||
var uri = "file:///" + file.Replace("\\", "/");
|
||||
if (file.Contains(".mp3"))
|
||||
LoadAudio(uri, AudioType.MPEG);
|
||||
else if (file.Contains(".wav"))
|
||||
LoadAudio(uri, AudioType.WAV);
|
||||
else if (file.Contains(".ogg"))
|
||||
LoadAudio(uri, AudioType.OGGVORBIS);
|
||||
else
|
||||
LoadAudio(url, AudioType.UNKNOWN);
|
||||
LoadAudio(uri, AudioType.UNKNOWN);
|
||||
}
|
||||
}
|
||||
|
||||
private async void LoadAudio(string url, AudioType audioType)
|
||||
{
|
||||
using (UnityWebRequest web = UnityWebRequestMultimedia.GetAudioClip(url, audioType))
|
||||
using (var web = UnityWebRequestMultimedia.GetAudioClip(url, audioType))
|
||||
{
|
||||
Logger.LogInfo("url: " + url);
|
||||
var operation = web.SendWebRequest();
|
||||
while (!operation.isDone)
|
||||
{
|
||||
Logger.LogInfo("operation pending..");
|
||||
await Task.Yield();
|
||||
}
|
||||
|
||||
|
||||
if (!web.isNetworkError && !web.isHttpError)
|
||||
{
|
||||
var clip = DownloadHandlerAudioClip.GetContent(web);
|
||||
Logger.LogInfo("AudioClip: " + clip.loadState + " " + clip.frequency);
|
||||
string audioclipname = url.Replace("file:///"+ (CurrDir + "/music/").Replace("\\","/"), "");
|
||||
clip.name = audioclipname;
|
||||
musicClips.Add(clip);
|
||||
clip.name = Path.GetFileNameWithoutExtension(url);
|
||||
_musicClips.Add(clip);
|
||||
}
|
||||
else
|
||||
{
|
||||
var Error = web.error;
|
||||
Debug.LogErrorFormat("Can't load audio at path:'{0}', error:{1}", new object[]
|
||||
{
|
||||
url,
|
||||
Error
|
||||
});
|
||||
Plugin.Logger.LogError($"Can't load audio at path: '{url}', error: {web.error}");
|
||||
Debug.LogErrorFormat("Can't load audio at path: '{0}', error: {1}", url, web.error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override MethodBase GetTargetMethod()
|
||||
|
||||
private static void Shuffle<T>(List<T> list)
|
||||
{
|
||||
return typeof(GUISounds).GetMethod("method_1", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
}
|
||||
|
||||
[PatchPostfix]
|
||||
private static void PatchPostfix(ref AudioClip[] ___audioClip_0, object __instance)
|
||||
{
|
||||
if (ReplaceOriginal["ReplaceOriginalMusic"])
|
||||
___audioClip_0 = musicClips.ToArray();
|
||||
else
|
||||
___audioClip_0 = ___audioClip_0.Concat(musicClips).ToArray();
|
||||
|
||||
foreach (var clip in musicClips)
|
||||
int n = list.Count;
|
||||
while (n > 1)
|
||||
{
|
||||
Logger.LogInfo("clip in array: " + clip.name);
|
||||
n--;
|
||||
int k = Random.Range(0, n + 1);
|
||||
(list[k], list[n]) = (list[n], list[k]);
|
||||
}
|
||||
|
||||
foreach (var clip2 in ___audioClip_0)
|
||||
}
|
||||
|
||||
[PatchPostfix]
|
||||
private static void PatchPostfix(ref AudioClip[] ___audioClip_0)
|
||||
{
|
||||
Shuffle(_musicClips);
|
||||
if (!_musicClips.Any())
|
||||
{
|
||||
Logger.LogInfo("\n \n \n" + "original cips: " + clip2.name);
|
||||
Plugin.Logger.LogFatal("Looks like there was an error loading music or you didn't provide any to SamSWAT.CustomMusic/music folder");
|
||||
Debug.LogErrorFormat("Looks like there was an error loading music or you didn't provide any to SamSWAT.CustomMusic/music folder");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_replaceOriginal)
|
||||
{
|
||||
//if there is only one element in the array, the game will stuck on loading screen for some reason
|
||||
if (_musicClips.Count == 1)
|
||||
{
|
||||
_musicClips.Add(_musicClips[0]);
|
||||
}
|
||||
___audioClip_0 = _musicClips.ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
___audioClip_0 = ___audioClip_0.Concat(_musicClips).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,16 @@
|
||||
using BepInEx;
|
||||
using Aki.Reflection.Patching;
|
||||
using BepInEx;
|
||||
using BepInEx.Logging;
|
||||
|
||||
namespace SamSWAT.CustomMusic
|
||||
{
|
||||
[BepInPlugin("com.samswat.custommusic", "SamSWAT.CustomMusic", "1.1.0")]
|
||||
public class CustomMusicPlugin : BaseUnityPlugin
|
||||
public class Plugin : BaseUnityPlugin
|
||||
{
|
||||
internal static ManualLogSource Logger;
|
||||
private void Awake()
|
||||
{
|
||||
Logger = base.Logger;
|
||||
new CustomMusicPatch().Enable();
|
||||
}
|
||||
}
|
@ -63,7 +63,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="CustomMusicPatch.cs" />
|
||||
<Compile Include="CustomMusicPlugin.cs" />
|
||||
<Compile Include="Plugin.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
|
Loading…
x
Reference in New Issue
Block a user