// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . // ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· // ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ // ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ // ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ // Magicbane Emulator Project © 2013 - 2026 // https://www.magicbane.com package engine.parsers.cache; import engine.gameManager.ConfigManager; import engine.mbEnums; import engine.objects.BaseClass; import engine.objects.Race; import engine.parsers.objects.RuneTemplate; import org.pmw.tinylog.Logger; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Objects; import java.util.function.Consumer; // Dev-only check that compares DB-built objects with the cache JSON before a loader is switched. // Enabled by the optional env var MB_PARITY_CHECK; not in the ConfigManager enum, which requires every entry. public class ParityCheck { private static final String FLAG_NAME = "MB_PARITY_CHECK"; private static final LinkedHashMap> comparators = new LinkedHashMap<>(); private final String table; private int comparedCount = 0; private int differenceCount = 0; private int missingCount = 0; static { register("static_rune_race", ParityCheck::compareRaces); } private ParityCheck(String table) { this.table = table; } public static boolean isEnabled() { return ConfigManager.configMap.getOrDefault(FLAG_NAME, "false").equalsIgnoreCase("true"); } public static void register(String table, Consumer comparator) { comparators.put(table, comparator); } public static void run() { ParityCheck check; for (String table : comparators.keySet()) { check = new ParityCheck(table); try { comparators.get(table).accept(check); } catch (Exception e) { Logger.error("Parity: " + table + ": comparator failed: " + e); } Logger.info("Parity: " + table + ": " + check.comparedCount + " compared, " + check.differenceCount + " differences, " + check.missingCount + " missing in json"); } } // Returns false when the row has no JSON counterpart, so the comparator can skip its fields. public boolean compareRow(int id, Object jsonObject) { comparedCount++; if (jsonObject == null) { missingCount++; Logger.warn("Parity: " + table + "/" + id + ": missing in json"); return false; } return true; } public void compareField(int id, String field, Object dbValue, Object jsonValue) { if (Objects.equals(dbValue, jsonValue)) return; differenceCount++; Logger.warn("Parity: " + table + "/" + id + "/" + field + ": db=" + dbValue + " json=" + jsonValue); } private static void compareRaces(ParityCheck check) { Race race; Race jsonRace; RuneTemplate rune; int id; for (mbEnums.RaceType raceType : mbEnums.RaceType.values()) { id = raceType.getRuneID(); race = Race.getRace(id); if (race == null) { Logger.warn("Parity: static_rune_race/" + id + ": not loaded from the db"); continue; } rune = RuneTemplate._runes.get(id); if (rune != null && rune.rune_type != mbEnums.RuneType.RACE) rune = null; if (!check.compareRow(id, rune)) continue; jsonRace = new Race(rune, race); check.compareField(id, "name", race.getName(), jsonRace.getName()); check.compareField(id, "strStart", race.getStrStart(), jsonRace.getStrStart()); check.compareField(id, "strMin", race.getStrMin(), jsonRace.getStrMin()); check.compareField(id, "strMax", race.getStrMax(), jsonRace.getStrMax()); check.compareField(id, "dexStart", race.getDexStart(), jsonRace.getDexStart()); check.compareField(id, "dexMin", race.getDexMin(), jsonRace.getDexMin()); check.compareField(id, "dexMax", race.getDexMax(), jsonRace.getDexMax()); check.compareField(id, "conStart", race.getConStart(), jsonRace.getConStart()); check.compareField(id, "conMin", race.getConMin(), jsonRace.getConMin()); check.compareField(id, "conMax", race.getConMax(), jsonRace.getConMax()); check.compareField(id, "intStart", race.getIntStart(), jsonRace.getIntStart()); check.compareField(id, "intMin", race.getIntMin(), jsonRace.getIntMin()); check.compareField(id, "intMax", race.getIntMax(), jsonRace.getIntMax()); check.compareField(id, "spiStart", race.getSpiStart(), jsonRace.getSpiStart()); check.compareField(id, "spiMin", race.getSpiMin(), jsonRace.getSpiMin()); check.compareField(id, "spiMax", race.getSpiMax(), jsonRace.getSpiMax()); check.compareField(id, "token", race.getToken(), jsonRace.getToken()); check.compareField(id, "healthBonus", race.getHealthBonus(), jsonRace.getHealthBonus()); check.compareField(id, "manaBonus", race.getManaBonus(), jsonRace.getManaBonus()); check.compareField(id, "staminaBonus", race.getStaminaBonus(), jsonRace.getStaminaBonus()); check.compareField(id, "startingPoints", race.getStartingPoints(), jsonRace.getStartingPoints()); check.compareField(id, "hairStyles", race.getHairStyles(), jsonRace.getHairStyles()); check.compareField(id, "beardStyles", race.getBeardStyles(), jsonRace.getBeardStyles()); check.compareField(id, "baseClasses", getObjectIds(race.getValidBaseClasses()), getObjectIds(jsonRace.getValidBaseClasses())); } } private static HashSet getObjectIds(Iterable baseClasses) { HashSet ids = new HashSet<>(); for (BaseClass baseClass : baseClasses) ids.add(baseClass.getObjectUUID()); return ids; } }