Re: res/scripts not containing pyc files

Published by picknicker on

#114259
picknicker
Participant

Regarding other mods: Depends what you wanna do, but since you are here I guess you are interested in the “less legal mods”. XVM is open source as well, so you might wanna take a look there. I was recently really surprised how they solved this XMQP interface (if activated, you actually send minimap/sixth sense data via their server using the pika protocol): https://bitbucket.org/XVM/xvm/src/f19902a448ddb0f1887cd7939ea9dac9b57ce7eb/src/xpm/xvm_battle/xmqp.py?at=default&fileviewer=file-view-default

 

Hooking:

Now this is some code I once grabbed from some other mod (I don’t know anymore which one it was so sorry to the original author):

class HookLib(object):
    ORIGIN_BEFORE = 0
    ORIGIN_INSIDE = 1
    HOOK_BEFORE = 2
    CALL_DEFAULT = ORIGIN_BEFORE

    def __init__(self, origin, hook, type = CALL_DEFAULT, active = True):
        if not isinstance(hook, (types.FunctionType, types.LambdaType)):
            raise TypeError('Hook must be function or lambda')
        self.__name__ = hook.__name__
        self.origin = origin
        self.hook = hook
        self.type = type
        self.active = active

    def __call__(self, *args, **kwargs):
        if self.active and self.type == self.ORIGIN_BEFORE:
            result = self.origin(*args, **kwargs)
            self.hook(*args, **kwargs)
        elif self.active and self.type == self.ORIGIN_INSIDE:
            result = self.hook(self.origin, *args, **kwargs)
        elif self.active and self.type == self.HOOK_BEFORE:
            self.hook(*args, **kwargs)
            result = self.origin(*args, **kwargs)
        else:
            result = self.origin(*args, **kwargs)
        return result


    def __get__(self, instance, type = None):
        return types.MethodType(self, instance, type)

    @classmethod
    def makeMethodHook(cls, target, method, hook, type = CALL_DEFAULT, active = True):
        origin = getattr(target, method).__func__
        override = cls(origin, hook, type, active)
        if isinstance(target, (types.TypeType, types.ClassType)):
            setattr(target, method, override)
        else:
            setattr(target, method, override.__get__(target, types.TypeType(target)))
        return hook

    @classmethod
    def methodHook(cls, target, method, type = CALL_DEFAULT, active = True):
        return functools.partial(cls.makeMethodHook, target, method, type=type, active=active)

 

Now in order to actually hook a function like e.g. PlayerAvatar.showVehicleDamageInfo

*cough* autofireextinguisher anyone? *cough*

@HookLib.methodHook(PlayerAvatar, 'showVehicleDamageInfo', HookLib.HOOK_BEFORE)
def showVehicleDamageInfo(self, vehicleID, damageIndex, extraIndex, entityID, equipmentID):
	#do sommething before the actual function is called

Now i wrote the last bit from my head, so it might not compile immediately, but I guess you get the point.