muzika-gromche/MuzikaGromche/Plugin.cs

423 lines
15 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using BepInEx;
using BepInEx.Configuration;
using CSync.Extensions;
using CSync.Lib;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Networking;
namespace MuzikaGromche
{
[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
[BepInDependency("com.sigurd.csync", "5.0.1")]
public class Plugin : BaseUnityPlugin
{
internal new static Config Config { get; private set; } = null!;
public static Track[] Tracks = [
new Track
{
Name = "MuzikaGromche",
WindUpTimer = 46.3f,
Bpm = 130f,
},
new Track
{
Name = "VseVZale",
WindUpTimer = 39f,
Bpm = 138f,
},
new Track
{
Name = "DeployDestroy",
WindUpTimer = 40.7f,
Bpm = 130f,
},
new Track
{
Name = "MoyaZhittya",
WindUpTimer = 34.5f,
Bpm = 120f,
},
new Track
{
Name = "Gorgorod",
WindUpTimer = 43.2f,
Bpm = 180f,
},
new Track
{
Name = "Durochka",
WindUpTimer = 37f,
Bpm = 130f,
}
];
public static int IndexOfTrack(string trackName)
{
return Array.FindIndex(Tracks, track => track.Name == trackName);
}
public static Track ChooseTrack()
{
var seed = RoundManager.Instance.dungeonGenerator.Generator.ChosenSeed;
int[] weights = [.. Tracks.Select(track => track.Weight.Value)];
var rwi = new RandomWeightedIndex(weights);
var trackId = rwi.GetRandomWeightedIndex(seed);
#if DEBUG
// Override for testing
// trackId = IndexOfTrack("DeployDestroy");
#endif
var track = Tracks[trackId];
Debug.Log($"Seed is {seed}, chosen track is \"{track.Name}\", #{trackId} of {rwi}");
return Tracks[trackId];
}
public static Coroutine JesterLightSwitching;
public static Track CurrentTrack;
public static void StartLightSwitching(MonoBehaviour __instance)
{
StopLightSwitching(__instance);
JesterLightSwitching = __instance.StartCoroutine(RotateColors());
}
public static void StopLightSwitching(MonoBehaviour __instance)
{
if (JesterLightSwitching != null)
{
__instance.StopCoroutine(JesterLightSwitching);
JesterLightSwitching = null;
}
}
public static void SetLightColor(Color color)
{
foreach (var light in RoundManager.Instance.allPoweredLights)
{
light.color = color;
}
}
public static void ResetLightColor()
{
SetLightColor(Color.white);
}
// TODO: Move to Track class to make them customizable per-song
static List<Color> colors = [Color.magenta, Color.cyan, Color.green, Color.yellow];
public static IEnumerator RotateColors()
{
Debug.Log("Starting color rotation");
var i = 0;
while (true)
{
var color = colors[i];
Debug.Log("Chose color " + color);
SetLightColor(color);
i = (i + 1) % colors.Count;
if (CurrentTrack != null)
{
yield return new WaitForSeconds(60f / CurrentTrack.Bpm);
}
else
{
yield break;
}
}
}
private void Awake()
{
string text = Info.Location.TrimEnd((PluginInfo.PLUGIN_NAME + ".dll").ToCharArray());
UnityWebRequest[] requests = new UnityWebRequest[Tracks.Length * 2];
for (int i = 0; i < Tracks.Length; i++)
{
Track track = Tracks[i];
requests[i * 2] = UnityWebRequestMultimedia.GetAudioClip($"File://{text}{track.FileNameStart}", track.AudioType);
requests[i * 2 + 1] = UnityWebRequestMultimedia.GetAudioClip($"File://{text}{track.FileNameLoop}", track.AudioType);
requests[i * 2].SendWebRequest();
requests[i * 2 + 1].SendWebRequest();
}
while (!requests.All(request => request.isDone)) { }
if (requests.All(request => request.result == UnityWebRequest.Result.Success))
{
for (int i = 0; i < Tracks.Length; i++)
{
Track track = Tracks[i];
track.LoadedStart = DownloadHandlerAudioClip.GetContent(requests[i * 2]);
track.LoadedLoop = DownloadHandlerAudioClip.GetContent(requests[i * 2 + 1]);
}
Config = new Config(base.Config);
new Harmony(PluginInfo.PLUGIN_NAME).PatchAll(typeof(JesterPatch));
}
else
{
Logger.LogError("Could not load audio file");
}
}
}
public class Track
{
public string Name;
// Wind-up time can and should be shorter than the Start audio track,
// so that the "pop" effect can be baked into the Start audio and kept away
// from the looped part. This also means that the light show starts before
// the looped track does, so we need to sync them up as soon as we enter the Loop.
public float WindUpTimer;
// BPM for light switching in sync with the music. There is no offset,
// so the Loop track should start precisely on a beat.
public float Bpm;
// MPEG is basically mp3, and it can produce gaps at the start.
// WAV is OK, but takes a lot of space. Try OGGVORBIS instead.
public AudioType AudioType = AudioType.MPEG;
public AudioClip LoadedStart;
public AudioClip LoadedLoop;
// How often this track should be chosen, relative to the sum of weights of all tracks.
public SyncedEntry<int> Weight;
public string FileNameStart => $"{Name}Start.{Ext}";
public string FileNameLoop => $"{Name}Loop.{Ext}";
private string Ext => AudioType switch
{
AudioType.MPEG => "mp3",
AudioType.WAV => "wav",
AudioType.OGGVORBIS => "ogg",
_ => "",
};
}
public readonly struct RandomWeightedIndex
{
public RandomWeightedIndex(int[] weights)
{
Weights = weights;
TotalWeights = Weights.Sum();
if (TotalWeights == 0)
{
// If everything is set to zero, everything is equally possible
Weights = [.. Weights.Select(_ => 1)];
TotalWeights = Weights.Length;
}
}
private byte[] GetHash(int seed)
{
var buffer = new byte[4 * (1 + Weights.Length)];
var offset = 0;
System.Buffer.BlockCopy(BitConverter.GetBytes(seed), 0, buffer, offset, sizeof(int));
// Make sure that tweaking weights even a little drastically changes the outcome
foreach (var weight in Weights)
{
offset += 4;
System.Buffer.BlockCopy(BitConverter.GetBytes(weight), 0, buffer, offset, sizeof(int));
}
var sha = SHA256.Create();
var hash = sha.ComputeHash(buffer);
return hash;
}
private int GetRawIndex(byte[] hash)
{
if (TotalWeights == 0)
{
// Should not happen, but what if Weights array is empty?
return -1;
}
var index = 0;
foreach (var t in hash)
{
// modulus division on byte array
index *= 256 % TotalWeights;
index %= TotalWeights;
index += t % TotalWeights;
index %= TotalWeights;
}
return index;
}
private int GetRawIndex(int rawIndex)
{
if (rawIndex < 0 || rawIndex >= TotalWeights)
{
return -1;
}
int sum = 0;
foreach (var (weight, index) in Weights.Select((x, i) => (x, i)))
{
sum += weight;
if (rawIndex < sum)
{
// Found
return index;
}
}
return -1;
}
public int GetRandomWeightedIndex(int seed)
{
var hash = GetHash(seed);
var index = GetRawIndex(hash);
return GetRawIndex(index);
}
public override string ToString()
{
return $"Weighted(Total={TotalWeights}, Weights=[{string.Join(',', Weights)}])";
}
readonly private int[] Weights;
readonly public int TotalWeights { get; }
}
public class Config : SyncedConfig2<Config>
{
// TODO: Comment out for now
// Personal preferrence, explicitly not synced
public ConfigEntry<bool> DisplayLyrics { get; private set; }
public Config(ConfigFile configFile) : base(PluginInfo.PLUGIN_GUID)
{
// TODO: Comment out for now
DisplayLyrics = configFile.Bind("General", "Display Lyrics", false, "Toggle lyrics display");
var chanceRange = new AcceptableValueRange<int>(0, 100);
foreach (var track in Plugin.Tracks)
{
string description = $"Random (relative) chance of selecting track {track.Name}. Set to zero to effectively disable the track.";
track.Weight = configFile.BindSyncedEntry(
new ConfigDefinition("Tracks", track.Name),
50,
new ConfigDescription(description, chanceRange, track));
}
foreach (var track in Plugin.Tracks)
{
// This is basically what ConfigFile.PopulateEntryContainer does
SyncedEntryBase entryBase = track.Weight;
EntryContainer.Add(entryBase.BoxedEntry.ToSyncedEntryIdentifier(), entryBase);
}
// TODO: Remove debug lines
this.InitialSyncCompleted += (sender, e) =>
{
var weights = string.Join(',', Plugin.Tracks.Select(track => track.Weight.Value));
Debug.Log($"MuzikaGromche Sync complete! weights={weights}");
};
foreach (var track in Plugin.Tracks)
{
track.Weight.Changed += (sender, e) =>
{
var track = (Track)e.ChangedEntry.Entry.Description.Tags[0];
Debug.Log($"MuzikaGromche Sync changed {track.Name}: {e.OldValue} -> {e.NewValue}");
};
}
ConfigManager.Register(this);
}
}
[HarmonyPatch(typeof(JesterAI))]
internal class JesterPatch
{
#if DEBUG
[HarmonyPatch("SetJesterInitialValues")]
[HarmonyPostfix]
public static void AlmostInstantFollowTimerPostfix(JesterAI __instance)
{
__instance.beginCrankingTimer = 1f;
}
#endif
[HarmonyPatch("Update")]
[HarmonyPrefix]
public static void DoNotStopTheMusicPrefix(JesterAI __instance, out State __state)
{
__state = new State
{
previousState = __instance.previousState
};
if (__instance.currentBehaviourStateIndex == 2 && __instance.previousState != 2)
{
// if just popped out
// then override farAudio so that vanilla logic does not stop the music
__state.farAudio = __instance.farAudio;
__instance.farAudio = __instance.creatureVoice;
}
}
[HarmonyPatch("Update")]
[HarmonyPostfix]
public static void DoNotStopTheMusic(JesterAI __instance, State __state)
{
if (__state.farAudio != null)
{
__instance.farAudio = __state.farAudio;
}
if (__instance.previousState == 1 && __state.previousState != 1)
{
// if just started winding up
// then stop the default music...
__instance.farAudio.Stop();
__instance.creatureVoice.Stop();
// ...and start modded music
Plugin.CurrentTrack = Plugin.ChooseTrack();
__instance.popUpTimer = Plugin.CurrentTrack.WindUpTimer;
__instance.farAudio.maxDistance = 150;
__instance.farAudio.clip = Plugin.CurrentTrack.LoadedStart;
__instance.farAudio.loop = false;
Debug.Log($"Playing start music: maxDistance: {__instance.farAudio.maxDistance}, minDistance: {__instance.farAudio.minDistance}, volume: {__instance.farAudio.volume}, spread: {__instance.farAudio.spread}");
__instance.farAudio.Play();
}
if (__instance.previousState == 2 && __state.previousState != 2)
{
__instance.creatureVoice.Stop();
Plugin.StartLightSwitching(__instance);
}
if (__instance.previousState != 2 && __state.previousState == 2)
{
Plugin.StopLightSwitching(__instance);
Plugin.ResetLightColor();
}
if (__instance.previousState == 2 && !__instance.creatureVoice.isPlaying)
{
__instance.creatureVoice.maxDistance = 150;
__instance.creatureVoice.clip = Plugin.CurrentTrack.LoadedLoop;
var time = __instance.farAudio.time;
var delay = Plugin.CurrentTrack.LoadedStart.length - time;
Debug.Log($"Start length: {Plugin.CurrentTrack.LoadedStart.length}; played time: {time}");
Debug.Log($"Playing loop music: maxDistance: {__instance.creatureVoice.maxDistance}, minDistance: {__instance.creatureVoice.minDistance}, volume: {__instance.creatureVoice.volume}, spread: {__instance.creatureVoice.spread}, in seconds: {delay}");
__instance.creatureVoice.PlayDelayed(delay);
}
}
}
internal class State
{
public AudioSource farAudio;
public int previousState;
}
}