mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-25 15:16:07 +00:00
Implement rudimentary reifer against new APIs
This commit is contained in:
@@ -29,6 +29,7 @@ function ApiContext.new(url, onMessage)
|
|||||||
serverId = nil,
|
serverId = nil,
|
||||||
connected = false,
|
connected = false,
|
||||||
messageCursor = -1,
|
messageCursor = -1,
|
||||||
|
partitionRoutes = nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
setmetatable(context, ApiContext)
|
setmetatable(context, ApiContext)
|
||||||
@@ -37,7 +38,7 @@ function ApiContext.new(url, onMessage)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function ApiContext:connect()
|
function ApiContext:connect()
|
||||||
return Http.get(self.url)
|
return Http.get(self.url .. "/api/rojo")
|
||||||
:andThen(function(response)
|
:andThen(function(response)
|
||||||
local body = response:json()
|
local body = response:json()
|
||||||
|
|
||||||
@@ -59,6 +60,7 @@ function ApiContext:connect()
|
|||||||
|
|
||||||
self.serverId = body.serverId
|
self.serverId = body.serverId
|
||||||
self.connected = true
|
self.connected = true
|
||||||
|
self.partitionRoutes = body.partitions
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -67,17 +69,17 @@ function ApiContext:readAll()
|
|||||||
return Promise.reject()
|
return Promise.reject()
|
||||||
end
|
end
|
||||||
|
|
||||||
return Http.get(self.url .. "/read_all")
|
return Http.get(self.url .. "/api/read_all")
|
||||||
:andThen(function(response)
|
:andThen(function(response)
|
||||||
local body = response:json()
|
local body = response:json()
|
||||||
|
|
||||||
if body.serverId ~= self.serverId then
|
if body.serverId ~= self.serverId then
|
||||||
return Promise.reject("server changed ID")
|
return Promise.reject("Server changed ID")
|
||||||
end
|
end
|
||||||
|
|
||||||
self.messageCursor = body.messageCursor
|
self.messageCursor = body.messageCursor
|
||||||
|
|
||||||
return body.instances
|
return body
|
||||||
end, function(err)
|
end, function(err)
|
||||||
self.connected = false
|
self.connected = false
|
||||||
|
|
||||||
@@ -90,12 +92,12 @@ function ApiContext:retrieveMessages()
|
|||||||
return Promise.reject()
|
return Promise.reject()
|
||||||
end
|
end
|
||||||
|
|
||||||
return Http.get(self.url .. "/subscribe/" .. self.messageCursor)
|
return Http.get(self.url .. "/api/subscribe/" .. self.messageCursor)
|
||||||
:andThen(function(response)
|
:andThen(function(response)
|
||||||
local body = response:json()
|
local body = response:json()
|
||||||
|
|
||||||
if body.serverId ~= self.serverId then
|
if body.serverId ~= self.serverId then
|
||||||
return Promise.reject("server changed ID")
|
return Promise.reject("Server changed ID")
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, message in ipairs(body.messages) do
|
for _, message in ipairs(body.messages) do
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ HttpError.Error = {
|
|||||||
},
|
},
|
||||||
ConnectFailed = {
|
ConnectFailed = {
|
||||||
message = "Rojo plugin couldn't connect to the Rojo server.\n" ..
|
message = "Rojo plugin couldn't connect to the Rojo server.\n" ..
|
||||||
"Make sure the server is running -- use 'Rojo serve' to run it!",
|
"Make sure the server is running -- use 'rojo serve' to run it!",
|
||||||
},
|
},
|
||||||
Timeout = {
|
Timeout = {
|
||||||
message = "Rojo timed out during a request.",
|
message = "Rojo timed out during a request.",
|
||||||
|
|||||||
@@ -12,45 +12,56 @@ function Session.new()
|
|||||||
setmetatable(self, Session)
|
setmetatable(self, Session)
|
||||||
|
|
||||||
-- TODO: Rewrite all instance tracking logic and implement a real reconciler
|
-- TODO: Rewrite all instance tracking logic and implement a real reconciler
|
||||||
local created = {}
|
local instancesById = {}
|
||||||
created["0"] = game:GetService("ReplicatedFirst")
|
|
||||||
|
local function createFoldersUntil(location, route)
|
||||||
|
for i = 1, #route - 1 do
|
||||||
|
local piece = route[i]
|
||||||
|
|
||||||
|
local child = location:FindFirstChild(piece)
|
||||||
|
|
||||||
|
if child == nil then
|
||||||
|
child = Instance.new("Folder")
|
||||||
|
child.Name = piece
|
||||||
|
child.Parent = location
|
||||||
|
end
|
||||||
|
|
||||||
|
location = child
|
||||||
|
end
|
||||||
|
|
||||||
|
return location
|
||||||
|
end
|
||||||
|
|
||||||
|
local function reify(instancesById, id)
|
||||||
|
local object = instancesById[tostring(id)]
|
||||||
|
local instance = Instance.new(object.className)
|
||||||
|
instance.Name = object.name
|
||||||
|
|
||||||
|
for key, property in pairs(object.properties) do
|
||||||
|
instance[key] = property.value
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, childId in ipairs(object.children) do
|
||||||
|
reify(instancesById, childId).Parent = instance
|
||||||
|
end
|
||||||
|
|
||||||
|
return instance
|
||||||
|
end
|
||||||
|
|
||||||
local api
|
local api
|
||||||
local function readAll()
|
local function readAll()
|
||||||
print("Reading all...")
|
print("Reading all...")
|
||||||
|
|
||||||
return api:readAll()
|
return api:readAll()
|
||||||
:andThen(function(instances)
|
:andThen(function(response)
|
||||||
local visited = {}
|
for partitionName, partitionRoute in pairs(api.partitionRoutes) do
|
||||||
for id, instance in pairs(instances) do
|
local parent = createFoldersUntil(game, partitionRoute)
|
||||||
visited[id] = true
|
|
||||||
if id ~= "0" then
|
|
||||||
local existing = created[id]
|
|
||||||
if existing ~= nil then
|
|
||||||
pcall(existing.Destroy, existing)
|
|
||||||
end
|
|
||||||
|
|
||||||
local real = Instance.new(instance.className)
|
local rootInstanceId = response.partitionInstances[partitionName]
|
||||||
real.Name = instance.name
|
|
||||||
|
|
||||||
for key, value in pairs(instance.properties) do
|
print("Root for", partitionName, "is", rootInstanceId)
|
||||||
real[key] = value
|
|
||||||
end
|
|
||||||
|
|
||||||
created[id] = real
|
reify(response.instances, rootInstanceId).Parent = parent
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
for id, instance in pairs(instances) do
|
|
||||||
if id ~= "0" then
|
|
||||||
created[id].Parent = created[tostring(instance.parent)]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
for id, object in pairs(created) do
|
|
||||||
if not visited[id] then
|
|
||||||
object:Destroy()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
@@ -58,7 +69,7 @@ function Session.new()
|
|||||||
api = ApiContext.new(REMOTE_URL, function(message)
|
api = ApiContext.new(REMOTE_URL, function(message)
|
||||||
if message.type == "InstanceChanged" then
|
if message.type == "InstanceChanged" then
|
||||||
print("Instance", message.id, "changed!")
|
print("Instance", message.id, "changed!")
|
||||||
readAll()
|
-- readAll()
|
||||||
else
|
else
|
||||||
warn("Unknown message type " .. message.type)
|
warn("Unknown message type " .. message.type)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user