You make a mod manager?

Read the companion. Merge clashes. Repair stale files.

What you get

The cost is a decompile step, a patch step, and a compile step. Your app runs the compile step only for a clash or a repair.

What a companion is

A companion is one .patch file that ships next to a mod VPK. The VPK holds the full mod and loads as it is. The companion holds a plain diff for each game file that the VPK replaces.

# spec: diffmod/v1
# decompiler: source2viewer-cli/20.1
# title: Compact HUD
--- a/panorama/layout/hud.xml
+++ b/panorama/layout/hud.xml
@@ -2,6 +2,7 @@
   <styles>
     <include src="s2r://panorama/styles/hud.vcss_c" />
     <include src="s2r://panorama/styles/hud_common.vcss_c" />
+    <include src="s2r://panorama/styles/compact_hud/main.vcss_c" />
   </styles>

The file name makes the pair. The companion of pak50_dir.vpk is pak50_dir.vpk.patch in the same folder. Each VPK has at most one companion. All header keys are optional.

What to build

  1. Find each pair. A file whose name ends in .vpk.patch is a companion. Remove .patch from the name to get its VPK, in the same folder. If that VPK is absent, skip the companion and warn.
  2. Keep each pair. If your app renames a VPK, rename its companion to match.
  3. Check each pair. Do this on install and after each game update. See Check a pair.
  4. Find clashes. A mod VPK changes each game file that it replaces. Two mods clash on a file if both change it.
  5. Rebuild each clashed file. See Rebuild a file.
  6. Find stale files. Do this after each game update. See Repair after a game update.
  7. Do nothing else. With no clash and no stale file, load each VPK as it is.

Check a pair

  1. Decompile each file in the mod VPK that the companion covers.
  2. Reverse apply the companion to those files.
  3. If the reverse apply fails, warn. Treat the VPK as a VPK with no companion.
  4. If the reverse apply succeeds, keep the result. It is the game file that the mod maker used.
(cd vpk-copy && git apply -R -p1 --check ../pak50_dir.vpk.patch)

Rebuild a file

  1. Decompile the game copy of the file. This is the base.
  2. Apply each companion that covers the file, in the mod order of the player.
  3. If a companion does not apply, leave it out of that file. Warn, and name the mod and the file.
  4. Compile the result with the CSDK resourcecompiler.
  5. Pack all rebuilt files into one VPK. That VPK must take priority over each mod VPK.

New files, such as the style sheet in the example above, still load from the mod VPKs.

If a VPK with no companion replaces the file, use its decompiled copy as the base. See spec rule 18.

Repair after a game update

  1. Check each pair again. The check gives the game file that the mod maker used.
  2. Compare that file with the game copy of today.
  3. If they differ, the VPK copy is stale. It hides Valve edits.
  4. Offer a rebuild of that file, and let the player decide.

A game update makes a VPK copy stale only if Valve edited that same file.

Tools

Reference and questions