koishipro-core.js 1.1.5 → 1.2.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.
@@ -1,156 +0,0 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
-
4
- const ROOT = path.resolve(__dirname, '..');
5
- const COMMON_H = '/home/nanahira/ygo/ygopro/ocgcore/common.h';
6
- const SCRIPT_CONSTANT_LUA = '/home/nanahira/ygo/ygopro/script/constant.lua';
7
- const OUT_OCORE = path.join(ROOT, 'src', 'vendor', 'ocgcore-constants.ts');
8
- const OUT_SCRIPT = path.join(ROOT, 'src', 'vendor', 'script-constants.ts');
9
-
10
- function readFileSafe(file) {
11
- try {
12
- return fs.readFileSync(file, 'utf8');
13
- } catch (err) {
14
- console.error(`[gen-constants] Failed to read: ${file}`);
15
- throw err;
16
- }
17
- }
18
-
19
- function parseNumber(raw) {
20
- const cleaned = raw.replace(/\s+/g, '');
21
- if (/^0x[0-9a-fA-F]+$/.test(cleaned)) return Number.parseInt(cleaned, 16);
22
- if (/^\d+$/.test(cleaned)) return Number.parseInt(cleaned, 10);
23
- return null;
24
- }
25
-
26
- function sanitizeExpr(expr) {
27
- return expr
28
- .replace(/\/\/.*$/g, '')
29
- .replace(/\/\*.*?\*\//g, '')
30
- .replace(/\bU\b/g, '')
31
- .replace(/\bUL\b/g, '')
32
- .replace(/\bULL\b/g, '')
33
- .replace(/\bL\b/g, '')
34
- .replace(/\s+/g, '');
35
- }
36
-
37
- function evalExpr(expr, constants) {
38
- const clean = sanitizeExpr(expr);
39
- const tokens = clean
40
- .replace(/<<|>>/g, (m) => ` ${m} `)
41
- .split(/\s+/)
42
- .filter(Boolean)
43
- .flatMap((token) => token.split(/([+\-*/()|&~^<>])/).filter(Boolean));
44
-
45
- const values = tokens.map((t) => {
46
- if (
47
- t === '+' ||
48
- t === '-' ||
49
- t === '*' ||
50
- t === '/' ||
51
- t === '(' ||
52
- t === ')' ||
53
- t === '|' ||
54
- t === '&' ||
55
- t === '~' ||
56
- t === '^' ||
57
- t === '<' ||
58
- t === '>' ||
59
- t === '<<' ||
60
- t === '>>'
61
- ) {
62
- return t;
63
- }
64
- const num = parseNumber(t);
65
- if (num !== null) return num;
66
- if (Object.prototype.hasOwnProperty.call(constants, t)) return constants[t];
67
- return NaN;
68
- });
69
-
70
- if (values.some((v) => Number.isNaN(v))) {
71
- return null;
72
- }
73
-
74
- const exprStr = values.join('');
75
- // eslint-disable-next-line no-new-func
76
- return Function(`"use strict";return (${exprStr});`)();
77
- }
78
-
79
- function parseCDefines(source) {
80
- const lines = source.split(/\r?\n/);
81
- const constants = {};
82
-
83
- for (const line of lines) {
84
- const trimmed = line.trim();
85
- if (!trimmed.startsWith('#define ')) continue;
86
- const content = trimmed.slice('#define '.length).trim();
87
- if (!content) continue;
88
- const parts = content.split(/\s+/);
89
- const name = parts.shift();
90
- if (!name) continue;
91
- if (name.includes('(')) continue; // macro with args
92
- const valueRaw = parts.join(' ');
93
- if (!valueRaw) continue;
94
- const value = evalExpr(valueRaw, constants);
95
- if (typeof value === 'number' && Number.isFinite(value)) {
96
- constants[name] = value;
97
- }
98
- }
99
-
100
- return constants;
101
- }
102
-
103
- function parseLuaConstants(source) {
104
- const lines = source.split(/\r?\n/);
105
- const constants = {};
106
-
107
- for (const line of lines) {
108
- const trimmed = line.trim();
109
- if (!trimmed || trimmed.startsWith('--')) continue;
110
- const match = /^([A-Z0-9_]+)\s*=\s*([^\-]+?)(?:\s+--.*)?$/.exec(trimmed);
111
- if (!match) continue;
112
- const name = match[1];
113
- const valueRaw = match[2].trim();
114
- const value = evalExpr(valueRaw, constants);
115
- if (typeof value === 'number' && Number.isFinite(value)) {
116
- constants[name] = value;
117
- }
118
- }
119
-
120
- return constants;
121
- }
122
-
123
- function emitConstantsTs(objectName, constants, sourcePath) {
124
- const keys = Object.keys(constants).sort();
125
- const lines = [];
126
- lines.push(`// Generated from ${sourcePath}`);
127
- lines.push(`export const ${objectName} = {`);
128
- for (const key of keys) {
129
- const value = constants[key];
130
- lines.push(` ${key}: ${value},`);
131
- }
132
- lines.push(`} as const;`);
133
- lines.push('');
134
- return lines.join('\n');
135
- }
136
-
137
- function writeFile(file, content) {
138
- fs.mkdirSync(path.dirname(file), { recursive: true });
139
- fs.writeFileSync(file, content, 'utf8');
140
- }
141
-
142
- function main() {
143
- const commonSource = readFileSafe(COMMON_H);
144
- const luaSource = readFileSafe(SCRIPT_CONSTANT_LUA);
145
-
146
- const ocgcoreConstants = parseCDefines(commonSource);
147
- const scriptConstants = parseLuaConstants(luaSource);
148
-
149
- writeFile(OUT_OCORE, emitConstantsTs('OcgcoreCommonConstants', ocgcoreConstants, COMMON_H));
150
- writeFile(OUT_SCRIPT, emitConstantsTs('OcgcoreScriptConstants', scriptConstants, SCRIPT_CONSTANT_LUA));
151
-
152
- console.log(`[gen-constants] Wrote ${Object.keys(ocgcoreConstants).length} common.h constants`);
153
- console.log(`[gen-constants] Wrote ${Object.keys(scriptConstants).length} constant.lua constants`);
154
- }
155
-
156
- main();