Table of Contents

Class ValheimWorldScope

Namespace
Valheim.Testing.Doubles
Assembly
Valheim.Testing.Doubles.dll

The game's process-wide state for one test. Created, it records every static the doubles keep (the singletons such as WorldGenerator, ZDOMan, ZoneSystem, ZNetScene, ZNet, ZRoutedRpc, ObjectDB and Jotunn's NetworkManager; the loaded heightmaps, terrain modifiers, Unity objects and pending destroys; the log capture, console commands, cheat gates, local player and player list; the registries' hooks, localization, input, preferences, config disk, heightmap builder, clock and the random generator Random.InitState installed, not its position in its sequence) plus the server and dedicated flags of the ZNet it found; disposed, it ends the test's frame (UnityEngine.Object.EndOfFrame) and restores the statics through StaticOverride in reverse order, each even when another fails. A second Dispose does nothing. It restores references, except the game's three static readonly lists of heightmaps, terrain modifiers and players, whose contents are restored in place. Other objects are not deep-copied, so a test that mutates one it did not install (adds a peer to the existing ZNet, a ZDO to the existing ZDOMan) leaves that change behind. Use the builder methods to install fresh, isolated objects instead (WithZdos(), WithScene(), WithNetwork(bool), WithCommands(), ...). A mod's own statics are the mod's to scope, with StaticOverride. The doubles are process-wide like the game's, so tests using them must not run in parallel (disable xUnit parallelization for the assembly).

public sealed class ValheimWorldScope
Inheritance
object
ValheimWorldScope

Properties

PreviewPlayer

The preview character AtMainMenu() made, or null.

public Player? PreviewPlayer { get; }

Property Value

Player

Methods

AddRemotePlayer(long, Vector3, string)

Another peer's player at position: its ZDO is owned by peerId and carries its name. On a server the peer is also connected (ZNet.GetPeers, with the player as its character); a client connects only to the server, so there it is only a player object.

public Player AddRemotePlayer(long peerId, Vector3 position, string name = "")

Parameters

peerId long
position Vector3
name string

Returns

Player

AsClient(Vector3)

A client in a world: not the server, with a local player at position.

public ValheimWorldScope AsClient(Vector3 position = default)

Parameters

position Vector3

Returns

ValheimWorldScope

AsDedicatedServer()

A dedicated server: server, dedicated, no local player, a world's singletons, and fresh preferences and localization. In 1.0.16 PlatformPrefs falls back to PlayerPrefs on a dedicated server, so a plugin's Awake may touch Localization and PlatformPrefs here; to test against the pre-1.0 client failure instead, set PlatformPrefs.Unavailable (the scope restores it).

public ValheimWorldScope AsDedicatedServer()

Returns

ValheimWorldScope

AsHost(Vector3)

A listen-server host: server, not dedicated, with a local player at position (owned by this session). A broadcast runs both its server and client handlers.

public ValheimWorldScope AsHost(Vector3 position = default)

Parameters

position Vector3

Returns

ValheimWorldScope

AsServer(bool)

Sets the server flag on the current ZNet (restored on dispose).

public ValheimWorldScope AsServer(bool server = true)

Parameters

server bool

Returns

ValheimWorldScope

AtMainMenu()

The main menu: no world, network, scene or local player, and a preview Player (PreviewPlayer) whose view has no ZDO; like the game's preview it is in Player.GetAllPlayers(), so a player patch runs for it.

public ValheimWorldScope AtMainMenu()

Returns

ValheimWorldScope

CaptureLog()

Captures every log line the mod writes for the rest of the scope.

public List<string> CaptureLog()

Returns

List<string>

Dispose()

public void Dispose()

RegisterHeightmap(Vector2s, int, bool)

Loads a zone heightmap (with a compiler unless withCompiler is false) and returns it. Zones add up, so a test can load neighbours one after another; loading a zone again replaces its heightmap, as an unload and reload would.

public Heightmap RegisterHeightmap(Vector2s zone, int width = 64, bool withCompiler = true)

Parameters

zone Vector2s
width int
withCompiler bool

Returns

Heightmap

UnloadHeightmap(Vector2s)

Unloads the zone's heightmap, as the game does when the zone leaves the active area.

public void UnloadHeightmap(Vector2s zone)

Parameters

zone Vector2s

WithCheats()

Opens both cheat gates (restored on dispose), as devcommands and confirmcheats do on a server: Terminal.m_cheat and Achievements.CheatedAtAll. Cheat commands still need the server side.

public ValheimWorldScope WithCheats()

Returns

ValheimWorldScope

WithClock(float)

Sets game and real time in seconds without running a frame or any behaviour's Update. The last frame length becomes zero; the frame count is unchanged. The scope restores all four clock values on dispose.

public ValheimWorldScope WithClock(float seconds)

Parameters

seconds float

Returns

ValheimWorldScope

WithCommands()

No console commands yet: the mod's registration in this test fills a fresh table.

public ValheimWorldScope WithCommands()

Returns

ValheimWorldScope

WithConfigFiles()

A config disk with no files yet: plugin configs start from their defaults.

public ValheimWorldScope WithConfigFiles()

Returns

ValheimWorldScope

WithHeightmapBuilder(HeightmapBuilder?)

Installs or removes a prepared heightmap builder for one test.

public ValheimWorldScope WithHeightmapBuilder(HeightmapBuilder? builder)

Parameters

builder HeightmapBuilder

Returns

ValheimWorldScope

WithLocalPlayer(Vector3)

A local player standing at position, as on a client or a host; without it there is none, as on a dedicated server.

public Player WithLocalPlayer(Vector3 position)

Parameters

position Vector3

Returns

Player

WithNetwork(bool)

A new ZNet with no peers, as the server or a client, a new ZRoutedRpc and a new Jotunn NetworkManager.

public ValheimWorldScope WithNetwork(bool server = true)

Parameters

server bool

Returns

ValheimWorldScope

WithScene()

A scene with no prefabs yet (ZNetScene.AddPrefab(string, float)), and no GameObjects, Unity components or pending destroys yet for FindObjectsByType, RunFrame and EndOfFrame.

public ValheimWorldScope WithScene()

Returns

ValheimWorldScope

WithTerrain(ITerrain)

public ValheimWorldScope WithTerrain(ITerrain terrain)

Parameters

terrain ITerrain

Returns

ValheimWorldScope

WithTerrainModifiers()

No live terrain modifiers yet: the modifiers this test wakes join the cleared game-owned list.

public ValheimWorldScope WithTerrainModifiers()

Returns

ValheimWorldScope

WithUnityOrder(UnityOrder)

The order the Unity doubles call behaviours and objects where Unity promises none (restored on dispose): see UnityOrder. Run a test that depends on two objects once in each order to find an order assumption.

public ValheimWorldScope WithUnityOrder(UnityOrder order)

Parameters

order UnityOrder

Returns

ValheimWorldScope

WithWorld(WorldGenerator)

public ValheimWorldScope WithWorld(WorldGenerator world)

Parameters

world WorldGenerator

Returns

ValheimWorldScope

WithZdos()

A new, empty ZDOMan (this session's id is 1).

public ValheimWorldScope WithZdos()

Returns

ValheimWorldScope

WithZoneSystem()

public ValheimWorldScope WithZoneSystem()

Returns

ValheimWorldScope

WithoutZdos()

Removes the ZDO manager for a test of code that runs before world loading.

public ValheimWorldScope WithoutZdos()

Returns

ValheimWorldScope