Question on editing mod/resolvers.lua using superload

All development conversation and discussion takes place here

Moderator: Moderator

Post Reply
Message
Author
wuxiangjinxing
Wayist
Posts: 29
Joined: Fri May 16, 2014 6:30 pm

Question on editing mod/resolvers.lua using superload

#1 Post by wuxiangjinxing »

I was trying to edit one function in mod/resolvers.lua. The function was named as:

function resolvers.calc.moddable_tile(t, e) -- located in line 545 of that file.

So what I did was I make a new file with the same name and directory under superload and did the following:

local _M = loadPrevious(...)

function _M:resolvers.calc.moddable_tile(t, e)

-- Here I copied the whole original function and made some very simple modifications. Just changing some strings, actually.

end

return _M

However this did not work at all. The game launched just as the plugin was not activated. I also checked all possible reasons that I could think of, such as superload = false. But I cannot find out how to solve this problem. Can any of you tell me what's wrong? Thanks a lot.

ghostbuster
Uruivellas
Posts: 617
Joined: Mon Oct 09, 2006 7:47 pm

Re: Question on editing mod/resolvers.lua using superload

#2 Post by ghostbuster »

Most mod/engines files have the following structure

require(...)
function _M:foo(...)
function _M:bar(...)

In that case, il you use the usual superload mechanism

local _M = loadPrevious(...)
-- it defines the local _M array and includes the original file.
-- As a result, all the _M:whatever function/var will be part of the local _M array
-- you can then do all you want including redefining function already defined
local old_foo = _M.foo
function _M:foo (....)
do something
local ret = old_foo()
etc
end
return _M

At the end, you send the array to the superload mechanism and its puts back the content of _M to its normal scope.

But, if functions in the superloaded file are not defined as part of an array, the following mechanism does not work.

In mod/resolvers.lua functions are
function resolvers.equip(t)
not
function _M:resolvers.equip(t)

So defining a local _M array before (super)loading the file is useless, and defining functions in _M even more.
All your mods will be completely ignored.

I think, what you want could be done

by

local _M = loadPrevious(...) --local _M is useless, but it does not hurt and who knows.... Probably the superload expects an array returned

function resolvers.calc.moddable_tile(t, e)
-- do whatever you want
end

return _M

As resolvers.calc.modable is not defined as _M:resolvers.calc...it will be directly redefined and it should work.

But beware.
A double load of the file may have unexpected results
And this is not good practice to directly redefine a function. If another addon does the same thing, its modifications (or yours) will be lost.

wuxiangjinxing
Wayist
Posts: 29
Joined: Fri May 16, 2014 6:30 pm

Re: Question on editing mod/resolvers.lua using superload

#3 Post by wuxiangjinxing »

Thank you for your detailed analysis. However even I removed the _M: just as you said, it still did not work at all.

I may have to use overload instead, though I really hate overload, LOL.
ghostbuster wrote:Most mod/engines files have the following structure

require(...)
function _M:foo(...)
function _M:bar(...)

In that case, il you use the usual superload mechanism

local _M = loadPrevious(...)
-- it defines the local _M array and includes the original file.
-- As a result, all the _M:whatever function/var will be part of the local _M array
-- you can then do all you want including redefining function already defined
local old_foo = _M.foo
function _M:foo (....)
do something
local ret = old_foo()
etc
end
return _M

At the end, you send the array to the superload mechanism and its puts back the content of _M to its normal scope.

But, if functions in the superloaded file are not defined as part of an array, the following mechanism does not work.

In mod/resolvers.lua functions are
function resolvers.equip(t)
not
function _M:resolvers.equip(t)

So defining a local _M array before (super)loading the file is useless, and defining functions in _M even more.
All your mods will be completely ignored.

I think, what you want could be done

by

local _M = loadPrevious(...) --local _M is useless, but it does not hurt and who knows.... Probably the superload expects an array returned

function resolvers.calc.moddable_tile(t, e)
-- do whatever you want
end

return _M

As resolvers.calc.modable is not defined as _M:resolvers.calc...it will be directly redefined and it should work.

