forked from nikita/muzika-gromche
				
			Compare commits
	
		
			7 Commits
		
	
	
		
			8518e0f62d
			...
			cb85bcb72c
		
	
	| Author | SHA1 | Date | 
|---|---|---|
|  | cb85bcb72c | |
|  | e2ae6873b8 | |
|  | 0b2b8992a5 | |
|  | ceb11e36f4 | |
|  | 566bc0993e | |
|  | df4418b040 | |
|  | ec18c12aa8 | 
|  | @ -5,4 +5,7 @@ riderModule.iml | ||||||
| /_ReSharper.Caches/ | /_ReSharper.Caches/ | ||||||
| .idea/ | .idea/ | ||||||
| *.dll | *.dll | ||||||
|  | .vs/ | ||||||
|  | dist/ | ||||||
| MuzikaGromche.sln.DotSettings.user | MuzikaGromche.sln.DotSettings.user | ||||||
|  | MuzikaGromche.zip | ||||||
|  |  | ||||||
|  | @ -1 +1,3 @@ | ||||||
| *.mp3 filter=lfs diff=lfs merge=lfs -text | *.mp3 filter=lfs diff=lfs merge=lfs -text | ||||||
|  | *.ogg filter=lfs diff=lfs merge=lfs -text | ||||||
|  | *.wav filter=lfs diff=lfs merge=lfs -text | ||||||
|  |  | ||||||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								Assets/MuzikaGromcheLoop.mp3 (Stored with Git LFS)
								
								
								
								
							
							
						
						
									
										
											BIN
										
									
								
								Assets/MuzikaGromcheLoop.mp3 (Stored with Git LFS)
								
								
								
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								Assets/MuzikaGromcheStart.mp3 (Stored with Git LFS)
								
								
								
								
							
							
						
						
									
										
											BIN
										
									
								
								Assets/MuzikaGromcheStart.mp3 (Stored with Git LFS)
								
								
								
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							|  | @ -10,15 +10,16 @@ using UnityEngine.Networking; | ||||||
| 
 | 
 | ||||||
