diff --git a/Makefile b/Makefile index faa966f..5ed7dd9 100644 --- a/Makefile +++ b/Makefile @@ -1,32 +1,57 @@ BUNDLE_NAME = bundle.lua HAXE_NAME = haxe.lua MINIFYD_NAME = bundle.min.lua +HAXE_ZIP_NAME = "haxe.zip" +UNPACK_NAME = unpack.lua +UNPACK_POLYFILLED_NAME = unpack.polyfill.lua +UNPACK_MINIFYD_NAME = unpack.min.lua BUILD_DIR = build HAXE_FLAGS = POLYFILLED_NAME = bundle.polyfill.lua POLYFILL_SRC = src/polyfill.lua CREAFTOS_PATH = craftos +BUILD_HXML = build.hxml HAXE_PATH := $(BUILD_DIR)/$(HAXE_NAME) MIN_PATH := $(BUILD_DIR)/$(MINIFYD_NAME) POLYFILL_PATH := $(BUILD_DIR)/$(POLYFILLED_NAME) +UNPACK_PATH := $(BUILD_DIR)/$(UNPACK_NAME) +UNPACK_MINIFYD_PATH := $(BUILD_DIR)/$(UNPACK_MINIFYD_NAME) +HAXE_ZIP_PATH := $(BUILD_DIR)/$(HAXE_ZIP_NAME) +UNPACK_POLYFILLED_PATH := $(BUILD_DIR)/$(UNPACK_POLYFILLED_NAME) -all: clean build +all: clean build unpack -build: HAXE_FLAGS += --main kernel.Entrypoint -D analyzer-optimize +build: HAXE_FLAGS += -D analyzer-optimize build: $(MIN_PATH) debug: HAXE_FLAGS += -D webconsole -D error_stack --debug -debug: build +debug: $(MIN_PATH) +unpack: $(UNPACK_MINIFYD_PATH) $(HAXE_ZIP_PATH) + +$(HAXE_PATH): HAXE_FLAGS += --main kernel.Entrypoint --lua $(HAXE_PATH) $(HAXE_PATH): $(shell find src -name '*.hx') - haxe build.hxml $(HAXE_FLAGS) + haxe $(BUILD_HXML) $(HAXE_FLAGS) $(POLYFILL_PATH): $(POLYFILL_SRC) $(HAXE_PATH) cat $(POLYFILL_SRC) $(HAXE_PATH) > $@ $(MIN_PATH): $(POLYFILL_PATH) - node minify.js $(POLYFILL_PATH) $@ + node tools/minify.js < $(POLYFILL_PATH) > $@ + +$(HAXE_ZIP_PATH): $(MIN_PATH) + node tools/zlibDeflate.js < $(MIN_PATH) > $@ + +$(UNPACK_PATH): HAXE_FLAGS += --main Unpack -D analyzer-optimize --lua $(UNPACK_PATH) +$(UNPACK_PATH): $(shell find src -name '*.hx') + haxe $(BUILD_HXML) $(HAXE_FLAGS) + +$(UNPACK_POLYFILLED_PATH): $(UNPACK_PATH) + cat $(POLYFILL_SRC) $(UNPACK_PATH) > $@ + +$(UNPACK_MINIFYD_PATH): $(UNPACK_POLYFILLED_PATH) + node tools/minify.js < $(UNPACK_POLYFILLED_PATH) > $@ .PHONY: deps deps: deps-hx deps-node @@ -54,7 +79,7 @@ emulator: .PHONY: webconsole webconsole: - node console.js + node tools/console.js .PHONY: format format: diff --git a/build.hxml b/build.hxml index 7af058f..34c1dca 100644 --- a/build.hxml +++ b/build.hxml @@ -10,5 +10,3 @@ -D lua-ver 5.1 --macro include("bin") - ---lua build/haxe.lua diff --git a/minify.js b/minify.js deleted file mode 100644 index 8e7b032..0000000 --- a/minify.js +++ /dev/null @@ -1,10 +0,0 @@ -const fs = require("fs"); -const luamin = require("luamin"); - -const haxeOutput = fs.readFileSync(process.argv[2] ?? "build/Haxe.min.lua",{encoding:"utf8"}); - -const minified = luamin.minify(haxeOutput); - -fs.writeFileSync(process.argv[3] ?? "build/Haxe.min.lua",minified); - -console.log("minified lua"); diff --git a/src/Unpack.hx b/src/Unpack.hx new file mode 100644 index 0000000..9eda905 --- /dev/null +++ b/src/Unpack.hx @@ -0,0 +1,29 @@ +import lua.Lua; +import haxe.io.Bytes; +import kernel.fs.FS; +import haxe.zip.Uncompress; + +class Unpack { + public static function main() { + var filename = "/haxe.zip"; + var handle = FS.openReadBinary(filename); + var size = FS.attributes(filename).size; + var data = Bytes.alloc(size); + + for (i in 0...size) { + data.set(i, handle.readByte()); + } + + var uncompressed = Uncompress.run(data); + + var res = Lua.load(uncompressed.toString()); // FIXME: Haxe is missing some parameters. This does not work. + + var f = res.func; // Required for silly haxe bug. + + if (res.message == null) { + f(); + } else { + trace('Failed: ${res.message}'); + } + } +} diff --git a/console.js b/tools/console.js similarity index 100% rename from console.js rename to tools/console.js diff --git a/tools/minify.js b/tools/minify.js new file mode 100644 index 0000000..4308dda --- /dev/null +++ b/tools/minify.js @@ -0,0 +1,8 @@ +const fs = require("fs"); +const luamin = require("luamin"); + +const haxeOutput = fs.readFileSync(0, { encoding: "utf8" }); + +const minified = luamin.minify(haxeOutput); + +fs.writeFileSync(1, minified); diff --git a/tools/zlibDeflate.js b/tools/zlibDeflate.js new file mode 100644 index 0000000..ef75660 --- /dev/null +++ b/tools/zlibDeflate.js @@ -0,0 +1,5 @@ +const zlib = require('node:zlib'); + +let def = zlib.createDeflate(); + +process.stdin.pipe(def).pipe(process.stdout);