From f87da1dfed980d27eb351e8a4fa2a65e43f39f36 Mon Sep 17 00:00:00 2001 From: ivan tkachenko Date: Sat, 12 Jul 2025 00:05:28 +0300 Subject: [PATCH] WIP: Add config synchronization via CSync --- MuzikaGromche/MuzikaGromche.csproj | 2 + MuzikaGromche/Plugin.cs | 66 ++++++++++++++++++++++++++---- manifest.json | 3 +- 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/MuzikaGromche/MuzikaGromche.csproj b/MuzikaGromche/MuzikaGromche.csproj index e0cfde1..0c60c9e 100644 --- a/MuzikaGromche/MuzikaGromche.csproj +++ b/MuzikaGromche/MuzikaGromche.csproj @@ -15,6 +15,8 @@ + + diff --git a/MuzikaGromche/Plugin.cs b/MuzikaGromche/Plugin.cs index 67baa47..8758aeb 100644 --- a/MuzikaGromche/Plugin.cs +++ b/MuzikaGromche/Plugin.cs @@ -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 { @@ -66,7 +71,7 @@ namespace MuzikaGromche var trackId = rwi.GetRandomWeightedIndex(seed); #if DEBUG // Override for testing - trackId = IndexOfTrack("DeployDestroy"); + // trackId = IndexOfTrack("DeployDestroy"); #endif var track = Tracks[trackId]; Debug.Log($"Seed is {seed}, chosen track is \"{track.Name}\", #{trackId} of {rwi}"); @@ -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(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 Weight; + public SyncedEntry Weight; public string FileNameStart => $"{Name}Start.{Ext}"; public string FileNameLoop => $"{Name}Loop.{Ext}"; @@ -287,6 +286,55 @@ namespace MuzikaGromche readonly public int TotalWeights { get; } } + public class Config : SyncedConfig2 + { + // TODO: Comment out for now + // Personal preferrence, explicitly not synced + public ConfigEntry 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(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 { diff --git a/manifest.json b/manifest.json index 8cc5352..c6b419f 100644 --- a/manifest.json +++ b/manifest.json @@ -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" ] }