RacesOfValheim/JotunnModStub/RaceRadial.cs
kanym e75c7072c4 fix(prefabs): replace every lookup name that does not exist in vanilla
An audit of all 59 prefab names the mod resolves, against the game's own
asset manifests, found nine that are not in Valheim at all. A name vanilla
does not use has no vanilla candidate to beat in Jotunn's name cache, so it
is a free slot for whoever ships one - which is how another mod's weapon
trail became the Skeleton's fire aura in 00d4fd6.

Eight of the nine failed in silence, which is why none of them was ever
reported:

  vfx_corpse_destruction  ->  _medium, _large for the Muspell. The puff that
      covers a transformation. Fenrir and Skadi at least had their sparks;
      the Muspell swapped bodies in plain view with nothing at all.
  vfx_surtling_death      ->  vfx_wraith_death for the Aswid's Mist Form,
      which reads as mist by construction and is the same creature the form
      already borrows its Visual from; vfx_corpse_destruction_medium for the
      thrall, where nothing is misty. Vanilla has no surtling death effect
      under any spelling.
  TrophyTroll             ->  TrophyForestTroll.
  TrophyBat               ->  TrophyDeathsquito. Vanilla ships no bat trophy,
      so the Aswid's two status effects and its radial entry had no icon.
  vfx_smoke               ->  gone. It was the Trenta's fallback for Purple
      Rain's particle material behind Greydwarf_Shaman, and the day the
      Shaman failed to resolve it would have worn whatever a mod supplies.
      Purple Rain is purple by design, so nobody would ever have reported it.

The ninth did not fail in silence. vfx_OozeHit was passed to Instantiate
unguarded, so every successful Draugh parry threw ArgumentException and took
the North Wind nova below it down as well. It is vfx_blob_hit.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-26 00:20:02 +02:00

