-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathice.lua
692 lines (487 loc) · 13.3 KB
/
ice.lua
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
-- Project: Ice
--
-- Version: 1.0
--
-- Author: Graham Ranson
--
-- Support: www.monkeydeadstudios.com or www.grahamranson.co.uk
--
-- Copyright (C) 2011 MonkeyDead Studios Limited. All Rights Reserved.
version = 1.0
-- INCLUDES --
local json = require( "json" )
require( "sqlite3" )
-- LOCAL FUNCTIONS --
local printError = function( message )
print( "Ice error - " .. message )
end
local function tableToString( t, indent )
local str = ""
if(indent == nil) then
indent = 0
end
-- Check the type
if(type(t) == "string") then
str = str .. (" "):rep(indent) .. t .. "\n"
elseif(type(t) == "number") then
str = str .. (" "):rep(indent) .. t .. "\n"
elseif(type(t) == "boolean") then
if(t == true) then
str = str .. "true"
else
str = str .. "false"
end
elseif(type(t) == "table") then
local i, v
for i, v in pairs(t) do
-- Check for a table in a table
if(type(v) == "table") then
str = str .. (" "):rep(indent) .. i .. ":\n"
str = str .. tableToString(v, indent + 2)
else
str = str .. (" "):rep(indent) .. i .. ": " .. tableToString(v, 0)
end
end
else
print("Error: unknown data type: %s", type(t))
end
return str
end
local createHeader = function()
local header = {}
header.version = version
header.created = os.time()
header.modified = os.time()
header.saved = os.time()
return header
end
local openDatabase = function( databaseName )
if databaseName then
local path = system.pathForFile( databaseName, system.DocumentsDirectory )
return sqlite3.open( path )
end
end
local saveDatabase = function( database, items, header )
if database then
local query = [[CREATE TABLE IF NOT EXISTS icebox (id INTEGER PRIMARY KEY, value, header);]]
database:exec( query )
local encodedItems = json.encode( items or {} )
if header then
header.saved = os.time()
end
local encodedHeader = json.encode( header or createHeader() )
local query = [[INSERT INTO icebox VALUES (NULL, ']] .. encodedItems .. [[',']] .. encodedHeader .. [['); ]]
database:exec( query )
database:close()
end
end
local loadDatabase = function( database )
local result = database:exec("SELECT * FROM icebox")
local items = {}
local header = {}
if database then
if result == 0 then
for row in database:nrows("SELECT * FROM icebox") do
local encodedItems = row.value
items = json.decode( encodedItems )
local encodedHeader = row.header
header = json.decode( encodedHeader )
if encodedHeader == "[]" then
header = createHeader()
end
end
end
end
return items, header
end
local deleteDatabase = function( databaseName )
if databaseName then
local path = system.pathForFile( databaseName, system.DocumentsDirectory )
os.remove( path )
end
end
local createRestorePointName = function( restorePointName, databaseName )
if restorePointName and databaseName then
return "ICE-RESTORE|" .. restorePointName .. "|" .. databaseName
end
end
-- ICE --
Ice = {}
Ice_mt = { __index = Ice }
--- Constructs a new Ice object.
function Ice:new()
local self = {}
self._boxes = {}
setmetatable( self, Ice_mt )
return self
end
--- Constructs a new IceBox.
-- @param name The name of the box.
function Ice:newBox( name )
self:destroyBox( name )
self._boxes[ #self._boxes + 1 ] = IceBox:new( name )
return self._boxes[ #self._boxes ]
end
--- Gets an IceBox.
-- @param name The name of the box.
function Ice:getBox( name )
for i = 1, #self._boxes, 1 do
if self._boxes[i]:getName() == name then
return self._boxes[ i ]
end
end
end
--- Destroys an IceBox.
-- @param name The name of the box.
function Ice:destroyBox( name )
local box = self:getBox( name )
if box then
box:destroy()
end
end
--- Saves an IceBox.
-- @param name The name of the box.
function Ice:saveBox( name )
local box = self:getBox( name )
if box then
box:save()
end
end
--- Loads an IceBox.
-- @param name The name of the box.
function Ice:loadBox( name )
local box = self:getBox( name )
if not box then
self._boxes[ #self._boxes + 1 ] = self:newBox( name )
self._boxes[ #self._boxes ]:load()
self._boxes[ #self._boxes ]:save()
box = self._boxes[ #self._boxes ]
end
return box
end
--- Stores/adjusts a value in an IceBox.
-- @param boxName The name of the box.
-- @param valueName The name of the value to store/adjust.
-- @param value The value to store.
function Ice:storeBoxValue( boxName, valueName, value )
local box = self:getBox( boxName )
if box then
box:store( valueName, value )
end
end
--- Retrieves a value from an IceBox.
-- @param boxName The name of the box.
-- @param valueName The name of the value to retrieve.
-- @return The value or nil if none found.
function Ice:retrieveBoxValue( boxName, valueName )
local box = self:getBox( boxName )
if box then
return box:retrieve( valueName )
end
end
--- Saves all stored iceboxes to the disk.
function Ice:save()
for i = 1, #self._boxes, 1 do
self._boxes[i]:save()
end
end
--- Clears all items out of the iceboxes and saves them.
function Ice:clear()
for i = 1, #self._boxes, 1 do
self._boxes[i]:clear()
end
end
--- Deletes all stored iceboxes from the disk.
function Ice:delete()
for i = 1, #self._boxes, 1 do
self._boxes[i]:delete()
end
end
--- Clears and deletes all iceboxes.
function Ice:destroy()
for i = 1, #self._boxes, 1 do
self._boxes[i]:destroy()
end
end
-- ICEBOX --
IceBox = {}
IceBox_mt = { __index = IceBox }
--- Constructs a new IceBox object.
-- @param name The name of the box.
function IceBox:new( name )
local self = {}
self._items = {}
self._version = 1.0
self._name = name or "default"
self._databaseName = self._name .. ".ice"
setmetatable( self, IceBox_mt )
return self
end
--- Saves a value in the icebox.
-- @param name The name of the value to add.
-- @param value The value to save.
-- @return The stored value.
function IceBox:store( name, value )
if name then
self._items[ name ] = value
if not self._header then
self._header = createHeader()
end
self._header.modified = os.time()
end
if self:isAutomaticSavingEnabled() then
self:save()
end
return value
end
--- Saves a value in the icebox if it isn't already in there.
-- @param name The name of the value to add.
-- @param value The value to save.
-- @return The stored value.
-- @return True if it was added, false if not.
function IceBox:storeIfNew( name, value )
local added = false
if name then
if self:hasValue( name ) then
else
self._items[ name ] = value
added = true
self._header.modified = os.time()
end
end
if self:isAutomaticSavingEnabled() then
self:save()
end
return added, value
end
--- Saves a value in the icebox if it is higher than the one already in there.
-- @param name The name of the value to add.
-- @param value The value to save.
-- @return The stored value.
-- @return True if it was added, false if not.
function IceBox:storeIfHigher( name, value )
local added = false
if name then
local currentValue = self:retrieve( name ) or 0
if currentValue then
if value > currentValue then
self._items[ name ] = value
added = true
self._header.modified = os.time()
end
else
self._items[ name ] = value
added = true
self._header.modified = os.time()
end
end
if self:isAutomaticSavingEnabled() then
self:save()
end
return added, value
end
--- Saves a value in the icebox if it is lower than the one already in there.
-- @param name The name of the value to add.
-- @param value The value to save.
-- @return The stored value.
-- @return True if it was added, false if not.
function IceBox:storeIfLower( name, value )
local added = false
if name then
local currentValue = self:retrieve( name ) or 0
if currentValue then
if currentValue ~= 0 then
if value < currentValue then
self._items[ name ] = value
added = true
self._header.modified = os.time()
end
else
self._items[ name ] = value
added = true
self._header.modified = os.time()
end
end
end
if self:isAutomaticSavingEnabled() then
self:save()
end
return added, value
end
--- Retrieves a value from the icebox.
-- @param name The name of the value to retrieve.
-- @return The value. Nil if none found.
function IceBox:retrieve( name )
if name then
return self._items[ name ]
end
end
--- Increments a value in the icebox.
-- @param name The name of the value to increment. Must be a number type.
-- @param amount The amount to increment the value. Optional, default is 1.
function IceBox:increment( name, amount )
local value = self:retrieve( name )
if not value then
value = self:store( name, 0 )
end
if value and type( value ) == "number" then
value = value + ( amount or 1 )
end
self:store( name, value )
self._header.modified = os.time()
if self:isAutomaticSavingEnabled() then
self:save()
end
end
--- Decrements a value in the icebox.
-- @param name The name of the value to decrement. Must be a number type.
-- @param amount The amount to decrement the value. Optional, default is 1.
function IceBox:decrement( name, amount )
local value = self:retrieve( name )
if not value then
value = self:store( name, 0 )
end
if value and type( value ) == "number" then
value = value - ( amount or 1 )
end
self:store( name, value )
self._header.modified = os.time()
if self:isAutomaticSavingEnabled() then
self:save()
end
end
--- Checks if a value is in the icebox.
-- @param name The name of the value to check for.
-- @return True of the value exists, False otherwise.
function IceBox:hasValue( name )
if name then
if self._items[ name ] ~= nil then
return true
else
return false
end
end
end
--- Removes a value from the icebox.
-- @param name The name of the value to remove.
function IceBox:remove( name )
if name then
if self:hasValue( name ) then
self._items[ name ] = nil
self._header.modified = os.time()
end
end
if self:isAutomaticSavingEnabled() then
self:save()
end
end
--- Returns all items in the icebox.
-- @return The stored items.
function IceBox:getItems()
return self._items
end
-- clears the icebox and sets its value as a copy of a table.
-- @set the table value as items.
function IceBox:setItems(items)
self._items={}
for k,v in pairs(items) do
self._items[k] = v
end
end
--- Returns the header of the icebox.
-- @return The header.
function IceBox:getHeader()
return self._header
end
--- Enables automatic saving. If enabled, the icebox will save everytime an element is added, remove or changed.
function IceBox:enableAutomaticSaving()
self._automaticSavingEnabled = true
end
--- Disables automatic saving.
function IceBox:disableAutomaticSaving()
self._automaticSavingEnabled = false
end
--- Checks if automatic saving is enabled or not.
-- @return True if it is, false if not.
function IceBox:isAutomaticSavingEnabled()
return self._automaticSavingEnabled
end
--- Saves the icebox to disk.
function IceBox:save()
self:delete()
local database = openDatabase( self._databaseName )
if database then
saveDatabase( database, self._items, self._header )
end
end
--- Loads the icebox from disk.
function IceBox:load()
local database = openDatabase( self._databaseName )
if database then
self._items, self._header = loadDatabase( database )
if not self._header or self._header == {} then
self._header = createHeader()
end
end
end
--- Gets the name of the icebox.
function IceBox:getName()
return self._name
end
--- Prints out all items in the icebox.
function IceBox:print()
print( tableToString( self._items ) )
end
--- Bookmarks the current state of the icebox so that it can be restored at any time.
-- @param name The name of the restore point.
function IceBox:setRestorePoint( name )
if name then
local restoreName = createRestorePointName( name, self._databaseName )
local database = openDatabase( restoreName )
if database then
saveDatabase( database, self._items )
end
end
end
--- Restores the state of the icebox from a bookmarked point.
-- @param name The name of the restore point.
function IceBox:restoreToPoint( name )
if name then
local restoreName = createRestorePointName( name, self._databaseName )
local database = openDatabase( restoreName )
if database then
self:clear()
self._items, self._header = loadDatabase( database )
if self:isAutomaticSavingEnabled() then
self:save()
end
end
end
end
--- Deletes a bookmarked point.
-- @param name The name of the restore point.
function IceBox:deleteRestorePoint( name )
if name then
local restoreName = createRestorePointName( name, self._databaseName )
deleteDatabase( restoreName )
end
end
--- Clears all items out of the icebox and saves it.
function IceBox:clear()
self._items = nil
self._items = {}
if self:isAutomaticSavingEnabled() then
self:save()
end
end
--- Deletes the stored icebox from the disk.
function IceBox:delete()
local path = system.pathForFile( self._databaseName, system.DocumentsDirectory )
os.remove( path )
end
--- Clears and deletes the icebox.
function IceBox:destroy()
self:clear()
self:delete()
end
_G.ice = Ice:new()