Skip to content

Commit 499bbe4

Browse files
authored
Merge pull request #1263 from melonjs/chore/add-tiled-inflate-plugin
Add tiled-inflate-plugin to monorepo
2 parents ddcb839 + 6230d2b commit 499bbe4

11 files changed

Lines changed: 532 additions & 1 deletion

File tree

.github/workflows/publish.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ on:
1010
options:
1111
- melonjs
1212
- debug-plugin
13+
- tiled-inflate-plugin
1314
dry-run:
1415
description: "Dry run (skip actual publish)"
1516
required: false
@@ -56,6 +57,9 @@ jobs:
5657
elif [ "${{ inputs.package }}" = "debug-plugin" ]; then
5758
echo "dir=packages/debug-plugin" >> "$GITHUB_OUTPUT"
5859
echo "build_cmd=pnpm clean && pnpm build" >> "$GITHUB_OUTPUT"
60+
elif [ "${{ inputs.package }}" = "tiled-inflate-plugin" ]; then
61+
echo "dir=packages/tiled-inflate-plugin" >> "$GITHUB_OUTPUT"
62+
echo "build_cmd=pnpm clean && pnpm build" >> "$GITHUB_OUTPUT"
5963
fi
6064
6165
- name: Build & test

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"publish-melonjs": "gh workflow run publish.yml -f package=melonjs",
1515
"publish-melonjs:dry": "gh workflow run publish.yml -f package=melonjs -f dry-run=true",
1616
"publish-debug-plugin": "gh workflow run publish.yml -f package=debug-plugin",
17-
"publish-debug-plugin:dry": "gh workflow run publish.yml -f package=debug-plugin -f dry-run=true"
17+
"publish-debug-plugin:dry": "gh workflow run publish.yml -f package=debug-plugin -f dry-run=true",
18+
"publish-tiled-inflate-plugin": "gh workflow run publish.yml -f package=tiled-inflate-plugin",
19+
"publish-tiled-inflate-plugin:dry": "gh workflow run publish.yml -f package=tiled-inflate-plugin -f dry-run=true"
1820
},
1921
"packageManager": "pnpm@9.5.0",
2022
"dependencies": {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (C) 2011 - 2023 Olivier Biot (AltByte Pte Ltd)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# @melonjs/tiled-inflate-plugin
2+
3+
A [melonJS](https://github.com/melonjs/melonJS) plugin to enable loading and parsing of compressed [Tiled](https://www.mapeditor.org/) maps.
4+
5+
Supports **gzip**, **zlib**, and **zstd** compressed tile layer data.
6+
7+
## Installation
8+
9+
```bash
10+
npm install @melonjs/tiled-inflate-plugin
11+
# or
12+
pnpm add @melonjs/tiled-inflate-plugin
13+
```
14+
15+
## Usage
16+
17+
```javascript
18+
import { plugin } from "melonjs";
19+
import { TiledInflatePlugin } from "@melonjs/tiled-inflate-plugin";
20+
21+
// register the plugin before loading any compressed Tiled maps
22+
plugin.register(TiledInflatePlugin);
23+
```
24+
25+
Once registered, melonJS will automatically decompress compressed tile layer data when loading Tiled maps.
26+
27+
## Supported Compression Formats
28+
29+
| Format | Supported | Library |
30+
|--------|-----------|---------|
31+
| gzip | Yes | [pako](https://github.com/nodeca/pako) |
32+
| zlib | Yes | [pako](https://github.com/nodeca/pako) |
33+
| zstd | Yes | [fzstd](https://github.com/101arrowz/fzstd) |
34+
35+
## License
36+
37+
[MIT](LICENSE)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"name": "@melonjs/tiled-inflate-plugin",
3+
"version": "1.2.0",
4+
"description": "a melonJS plugin to enable loading and parsing of compressed Tiled maps",
5+
"homepage": "https://github.com/melonjs/melonJS/tree/master/packages/tiled-inflate-plugin#readme",
6+
"type": "module",
7+
"keywords": [
8+
"2D",
9+
"HTML5",
10+
"javascript",
11+
"TypeScript",
12+
"es6",
13+
"melonjs",
14+
"Tiled",
15+
"plugin",
16+
"gzip",
17+
"zlib"
18+
],
19+
"repository": {
20+
"type": "git",
21+
"url": "git+https://github.com/melonjs/melonJS.git",
22+
"directory": "packages/tiled-inflate-plugin"
23+
},
24+
"bugs": {
25+
"url": "https://github.com/melonjs/melonJS/issues"
26+
},
27+
"license": "MIT",
28+
"author": "Olivier Biot (AltByte Pte Ltd)",
29+
"funding": "https://github.com/sponsors/melonjs",
30+
"engines": {
31+
"node": ">= 19"
32+
},
33+
"types": "./build/index.d.ts",
34+
"exports": {
35+
".": "./build/index.js"
36+
},
37+
"files": [
38+
"build/",
39+
"package.json",
40+
"README.md",
41+
"LICENSE"
42+
],
43+
"peerDependencies": {
44+
"melonjs": ">=15.2.1"
45+
},
46+
"dependencies": {
47+
"fzstd": "^0.1.1",
48+
"js-base64": "^3.7.7",
49+
"pako": "^2.1.0"
50+
},
51+
"devDependencies": {
52+
"esbuild": "^0.27.4",
53+
"melonjs": "workspace:*",
54+
"tsconfig": "workspace:*",
55+
"tsx": "^4.21.0",
56+
"typescript": "^5.9.3"
57+
},
58+
"scripts": {
59+
"build": "tsx scripts/build.ts && pnpm types",
60+
"prepublishOnly": "pnpm clean && pnpm build",
61+
"clean": "tsx scripts/clean.ts",
62+
"types": "tsc --project tsconfig.build.json"
63+
}
64+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import esbuild, { type BuildOptions } from "esbuild";
2+
import packageJson from "../package.json" with { type: "json" };
3+
4+
const banner = [
5+
"/*!",
6+
` * ${packageJson.description} - ${packageJson.version}`,
7+
" * http://www.melonjs.org",
8+
` * ${packageJson.name} is licensed under the MIT License.`,
9+
" * http://www.opensource.org/licenses/mit-license",
10+
` * @copyright (C) 2011 - ${new Date().getFullYear()} ${packageJson.author}`,
11+
" */",
12+
].join("\n");
13+
14+
const buildOptions = {
15+
entryPoints: ["src/index.js"],
16+
external: ["melonjs"],
17+
splitting: true,
18+
format: "esm",
19+
outdir: "build",
20+
sourcemap: true,
21+
bundle: true,
22+
define: { __VERSION__: JSON.stringify(packageJson.version) },
23+
banner: {
24+
js: banner,
25+
},
26+
} satisfies BuildOptions;
27+
28+
await esbuild.build(buildOptions);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { rm } from "node:fs/promises";
2+
3+
await rm("build", { recursive: true, force: true });
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { decompress as zstdDecompress } from "fzstd";
2+
import { Base64 } from "js-base64";
3+
import { plugin, TMXUtils } from "melonjs";
4+
import pako from "pako";
5+
6+
/**
7+
* @classdesc
8+
* a melonJS plugin to enable loading and parsing of compressed Tiled maps.
9+
* Supports gzip, zlib, and zstd compressed tile layer data.
10+
* @augments plugin.BasePlugin
11+
* @example
12+
* import { TiledInflatePlugin } from "@melonjs/tiled-inflate-plugin";
13+
* import { plugin as mePlugin } from "melonjs";
14+
*
15+
* // register the plugin
16+
* mePlugin.register(TiledInflatePlugin);
17+
*/
18+
export class TiledInflatePlugin extends plugin.BasePlugin {
19+
constructor() {
20+
// call the super constructor
21+
super();
22+
23+
// minimum melonJS version expected to run this plugin
24+
this.version = "15.2.1";
25+
26+
/**
27+
* decompress and decode zlib/gzip/zstd data
28+
* @param {string} input - base64 encoded and compressed data
29+
* @param {string} format - compressed data format ("gzip", "zlib", "zstd")
30+
* @returns {Uint32Array} decoded and decompressed data
31+
*/
32+
TMXUtils.setInflateFunction((data, format) => {
33+
let output;
34+
switch (format) {
35+
case "gzip":
36+
case "zlib":
37+
output = pako.inflate(Base64.toUint8Array(data));
38+
break;
39+
case "zstd":
40+
output = zstdDecompress(Base64.toUint8Array(data));
41+
break;
42+
default:
43+
throw new Error(`${format} compressed TMX Tile Map not supported!`);
44+
}
45+
return new Uint32Array(
46+
output.buffer,
47+
output.byteOffset,
48+
output.byteLength / 4,
49+
);
50+
});
51+
}
52+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "tsconfig/base.json",
3+
"compilerOptions": {
4+
"declarationDir": "build",
5+
"removeComments": false,
6+
"declarationMap": true,
7+
"declaration": true,
8+
"emitDeclarationOnly": true,
9+
"skipLibCheck": true
10+
},
11+
"include": ["src"]
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "tsconfig/base.json",
3+
"compilerOptions": {
4+
"noEmit": true
5+
},
6+
"include": ["src", "scripts"]
7+
}

0 commit comments

Comments
 (0)