Jump to content

Recommended Posts

I've been trying to learn how to use upvalue hacker lately and now I'm trying to wrap my head around it by actually testing out some codes.

I've already tried two.

local TheSim = GLOBAL.TheSim
local UpvalueHacker = GLOBAL.require("tools/upvaluehacker")

AddPrefabPostInit("world", function(inst)
	local function OnInit(inst)
	    if inst:HasTag("fruitflyfruit") then
		--Rebind Friendly Fruit Fly
		local fruitfly = TheSim:FindFirstEntityWithTag("glommer") or SpawnFriendlyFruitFly(inst) --made it glommer just to see if new fruitfly would follow glom
		if fruitfly ~= nil and
		    fruitfly.components.health ~= nil and
		    not fruitfly.components.health:IsDead() and
		    fruitfly.components.follower.leader ~= inst then
		        fruitfly.components.follower:SetLeader(inst)
		end
	    end
	end
	UpvalueHacker.SetUpvalue(GLOBAL.Prefabs.fruitfly.fruitfn, OnInit, "OnInit")
end)

and

local function GetUpvalue(func, name)
	local debug = GLOBAL.debug
	local i = 1
	while true do
		local n, v = debug.getupvalue(func, i)
		if not n then
			return nil, nil
		end
		if n == name then
			return v, i
		end
		i = i + 1
	end
end

local function SetUpvalue(func, ind, value)
	local debug = GLOBAL.debug
	debug.setupvalue(func, ind, value)
end

local function HackOnInit()
	local friendlyfruitfly_fn = GLOBAL.Prefabs["friendlyfruitfly"].fruitfn

	local OnInit, OnInit_index = GetUpvalue(friendlyfruitfly_fn, "OnInit")

	local old_OnInit = OnInit
	local new_OnInit = function(inst)
		print("Worksss")
	end
	SetUpvalue(friendlyfruitfly_fn, OnInit_index, new_OnInit)
end

AddPrefabPostInit("world", HackOnInit)

Both yields an error regarding friuitfn() from the prefab file being nil, even though lordfruitfly's fn() works just fine.

Here's the latest logs:

Spoiler

[string "../mods/Experimental/modmain.lua"]:7: bad argument #1 to 'getupvalue' (function expected, got nil)
LUA ERROR stack traceback:
        =[C] in function 'getupvalue'
        ../mods/Experimental/modmain.lua(7,1) in function 'GetUpvalue'
        ../mods/Experimental/modmain.lua(26,1)
        =(tail call) ?
        =[C] in function 'xpcall'
        scripts/mods.lua(174,1) in function 'mod'
        scripts/mainfunctions.lua(342,1)
        =[C] in function 'SpawnPrefab'
        scripts/mainfunctions.lua(382,1) in function 'SpawnPrefab'
        scripts/gamelogic.lua(396,1) in function 'PopulateWorld'
        scripts/gamelogic.lua(876,1) in function 'DoInitGame'
    ...
        =[C] in function 'GetPersistentString'
        scripts/saveindex.lua(285,1) in function 'Load'
        scripts/gamelogic.lua(1221,1) in function 'callback'
        scripts/playerprofile.lua(1549,1) in function 'Set'
        scripts/playerprofile.lua(1394,1)
        =[C] in function 'GetPersistentString'
        scripts/playerprofile.lua(1392,1) in function 'Load'
        scripts/gamelogic.lua(1220,1) in main chunk
        =[C] in function 'require'
        scripts/mainfunctions.lua(1313,1)

How come fruitfn() is considered "nil" while fn() from the exact same prefab file works fine??

Turns out fn is the constructor for the prefabs, even for pigman which has normal() instead of fn(). But I still couldnt get the replacement function to override the old. Please help

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...