Add configurable weights per track, with synchronization #3

Merged
nikita merged 5 commits from ratijas/muzika-gromche:work/r/dev into master 2025-07-12 22:01:42 +00:00
3 changed files with 42 additions and 9 deletions
Showing only changes of commit 34e72da748 - Show all commits

View File

@ -15,6 +15,11 @@
<PackageReference Include="BepInEx.PluginInfoProps" Version="1.*"/>
<PackageReference Include="UnityEngine.Modules" Version="2022.3.9" IncludeAssets="compile"/>
<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. See https://github.com/lc-sigurd/CSync/issues/11
-->
<PackageReference Include="Sigurd.BepInEx.CSync" Version="5.0.1" Publicize="true" />
</ItemGroup>
<ItemGroup>

View File

@ -5,6 +5,8 @@ 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;
@ -12,8 +14,11 @@ 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
{
@ -151,13 +156,7 @@ namespace MuzikaGromche
track.LoadedStart = DownloadHandlerAudioClip.GetContent(requests[i * 2]);
track.LoadedLoop = DownloadHandlerAudioClip.GetContent(requests[i * 2 + 1]);
}
// Initialize config
var chanceRange = new AcceptableValueRange<int>(0, 100);
foreach (var track in Tracks)
{
string description = $"Random (relative) chance of selecting track {track.Name}. Set to zero to effectively disable the track.";
track.Weight = Config.Bind("Tracks", track.Name, 50, new ConfigDescription(description, chanceRange));
}
Config = new Config(base.Config);
new Harmony(PluginInfo.PLUGIN_NAME).PatchAll(typeof(JesterPatch));
}
else
@ -187,7 +186,7 @@ namespace MuzikaGromche
public AudioClip LoadedLoop;
// How often this track should be chosen, relative to the sum of weights of all tracks.
public ConfigEntry<int> Weight;
public SyncedEntry<int> Weight;
public string FileNameStart => $"{Name}Start.{Ext}";
public string FileNameLoop => $"{Name}Loop.{Ext}";
@ -287,6 +286,34 @@ namespace MuzikaGromche
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));
}
// HACK because CSync doesn't provide an API to register a list of config entries
// See https://github.com/lc-sigurd/CSync/issues/11
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);
}
}
[HarmonyPatch(typeof(JesterAI))]
internal class JesterPatch
{

View File

@ -5,6 +5,7 @@
"description": "Glaza zakryvaj",
"website_url": "https://git.vilunov.me/nikita/muzika-gromche",
"dependencies": [
"BepInEx-BepInExPack-5.4.2100"
"BepInEx-BepInExPack-5.4.2100",
"Sigurd-CSync-5.0.1"
]
}