forked from nikita/muzika-gromche
				
			Compare commits
	
		
			No commits in common. "f87da1dfed980d27eb351e8a4fa2a65e43f39f36" and "787f15944af612492d228dcf67ee62f427996783" have entirely different histories.
		
	
	
		
			f87da1dfed
			...
			787f15944a
		
	
		|  | @ -15,8 +15,6 @@ | ||||||
|         <PackageReference Include="BepInEx.PluginInfoProps" Version="1.*"/> |         <PackageReference Include="BepInEx.PluginInfoProps" Version="1.*"/> | ||||||
|         <PackageReference Include="UnityEngine.Modules" Version="2022.3.9" IncludeAssets="compile"/> |         <PackageReference Include="UnityEngine.Modules" Version="2022.3.9" IncludeAssets="compile"/> | ||||||
|         <PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.1" PrivateAssets="all" /> |         <PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.1" PrivateAssets="all" /> | ||||||
|         <!-- Publicize internal methods, so we could generate config entries for tracks at runtime instead of generating code at compile time --> |  | ||||||
|         <PackageReference Include="Sigurd.BepInEx.CSync" Version="5.0.1" Publicize="true" /> |  | ||||||
|     </ItemGroup> |     </ItemGroup> | ||||||
| 
 | 
 | ||||||
|     <ItemGroup> |     <ItemGroup> | ||||||
|  |  | ||||||
|  | @ -4,9 +4,6 @@ using System.Collections.Generic; | ||||||
| using System.Linq; | using System.Linq; | ||||||
| using System.Security.Cryptography; | using System.Security.Cryptography; | ||||||
| using BepInEx; | using BepInEx; | ||||||
| using BepInEx.Configuration; |  | ||||||
| using CSync.Extensions; |  | ||||||
| using CSync.Lib; |  | ||||||
| using HarmonyLib; | using HarmonyLib; | ||||||
| using UnityEngine; | using UnityEngine; | ||||||
| using UnityEngine.Networking; | using UnityEngine.Networking; | ||||||
|  | @ -14,11 +11,8 @@ 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)] | ||||||
|     [BepInDependency("com.sigurd.csync", "5.0.1")] |  | ||||||
|     public class Plugin : BaseUnityPlugin |     public class Plugin : BaseUnityPlugin | ||||||
|     { |     { | ||||||
|         internal new static Config Config { get; private set; } = null!; |  | ||||||
| 
 |  | ||||||
|         public static Track[] Tracks = [ |         public static Track[] Tracks = [ | ||||||
|             new Track |             new Track | ||||||
|             { |             { | ||||||
|  | @ -58,26 +52,6 @@ namespace MuzikaGromche | ||||||
|             } |             } | ||||||
|         ]; |         ]; | ||||||
| 
 | 
 | ||||||
|         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 Coroutine JesterLightSwitching; | ||||||
|         public static Track CurrentTrack; |         public static Track CurrentTrack; | ||||||
| 
 | 
 | ||||||
|  | @ -156,7 +130,6 @@ namespace MuzikaGromche | ||||||
|                     track.LoadedStart = DownloadHandlerAudioClip.GetContent(requests[i * 2]); |                     track.LoadedStart = DownloadHandlerAudioClip.GetContent(requests[i * 2]); | ||||||
|                     track.LoadedLoop = DownloadHandlerAudioClip.GetContent(requests[i * 2 + 1]); |                     track.LoadedLoop = DownloadHandlerAudioClip.GetContent(requests[i * 2 + 1]); | ||||||
|                 } |                 } | ||||||
|                 Config = new Config(base.Config); |  | ||||||
|                 new Harmony(PluginInfo.PLUGIN_NAME).PatchAll(typeof(JesterPatch)); |                 new Harmony(PluginInfo.PLUGIN_NAME).PatchAll(typeof(JesterPatch)); | ||||||
|             } |             } | ||||||
|             else |             else | ||||||
|  | @ -185,9 +158,6 @@ namespace MuzikaGromche | ||||||
|         public AudioClip LoadedStart; |         public AudioClip LoadedStart; | ||||||
|         public AudioClip LoadedLoop; |         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 FileNameStart => $"{Name}Start.{Ext}"; | ||||||
|         public string FileNameLoop => $"{Name}Loop.{Ext}"; |         public string FileNameLoop => $"{Name}Loop.{Ext}"; | ||||||
|         private string Ext => AudioType switch |         private string Ext => AudioType switch | ||||||
|  | @ -199,142 +169,6 @@ namespace MuzikaGromche | ||||||
|         }; |         }; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     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))] |     [HarmonyPatch(typeof(JesterAI))] | ||||||
|     internal class JesterPatch |     internal class JesterPatch | ||||||
|     { |     { | ||||||
|  | @ -380,7 +214,20 @@ namespace MuzikaGromche | ||||||
|                 __instance.creatureVoice.Stop(); |                 __instance.creatureVoice.Stop(); | ||||||
| 
 | 
 | ||||||
|                 //  ...and start modded music |                 //  ...and start modded music | ||||||
|                 Plugin.CurrentTrack = Plugin.ChooseTrack(); |                 var seed = RoundManager.Instance.dungeonGenerator.Generator.ChosenSeed; | ||||||
|  |                 var sha = SHA256.Create(); | ||||||
|  |                 var hash = sha.ComputeHash(BitConverter.GetBytes(seed)); | ||||||
|  |                 var trackId = 0; | ||||||
|  |                 foreach (var t in hash) | ||||||
|  |                 { | ||||||
|  |                     // modulus division on byte array | ||||||
|  |                     trackId *= 256 % Plugin.Tracks.Length; | ||||||
|  |                     trackId %= Plugin.Tracks.Length; | ||||||
|  |                     trackId += t % Plugin.Tracks.Length; | ||||||
|  |                     trackId %= Plugin.Tracks.Length; | ||||||
|  |                 } | ||||||
|  |                 Debug.Log($"Seed is {seed}, chosen track is {trackId} out of {Plugin.Tracks.Length} tracks"); | ||||||
|  |                 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; | ||||||
|  |  | ||||||
|  | @ -5,7 +5,6 @@ | ||||||
|     "description": "Glaza zakryvaj", |     "description": "Glaza zakryvaj", | ||||||
|     "website_url": "https://git.vilunov.me/nikita/muzika-gromche", |     "website_url": "https://git.vilunov.me/nikita/muzika-gromche", | ||||||
|     "dependencies": [ |     "dependencies": [ | ||||||
|         "BepInEx-BepInExPack-5.4.2100", |         "BepInEx-BepInExPack-5.4.2100" | ||||||
|         "Sigurd-CSync-5.0.1" |  | ||||||
|     ] |     ] | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue