RacesOfValheim/JotunnModStub/ConfigSync.cs
kanym b2346c1437 feat(trol): Legacy True Form switch, chosen per player
The Troll Model option becomes Legacy True Form, off by default. On,
True Form is the old one: the player's own Trol grown bigger, weapons
in hand, body untouched, which player model mods such as VRM need.

It is a local preference, not a synced one, so a server cannot force
the troll costume on a VRM user. The owner publishes the choice on the
ZDO and every other client reads it from there, so onlookers see each
Trol the way that Trol's owner picked.
2026-10-17 15:32:06 +02:00

100 lines
4.6 KiB
C#

using System;
using System.Linq;
using System.Reflection;
using BepInEx.Configuration;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Marks every gameplay config entry as admin-only so Jotunn syncs it from the server to the
/// clients (and locks it for non-admins in Configuration Manager).
///
/// Jotunn only recognises ITS OWN ConfigurationManagerAttributes (global namespace, in
/// Jotunn.dll) as a tag - it does an `is` check, not the field-name duck typing Configuration
/// Manager does - so our internal look-alike tag is not enough. This runs once after every
/// Bind() and adds Jotunn's tag beside whatever tags the entry already has, rather than
/// touching each of the ~350 Bind() calls.
///
/// Left local, because they are per-player preferences and syncing them would only take the
/// choice away from the player: keybinds, and the purely cosmetic entries (eye/claw colours,
/// glow, tint, rotation and offset of models, the VFX toggle, the stamina-bar toggle, debug
/// logging). Anything that changes a number the game computes with stays synced.
/// </summary>
internal static class ConfigSync
{
// Whole sections that are cosmetic or per-player.
private static readonly string[] LocalSections =
{
"Aswid - Claws",
"Aswid - Eyes",
"Skeleton - Visuals",
"Sverm - Dorsal Spines",
"Sverm - Emberform",
"Trol - Appearance",
"War Vulture - Model",
"War Vulture - Interface",
};
// Cosmetic entries scattered through otherwise gameplay sections.
private static readonly string[] LocalKeyParts =
{
"marker light", "tint", "colour", "color", "glow", "swarm scale", "log rounds", "debug",
// Each player picks their own True Form: a player model mod (VRM and the like) breaks
// under the troll costume, so a server cannot be allowed to force the costume on.
"legacy true form",
};
public static void Apply(ConfigFile config)
{
FieldInfo descField = typeof(ConfigEntryBase)
.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.FirstOrDefault(f => f.FieldType == typeof(ConfigDescription));
if (descField == null)
{
Debug.LogWarning("[RacesOfValheim] Config sync skipped: ConfigEntryBase has no ConfigDescription field.");
return;
}
int synced = 0, local = 0;
foreach (ConfigDefinition def in config.Keys.ToList())
{
ConfigEntryBase entry = config[def];
if (!ShouldSync(entry)) { local++; continue; }
ConfigDescription old = entry.Description;
object[] tags = (old.Tags ?? new object[0]).Concat(new object[]
{
new global::ConfigurationManagerAttributes { IsAdminOnly = true }
}).ToArray();
descField.SetValue(entry, new ConfigDescription(old.Description, old.AcceptableValues, tags));
synced++;
}
Debug.Log($"[RacesOfValheim] Config sync: {synced} entries admin-synced, {local} left local.");
}
private static bool ShouldSync(ConfigEntryBase entry)
{
Type type = entry.SettingType;
if (type == typeof(KeyboardShortcut) || type == typeof(KeyCode)) return false;
string section = entry.Definition.Section ?? "";
string key = (entry.Definition.Key ?? "").ToLowerInvariant();
if (section.EndsWith("Keybinds", StringComparison.OrdinalIgnoreCase)) return false;
if (LocalSections.Contains(section, StringComparer.OrdinalIgnoreCase)) return false;
if (LocalKeyParts.Any(key.Contains)) return false;
// Model pose nudges (rotation/offset/yaw) are cosmetic, but "Attack Reach Offset" is a
// real hit-detection distance and must stay in step with the server.
if ((key.Contains("rotation") || key.Contains("offset") || key.Contains("yaw"))
&& !key.Contains("reach")) return false;
// Decorative note lines (CustomDrawer/ReadOnly) carry no value worth sending.
ConfigurationManagerAttributes ours = (entry.Description?.Tags ?? new object[0]).OfType<ConfigurationManagerAttributes>().FirstOrDefault();
if (ours != null && (ours.CustomDrawer != null || ours.ReadOnly == true)) return false;
return true;
}
}
}