Skip to content

Commit

Permalink
Improve undoing instance removals and updates
Browse files Browse the repository at this point in the history
  • Loading branch information
DervexDev committed Jul 11, 2024
1 parent 2586329 commit 9e68a96
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
### Added

- Experimental support for syncing MeshPart's MeshId
- Argon sync actions can now be undone an unlimited number of times

### Fixed

Expand Down
11 changes: 0 additions & 11 deletions src/Core/.src.luau
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local ChangeHistoryService = game:GetService('ChangeHistoryService')
local ScriptEditorService = game:GetService('ScriptEditorService')

local Argon = script:FindFirstAncestor('Argon')
Expand Down Expand Up @@ -57,16 +56,6 @@ function Core.new(host: string?, port: string?)

self:__handleOpenInEditor(Config:get('OpenInEditor'))

-- handle undo of instance removal
table.insert(
self.connections,
ChangeHistoryService.OnUndo:Connect(function(action: string)
if action:match('^Argon remove') then
self.processor.write:undoLastRemoval()
end
end)
)

-- watch for `OpenInEditor setting
table.insert(
self.connections,
Expand Down
43 changes: 3 additions & 40 deletions src/Core/Processor/Write.luau
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ local Types = require(Argon.Types)
local Config = require(Argon.Config)

local equals = require(Argon.Helpers.equals)
local generateRef = require(Argon.Helpers.generateRef)

local Meta = require(script.Parent.Parent.Meta)
local Error = require(script.Parent.Parent.Error)

local MESH_RERLOAD_PROPERTIES = { 'MeshId', 'InitialSize' }
Expand Down Expand Up @@ -71,7 +69,6 @@ WriteProcessor.__index = WriteProcessor
function WriteProcessor.new(tree)
return setmetatable({
tree = tree,
lastRemovedInstance = nil,
}, WriteProcessor)
end

Expand Down Expand Up @@ -240,8 +237,8 @@ function WriteProcessor:applyUpdate(snapshot: Types.UpdatedSnapshot, initial: bo
end

newInstance.Parent = instance.Parent
instance.Parent = nil

instance:Destroy()
self.tree:updateInstance(snapshot.id, newInstance)

instance = newInstance
Expand Down Expand Up @@ -311,7 +308,7 @@ function WriteProcessor:applyRemoval(object: Types.Ref | Instance)

if typeof(object) == 'Instance' then
self.tree:removeByInstance(object)
object:Destroy()
object.Parent = nil
else
local instance = self.tree:getInstance(object)

Expand All @@ -329,45 +326,11 @@ function WriteProcessor:applyRemoval(object: Types.Ref | Instance)
return
end

self.lastRemovedInstance = {
instance = instance:Clone(),
parent = instance.Parent,
}

self.tree:removeById(object)
instance:Destroy()
instance.Parent = nil
end

setWaypoint('remove')
end

function WriteProcessor:undoLastRemoval()
if not self.lastRemovedInstance then
Log.warn('Argon failed to restore the last removed instance')
return
end

local instance = self.lastRemovedInstance.instance
instance.Parent = self.lastRemovedInstance.parent

self.lastRemovedInstance = nil

if not Config:get('TwoWaySync') then
local function walk(instance)
-- TODO: Restore original meta as well
self.tree:insertInstance(instance, generateRef(true), Meta.new())

for _, child in ipairs(instance:GetChildren()) do
walk(child)
end
end

walk(instance)
end

print('Argon successfully restored this instance, you can ignore the warning above')

setWaypoint('undo')
end

return WriteProcessor

0 comments on commit 9e68a96

Please sign in to comment.