Functions

Functions in pfp can either be defined natively in python, or in the template script itself.

Native Functions

Two main methods exist to add native python functions to the pfp interpreter:

  1. The @native decorator
  2. The add_native method

Follow the links above for detailed information.

Interpreted Functions

Interpreted functions can declared as you normally would in an 010 template (basically c-style syntax).

Functions are hoisted to the top of the scope they are declared in. E.g. the following script is valid:

HelloWorld(10);

typedef unsigned short custom_short;
void HelloWorld(custom_short arg1) {
    Printf("Hello World, %d", arg1);
}

Functions Reference Documentation

class pfp.functions.Function(return_type, params, scope)[source]

A class to maintain function state and arguments

class pfp.functions.NativeFunction(name, func, ret, send_interp=False)[source]

A class for native functions

class pfp.functions.ParamClsWrapper(param_cls)[source]

This is a temporary wrapper around a param class that can store temporary information, such as byref values

class pfp.functions.ParamList(params)[source]

Used for when a function is actually called. See ParamListDef for how function definitions store function parameter definitions

class pfp.functions.ParamListDef(params, coords)[source]

docstring for ParamList

instantiate(scope, args, interp)[source]

Create a ParamList instance for actual interpretation

Args:TODO
Returns:A ParamList object
pfp.native.native(name, ret, interp=None, send_interp=False)[source]

Used as a decorator to add the decorated function to the pfp interpreter so that it can be used from within scripts.

Parameters:
  • name (str) – The name of the function as it will be exposed in template scripts.
  • ret (pfp.fields.Field) – The return type of the function (a class)
  • interp (pfp.interp.PfpInterp) – The specific interpreter to add the function to
  • send_interp (bool) – If the current interpreter should be passed to the function.

Examples:

The example below defines a Sum function that will return the sum of all parameters passed to the function:

from pfp.fields import PYVAL

@native(name="Sum", ret=pfp.fields.Int64)
def sum_numbers(params, ctxt, scope, stream, coord):
    res = 0
    for param in params:
        res += PYVAL(param)
    return res

The code below is the code for the Int3 function. Notice that it requires that the interpreter be sent as a parameter:

@native(name="Int3", ret=pfp.fields.Void, send_interp=True)
def int3(params, ctxt, scope, stream, coord, interp):
    if interp._no_debug:
        return

    if interp._int3:
        interp.debugger = PfpDbg(interp)
        interp.debugger.cmdloop()