Page 1 of 1

[GUIDE] creating blueprints manually

Posted: Tue May 14, 2019 5:36 pm
by ownlyme
I made a copy-paste mod for 0.16, but i figured out i didn't need to create the blueprint strings myself.
So here's what i found:

Requirements:
https://github.com/jiyinyiyong/json-lua
https://github.com/iskolbin/lbase64
https://github.com/SafeteeWoW/LibDeflate

Example script:

Code: Select all

require 'lib.LibDeflate'
base64 = require("lib.base64")
require 'lib.JSON'


script.on_event(defines.events.on_player_changed_position, function(event)
	local example_input = '{"blueprint":{"icons":[{"signal":{"type":"item","name":"assembling-machine-3"},"index":1}],"entities":[{"entity_number":1,"name":"assembling-machine-3","position":{"x":-2,"y":0}},{"entity_number":2,"name":"assembling-machine-3","position":{"x":1,"y":0}}],"item":"blueprint","version":73016934401}}'
	local compressed = LibDeflate:CompressZlib(example_input,{strategy="fixed"})  --level = ?? ... can't figure out the exact algorithm the game uses - but fortunately the game doesn't care which compression is used
	compressed = base64.encode( compressed)
	compressed = "0"..compressed
	game.players[1].cursor_stack.import_stack(compressed)
end)
(I didn't work out the JSON part yet, but should be pretty straightforward)

to decode a blueprint-string, simply remove the first character, run base64.decode and then LibDeflate:DecompressZlib

Code: Select all

	local test = base64.decode(compressed:sub(2))
	test = LibDeflate:DecompressZlib(test)
	game.players[1].print(test)

Re: [GUIDE] creating blueprints manually

Posted: Tue May 14, 2019 6:33 pm
by Choumiko
Why not use:
LuaItemStack.export_stack
LuaGameScript.json_to_table
LuaGameScript.table_to_json
and there is also util.encode and util.decode in data/core/lualib/util.lua

Unless i'm missing something that's all you should need, no?
To stay in your example: set the cursor to an empty blueprint, use json_to_table, assign the icons, entitites, tiles, and export_stack() the cursor