Skip to content

Commit 042b2e2

Browse files
committed
Added core.get_interface()
1 parent 27b8d5f commit 042b2e2

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

addons/source-python/packages/source-python/core/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
# Source.Python Imports
4040
# Core
4141
from _core import console_message
42+
from _core import get_interface
4243
from _core import SOURCE_ENGINE
4344
from _core import SOURCE_ENGINE_BRANCH
4445

@@ -55,6 +56,7 @@
5556
'SOURCE_ENGINE_BRANCH',
5657
'console_message',
5758
'echo_console',
59+
'get_interface',
5860
)
5961

6062

src/core/modules/core/core.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
// Includes.
3232
//-----------------------------------------------------------------------------
3333
#include "dbg.h"
34+
#include "interface.h"
35+
#include "dynload.h"
3436

3537

3638
//-----------------------------------------------------------------------------
@@ -41,5 +43,24 @@ inline void ConsoleMessage(const char* msg)
4143
ConMsg(msg);
4244
}
4345

46+
inline void* GetInterface(const char* library, const char* interface_name)
47+
{
48+
DLLib* lib = dlLoadLibrary(library);
49+
if (!lib)
50+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to load library '%s'.", library)
51+
52+
CreateInterfaceFn pCreateInterface = (CreateInterfaceFn) dlFindSymbol(lib, CREATEINTERFACE_PROCNAME);
53+
dlFreeLibrary(lib);
54+
55+
if (!pCreateInterface)
56+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to retrieve interface function'%s'.", CREATEINTERFACE_PROCNAME)
57+
58+
int return_code;
59+
void* result = pCreateInterface(interface_name, &return_code);
60+
if (!result)
61+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to find interface '%s'. Return code: %i.", interface_name, return_code)
62+
63+
return result;
64+
}
4465

4566
#endif // _CORE_H

src/core/modules/core/core_wrap.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,11 @@ void export_functions(scope _core)
8888
&ConsoleMessage,
8989
"Output a message to the server console."
9090
);
91+
92+
def(
93+
"get_interface",
94+
&GetInterface,
95+
return_by_value_policy(),
96+
"Retrieve an interface from a library."
97+
);
9198
}

0 commit comments

Comments
 (0)