forked from nikita/muzika-gromche
Compare commits
3 Commits
f87da1dfed
...
54e88b08a8
Author | SHA1 | Date |
---|---|---|
|
54e88b08a8 | |
|
26fb620173 | |
|
aead762721 |
|
@ -15,6 +15,9 @@
|
||||||
<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" />
|
||||||
|
<PackageReference Include="AinaVT-LethalConfig" Version="1.4.6" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -4,6 +4,12 @@ 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 LethalConfig;
|
||||||
|
using LethalConfig.ConfigItems;
|
||||||
|
using LethalConfig.ConfigItems.Options;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Networking;
|
using UnityEngine.Networking;
|
||||||
|
@ -11,8 +17,12 @@ 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")]
|
||||||
|
[BepInDependency("ainavt.lc.lethalconfig", "1.4.6")]
|
||||||
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
|
||||||
{
|
{
|
||||||
|
@ -60,23 +70,15 @@ namespace MuzikaGromche
|
||||||
public static Track ChooseTrack()
|
public static Track ChooseTrack()
|
||||||
{
|
{
|
||||||
var seed = RoundManager.Instance.dungeonGenerator.Generator.ChosenSeed;
|
var seed = RoundManager.Instance.dungeonGenerator.Generator.ChosenSeed;
|
||||||
var sha = SHA256.Create();
|
int[] weights = [.. Tracks.Select(track => track.Weight.Value)];
|
||||||
var hash = sha.ComputeHash(BitConverter.GetBytes(seed));
|
var rwi = new RandomWeightedIndex(weights);
|
||||||
var trackId = 0;
|
var trackId = rwi.GetRandomWeightedIndex(seed);
|
||||||
foreach (var t in hash)
|
|
||||||
{
|
|
||||||
// modulus division on byte array
|
|
||||||
trackId *= 256 % Tracks.Length;
|
|
||||||
trackId %= Tracks.Length;
|
|
||||||
trackId += t % Tracks.Length;
|
|
||||||
trackId %= Tracks.Length;
|
|
||||||
}
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
// Override for testing
|
// Override for testing
|
||||||
trackId = IndexOfTrack("DeployDestroy");
|
// trackId = IndexOfTrack("DeployDestroy");
|
||||||
#endif
|
#endif
|
||||||
var track = Tracks[trackId];
|
var track = Tracks[trackId];
|
||||||
Debug.Log($"Seed is {seed}, chosen track is \"{track.Name}\", {trackId} out of {Tracks.Length} tracks");
|
Debug.Log($"Seed is {seed}, chosen track is \"{track.Name}\", #{trackId} of {rwi}");
|
||||||
return Tracks[trackId];
|
return Tracks[trackId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,6 +160,7 @@ 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
|
||||||
|
@ -186,6 +189,9 @@ 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
|
||||||
|
@ -197,6 +203,145 @@ 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;
|
||||||
|
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;
|
||||||
|
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 GetWeightedIndex(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 GetWeightedIndex(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>
|
||||||
|
{
|
||||||
|
public Config(ConfigFile configFile) : base(PluginInfo.PLUGIN_GUID)
|
||||||
|
{
|
||||||
|
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));
|
||||||
|
|
||||||
|
var slider = new IntSliderConfigItem(track.Weight.Entry, new IntSliderOptions
|
||||||
|
{
|
||||||
|
RequiresRestart = false,
|
||||||
|
CanModifyCallback = CanModifyCallback,
|
||||||
|
});
|
||||||
|
LethalConfigManager.AddConfigItem(slider);
|
||||||
|
}
|
||||||
|
|
||||||
|
// HACK because CSync doesn't provide an API to register a list of config entries
|
||||||
|
foreach (var track in Plugin.Tracks)
|
||||||
|
{
|
||||||
|
// This is basically what ConfigFile.PopulateEntryContainer does
|
||||||
|
SyncedEntryBase entryBase = track.Weight;
|
||||||
|
EntryContainer.Add(entryBase.BoxedEntry.ToSyncedEntryIdentifier(), entryBase);
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigManager.Register(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CanModifyResult CanModifyCallback()
|
||||||
|
{
|
||||||
|
var startOfRound = StartOfRound.Instance;
|
||||||
|
if (!startOfRound)
|
||||||
|
{
|
||||||
|
return CanModifyResult.True(); // Main menu
|
||||||
|
}
|
||||||
|
if (!startOfRound.IsHost)
|
||||||
|
{
|
||||||
|
return CanModifyResult.False("Only for host");
|
||||||
|
}
|
||||||
|
if (!startOfRound.inShipPhase)
|
||||||
|
{
|
||||||
|
return CanModifyResult.False("Only while orbiting");
|
||||||
|
}
|
||||||
|
return CanModifyResult.True();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[HarmonyPatch(typeof(JesterAI))]
|
[HarmonyPatch(typeof(JesterAI))]
|
||||||
internal class JesterPatch
|
internal class JesterPatch
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,5 +2,6 @@
|
||||||
<configuration>
|
<configuration>
|
||||||
<packageSources>
|
<packageSources>
|
||||||
<add key="BepInEx" value="https://nuget.bepinex.dev/v3/index.json" />
|
<add key="BepInEx" value="https://nuget.bepinex.dev/v3/index.json" />
|
||||||
|
<add key="AAron Thunderstore" value="https://nuget.windows10ce.com/nuget/v3/index.json" />
|
||||||
</packageSources>
|
</packageSources>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
"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",
|
||||||
|
"ainavt.lc.lethalconfig-1.4.6"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue