local g=false if not g then(require(script.Parent.Src.main))(plugin)return end local n=require(script.Parent.Packages.Signal)local K=require(script.Parent.Packages.Maid)local f={}local function k(g)local n=setmetatable({_instance=plugin:CreateToolbar(g);Buttons={},CreateButton=function(g,n,K,f,k)local L=g.Buttons[n]if not L then L=g._instance:CreateButton(n,K,f,k)g.Buttons[n]=L end return L end},{__index=function(g,n)return g._instance[n]end,__newindex=function(g,n,K)g._instance[n]=K end})return n end local L=setmetatable({_instance=plugin,Unloading=n.new(),CreateToolbar=function(g,n)local K=f[n]if not K then K=k(n)f[n]=K end return K end;GetMouse=function(g)return plugin:GetMouse()end},{__index=function(g,n)return plugin[n]end,__newindex=function(g,n,K)plugin[n]=K end})local A=script.Parent.Src local F=script.Parent.Packages local s local x=false local function m()x=true if s then L.Unloading:Fire()s:Destroy()s=nil end x=false end local function R()x=true s=Instance.new("\070\111\108\100\101\114")s.Name="\077\111\099\107\080\108\117\103\105\110"s.Archivable=false;(A:Clone()).Parent=s;(F:Clone()).Parent=s;(require(s.Src.main))(L)x=false end local function i()print("\082\101\108\111\097\100\046\046\046")m()R()end local Z=K.new()Z:GiveTask(m)local H local function b()if not H then H=task.delay(.5,function()H=nil i()end)end end Z:GiveTask(script.Parent.DescendantRemoving:Connect(function(g)if H then task.cancel(H)end end))for g,n in script.Parent:GetDescendants()do Z:GiveTask(n.Changed:Connect(function()if not x then b()end end))end script.Parent.DescendantAdded:Connect(function(g)if not x then b()Z:GiveTask(g.Changed:Connect(function()if not x then b()end end))end end)script.Parent.DescendantRemoving:Connect(function(g)if not x then b()end end)plugin.Unloading:Connect(function()Z:Destroy()end)local l=plugin:CreateToolbar("\082\101\108\111\097\100")local y=l:CreateButton("\082\101\108\111\097\100","\082\101\108\111\097\100","","\082\101\108\111\097\100")y.Click:Connect(i)R()
local DEVELOPMENT = false

-- When not in development, just directly run the plugin
if not DEVELOPMENT then
	require(script.Parent.Src.main)(plugin)
	return
end

-- Otherwise, mock the plugin to allow reloading...

local Signal = require(script.Parent.Packages.Signal)
local Maid = require(script.Parent.Packages.Maid)

local MockToolbars = {}

local function makeMockToolbar(name)
	local mockToolbar = setmetatable({
		_instance = plugin:CreateToolbar(name),
		Buttons = {},
		CreateButton = function(self, id, tooltip, icon, text)
			local button = self.Buttons[id]
			if not button then
				button = self._instance:CreateButton(id, tooltip, icon, text)
				self.Buttons[id] = button
			end
			return button
		end,
	}, {
		__index = function(self, key)
			return self._instance[key]
		end,
		__newindex = function(self, key, value)
			self._instance[key] = value
		end,
	})
	return mockToolbar
end

local MockPlugin = setmetatable({
	_instance = plugin,
	Unloading = Signal.new(),
	CreateToolbar = function(self, name)
		local mockToolbar = MockToolbars[name]
		if not mockToolbar then
			mockToolbar = makeMockToolbar(name)
			MockToolbars[name] = mockToolbar
		end
		return mockToolbar
	end,
	GetMouse = function(self)
		return plugin:GetMouse()
	end,
}, {
	__index = function(self, key)
		return plugin[key]
	end,
	__newindex = function(self, key, value)
		plugin[key] = value
	end,
})

local originalSrc = script.Parent.Src
local originalPackages = script.Parent.Packages
local currentContainer;
local loading = false

local function unload()
	loading = true
	if currentContainer then
		MockPlugin.Unloading:Fire()
		currentContainer:Destroy()
		currentContainer = nil
	end
	loading = false
end

local function load()
	loading = true
	currentContainer = Instance.new("Folder")
	currentContainer.Name = "MockPlugin"
	currentContainer.Archivable = false
	originalSrc:Clone().Parent = currentContainer
	originalPackages:Clone().Parent = currentContainer
	require(currentContainer.Src.main)(MockPlugin)
	loading = false
end

local function reload()
	print("Reload...")
	unload()
	load()
end

local unloadingMaid = Maid.new()
unloadingMaid:GiveTask(unload)

-- Deferred reloading task
local reloadTask;
local function queueReload()
	if not reloadTask then
		reloadTask = task.delay(0.5, function()
			reloadTask = nil
			reload()
		end)
	end
end
unloadingMaid:GiveTask(script.Parent.DescendantRemoving:Connect(function(desc)
	if reloadTask then
		task.cancel(reloadTask)
	end
end))

-- Listen for any descendants being added / removed / changing
for _, desc in script.Parent:GetDescendants() do
	unloadingMaid:GiveTask(desc.Changed:Connect(function()
		if not loading then
			queueReload()
		end
	end))
end
script.Parent.DescendantAdded:Connect(function(desc)
	if not loading then
		queueReload()
		unloadingMaid:GiveTask(desc.Changed:Connect(function()
			if not loading then
				queueReload()
			end
		end))
	end
end)
script.Parent.DescendantRemoving:Connect(function(desc)
	if not loading then
		queueReload()
	end
end)

plugin.Unloading:Connect(function()
	unloadingMaid:Destroy()
end)

local ReloadToolbar = plugin:CreateToolbar("Reload")
local ReloadButton = ReloadToolbar:CreateButton("Reload", "Reload", "", "Reload")
ReloadButton.Click:Connect(reload)

load()