-- Compiled with roblox-ts v3.0.0 local TS = _G[script] local HotReloader = TS.import(script, TS.getModule(script, "@rbxts", "rewire").out).HotReloader local PlanckRewirePlugin do PlanckRewirePlugin = setmetatable({}, { __tostring = function() return "PlanckRewirePlugin" end, }) PlanckRewirePlugin.__index = PlanckRewirePlugin function PlanckRewirePlugin.new(...) local self = setmetatable({}, PlanckRewirePlugin) return self:constructor(...) or self end function PlanckRewirePlugin:constructor(folders) self.moduleToSystem = {} self.hotReloader = HotReloader.new() self.folders = folders self.moduleToSystem = {} end function PlanckRewirePlugin:reloadSystem(system) local _arg0 = self.context ~= nil assert(_arg0, "Cannot reload a system outside of a reloading context") local name = system.name local oldSystem = self:getSystemByName(name) if oldSystem == nil then -- This is a new system local _newSystems = self.context.newSystems local _system = system table.insert(_newSystems, _system) end if oldSystem ~= nil then -- This is an existing system local _result = self.schedular if _result ~= nil then _result:removeSystem(system.system) end local _result_1 = self.schedular if _result_1 ~= nil then _result_1:replaceSystem(oldSystem.system, system.system) end local _newSystems = self.context.newSystems local _system = system table.insert(_newSystems, _system) self:unmarkForCleanup(oldSystem) end end function PlanckRewirePlugin:unmarkForCleanup(system) local _arg0 = self.context ~= nil assert(_arg0, "Cannot unmark a system for cleanup outside of a reloading context") local module = self.context.originalModule local systems = self.moduleToSystem[module] if systems == nil then return nil end local _moduleToSystem = self.moduleToSystem -- ▼ ReadonlyArray.filter ▼ local _newValue = {} local _callback = function(s) return s ~= system end local _length = 0 for _k, _v in systems do if _callback(_v, _k - 1, systems) == true then _length += 1 _newValue[_length] = _v end end -- ▲ ReadonlyArray.filter ▲ _moduleToSystem[module] = _newValue end function PlanckRewirePlugin:cleanupModule(module) local _moduleToSystem = self.moduleToSystem local _module = module local systems = _moduleToSystem[_module] if systems == nil then return nil end -- ▼ ReadonlyArray.forEach ▼ local _callback = function(system) local _result = self.schedular if _result ~= nil then _result:removeSystem(system.system) end end for _k, _v in systems do _callback(_v, _k - 1, systems) end -- ▲ ReadonlyArray.forEach ▲ end function PlanckRewirePlugin:getSystemByName(name) -- Ensure we are in a reloading context if self.context == nil then return nil end -- Find the system in the original module local og = self.context.originalModule local systems = self.moduleToSystem[og] if systems == nil then return nil end -- ▼ ReadonlyArray.find ▼ local _callback = function(system) return system.name == name end local _result for _i, _v in systems do if _callback(_v, _i - 1, systems) == true then _result = _v break end end -- ▲ ReadonlyArray.find ▲ local system = _result return system end function PlanckRewirePlugin:build(schedular) self.schedular = schedular schedular:_addHook(schedular.Hooks.SystemAdd, function(info) if self.context == nil then return nil end if self.context.isReloading then self:reloadSystem(info.system) end if not self.context.isReloading then local _newSystems = self.context.newSystems local _system = info.system table.insert(_newSystems, _system) end end) -- Load all the modules local _exp = self.folders -- ▼ ReadonlyArray.forEach ▼ local _callback = function(folder) self.hotReloader:scan(folder, function(module, context) -- Set the current module self.context = { originalModule = context.originalModule, newSystems = {}, isReloading = context.isReloading, } pcall(function() require(module) end) self:cleanupModule(module) local _moduleToSystem = self.moduleToSystem local _originalModule = context.originalModule local _newSystems = self.context.newSystems _moduleToSystem[_originalModule] = _newSystems self.context = nil end, function(module, context) if context.isReloading then return nil end self:cleanupModule(context.originalModule) local _moduleToSystem = self.moduleToSystem local _originalModule = context.originalModule _moduleToSystem[_originalModule] = nil end) end for _k, _v in _exp do _callback(_v, _k - 1, _exp) end -- ▲ ReadonlyArray.forEach ▲ end end return PlanckRewirePlugin