96 lines
4.3 KiB
C#
96 lines
4.3 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",
|
|
"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",
|
|
};
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|