But beware.
A double load of the file may have unexpected results
And this is not good practice to directly redefine a function. If another addon does the same thing, its modifications (or yours) will be lost.

HousePet
Perspiring Physicist
Posts: 6215
Joined: Sun Sep 09, 2012 7:43 am

Re: Question on editing mod/resolvers.lua using superload

#4 Post by HousePet »

Unless I'm really confused (which does happen), I think your problem here is that mod/resolvers.lua does not exist.
when superloading via superload/mod/... you are superloading a file which is inside the class folder of the module file. Resolvers.lua is not in that folder.

I think I have edited a resolver before, so I'll try to remember to look in my addons when I'm at home for how to do it.
My feedback meter decays into coding. Give me feedback and I make mods.

wuxiangjinxing
Wayist
Posts: 29
Joined: Fri May 16, 2014 6:30 pm

Re: Question on editing mod/resolvers.lua using superload

#5 Post by wuxiangjinxing »

I double checked the tome-1.5.5 module and there is a file tome-1.5.5.team\mod\resolvers.lua, and overloading this file does work so I believe the problem is how to edit the file by superloading it.
HousePet wrote:Unless I'm really confused (which does happen), I think your problem here is that mod/resolvers.lua does not exist.
when superloading via superload/mod/... you are superloading a file which is inside the class folder of the module file. Resolvers.lua is not in that folder.

I think I have edited a resolver before, so I'll try to remember to look in my addons when I'm at home for how to do it.

HousePet
Perspiring Physicist
Posts: 6215
Joined: Sun Sep 09, 2012 7:43 am

Re: Question on editing mod/resolvers.lua using superload

#6 Post by HousePet »

Okay I am confused, and I don't seem to have any code that edits a resolver.

This would be easier if you had pasted your code in this thread, but anyway:

You don't need to superload the file to edit these functions.
You should be able to overwrite them at any time in your code. The best time for "any time" is in your addon's "ToME:load" hook.
By the time that hook is called, all resolvers should have been created but not yet resolved, and they are all stored in a table that can be accessed by just typing resolvers.whichever_on_you_want...

So add in something like:

Code: Select all

resolvers.calc.moddable_tile = function(t, e) 

whatever you want...

end
Note that this is basically overwriting the function, so if anyone else does the same thing there is no protections for allowing both of you to do that safely.
My feedback meter decays into coding. Give me feedback and I make mods.

wuxiangjinxing
Wayist
Posts: 29
Joined: Fri May 16, 2014 6:30 pm

Re: Question on editing mod/resolvers.lua using superload

#7 Post by wuxiangjinxing »

Thank you for your great guidance. Now I'm doing something like:

in hooks/load.lua:

class:bindHook("ToME:load", function(self, data)
function resolvers.calc.moddable_tile(t, e)

--Some modifications

end
end)

This seems to be working fine. But is this exactly what you meant? I just want to make sure that I do not make any mistakes.

HousePet wrote:Okay I am confused, and I don't seem to have any code that edits a resolver.

This would be easier if you had pasted your code in this thread, but anyway:

You don't need to superload the file to edit these functions.
You should be able to overwrite them at any time in your code. The best time for "any time" is in your addon's "ToME:load" hook.
By the time that hook is called, all resolvers should have been created but not yet resolved, and they are all stored in a table that can be accessed by just typing resolvers.whichever_on_you_want...

So add in something like:

Code: Select all

resolvers.calc.moddable_tile = function(t, e) 

whatever you want...

end
Note that this is basically overwriting the function, so if anyone else does the same thing there is no protections for allowing both of you to do that safely.

HousePet
Perspiring Physicist
Posts: 6215
Joined: Sun Sep 09, 2012 7:43 am

Re: Question on editing mod/resolvers.lua using superload

#8 Post by HousePet »

I'm not sure on the exact syntax required for this, so if it works I guess its right.

I was thinking about it after I posted last, and I think we can potentially still do the superload wrapper trick by storing the original resolver function and calling it at the start or end of the new function. This will of course depend on how complex your change to the function is, as to whether you want to go to the effort of making sure that any changes to the base code or from other addons are compatible with your addon.
My feedback meter decays into coding. Give me feedback and I make mods.

Post Reply