-
Notifications
You must be signed in to change notification settings - Fork 836
Expand file tree
/
Copy pathsaverestore.lua
More file actions
428 lines (290 loc) · 9.53 KB
/
saverestore.lua
File metadata and controls
428 lines (290 loc) · 9.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
local ErrorNoHaltWithStack = ErrorNoHaltWithStack
local type = type
local pairs = pairs
local gmod = gmod
local table = table
--[[
This module is to help bind the engine's saverestore stuff to Lua.
This is so we can save Lua stuff in the save game file. The entities
should automatically save most of their table contents to the save file.
!!Warning!!: Editing this file may render old saves useless.
You can hook into this system like so
local function MySaveFunction( save )
saverestore.WriteTable( my_table, save )
end
local function MyRestoreFunction( restore )
my_table = saverestore.ReadTable( restore )
end
saverestore.AddSaveHook( "HookNameHere", MySaveFunction )
saverestore.AddRestoreHook( "HookNameHere", MyRestoreFunction )
--]]
module( "saverestore" )
local TYPE_NONE = 0
local TYPE_FLOAT = 1 -- Treat all numbers as floats (they're all the same to Lua)
local TYPE_STRING = 2
local TYPE_ENTITY = 3
local TYPE_VECTOR = 4
local TYPE_TABLE = 5
local TYPE_BOOL = 6
local TYPE_ANGLE = 7
local SaveHooks = {}
local RestoreHooks = {}
local TableRefs = {}
local TableRef = 1
function PreSave()
TableRefs = {}
TableRef = 1
end
function PreRestore()
TableRefs = {}
TableRef = 1
end
--[[---------------------------------------------------------
Name: GetTypeStr
Desc: Given a string returns a TYPE_
-----------------------------------------------------------]]
local function GetTypeStr( name )
if ( name == "function" ) then return TYPE_NONE end
if ( name == "number" ) then return TYPE_FLOAT end
if ( name == "string" ) then return TYPE_STRING end
if ( name == "Entity" ) then return TYPE_ENTITY end
if ( name == "Vehicle" ) then return TYPE_ENTITY end
if ( name == "NPC" ) then return TYPE_ENTITY end
if ( name == "Player" ) then return TYPE_ENTITY end
if ( name == "Weapon" ) then return TYPE_ENTITY end
if ( name == "Vector" ) then return TYPE_VECTOR end
if ( name == "Angle" ) then return TYPE_ANGLE end
if ( name == "table" ) then return TYPE_TABLE end
if ( name == "boolean" ) then return TYPE_BOOL end
-- These aren't saved
if ( name == "ConVar" ) then return TYPE_NONE end
if ( name == "PhysObj" ) then return TYPE_NONE end
-- Bitch about it incase I've forgot to hook a savable type up
ErrorNoHaltWithStack( "Can't save or load unknown type " .. name .. "\n" )
return TYPE_NONE
end
--[[---------------------------------------------------------
Name: GetType
Desc: Given a variable returns a TYPE_
-----------------------------------------------------------]]
local function GetType( var )
return GetTypeStr( type(var) )
end
--[[---------------------------------------------------------
Name: IsWritable
Desc: Returns true if we can save the K/V pair
-----------------------------------------------------------]]
local function IsWritable( k, v )
local itype = GetType( k )
if ( itype == TYPE_NONE ) then return false end
if ( itype == TYPE_STRING && k == "SR_Recursion" ) then return false end
local itype = GetType( v )
if ( itype == TYPE_NONE ) then return false end
return true
end
--[[---------------------------------------------------------
Name: WriteVar
Desc: Writes a single variable to the save
-----------------------------------------------------------]]
function WritableKeysInTable( t )
local i = 0
for k, v in pairs( t ) do
if ( IsWritable( k, v ) ) then
i = i + 1
end
end
return i
end
--[[---------------------------------------------------------
Name: WriteVar
Desc: Writes a single variable to the save
-----------------------------------------------------------]]
function WriteVar( var, save )
local itype = GetType( var )
if ( itype == TYPE_NONE ) then return end
save:StartBlock( type( var ) )
if ( itype == TYPE_FLOAT ) then
save:WriteFloat( var )
elseif ( itype == TYPE_BOOL ) then
save:WriteBool( var )
elseif ( itype == TYPE_STRING ) then
save:WriteString( var )
elseif ( itype == TYPE_ENTITY ) then
save:WriteEntity( var )
elseif ( itype == TYPE_ANGLE ) then
save:WriteAngle( var )
elseif ( itype == TYPE_VECTOR ) then
save:WriteVector( var )
elseif ( itype == TYPE_TABLE ) then
WriteTable( var, save )
else
ErrorNoHaltWithStack( "Error! Saving unsupported Type: " .. type( var ) .. "\n" )
end
save:EndBlock()
end
--[[---------------------------------------------------------
Name: ReadVar
Desc: Reads a single variable
-----------------------------------------------------------]]
function ReadVar( restore )
local retval = 0
local typename = restore:StartBlock()
local itype = GetTypeStr( typename )
if ( itype == TYPE_FLOAT ) then
retval = restore:ReadFloat()
elseif ( itype == TYPE_BOOL ) then
retval = restore:ReadBool()
elseif ( itype == TYPE_STRING ) then
retval = restore:ReadString()
elseif ( itype == TYPE_ENTITY ) then
retval = restore:ReadEntity()
elseif ( itype == TYPE_ANGLE ) then
retval = restore:ReadAngle()
elseif ( itype == TYPE_VECTOR ) then
retval = restore:ReadVector()
elseif ( itype == TYPE_TABLE ) then
retval = ReadTable( restore )
else
ErrorNoHaltWithStack( "Error! Loading unsupported Type: " .. typename .. "\n" )
end
restore:EndBlock()
return retval
end
--[[---------------------------------------------------------
Name: WriteTable
Desc: Writes a table to the save
-----------------------------------------------------------]]
function WriteTable( tab, save )
-- Write a blank table (because read will be expecting one)
if ( tab == nil ) then
save:StartBlock( "Table" )
save:EndBlock()
end
-- We have already saved this table
if ( TableRefs[ tab ] ) then
save:StartBlock( "TableRef" )
save:WriteInt( TableRefs[ tab ] )
save:EndBlock()
return
end
TableRefs[ tab ] = TableRef
save:StartBlock( "Table" )
local iCount = WritableKeysInTable( tab )
save:WriteInt( TableRef )
TableRef = TableRef + 1
save:WriteInt( iCount )
for k, v in pairs( tab ) do
if ( IsWritable( k, v ) ) then
WriteVar( k, save )
WriteVar( v, save )
end
end
save:EndBlock()
end
--[[---------------------------------------------------------
Name: ReadTable
Desc: Assumes a table is waiting to be read, returns a table
-----------------------------------------------------------]]
function ReadTable( restore )
local name = restore:StartBlock()
local tab = {}
if ( name == "TableRef" ) then
local ref = restore:ReadInt()
if ( !TableRefs[ ref ] ) then
TableRefs[ ref ] = {}
restore:EndBlock()
ErrorNoHaltWithStack( "Failed to read SaveRestore table!" )
return
end
tab = TableRefs[ ref ]
else
local iRef = restore:ReadInt()
local iCount = restore:ReadInt()
if ( TableRefs[ iRef ] ) then
tab = TableRefs[ iRef ]
end
for i = 0, iCount - 1 do
local k = ReadVar( restore )
local v = ReadVar( restore )
tab[ k ] = v
end
TableRefs[ iRef ] = tab
end
restore:EndBlock()
return tab
end
--[[---------------------------------------------------------
Name: SaveEntity
Desc: Called by the engine for each entity
-----------------------------------------------------------]]
function SaveEntity( ent, save )
save:StartBlock( "EntityTable" )
WriteTable( ent:GetTable(), save )
save:EndBlock()
end
--[[---------------------------------------------------------
Name: LoadEntity
Desc: Called by the engine for each entity
-----------------------------------------------------------]]
function LoadEntity( ent, restore )
local EntTable = ent:GetTable()
local name = restore:StartBlock()
if ( name == "EntityTable" ) then
table.Merge( EntTable, ReadTable( restore ) )
end
restore:EndBlock()
ent:SetTable( EntTable )
end
--[[---------------------------------------------------------
Name: AddSaveHook
Desc: Adds a hook enabling something to save something..
-----------------------------------------------------------]]
function AddSaveHook( name, func )
local h = {}
h.Name = name
h.Func = func
SaveHooks[ name ] = h
end
--[[---------------------------------------------------------
Name: AddRestoreHook
Desc: Adds a hook enabling something to restore something..
-----------------------------------------------------------]]
function AddRestoreHook( name, func )
local h = {}
h.Name = name
h.Func = func
RestoreHooks[ name ] = h
end
--[[---------------------------------------------------------
Name: SaveGlobal
Desc: Should save any Lua stuff that isn't entity based.
-----------------------------------------------------------]]
function SaveGlobal( save )
save:StartBlock( "GameMode" )
WriteTable( gmod.GetGamemode(), save )
save:EndBlock()
for k, v in pairs( SaveHooks ) do
save:StartBlock( v.Name )
v.Func( save )
save:EndBlock()
end
end
--[[---------------------------------------------------------
Name: LoadGlobal
Desc: ...
-----------------------------------------------------------]]
function LoadGlobal( restore )
local name = restore:StartBlock()
if ( name == "GameMode" ) then
table.Merge( gmod.GetGamemode(), ReadTable( restore ) )
end
restore:EndBlock()
while ( name != "EndGlobal" ) do
name = restore:StartBlock()
local tab = RestoreHooks[ name ]
if ( tab ) then
tab.Func( restore )
end
restore:EndBlock()
end
end