-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJournalShared.lua
More file actions
129 lines (116 loc) · 3.56 KB
/
JournalShared.lua
File metadata and controls
129 lines (116 loc) · 3.56 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
local JournalShared = {}
local initialized = false
local DataStore;
local newComment = require(script.Parent.newComment)
local Version = require(script.Parent.Version)
local DataStoreService = game:GetService("DataStoreService")
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
function JournalShared.getJournalDataStore(): DataStore
if not initialized then
local st, err = pcall(function()
DataStore = DataStoreService:GetDataStore("DevCommentJournal", tostring(game.PlaceId))
end)
if not st then
warn(`Access to API services has not been enabled, can't send comments back to place file: {err}`)
end
initialized = true
end
return DataStore
end
JournalShared.MostRecentCheckTimeAttribute = "_devCommentLastChecked"
JournalShared.MostRecentEntryAttribute = "_devCommentMostRecent"
JournalShared.MostRecentEntryIdKey = "MostRecent"
local function getFrame(instance: Instance)
if instance:IsA("PVInstance") then
return instance:GetPivot()
else
local ancestor = instance:FindFirstAncestorWhichIsA("PVInstance")
if ancestor then
return ancestor:GetPivot()
else
return nil
end
end
end
local function findChildWithNameAndOffset(parent: Instance, name: string, expectedOffset: Vector3)
local closestOffset = math.huge
local closestChild = nil
local frame = getFrame(parent)
for _, ch in parent:GetChildren() do
if ch.Name == name and ch:IsA("PVInstance") then
local localOffset = frame:PointToObjectSpace(ch:GetPivot().Position)
local distance = (localOffset - expectedOffset).Magnitude
if distance < closestOffset then
closestOffset = distance
closestChild = ch
end
end
end
return closestChild
end
local function resolvePath(path: table)
local current = workspace
for _, pathPart in path do
if pathPart.p then
local pos = pathPart.p
local decodedLocalPos = Vector3.new(pos[1], pos[2], pos[3])
current = findChildWithNameAndOffset(current, pathPart.n, decodedLocalPos)
else
current = current:FindFirstChild(pathPart.n)
end
if not current then
break
end
end
return current
end
function JournalShared.applyEntry(entry)
local extistingCommentsById = {}
for _, comment in CollectionService:GetTagged("Comment") do
extistingCommentsById[comment:GetAttribute("id")] = comment
end
if Version.DEBUG_JOURNAL then
print(`Consuming journal entry {entry.action}, id {entry.id}`)
end
if entry.action == "added" then
if extistingCommentsById[entry.id] then
if not RunService:IsRunning() then
warn(`Tried to add already existing comment`)
end
return false
end
local comment = newComment(entry.author, entry.id)
comment:SetAttribute("id", entry.id)
comment:SetAttribute("time", entry.time)
if entry.position then
comment:SetAttribute("position",
Vector3.new(entry.position[1], entry.position[2], entry.position[3]))
end
comment.Parent = resolvePath(entry.path)
elseif entry.action == "changed" then
local comment = extistingCommentsById[entry.id]
if comment then
if comment.Name == entry.author and comment.Value == entry.text then
-- No warn because redundant edits are fine
return false
end
comment.Name = entry.author
comment.Value = entry.text
else
warn(`Couldn't find comment with id {entry.id} to update`)
end
elseif entry.action == "deleted" then
local comment = extistingCommentsById[entry.id]
if comment then
comment:Destroy()
else
if not RunService:IsRunning() then
warn(`Couldn't find comment with id {entry.id} to delete`)
end
return false
end
end
return true
end
return JournalShared