Aufgrund einer Konfigurationsänderung wird die GitLab Registry ab 10 Uhr nur Read Only zur Verfügung stehen. / Due to a configuration change, the GitLab Registry will be available for read-only access from 10am.

vasingletonmethods.hpp 6.32 KB
Newer Older
1 2 3
#include <Python.h>
#include <VANetClient.h>
#include <VACore.h>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
4
#include <VAStruct.h>
5 6 7
#include <VAException.h>
#include <string.h>

8
// If you want to extend the va Python pSelf interface, also add
9 10 11 12
// the function to the va_methods table in vasingleton.cpp - otherwise they will not show up.
// Documentation goes into vasingletondoc.hpp

static IVANetClient* g_pVANetClient = nullptr; //!< Static pointer to VANetClient instance
13
static PyObject* g_pVAError = nullptr; //!< Static pointer to error instance
14

15 16 17 18
// Ugly definitions to ease try-catching VA exceptions
#define VAPY_REQUIRE_CONN_TRY try { RequireCoreAvailable();
#define VAPY_CATCH_RETURN } catch (const CVAException& oError) { PyErr_SetString(PyExc_Exception, oError.ToString().c_str()); return NULL; }

19
//! Helper for API dev
20
static PyObject* va_not_implemented(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames)
21 22 23
{
	VA_EXCEPT_NOT_IMPLEMENTED;
	return NULL;
24
};
25

26 27 28
//! Raises an exception if core is not available
static void RequireCoreAvailable()
{
29 30
	if (!g_pVANetClient)
		VA_EXCEPT2(CVAException::NETWORK_ERROR, "VA client not available, please connect first");
31

32 33 34
	if (!g_pVANetClient->GetCoreInstance())
		VA_EXCEPT2(CVAException::NETWORK_ERROR, "VA client available, but access to VA interface failed. Please reconnect.");
};
35

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
//! Helper to convert recursively from VAStruct to Python dict
PyObject* ConvertVAStructToPythonDict( const CVAStruct& oInStruct )
{
	return PyLong_FromLong( -1 );
};

//! Helper to convert recursively from Python dict to VAStruct
CVAStruct ConvertPythonDictToVAStruct( PyObject* pInDict )
{
	return CVAStruct();
};


// ------------------------------- Python module extension methods

51
static PyObject* va_connect(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames)
52
{
53
	if (!g_pVANetClient)
54 55
		g_pVANetClient = IVANetClient::Create();

56
	if (g_pVANetClient->IsConnected())
57
	{
58
		PyErr_WarnEx(NULL, "Was still connected, forced disconnect.", 1 );
59
		g_pVANetClient->Disconnect();
60 61 62
	}

	static const char * const _keywords[] = { "server", "port", NULL };
63
	static _PyArg_Parser _parser = { "|si:connect", _keywords, 0 };
64
	char* pcServerIP = nullptr;
65 66
	int iServerPort = 12340;

67
	if (!_PyArg_ParseStack(ppArgs, nArgs, pKeywordNames, &_parser, &pcServerIP, &iServerPort))
68
		return NULL;
69

70
	std::string sServerIP = pcServerIP ? std::string(pcServerIP) : "localhost";
71

72 73 74 75
	if (IVANetClient::VA_NO_ERROR == g_pVANetClient->Initialize(sServerIP, iServerPort))
		return PyBool_FromLong(1);

	PyErr_SetString(PyExc_ConnectionError, std::string("Could not connect to " + sServerIP + " on " + std::to_string((long)iServerPort)).c_str());
76
	return NULL;
77
};
78

79
static PyObject* va_disconnect(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames)
80
{
81
	if (g_pVANetClient)
82 83 84 85 86 87 88
	{
		if (g_pVANetClient->IsConnected())
		{
			g_pVANetClient->Disconnect();
			return PyBool_FromLong(1);
		}
	}
89

90
	PyErr_WarnEx(NULL, "Was not connected, doing nothing. Use is_connected to avoid this message.", 1);
91 92
	return PyBool_FromLong(1);
};
93

94
static PyObject* va_is_connected(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames)
95
{
96 97
	if (!g_pVANetClient)
		return PyBool_FromLong(0);
98
	else
99 100
		return PyBool_FromLong(g_pVANetClient->IsConnected());
};
101

