quake2ts 0.0.82 → 0.0.84

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5033,6 +5033,55 @@ function cloneRngState(state) {
5033
5033
  }
5034
5034
  };
5035
5035
  }
5036
+ function hashValue(hash, value) {
5037
+ if (value === null || value === void 0) {
5038
+ return hash;
5039
+ }
5040
+ if (typeof value === "number") {
5041
+ return hashNumber(hash, value);
5042
+ }
5043
+ if (typeof value === "string") {
5044
+ return hashString(hash, value);
5045
+ }
5046
+ if (typeof value === "boolean") {
5047
+ return hashNumber(hash, value ? 1 : 0);
5048
+ }
5049
+ if (Array.isArray(value)) {
5050
+ let h = hash;
5051
+ for (const item of value) {
5052
+ h = hashValue(h, item);
5053
+ }
5054
+ return h;
5055
+ }
5056
+ if (typeof value === "object") {
5057
+ let h = hash;
5058
+ const keys = Object.keys(value).sort();
5059
+ for (const key of keys) {
5060
+ h = hashString(h, key);
5061
+ h = hashValue(h, value[key]);
5062
+ }
5063
+ return h;
5064
+ }
5065
+ return hash;
5066
+ }
5067
+ function calculateSaveChecksum(save) {
5068
+ let hash = FNV_OFFSET_BASIS;
5069
+ hash = hashNumber(hash, save.version);
5070
+ hash = hashNumber(hash, save.timestamp);
5071
+ hash = hashString(hash, save.map);
5072
+ hash = hashNumber(hash, save.difficulty);
5073
+ hash = hashNumber(hash, save.playtimeSeconds);
5074
+ hash = hashValue(hash, save.gameState);
5075
+ hash = hashValue(hash, save.level);
5076
+ hash = hashValue(hash, save.rng);
5077
+ hash = hashValue(hash, save.entities);
5078
+ hash = hashValue(hash, save.cvars);
5079
+ hash = hashValue(hash, save.configstrings);
5080
+ if (save.player) {
5081
+ hash = hashValue(hash, save.player);
5082
+ }
5083
+ return hash >>> 0;
5084
+ }
5036
5085
  function createSaveFile(options) {
5037
5086
  const {
5038
5087
  map,
@@ -5047,7 +5096,7 @@ function createSaveFile(options) {
5047
5096
  timestamp = Date.now(),
5048
5097
  player
5049
5098
  } = options;
5050
- return {
5099
+ const saveWithoutChecksum = {
5051
5100
  version: SAVE_FORMAT_VERSION,
5052
5101
  timestamp,
5053
5102
  map,
@@ -5061,6 +5110,10 @@ function createSaveFile(options) {
5061
5110
  configstrings: [...configstrings],
5062
5111
  player: player ? serializePlayerInventory(player) : void 0
5063
5112
  };
5113
+ return {
5114
+ ...saveWithoutChecksum,
5115
+ checksum: calculateSaveChecksum(saveWithoutChecksum)
5116
+ };
5064
5117
  }
5065
5118
  function parseSaveFile(serialized, options = {}) {
5066
5119
  const { allowNewerVersion = true } = options;
@@ -5074,7 +5127,7 @@ function parseSaveFile(serialized, options = {}) {
5074
5127
  if (version > SAVE_FORMAT_VERSION && !allowNewerVersion) {
5075
5128
  throw new Error(`Save version ${version} is newer than supported ${SAVE_FORMAT_VERSION}`);
5076
5129
  }
5077
- return {
5130
+ const parsedSave = {
5078
5131
  version,
5079
5132
  timestamp: ensureNumber(save.timestamp, "timestamp"),
5080
5133
  map: ensureString(save.map, "map"),
@@ -5086,8 +5139,16 @@ function parseSaveFile(serialized, options = {}) {
5086
5139
  entities: parseEntitySnapshot(save.entities),
5087
5140
  cvars: parseCvars(save.cvars),
5088
5141
  configstrings: parseConfigstrings(save.configstrings),
5089
- player: save.player ? save.player : void 0
5142
+ player: save.player ? save.player : void 0,
5143
+ checksum: save.checksum !== void 0 ? ensureNumber(save.checksum, "checksum") : void 0
5090
5144
  };
5145
+ if (parsedSave.checksum !== void 0) {
5146
+ const calculated = calculateSaveChecksum(parsedSave);
5147
+ if (calculated !== parsedSave.checksum) {
5148
+ throw new Error(`Save file checksum mismatch: expected ${parsedSave.checksum}, calculated ${calculated}`);
5149
+ }
5150
+ }
5151
+ return parsedSave;
5091
5152
  }
5092
5153
  function applySaveFile(save, targets) {
5093
5154
  targets.levelClock.restore(save.level);
@@ -5957,6 +6018,7 @@ export {
5957
6018
  applyRegularArmor,
5958
6019
  applySaveFile,
5959
6020
  calculateFallingDamage,
6021
+ calculateSaveChecksum,
5960
6022
  canPickupHealth,
5961
6023
  changeYaw,
5962
6024
  clampAmmoCounts,