Refactor PatchTree and applyPatch for improved readability and logic consistency

- Restructured property change checks in PatchTree.build to enhance clarity and maintainability.
- Updated applyPatch to improve the handling of instance retrieval logic for removed instances.
- Simplified the findDuplicateNames function in diff.lua for better readability and consistency in handling duplicate names.
This commit is contained in:
Joe 2026-01-20 23:05:16 -06:00
parent d21331cb65
commit cd05690d8a
3 changed files with 38 additions and 36 deletions

View file

@ -257,40 +257,40 @@ function PatchTree.build(patch, instanceMap, changeListHeaders)
addProp("Name", instance.Name, change.changedName) addProp("Name", instance.Name, change.changedName)
end end
for prop, incoming in change.changedProperties do for prop, incoming in change.changedProperties do
local incomingSuccess, incomingValue = decodeValue(incoming, instanceMap) local incomingSuccess, incomingValue = decodeValue(incoming, instanceMap)
local currentSuccess, currentValue = getProperty(instance, prop) local currentSuccess, currentValue = getProperty(instance, prop)
-- If incoming is a raw number and current is an EnumItem, convert incoming -- If incoming is a raw number and current is an EnumItem, convert incoming
-- to the proper EnumItem for better display (server sends Enum as raw numbers) -- to the proper EnumItem for better display (server sends Enum as raw numbers)
if if
incomingSuccess incomingSuccess
and currentSuccess and currentSuccess
and typeof(incomingValue) == "number" and typeof(incomingValue) == "number"
and typeof(currentValue) == "EnumItem" and typeof(currentValue) == "EnumItem"
then then
local enumType = currentValue.EnumType local enumType = currentValue.EnumType
local ok, converted = pcall(function() local ok, converted = pcall(function()
return enumType:FromValue(incomingValue) return enumType:FromValue(incomingValue)
end) end)
if ok and converted then if ok and converted then
incomingValue = converted incomingValue = converted
end
end end
end
-- Check if this is a Source property - count line differences -- Check if this is a Source property - count line differences
if prop == "Source" and currentSuccess and incomingSuccess then if prop == "Source" and currentSuccess and incomingSuccess then
hasSourceChange = true hasSourceChange = true
totalLineChanges, whitespaceLineChanges = totalLineChanges, whitespaceLineChanges =
WhitespaceUtil.CountLineDifferences(currentValue, incomingValue) WhitespaceUtil.CountLineDifferences(currentValue, incomingValue)
end end
addProp( addProp(
prop, prop,
if currentSuccess then currentValue else "[Error]", if currentSuccess then currentValue else "[Error]",
if incomingSuccess then incomingValue else select(2, next(incoming)) if incomingSuccess then incomingValue else select(2, next(incoming))
) )
end end
-- Count non-Source property changes -- Count non-Source property changes
local propCount = changeIndex - (if hasSourceChange then 1 else 0) local propCount = changeIndex - (if hasSourceChange then 1 else 0)

View file

@ -29,7 +29,9 @@ local function applyPatch(instanceMap, patch)
for _, removedIdOrInstance in ipairs(patch.removed) do for _, removedIdOrInstance in ipairs(patch.removed) do
-- Skip auto-created instances that shouldn't be synced -- Skip auto-created instances that shouldn't be synced
local instance = if Types.RbxId(removedIdOrInstance) then instanceMap.fromIds[removedIdOrInstance] else removedIdOrInstance local instance = if Types.RbxId(removedIdOrInstance)
then instanceMap.fromIds[removedIdOrInstance]
else removedIdOrInstance
if instance and Config.ignoredClassNames[instance.ClassName] then if instance and Config.ignoredClassNames[instance.ClassName] then
continue continue
end end

View file

@ -17,7 +17,10 @@ local function isEmpty(table)
end end
-- Finds duplicate names among children and returns a set of names that are duplicated -- Finds duplicate names among children and returns a set of names that are duplicated
local function findDuplicateNames(children: { Instance } | { string }, virtualInstances: { [string]: any }?): { [string]: boolean } local function findDuplicateNames(
children: { Instance } | { string },
virtualInstances: { [string]: any }?
): { [string]: boolean }
local nameCounts: { [string]: number } = {} local nameCounts: { [string]: number } = {}
local duplicates: { [string]: boolean } = {} local duplicates: { [string]: boolean } = {}
@ -411,10 +414,7 @@ local function diff(instanceMap, virtualInstances, rootId)
-- Log count of skipped duplicates -- Log count of skipped duplicates
if skippedDuplicateCount > 0 then if skippedDuplicateCount > 0 then
Log.warn( Log.warn("Skipped {} location(s) with duplicate-named siblings (cannot reliably sync)", skippedDuplicateCount)
"Skipped {} location(s) with duplicate-named siblings (cannot reliably sync)",
skippedDuplicateCount
)
end end
if not diffSuccess then if not diffSuccess then