From e83c3c5d74757a951343d36d0a4bbce6e224f44e Mon Sep 17 00:00:00 2001 From: kanym Date: Sat, 17 Oct 2026 19:22:24 +0200 Subject: [PATCH] fix(fenrir): enter the Ulv's sleep by its real state path scripts/dump_controller.py reads an AnimatorController's states and transitions offline from Valheim's bundles with UnityPy. On ulv_animator it shows the entry state "check sleeping" picks Sleep only at start-up and Movement has no way back into it, so the bool alone could never work. The cross-fade now targets "Base Layer.Sleep". --- JotunnModStub/Fenrir/FenrirFormShared.cs | 9 +- scripts/dump_controller.py | 105 +++++++++++++++++++++++ 2 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 scripts/dump_controller.py diff --git a/JotunnModStub/Fenrir/FenrirFormShared.cs b/JotunnModStub/Fenrir/FenrirFormShared.cs index 5ee2233..a6eb382 100644 --- a/JotunnModStub/Fenrir/FenrirFormShared.cs +++ b/JotunnModStub/Fenrir/FenrirFormShared.cs @@ -50,7 +50,10 @@ namespace RacesOfValheim private bool hasSleeping; private int sleepState; private bool wasSitting; - private static readonly string[] SleepStateNames = { "Sleep", "sleep", "Sleeping", "sleeping" }; + // Read off ulv_animator with scripts/dump_controller.py: the entry state "check sleeping" goes + // to Sleep only at start-up, and Movement has no way back into it. "Base Layer.Sleep" is the + // real path; the short name stays as a fallback for another creature built the same way. + private static readonly string[] SleepStateNames = { "Base Layer.Sleep", "Sleep" }; private static readonly System.Collections.Generic.HashSet sleepLogged = new System.Collections.Generic.HashSet(); /// Copies the movement parameters, and says whether the form counts as grounded. @@ -91,8 +94,8 @@ namespace RacesOfValheim if (p.type == AnimatorControllerParameterType.Bool && p.name == "sleeping") { hasSleeping = true; break; } } - // The state's name is not readable at runtime, only testable: the clip is 'Sleep', and - // the state is most likely named after it. + // The state's name is not readable at runtime, only testable - but it is readable offline, + // see SleepStateNames. sleepState = 0; string found = "none"; if (hasSleeping) diff --git a/scripts/dump_controller.py b/scripts/dump_controller.py new file mode 100644 index 0000000..07b9274 --- /dev/null +++ b/scripts/dump_controller.py @@ -0,0 +1,105 @@ +r"""Dump a Unity AnimatorController's parameters, states and transitions from Valheim's own files. + +Reads the game's asset files offline, so animator state and parameter names are read, not guessed. + +Setup, once (UnityPy into a folder of your choice, not into the repo): + python -m pip install --target C:\path o\pylib UnityPy + +Usage (Windows paths: a Git Bash "/c/..." path does not resolve for a Windows Python): + python -I scripts/dump_controller.py C:\path o\pylib " alheim_Data" ulv_animator wolf_animator + +Pass valheim_Data to search everything (slow, a few minutes), or a single bundle file to read +only that one. Creature animators live in valheim_Data\StreamingAssets\SoftRef\Bundles; the +Fenrir forms' four (wolf, ulv, fenring, fenring_cultist) are all in bundle c4210710 as of 1.0.20. +A controller's name is the one dumpanim prints ("controller 'ulv_animator'"). +""" +import os, sys, json + +sys.path.insert(0, sys.argv[1]) +import UnityPy # noqa: E402 + +data_dir = sys.argv[2] +wanted = set(sys.argv[3:]) + +candidates = [data_dir] if os.path.isfile(data_dir) else [] +for root, _, files in ([] if os.path.isfile(data_dir) else os.walk(data_dir)): + for f in files: + p = os.path.join(root, f) + if f.endswith(('.resS', '.resource', '.manifest', '.json', '.dll', '.csv', '.txt', '.info', '.config')): + continue + if os.path.getsize(p) < 1024: + continue + candidates.append(p) + +found = {} +for path in candidates: + try: + env = UnityPy.load(path) + except Exception: + continue + for obj in env.objects: + if obj.type.name != 'AnimatorController': + continue + try: + name = obj.peek_name() + except Exception: + name = None + if name is None: + try: + name = obj.read().m_Name + except Exception: + continue + if name not in wanted or name in found: + continue + tree = obj.read_typetree() + found[name] = (path, tree) + if len(found) == len(wanted): + break + +for name, (path, tree) in found.items(): + print(f'=== {name} ({os.path.basename(path)})') + tos = {int(k): v for k, v in tree.get('m_TOS', [])} + print('m_TOS (hash -> name):') + for h, v in sorted(tos.items(), key=lambda kv: kv[1]): + print(f' {h:>12} {v}') + + ctrl = tree.get('m_Controller', {}) + params = ctrl.get('m_Values', {}).get('data', {}).get('m_ValueArray', []) + print('parameters:') + for p in params: + print(f" {tos.get(p.get('m_ID'), p.get('m_ID'))} type={p.get('m_Type')}") + + for sm_i, sm in enumerate(ctrl.get('m_StateMachineArray', [])): + sm = sm.get('data', sm) + states = sm.get('m_StateConstantArray', []) + print(f'state machine {sm_i}: {len(states)} states, default index {sm.get("m_DefaultState")}') + any_tr = sm.get('m_AnyStateTransitionConstantArray', []) + + def describe(tr): + tr = tr.get('data', tr) + conds = [] + for c in tr.get('m_ConditionConstantArray', []): + c = c.get('data', c) + conds.append(f"{tos.get(c.get('m_EventID'), c.get('m_EventID'))} mode={c.get('m_ConditionMode')} th={c.get('m_EventThreshold')}") + dest = tr.get('m_DestinationState') + return dest, conds + + names = [] + for st in states: + st = st.get('data', st) + names.append(tos.get(st.get('m_NameID'), st.get('m_NameID'))) + for i, st in enumerate(states): + st = st.get('data', st) + print(f' [{i}] state "{names[i]}" fullPath="{tos.get(st.get("m_FullPathID"), st.get("m_FullPathID"))}" tag="{tos.get(st.get("m_TagID"), st.get("m_TagID"))}"') + for tr in st.get('m_TransitionConstantArray', []): + dest, conds = describe(tr) + dname = names[dest] if isinstance(dest, int) and 0 <= dest < len(names) else dest + print(f' -> {dname} when {conds}') + for tr in any_tr: + dest, conds = describe(tr) + dname = names[dest] if isinstance(dest, int) and 0 <= dest < len(names) else dest + print(f' AnyState -> {dname} when {conds}') + +missing = wanted - set(found) +if missing: + print('not found:', ', '.join(sorted(missing)))