| namespace MuzikaGromche | namespace MuzikaGromche | ||||||
| { | { | ||||||
|     [BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)] | 	[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)] | ||||||
|     public class Plugin : BaseUnityPlugin | 	public class Plugin : BaseUnityPlugin | ||||||
|     { | 	{ | ||||||
| 		public static Track[] Tracks = [ | 		public static Track[] Tracks = [ | ||||||
| 			new Track | 			new Track | ||||||
| 			{ | 			{ | ||||||
| 				Name = "MuzikaGromche", | 				Name = "MuzikaGromche", | ||||||
| 				WindUpTimer = 46.3f, | 				WindUpTimer = 46.3f, | ||||||
| 				Bpm = 130f, | 				Bpm = 130f, | ||||||
|  | 				AudioType = AudioType.OGGVORBIS, | ||||||
| 			}, | 			}, | ||||||
| 			new Track | 			new Track | ||||||
| 			{ | 			{ | ||||||
|  | @ -49,20 +50,75 @@ namespace MuzikaGromche | ||||||
| 				Name = "Durochka", | 				Name = "Durochka", | ||||||
| 				WindUpTimer = 37f, | 				WindUpTimer = 37f, | ||||||
| 				Bpm = 130f, | 				Bpm = 130f, | ||||||
| 			} | 			}, | ||||||
|  | 			new Track | ||||||
|  | 			{ | ||||||
|  | 				Name = "GodMode", | ||||||
|  | 				WindUpTimer = 40.38f, | ||||||
|  | 				Bpm = 108f, | ||||||
|  | 				AudioType = AudioType.OGGVORBIS, | ||||||
|  | 			}, | ||||||
| 		]; | 		]; | ||||||
| 
 | 
 | ||||||
| 		public static Coroutine JesterLightSwitching; | 		public static Coroutine JesterLightSwitching; | ||||||
| 		public static Track CurrentTrack; | 		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() | 		private void Awake() | ||||||
| 		{ | 		{ | ||||||
| 			string text = Info.Location.TrimEnd((PluginInfo.PLUGIN_NAME + ".dll").ToCharArray()); | 			string text = Info.Location.TrimEnd((PluginInfo.PLUGIN_NAME + ".dll").ToCharArray()); | ||||||
| 			UnityWebRequest[] requests = new UnityWebRequest[Tracks.Length * 2]; | 			UnityWebRequest[] requests = new UnityWebRequest[Tracks.Length * 2]; | ||||||
| 			for (int i = 0; i < Tracks.Length; i++) { | 			for (int i = 0; i < Tracks.Length; i++) { | ||||||
| 				Track track = Tracks[i]; | 				Track track = Tracks[i]; | ||||||
| 				requests[i * 2] = UnityWebRequestMultimedia.GetAudioClip($"File://{text}{track.Name}Start.mp3", AudioType.MPEG); | 				requests[i * 2] = UnityWebRequestMultimedia.GetAudioClip($"File://{text}{track.FileNameStart}", track.AudioType); | ||||||
| 				requests[i * 2 + 1] = UnityWebRequestMultimedia.GetAudioClip($"File://{text}{track.Name}Loop.mp3", AudioType.MPEG); | 				requests[i * 2 + 1] = UnityWebRequestMultimedia.GetAudioClip($"File://{text}{track.FileNameLoop}", track.AudioType); | ||||||
| 				requests[i * 2].SendWebRequest(); | 				requests[i * 2].SendWebRequest(); | ||||||
| 				requests[i * 2 + 1].SendWebRequest(); | 				requests[i * 2 + 1].SendWebRequest(); | ||||||
| 			} | 			} | ||||||
|  | @ -71,141 +127,130 @@ namespace MuzikaGromche | ||||||
| 
 | 
 | ||||||
| 			if (requests.All(request => request.result == UnityWebRequest.Result.Success)) { | 			if (requests.All(request => request.result == UnityWebRequest.Result.Success)) { | ||||||
| 				for (int i = 0; i < Tracks.Length; i++) { | 				for (int i = 0; i < Tracks.Length; i++) { | ||||||
| 					Tracks[i].LoadedStart = DownloadHandlerAudioClip.GetContent(requests[i * 2]); | 					Track track = Tracks[i]; | ||||||
| 					Tracks[i].LoadedLoop = DownloadHandlerAudioClip.GetContent(requests[i * 2 + 1]); | 					track.LoadedStart = DownloadHandlerAudioClip.GetContent(requests[i * 2]); | ||||||
|  | 					track.LoadedLoop = DownloadHandlerAudioClip.GetContent(requests[i * 2 + 1]); | ||||||
| 				} | 				} | ||||||
| 				new Harmony(PluginInfo.PLUGIN_NAME).PatchAll(typeof(JesterPatch)); | 				new Harmony(PluginInfo.PLUGIN_NAME).PatchAll(typeof(JesterPatch)); | ||||||
| 			} else { | 			} else { | ||||||
| 				Logger.LogError("Could not load audio file"); | 				Logger.LogError("Could not load audio file"); | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|     } | 	} | ||||||
| 
 | 
 | ||||||
|     public class Track | 	public class Track | ||||||
|     { | 	{ | ||||||
| 	    public string Name; | 		public string Name; | ||||||
| 	    public float WindUpTimer; | 		// Wind-up time can be shorter than the Start audio track, so that | ||||||
| 	    public float Bpm; | 		// the "pop" effect can be baked in the Start audio and kept away | ||||||
| 	    public AudioClip LoadedStart; | 		// from the looped part. | ||||||
| 	    public AudioClip LoadedLoop; | 		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; | ||||||
| 
 | 
 | ||||||
|     [HarmonyPatch(typeof(JesterAI))] | 		// MPEG is basically mp3, and it can produce gaps at the start. | ||||||
|     internal class JesterPatch | 		// WAV is OK, but takes a lot of space. Try OGGVORBIS instead. | ||||||
|     { | 		public AudioType AudioType = AudioType.MPEG; | ||||||
| 	    [HarmonyPatch("Update")] |  | ||||||
| 	    [HarmonyPrefix] |  | ||||||
| 	    public static void DoNotStopTheMusicPrefix(JesterAI __instance, out State __state) |  | ||||||
| 	    { |  | ||||||
| 		    __state = new State(); |  | ||||||
| 		    __state.prevStateindex = __instance.previousState; |  | ||||||
| 		    if (__instance.currentBehaviourStateIndex == 2 && __instance.previousBehaviourStateIndex != 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; |  | ||||||
| 		    } |  | ||||||
| 	    } |  | ||||||
| 
 | 
 | ||||||
| 	    static List<Color> colors = [Color.magenta, Color.cyan, Color.green, Color.yellow]; | 		public AudioClip LoadedStart; | ||||||
|  | 		public AudioClip LoadedLoop; | ||||||
| 
 | 
 | ||||||
| 	    public static IEnumerator rotateColors() | 		public string FileNameStart => $"{Name}Start.{ext}"; | ||||||
| 	    { | 		public string FileNameLoop => $"{Name}Loop.{ext}"; | ||||||
| 		    Debug.Log("Starting color rotation"); | 		private string ext => AudioType switch | ||||||
| 		    var i = 0; | 		{ | ||||||
| 		    while (true) | 			AudioType.MPEG => "mp3", | ||||||
| 		    { | 			AudioType.WAV => "wav", | ||||||
| 			    var color = colors[i]; | 			AudioType.OGGVORBIS => "ogg", | ||||||
| 			    Debug.Log("Chose color " + color); | 			_ => "", | ||||||
| 			    foreach (var light in RoundManager.Instance.allPoweredLights) | 		}; | ||||||
| 			    { | 	} | ||||||
| 				    light.color = color; |  | ||||||
| 			    } |  | ||||||
| 
 | 
 | ||||||
| 			    i += 1; | 	[HarmonyPatch(typeof(JesterAI))] | ||||||
| 			    if (i >= colors.Count) i = 0; | 	internal class JesterPatch | ||||||
| 			    yield return new WaitForSeconds(60f / Plugin.CurrentTrack.Bpm); | 	{ | ||||||
| 		    } | 		[HarmonyPatch("Update")] | ||||||
| 	    } | 		[HarmonyPrefix] | ||||||
|  | 		public static void DoNotStopTheMusicPrefix(JesterAI __instance, out State __state) | ||||||
|  | 		{ | ||||||
|  | 			__state = new State(); | ||||||
|  | 			__state.prevStateindex = __instance.previousState; | ||||||
|  | 			if (__instance.currentBehaviourStateIndex == 2 && __instance.previousBehaviourStateIndex != 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")] | 		[HarmonyPatch("Update")] | ||||||
|         [HarmonyPostfix] | 		[HarmonyPostfix] | ||||||
|         public static void DoNotStopTheMusic(JesterAI __instance, State __state) | 		public static void DoNotStopTheMusic(JesterAI __instance, State __state) | ||||||
|         { | 		{ | ||||||
| 	        if (__state.farAudio != null) | 			if (__state.farAudio != null) | ||||||
| 	        { | 			{ | ||||||
| 		        __instance.farAudio = __state.farAudio; | 				__instance.farAudio = __state.farAudio; | ||||||
| 	        } | 			} | ||||||
| 
 | 
 | ||||||
| 	        if (__instance.currentBehaviourStateIndex is 1 && __state.prevStateindex != 1) | 			if (__instance.currentBehaviourStateIndex is 1 && __state.prevStateindex != 1) | ||||||
| 	        { | 			{ | ||||||
| 		        // if just started winding up | 				// if just started winding up | ||||||
| 		        // then stop the default music... | 				// then stop the default music... | ||||||
| 		        __instance.farAudio.Stop(); | 				__instance.farAudio.Stop(); | ||||||
| 		        __instance.creatureVoice.Stop(); | 				__instance.creatureVoice.Stop(); | ||||||
| 
 | 
 | ||||||
| 		        //  ...and start modded music | 				//  ...and start modded music | ||||||
| 		        var seed = RoundManager.Instance.dungeonGenerator.Generator.ChosenSeed; | 				var seed = RoundManager.Instance.dungeonGenerator.Generator.ChosenSeed; | ||||||
| 		        var sha = SHA256.Create(); | 				var sha = SHA256.Create(); | ||||||
| 		        var hash = sha.ComputeHash(BitConverter.GetBytes(seed)); | 				var hash = sha.ComputeHash(BitConverter.GetBytes(seed)); | ||||||
| 		        var trackId = 0; | 				var trackId = 0; | ||||||
| 		        foreach (var t in hash) | 				foreach (var t in hash) | ||||||
| 		        { | 				{ | ||||||
| 			        // modulus division on byte array | 					// modulus division on byte array | ||||||
| 			        trackId *= 256 % Plugin.Tracks.Length; | 					trackId *= 256 % Plugin.Tracks.Length; | ||||||
| 			        trackId %= Plugin.Tracks.Length; | 					trackId %= Plugin.Tracks.Length; | ||||||
| 			        trackId += t % Plugin.Tracks.Length; | 					trackId += t % Plugin.Tracks.Length; | ||||||
| 			        trackId %= Plugin.Tracks.Length; | 					trackId %= Plugin.Tracks.Length; | ||||||
| 		        } | 				} | ||||||
| 		        Debug.Log($"Seed is {seed}, chosen track is {trackId} out of {Plugin.Tracks.Length} tracks"); | 				Debug.Log($"Seed is {seed}, chosen track is {trackId} out of {Plugin.Tracks.Length} tracks"); | ||||||
| 		        Plugin.CurrentTrack = Plugin.Tracks[trackId]; | 				Plugin.CurrentTrack = Plugin.Tracks[trackId]; | ||||||
| 		        __instance.popUpTimer = Plugin.CurrentTrack.WindUpTimer; | 				__instance.popUpTimer = Plugin.CurrentTrack.WindUpTimer; | ||||||
| 		        __instance.farAudio.maxDistance = 150; | 				__instance.farAudio.maxDistance = 150; | ||||||
| 		        __instance.farAudio.clip = Plugin.CurrentTrack.LoadedStart; | 				__instance.farAudio.clip = Plugin.CurrentTrack.LoadedStart; | ||||||
| 		        __instance.farAudio.loop = false; | 				__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}"); | 				Debug.Log($"Playing start music: maxDistance: {__instance.farAudio.maxDistance}, minDistance: {__instance.farAudio.minDistance}, volume: {__instance.farAudio.volume}, spread: {__instance.farAudio.spread}"); | ||||||
| 		        __instance.farAudio.Play(); | 				__instance.farAudio.Play(); | ||||||
| 	        } | 			} | ||||||
| 
 | 
 | ||||||
| 	        if (__instance.currentBehaviourStateIndex is 2 && __state.prevStateindex != 2) | 			if (__instance.currentBehaviourStateIndex is 2 && __state.prevStateindex != 2) | ||||||
| 	        { | 			{ | ||||||
| 		        __instance.creatureVoice.Stop(); | 				__instance.creatureVoice.Stop(); | ||||||
|  | 				Plugin.StartLightSwitching(__instance); | ||||||
|  | 			} | ||||||
| 
 | 
 | ||||||
| 		        if (Plugin.JesterLightSwitching != null) { | 			if (__instance.currentBehaviourStateIndex != 2 && __state.prevStateindex == 2) | ||||||
| 			        __instance.StopCoroutine(Plugin.JesterLightSwitching); | 			{ | ||||||
| 			        Plugin.JesterLightSwitching = null; | 				Plugin.StopLightSwitching(__instance); | ||||||
| 		        } | 				Plugin.ResetLightColor(); | ||||||
| 		        Plugin.JesterLightSwitching = __instance.StartCoroutine(rotateColors()); | 			} | ||||||
| 	        } |  | ||||||
| 
 | 
 | ||||||
| 	        if (__instance.currentBehaviourStateIndex != 2 && __state.prevStateindex == 2) | 			if (__instance.currentBehaviourStateIndex is 2 && !__instance.creatureVoice.isPlaying) | ||||||
| 	        { | 			{ | ||||||
| 		        if (Plugin.JesterLightSwitching != null) { | 				__instance.creatureVoice.maxDistance = 150; | ||||||
| 			        __instance.StopCoroutine(Plugin.JesterLightSwitching); | 				__instance.creatureVoice.clip = Plugin.CurrentTrack.LoadedLoop; | ||||||
| 			        Plugin.JesterLightSwitching = null; | 				var time = __instance.farAudio.time; | ||||||
| 		        } | 				var delay = Plugin.CurrentTrack.LoadedStart.length - time; | ||||||
| 		        foreach (var light in RoundManager.Instance.allPoweredLights) | 				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}"); | ||||||
| 			        light.color = Color.white; | 				__instance.creatureVoice.PlayDelayed(delay); | ||||||
| 		        } | 			} | ||||||
| 	        } | 		} | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	        if (__instance.currentBehaviourStateIndex is 2 && !__instance.creatureVoice.isPlaying) | 	internal class State | ||||||
| 	        { | 	{ | ||||||
| 		        __instance.creatureVoice.maxDistance = 150; | 		public AudioSource farAudio; | ||||||
| 		        __instance.creatureVoice.clip = Plugin.CurrentTrack.LoadedLoop; | 		public int prevStateindex; | ||||||
| 		        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 prevStateindex; |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -0,0 +1,10 @@ | ||||||
|  | { | ||||||
|  |     "name": "MuzikaGromche", | ||||||
|  |     "version_number": "13.37.6", | ||||||
|  |     "author": "Oflor", | ||||||
|  |     "description": "Glaza zakryvaj", | ||||||
|  | 	"website_url": "https://git.vilunov.me/nikita/muzika-gromche", | ||||||
|  |     "dependencies": [ | ||||||
|  |       "BepInEx-BepInExPack-5.4.2100" | ||||||
|  |     ] | ||||||
|  |   } | ||||||
		Loading…
	
		Reference in New Issue