102
static PyObject* va_reset(PyObject*, PyObject*)
103
{
104
	VAPY_REQUIRE_CONN_TRY;
105 106

	g_pVANetClient->GetCoreInstance()->Reset();
107
	return PyBool_FromLong(1);
108

109 110
	VAPY_CATCH_RETURN;
};
111

112
static PyObject* va_enumerate_modules(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames)
113
{
114
	VAPY_REQUIRE_CONN_TRY;
115 116 117 118

	std::vector< CVAModuleInfo > voModuleInfos;
	g_pVANetClient->GetCoreInstance()->EnumerateModules(voModuleInfos);

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
119
	PyObject* pModuleList = PyList_New(voModuleInfos.size());
120 121 122 123

	for (size_t i = 0; i < voModuleInfos.size(); i++)
	{
		CVAModuleInfo& oModule(voModuleInfos[i]);
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
124
		PyObject* pModuleInfo = Py_BuildValue("{s:i,s:s,s:s}", "index", i, "name", oModule.sName.c_str(), "description", oModule.sDesc.c_str() );
125 126 127 128 129
		PyList_SetItem(pModuleList, i, pModuleInfo); // steals reference
	}

	return pModuleList;

130 131 132 133
	VAPY_CATCH_RETURN;
};

static PyObject* va_call_module(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames)
134
{
135
	VAPY_REQUIRE_CONN_TRY;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	
	static const char * const _keywords[] = { "module_name", "arguments", NULL };
	static _PyArg_Parser _parser = { "so:call_module", _keywords, 0 };
	char* pcModuleName = nullptr;
	PyObject* pArguments = nullptr;

	if( !_PyArg_ParseStack( ppArgs, nArgs, pKeywordNames, &_parser, &pcModuleName, &pArguments ) )
		return NULL;
	
	std::string sModuleName = std::string( pcModuleName );
	CVAStruct oArgs = ConvertPythonDictToVAStruct( pArguments );
	CVAStruct oReturn;

	g_pVANetClient->GetCoreInstance()->CallModule( sModuleName, oArgs, oReturn );

	return ConvertVAStructToPythonDict( oReturn );
	
153 154 155 156
	VAPY_CATCH_RETURN;
};

static PyObject* va_add_search_path(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames)
157
{
158
	VAPY_REQUIRE_CONN_TRY;
159

160 161 162 163 164 165 166
	static const char * const _keywords[] = { "directory_path", NULL };
	static _PyArg_Parser _parser = { "s:add_search_path", _keywords, 0 };
	char* pcPath = nullptr;
	if (!_PyArg_ParseStack(ppArgs, nArgs, pKeywordNames, &_parser, &pcPath))
		return NULL;

	return PyBool_FromLong(g_pVANetClient->GetCoreInstance()->AddSearchPath(std::string(pcPath)));
167

168 169 170 171
	VAPY_CATCH_RETURN;
};

static PyObject* va_create_listener(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames)
172
{
173
	VAPY_REQUIRE_CONN_TRY;
174 175

	std::string sName = "PyListener";
176
	int iID = g_pVANetClient->GetCoreInstance()->CreateListener(sName, IVACore::VA_AURAMODE_ALL);
177

178 179 180 181
	return PyLong_FromLong(iID);
	
	VAPY_CATCH_RETURN;
};
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

static PyObject* va_is_scene_locked( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames )
{
	VAPY_REQUIRE_CONN_TRY;	
	return PyLong_FromLong( g_pVANetClient->GetCoreInstance()->IsSceneLocked() );
	VAPY_CATCH_RETURN;
};

static PyObject* va_lock_scene( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames )
{
	VAPY_REQUIRE_CONN_TRY
	g_pVANetClient->GetCoreInstance()->LockScene();
	return PyLong_FromLong( 1 );
	VAPY_CATCH_RETURN;
};

static PyObject* va_unlock_scene( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames )
{
	VAPY_REQUIRE_CONN_TRY;
	return PyLong_FromLong( g_pVANetClient->GetCoreInstance()->UnlockScene() );
	VAPY_CATCH_RETURN;
};