fix(fenrir): register the claw set from a live object, not a half-built singleton

The Cursed Claws were never issued because FenrirClaws was never in the
roster. RaceClawSet's static constructor registers the two singletons by
reading AswidClaws.Instance and FenrirClaws.Instance - but constructing
a derived type runs the base type's static constructor first, so the
race that triggers it is still mid-construction when its own Instance is
read, and reads null. Register(null) returns silently and that race is
left out for the rest of the session: For() never matches it, Maintain
never runs, no claws, no claw damage.

It was the Fenrir and not the Aswid only because FenrirRace runs first in
CreateAllModAssets - whichever race gets there first is the one that is
half-built.

EnsureRegistered now calls Register(this) before anything else. `this` is
a live object by definition, so there is nothing left to be null, and
every race puts itself in the roster at startup. The static constructor
stays as a harmless backstop with the trap written down next to it.

Damage scaling checked while here and needs no change: all three races
run the same SvermClaws.GetReferenceDamage/GetDamagePercent pair over the
one shared per-state table, so the same tier is worth the same damage.
This commit is contained in:
kanym 2026-08-12 17:05:58 +02:00
commit 6db21b9d96

View file

@ -74,6 +74,24 @@ namespace RacesOfValheim
/// The roster. Kept here rather than in RacesOfValheim.Awake so the whole feature is this
/// file plus one short file per race, and adding a third clawed race is one line - not one
/// line here and another one three hundred lines away in the plugin's startup.
///
/// A BACKSTOP ONLY - EnsureRegistered is what actually fills this list, and it has to be.
/// This constructor cannot be trusted to register a race that is still being constructed,
/// and one of them always is:
///
/// FenrirRace.CreateModAssets touches FenrirClaws.Instance
/// -> the CLR begins FenrirClaws' static init: Instance = new FenrirClaws()
/// -> constructing a derived type runs the BASE type's static ctor first, i.e. this one
/// -> this one reads FenrirClaws.Instance... which is still null, because the static
/// init that will assign it is the thing waiting on us
/// -> Register(null) returns silently, and the Fenrir is left out of the roster for the
/// rest of the session: For() never matches it, so Maintain never runs, so the
/// Cursed Claws are never issued and never do their damage.
///
/// It bit the Fenrir and not the Aswid purely because FenrirRace.CreateModAssets runs
/// first in RacesOfValheim.CreateAllModAssets - whichever race gets there first is the one
/// that is half-built when this runs. Register(this) in EnsureRegistered closes it for
/// good: `this` is a live object by definition, so there is nothing to be null.
/// </summary>
static RaceClawSet()
{
@ -146,6 +164,11 @@ namespace RacesOfValheim
/// </summary>
public void EnsureRegistered()
{
// BEFORE the early return, and before anything else: this is the one place the roster
// is filled reliably. See the static constructor for why it cannot do this itself.
// Idempotent, so calling it on every tick as well as at startup costs nothing.
Register(this);
if (registered) return;
if (ObjectDB.instance != null && ObjectDB.instance.GetItemPrefab(ItemName) != null)