--[[
Methods that are easier to define in Lua

22 Apr 08	drd		Created
--]]

if ghf == nil then
	ghf = {}
end
if ghf.GameNode == nil then
	ghf.GameNode = {}
	local meta = setmetatable(ghf.GameNode, {})
	function meta.__index(t, k)
		local g = rawget(t, "__getters")
		if g and g[k] then
			return g[k](k)
		else
			g = rawget(meta, "__getters")
			if g and g[k] then
				return g[k](k)
			end
		end
	end
	function meta.__newindex(t, k, v)
		local s = rawget(t, "__setters")
		if s and s[k] then
			return s[k](k, v)
		else
			s = rawget(meta, "__setters")
			if s and s[k] then
				s[k](k, v)
			else
				rawset(t, k, v)
			end
		end
	end
end

function setupAccessors(t)
	local meta = {}
	local old_meta = getmetatable(t)
	local old_index, old_newindex
	if old_meta then
		for k,v in pairs(old_meta) do
			meta[k] = v
		end
		old_index = old_meta.__index
		old_newindex = old_meta.__newindex
	end
	if type(old_index) == "function" then
		function meta.__index(t, k)
			local g = rawget(t, "__getters")
			if g and g[k] then
				return g[k](k)
			else
				g = rawget(meta, "__getters")
				if g and g[k] then
					return g[k](k)
				else
					return old_index(t, k)
				end
			end
		end
	elseif type(old_index) == "table" then
		function meta.__index(t, k)
			local g = rawget(t, "__getters")
			if g and g[k] then
				return g[k](k)
			else
				g = rawget(meta, "__getters")
				if g and g[k] then
					return g[k](k)
				else
					return old_index[k]
				end
			end
		end
	else
		function meta.__index(t, k)
			local g = rawget(t, "__getters")
			if g and g[k] then
				return g[k](k)
			else
				g = rawget(meta, "__getters")
				if g and g[k] then
					return g[k](k)
				end
			end
		end
	end

	if old_newindex then
		function meta.__newindex(t, k, v)
			local s = rawget(t, "__setters")
			if s and s[k] then
				return s[k](k, v)
			else
				s = rawget(meta, "__setters")
				if s and s[k] then
					s[k](k, v)
				else
					old_newindex(t, k, v)
				end
			end
		end
	else
		function meta.__newindex(t, k, v)
			local s = rawget(t, "__setters")
			if s and s[k] then
				return s[k](k, v)
			else
				s = rawget(meta, "__setters")
				if s and s[k] then
					s[k](k, v)
				else
					rawset(t, k, v)
				end
			end
		end
	end
	
	setmetatable(t, meta)
end
