[GUIDE] creating blueprints manually

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
User avatar
ownlyme
Filter Inserter
Filter Inserter
Posts: 400
Joined: Thu Dec 21, 2017 8:02 am
Contact:

[GUIDE] creating blueprints manually

Post 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)
mods.factorio.com/user/ownlyme
My requests: uiAbove||Grenade arc||Blueprint allies||Creeps forget command/ don't get removed||Player Modifiers||textbox::selection||Better Heat IF||Singleplayer RCON||tank bug w/ min_range >= projectile_creation_distance

Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: [GUIDE] creating blueprints manually

Post 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

Post Reply

Return to “Modding discussion”