local function U(U,Z)if script then local Z=pcall(check,U)if not Z then return end end local f=NodeLookup[U.Parent]if not f then return end local Q={Object=U;Parent=f;Index=0,Expanded=false,Selected=false;Depth=depth(U)}connLookup[U]=Connect(U.AncestryChanged,function(Z,f)if Z==U then if f==nil then removeObject(Z)else moveObject(Z,f)end end end)NodeLookup[U]=Q insert(f,#f+1,Q)if not Z then if nodeIsVisible(Q)then updateList()elseif nodeIsVisible(Q.Parent)then updateScroll()end end end local function Z(U,Z)local f=Instance.new(U.ClassName)for U,Z in pairs(U.Properties)do pcall(function()local U U=ToPropValue(Z.Value,Z.Type)f[Z.Name]=U end)end f.Parent=Z end local function f(U)local Z={ClassName=U.ClassName,Properties={}}for f,Q in pairs(RbxApi.GetProperties(U.className))do if Q.Name~="\080\097\114\101\110\116"then print("\116\104\105\115\112\097\115\115\101\100")table.insert(Z.Properties,{Name=Q.Name,Type=Q.ValueType,Value=tostring(U[Q.Name])})end end return Z end local function Q()local U local Z,f=pcall(function()U=(game:GetObjects("\114\098\120\097\115\115\101\116\058\047\047\068\101\120\083\116\111\114\097\103\101\046\114\098\120\109"))[1]end)if Z and U then for U,Z in pairs(U:GetChildren())do pcall(function()Z.Parent=DexStorageMain end)end end updateDexStorageListeners()end local K=false local G={}local function w()if K then return end K=true wait()pcall(function()saveinstance("\099\111\110\116\101\110\116\047\047\068\101\120\083\116\111\114\097\103\101\046\114\098\120\109",DexStorageMain)end)updateDexStorageListeners()K=false end function updateDexStorageListeners()for U,Z in pairs(G)do Z:Disconnect()end G={}for U,Z in pairs(DexStorageMain:GetChildren())do pcall(function()local U=Z.Changed:connect(w)table.insert(G,U)end)end end do NodeLookup[workspace.Parent]={Object=workspace.Parent,Parent=nil;Index=0,Expanded=true}if DexStorageEnabled then NodeLookup[DexStorage]={Object=DexStorage;Parent=nil,Index=0,Expanded=true}end if NilStorageEnabled then NodeLookup[NilStorage]={Object=NilStorage,Parent=nil;Index=0;Expanded=true}end Connect(game.DescendantAdded,U)Connect(game.DescendantRemoving,removeObject)if DexStorageEnabled then Q()Connect(DexStorage.DescendantAdded,U)Connect(DexStorage.DescendantRemoving,removeObject)Connect(DexStorage.DescendantAdded,w)Connect(DexStorage.DescendantRemoving,w)end if NilStorageEnabled then Connect(NilStorage.DescendantAdded,U)Connect(NilStorage.DescendantRemoving,removeObject)local Z=get_nil_instances()spawn(function()while wait()do if#Z~=#get_nil_instances()then Z=get_nil_instances()for U,Z in pairs(get_nil_instances())do if Z~=NilStorage and Z~=DexStorage then pcall(function()Z.Parent=NilStorageMain end)end end end end end)end local function Z(U)return U:GetChildren()end local function f(Q)local K,G=pcall(Z,Q)if K then for Z=1,#G,1 do U(G[Z],true)f(G[Z])end end end f(workspace.Parent)if DexStorageEnabled then f(DexStorage)end if NilStorageEnabled then f(NilStorage)end scrollBar.VisibleSpace=math.ceil(listFrame.AbsoluteSize.y/ENTRY_BOUND)updateList()end
-- Creates a new tree node from an object. Called when an object starts
-- existing in the game tree.
local function addObject(object, noupdate)
	if script then
		-- protect against naughty RobloxLocked objects
		local s = pcall(check, object)
		if not s then
			return
		end
	end

	local parentNode = NodeLookup[object.Parent]
	if not parentNode then
		return
	end

	local objectNode = {
		Object = object,
		Parent = parentNode,
		Index = 0,
		Expanded = false,
		Selected = false,
		Depth = depth(object),
	}

	connLookup[object] = Connect(object.AncestryChanged, function(c, p)
		if c == object then
			if p == nil then
				removeObject(c)
			else
				moveObject(c, p)
			end
		end
	end)

	NodeLookup[object] = objectNode
	insert(parentNode, #parentNode + 1, objectNode)

	if not noupdate then
		if nodeIsVisible(objectNode) then
			updateList()
		elseif nodeIsVisible(objectNode.Parent) then
			updateScroll()
		end
	end
end

local function makeObject(obj, par)
	local newObject = Instance.new(obj.ClassName)
	for i, v in pairs(obj.Properties) do
		pcall(function()
			local newProp
			newProp = ToPropValue(v.Value, v.Type)
			newObject[v.Name] = newProp
		end)
	end
	newObject.Parent = par
end

local function writeObject(obj)
	local newObject = { ClassName = obj.ClassName, Properties = {} }
	for i, v in pairs(RbxApi.GetProperties(obj.className)) do
		if v["Name"] ~= "Parent" then
			print("thispassed")
			table.insert(
				newObject.Properties,
				{ Name = v["Name"], Type = v["ValueType"], Value = tostring(obj[v["Name"]]) }
			)
		end
	end
	return newObject
end

local function buildDexStorage()
	local localDexStorage

	local success, err = pcall(function()
		localDexStorage = game:GetObjects("rbxasset://DexStorage.rbxm")[1]
	end)

	if success and localDexStorage then
		for i, v in pairs(localDexStorage:GetChildren()) do
			pcall(function()
				v.Parent = DexStorageMain
			end)
		end
	end

	updateDexStorageListeners()
	--[[
	local localDexStorage = readfile(getelysianpath().."DexStorage.txt")--game:GetService("CookiesService"):GetCookieValue("DexStorage")
	--local success,err = pcall(function()
		if localDexStorage then
			local objTable = game:GetService("HttpService"):JSONDecode(localDexStorage)
			for i,v in pairs(objTable) do
				makeObject(v,DexStorageMain)
			end
		end
	--end)
	--]]
end

local dexStorageDebounce = false
local dexStorageListeners = {}

local function updateDexStorage()
	if dexStorageDebounce then
		return
	end
	dexStorageDebounce = true

	wait()

	pcall(function()
		saveinstance("content//DexStorage.rbxm", DexStorageMain)
	end)

	updateDexStorageListeners()

	dexStorageDebounce = false
	--[[
	local success,err = pcall(function()
		local objs = {}
		for i,v in pairs(DexStorageMain:GetChildren()) do
			table.insert(objs,writeObject(v))
		end
		writefile(getelysianpath().."DexStorage.txt",game:GetService("HttpService"):JSONEncode(objs))
		--game:GetService("CookiesService"):SetCookieValue("DexStorage",game:GetService("HttpService"):JSONEncode(objs))
	end)
	if err then
		CreateCaution("DexStorage Save Fail!","DexStorage broke! If you see this message, report to Raspberry Pi!")
	end
	print("hi")
	--]]
end

function updateDexStorageListeners()
	for i, v in pairs(dexStorageListeners) do
		v:Disconnect()
	end
	dexStorageListeners = {}
	for i, v in pairs(DexStorageMain:GetChildren()) do
		pcall(function()
			local ev = v.Changed:connect(updateDexStorage)
			table.insert(dexStorageListeners, ev)
		end)
	end
end

do
	NodeLookup[workspace.Parent] = {
		Object = workspace.Parent,
		Parent = nil,
		Index = 0,
		Expanded = true,
	}

	if DexStorageEnabled then
		NodeLookup[DexStorage] = {
			Object = DexStorage,
			Parent = nil,
			Index = 0,
			Expanded = true,
		}
	end

	if NilStorageEnabled then
		NodeLookup[NilStorage] = {
			Object = NilStorage,
			Parent = nil,
			Index = 0,
			Expanded = true,
		}
	end

	Connect(game.DescendantAdded, addObject)
	Connect(game.DescendantRemoving, removeObject)

	if DexStorageEnabled then
		--[[
		if readfile(getelysianpath().."DexStorage.txt") == nil then
			writefile(getelysianpath().."DexStorage.txt","")
		end
		--]]

		buildDexStorage()

		Connect(DexStorage.DescendantAdded, addObject)
		Connect(DexStorage.DescendantRemoving, removeObject)

		Connect(DexStorage.DescendantAdded, updateDexStorage)
		Connect(DexStorage.DescendantRemoving, updateDexStorage)
	end

	if NilStorageEnabled then
		Connect(NilStorage.DescendantAdded, addObject)
		Connect(NilStorage.DescendantRemoving, removeObject)

		local currentTable = get_nil_instances()

		spawn(function()
			while wait() do
				if #currentTable ~= #get_nil_instances() then
					currentTable = get_nil_instances()
					--NilStorageMain:ClearAllChildren()
					for i, v in pairs(get_nil_instances()) do
						if v ~= NilStorage and v ~= DexStorage then
							pcall(function()
								v.Parent = NilStorageMain
							end)
							--[[
							local newNil = v
							newNil.Archivable = true
							newNil:Clone().Parent = NilStorageMain
							--]]
						end
					end
				end
			end
		end)
	end

	local function get(o)
		return o:GetChildren()
	end

	local function r(o)
		local s, children = pcall(get, o)
		if s then
			for i = 1, #children do
				addObject(children[i], true)
				r(children[i])
			end
		end
	end

	r(workspace.Parent)
	if DexStorageEnabled then
		r(DexStorage)
	end
	if NilStorageEnabled then
		r(NilStorage)
	end

	scrollBar.VisibleSpace = math.ceil(listFrame.AbsoluteSize.y / ENTRY_BOUND)
	updateList()
end
