1
+ ---
2
+ -- @module SaveHandler
3
+ --
4
+
5
+ -- ------------------------------------------------
6
+ -- Required Modules
7
+ -- ------------------------------------------------
8
+
1
9
local Log = require ( ' src.util.Log' );
10
+ local Compressor = require ( ' src.util.Compressor' )
2
11
3
12
-- ------------------------------------------------
4
13
-- Module
@@ -11,79 +20,20 @@ local SaveHandler = {};
11
20
-- ------------------------------------------------
12
21
13
22
local SAVE_FOLDER = ' saves'
14
- local UNCOMPRESSED_SAVE = ' uncompressed.lua'
15
23
local COMPRESSED_SAVE = ' compressed.data'
16
24
local VERSION_FILE = ' version.data'
17
- local DEBUG = false
18
25
19
26
-- ------------------------------------------------
20
27
-- Private Functions
21
28
-- ------------------------------------------------
22
29
23
30
---
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.
58
34
--
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
-
85
35
local function createVersionFile ( dir , version )
86
- love . filesystem . write ( dir .. ' /' .. VERSION_FILE , love . math . compress ( version , ' lz4 ' , 9 ) )
36
+ Compressor . save ( version , dir .. ' /' .. VERSION_FILE )
87
37
end
88
38
89
39
-- ------------------------------------------------
@@ -102,38 +52,18 @@ function SaveHandler.save( t, name )
102
52
local folder = SAVE_FOLDER .. ' /' .. name
103
53
love .filesystem .createDirectory ( folder )
104
54
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 () })
120
56
121
57
-- Save compressed file.
122
- love . filesystem . write ( folder .. ' /' .. COMPRESSED_SAVE , compress )
58
+ Compressor . save ( t , folder .. ' /' .. COMPRESSED_SAVE )
123
59
end
124
60
125
61
function 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 )
132
63
end
133
64
134
65
function 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
137
67
end
138
68
139
69
function SaveHandler .getSaveFolder ()
0 commit comments