Skip to content

Commit f232006

Browse files
committed
Big maintenance update
1 parent a7cda14 commit f232006

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+7171
-33177
lines changed

.eslintrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"parser": "@typescript-eslint/parser",
3+
"plugins": ["@typescript-eslint"],
4+
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"]
5+
}

.gitignore

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
node_modules
2+
/dist
13
*.js
24
*.js.map
3-
!docs/convert/convertOut.js
4-
!docs/preview/previewOut.js
5-
!docs/decodeblp/decodeblpOut.js
6-
!docs/optframes/optframesOut.js
7-
!docs/shim.js
5+
!samples.webpack.config.js
86
!third_party/decoder.js
7+
!docs/*/dist/*.js

.npmignore

-5
This file was deleted.

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
save-exact=true

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017 4eb0da
3+
Copyright (c) 2017-2021 4eb0da
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+27-8
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,37 @@
22
TypeScript-based mdl/mdx (Warcraft 3 model formats) converter/renderer
33

44
## Demo
5-
* [MDL/MDX converter (also json-like structure-previewer)](https://4eb0da.github.io/war3-model/convert.html)
6-
* [WebGL model previewer](https://4eb0da.github.io/war3-model/preview.html)
7-
* [BLP previewer (BLP1 decoder only)](https://4eb0da.github.io/war3-model/decodeblp.html)
5+
* [MDL/MDX converter (also json-like structure-previewer)](https://4eb0da.github.io/war3-model/convert/convert.html)
6+
* [WebGL model previewer](https://4eb0da.github.io/war3-model/preview/preview.html)
7+
* [BLP previewer (BLP1 decoder only)](https://4eb0da.github.io/war3-model/decodeblp/decodeblp.html)
8+
* [Simple model optimizer](https://4eb0da.github.io/war3-model/optframes/optframes.html)
89

910
## Usage
11+
12+
* [Usage in Node.js or with bundler (e.g. webpack)](docs/node.md)
13+
* [Usage in browser as external script](docs/browser-global.md)
14+
* [Usage in browser as ES Module directly](docs/browser-es.md)
15+
* [Exported APIs](docs/interface.md)
16+
* [How to render model in browser](docs/how-to-render.md)
17+
1018
```bash
1119
npm i war3-model --save
1220
```
1321

1422
MDL parsing/generation
1523
```typescript
16-
import {parse as parseMDL} from 'war3-model/mdl/parse';
17-
import {generate as generateMDL} from 'war3-model/mdl/generate';
24+
import { parseMDL, generateMDL } from 'war3-model';
1825

1926
let model = parseMDL('...');
2027
let mdl = generateMDL(model);
2128
console.log(mdl);
2229
```
2330

24-
BLP => PNG node.js converter
31+
BLP => PNG node.js cli converter
2532
```typescript
2633
import * as fs from 'fs';
27-
import {PNG} from 'pngjs';
28-
import {decode, getImageData} from 'war3-model/blp/decode';
34+
import { PNG } from 'pngjs';
35+
import { decodeBLP, getBLPImageData } from 'war3-model';
2936

3037
let blp = decode(new Uint8Array(fs.readFileSync(process.argv[2])).buffer);
3138
let imageData = getImageData(blp, 0);
@@ -36,6 +43,18 @@ png.data = Buffer.from(imageData.data.buffer);
3643
fs.writeFileSync('out.png', PNG.sync.write(png));
3744
```
3845

46+
## Is it good enough?
47+
48+
100% of old classic Warcraft 3 models can be parsed.
49+
50+
After conversion `mdx binary file` -> `in-memory structure` -> `mdx binary file` all of them would be byte-to-byte identical.
51+
52+
~98.4% of models (3276/3329) would be identical after `mdx` -> `structure` -> `mdl` -> `structure` -> `mdx` (because warcraft contains extraneous data in models and because of unsupported multiple texture chunks in mdl).
53+
54+
## What about Reforged?
55+
56+
Nope. Currently not supported, sorry
57+
3958
## MDL/MDX support
4059
* All standart features like Sequences, Bones, Cameras, etc
4160
* Multiple texture chunks (mdx only)

blp/decode.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as decodeJPEG from '../third_party/decoder';
1+
import decodeJPEG from '../third_party/decoder';
22
import {BLPImage, BLPContent, BLPType} from './blpimage';
33

44
function keyword (view: DataView, offset: number): string {
@@ -16,7 +16,7 @@ function uint32 (view: DataView, offset: number): number {
1616

1717
function bitVal (data: Uint8Array, bitCount: number, index: number): number {
1818
// only 1, 4 or 8 bits
19-
let byte = data[Math.floor(index * bitCount / 8)],
19+
const byte = data[Math.floor(index * bitCount / 8)],
2020
valsPerByte = 8 / bitCount;
2121

2222
return (byte >> (valsPerByte - index % valsPerByte - 1)) & ((1 << bitCount) - 1);
@@ -42,9 +42,9 @@ function createImageData (width: number, height: number): ImageDataLike {
4242
}
4343

4444
export function decode (arrayBuffer: ArrayBuffer): BLPImage {
45-
let view = new DataView(arrayBuffer);
45+
const view = new DataView(arrayBuffer);
4646

47-
let image: BLPImage = {
47+
const image: BLPImage = {
4848
type: BLPType.BLP1,
4949
width: 0,
5050
height: 0,
@@ -54,7 +54,7 @@ export function decode (arrayBuffer: ArrayBuffer): BLPImage {
5454
data: arrayBuffer,
5555
};
5656

57-
let type = keyword(view, 0);
57+
const type = keyword(view, 0);
5858

5959
if (type === 'BLP0' || type === 'BLP2') {
6060
throw new Error('BLP0/BLP2 not supported');
@@ -74,7 +74,7 @@ export function decode (arrayBuffer: ArrayBuffer): BLPImage {
7474
image.height = uint32(view, 4);
7575

7676
for (let i = 0; i < 16; ++i) {
77-
let mipmap = {
77+
const mipmap = {
7878
offset: uint32(view, 7 + i),
7979
size: uint32(view, 7 + 16 + i)
8080
};
@@ -90,20 +90,20 @@ export function decode (arrayBuffer: ArrayBuffer): BLPImage {
9090
}
9191

9292
export function getImageData (blp: BLPImage, mipmapLevel: number): ImageDataLike {
93-
let view = new DataView(blp.data),
93+
const view = new DataView(blp.data),
9494
uint8Data = new Uint8Array(blp.data),
9595
mipmap = blp.mipmaps[mipmapLevel];
9696

9797
if (blp.content === BLPContent.JPEG) {
98-
let headerSize = uint32(view, 39),
98+
const headerSize = uint32(view, 39),
9999
data = new Uint8Array(headerSize + mipmap.size);
100100

101101
data.set(uint8Data.subarray(40 * 4, 40 * 4 + headerSize));
102102
data.set(uint8Data.subarray(mipmap.offset, mipmap.offset + mipmap.size), headerSize);
103103

104104
return decodeJPEG(data);
105105
} else {
106-
let palette = new Uint8Array(blp.data, 39 * 4, 256 * 4),
106+
const palette = new Uint8Array(blp.data, 39 * 4, 256 * 4),
107107
width = blp.width / (1 << mipmapLevel),
108108
height = blp.height / (1 << mipmapLevel),
109109
size = width * height,
@@ -112,7 +112,7 @@ export function getImageData (blp: BLPImage, mipmapLevel: number): ImageDataLike
112112
valPerAlphaBit = 255 / ((1 << blp.alphaBits) - 1);
113113

114114
for (let i = 0; i < size; ++i) {
115-
let paletteIndex = view.getUint8(mipmap.offset + i) * 4;
115+
const paletteIndex = view.getUint8(mipmap.offset + i) * 4;
116116
// BGRA order
117117
imageData.data[i * 4] = palette[paletteIndex + 2];
118118
imageData.data[i * 4 + 1] = palette[paletteIndex + 1];

browser.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as model from './model';
2+
import {parse as parseMDL} from './mdl/parse';
3+
import {parse as parseMDX} from './mdx/parse';
4+
import {generate as generateMDL} from './mdl/generate';
5+
import {generate as generateMDX} from './mdx/generate';
6+
import * as blp from './blp/blpimage';
7+
import {decode as decodeBLP, getImageData as getBLPImageData} from './blp/decode';
8+
import {ModelRenderer} from './renderer/modelRenderer';
9+
10+
const war3model = {
11+
model,
12+
parseMDX,
13+
generateMDX,
14+
parseMDL,
15+
generateMDL,
16+
blp,
17+
decodeBLP,
18+
getBLPImageData,
19+
ModelRenderer
20+
};
21+
22+
declare global {
23+
interface Window {
24+
war3model: typeof war3model;
25+
}
26+
}
27+
28+
window.war3model = war3model;

docs/browser-es.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Usage in browser as ES Module directly
2+
3+
```html
4+
<script type="module">
5+
import { parseMDL } from './node_modules/war3-model/dist/es/war3-model.browser.js';
6+
7+
console.log(parseMDL('...'));
8+
</script>
9+
```

docs/browser-global.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Usage in browser as external script
2+
3+
```html
4+
<script src="./node_modules/war3-model/dist/war3-model.browser.js"></script>
5+
<script>
6+
console.log(war3model.parseMDL('...'));
7+
</script>
8+
```
9+
10+
Also there is typings for browser:
11+
12+
```json
13+
// tsconfig.json
14+
{
15+
"include": [
16+
"node_modules/war3-model/dist/war3-model.browser.d.ts",
17+
...
18+
]
19+
}
20+
```

docs/convert/codemirror-config.d.ts

-9
This file was deleted.

docs/convert/convert.css

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
html, body {
2+
height: 100%;
3+
}
4+
15
body {
6+
display: flex;
7+
flex-direction: column;
28
margin: 0;
39
font: 16px sans-serif;
410
}
@@ -7,9 +13,9 @@ body {
713
position: relative;
814
display: flex;
915
flex-direction: column;
10-
max-width: 600px;
11-
min-height: 400px;
12-
margin: 20px auto auto;
16+
flex: 1 1 auto;
17+
min-height: 300px;
18+
margin: 20px;
1319
padding: 20px;
1420
border: 3px dashed #aaa;
1521
border-radius: 10px;
@@ -31,10 +37,17 @@ body {
3137
font-size: 120%;
3238
}
3339

34-
.textarea, .CodeMirror {
40+
.editor {
3541
display: none;
3642
width: 100%;
3743
flex: 1 1 auto;
44+
flex-direction: column;
45+
min-height: 0;
46+
}
47+
48+
.editor-elem {
49+
flex: 1 1 auto;
50+
min-height: 0;
3851
}
3952

4053
.save {
@@ -57,8 +70,10 @@ body {
5770
display: none;
5871
}
5972

60-
.container_with-data .textarea,
61-
.container_with-data .CodeMirror,
73+
.container_with-data .editor {
74+
display: flex;
75+
}
76+
6277
.container_with-data .save {
6378
display: block;
6479
}

docs/convert.html renamed to docs/convert/convert.html

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport"
6-
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
6+
content="width=device-width">
77
<meta http-equiv="X-UA-Compatible" content="ie=edge">
88
<title>MDL/MDX Converter</title>
9-
<link rel="stylesheet" type="text/css" href="convert/convertOut.css">
9+
<link rel="stylesheet" type="text/css" href="convert.css">
1010
</head>
1111
<body>
1212
<div class="container" dropzone="copy">
1313
<div class="label">Drop files here</div>
14-
<textarea class="textarea"></textarea>
14+
<div class="editor">
15+
<div class="editor-elem"></div>
16+
</div>
1517
<button class="save">Save</button>
1618
</div>
17-
<script src="convert/convertOut.js"></script>
19+
<script src="dist/main.js"></script>
1820
</body>
1921
</html>

0 commit comments

Comments
 (0)