koishipro-core.js 1.0.2 → 1.0.7

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
@@ -5,7 +5,6 @@ export * from './src/types';
5
5
  export * from './src/structs';
6
6
  export * from './src/adapters';
7
7
  export * from './src/constants';
8
- export * from './src/utility';
9
8
  export * from './src/vendor';
10
9
  export * from './src/sqljs-card-reader';
11
10
  export * from './src/play-yrp';
package/dist/index.mjs CHANGED
@@ -1,5 +1,12 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
1
8
  // index.ts
2
- import { Buffer } from "buffer";
9
+ import { Buffer as Buffer2 } from "buffer";
3
10
 
4
11
  // src/vendor/ocgcore-constants.ts
5
12
  var OcgcoreCommonConstants = {
@@ -394,11 +401,17 @@ function readU32(buf, offset) {
394
401
  );
395
402
  }
396
403
 
397
- // src/adapters/ocgcore-parsers.ts
404
+ // src/utility/utf8.ts
405
+ var encoder = new TextEncoder();
398
406
  var decoder = new TextDecoder("utf-8");
407
+ function encodeUtf8(value) {
408
+ return encoder.encode(value);
409
+ }
399
410
  function decodeUtf8(value) {
400
411
  return decoder.decode(value);
401
412
  }
413
+
414
+ // src/adapters/ocgcore-parsers.ts
402
415
  function parseCardInfo(payload) {
403
416
  let offset = 0;
404
417
  const flags = readI32(payload, offset) >>> 0;
@@ -680,13 +693,6 @@ function normalizeStartDuelOptions(options) {
680
693
  return ((duelRule & 65535) << 16 | duelOptions & 65535) >>> 0;
681
694
  }
682
695
 
683
- // src/utility/utf8.ts
684
- var encoder = new TextEncoder();
685
- var decoder2 = new TextDecoder("utf-8");
686
- function encodeUtf8(value) {
687
- return encoder.encode(value);
688
- }
689
-
690
696
  // src/ocgcore-duel.ts
691
697
  var OcgcoreDuel = class {
692
698
  constructor(ocgcoreWrapper, duelPtr) {
@@ -941,6 +947,9 @@ var OcgcoreWrapper = class {
941
947
  this.scriptReaderFunc = 0;
942
948
  this.cardReaderFunc = 0;
943
949
  this.messageHandlerFunc = 0;
950
+ this.scriptReaders = [];
951
+ this.cardReaders = [];
952
+ this.messageHandlers = [];
944
953
  this.encoder = new TextEncoder();
945
954
  this.decoder = new TextDecoder("utf-8");
946
955
  this.duels = /* @__PURE__ */ new Map();
@@ -948,6 +957,101 @@ var OcgcoreWrapper = class {
948
957
  this.heapView = new DataView(this.heapU8.buffer);
949
958
  this.scriptBufferSize = options?.scriptBufferSize ?? 1048576;
950
959
  this.logBufferSize = options?.logBufferSize ?? 1024;
960
+ this.scriptReaderFunc = this.createFunction((scriptPtr, lenPtr) => {
961
+ const scriptPath = this.getUTF8String(scriptPtr);
962
+ let content;
963
+ for (const reader of this.scriptReaders) {
964
+ try {
965
+ content = reader(scriptPath);
966
+ } catch {
967
+ content = null;
968
+ }
969
+ if (content != null) {
970
+ break;
971
+ }
972
+ }
973
+ if (content == null) {
974
+ return 0;
975
+ }
976
+ if (!this.scriptBufferPtr) {
977
+ this.scriptBufferPtr = this.ocgcoreModule._malloc(
978
+ this.scriptBufferSize
979
+ );
980
+ }
981
+ const bytes = typeof content === "string" ? this.encoder.encode(content) : content;
982
+ if (bytes.length > this.scriptBufferSize) {
983
+ this.ocgcoreModule._free(this.scriptBufferPtr);
984
+ this.scriptBufferPtr = this.ocgcoreModule._malloc(bytes.length);
985
+ this.scriptBufferSize = bytes.length;
986
+ }
987
+ this.heapU8.set(bytes, this.scriptBufferPtr);
988
+ this.heapView.setInt32(lenPtr, bytes.length, true);
989
+ return this.scriptBufferPtr;
990
+ }, "iii");
991
+ this.ocgcoreModule._set_script_reader(this.scriptReaderFunc);
992
+ this.cardReaderFunc = this.createFunction((cardId, cardDataPtr) => {
993
+ let data;
994
+ for (const reader of this.cardReaders) {
995
+ try {
996
+ data = reader(cardId);
997
+ } catch {
998
+ data = null;
999
+ }
1000
+ if (data) {
1001
+ break;
1002
+ }
1003
+ }
1004
+ if (!data) {
1005
+ return 0;
1006
+ }
1007
+ const CardDataCtor = CardDataStruct;
1008
+ let buf;
1009
+ if (data instanceof CardDataCtor) {
1010
+ buf = CardDataStruct.raw(data);
1011
+ } else {
1012
+ const cardData = new CardDataCtor();
1013
+ cardData.code = data.code;
1014
+ cardData.alias = data.alias;
1015
+ const targetSetcode = cardData.setcode;
1016
+ targetSetcode.fill(0);
1017
+ if (data.setcode instanceof Uint16Array && data.setcode.length === 16) {
1018
+ targetSetcode.set(data.setcode);
1019
+ } else {
1020
+ for (let i = 0; i < 16 && i < data.setcode.length; i++) {
1021
+ targetSetcode[i] = data.setcode[i];
1022
+ }
1023
+ }
1024
+ cardData.type = data.type;
1025
+ cardData.level = data.level;
1026
+ cardData.attribute = data.attribute;
1027
+ cardData.race = data.race;
1028
+ cardData.attack = data.attack;
1029
+ cardData.defense = data.defense;
1030
+ cardData.lscale = data.lscale;
1031
+ cardData.rscale = data.rscale;
1032
+ cardData.linkMarker = data.linkMarker;
1033
+ buf = CardDataStruct.raw(cardData);
1034
+ }
1035
+ this.heapU8.set(buf, cardDataPtr);
1036
+ return 0;
1037
+ }, "iii");
1038
+ this.ocgcoreModule._set_card_reader(this.cardReaderFunc);
1039
+ this.messageHandlerFunc = this.createFunction((duelPtr, messageType) => {
1040
+ if (!this.logBufferPtr) {
1041
+ this.logBufferPtr = this.ocgcoreModule._malloc(this.logBufferSize);
1042
+ }
1043
+ this.ocgcoreModule._get_log_message(duelPtr, this.logBufferPtr);
1044
+ const message = this.getUTF8String(this.logBufferPtr);
1045
+ const type = messageType === 1 ? "ScriptError" /* ScriptError */ : messageType === 2 ? "DebugMessage" /* DebugMessage */ : messageType;
1046
+ const duel = this.getOrCreateDuel(duelPtr);
1047
+ for (const handler of this.messageHandlers) {
1048
+ try {
1049
+ handler(duel, message, type);
1050
+ } catch {
1051
+ }
1052
+ }
1053
+ }, "iii");
1054
+ this.ocgcoreModule._set_message_handler(this.messageHandlerFunc);
951
1055
  }
952
1056
  getUTF8String(ptr) {
953
1057
  let length = 0;
@@ -966,7 +1070,10 @@ var OcgcoreWrapper = class {
966
1070
  const encoded = args.map(
967
1071
  (item) => typeof item === "string" ? this.encoder.encode(item) : item
968
1072
  );
969
- const totalLength = encoded.reduce((acc, bytes) => acc + bytes.length + 1, 0);
1073
+ const totalLength = encoded.reduce(
1074
+ (acc, bytes) => acc + bytes.length + 1,
1075
+ 0
1076
+ );
970
1077
  if (totalLength > this.tmpStringBufferSize) {
971
1078
  if (this.tmpStringBufferPtr) {
972
1079
  this.ocgcoreModule._free(this.tmpStringBufferPtr);
@@ -1009,7 +1116,7 @@ var OcgcoreWrapper = class {
1009
1116
  this.ocgcoreModule._free(ptr);
1010
1117
  return this.getOrCreateDuel(duelPtr);
1011
1118
  }
1012
- defaultScriptReader(namePtr, dataPtr) {
1119
+ _defaultScriptReader(namePtr, dataPtr) {
1013
1120
  return this.ocgcoreModule._default_script_reader(namePtr, dataPtr);
1014
1121
  }
1015
1122
  malloc(size) {
@@ -1018,99 +1125,29 @@ var OcgcoreWrapper = class {
1018
1125
  free(ptr) {
1019
1126
  this.ocgcoreModule._free(ptr);
1020
1127
  }
1021
- _setScriptReader(funcPtr) {
1022
- this.ocgcoreModule._set_script_reader(funcPtr);
1023
- }
1024
- _setCardReader(funcPtr) {
1025
- this.ocgcoreModule._set_card_reader(funcPtr);
1026
- }
1027
- _setMessageHandler(funcPtr) {
1028
- this.ocgcoreModule._set_message_handler(funcPtr);
1029
- }
1030
- stdioExit() {
1128
+ _stdioExit() {
1031
1129
  this.ocgcoreModule.___stdio_exit();
1032
1130
  }
1033
- setScriptReader(reader) {
1034
- if (this.scriptReaderFunc) {
1035
- this.ocgcoreModule.removeFunction(this.scriptReaderFunc);
1036
- }
1037
- if (!this.scriptBufferPtr) {
1038
- this.scriptBufferPtr = this.ocgcoreModule._malloc(this.scriptBufferSize);
1131
+ setScriptReader(reader, reset = false) {
1132
+ if (reset) {
1133
+ this.scriptReaders = [];
1039
1134
  }
1040
- this.scriptReaderFunc = this.createFunction((scriptPtr, lenPtr) => {
1041
- const scriptPath = this.getUTF8String(scriptPtr);
1042
- const content = reader(scriptPath);
1043
- if (content == null) {
1044
- return 0;
1045
- }
1046
- const bytes = typeof content === "string" ? this.encoder.encode(content) : content;
1047
- if (bytes.length > this.scriptBufferSize) {
1048
- this.ocgcoreModule._free(this.scriptBufferPtr);
1049
- this.scriptBufferPtr = this.ocgcoreModule._malloc(bytes.length);
1050
- this.scriptBufferSize = bytes.length;
1051
- }
1052
- this.heapU8.set(bytes, this.scriptBufferPtr);
1053
- this.heapView.setInt32(lenPtr, bytes.length, true);
1054
- return this.scriptBufferPtr;
1055
- }, "iii");
1056
- this._setScriptReader(this.scriptReaderFunc);
1135
+ this.scriptReaders.push(reader);
1136
+ return this;
1057
1137
  }
1058
- setCardReader(reader) {
1059
- if (this.cardReaderFunc) {
1060
- this.ocgcoreModule.removeFunction(this.cardReaderFunc);
1138
+ setCardReader(reader, reset = false) {
1139
+ if (reset) {
1140
+ this.cardReaders = [];
1061
1141
  }
1062
- this.cardReaderFunc = this.createFunction((cardId, cardDataPtr) => {
1063
- const data = reader(cardId);
1064
- if (!data) {
1065
- return 0;
1066
- }
1067
- const CardDataCtor = CardDataStruct;
1068
- let buf;
1069
- if (data instanceof CardDataCtor) {
1070
- buf = CardDataStruct.raw(data);
1071
- } else {
1072
- const cardData = new CardDataCtor();
1073
- cardData.code = data.code;
1074
- cardData.alias = data.alias;
1075
- const targetSetcode = cardData.setcode;
1076
- targetSetcode.fill(0);
1077
- if (data.setcode instanceof Uint16Array && data.setcode.length === 16) {
1078
- targetSetcode.set(data.setcode);
1079
- } else {
1080
- for (let i = 0; i < 16 && i < data.setcode.length; i++) {
1081
- targetSetcode[i] = data.setcode[i];
1082
- }
1083
- }
1084
- cardData.type = data.type;
1085
- cardData.level = data.level;
1086
- cardData.attribute = data.attribute;
1087
- cardData.race = data.race;
1088
- cardData.attack = data.attack;
1089
- cardData.defense = data.defense;
1090
- cardData.lscale = data.lscale;
1091
- cardData.rscale = data.rscale;
1092
- cardData.linkMarker = data.linkMarker;
1093
- buf = CardDataStruct.raw(cardData);
1094
- }
1095
- this.heapU8.set(buf, cardDataPtr);
1096
- return 0;
1097
- }, "iii");
1098
- this._setCardReader(this.cardReaderFunc);
1142
+ this.cardReaders.push(reader);
1143
+ return this;
1099
1144
  }
1100
- setMessageHandler(handler) {
1101
- if (!this.logBufferPtr) {
1102
- this.logBufferPtr = this.ocgcoreModule._malloc(this.logBufferSize);
1103
- }
1104
- if (this.messageHandlerFunc) {
1105
- this.ocgcoreModule.removeFunction(this.messageHandlerFunc);
1145
+ setMessageHandler(handler, reset = false) {
1146
+ if (reset) {
1147
+ this.messageHandlers = [];
1106
1148
  }
1107
- this.messageHandlerFunc = this.createFunction((duelPtr, messageType) => {
1108
- this.ocgcoreModule._get_log_message(duelPtr, this.logBufferPtr);
1109
- const message = this.getUTF8String(this.logBufferPtr);
1110
- const type = messageType === 1 ? "ScriptError" /* ScriptError */ : messageType === 2 ? "DebugMessage" /* DebugMessage */ : messageType;
1111
- handler(this.getOrCreateDuel(duelPtr), message, type);
1112
- }, "iii");
1113
- this._setMessageHandler(this.messageHandlerFunc);
1149
+ this.messageHandlers.push(handler);
1150
+ return this;
1114
1151
  }
1115
1152
  getOrCreateDuel(duelPtr) {
1116
1153
  const existing = this.duels.get(duelPtr);
@@ -1193,6 +1230,54 @@ async function createOcgcoreWrapper(options = {}) {
1193
1230
 
1194
1231
  // src/adapters/script-readers.ts
1195
1232
  import JSZip from "jszip";
1233
+
1234
+ // src/utility/node-fs.ts
1235
+ function getNodeFs(noThrow = false) {
1236
+ const req = typeof __require !== "undefined" && __require || Function('return typeof require !== "undefined" && require')();
1237
+ if (!req) {
1238
+ if (noThrow) {
1239
+ return null;
1240
+ }
1241
+ throw new Error("Node.js fs module is not available.");
1242
+ }
1243
+ try {
1244
+ return req("node:fs");
1245
+ } catch {
1246
+ try {
1247
+ return req("fs");
1248
+ } catch {
1249
+ if (noThrow) {
1250
+ return null;
1251
+ }
1252
+ throw new Error("Node.js fs module is not available.");
1253
+ }
1254
+ }
1255
+ }
1256
+
1257
+ // src/utility/node-path.ts
1258
+ function getNodePath(noThrow = false) {
1259
+ const req = typeof __require !== "undefined" && __require || Function('return typeof require !== "undefined" && require')();
1260
+ if (!req) {
1261
+ if (noThrow) {
1262
+ return null;
1263
+ }
1264
+ throw new Error("Node.js path module is not available.");
1265
+ }
1266
+ try {
1267
+ return req("node:path");
1268
+ } catch {
1269
+ try {
1270
+ return req("path");
1271
+ } catch {
1272
+ if (noThrow) {
1273
+ return null;
1274
+ }
1275
+ throw new Error("Node.js path module is not available.");
1276
+ }
1277
+ }
1278
+ }
1279
+
1280
+ // src/adapters/script-readers.ts
1196
1281
  var SCRIPT_PREFIX = "./script/";
1197
1282
  function normalizePath(input) {
1198
1283
  let path = input.replace(/\\/g, "/");
@@ -1218,13 +1303,47 @@ function buildCandidates(filename) {
1218
1303
  }
1219
1304
  return candidates;
1220
1305
  }
1221
- function MapReader(map) {
1306
+ function joinPath(baseDir, relativePath) {
1307
+ const pathMod = getNodePath();
1308
+ if (pathMod) {
1309
+ return pathMod.join(baseDir, relativePath);
1310
+ }
1311
+ const trimmedBase = baseDir.replace(/[/\\]+$/, "");
1312
+ const trimmedRel = relativePath.replace(/^[/\\]+/, "");
1313
+ return `${trimmedBase}/${trimmedRel}`;
1314
+ }
1315
+ function MapReader(...maps) {
1222
1316
  return (path) => {
1223
1317
  const filename = normalizePath(path);
1318
+ if (!filename.toLowerCase().endsWith(".lua")) {
1319
+ return null;
1320
+ }
1224
1321
  const candidates = buildCandidates(filename);
1225
1322
  for (const candidate of candidates) {
1226
- if (map.has(candidate)) {
1227
- return map.get(candidate) ?? null;
1323
+ for (const map of maps) {
1324
+ if (map.has(candidate)) {
1325
+ return map.get(candidate) ?? null;
1326
+ }
1327
+ }
1328
+ }
1329
+ return null;
1330
+ };
1331
+ }
1332
+ function DirReader(...baseDirs) {
1333
+ const fs = getNodeFs();
1334
+ return (path) => {
1335
+ const filename = normalizePath(path);
1336
+ if (!filename.toLowerCase().endsWith(".lua")) {
1337
+ return null;
1338
+ }
1339
+ const candidates = buildCandidates(filename);
1340
+ for (const baseDir of baseDirs) {
1341
+ for (const candidate of candidates) {
1342
+ const normalized = candidate.startsWith("/") ? candidate.slice(1) : candidate;
1343
+ const fullPath = joinPath(baseDir, normalized);
1344
+ if (fs.existsSync(fullPath)) {
1345
+ return fs.readFileSync(fullPath);
1346
+ }
1228
1347
  }
1229
1348
  }
1230
1349
  return null;
@@ -1239,25 +1358,30 @@ function normalizeZipEntryName(name) {
1239
1358
  }
1240
1359
  return Array.from(names);
1241
1360
  }
1242
- async function ZipReader(data) {
1243
- const zip = await JSZip.loadAsync(data);
1244
- const map = /* @__PURE__ */ new Map();
1245
- const entries = Object.values(zip.files);
1246
- await Promise.all(
1247
- entries.map(async (entry) => {
1248
- if (entry.dir) {
1249
- return;
1250
- }
1251
- if (!entry.name.toLowerCase().endsWith(".lua")) {
1252
- return;
1253
- }
1254
- const content = await entry.async("uint8array");
1255
- for (const name of normalizeZipEntryName(entry.name)) {
1256
- map.set(name, content);
1257
- }
1361
+ async function ZipReader(...inputs) {
1362
+ const maps = await Promise.all(
1363
+ inputs.map(async (data) => {
1364
+ const zip = await JSZip.loadAsync(data);
1365
+ const map = /* @__PURE__ */ new Map();
1366
+ const entries = Object.values(zip.files);
1367
+ await Promise.all(
1368
+ entries.map(async (entry) => {
1369
+ if (entry.dir) {
1370
+ return;
1371
+ }
1372
+ if (!entry.name.toLowerCase().endsWith(".lua")) {
1373
+ return;
1374
+ }
1375
+ const content = await entry.async("uint8array");
1376
+ for (const name of normalizeZipEntryName(entry.name)) {
1377
+ map.set(name, content);
1378
+ }
1379
+ })
1380
+ );
1381
+ return map;
1258
1382
  })
1259
1383
  );
1260
- return MapReader(map);
1384
+ return MapReader(...maps);
1261
1385
  }
1262
1386
 
1263
1387
  // src/vendor/script-constants.ts
@@ -2115,7 +2239,7 @@ function toUint16ArrayFromSetcode(value) {
2115
2239
  }
2116
2240
  function mapRowToCardData(row) {
2117
2241
  const type = (row.type ?? 0) >>> 0;
2118
- let attack = row.atk ?? 0;
2242
+ const attack = row.atk ?? 0;
2119
2243
  let defense = row.def ?? 0;
2120
2244
  let linkMarker = 0;
2121
2245
  if ((type & OcgcoreCommonConstants.TYPE_LINK) >>> 0 !== 0) {
@@ -2307,18 +2431,6 @@ var playYrp = (ocgcoreWrapper, yrpInput) => {
2307
2431
  duel.startDuel(yrp.opt >>> 0);
2308
2432
  while (true) {
2309
2433
  const { raw, status } = duel.process();
2310
- console.log(
2311
- "Duel process status:",
2312
- status,
2313
- "Message length:",
2314
- raw.length,
2315
- "Message:",
2316
- raw[0],
2317
- "Message string:",
2318
- Object.entries(OcgcoreCommonConstants).find(
2319
- ([k, v]) => v === raw[0] && k.startsWith("MSG_")
2320
- )?.[0] ?? "Unknown"
2321
- );
2322
2434
  messages.push(raw);
2323
2435
  if (raw.length > 0 && raw[0] === OcgcoreCommonConstants.MSG_RETRY) {
2324
2436
  throw new Error("Got MSG_RETRY");
@@ -2347,10 +2459,11 @@ var playYrp = (ocgcoreWrapper, yrpInput) => {
2347
2459
 
2348
2460
  // index.ts
2349
2461
  if (typeof globalThis !== "undefined" && !globalThis.Buffer) {
2350
- globalThis.Buffer = Buffer;
2462
+ globalThis.Buffer = Buffer2;
2351
2463
  }
2352
2464
  export {
2353
2465
  CardDataStruct,
2466
+ DirReader,
2354
2467
  LEN_EMPTY,
2355
2468
  LEN_FAIL,
2356
2469
  LEN_HEADER,
@@ -2368,17 +2481,12 @@ export {
2368
2481
  ZipReader,
2369
2482
  createOcgcoreWrapper,
2370
2483
  createSqljsCardReader,
2371
- encodeUtf8,
2372
2484
  normalizeStartDuelOptions,
2373
2485
  parseCardQuery,
2374
2486
  parseFieldCardQuery,
2375
2487
  parseFieldInfo,
2376
2488
  parseRegistryDump,
2377
2489
  parseRegistryKeys,
2378
- playYrp,
2379
- readI32,
2380
- readU16,
2381
- readU32,
2382
- readU8
2490
+ playYrp
2383
2491
  };
2384
2492
  //# sourceMappingURL=index.mjs.map