209 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using Valheim.UI;
using Object = UnityEngine.Object;
namespace RacesOfValheim
{
/// <summary>
/// The race's powers on a gamepad: one more group in Valheim 1.0's radial menu, named after the
/// player's race, with one entry per form and ability.
///
/// WHY THE RADIAL AND NOT A BUTTON. Read off the 1.0 assemblies with Cecil: every physical pad
/// button is bound in all four layouts, the only chords vanilla leaves alone are LT+X and LT+R3
/// in Classic only, and LT is already the transformed forms' Mouse1 attack through
/// RaceInput.PadAttackButtons. The radial is opened the way vanilla opens it (hold R3, or the
/// keyboard's own radial key), follows whatever layout is picked, and has room for every race.
///
/// NO RACE CODE CHANGES. An entry does not call into the race: it "presses" the key the player
/// already has bound (RaceInput.Press), and the race's own IsDownIgnoringHeldKeys check picks it
/// up on its next Update. Unlock rules, cooldowns and the messages that go with them therefore
/// behave exactly as they do from the keyboard, and a rebound key is followed for free.
///
/// Everything vanilla-side was read, not guessed: ValheimRadialConfig.InitRadialConfig builds
/// its top page by calling AddEmoteGroup(radial, elements) among others, a group is a
/// GroupElement instantiated from RadialData.SO and Init'ed with (config, back, radial), and its
/// Interact just calls radial.QueuedOpen(config, back). Leaves are elements whose Interact and
/// CloseOnInteract are public Func&lt;bool&gt; setters; Name's setter is private, hence the
/// reflection.
/// </summary>
public static class RaceRadial
{
private class Entry
{
public string Race;
public string Label;
public Func<ConfigEntry<KeyboardShortcut>> Key;
public string Icon; // an item prefab whose icon is borrowed; may not exist
public Func<Player, bool> Available; // null means always, for that race
}
private static readonly List<Entry> Entries = new List<Entry>
{
new Entry { Race = "Fenrir", Label = "Wolf Form", Key = () => FenrirConfig.WolfFormKey, Icon = "TrophyWolf" },
new Entry { Race = "Fenrir", Label = "Fenrir Form", Key = () => FenrirConfig.FenrirFormKey, Icon = "TrophyFenring" },
new Entry { Race = "Fenrir", Label = "Cultist Form", Key = () => FenrirConfig.CultistFormKey, Icon = "TrophyCultist" },
new Entry { Race = "Fenrir", Label = "Howl", Key = () => FenrirConfig.HowlKey, Icon = "TrophyUlv" },
new Entry { Race = "Aswid", Label = "Bat Form", Key = () => AswidConfig.BatFormKey, Icon = "TrophyDeathsquito" },
new Entry { Race = "Aswid", Label = "Mist Form", Key = () => AswidConfig.MistFormKey, Icon = "TrophyWraith" },
new Entry { Race = "Aswid", Label = "Eclipse", Key = () => AswidConfig.EclipseKey, Icon = "TrophyFader" },
new Entry { Race = "Aswid", Label = "Tame", Key = () => AswidConfig.TameKey, Icon = "TrophyWolf" },
new Entry { Race = "Aswid", Label = "Devour", Key = () => AswidConfig.ToggleDevourKey, Icon = "TrophyDraugr" },
new Entry { Race = "Skadi", Label = "Drake Form", Key = () => SkadiConfig.DrakeFormKey, Icon = "TrophyHatchling" },
new Entry { Race = "Skadi", Label = "Moder Form", Key = () => SkadiConfig.ModerFormKey, Icon = "TrophyDragonQueen" },
new Entry { Race = "Skadi", Label = "Fader Form", Key = () => SkadiConfig.FaderFormKey, Icon = "TrophyFader" },
new Entry { Race = "Trol", Label = "True Form", Key = () => TrolConfig.TrueFormKey, Icon = "TrophyFrostTroll" },
new Entry { Race = "Trol", Label = "Throw Rock", Key = () => TrolConfig.ThrowRockKey, Icon = "Stone" },
new Entry { Race = "Trol", Label = "Grab Log", Key = () => TrolConfig.GrabLogKey, Icon = "Wood" },
new Entry { Race = "Trenta", Label = "Purple Rain", Key = () => TrentaConfigManagement.PurpleRainKey, Icon = "Blueberries" },
new Entry { Race = "Trenta", Label = "Homecoming Portal", Key = () => TrentaConfigManagement.PortalKey, Icon = "SurtlingCore" },
new Entry { Race = "Trenta", Label = "Muspell Form", Key = () => TrentaConfigManagement.MuspellKey, Icon = "TrophySurtling" },
new Entry { Race = "Dvergar", Label = "Runic Ping", Key = () => DvergarConfigManagement.RunicPingKey, Icon = "TrophyDvergr" },
new Entry { Race = "Dvergar", Label = "Runic Overcharge", Key = () => DvergarConfigManagement.RunicOverchargeKey, Icon = "Eitr" },
new Entry { Race = "Sverm", Label = "Ravager Strain", Key = () => SvermConfigManagement.RavagerKey, Icon = "TrophySeekerBrute" },
// Any race: shown to whoever RaceClawSet has claws for.
new Entry { Race = null, Label = "Grow Claws", Key = () => RaceClawsConfig.ActivateKey, Icon = "TrophyWolf",
Available = player => RaceClawSet.For(player) != null },
};
private static readonly MethodInfo SetName = AccessTools.PropertySetter(typeof(RadialMenuElement), "Name");
/// <summary>Called from the AddEmoteGroup postfix: appends the race's group to vanilla's top page.</summary>
public static void AddGroup(IRadialConfig back, RadialBase radial, List<RadialMenuElement> elements)
{
Player player = Player.m_localPlayer;
if (player == null || radial == null || elements == null) return;
if (RadialData.SO == null || RadialData.SO.GroupElement == null) return;
string race = RaceOf(player);
List<Entry> mine = For(player, race);
if (mine.Count == 0) return;
GroupElement group = Object.Instantiate(RadialData.SO.GroupElement);
group.Init(new RaceRadialConfig(race, mine), back, radial);
elements.Add(group);
}
private static List<Entry> For(Player player, string race)
{
List<Entry> mine = new List<Entry>();
foreach (Entry entry in Entries)
{
if (entry.Race != null && entry.Race != race) continue;
if (entry.Available != null && !entry.Available(player)) continue;
if (entry.Key() == null) continue; // that race's config never bound: nothing to press
mine.Add(entry);
}
return mine;
}
/// <summary>The same two sources RaceClaws.GetRace reads, in the same order, "Human" in the
/// ZDO counting as no answer.</summary>
private static string RaceOf(Player player)
{
ZNetView nview = player.GetComponent<ZNetView>();
if (nview != null && nview.IsValid())
{
string race = nview.GetZDO().GetString("RacesOfValheim_Race", "");
if (!string.IsNullOrEmpty(race) && race != "Human") return race;
}
if (player.m_customData != null && player.m_customData.TryGetValue("Race", out string custom))
{
return custom;
}
return "Human";
}
/// <summary>An item's inventory icon, or null - a missing prefab only costs the picture.</summary>
private static Sprite IconOf(string prefabName)
{
if (ObjectDB.instance == null || string.IsNullOrEmpty(prefabName)) return null;
GameObject prefab = ObjectDB.instance.GetItemPrefab(prefabName);
ItemDrop drop = prefab != null ? prefab.GetComponent<ItemDrop>() : null;
if (drop == null || drop.m_itemData?.m_shared?.m_icons == null || drop.m_itemData.m_shared.m_icons.Length == 0) return null;
return drop.m_itemData.GetIcon();
}
/// <summary>The group's own page: one leaf per entry, built fresh every time it is opened so a
/// rebound key or a newly grown claw set is always current.</summary>
private class RaceRadialConfig : IRadialConfig
{
private readonly string m_race;
private readonly List<Entry> m_entries;
public RaceRadialConfig(string race, List<Entry> entries)
{
m_race = race;
m_entries = entries;
}
public string LocalizedName => m_race;
public Sprite Sprite => m_entries.Count > 0 ? IconOf(m_entries[0].Icon) : null;
public void InitRadialConfig(RadialBase radial)
{
List<RadialMenuElement> leaves = new List<RadialMenuElement>();
foreach (Entry entry in m_entries)
{
RadialMenuElement leaf = Object.Instantiate(RadialData.SO.EmoteElement);
Entry captured = entry;
string key = entry.Key().Value.ToString();
SetName?.Invoke(leaf, new object[] { string.IsNullOrEmpty(key) ? entry.Label : $"{entry.Label} ({key})" });
leaf.Interact = () =>
{
RaceInput.Press(captured.Key().Value);
return true;
};
leaf.CloseOnInteract = () => true;
Sprite icon = IconOf(entry.Icon);
if (leaf.Icon != null)
{
leaf.Icon.gameObject.SetActive(icon != null);
leaf.Icon.sprite = icon;
}
leaves.Add(leaf);
}
radial.ConstructRadial(leaves);
}
}
}
[HarmonyPatch(typeof(ValheimRadialConfig), "AddEmoteGroup")]
public static class RaceRadial_AddEmoteGroup_Patch
{
// Wrapped: a mistake here must cost the race group, never the player's whole radial menu.
public static void Postfix(ValheimRadialConfig __instance, RadialBase radial, List<RadialMenuElement> elements)
{
try
{
RaceRadial.AddGroup(__instance, radial, elements);
}
catch (Exception e)
{
Jotunn.Logger.LogWarning($"[RaceRadial] Could not add the race group to the radial menu: {e}");
}
}
}
}