From fab0060bcaae5e330cf3f237f74a21a5dfc080eb Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 3 Oct 2026 20:46:33 +0200 Subject: [PATCH] Verify adaptive API restoration and override ownership --- .../client/DistantTerrainBudget.java | 1 + .../client/DistantTerrainBudgetTest.java | 56 +++++++++++++++++++ tools/build_mod.py | 8 +++ 3 files changed, 65 insertions(+) create mode 100644 mods/horizon-lab/src/test/java/dev/horizonlab/client/DistantTerrainBudgetTest.java diff --git a/mods/horizon-lab/src/main/java/dev/horizonlab/client/DistantTerrainBudget.java b/mods/horizon-lab/src/main/java/dev/horizonlab/client/DistantTerrainBudget.java index 8a3d26db0..4fa9430c2 100644 --- a/mods/horizon-lab/src/main/java/dev/horizonlab/client/DistantTerrainBudget.java +++ b/mods/horizon-lab/src/main/java/dev/horizonlab/client/DistantTerrainBudget.java @@ -58,6 +58,7 @@ final class DistantTerrainBudget { void suspend() { if (setting != null && owned != null && java.util.Objects.equals(setting.getApiValue(), owned)) { setting.clearValue(); + HorizonLab.LOGGER.info("Adaptive terrain override released; effective runtime ratio: {}", setting.getValue()); } owned = null; policy = setting == null ? null : new TerrainBudget(setting.getTrueValue()); diff --git a/mods/horizon-lab/src/test/java/dev/horizonlab/client/DistantTerrainBudgetTest.java b/mods/horizon-lab/src/test/java/dev/horizonlab/client/DistantTerrainBudgetTest.java new file mode 100644 index 000000000..f8796c25c --- /dev/null +++ b/mods/horizon-lab/src/test/java/dev/horizonlab/client/DistantTerrainBudgetTest.java @@ -0,0 +1,56 @@ +package dev.horizonlab.client; + +import com.seibel.distanthorizons.api.DhApi; +import com.seibel.distanthorizons.api.interfaces.config.IDhApiConfig; +import com.seibel.distanthorizons.api.interfaces.config.IDhApiConfigValue; +import com.seibel.distanthorizons.api.interfaces.config.client.IDhApiMultiThreadingConfig; +import java.lang.reflect.Proxy; + +/** Exercise the actual adapter against the upstream API contracts, without a game process. */ +public final class DistantTerrainBudgetTest { + private static Double override; + private static double userValue = 0.5; + + @SuppressWarnings("unchecked") + private static T proxy(Class type, java.lang.reflect.InvocationHandler handler) { + return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}, handler); + } + + private static void check(boolean value, String reason) { if (!value) throw new AssertionError(reason); } + + public static void main(String[] args) { + IDhApiConfigValue value = proxy(IDhApiConfigValue.class, (p, method, values) -> switch (method.getName()) { + case "getTrueValue" -> userValue; + case "getValue" -> override == null ? userValue : override; + case "getApiValue" -> override; + case "getCanBeOverrodeByApi" -> true; + case "setValue" -> { override = (Double) values[0]; yield true; } + case "clearValue" -> { override = null; yield true; } + default -> throw new AssertionError("Unexpected API call: " + method); + }); + var threading = proxy(IDhApiMultiThreadingConfig.class, (p, method, values) -> value); + DhApi.Delayed.configs = proxy(IDhApiConfig.class, (p, method, values) -> threading); + + DistantTerrainBudget budget = new DistantTerrainBudget(); + budget.observe(1, 0, 16.667); + budget.observe(3_000_000_001L, 40_000_000, 16.667); + check(Math.abs(override - 0.325) < 1e-9, "Actual adapter applies the policy through the API"); + budget.suspend(); + check(override == null && userValue == 0.5, "Suspending releases only our temporary override"); + budget.observe(4_000_000_001L, 0, 16.667); + budget.observe(7_000_000_001L, 40_000_000, 16.667); + check(Math.abs(override - 0.325) < 1e-9, "Resuming resets stale frame observations and policy state"); + userValue = 0.1; + budget.observe(8_000_000_001L, 40_000_000, 16.667); + budget.observe(11_000_000_001L, 40_000_000, 16.667); + check(override <= 0.1, "A changed user ceiling is honored"); + override = 0.7; + budget.observe(12_000_000_001L, 40_000_000, 16.667); + budget.suspend(); + check(override == 0.7 && budget.status().startsWith("yielded"), "Never clear another mod's changed override"); + budget = new DistantTerrainBudget(); + budget.observe(13_000_000_001L, 40_000_000, 16.667); + check(override == 0.7 && budget.status().startsWith("yielded"), "Never acquire an existing override"); + System.out.println("Distant terrain API adapter tests passed"); + } +} diff --git a/tools/build_mod.py b/tools/build_mod.py index ee7ce039e..6f17c2321 100644 --- a/tools/build_mod.py +++ b/tools/build_mod.py @@ -61,6 +61,14 @@ def build(): argfile = BUILD / "javac.args" argfile.write_text("\n".join(json.dumps(arg) for arg in command) + "\n") subprocess.run(["javac", "@" + str(argfile)], cwd=ROOT, check=True) + adapter_tests = BUILD / "adapter-tests" + adapter_tests.mkdir(parents=True, exist_ok=True) + test_classpath = os.pathsep.join(map(str, [classes, *jars])) + subprocess.run(["javac", "--release", "25", "-proc:none", "-classpath", test_classpath, + "-d", str(adapter_tests), + str(SOURCE / "test/java/dev/horizonlab/client/DistantTerrainBudgetTest.java")], check=True) + subprocess.run(["java", "-cp", os.pathsep.join([str(adapter_tests), test_classpath]), + "dev.horizonlab.client.DistantTerrainBudgetTest"], check=True) output = ROOT / "dist" / f"horizon-lab-{lock['pack_version']}.jar" output.parent.mkdir(exist_ok=True) entries = {path.relative_to(classes).as_posix(): path.read_bytes() for path in classes.rglob("*.class")}