dansanderson / picotool

Tools and Python libraries for manipulating Pico-8 game files. http://www.lexaloffle.com/pico-8.php

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

luamin option to support preserving property names in dot syntax when also accessed via value indexing

danhowardgames opened this issue · comments

swordfish1_fmt_000

I suspect its something to do with the renaming of table indexes but im not sure!

The error in question is here, but it appears in other sectons if i comment this out.

original:

function draw_puffer(e)
 local sinfo=e.spr_info[e.state] --load state sprites
 local sprites = sinfo.sprites -- all states have one sprite set for puffer
 local flipx = false -- is it flipped horixontally?
 local flipy = false --is it flipped vertically?
 local sprw=1 --how many sprites wide is it?
 local sprh=1 --how many sprites tall is it?
 local xoff=-4 --how much is the draw offset on x
 local yoff=-4 --how much is the draw offset on y
 --if player is to the right, flip
 if player.x > e.x then flipx = true end
 --if puffed, change offest
 if e.state=="puffed" then xoff = -8 yoff = -8 sprw=2 sprh=2 end
 --set the current sprite accoring to animt and dt
 local spri = sprites[flr(e.animt/sinfo.dt)%#sprites+1]
 drawoutline(spri,e.x+xoff,e.y+yoff,sprw,sprh,flipx,flipy,0,true)
end

minified:

function gx(gh)
local ft=gh.eo[gh.cc]
local fy=ft.fy
local fu=false
local fv=false
local fw=1
local fx=1
local ct=-4
local cu=-4
if s.t>gh.t then fu=true end
if gh.cc=="puffed"then ct=-8 cu=-8 fw=2 fx=2 end
local fz=fy[flr(gh.ej/ft.eu)%#fy+1]
gc(fz,gh.t+ct,gh.u+cu,fw,fx,fu,fv,0,true)
end

i'm happy to send you the full code if that helps find the error?

Thanks

@chowyunbrent I have a guess as to what's going on: the sprites property of sinfo is being recognized as a symbol and thus minified to fy. This works only if the sprites property is always assigned using dot notation, so that every sprites is replaced with fy. I bet that somewhere else in your code, you refer to the sprites property with string index notation, which it doesn't know to minify. Am I right?

test = {}
test['foo'] = 123
print(test.foo)

minifies to:

a={}
a['foo']=123
print(a.b)

which is wrong. The correct behavior is probably to never minify property names, which is a minor loss but the only way to assure correctness.

If you can, you can work around this using dot notation everywhere:

test = {}
test.foo = 123
print(test.foo)

Table initializer syntax is also unaffected:

test = {foo=123}
print(test.foo)

minifies correctly to:

a={b=123}
print(a.b)

Hi! Thank you so much for getting back to me.

Yes i think you are right... but it looks like its the e.spr_info[e.state] that uses e.state as string index notation (the state name e.g. "idle"). I'll have a go at rewriting it and try again.

Thanks for your help Dan!

Ah yes, if e.spr_info is defined elsewhere using dot or table initializer syntax, then e.state will be "idle" but the actual state name will be minified. You can change where that is initialized to use strings: e.spr_info["idle"] = ... and it should work.

I'm leaving this issue open because the tool should really account for this. Thanks for the report!

You may want to try my pull request. It addresses this specific issue and may fix your problem.

luamin now has --keep-names-from-file= for project-specific property name preservation (or just working around my delays in adding new built-ins), as well as --keep-all-names to restrict minification to comments and whitespace.

I've decided not to support a luamin flag that exempts all properties. Any project that refers to the same property with dot notation and index notation is already dealing with a fixed quantity of properties that the author can put in a file. It's a bit more to ask of authors that are doing this, but I feel like it's enough of a solution and there's not enough added benefit to fully implement something that exempts all property names and nothing else.