forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportFrontendOptimizedGraphics.ts
More file actions
184 lines (150 loc) · 5.55 KB
/
Copy pathexportFrontendOptimizedGraphics.ts
File metadata and controls
184 lines (150 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import fs from "node:fs";
import path from "node:path";
type SourceGraphicData = {
numFrames?: number | string;
numFile?: number | string;
sX?: number | string;
sY?: number | string;
width?: number | string;
height?: number | string;
frames?: Record<string, number | string>;
speed?: number | string;
offset?: {
x?: number | string;
y?: number | string;
};
};
type CompactSimpleGraphic = [numFile: number, sX: number, sY: number, width: number, height: number];
type CompactExtendedGraphic = {
f?: number;
n?: number;
x?: number;
y?: number;
w?: number;
h?: number;
r?: number[];
s?: number;
o?: [x: number, y: number];
};
type CompactGraphicsDB = Record<string, CompactSimpleGraphic | CompactExtendedGraphic>;
const DEFAULT_INPUT_PATH = path.resolve(__dirname, "../../../frontend/public/init/graficos.json");
const DEFAULT_OUTPUT_PATH = path.resolve(__dirname, "../../../frontend/public/init/graficos_optimized.json");
function resolveCliPath(value: string | undefined, fallback: string): string {
if (!value?.trim()) {
return fallback;
}
return path.resolve(process.cwd(), value);
}
function formatBytes(bytes: number): string {
return `${(bytes / 1024).toFixed(1)} KB`;
}
function toFiniteNumber(value: unknown): number | undefined {
if (typeof value === "number" && Number.isFinite(value)) {
return value;
}
if (typeof value === "string" && value.trim()) {
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : undefined;
}
return undefined;
}
function normalizeFrames(frames: SourceGraphicData["frames"]): number[] {
if (!frames) {
return [];
}
return Object.entries(frames)
.sort(([left], [right]) => Number(left) - Number(right))
.map(([, frameId]) => toFiniteNumber(frameId))
.filter((frameId): frameId is number => frameId !== undefined);
}
function compactGraphicEntry(
graphicId: string,
graphicData: SourceGraphicData,
): CompactSimpleGraphic | CompactExtendedGraphic {
const numericGraphicId = toFiniteNumber(graphicId);
const numFrames = toFiniteNumber(graphicData.numFrames);
const numFile = toFiniteNumber(graphicData.numFile);
const sX = toFiniteNumber(graphicData.sX);
const sY = toFiniteNumber(graphicData.sY);
const width = toFiniteNumber(graphicData.width);
const height = toFiniteNumber(graphicData.height);
const speed = toFiniteNumber(graphicData.speed);
const offsetX = toFiniteNumber(graphicData.offset?.x) ?? 0;
const offsetY = toFiniteNumber(graphicData.offset?.y) ?? 0;
const frames = normalizeFrames(graphicData.frames);
const inferredNumFrames = numFrames ?? frames.length;
const isDefaultSingleFrameReference =
inferredNumFrames === 1 &&
frames.length <= 1 &&
numericGraphicId !== undefined &&
(frames.length === 0 || frames[0] === numericGraphicId);
if (
inferredNumFrames === 1 &&
numFile !== undefined &&
sX !== undefined &&
sY !== undefined &&
width !== undefined &&
height !== undefined &&
speed === undefined &&
offsetX === 0 &&
offsetY === 0 &&
isDefaultSingleFrameReference
) {
return [numFile, sX, sY, width, height];
}
const compactGraphic: CompactExtendedGraphic = {};
if (inferredNumFrames > 1 || (inferredNumFrames === 1 && !isDefaultSingleFrameReference)) {
compactGraphic.f = inferredNumFrames;
}
if (numFile !== undefined) {
compactGraphic.n = numFile;
}
if (sX !== undefined) {
compactGraphic.x = sX;
}
if (sY !== undefined) {
compactGraphic.y = sY;
}
if (width !== undefined) {
compactGraphic.w = width;
}
if (height !== undefined) {
compactGraphic.h = height;
}
if (!isDefaultSingleFrameReference && graphicData.frames) {
compactGraphic.r = frames;
}
if (speed !== undefined) {
compactGraphic.s = speed;
}
if (offsetX !== 0 || offsetY !== 0) {
compactGraphic.o = [offsetX, offsetY];
}
return compactGraphic;
}
function compactGraphicsDb(graphicsDb: Record<string, SourceGraphicData>): CompactGraphicsDB {
const compactGraphicsDb: CompactGraphicsDB = {};
for (const [graphicId, graphicData] of Object.entries(graphicsDb)) {
compactGraphicsDb[graphicId] = compactGraphicEntry(graphicId, graphicData);
}
return compactGraphicsDb;
}
function main(): void {
const inputPath = resolveCliPath(process.argv[2], DEFAULT_INPUT_PATH);
const outputPath = resolveCliPath(process.argv[3], DEFAULT_OUTPUT_PATH);
const sourceRaw = fs.readFileSync(inputPath, "utf8");
const sourceGraphicsDb = JSON.parse(sourceRaw) as Record<string, SourceGraphicData>;
const compactGraphics = compactGraphicsDb(sourceGraphicsDb);
const outputRaw = `${JSON.stringify(compactGraphics)}\n`;
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
fs.writeFileSync(outputPath, outputRaw, "utf8");
const inputSize = Buffer.byteLength(sourceRaw, "utf8");
const outputSize = Buffer.byteLength(outputRaw, "utf8");
const savedBytes = inputSize - outputSize;
const savedPercent = inputSize > 0 ? (savedBytes / inputSize) * 100 : 0;
console.log(`Compacted graphics dataset to ${outputPath}`);
console.log(`Input: ${formatBytes(inputSize)}`);
console.log(`Output: ${formatBytes(outputSize)}`);
console.log(`Saved: ${formatBytes(savedBytes)} (${savedPercent.toFixed(2)}%)`);
}
main();