Page 1 of 1

gzip/base64 with Lua

Posted: Sun Feb 08, 2009 11:43 pm
by Skofo
I was thinking of creating a LOVE library which can parse Tiled maps, but Tiled map files' data is apparently gzip compressed and base64 encoded. Anyone know if there is functionality (or ability to implement functionality) in LOVE/Lua which can decompress gzip and decode base64? I couldn't find anything on Google.

Re: gzip/base64 with Lua

Posted: Mon Feb 09, 2009 6:32 am
by bartbes
I see results for both "base64" and "gzip" when searching on http://luaforge.net, have fun!

Re: gzip/base64 with Lua

Posted: Sat Feb 14, 2009 8:49 pm
by Tad2020
I just read about this and tried it. Why not turn off the compression and encoding? Edit>Perfs>Saving, untick Compress, Use encoding, Embed images. This made it save in clear txt. I would run this map file through a cleanup filter afterwards though, I saw a ton of waste with the Lua file export.

Re: gzip/base64 with Lua

Posted: Sun Feb 15, 2009 12:52 am
by Tad2020
Yeah, turn off the compress, encode and embed, and export as lua. I made a quick and dirty re-serializer for use with stand alone Lua. Its rather simple, but it shows what I'm talking about.

Code: Select all

--Usage: Lua.exe map_file.lua

dofile(arg[1])

output = "map = {\n"

output = output.."\ttile_size = "..map.tilewidth..",\n"
output = output.."\twidth = "..map.width..",\n"
output = output.."\theight = "..map.height..",\n"

output = output.."\tlayers = {\n"

for k,layer in pairs (map.layers) do
	output = output.."\t\t["..layer.name.."] = {\n"
	for i,t in pairs (layer.data) do
		local y = math.floor((i-1)/map.width)
		local x = (i-1) % layer.width
		
		if x == 0 then
			output = output.."\t\t\t{"
		end
		
		output = output..t.gid..", "
		
		if x == layer.width - 1 then
			output = output.."},--"..(y+1).."\n"
		end
	end
	output = output.."\t\t},\n"
end

output = output.."\t},\n}"

function save_file(filename,data)
   local file = assert(io.open(filename,"w+"))
   file:write(data)
   file:flush()
   file:close()
end

save_file("converted_"..arg[1],output)
Note: I used numeric layer names and it lacks tileset info


Turns this:

Code: Select all

-- Generated by Tiled's Lua Exporter Plugin.
map = {
  ["label"] = "map";
  ["version"] = "0.99b";
  ["luaversion"] = "5.1";
  ["orientation"] = "orthogonal";
  ["width"] = 64;
  ["height"] = 64;
  ["tilewidth"] = 32;
  ["tileheight"] = 32;
  ["tilesets"] = {
    {
      ["label"] = "tileset";
      ["name"] = "Untitled";
      ["firstgid"] = 1;
      ["tilewidth"] = 32;
      ["tileheight"] = 32;
      {
        ["label"] = "image";
        ["source"] = "1b.png";
      };
    };
  };
  ["layers"] = {
    {
      ["label"] = "layer";
      ["name"] = "0";
      ["width"] = 64;
      ["height"] = 64;
      ["data"] = {
        {
          ["label"] = "tile";
          ["gid"] = 1;
        };
        {
          ["label"] = "tile";
          ["gid"] = 1;
        };
        {
          ["label"] = "tile";
          ["gid"] = 1;
        };
        {
          ["label"] = "tile";
          ["gid"] = 1;
        };
        {
          ["label"] = "tile";
          ["gid"] = 1;
        };
        {
          ["label"] = "tile";
          ["gid"] = 1;
        };
In to this:

Code: Select all

map = {
	tile_size = 32,
	width = 64,
	height = 64,
	layers = {
		[0] = {
			{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, },--0
			{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, },--1
			{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 1, 1, 1, 8, 8, 1, 1, },--2
			

Re: gzip/base64 with Lua

Posted: Tue Feb 17, 2009 3:45 am
by Inny
When I wrote that plugin, I didn't write an importer, so don't use it as an active development file. Use the default TMX files for something you'll keep editing, and save the lua file when you're ready to test it. I whole heartily encourage anyone who feels they can to spruce up the Lua plugin. My Java is rusty from disuse and I don't have the time to refresh it.