Syntax: Select all
static int numargs = 0;
static PyObject* pawnbridge_numargs(PyObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":numargs"))
return NULL;
return PyLong_FromLong(numargs);
}
static PyMethodDef PawnBridgeMethods[] = {
{ "numargs", pawnbridge_numargs, METH_VARARGS,
"Return the number of arguments received by the process." },
{ NULL, NULL, 0, NULL }
};
static PyModuleDef PawnBridgeModule = {
PyModuleDef_HEAD_INIT, "pawnbridge", NULL, -1, PawnBridgeMethods,
NULL, NULL, NULL, NULL
};
PyMODINIT_FUNC PyInit_pawnbridge(void)
{
return PyModule_Create(&PawnBridgeModule);
}
/**
* @brief This is called after the initial loading sequence has been processed.
*
* @param error Error message buffer.
* @param maxlength Size of error message buffer.
* @param late Whether or not the module was loaded after map load.
* @return True to succeed loading, false to fail.
*/
bool PawnBridge::SDK_OnLoad(char *error, size_t maxlength, bool late)
{
numargs = 42;
// also doesn't work when I uncomment it
//PyImport_AddModule("pawnbridge");
// test to make sure that dll is loaded; return value is a valid handle (also works when commenting out)
smutils->LogMessage(myself, "LoadLibrary: %d", LoadLibrary("D:\\C++\\Source\\SrcDS\\games\\css\\cstrike\\addons\\source-python\\Python3\\plat-win\\python34.dll"));
// try to add the dll to the sys.path, no effect
PyRun_SimpleString("sys.path.insert(1, r'D:\\C++\\Source\\SrcDS\\games\\css\\cstrike\\addons\\sourcemod\\extensions')");
// print out a simple thing. works.
PyRun_SimpleString("print('lolxd')");
// works. includes also source-python paths
PyRun_SimpleString("print(str(sys.path))");
// try again to add the path. no effect
std::wstring x = Py_GetPath();
x += L";D:\\C++\\Source\\SrcDS\\games\\css\\cstrike\\addons\\sourcemod\\extensions";
Py_SetPath(x.c_str());
// try using this, out of documentation. when removing this, "no module named pawnbridge" appears, otherwise "pawnbridge is no built-in module"
smutils->LogMessage(myself, "PyImport_AppendInittab: %d", PyImport_AppendInittab("pawnbridge", &PyInit_pawnbridge));
// try additionally to call the init func manually, no effect
PyModule_Create(&PawnBridgeModule);
// try to import it inside the dll itself. fails.
PyImport_ImportModule("pawnbridge");
// initialize
Py_Initialize();
// try to output something with source-python. works
PyRun_SimpleString("from core import echo_console; echo_console('loltest');");
return true;
}
Test script:
Syntax: Select all
import pawnbridge
from core import echo_console
print(dir(pawnbridge))
echo_console('This is a test: {:d}'.format(pawnbridge.numargs()))