Class ValheimWorldScope
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
-
objectValheimWorldScope
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
peerIdlongpositionVector3namestring
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
positionVector3
Returns
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
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
positionVector3
Returns
AsServer(bool)
Sets the server flag on the current ZNet (restored on dispose).
public ValheimWorldScope AsServer(bool server = true)
Parameters
serverbool
Returns
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
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
zoneVector2swidthintwithCompilerbool
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
zoneVector2s
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
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
secondsfloat
Returns
WithCommands()
No console commands yet: the mod's registration in this test fills a fresh table.
public ValheimWorldScope WithCommands()
Returns
WithConfigFiles()
A config disk with no files yet: plugin configs start from their defaults.
public ValheimWorldScope WithConfigFiles()
Returns
WithHeightmapBuilder(HeightmapBuilder?)
Installs or removes a prepared heightmap builder for one test.
public ValheimWorldScope WithHeightmapBuilder(HeightmapBuilder? builder)
Parameters
builderHeightmapBuilder
Returns
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
positionVector3
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
serverbool
Returns
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
WithTerrain(ITerrain)
public ValheimWorldScope WithTerrain(ITerrain terrain)
Parameters
terrainITerrain
Returns
WithTerrainModifiers()
No live terrain modifiers yet: the modifiers this test wakes join the cleared game-owned list.
public ValheimWorldScope WithTerrainModifiers()
Returns
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
orderUnityOrder
Returns
WithWorld(WorldGenerator)
public ValheimWorldScope WithWorld(WorldGenerator world)
Parameters
worldWorldGenerator
Returns
WithZdos()
A new, empty ZDOMan (this session's id is 1).
public ValheimWorldScope WithZdos()
Returns
WithZoneSystem()
public ValheimWorldScope WithZoneSystem()
Returns
WithoutZdos()
Removes the ZDO manager for a test of code that runs before world loading.
public ValheimWorldScope WithoutZdos()