From d1c95742ddd83899bb35051de9d731d38aba80a4 Mon Sep 17 00:00:00 2001 From: E14 <1640391+E14@users.noreply.github.com> Date: Sun, 22 Sep 2019 22:57:54 +0200 Subject: Add ProtocolBlockTypePalette (#4391) --- Tools/BlockTypePaletteGenerator/Generator.lua | 91 +++++++++++++++++++++++++++ Tools/BlockTypePaletteGenerator/Readme.md | 63 +++++++++++++++++++ Tools/BlockTypePaletteGenerator/lib/lunajson | 1 + 3 files changed, 155 insertions(+) create mode 100644 Tools/BlockTypePaletteGenerator/Generator.lua create mode 100644 Tools/BlockTypePaletteGenerator/Readme.md create mode 160000 Tools/BlockTypePaletteGenerator/lib/lunajson (limited to 'Tools/BlockTypePaletteGenerator') diff --git a/Tools/BlockTypePaletteGenerator/Generator.lua b/Tools/BlockTypePaletteGenerator/Generator.lua new file mode 100644 index 000000000..f33f2b789 --- /dev/null +++ b/Tools/BlockTypePaletteGenerator/Generator.lua @@ -0,0 +1,91 @@ +-- lib/lunajson/src/ is not in default Lua package paths +package.path = 'lib/lunajson/src/?.lua;' .. package.path; + + +--- Prints usage instructions to stdout. +-- If the optional `message` is passed, output is prepended by message _and_ +-- redirected to stderr. +function usage(message) + if message then + io.output(io.stderr); + io.write(message, "\n\n"); + end + io.write( + "Usage: lua Generator.lua INPUTFILE OUTPUTFILE\n".. + "Converts the Minecraft blocks.json report format to the cuberite ".. + "block type palette format.\n".. + "\n".. + "INPUTFILE and OUTPUTFILE must point to a valid path. INPUTFILE must ".. + "be readable and OUTPUTFILE must be writable. Either can be replaced ".. + "with `-` (dash character) to point to standard-input or -output.\n"); + os.exit(message and 1 or 0); +end + + +-- Test whether the script is run in a path where it can load it's libraries +if not pcall(function() require("lunajson.decoder") end) then + usage("Could not load required libraries, please run `Generator.lua` ".. + "within its directory and make sure to run `git submodule update`."); +end + + +-- Check/Prepare CLI arguments +local inpath, outpath = ...; +io.input(io.stdin); +io.output(io.stdout); + +if select("#", ...) ~= 2 then + usage("Incorrect number of arguments."); +end + +if inpath ~= "-" then + local handle, err = io.open(inpath, "r"); + io.input(handle or usage(err)); +end + +if outpath ~= "-" then + local handle, err = io.open(outpath, "w"); + io.output(handle or usage(err)); +end + + +-- Main program starts here +local decode = (require("lunajson.decoder"))(); +local encode = (require("lunajson.encoder"))(); + +local input = decode(io.input():read("*a")); +local registry = {}; +local max_id = -1; + + +for blockname, blockdata in pairs(input) do + for i = 1, #(blockdata.states or {}) do + local state = blockdata.states[i]; + assert(registry[state.id + 1] == nil, "Ensure no duplicate IDs"); + + -- needed in the end to verify we got no holes in the array: + max_id = math.max(max_id, state.id); + + registry[state.id + 1] = { + id = assert(state.id, "id is required."), + name = assert(blockname, "Block type name is required."), + -- default = state.default or nil, -- may need this later + props = state.properties, + }; + end +end + + +-- The following assertion is not necessary by the current spec, but is required +-- by how lunajson distinguishes objects from arrays. Also if this fails, it is +-- _very_ likely that the input file is faulty. +assert(#registry == max_id + 1, "Ensure that registry has contiguous keys"); + +local out = { + Metadata = { + ProtocolBlockTypePaletteVersion = 1 + }, + Palette = registry +}; + +io.write(encode(out), "\n"); diff --git a/Tools/BlockTypePaletteGenerator/Readme.md b/Tools/BlockTypePaletteGenerator/Readme.md new file mode 100644 index 000000000..dc479d16f --- /dev/null +++ b/Tools/BlockTypePaletteGenerator/Readme.md @@ -0,0 +1,63 @@ +This generator crafts an intermediate index format to be read by cuberite + +# Running + +Run `lua ./Generator.lua`, pass `blocks.json` as first argument to the script +and the desired output location as 2nd argument. + +Make sure to run the Generator from within its directory (`cd` into the path +where `Generator.lua` is.) + +## Examples + +```bash +SERVER=/path/to/server.jar +java -cp "$SERVER" net.minecraft.data.Main --reports && +lua Generator.lua \ + generated/reports/blocks.json \ + ../../Server/Protocol/1.13/ProtocolBlockTypePalette.json +``` + +```bash +SERVER=/path/to/server.jar +java -cp "$SERVER" net.minecraft.data.Main --reports && +lua Generator.lua - -\ + < generated/reports/blocks.json \ + > ../Server/Protocol/1.13/ProtocolBlockTypePalette.json +``` + +## Output format + +The Format is a `JSON` document containing an object with at least two keys at +the top level: `Metadata` and `Palette`. + +`Metadata` contains document metadata, namely a key `"ProtocolBlockType": 1`. + +`Palette` contains an array of objects. Each of these objects has at least the +keys `id`, `name` and an optional `props` key that contains the individual +properties of the current state. These properties are a KV dict of pure strings. + +The order of the array or object elements is not significant. `id` is unique. + +```json +{ + "Metadata": { + "ProtocolBlockType": 1 + }, + "Palette": [{ + "id": 0, + "name": "minecraft:air" + }, { + "id": 1, + "name": "minecraft:stone" + }, { + "id": 221, + "name": "minecraft:dark_oak_leaves", + "props": { + "persistent": "false", + "distance": "4" + } + } + ] +} +``` \ No newline at end of file diff --git a/Tools/BlockTypePaletteGenerator/lib/lunajson b/Tools/BlockTypePaletteGenerator/lib/lunajson new file mode 160000 index 000000000..1bdc886a9 --- /dev/null +++ b/Tools/BlockTypePaletteGenerator/lib/lunajson @@ -0,0 +1 @@ +Subproject commit 1bdc886a9abb1be745fa7436f2a043b0455d70b8 -- cgit v1.2.3