-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeMachine.lua
More file actions
50 lines (42 loc) · 1.06 KB
/
TimeMachine.lua
File metadata and controls
50 lines (42 loc) · 1.06 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
local TimeMachine = {}
local Util = require("Util")
local Debug = require("dbg")
dbg = Debug:new()
dbg.Enabled = false
local mt = {}
function TimeMachine.new(self, l)
local tm = {context={}, save={}}
setmetatable(tm, self)
self.__index = self
if l then
local copyL = Util.copy(l)
for _i, v in pairs(copyL) do
tm.context[_i] = v
end
end
return tm
end
function TimeMachine.current(tm)
local proxy = {}
setmetatable(proxy, proxy)
proxy.__index = function(t, k) return tm.context[k] end
proxy.__newindex = function(t, k, v) return rawset(tm.context, k, v) end
return proxy
end
function TimeMachine.savePoint(tm, label)
label = label or "DEFAULT"
tm.save[label] = Util.copy(tm.context)
end
function TimeMachine.rollBack(tm, label, notRemove)
label = label or "DEFAULT"
local sv = tm.save[label]
if not sv then
return false
end
tm.context = Util.copy(sv)
if not notRemove and label ~= "DEFAULT" then
tm.save[label] = nil
end
return true
end
return TimeMachine