1+ ---
2+ -- @module SaveHandler
3+ --
4+
5+ -- ------------------------------------------------
6+ -- Required Modules
7+ -- ------------------------------------------------
8+
19local Log = require ( ' src.util.Log' );
10+ local Compressor = require ( ' src.util.Compressor' )
211
312-- ------------------------------------------------
413-- Module
@@ -11,79 +20,20 @@ local SaveHandler = {};
1120-- ------------------------------------------------
1221
1322local SAVE_FOLDER = ' saves'
14- local UNCOMPRESSED_SAVE = ' uncompressed.lua'
1523local COMPRESSED_SAVE = ' compressed.data'
1624local VERSION_FILE = ' version.data'
17- local DEBUG = false
1825
1926-- ------------------------------------------------
2027-- Private Functions
2128-- ------------------------------------------------
2229
2330---
24- -- Takes a table and recursively turns it into a human-readable and nicely
25- -- formatted string stored as a sequence.
26- -- @param value (mixed) The value to serialize.
27- -- @param output (table) The table used for storing the lines of the final file.
28- -- @param depth (number) An indicator for the depth of the recursion.
29- --
30- local function serialize ( value , output , depth )
31- -- Append whitespace for each depth layer.
32- local ws = ' ' ;
33- for _ = 1 , depth do
34- ws = ws .. ' ' ;
35- end
36-
37- if type ( value ) == ' table' then
38- for k , v in pairs (value ) do
39- if type ( v ) == ' table' then
40- table.insert ( output , string.format ( ' %s[\' %s\' ] = {' , ws , tostring ( k )));
41- serialize ( v , output , depth + 1 );
42- table.insert ( output , string.format ( ' %s},' , ws ));
43- elseif type ( v ) == ' string' then
44- table.insert ( output , string.format ( ' %s[\' %s\' ] = "%s",' , ws , tostring ( k ), tostring ( v )));
45- else
46- table.insert ( output , string.format ( ' %s[\' %s\' ] = %s,' , ws , tostring ( k ), tostring ( v )));
47- end
48- end
49- else
50- table.insert ( output , string.format ( ' %s%s,' , tostring ( value )));
51- end
52- end
53-
54- ---
55- -- Takes care of transforming strings to numbers if possible.
56- -- @param value (mixed) The value to check.
57- -- @return (mixed) The converted value.
31+ -- Creates a file containing only the version string.
32+ -- @string dir The directory to store the version file in.
33+ -- @table version A table containing the version field.
5834--
59- local function convertStrings ( value )
60- local keysToReplace = {};
61-
62- for k , v in pairs ( value ) do
63- if tonumber ( k ) then
64- keysToReplace [# keysToReplace + 1 ] = k ;
65- end
66-
67- if type ( v ) == ' table' then
68- convertStrings ( v );
69- elseif tonumber ( v ) then
70- value [k ] = tonumber ( v );
71- end
72- end
73-
74- -- If the key can be transformed into a number delete the original
75- -- key-value pair and store the value with the numerical key.
76- for _ , k in ipairs ( keysToReplace ) do
77- local v = value [k ];
78- value [k ] = nil ;
79- value [tonumber (k )] = v ;
80- end
81-
82- return value ;
83- end
84-
8535local function createVersionFile ( dir , version )
86- love . filesystem . write ( dir .. ' /' .. VERSION_FILE , love . math . compress ( version , ' lz4 ' , 9 ) )
36+ Compressor . save ( version , dir .. ' /' .. VERSION_FILE )
8737end
8838
8939-- ------------------------------------------------
@@ -102,38 +52,18 @@ function SaveHandler.save( t, name )
10252 local folder = SAVE_FOLDER .. ' /' .. name
10353 love .filesystem .createDirectory ( folder )
10454
105- createVersionFile ( folder , getVersion () )
106-
107- -- Serialize the table.
108- local output = {};
109- table.insert ( output , ' return {' );
110- serialize ( t , output , 0 )
111- table.insert ( output , ' }' );
112-
113- local str = table.concat ( output , ' \n ' );
114- local compress = love .math .compress ( str , ' lz4' , 9 );
115-
116- -- Save uncompressed output for debug purposes only.
117- if DEBUG then
118- love .filesystem .write ( folder .. ' /' .. UNCOMPRESSED_SAVE , str )
119- end
55+ createVersionFile ( folder , { version = getVersion () })
12056
12157 -- Save compressed file.
122- love . filesystem . write ( folder .. ' /' .. COMPRESSED_SAVE , compress )
58+ Compressor . save ( t , folder .. ' /' .. COMPRESSED_SAVE )
12359end
12460
12561function SaveHandler .load ( path )
126- local compressed , bytes = love .filesystem .read ( path .. ' /' .. COMPRESSED_SAVE )
127- Log .print ( string.format ( ' Loaded savegame (Size: %d bytes)' , bytes ), ' SaveHandler' );
128-
129- local decompressed = love .math .decompress ( compressed , ' lz4' );
130- local rawsave = loadstring ( decompressed )();
131- return convertStrings ( rawsave );
62+ return Compressor .load ( path .. ' /' .. COMPRESSED_SAVE )
13263end
13364
13465function SaveHandler .loadVersion ( path )
135- local compressed = love .filesystem .read ( path .. ' /' .. VERSION_FILE )
136- return love .math .decompress ( compressed , ' lz4' )
66+ return Compressor .load ( path .. ' /' .. VERSION_FILE ).version
13767end
13868
13969function SaveHandler .getSaveFolder ()
0 commit comments