Skip to content

Commit

Permalink
stats command calculates compressed size even if the cart code was no…
Browse files Browse the repository at this point in the history
…t originally compressed (.p8 or small .p8.png).

I'm only now noticing that the compression routine is compatible with Pico-8's decompressor but produces a much less efficient result than Pico-8's compressor. Filed issue dansanderson#7.
  • Loading branch information
dansanderson committed Oct 22, 2016
1 parent 1b7198b commit 8f55cc6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
14 changes: 14 additions & 0 deletions pico8/game/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,20 @@ def from_p8png_file(cls, instr, filename=None):

return new_game

def get_compressed_size(self):
"""Gets the compressed code size.
If the code was not already stored compressed, this runs the
compression routine to determine the size it would be if compressed.
Returns:
The compressed code size, as a number of bytes.
"""
if self.compressed_size is not None:
return self.compressed_size
comp_result = self.compress_code(''.join(self.lua.to_lines()))
return len(comp_result)

def to_p8_file(self, outstr, lua_writer_cls=None, lua_writer_args=None,
filename=None):
"""Write the game data as a .p8 file.
Expand Down
5 changes: 2 additions & 3 deletions pico8/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def stats(args):
g.lua.get_char_count(),
g.lua.get_token_count(),
g.lua.get_line_count(),
g.compressed_size
g.get_compressed_size()
])
else:
title = g.lua.get_title()
Expand All @@ -112,8 +112,7 @@ def stats(args):
'- tokens: {}\n- compressed chars: {}\n'.format(
g.lua.version, g.lua.get_line_count(),
g.lua.get_char_count(), g.lua.get_token_count(),
g.compressed_size if g.compressed_size is not None
else '(not compressed)'))
g.get_compressed_size()))
util.write('\n')

return 0
Expand Down
7 changes: 5 additions & 2 deletions tests/pico8/game/game_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,12 @@ def testCompressCodeHelloExample(self):
comp_result])
decomp_result = game.Game.decompress_code(code_bytes)
self.assertEqual(decomp_result[1], test_str)

# TODO: actual Pico-8 result:
#self.assertEqual(bytearray([13, 51, 0, 34, 20, 17, 24, 24, 27, 0, 34, 1, 14, 60, 90, 2, 13, 24, 31, 60, 223, 61, 254, 62, 253, 63, 252, 64, 171]),
# result)
p8_comp_result = bytearray([13, 51, 0, 34, 20, 17, 24, 24, 27, 0, 34, 1, 14, 60, 90, 2, 13, 24, 31, 60, 223, 61, 254, 62, 253, 63, 252, 64, 171])
self.assertEqual(len(comp_result), len(p8_comp_result))
self.assertEqual(comp_result, p8_comp_result)


class TestGameToP8(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 8f55cc6

Please sign in to comment.