@scm-js/core 0.17.0 → 0.18.0

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.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @scm-js/core 0.17.0 — StarCraft maps as the scmJS editor reads and writes them.
2
+ * @scm-js/core 0.18.0 — StarCraft maps as the scmJS editor reads and writes them.
3
3
  * Generated by `npm run build:core` in github.com/scm-js/scm-js from core/src/index.ts; the guide is docs/core.md.
4
4
  */
5
5
  import { CarriedModule as PatcherPlugin, DataSetRace, ExeArchive as PackageExeArchive, GAME_ARCHIVES, GAME_FOLDERS, GAME_NETWORK_TBL_ENTRIES, Identity, MAX_RACE_NAME as RACE_NAME_MAX, MOD_MESSAGES, ModDescription, ModError as ModSourceError, ModFiles, ModMessage, PatcherError, RaceCandidates, SYSTEM_FILES, ZipMember, gameCopyFiles, hashLines, hashLinesAsWere, isCodeFile as isModCode, isExe, isMpq, isPluginFile, isSystemFile, isSystemLine, isZip, libraryName, modRoot, normalizeRaces, probeNames, programEnd, raceCandidates, readMod, readZip, renderMessage, sha256Hex as fileDigest } from '@scm-js/mod';
@@ -890,6 +890,12 @@ export interface PlannedStored {
890
890
  size: number;
891
891
  /** False for a bare .chk, the one format that cannot carry them. */
892
892
  kept: boolean;
893
+ /**
894
+ * How many of them are left out: nothing in the map names them, and the hash table they
895
+ * pin has no room for them and the files the save writes (`fitStored`). The game never
896
+ * opens such a member; a protector puts them there to fill the table.
897
+ */
898
+ dropped: number;
893
899
  }
894
900
  export interface SavePlan {
895
901
  /** The CHK as it will be written. */
package/dist/index.mjs CHANGED
@@ -3595,16 +3595,25 @@ var CRYPT = (() => {
3595
3595
  }
3596
3596
  return table;
3597
3597
  })();
