@@ -8,6 +8,9 @@ module (..., package.seeall)
8
8
-- See "Programming In Lua" chapter 12.1.2.
9
9
-- Also see forum thread:
10
10
-- http://www.gammon.com.au/forum/?id=4960
11
+
12
+ -- Amended 7th April 2020 to fix bug re serializing variables
13
+ -- named 'true' or 'false'
11
14
-- ----------------------------------------------------------
12
15
13
16
--[[
@@ -16,7 +19,7 @@ module (..., package.seeall)
16
19
17
20
require "serialize"
18
21
SetVariable ("mobs", serialize.save ("mobs")) --> serialize mobs table
19
- loadstring (GetVariable ("mobs")) () --> restore mobs table
22
+ loadstring (GetVariable ("mobs")) () --> restore mobs table
20
23
21
24
If you need to serialize two tables where subsequent ones refer to earlier ones
22
25
you can supply your own "saved tables" variable, like this:
@@ -26,10 +29,10 @@ module (..., package.seeall)
26
29
result = result .. "\n" .. serialize.save ("quests", nil, t)
27
30
28
31
In this example the serializing of "quests" also knows about the "mobs" table
29
- and will use references to it where necessary.
32
+ and will use references to it where necessary.
30
33
31
34
You can also supply the actual variable if the variable to be serialized does
32
- not exist in the global namespace (for instance, if the variable is a local
35
+ not exist in the global namespace (for instance, if the variable is a local
33
36
variable to a function). eg.
34
37
35
38
require "serialize"
@@ -42,17 +45,17 @@ module (..., package.seeall)
42
45
because it would not be found in the _G namespace.
43
46
44
47
----- Added on 19 July 2007:
45
-
48
+
46
49
You can now do a "simple save" which is intended for tables without cycles. That is,
47
50
tables, that do not refer to other tables. This is appropriate for "simple" data, like
48
51
a straight table of keys/values, including subtables.
49
-
52
+
50
53
For a simple save, all you need to do is supply the value, like this:
51
-
54
+
52
55
print (serialize.save_simple ({ foo = 22, bar = "hi", t = { s = 9, k = 22 } }))
53
-
56
+
54
57
This produces:
55
-
58
+
56
59
{
57
60
t = {
58
61
s = 9,
@@ -61,7 +64,7 @@ module (..., package.seeall)
61
64
bar = "hi",
62
65
foo = 22,
63
66
}
64
-
67
+
65
68
--]]
66
69
67
70
local save_item -- forward declaration, function appears near the end
@@ -72,12 +75,12 @@ function save (what, v, saved)
72
75
saved = saved or {} -- initial table of tables we have already done
73
76
v = v or _G [what ] -- default to "what" in global namespace
74
77
75
- assert (type (what ) == " string" ,
78
+ assert (type (what ) == " string" ,
76
79
" 1st argument to serialize.save should be the *name* of a variable" )
77
-
80
+
78
81
assert (v , " Variable '" .. what .. " ' does not exist" )
79
82
80
- assert (type (saved ) == " table" or saved == nil ,
83
+ assert (type (saved ) == " table" or saved == nil ,
81
84
" 3rd argument to serialize.save should be a table or nil" )
82
85
83
86
local out = {} -- output to this table
@@ -88,7 +91,7 @@ end -- serialize.save
88
91
function save_simple (v )
89
92
local out = {} -- output to this table
90
93
save_item_simple (v , out , 2 ) -- do serialization
91
- return table.concat (out ) -- turn into a string
94
+ return table.concat (out ) -- turn into a string
92
95
end -- serialize.save_simple
93
96
94
97
--- below are local functions for this module -------------
@@ -99,7 +102,7 @@ local function basicSerialize (o)
99
102
else -- assume it is a string
100
103
return string.format (" %q" , o )
101
104
end
102
- end -- basicSerialize
105
+ end -- basicSerialize
103
106
104
107
--
105
108
-- Lua keywords might look OK to not be quoted as keys but must be.
@@ -109,22 +112,22 @@ end -- basicSerialize
109
112
local lua_reserved_words = {}
110
113
111
114
for _ , v in ipairs ({
112
- " and" , " break" , " do" , " else" , " elseif" , " end" ,
113
- " for" , " function" , " if" , " in" , " local" , " nil" , " not" , " or" ,
114
- " repeat" , " return" , " then" , " until" , " while"
115
+ " and" , " break" , " do" , " else" , " elseif" , " end" ,
116
+ " for" , " function" , " if" , " in" , " local" , " nil" , " not" , " or" ,
117
+ " repeat" , " return" , " then" , " until" , " while" , " true " , " false "
115
118
}) do lua_reserved_words [v ] = true end
116
119
117
120
-- ----------------------------------------------------------
118
121
-- save one variable (calls itself recursively)
119
- --
122
+ --
120
123
-- Modified on 23 October 2005 to better handle keys (like table keys)
121
124
-- ----------------------------------------------------------
122
125
function save_item (name , value , out , indent , saved ) -- important! no "local" keyword
123
126
local iname = string.rep (" " , indent ) .. name -- indented name
124
127
125
128
-- numbers, strings, and booleans can be simply serialized
126
129
127
- if type (value ) == " number" or
130
+ if type (value ) == " number" or
128
131
type (value ) == " string" or
129
132
type (value ) == " boolean" then
130
133
table.insert (out , iname .. " = " .. basicSerialize (value ))
@@ -141,31 +144,31 @@ function save_item (name, value, out, indent, saved) -- important! no "local" k
141
144
saved [value ] = name -- save name for next time
142
145
143
146
-- make the table constructor, and recurse to save its contents
144
-
147
+
145
148
table.insert (out , iname .. " = {}" ) -- create a new table
146
149
147
150
for k , v in pairs (value ) do -- save its fields
148
- local fieldname
151
+ local fieldname
149
152
150
153
-- if key is a Lua variable name which is not a reserved word
151
154
-- we can express it as tablename.keyname
152
155
153
156
if type (k ) == " string"
154
- and string.find (k , " ^[_%a][_%a%d]*$" )
157
+ and string.find (k , " ^[_%a][_%a%d]*$" )
155
158
and not lua_reserved_words [k ] then
156
159
fieldname = string.format (" %s.%s" , name , k )
157
160
158
161
-- if key is a table itself, and we know its name then we can use that
159
162
-- eg. tablename [ tablekeyname ]
160
163
161
164
elseif type (k ) == " table" and saved [k ] then
162
- fieldname = string.format (" %s[%s]" , name , saved [k ])
165
+ fieldname = string.format (" %s[%s]" , name , saved [k ])
163
166
164
167
-- if key is an unknown table, we have to raise an error as we cannot
165
168
-- deduce its name
166
-
169
+
167
170
elseif type (k ) == " table" then
168
- error (" Key table entry " .. tostring (k ) ..
171
+ error (" Key table entry " .. tostring (k ) ..
169
172
" in table " .. name .. " is not known" )
170
173
171
174
-- if key is a number or a boolean it can simply go in brackets,
@@ -175,7 +178,7 @@ function save_item (name, value, out, indent, saved) -- important! no "local" k
175
178
fieldname = string.format (" %s[%s]" , name , tostring (k ))
176
179
177
180
-- now key should be a string, otherwise an error
178
-
181
+
179
182
elseif type (k ) ~= " string" then
180
183
error (" Cannot serialize table keys of type '" .. type (k ) ..
181
184
" ' in table " .. name )
@@ -185,13 +188,13 @@ function save_item (name, value, out, indent, saved) -- important! no "local" k
185
188
186
189
else
187
190
fieldname = string.format (" %s[%s]" , name ,
188
- basicSerialize (k ))
191
+ basicSerialize (k ))
189
192
end
190
193
191
194
-- now we have finally worked out a suitable name for the key,
192
195
-- recurse to save the value associated with it
193
196
194
- save_item (fieldname , v , out , indent + 2 , saved )
197
+ save_item (fieldname , v , out , indent + 2 , saved )
195
198
end
196
199
end
197
200
@@ -200,13 +203,13 @@ function save_item (name, value, out, indent, saved) -- important! no "local" k
200
203
else
201
204
error (" Cannot serialize '" .. name .. " ' (" .. type (value ) .. " )" )
202
205
end -- if type of variable
203
- end -- save_item
206
+ end -- save_item
204
207
205
208
-- saves tables *without* cycles (produces smaller output)
206
209
function save_item_simple (value , out , indent )
207
210
-- numbers, strings, and booleans can be simply serialized
208
211
209
- if type (value ) == " number" or
212
+ if type (value ) == " number" or
210
213
type (value ) == " string" or
211
214
type (value ) == " boolean" then
212
215
table.insert (out , basicSerialize (value ))
@@ -215,23 +218,23 @@ function save_item_simple (value, out, indent)
215
218
216
219
for k , v in pairs (value ) do -- save its fields
217
220
table.insert (out , string.rep (" " , indent ))
218
- if not string.find (k , ' ^[_%a][_%a%d]*$' )
221
+ if not string.find (k , ' ^[_%a][_%a%d]*$' )
219
222
or lua_reserved_words [k ] then
220
223
table.insert (out , " [" .. basicSerialize (k ) .. " ] = " )
221
224
else
222
225
table.insert (out , k .. " = " )
223
226
end -- if
224
- save_item_simple (v , out , indent + 2 )
227
+ save_item_simple (v , out , indent + 2 )
225
228
table.insert (out , " ,\n " )
226
229
end -- for each table item
227
-
230
+
228
231
table.insert (out , string.rep (" " , indent ) .. " }" )
229
-
232
+
230
233
-- cannot serialize things like functions, threads
231
234
232
235
else
233
236
error (" Cannot serialize " .. type (value ))
234
237
end -- if type of variable
235
-
238
+
236
239
end -- save_item_simple
237
240
0 commit comments