changed: moved url and isFolder arguments back to XBMC_setDirectoryEntry
[xbmc:xbmc-antiquated.git] / xbmc / lib / libPython / XBPythonDll.cpp
1
2 #include "stdafx.h"
3 #include "XBPythonDll.h"
4 #include "../../cores/DllLoader/DllLoader.h"
5
6 template<byte count>
7 struct SVaPassNext{
8   SVaPassNext<count-1> big;
9   DWORD dw;
10 };
11 template<> struct SVaPassNext<0>{};
12 //SVaPassNext - is generator of structure of any size at compile time.
13
14 class CVaPassNext{
15   public:
16     SVaPassNext<50> svapassnext;
17     CVaPassNext(va_list & args){
18       try{//to avoid access violation
19         memcpy(&svapassnext, args, sizeof(svapassnext));
20       } catch (...) {}
21     }
22 };
23 #define va_pass(valist) CVaPassNext(valist).svapassnext
24
25 // macro to make life a bit simpler
26
27 #define DLL_FUNCTION(function) #function, (void**)&p_##function
28 #define DLL_VA_FUNCTION(function) #function, (void**)&p_va_##function
29
30 // use these if the dll exports pointers
31 #define DLL_POINTER_DATA(data) #data, (void**)&data
32 #define DATA_POINTER(data) unsigned long data;
33
34 // use these if the dll exports data structs
35 #define DLL_OBJECT_DATA(data) #data, (void**)&pointer_##data
36
37 #define _Py_NoneStruct (*((PyObject*)pointer__Py_NoneStruct))
38 #define PyString_Type (*((PyTypeObject*)pointer_PyString_Type))
39 #define PyList_Type (*((PyTypeObject*)pointer_PyList_Type))
40 #define PyLong_Type (*((PyTypeObject*)pointer_PyLong_Type))
41 #define PyInt_Type (*((PyTypeObject*)pointer_PyInt_Type))
42 #define PyUnicode_Type (*((PyTypeObject*)pointer_PyUnicode_Type))
43 #define PyTuple_Type (*((PyTypeObject*)pointer_PyTuple_Type))
44 #define PyDict_Type (*((PyTypeObject*)pointer_PyDict_Type))
45
46 #define DLL_ORD_FUNCTION(ord, function) "hapdbg.dll", ord, (void**)&p_##function
47
48 #define FUNCTION(function) \
49     void* (__cdecl* p_##function)(); \
50     void* ##function() { return p_##function(); }
51
52 #define FUNCTION4(function) \
53     void* (__cdecl* p_##function)(void* a); \
54     void* ##function(void* a) { return p_##function(a); }
55
56 #define FUNCTION8(function) \
57     void* (__cdecl* p_##function)(void* a, void* b); \
58     void* ##function(void* a, void* b) { return p_##function(a, b); }
59
60 #define VA_FUNC_START(x) \
61       void* ret; \
62       va_list va; \
63       va_start(va, x); \
64       ret =
65
66 #define VA_FUNC_END \
67       va_end(va); \
68       return ret;
69
70 #define VA_FUNCTION8(function) \
71     void* (__cdecl* p_va_##function)(void* a, ...); \
72     void* ##function(void* a, ...) { VA_FUNC_START(a) p_va_##function(a, va_pass(va)); VA_FUNC_END }
73
74 #define VA_FUNCTION12(function) \
75     void* (__cdecl* p_va_##function)(void* a, void* b, ...); \
76     void* ##function(void* a, void* b, ...) { VA_FUNC_START(b) p_va_##function(a, b, va_pass(va)); VA_FUNC_END }
77
78 #define VA_FUNCTION16(function) \
79     void* (__cdecl* p_va_##function)(void* a, void* b, void* c, ...); \
80     void* ##function(void* a, void* b, void* c, ...) { VA_FUNC_START(c) p_va_##function(a, b, c, va_pass(va)); VA_FUNC_END }
81
82 #define VA_FUNCTION20(function) \
83     void* (__cdecl* p_va_##function)(void* a, void* b, void* c, void* d, ...); \
84     void* ##function(void* a, void* b, void* c, void* d, ...) { VA_FUNC_START(d) p_va_##function(a, b, c, d, va_pass(va)); VA_FUNC_END }
85
86 #define FUNCTION12(function) \
87     void* (__cdecl* p_##function)(void* a, void* b, void* c); \
88     void* ##function(void* a, void* b, void* c) { return p_##function(a, b, c); }
89
90 #define FUNCTION16(function) \
91     void* (__cdecl* p_##function)(void* a, void* b, void* c, void* d); \
92     void* ##function(void* a, void* b, void* c, void* d) { return p_##function(a, b, c, d); }
93
94 #define FUNCTION20(function) \
95     void* (__cdecl* p_##function)(void* a, void* b, void* c, void* d, void* e); \
96     void* ##function(void* a, void* b, void* c, void* d, void* e) { return p_##function(a, b, c, d, e); }
97
98 extern "C"
99 {
100   /*****************************************
101    * python24.dll
102    */
103
104   FUNCTION(PyEval_ReleaseLock)
105   FUNCTION(PyEval_AcquireLock)
106   FUNCTION(PyThreadState_Get)
107   FUNCTION4(PyRun_SimpleString)
108   FUNCTION(PyEval_InitThreads)
109   FUNCTION(Py_Initialize)
110   FUNCTION(Py_Finalize)
111   FUNCTION4(PyThreadState_Swap)
112   FUNCTION8(PyErr_SetString)
113   FUNCTION4(PyThreadState_New)
114   FUNCTION(PyErr_Print)
115   FUNCTION(PyErr_Occurred)
116   FUNCTION8(PyRun_SimpleFile)
117   FUNCTION4(PySys_SetPath)
118   FUNCTION4(PyThreadState_Delete)
119   FUNCTION4(PyThreadState_Clear)
120
121   VA_FUNCTION8(Py_BuildValue)
122   /*void* Py_BuildValue(void* a, ...)
123   {
124     void* ret;
125     va_list va;
126     va_start(va, a);
127     ret = Py_VaBuildValue(a, va);
128     va_end(va);
129     return ret;
130   }*/
131
132   VA_FUNCTION12(PyArg_Parse)
133   VA_FUNCTION12(PyArg_ParseTuple)
134   /*void* PyArg_ParseTuple(void* a, void* b, ...)
135   {
136     void* ret;
137     va_list va;
138     va_start(va, b);
139     ret = PyArg_VaParse(a, b, va);
140     va_end(va);
141     return ret;
142   }*/
143
144   FUNCTION8(PyType_IsSubtype)
145   VA_FUNCTION20(PyArg_ParseTupleAndKeywords)
146   /*void* PyArg_ParseTupleAndKeywords(void* a, void* b, void* c, void* d, ...)
147   {
148     void* ret;
149     va_list va;
150     va_start(va, d);
151     ret = PyArg_VaParseTupleAndKeywords(a, b, c, d, va);
152     va_end(va);
153     return ret;
154   }*/
155
156   FUNCTION4(PyString_AsString)
157   FUNCTION8(Py_AddPendingCall)
158   FUNCTION8(PyList_GetItem)
159   FUNCTION4(PyList_Size)
160   FUNCTION4(_PyObject_New)
161   FUNCTION4(PyLong_AsLong)
162
163   VA_FUNCTION12(PyErr_Format)
164   /*void* PyErr_Format(void* a, void* b, ...)
165   {
166     void* ret;
167     va_list va;
168     va_start(va, b);
169     ret = PyErr_VaFormat(a, b, va);
170     va_end(va);
171     return ret;
172   }*/
173   FUNCTION4(PyUnicodeUCS2_AsUnicode)
174   FUNCTION(Py_MakePendingCalls)
175   FUNCTION(PyEval_SaveThread)
176   FUNCTION4(PyEval_RestoreThread)
177   FUNCTION4(PyLong_FromLong)
178   FUNCTION12(PyModule_AddStringConstant)
179   FUNCTION12(PyModule_AddObject)
180   FUNCTION20(Py_InitModule4)
181   FUNCTION4(PyInt_AsLong)
182   FUNCTION4(PyFloat_AsDouble)
183   FUNCTION4(PyString_FromString)
184   FUNCTION12(PyModule_AddIntConstant)
185
186   //void* (__cdecl* p_va_PyObject_CallFunction)(void* a, void* b, ...);
187   VA_FUNCTION12(PyObject_CallFunction) // va arg
188   /*
189   void* PyObject_CallFunction(void* a, void* b, ...)
190   {
191     void* ret;
192     va_list va;
193     va_start(va, b);
194     ret = p_va_PyObject_CallFunction(a, b, va_pass(va));
195     va_end(va);
196     return ret;
197   }*/
198
199   //void* (__cdecl* p_va_PyObject_CallMethod)(void* a, void* b, void* c, ...);
200   VA_FUNCTION16(PyObject_CallMethod)
201   /*void* PyObject_CallMethod(void* a, void* b, void* c, ...)
202   {
203     void* ret;
204     va_list va;
205     va_start(va, c);
206     ret = p_va_PyObject_CallMethod(a, b, c, va_pass(va));
207     va_end(va);
208     return ret;
209   }*/
210   FUNCTION12(PyDict_SetItemString)
211   FUNCTION(PyDict_New)
212   FUNCTION4(PyModule_GetDict)
213   FUNCTION4(PyImport_Import)
214   FUNCTION4(PyInt_FromLong)
215   FUNCTION8(PyDict_GetItemString)
216   //FUNCTION8(PyDict_GetItem)
217   //FUNCTION4(PyDict_Keys)
218   FUNCTION16(PyDict_Next);
219   FUNCTION4(PyDict_Size);
220   FUNCTION4(PyType_Ready);
221   FUNCTION12(PyType_GenericNew);
222   FUNCTION8(PySys_SetArgv);
223   FUNCTION12(PyObject_RichCompare);
224
225   // PyFloat_FromDouble(double)
226   void* (__cdecl* p_PyFloat_FromDouble)(double a); \
227   void* PyFloat_FromDouble(double a) { return p_PyFloat_FromDouble(a); }
228
229   DATA_OBJECT(PyExc_SystemError);
230   DATA_OBJECT(PyExc_ValueError);
231   DATA_OBJECT(PyExc_Exception);
232   DATA_OBJECT(PyExc_TypeError);
233   DATA_OBJECT(PyExc_KeyboardInterrupt);
234   DATA_OBJECT(PyExc_RuntimeError);
235   DATA_OBJECT(PyExc_ReferenceError);
236
237   DATA_OBJECT(_Py_NoneStruct);
238   DATA_OBJECT(_Py_NotImplementedStruct);
239   DATA_OBJECT(_Py_TrueStruct);
240   DATA_OBJECT(_Py_ZeroStruct);
241   DATA_OBJECT(PyString_Type);
242   DATA_OBJECT(PyList_Type);
243   DATA_OBJECT(PyLong_Type);
244   DATA_OBJECT(PyInt_Type);
245   DATA_OBJECT(PyUnicode_Type);
246   DATA_OBJECT(PyTuple_Type);
247   DATA_OBJECT(PyDict_Type);
248
249   bool python_load_dll(LibraryLoader& dll)
250   {
251     bool bResult;
252
253     bResult = (
254       dll.ResolveExport(DLL_FUNCTION(PyEval_ReleaseLock)) &&
255       dll.ResolveExport(DLL_FUNCTION(PyEval_AcquireLock)) &&
256       dll.ResolveExport(DLL_FUNCTION(PyThreadState_Get)) &&
257       dll.ResolveExport(DLL_FUNCTION(PyRun_SimpleString)) &&
258       dll.ResolveExport(DLL_FUNCTION(PyEval_InitThreads)) &&
259       dll.ResolveExport(DLL_FUNCTION(Py_Initialize)) &&
260       dll.ResolveExport(DLL_FUNCTION(Py_Finalize)) &&
261       dll.ResolveExport(DLL_FUNCTION(PyThreadState_Swap)) &&
262       dll.ResolveExport(DLL_FUNCTION(PyErr_SetString)) &&
263       dll.ResolveExport(DLL_FUNCTION(PyThreadState_New)) &&
264       dll.ResolveExport(DLL_FUNCTION(PyErr_Print)) &&
265       dll.ResolveExport(DLL_FUNCTION(PyErr_Occurred)) &&
266       dll.ResolveExport(DLL_FUNCTION(PyRun_SimpleFile)) &&
267       dll.ResolveExport(DLL_FUNCTION(PySys_SetPath)) &&
268       dll.ResolveExport(DLL_FUNCTION(PyThreadState_Delete)) &&
269       dll.ResolveExport(DLL_FUNCTION(PyThreadState_Clear)) &&
270       dll.ResolveExport(DLL_VA_FUNCTION(Py_BuildValue)) &&
271       dll.ResolveExport(DLL_FUNCTION(PyType_IsSubtype)) &&
272       dll.ResolveExport(DLL_VA_FUNCTION(PyArg_ParseTupleAndKeywords)) &&
273       dll.ResolveExport(DLL_FUNCTION(PyString_AsString)) &&
274       dll.ResolveExport(DLL_FUNCTION(Py_AddPendingCall)) &&
275       dll.ResolveExport(DLL_VA_FUNCTION(PyObject_CallMethod)) &&
276       dll.ResolveExport(DLL_FUNCTION(PyList_GetItem)) &&
277       dll.ResolveExport(DLL_FUNCTION(PyList_Size)) &&
278       dll.ResolveExport(DLL_FUNCTION(_PyObject_New)) &&
279       dll.ResolveExport(DLL_FUNCTION(PyLong_AsLong)) &&
280       dll.ResolveExport(DLL_VA_FUNCTION(PyErr_Format)) &&
281       dll.ResolveExport(DLL_FUNCTION(PyUnicodeUCS2_AsUnicode)) &&
282       dll.ResolveExport(DLL_FUNCTION(Py_MakePendingCalls)) &&
283       dll.ResolveExport(DLL_FUNCTION(PyEval_SaveThread)) &&
284       dll.ResolveExport(DLL_FUNCTION(PyEval_RestoreThread)) &&
285       dll.ResolveExport(DLL_FUNCTION(PyLong_FromLong)) &&
286       dll.ResolveExport(DLL_FUNCTION(PyModule_AddStringConstant)) &&
287       dll.ResolveExport(DLL_FUNCTION(PyModule_AddObject)) &&
288       dll.ResolveExport(DLL_FUNCTION(Py_InitModule4)) &&
289       dll.ResolveExport(DLL_FUNCTION(PyInt_AsLong)) &&
290       dll.ResolveExport(DLL_FUNCTION(PyFloat_AsDouble)) &&
291       dll.ResolveExport(DLL_FUNCTION(PyString_FromString)) &&
292       dll.ResolveExport(DLL_FUNCTION(PyModule_AddIntConstant)) &&
293       dll.ResolveExport(DLL_VA_FUNCTION(PyObject_CallFunction)) &&
294       dll.ResolveExport(DLL_FUNCTION(PyDict_SetItemString)) &&
295       dll.ResolveExport(DLL_FUNCTION(PyDict_New)) &&
296       dll.ResolveExport(DLL_FUNCTION(PyModule_GetDict)) &&
297       dll.ResolveExport(DLL_FUNCTION(PyImport_Import)) &&
298       dll.ResolveExport(DLL_FUNCTION(PyFloat_FromDouble)) &&
299       dll.ResolveExport(DLL_FUNCTION(PyInt_FromLong)) &&
300       dll.ResolveExport(DLL_FUNCTION(PyDict_GetItemString)) &&
301       //dll.ResolveExport(DLL_FUNCTION(PyDict_GetItem)) &&
302       dll.ResolveExport(DLL_FUNCTION(PyDict_Next)) &&
303       dll.ResolveExport(DLL_FUNCTION(PyDict_Size)) &&
304       dll.ResolveExport(DLL_FUNCTION(PyType_Ready)) &&
305       dll.ResolveExport(DLL_FUNCTION(PyType_GenericNew)) &&
306       dll.ResolveExport(DLL_VA_FUNCTION(PyArg_Parse)) &&
307       dll.ResolveExport(DLL_VA_FUNCTION(PyArg_ParseTuple)) &&
308       dll.ResolveExport(DLL_FUNCTION(PySys_SetArgv)) &&
309       dll.ResolveExport(DLL_FUNCTION(PyObject_RichCompare)) &&
310
311       dll.ResolveExport(DLL_OBJECT_DATA(PyExc_SystemError)) &&
312       dll.ResolveExport(DLL_OBJECT_DATA(PyExc_ValueError)) &&
313       dll.ResolveExport(DLL_OBJECT_DATA(PyExc_Exception)) &&
314       dll.ResolveExport(DLL_OBJECT_DATA(PyExc_TypeError)) &&
315       dll.ResolveExport(DLL_OBJECT_DATA(PyExc_KeyboardInterrupt)) &&
316       dll.ResolveExport(DLL_OBJECT_DATA(PyExc_RuntimeError)) &&
317       dll.ResolveExport(DLL_OBJECT_DATA(PyExc_ReferenceError)) &&
318
319       dll.ResolveExport(DLL_OBJECT_DATA(_Py_NoneStruct)) &&
320       dll.ResolveExport(DLL_OBJECT_DATA(_Py_NotImplementedStruct)) &&
321       dll.ResolveExport(DLL_OBJECT_DATA(_Py_TrueStruct)) &&
322       dll.ResolveExport(DLL_OBJECT_DATA(_Py_ZeroStruct)) &&
323       dll.ResolveExport(DLL_OBJECT_DATA(PyString_Type)) &&
324       dll.ResolveExport(DLL_OBJECT_DATA(PyList_Type)) &&
325       dll.ResolveExport(DLL_OBJECT_DATA(PyLong_Type)) &&
326       dll.ResolveExport(DLL_OBJECT_DATA(PyInt_Type)) &&
327       dll.ResolveExport(DLL_OBJECT_DATA(PyUnicode_Type)) &&
328       dll.ResolveExport(DLL_OBJECT_DATA(PyTuple_Type)) &&
329       dll.ResolveExport(DLL_OBJECT_DATA(PyDict_Type)));
330
331     return bResult;
332   }
333 }