Compare commits

...

2 Commits

Author SHA1 Message Date
ivan tkachenko 766e393a58 Add LethalConfig with suitable custom options
The custom callback attempts to prevent modifications mid-round. Use
IsHost to check for permissions, as IsClient is always true for
everyone even in local single-player setting.

There is a bug in LethalConfig which makes it possible to modify entries
bypassing the callback once per round, but it is pretty hard to abuse:
https://github.com/AinaVT/LethalConfig/issues/60
2025-07-12 16:56:58 +03:00
ivan tkachenko 26fb620173 Add config synchronization via CSync
It only synchronizes from host to clients.
2025-07-12 02:11:32 +03:00
4 changed files with 70 additions and 9 deletions

View File

@ -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>

View File

@ -5,6 +5,11 @@ using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using BepInEx; using BepInEx;
using BepInEx.Configuration; 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;
@ -12,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
{ {
@ -151,13 +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]);
} }
// Initialize config Config = new Config(base.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));
}
new Harmony(PluginInfo.PLUGIN_NAME).PatchAll(typeof(JesterPatch)); new Harmony(PluginInfo.PLUGIN_NAME).PatchAll(typeof(JesterPatch));
} }
else else
@ -187,7 +190,7 @@ namespace MuzikaGromche
public AudioClip LoadedLoop; public AudioClip LoadedLoop;
// How often this track should be chosen, relative to the sum of weights of all tracks. // 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 FileNameStart => $"{Name}Start.{Ext}";
public string FileNameLoop => $"{Name}Loop.{Ext}"; public string FileNameLoop => $"{Name}Loop.{Ext}";
@ -287,6 +290,58 @@ namespace MuzikaGromche
readonly public int TotalWeights { get; } 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 = CanModifyWeightsNow,
});
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 CanModifyWeightsNow()
{
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
{ {

View File

@ -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>

View File

@ -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"
] ]
} }