random-patches icon indicating copy to clipboard operation
random-patches copied to clipboard

Metatable change critical bug

Open klen-list opened this issue 3 years ago • 2 comments

If SENT has Base = "base_entity" and has no Use method, calling Use or direct use by the player causes an overflow error on the dedicated server and crash in local hosted game. Example: 3D2DTextscreens image

Temporary solution - https://github.com/Pika-Software/gmod_random_patches/commit/1dde1e6a1d65fc3d3500e615887aa48ad0110a69

klen-list avatar Mar 30 '23 12:03 klen-list

Commentary changed: https://github.com/Pika-Software/gmod_random_patches/commit/0d071c3076f21b29056338a10290edcbcbc9c0db

unknown-gd avatar Mar 30 '23 12:03 unknown-gd

	do
		local _R = debug.getregistry()
		local ENTITY = _R.Entity

		local entTabs = {
			["Entity"] = { __index = ENTITY },
			["Player"] = { __index = setmetatable( table.Copy( _R.Player ), { __index = ENTITY } ) },
			["Weapon"] = { __index = setmetatable( table.Copy( _R.Weapon ), { __index = ENTITY } ) },
			["Vehicle"] = { __index = setmetatable( table.Copy( _R.Vehicle ), { __index = ENTITY } ) },
			["NPC"] = { __index = setmetatable( table.Copy( _R.NPC ), { __index = ENTITY } ) }
		}

		local ownerkey = "Owner"
		local copyKeys = {"MetaID", "MetaName", "__tostring", "__eq", "__concat"}
		local copyKeysLength = #copyKeys

		local function copyMetatable( ent, entTabName )
			if (!entTabs[entTabName]) then return end
			local tab = GetTable[ent]
			setmetatable(tab, entTabs[entTabName])

			local mt = {
				__index = function( self, key )
					if key == ownerkey then return GetOwner(self, key) end
					return tab[key]
				end,
				__newindex = tab,
				__metatable = ENTITY
			}

			local v
			for i = 1, copyKeysLength do
				v = copyKeys[i]
				mt[v] = ENTITY[v]
			end
			v = nil

			debug.setmetatable(ent, mt)
		end

		hook.Add("OnEntityCreated", "turbo.ChangeEntMeta", function(ent)
			timer.Simple(0, function()
				if (IsValid(ent)) then
					copyMetatable(ent, debug.getmetatable(ent).MetaName)
				end
			end)
		end, HOOK_MONITOR_HIGH)
	end

or

https://github.com/swampservers/contrib/blob/master/lua/swamp/sh_meta.lua

Heyter avatar Apr 14 '23 10:04 Heyter

This functionality has been removed and there are currently no plans to bring it back, I am closing this issue.

unknown-gd avatar Jan 09 '25 02:01 unknown-gd