允许用户“扩展” API函数 [英] allow users to "extend" API functions

查看:90
本文介绍了允许用户“扩展” API函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的游戏编写一个modding API,我想让用户告诉游戏引擎在API函数之前或之后立即运行他们的mod函数,换句话说就是扩展这些函数。 / p>

在此之前,mods不得不重写函数来为其添加功能,这意味着要挖掘游戏的源代码,我认为这很吸引人。



现在我正在考虑这样的事情:

  def moddersFunction():
传递

myAPI.extendFunction('functionName','before-after-after',moddersFunction,extraArgs = [])

现在怎样才能实现这一目标?
换句话说,如果你写它, myAPI.extendFunction 的内部看起来会是什么样子?



我正在考虑创建一个myAPI.extendFunction将添加函数的字典,如果它已被设置(注册),我的API中的函数将检查并运行mod的函数。
但是这意味着添加代码来检查我的modding API中的每一个函数中的字典,我想允许它被扩展(即使它只是一个单独的函数调用,它会自动执行检查和函数调用,看起来好像我很想知道是否有一些简洁的Python技巧,它可以让这些扩展的功能没有屠杀(也许我夸大其词)我的游戏API的现有源代码。

解决方案

我会按照以下方式做一些事情:

  class hookable(object):
def __init __(self,fn):
self.pre = []
self。 post = []
self.fn = fn
def add_pre(self,hook):
self.pre.append(hook)
def add_post(self,hook):
self.post.append(钩子)
def __call __(self,* args,** kwargs):
用于钩子在self.pre中:
钩子(* args,** kwargs)
ret = self.fn(* args,** kwargs)
作为self.post中的钩子:
钩子(* args,** kwargs)
返回ret

任何可扩展函数现在都可以这样装饰:

  @hookable 
def square(x):
return x ** 2

现在您可以使用 square.add_pre() square.add_post()来注册在每次调用之前和之后自动调用的函数 square()

  print square(2)

def pre(x):print'pre',x
def post(x):print'post',x

square.add_pre(pre)
印刷广场(2)
印刷广场(3)

square.add_post(后)
印刷广场(4)
印刷广场(5)


I'm writing a modding API for my game and I want to allow users to tell the game engine to run their mod's function immediately before or after one of the API functions, in other words to "extend" the functions.

Before now modders had to rewrite the function to add functionality to it which meant digging in the game's sourcecode, which I think sucks for them.

Right now I'm thinking of something like this:

def moddersFunction():
    pass

myAPI.extendFunction('functionName', 'before-or-after', moddersFunction, extraArgs=[])

Now what will be comparably clean way to implement this? In other words, what would the internals of myAPI.extendFunction look like if you were to write it?

I'm thinking of having a dictionary which myAPI.extendFunction will add functions to and the functions in my API will check and run the mod's function if it has been set ("registered"). But this means adding the code which checks the dictionary in every single function in my modding API which I want to allow to be extended (even if it's just a single function call which does the check and function calling itself, it seems to much itself).

I'm wondering if there's some neat Python trick which will allow such "extending" of functions without "butchering" (maybe I'm exaggerating) the existing sourcecode of my game's API.

解决方案

I would do something along the following lines:

class hookable(object):
  def __init__(self, fn):
    self.pre = []
    self.post = []
    self.fn = fn
  def add_pre(self, hook):
    self.pre.append(hook)
  def add_post(self, hook):
    self.post.append(hook)
  def __call__(self, *args, **kwargs):
    for hook in self.pre:
      hook(*args, **kwargs)
    ret = self.fn(*args, **kwargs)
    for hook in self.post:
      hook(*args, **kwargs)
    return ret

Any "extendable" function can now be decorated like so:

@hookable
def square(x):
  return x**2

Now you can use square.add_pre() and square.add_post() to register functions that would automatically get called before and after each call to square():

print square(2)

def pre(x): print 'pre', x
def post(x): print 'post', x

square.add_pre(pre)
print square(2)
print square(3)

square.add_post(post)
print square(4)
print square(5)

这篇关于允许用户“扩展” API函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