3598
- /** The file key of a member: the hash of its name after the last slash, as the game takes it (ASCII upper case). */
3599
- function fileKey(name, offset, size, adjusted) {
3600
- const plain = name.slice(Math.max(name.lastIndexOf("\\"), name.lastIndexOf("/")) + 1);
3598
+ /** MPQ's string hash of type `table` (0x100 name A, 0x200 name B, 0x300 file key), ASCII upper case, slashes as given. */
3599
+ function hashName(name, table) {
3601
3600
  let s1 = 2146271213, s2 = 4008636142;
3602
- for (let i = 0; i < plain.length; i++) {
3603
- let c = plain.charCodeAt(i) & 255;
3601
+ for (let i = 0; i < name.length; i++) {
3602
+ let c = name.charCodeAt(i) & 255;
3604
3603
  if (c >= 97 && c <= 122) c -= 32;
3605
- s1 = (CRYPT[HASH_FILE_KEY + c] ^ s1 + s2 >>> 0) >>> 0;
3604
+ s1 = (CRYPT[table + c] ^ s1 + s2 >>> 0) >>> 0;
3606
3605
  s2 = c + s1 + s2 + (s2 << 5 >>> 0) + 3 >>> 0;
3607
3606
  }
3607
+ return s1;
3608
+ }
3609
+ /** Whether a hash entry is the one a lookup of `name` matches: the two name hashes it compares. */
3610
+ function entryNames(entry, name) {
3611
+ const path = name.replace(/\//g, "\\");
3612
+ return entry.hashA === hashName(path, 256) && entry.hashB === hashName(path, 512);
3613
+ }
3614
+ /** The file key of a member: the hash of its name after the last slash, as the game takes it (ASCII upper case). */
3615
+ function fileKey(name, offset, size, adjusted) {
3616
+ const s1 = hashName(name.slice(Math.max(name.lastIndexOf("\\"), name.lastIndexOf("/")) + 1), HASH_FILE_KEY);
3608
3617
  return adjusted ? (s1 + offset ^ size) >>> 0 : s1;
3609
3618
  }
3610
3619
  /** Decrypts (`encrypt` false) or encrypts the whole 32-bit words of `data` in place with `key`. */
@@ -3831,9 +3840,11 @@ var STAREDIT_SECTOR_SIZE = 4096;
3831
3840
  async function saveMap(chk, options = {}) {
3832
3841
  const method = options.compress === "zlib" || options.compress === "pkware" ? options.compress : false;
3833
3842
  const encrypt = options.encrypt ?? false;
3834
- const members = options.stored?.members.filter(hasBytes) ?? [];
3835
- const stored = options.stored && members.length > 0 ? options.stored : null;
3836
3843
  const extras = [...options.extras ?? []].filter(([name]) => normalize$2(name) !== normalize$2(SCENARIO_PATH));
3844
+ const fit = fitStored(options.stored ?? null, 1 + extras.length + (options.editorMembers?.size ?? 0), options.listfile ?? true, options.referenced);
3845
+ if (!fit) throw new Error(t("The archive has no room for the files this save writes: its hash table is filled by members the editor could not read."));
3846
+ const members = fit.members;
3847
+ const stored = options.stored && members.length > 0 ? options.stored : null;
3837
3848
  let hashTable = stored?.hashTable ?? null;
3838
3849
  let sectorSize = stored ? stored.sectorSize : options.sectorSize ?? 4096;
3839
3850
  const carried = [];
@@ -3853,7 +3864,7 @@ async function saveMap(chk, options = {}) {
3853
3864
  });
3854
3865
  carriedNames.add(name);
3855
3866
  }
3856
- const listfile = options.listfile ?? true;
3867
+ const listfile = fit.listfile;
3857
3868
  const creator = new Creator({
3858
3869
  sectorSize,
3859
3870
  listfile: listfile && carried.length === 0,
@@ -3893,6 +3904,49 @@ async function saveMap(chk, options = {}) {
3893
3904
  return creator.writeAsync();
3894
3905
  }
3895
3906
  /**
3907
+ * Which stored members a save carries. They are laid over the hash table they came from, which
3908
+ * keeps its size, and every file the save writes by name (`files`: the scenario, the extras,
3909
+ * the editor's members; the `(listfile)` on top) needs a free slot in it. A protector sizes
3910
+ * the table to its files and fills it with members nothing names, so on such a map there may
3911
+ * be none. The game opens a member only by a name the scenario carries, and every such name
3912
+ * was tried when the map was opened (and is tried again here, with `referenced`), so the
3913
+ * members no name reaches are never opened: when they do not fit, they are left out, and with
3914
+ * none left the archive is written afresh with a table of its own size. A member read by name
3915
+ * whose bytes could not be decoded (`unreadable`) is always kept. Last, the `(listfile)` is
3916
+ * dropped — the game never reads it. Null when even that leaves no room.
3917
+ */
3918
+ function fitStored(stored, files, listfile, referenced = []) {
3919
+ const all = stored?.members.filter(hasBytes) ?? [];
3920
+ if (!stored || all.length === 0) return {
3921
+ members: [],
3922
+ dropped: 0,
3923
+ listfile
3924
+ };
3925
+ const room = (kept) => stored.hashTable.length - kept;
3926
+ if (room(all.length) >= files + (listfile ? 1 : 0)) return {
3927
+ members: all,
3928
+ dropped: 0,
3929
+ listfile
3930
+ };
3931
+ const reached = reachedStored(stored, referenced);
3932
+ if (reached.length === 0 || room(reached.length) >= files + (listfile ? 1 : 0)) return {
3933
+ members: reached,
3934
+ dropped: all.length - reached.length,
3935
+ listfile
3936
+ };
3937
+ if (room(reached.length) >= files) return {
3938
+ members: reached,
3939
+ dropped: all.length - reached.length,
3940
+ listfile: false
3941
+ };
3942
+ return null;
3943
+ }
3944
+ /** The stored members a name reaches: one the scenario refers to (`referenced`), or one read by name but not decoded. */
3945
+ function reachedStored(stored, referenced) {
3946
+ const named = [...stored.unreadable, ...referenced];
3947
+ return stored.members.filter((m) => hasBytes(m) && named.some((name) => entryNames(m.hash, name)));
3948
+ }
3949
+ /**
3896
3950
  * Whether a stored member has bytes to carry. A protector's block entry pointing past the
3897
3951
  * end of the file came back from mopaq before 1.6.2 as an empty member at that offset, and
3898
3952
  * the writer padded the archive out to reach it — a save of tens of megabytes of zeros, or a
@@ -9375,6 +9429,28 @@ var SCRIPT_FOLDER = "trigscript\\";
9375
9429
  var SCRIPT_MEMBER = `${SCRIPT_FOLDER}main.ts`;
9376
9430
  var MANIFEST_MEMBER = `${SCRIPT_FOLDER}build.json`;
9377
9431
  /**
9432
+ * Every archive path the scenario itself names: the WAV table's entries and the `wav`
9433
+ * argument of every trigger and briefing action, plus the members the TrigScript
9434
+ * plugin keeps its entry file and build manifest in (the manifest then names the rest,
9435
+ * see `scriptMembersFromManifest`). What `readMembers` probes an archive for when its
9436
+ * (listfile) is gone — the one way a protected map's sounds get their names back.
9437
+ */
9438
+ function referencedMembers(scn) {
9439
+ const out = /* @__PURE__ */ new Set();
9440
+ const add = (index) => {
9441
+ const path = index > 0 ? getString(scn.strings, index) : null;
9442
+ if (path) out.add(path);
9443
+ };
9444
+ for (const index of scn.wavs ?? []) add(index);
9445
+ for (const t of scn.triggers) for (const a of t.actions) for (const s of actionStrings(a, false)) if (s.kind === "wav") add(s.index);
9446
+ for (const t of scn.briefing) for (const a of t.actions) for (const s of actionStrings(a, true)) if (s.kind === "wav") add(s.index);
9447
+ return [
9448
+ ...out,
9449
+ SCRIPT_MEMBER,
9450
+ MANIFEST_MEMBER
9451
+ ];
9452
+ }
9453
+ /**
9378
9454
  * The script's other files, from its build manifest: the manifest lists every file the
9379
9455
  * block was built from, so an archive without a listfile still gives them up by name.
9380
9456
  * Empty for anything that is not such a manifest.
@@ -9555,19 +9631,22 @@ function planSave(scn, extras, options, storedMembers = null) {
9555
9631
  kind: extraKind(name),
9556
9632
  kept: options.format !== "chk" && !omitted.has(name.toLowerCase())
9557
9633
  }));
9634
+ const fit = storedMembers ? fitStored(storedMembers, 1 + planned.filter((e) => e.kept).length, true, referencedMembers(scn)) : null;
9558
9635
  const stored = storedMembers && storedMembers.members.length > 0 ? {
9559
9636
  members: storedMembers,
9560
9637
  count: storedMembers.members.length,
9561
9638
  size: storedMembers.members.reduce((n, m) => n + m.data.length, 0),
9562
- kept: options.format !== "chk"
9639
+ kept: options.format !== "chk",
9640
+ dropped: fit?.dropped ?? 0
9563
9641
  } : null;
9564
9642
  const warnings = [];
9565
9643
  if (options.stripTerrainEditing && counts.terrainEditing > 0) warnings.push(t("Without ISOM, TILE and DD2 the isometric brush and the Doodads layer have nothing to work with when the file is opened in an editor again. The map plays the same."));
9566
9644
  if (options.stripBookkeeping && counts.bookkeeping > 0) warnings.push(t("Switch names and the sound list are lost to editors; the game does not read them."));
9567
9645
  if (options.stripUnknown && counts.unknown > 0) warnings.push(t("{unknown, plural, one {# section} other {# sections}} with names the format reference does not know are left out. Another editor or a protector may have put them there.", { unknown: counts.unknown }));
9568
9646
  if (options.format === "chk" && (extras.size > 0 || stored)) warnings.push(t("A bare .chk is the scenario alone: the archive's other files are not written."));
9569
- if (stored?.kept) {
9570
- const n = stored.count;
9647
+ if (stored?.kept && stored.dropped > 0) warnings.push(t("{n, plural, one {# archive member nothing in the map names is} other {# archive members nothing in the map names are}} left out: the archive's hash table has no room for them and the files this save writes. The game never opens them; a protector puts them there to fill the table.", { n: stored.dropped }));
9648
+ if (stored?.kept && stored.count > stored.dropped) {
9649
+ const n = stored.count - stored.dropped;
9571
9650
  warnings.push(t("{n, plural, one {# archive member has no name the editor knows{decoded}; it is} other {# archive members have no name the editor knows{decoded}; they are}} written back exactly as stored, and the archive keeps its {sectorSize}-byte sectors{zlib}.", {
9572
9651
  n,
9573
9652
  decoded: stored.members.unreadable.length > 0 ? t(", or could not be decoded") : "",
@@ -9618,10 +9697,11 @@ async function buildMapFile(scn, extras, options, plan, stored = null) {
9618
9697
  plan ??= planSave(scn, extras, options, stored);
9619
9698
  const chk = buildChk(plan);
9620
9699
  if (options.format === "chk") return chk;
9621
- const kept = plan.stored?.kept ? plan.stored.members : null;
9700
+ const kept = plan.stored?.kept && plan.stored.count > plan.stored.dropped ? plan.stored.members : null;
9622
9701
  return saveMap(chk, {
9623
9702
  extras: keptExtras(plan, extras),
9624
9703
  stored: kept,
9704
+ referenced: referencedMembers(scn),
9625
9705
  compress: options.compression,
9626
9706
  encrypt: options.encrypt,
9627
9707
  sectorSize: requiredSectorSize(kept, options.compression === "zlib" ? 65536 : STAREDIT_SECTOR_SIZE)
@@ -9663,28 +9743,6 @@ function wavUsage(scn, stringIndex) {
9663
9743
  scan(t("Briefing"), scn.briefing, true);
9664
9744
  return out;
9665
9745
  }
9666
- /**
9667
- * Every archive path the scenario itself names: the WAV table's entries and the `wav`
9668
- * argument of every trigger and briefing action, plus the members the TrigScript
9669
- * plugin keeps its entry file and build manifest in (the manifest then names the rest,
9670
- * see `scriptMembersFromManifest`). What `readMembers` probes an archive for when its
9671
- * (listfile) is gone — the one way a protected map's sounds get their names back.
9672
- */
9673
- function referencedMembers(scn) {
9674
- const out = /* @__PURE__ */ new Set();
9675
- const add = (index) => {
9676
- const path = index > 0 ? getString(scn.strings, index) : null;
9677
- if (path) out.add(path);
9678
- };
9679
- for (const index of scn.wavs ?? []) add(index);
9680
- for (const t of scn.triggers) for (const a of t.actions) for (const s of actionStrings(a, false)) if (s.kind === "wav") add(s.index);
9681
- for (const t of scn.briefing) for (const a of t.actions) for (const s of actionStrings(a, true)) if (s.kind === "wav") add(s.index);
9682
- return [
9683
- ...out,
9684
- SCRIPT_MEMBER,
9685
- MANIFEST_MEMBER
9686
- ];
9687
- }
9688
9746
  function readWavs(scn) {
9689
9747
  return scn.wavs ? scn.wavs.slice() : defaultWavs();
9690
9748
  }
@@ -9831,6 +9889,9 @@ async function packBuiltMap(input) {
9831
9889
  extras: members,
9832
9890
  editorMembers: /* @__PURE__ */ new Map([[BUILD_SOURCE_MEMBER, input.sourceChk], [BUILD_MANIFEST_MEMBER, new TextEncoder().encode(JSON.stringify(manifest))]]),
9833
9891
  stored: input.stored,
9892
+ referenced: (function* () {
9893
+ yield* referencedMembers(parseScenario(out.chk));
9894
+ })(),
9834
9895
  compress: compression,
9835
9896
  encrypt,
9836
9897
  sectorSize: requiredSectorSize(input.stored, compression === "zlib" ? 65536 : STAREDIT_SECTOR_SIZE)
@@ -9934,6 +9995,9 @@ function writeArchiveBytes(chk, options = {}) {
9934
9995
  return saveMap(chk, {
9935
9996
  extras: options.extras,
9936
9997
  stored: options.stored ?? null,
9998
+ referenced: (function* () {
9999
+ yield* referencedMembers(parseScenario(chk));
10000
+ })(),
9937
10001
  compress: compression,
9938
10002
  encrypt: options.encrypt ?? false,
9939
10003
  listfile: options.listfile ?? true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scm-js/core",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "description": "StarCraft: Brood War maps in Node.js and the browser, as the scmJS editor reads and writes them: open .scm/.scx/.chk, triggers, strings, units, locations, settings, Check Map, and write the map back",
5
5
  "keywords": [
6
6
  "starcraft",