local A=(require(script.Parent.Parent:WaitForChild("\078\101\120\117\115\080\108\117\103\105\110\070\114\097\109\101\119\111\114\107\080\114\111\106\101\099\116"))):GetContext(script)local b=A:GetResource("\078\101\120\117\115\073\110\115\116\097\110\099\101\046\078\101\120\117\115\073\110\115\116\097\110\099\101")local O=b:Extend()O:SetClassName("\079\118\101\114\114\105\100\097\098\108\101\073\110\100\101\120\073\110\115\116\097\110\099\101")A:SetContextResource(O)function O.__new(A)A:InitializeSuper()local b=getmetatable(A.object)local O=b.__newindex local w=A.object.__setindex b.__newindex=function(b,X,D)if w then D=w(A.object,X,D)end O(A.object,X,D)end end function O.__createindexmethod(X,A,O,w)if not w then w=O end local D=b:__createindexmethod(A,O,w)local E=w.__getindex return function(b,O)if O=="\066\097\115\101\073\110\100\101\120\077\101\116\097\077\101\116\104\111\100"then return D end local w=D(X,O)if E and O~="\115\117\112\101\114"then local b,X=E(A,O,w)if X==true or b~=nil then return b end end return w end end function O.__rawget(b,A)return b.BaseIndexMetaMethod(b,A)end function O.__getindex(O,A,b)return nil,false end function O.__setindex(O,A,b)return b end return O
--[[
TheNexusAvenger

Extends Nexus Instance to allow lower level overriding of indexing.
--]]

local NexusPluginFramework = require(script.Parent.Parent:WaitForChild("NexusPluginFrameworkProject")):GetContext(script)
local NexusInstance = NexusPluginFramework:GetResource("NexusInstance.NexusInstance")

local OverridableIndexInstance = NexusInstance:Extend()
OverridableIndexInstance:SetClassName("OverridableIndexInstance")
NexusPluginFramework:SetContextResource(OverridableIndexInstance)



--[[
Creates an instance of a Nexus Instance.
--]]
function OverridableIndexInstance:__new()
	--Set up the base object.
	self:InitializeSuper()
	
	--Get the base index method.
	local Metatable = getmetatable(self.object)
	local ExistingNewIndex = Metatable.__newindex
	
	--Get the new index method.
	local CustomNewIndexFunction = self.object["__setindex"]
	
	--Set up custom new indexing.
	Metatable.__newindex = function(_,Index,NewValue)
		--Get the new value.
		if CustomNewIndexFunction then
			NewValue = CustomNewIndexFunction(self.object,Index,NewValue)
		end
		
		--Set the value.
		ExistingNewIndex(self.object,Index,NewValue)
	end
end

--[[
Creates an __index metamethod for an object.
--]]
function OverridableIndexInstance:__createindexmethod(Object,Class,RootClass)
	if not RootClass then RootClass = Class end
	
	--Get the base method.
	local BaseIndexMethod = NexusInstance:__createindexmethod(Object,Class,RootClass)
	local CustomIndexFunction = RootClass.__getindex
	
	--Return a wrapped method.
	return function(MethodObject,Index)
		--Return the base meta method.
		if Index == "BaseIndexMetaMethod" then
			return BaseIndexMethod
		end
		
		--Return a custom index.
		local BaseIndex = BaseIndexMethod(self,Index)
		if CustomIndexFunction and Index ~= "super" then
			local CustomIndex,ForceNil = CustomIndexFunction(Object,Index,BaseIndex)
			if ForceNil == true or CustomIndex ~= nil then
				return CustomIndex
			end
		end
		
		--Return the base index.
		return BaseIndex
	end
end

--[[
Returns the raw index of the object (bypasses __getindex).
--]]
function OverridableIndexInstance:__rawget(Index)
	return self.BaseIndexMetaMethod(self,Index)
end

--[[
Returns the value for a custom index. If the second
value returned is true, it will force return the
returned value, even if it is nil. If not, regular
indexing will be used.
--]]
function OverridableIndexInstance:__getindex(IndexName,OriginalReturn)
	return nil,false
end

--[[
Returns the value for an index to be set. This is
run before the value of the object is set.
--]]
function OverridableIndexInstance:__setindex(IndexName,NewValue)
	return NewValue
end



return OverridableIndexInstance