Languages: C#, .NET CIL
Technologies: Unity, Harmony, BepinEx
Skills: Reverse Engineering
This page describes three mods for the Unity game Arkshot, implemented using BepinEx and HarmonyX:
Installation instructions can be found in the README of each mod.
Note: Arkshot is multiplayer only and there is no active playerbase, so you would need a friend.
Arkshot is a bow-based FPS game which is a hybrid between a party game and a competitive arena shooter. When playing it with my friends we treat it casually, and found some of the stamina limitations to have negative effects on our enjoyment. There’s also more than 4 of us, but the game limits lobbies to 4 players. I created these mods to solve these problems for us.
MultiplayerSync provides an API which allows mods to register themselves as requirements for joining a lobby, as well as sync arbitrary values and BepinEx ConfigEntry instances. Mod registration can be conditional, such that if a client’s config would be identical to vanilla even with the mod loaded other clients will not be required to have the mod loaded.
If a client does not have a required mod installed when joining a lobby, they immediately leave and a pop-up appears listing their missing mods. If the host does not have a mod and the client does, the default config is used. Upon a successful join the OnJoin event is called, allowing mods to handle the change in configuration.
Required mods and synced data are stored in static Dictionaries. When a lobby is created these are added to the Photon room custom properties, and when a modded client joins it reads the synced data from this. Custom serialization functions were required for this. The read function is called by a Prefix patch, and the function which writes these properties is called by a function call injected by a CIL transpiler.
MorePlayers registers itself as a conditional MultiplayerSync requirement if the maxPlayerCount is greater than 4.
It uses a transpiler on the createRoom method to replace the hard-coded constant on the room size with a read of the config variable.
It also patches the spawnPlayer method. The vanilla game delays your respawn by player_id / 4f, presumably to help avoid spawning players in the same location. However, when this delay becomes greater than 1, players enter an infinite respawn loop upon dying. To avoid this, the delay is replaced with id % 4 / 4f. During the initial spawn, as well as if no free spawn-point can be found on respawn, the player’s id is used as an index in the spawn point list - however this causes issues if there are more players than spawn points. This fallback is replaced with id % spawn_count.
StaminaPlus utilises MultiplayerSync’s SyncedConfigEntry to sync the modified stamina costs between clients.
The default stamina costs are hard-coded into the game dll, so a transpiler was required to modify these. The hard-coded values are replaced with reads of the SyncedConfigEntry values, produced by SyncedEntry.GetValueIL. Some modifications also required more complicated changes in logic, such as inserting or bypassing branches. To simplify this I created a set of helper functions which perform certain tasks, such as finding a specific CIL instruction. These can be found under the Helpers class. A few simple examples of their use are found below, while more complex branches and logic that requires injecting local variables can be found in Plugin.cs.
foreach (CodeInstruction instr in SkipCond(instr => NextF(instr, 100f)))
{
yield return instr;
}
foreach (CodeInstruction instr in maxStamina.SyncedEntry.GetValueIL())
{
yield return instr;
}
100f with a read of maxStamina, leaving the instructions up to the read intact.//only check stamina >= 0 if dash has a stamina cost
Label skipStaminaCheck = generator.DefineLabel();
foreach (CodeInstruction instr in dashCost.SyncedEntry.GetValueIL())
{
yield return instr;
}
yield return new CodeInstruction(OpCodes.Ldc_R4, 0f);
yield return new CodeInstruction(OpCodes.Ble, skipStaminaCheck);
foreach (CodeInstruction instr in PassX(4))
{
yield return instr;
}
CodeInstruction last = GetNext();
last.labels.Add(skipStaminaCheck);
yield return last;
stamina > 0, with a branch that checks dashCost <= 0 || stamina > 0This approach is fragile to changes in the code-base, but the game has not been updated since 2020 so this is not too big a concern.
The mods were ultimately primarily designed for me and my friends to use, and the game has basically no players, so I didn’t include some more player-friendly features. If this was not the case, I would implement
SyncedEntry can be specified entirely by the mod, easily allowing for conflicts