-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsnapshot.lua
More file actions
115 lines (101 loc) · 2.87 KB
/
snapshot.lua
File metadata and controls
115 lines (101 loc) · 2.87 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
function Common:startSnapshot()
self.lastSaveAttemptTime = nil
self.lastSuccessfulSaveData = nil
end
function Common:setLastSuccessfulSaveSnapshot(snapshot)
self.lastSuccessfulSaveData = cjson.encode(
{
snapshot = snapshot
}
)
end
function Common:restoreSnapshot(snapshot)
self:callHandlers("clearScene")
-- Clear existing library entries
for entryId, entry in pairs(self.library) do
if not entry.isCore then
self:send("removeLibraryEntry", entryId)
end
end
-- Clear existing actors
for actorId in pairs(self.actors) do
self:send("removeActor", self.clientId, actorId)
end
if snapshot then
-- Add new library entries
for entryId, entry in pairs(snapshot.library or {}) do
self:send("addLibraryEntry", entryId, entry)
end
-- Add new actors
for _, actorSp in pairs(snapshot.actors or {}) do
self:sendAddActor(
actorSp.bp,
{
actorId = actorSp.actorId,
parentEntryId = actorSp.parentEntryId
}
)
end
end
end
function Common:createSnapshot()
snapshot = {}
-- Snapshot non-core library entries
snapshot.library = {}
for entryId, entry in pairs(self.library) do
if not entry.isCore then
snapshot.library[entryId] = entry
end
end
-- Snapshot actors in draw order
snapshot.actors = {}
self:forEachActorByDrawOrder(
function(actor)
local actorBp = self:blueprintActor(actor.actorId)
table.insert(
snapshot.actors,
{
actorId = actor.actorId,
parentEntryId = actor.parentEntryId,
bp = actorBp
}
)
end
)
return snapshot
end
function Common:saveScene(snapshot)
if not self.performing then
self.lastSaveAttemptTime = love.timer.getTime()
local data =
cjson.encode(
{
snapshot = snapshot or self:createSnapshot()
}
)
if data ~= self.lastSuccessfulSaveData then
if next(self.actors) then
pcall(
function()
--writeBackup(data)
end
)
end
jsEvents.send(
"GHOST_MESSAGE",
{
messageType = "UPDATE_SCENE",
data = data
}
)
self.lastSuccessfulSaveData = data
end
end
end
function Common:updateAutoSaveScene()
if not self.performing then
if not self.lastSaveAttemptTime or love.timer.getTime() - self.lastSaveAttemptTime > 1 then
self:saveScene()
end
end
end