-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmap-data-parser.html
More file actions
642 lines (563 loc) · 23 KB
/
map-data-parser.html
File metadata and controls
642 lines (563 loc) · 23 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tanki Online Map Data Parser</title>
<!-- 引入第一字体 Rubik 和第二字体 M PLUS 1p -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=M+PLUS+1p:wght@400;500;700&family=Rubik:wght@400;500;700&display=swap" rel="stylesheet">
<style>
:root {
--md-sys-color-background: #001926;
--md-sys-color-primary: #76FF33;
--md-sys-color-secondary: #BFD5FF;
--md-sys-color-on-primary: #001926;
--md-sys-color-surface: rgba(191, 213, 255, 0.06);
--md-sys-color-outline: rgba(191, 213, 255, 0.15);
}
* {
-webkit-tap-highlight-color: transparent;
box-sizing: border-box;
}
body {
font-family: 'Rubik', 'M PLUS 1p', sans-serif;
background: var(--md-sys-color-background);
color: var(--md-sys-color-secondary);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
padding: 24px;
-webkit-font-smoothing: antialiased;
}
.container {
background: var(--md-sys-color-surface);
padding: 48px 40px;
border-radius: 32px;
border: 1px solid var(--md-sys-color-outline);
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
text-align: center;
max-width: 560px;
width: 100%;
}
h2 {
margin-top: 0;
color: var(--md-sys-color-primary);
font-size: 2.25rem;
font-weight: 700;
letter-spacing: -0.02em;
margin-bottom: 16px;
}
p {
color: var(--md-sys-color-secondary);
font-size: 1rem;
margin-bottom: 32px;
line-height: 1.6;
opacity: 0.85;
}
/* 类型选择器 Segmented Button */
.type-selector {
display: flex;
background: rgba(191, 213, 255, 0.08);
border-radius: 100px;
padding: 6px;
margin-bottom: 32px;
position: relative;
}
.type-selector label {
flex: 1;
cursor: pointer;
position: relative;
}
.type-selector input[type="radio"] {
position: absolute;
opacity: 0;
width: 0;
height: 0;
margin: 0;
}
.type-selector span {
display: block;
text-align: center;
padding: 12px 16px;
border-radius: 100px;
font-weight: 600;
font-size: 0.95rem;
color: var(--md-sys-color-secondary);
transition: all 0.3s cubic-bezier(0.2, 0, 0, 1);
z-index: 2;
position: relative;
}
.type-selector input[type="radio"]:checked + span {
background: var(--md-sys-color-secondary);
color: var(--md-sys-color-background);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}
.type-selector span:hover {
background: rgba(191, 213, 255, 0.15);
}
.type-selector input[type="radio"]:checked + span:hover {
background: var(--md-sys-color-secondary);
}
input[type="file"] {
display: none;
}
.upload-btn {
background: var(--md-sys-color-primary);
color: var(--md-sys-color-on-primary);
padding: 18px 36px;
border-radius: 100px;
cursor: pointer;
font-weight: 700;
font-size: 1.125rem;
display: inline-flex;
align-items: center;
justify-content: center;
transition: all 0.25s cubic-bezier(0.2, 0, 0, 1);
border: none;
outline: none;
}
.upload-btn:hover {
transform: translateY(-2px);
box-shadow: 0 6px 16px rgba(118, 255, 51, 0.25);
filter: brightness(1.05);
}
.upload-btn:active {
transform: translateY(0);
box-shadow: 0 2px 8px rgba(118, 255, 51, 0.15);
}
#log {
margin-top: 32px;
color: var(--md-sys-color-secondary);
font-variant-numeric: tabular-nums;
white-space: pre-wrap;
text-align: left;
background: var(--md-sys-color-background);
padding: 24px;
border-radius: 24px;
max-height: 280px;
overflow-y: auto;
border: 1px solid var(--md-sys-color-outline);
font-size: 0.9rem;
line-height: 1.6;
}
#log::-webkit-scrollbar {
width: 8px;
}
#log::-webkit-scrollbar-track {
background: transparent;
}
#log::-webkit-scrollbar-thumb {
background: rgba(191, 213, 255, 0.2);
border-radius: 8px;
}
#log::-webkit-scrollbar-thumb:hover {
background: rgba(191, 213, 255, 0.4);
}
</style>
</head>
<body>
<div class="container">
<h2 id="ui-title">3D坦克地图数据解析</h2>
<p id="ui-desc">该工具会在浏览器本地读取并解压3D坦克的二进制数据文件。解析完成后会自动将其转为 JSON 文件供你下载查看。</p>
<!-- 类型选择器 -->
<div class="type-selector">
<label>
<input type="radio" name="fileType" value="map" checked>
<span>map.bin</span>
</label>
<label>
<input type="radio" name="fileType" value="lightmap">
<span>lightmapdata</span>
</label>
</div>
<label class="upload-btn" tabindex="0">
<span id="ui-btn">选择并解析 map.bin</span>
<input type="file" id="fileInput" accept=".bin">
</label>
<div id="log">等待操作...</div>
</div>
<script>
// 多语言配置
const i18n = {
zh: {
title: "3D坦克地图数据解析",
desc: "该工具会在浏览器本地读取并解压3D坦克的二进制地图数据文件。解析完成后会自动将其转为 JSON 文件供你下载查看。",
btnMap: "选择并解析 map.bin",
btnLightmap: "选择并解析 lightmapdata",
logWait: "等待操作...",
logStart: "[INFO] 开始读取文件: ",
logSize: "[INFO] 文件大小: ",
logProcMap: "[PROC] 正在进行 Zlib 解压并读取数据树...",
logProcLightmap: "[PROC] 正在读取并解析 Lightmap 数据...",
logLightmapSum: "[INFO] 光照贴图解析成功!\n - 光照贴图数量: {m}\n - 影响物体数: {o}\n - 平行光颜色: {l}\n - 环境光颜色: {a}",
logDone: "[DONE] 解析完成!准备生成 JSON 文件...",
logSuccess: "[SUCCESS] 下载已触发!请检查浏览器的下载记录。",
logFail: "[ERROR] 解析失败: "
},
en: {
title: "Tanki Online Map Data Parser",
desc: "This tool reads and decompresses Tanki Online's binary map data files locally. Once parsed, it automatically converts them into JSON files for you to download.",
btnMap: "Select and Parse map.bin",
btnLightmap: "Select and Parse lightmapdata",
logWait: "Waiting for action...",
logStart: "[INFO] Started reading file: ",
logSize: "[INFO] File size: ",
logProcMap: "[PROC] Decompressing Zlib and reading data tree...",
logProcLightmap: "[PROC] Reading and parsing lightmap data...",
logLightmapSum: "[INFO] Lightmap parsed successfully!\n - Lightmaps count: {m}\n - Affected objects: {o}\n - Light Color: {l}\n - Ambient Color: {a}",
logDone: "[DONE] Parsing completed! Generating JSON file...",
logSuccess: "[SUCCESS] Download triggered! Please check your browser's download history.",
logFail: "[ERROR] Parsing failed: "
}
};
// 自动检测并切换语言
const isZh = navigator.language.toLowerCase().startsWith('zh');
const lang = isZh ? 'zh' : 'en';
const t = i18n[lang];
document.documentElement.lang = isZh ? 'zh-CN' : 'en';
document.title = "Tanki Online Parser";
document.getElementById('log').innerText = t.logWait;
// 初始化固定的文字
document.getElementById('ui-title').innerText = t.title;
document.getElementById('ui-desc').innerText = t.desc;
// 更新操作按钮相关的 UI
function updateUI() {
const type = document.querySelector('input[name="fileType"]:checked').value;
if (type === 'map') {
document.getElementById('ui-btn').innerText = t.btnMap;
document.getElementById('fileInput').accept = '.bin';
} else {
document.getElementById('ui-btn').innerText = t.btnLightmap;
document.getElementById('fileInput').accept = '*/*';
}
}
updateUI(); // 初始化
// 监听单选按钮切换
document.querySelectorAll('input[name="fileType"]').forEach(radio => {
radio.addEventListener('change', () => {
updateUI();
document.getElementById('log').innerText = t.logWait;
});
});
// 日志追加函数
function logMsg(msg) {
const logEl = document.getElementById('log');
if (logEl.innerText === t.logWait) logEl.innerText = '';
else logEl.innerText += '\n';
logEl.innerText += msg;
logEl.scrollTop = logEl.scrollHeight;
}
// 核心读写器
class BinaryStream {
constructor(buffer) {
this.buffer = new Uint8Array(buffer);
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
this.offset = 0;
}
readUint8() { const v = this.view.getUint8(this.offset); this.offset += 1; return v; }
readUint16(le = false) { const v = this.view.getUint16(this.offset, le); this.offset += 2; return v; }
readUint32(le = false) { const v = this.view.getUint32(this.offset, le); this.offset += 4; return v; }
readInt32(le = false) { const v = this.view.getInt32(this.offset, le); this.offset += 4; return v; }
readFloat32(le = false) { const v = this.view.getFloat32(this.offset, le); this.offset += 4; return v; }
readFloat64(le = false) { const v = this.view.getFloat64(this.offset, le); this.offset += 8; return v; }
readBytes(len) { const v = this.buffer.subarray(this.offset, this.offset + len); this.offset += len; return v; }
readStringLength() {
const flags = this.readUint8();
if ((flags & 0b10000000) === 0) return flags & 0b01111111;
if ((flags & 0b01000000) === 0) return ((flags & 0b00111111) << 8) + this.readUint8();
return ((flags & 0b00111111) << 16) + this.readUint16(false);
}
readString() { return new TextDecoder().decode(this.readBytes(this.readStringLength())); }
}
async function decompressZlib(uint8array) {
const ds = new DecompressionStream("deflate");
const writer = ds.writable.getWriter();
writer.write(uint8array);
writer.close();
return new Uint8Array(await new Response(ds.readable).arrayBuffer());
}
async function unwrapPacket(stream) {
const flags = stream.readUint8();
const compressed = (flags & 0b01000000) > 0;
let len = 0;
if ((flags & 0b10000000) === 0) {
len = stream.readUint8() + ((flags & 0b00111111) << 8);
} else {
const b1 = stream.readUint8(), b2 = stream.readUint8(), b3 = stream.readUint8();
len = (b1 << 16) | (b2 << 8) | b3;
len += (flags & 0b00111111) * 16777216;
}
let data = stream.readBytes(len);
if (compressed) data = await decompressZlib(data);
return new BinaryStream(data);
}
/* =========================================
Map.bin 解析逻辑 (包含隐藏碰撞体积解锁)
========================================= */
async function parseMapBin(buffer) {
const stream = new BinaryStream(buffer);
const packet = await unwrapPacket(stream);
// 解析标志位屏蔽树
const optMask =[];
const flags = packet.readUint8();
if ((flags & 0b10000000) === 0) {
const intBits = flags << 3;
for (let i = 7; i >= 3; i--) optMask.push((intBits & (1 << i)) === 0);
const extCount = (flags & 0b01100000) >> 5;
const extBytes = packet.readBytes(extCount);
for (let i = 0; i < extBytes.length; i++) for (let b = 7; b >= 0; b--) optMask.push((extBytes[i] & (1 << b)) === 0);
} else {
let extCount = ((flags & 0b01000000) === 0) ? (flags & 0b00111111) : (((flags & 0b00111111) << 16) + packet.readUint16(false));
const extBytes = packet.readBytes(extCount);
for (let i = 0; i < extBytes.length; i++) for (let b = 7; b >= 0; b--) optMask.push((extBytes[i] & (1 << b)) === 0);
}
optMask.reverse();
const readV3 = () =>[packet.readFloat32(false), packet.readFloat32(false), packet.readFloat32(false)];
const result = {
atlases: {},
markers:[],
collisionData1: {},
collisionData2: {},
materials: {},
lightingGroups: [],
props:[]
};
// 1. 图集解析 (Atlases)
if (optMask.pop()) {
const atlasLen = packet.readStringLength();
for(let i=0; i<atlasLen; i++) {
const aHeight = packet.readInt32(false);
const aName = packet.readString();
const aUnknown = packet.readUint32(false);
const rects = {};
const rectLen = packet.readStringLength();
for(let j=0; j<rectLen; j++) {
const rHeight = packet.readUint32(false);
const rLib = packet.readString();
const rName = packet.readString();
const rWidth = packet.readUint32(false);
const rx = packet.readUint32(false);
const ry = packet.readUint32(false);
rects[`${rLib}_${rName}`] = { x: rx, y: ry, w: rWidth, h: rHeight };
}
const aWidth = packet.readUint32(false);
result.atlases[aName] = { width: aWidth, height: aHeight, aUnknown, rects };
}
}
// 2. 特殊标记/重生点 (Markers/Spawns)
if (optMask.pop()) {
const len = packet.readStringLength();
for(let i=0; i<len; i++) {
const id = packet.readUint32(false);
const name = packet.readString();
const pos = readV3();
const type = packet.readString();
result.markers.push({ id, name, pos, type });
}
}
// 3. 物理碰撞树解析 (Collisions)
const parseCollisions = () => {
const cols = { shapesType1: [], shapesType2: [], shapesType3:[] };
let len1 = packet.readStringLength();
for(let i=0; i<len1; i++) {
const data =[]; for(let j=0; j<9; j++) data.push(packet.readFloat32(false));
cols.shapesType1.push(data);
}
let len2 = packet.readStringLength();
for(let i=0; i<len2; i++) {
const f1 = packet.readFloat64(false);
const data =[]; for(let j=0; j<6; j++) data.push(packet.readFloat32(false));
const f2 = packet.readFloat64(false);
cols.shapesType2.push({ f1, data, f2 });
}
let len3 = packet.readStringLength();
for(let i=0; i<len3; i++) {
const f1 = packet.readFloat64(false);
const data =[]; for(let j=0; j<15; j++) data.push(packet.readFloat32(false));
cols.shapesType3.push({ f1, data });
}
return cols;
};
result.collisionData1 = parseCollisions();
result.collisionData2 = parseCollisions();
// 4. 材质深度解析 (Materials)
const matLen = packet.readStringLength();
for(let i=0; i<matLen; i++) {
const matID = packet.readUint32(false);
const matName = packet.readString();
const materialParams_1 =[];
if (optMask.pop()) {
const len = packet.readStringLength();
for(let j=0; j<len; j++) materialParams_1.push({ name: packet.readString(), val: packet.readFloat32(false) });
}
const shader = packet.readString();
const texParams =[];
const texLen = packet.readStringLength();
for(let j=0; j<texLen; j++) {
let libName = null;
if (optMask.pop()) libName = packet.readString();
const texParamName = packet.readString();
const texName = packet.readString();
texParams.push({ libName, name: texParamName, texName });
}
const materialParams_2 =[];
if (optMask.pop()) {
const len = packet.readStringLength();
for(let j=0; j<len; j++) materialParams_2.push({ name: packet.readString(), val:[packet.readFloat32(false), packet.readFloat32(false)] });
}
const materialParams_3 =[];
if (optMask.pop()) {
const len = packet.readStringLength();
for(let j=0; j<len; j++) materialParams_3.push({ name: packet.readString(), val: readV3() });
}
const materialParams_4 =[];
if (optMask.pop()) {
const len = packet.readStringLength();
for(let j=0; j<len; j++) materialParams_4.push({ name: packet.readString(), val:[packet.readFloat32(false), packet.readFloat32(false), packet.readFloat32(false), packet.readFloat32(false)] });
}
result.materials[matID] = {
name: matName, shader, texParams,
extraParams: { float1: materialParams_1, float2: materialParams_2, float3: materialParams_3, float4: materialParams_4 }
};
}
// 5. 光照组 / 附加属性
if (optMask.pop()) {
const len = packet.readStringLength();
for(let i=0; i<len; i++) {
const data =[]; for(let j=0; j<7; j++) data.push(packet.readFloat32(false));
result.lightingGroups.push(data);
}
}
// 6. 物体实例 (Props)
const propLen = packet.readStringLength();
for(let i=0; i<propLen; i++) {
let grpName = ""; if(optMask.pop()) grpName = packet.readString();
const id = packet.readUint32(false);
const libName = packet.readString();
const matID = packet.readUint32(false);
const name = packet.readString();
const pos = readV3();
const rot = optMask.pop() ? readV3() : [0,0,0];
const scale = optMask.pop() ? readV3() : [1,1,1];
result.props.push({ id, grpName, libName, matID, name, pos, rot, scale });
}
return result;
}
/* =========================================
LightmapData 解析逻辑 (修复 UV 错位 Bug)
========================================= */
function decodeIntColorToHex(intColor) {
const r = (intColor >> 16) & 255;
const g = (intColor >> 8) & 255;
const b = intColor & 255;
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
}
function parseLightmapData(buffer) {
const stream = new BinaryStream(buffer);
const version = stream.readUint32(true);
if (version !== 2) throw new Error(`Unsupported LightmapData version: ${version} (expected 2)`);
const lightColorInt = stream.readUint32(true);
const ambientColorInt = stream.readUint32(true);
const lightAngleX = stream.readFloat32(true);
const lightAngleZ = stream.readFloat32(true);
const lightmapCount = stream.readUint32(true);
const lightmaps =[];
for (let i = 0; i < lightmapCount; i++) lightmaps.push(stream.readString());
const mapObjectCount = stream.readUint32(true);
const mapObjects =[];
for (let i = 0; i < mapObjectCount; i++) {
const index = stream.readInt32(true);
const lightmapIndex = stream.readInt32(true);
let lightmapScaleOffset = null;
let uvs =[]; // 完美修复的 UV 读取循环
if (lightmapIndex >= 0) {
lightmapScaleOffset =[
stream.readFloat32(true), stream.readFloat32(true),
stream.readFloat32(true), stream.readFloat32(true)
];
const hasUVs = stream.readUint8();
if (hasUVs > 0) {
const vertexCount = stream.readUint32(true);
for (let v = 0; v < vertexCount; v++) {
uvs.push([stream.readFloat32(true), stream.readFloat32(true)]);
}
}
}
const castShadows = stream.readUint8() > 0;
const receiveShadows = stream.readUint8() > 0;
mapObjects.push({
index, lightmapIndex, lightmapScaleOffset, uvs, castShadows, receiveShadows
});
}
return {
version,
lightColor: decodeIntColorToHex(lightColorInt),
ambientColor: decodeIntColorToHex(ambientColorInt),
lightAngleX,
lightAngleZ,
lightmaps,
mapObjects
};
}
/* =========================================
事件绑定与主流程
========================================= */
const uploadLabel = document.querySelector('.upload-btn');
uploadLabel.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
document.getElementById('fileInput').click();
}
});
document.getElementById('fileInput').addEventListener('change', async (e) => {
const file = e.target.files[0];
if (!file) return;
const type = document.querySelector('input[name="fileType"]:checked').value;
document.getElementById('log').innerText = '';
logMsg(`${t.logStart}${file.name}`);
try {
const buffer = await file.arrayBuffer();
logMsg(`${t.logSize}${(buffer.byteLength / 1024).toFixed(2)} KB`);
let result;
let dlName = file.name;
if (type === 'map') {
logMsg(t.logProcMap);
result = await parseMapBin(buffer);
dlName = dlName.replace(/\.bin$/i, '.json');
} else {
logMsg(t.logProcLightmap);
result = parseLightmapData(buffer);
dlName = dlName.endsWith('.json') ? dlName : dlName + '.json';
const sumMsg = t.logLightmapSum
.replace('{m}', result.lightmaps.length)
.replace('{o}', result.mapObjects.length)
.replace('{l}', result.lightColor)
.replace('{a}', result.ambientColor);
logMsg(sumMsg);
}
logMsg(t.logDone);
const jsonString = JSON.stringify(result, null, 2);
const blob = new Blob([jsonString], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = dlName;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
logMsg(t.logSuccess);
} catch (err) {
logMsg(`${t.logFail}${err.message}`);
console.error(err);
}
e.target.value = '';
});
</script>
</body>
</html>