-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path4DPlugin-python.cpp
More file actions
executable file
·693 lines (576 loc) · 21.6 KB
/
4DPlugin-python.cpp
File metadata and controls
executable file
·693 lines (576 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/* --------------------------------------------------------------------------------
#
# 4DPlugin-python.cpp
# source generated by 4D Plugin Wizard
# Project : python
# author : miyako
# 2021/11/17
#
# --------------------------------------------------------------------------------*/
#include "4DPlugin-python.h"
#pragma mark -
#if VERSIONMAC
static bool getPythonHome(std::wstring& path) {
NSBundle *thisBundle = [NSBundle bundleWithIdentifier:THIS_BUNDLE_ID];
if(thisBundle){
NSString *str = [[[thisBundle resourcePath]stringByAppendingPathComponent:@"python"]stringByAppendingPathComponent:@"lib"];
if(str){
NSData *u32 = [str dataUsingEncoding:NSUTF32LittleEndianStringEncoding];//default is BE
path = std::wstring((wchar_t *)[u32 bytes], wcslen((wchar_t *)[u32 bytes]));
return true;
}
}
return false;
}
#else
static bool getPythonHome(std::wstring& path) {
wchar_t fDrive[_MAX_DRIVE],
fDir[_MAX_DIR],
fName[_MAX_FNAME],
fExt[_MAX_EXT];
wchar_t thisPath[_MAX_PATH] = {0};
wchar_t resourcesPath[_MAX_PATH] = {0};
wchar_t python_path[_MAX_PATH] = {0};
wchar_t lib_path[_MAX_PATH] = { 0 };
HMODULE hplugin = GetModuleHandleW(THIS_BUNDLE_NAME);
GetModuleFileNameW(hplugin, thisPath, _MAX_PATH);
_wsplitpath_s(thisPath, fDrive, fDir, fName, fExt);
std::wstring windowsPath = fDrive;
windowsPath += fDir;
if(windowsPath.at(windowsPath.size() - 1) == L'\\')
windowsPath = windowsPath.substr(0, windowsPath.size() - 1);
_wsplitpath_s(windowsPath.c_str(), fDrive, fDir, fName, fExt);
_wmakepath_s(resourcesPath, fDrive, fDir, L"Resources\\", NULL);
_wsplitpath_s(resourcesPath, fDrive, fDir, fName, fExt);
_wmakepath_s(python_path, fDrive, fDir, L"python\\", NULL);
_wsplitpath_s(python_path, fDrive, fDir, fName, fExt);
_wmakepath_s(lib_path, fDrive, fDir, L"lib", NULL);
path = std::wstring(lib_path);
return true;
}
#endif
static Json::Value fourd_collection_to_json(PA_CollectionRef obj, Json::Value *jsonValue) {
bool parse = false;
if(obj) {
PA_Variable params[1], result;
params[0] = PA_CreateVariable(eVK_Collection);
PA_SetObjectVariable(¶ms[0], obj);
result = PA_ExecuteCommandByID(1217, params, 1);
PA_Unistring json = PA_GetStringVariable(result);
CUTF16String u16;
u16 = PA_GetUnistring(&json);
std::string u8;
u16_to_u8(u16, u8);
Json::CharReaderBuilder builder;
Json::CharReader *reader = builder.newCharReader();
std::string errors;
parse = reader->parse((const char *)u8.c_str(),
(const char *)u8.c_str() + u8.size(),
jsonValue,
&errors);
delete reader;
}
return parse;
}
static Json::Value fourd_object_to_json(PA_ObjectRef obj, Json::Value *jsonValue) {
bool parse = false;
if(obj) {
PA_Variable params[1], result;
params[0] = PA_CreateVariable(eVK_Object);
PA_SetObjectVariable(¶ms[0], obj);
result = PA_ExecuteCommandByID(1217, params, 1);
PA_Unistring json = PA_GetStringVariable(result);
CUTF16String u16;
u16 = PA_GetUnistring(&json);
std::string u8;
u16_to_u8(u16, u8);
Json::CharReaderBuilder builder;
Json::CharReader *reader = builder.newCharReader();
std::string errors;
parse = reader->parse((const char *)u8.c_str(),
(const char *)u8.c_str() + u8.size(),
jsonValue,
&errors);
delete reader;
}
return parse;
}
static void json_to_python_object(Json::Value jsonValue, PyObject *obj);
static PyObject *fourd_type_to_python_type(PA_Variable status) {
PyObject *result = NULL;
PA_VariableKind vk = PA_GetVariableKind(status);
switch (vk) {
case eVK_Blob:
{
PA_Handle h = PA_GetBlobHandleVariable(status);
if(h) {
result = PyBytes_FromStringAndSize((const char *)PA_LockHandle(h), (Py_ssize_t)PA_GetHandleSize(h));
PA_UnlockHandle(h);
}
}
break;
case eVK_Date:
//TODO: C_DATE
break;
case eVK_Real:
result = PyFloat_FromDouble(PA_GetRealVariable(status));
break;
case eVK_Object:
{
PA_ObjectRef o = PA_GetObjectVariable(status);
Json::Value jsonValue;
if(fourd_object_to_json(o, &jsonValue)) {
if(jsonValue.isObject())
{
result = PyDict_New();
json_to_python_object(jsonValue, result);
}
}
}
break;
case eVK_Boolean:
if(PA_GetBooleanVariable(status)) {
Py_RETURN_TRUE;
}else{
Py_RETURN_FALSE;
}
break;
case eVK_Longint:
case eVK_Integer:
case eVK_Time:
result = PyLong_FromLong(PA_GetLongintVariable(status));
break;
case eVK_Collection:
{
PA_CollectionRef c = PA_GetCollectionVariable(status);
Json::Value jsonValue;
if(fourd_collection_to_json(c, &jsonValue)) {
if(jsonValue.isArray())
{
result = PyList_New(0);
json_to_python_object(jsonValue, result);
}
}
}
break;
case eVK_Picture:
//TODO: C_PICTURE
break;
case eVK_Unistring:
case eVK_Text:
{
PA_Unistring str = PA_GetStringVariable(status);
CUTF16String u16 = PA_GetUnistring(&str);
std::string u8;
u16_to_u8(u16, u8);
result = PyUnicode_FromStringAndSize(u8.c_str(), (Py_ssize_t)u8.length());
}
break;
case eVK_Null:
Py_RETURN_NONE;
break;
case eVK_Undefined:
default:
break;
}
return result;
}
static void python_type_to_fourd_type(PyObject *arg, PA_ObjectRef obj);
static void python_list_to_fourd_collection(PyObject *arg, PA_CollectionRef col) {
for(Py_ssize_t i = 0; i < PyList_Size(arg); ++i) {
PyObject *elem = PyList_GetItem(arg, i);
if(PyUnicode_Check(elem)) {
std::string u8 = PyUnicode_AsUTF8AndSize(elem, NULL);
CUTF16String u16;
u8_to_u16(u8, u16);
PA_Variable v = PA_CreateVariable(eVK_Unistring);
PA_Unistring value = PA_CreateUnistring((PA_Unichar *)u16.c_str());
PA_SetStringVariable(&v, &value);
PA_SetCollectionElement(col, PA_GetCollectionLength(col), v);
continue;
}
if(PyLong_Check(elem)) {
PA_Variable v = PA_CreateVariable(eVK_Longint);
PA_SetLongintVariable(&v, (PA_long32)PyLong_AsLong(elem));
PA_SetCollectionElement(col, PA_GetCollectionLength(col), v);
continue;
}
if(PyFloat_Check(elem)) {
PA_Variable v = PA_CreateVariable(eVK_Real);
PA_SetRealVariable(&v, PyFloat_AsDouble(elem));
PA_SetCollectionElement(col, PA_GetCollectionLength(col), v);
continue;
}
if(elem == NULL) {
PA_Variable v = PA_CreateVariable(eVK_Null);
PA_SetCollectionElement(col, PA_GetCollectionLength(col), v);
continue;
}
if(PyBool_Check(elem)) {
PA_Variable v = PA_CreateVariable(eVK_Boolean);
PA_SetBooleanVariable(&v, (char)PyLong_AsLong(elem));
PA_SetCollectionElement(col, PA_GetCollectionLength(col), v);
continue;
}
if(PyDict_Check(elem)) {
PA_ObjectRef prop = PA_CreateObject();
python_type_to_fourd_type(elem, prop);
PA_Variable v = PA_CreateVariable(eVK_Object);
PA_SetObjectVariable(&v, prop);
PA_SetCollectionElement(col, PA_GetCollectionLength(col), v);
continue;
}
if(PyList_Check(elem)) {
PA_CollectionRef col2 = PA_CreateCollection();
python_list_to_fourd_collection(elem, col2);
PA_Variable v = PA_CreateVariable(eVK_Collection);
PA_SetCollectionVariable(&v, col2);
PA_SetCollectionElement(col, PA_GetCollectionLength(col), v);
}
}
}
static void python_type_to_fourd_type(PyObject *arg, PA_ObjectRef obj) {
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(arg, &pos, &key, &value)) {
const char *u8 = PyUnicode_AsUTF8AndSize(key, NULL);
if(u8) {
if(PyUnicode_Check(value)) {
ob_set_s(obj, u8, PyUnicode_AsUTF8AndSize(value, NULL));
continue;
}
if(PyLong_Check(value)) {
ob_set_n(obj, u8, PyLong_AsLong(value));
continue;
}
if(PyFloat_Check(value)) {
ob_set_n(obj, u8, PyFloat_AsDouble(value));
continue;
}
if(value == NULL) {
ob_set_0(obj, u8);
continue;
}
if(PyBool_Check(value)) {
ob_set_b(obj, u8, PyLong_AsLong(value));
continue;
}
if(PyDict_Check(value)) {
PA_ObjectRef prop = PA_CreateObject();
python_type_to_fourd_type(value, prop);
ob_set_o(obj, u8, prop);
continue;
}
if(PyList_Check(value)) {
PA_CollectionRef col = PA_CreateCollection();
python_list_to_fourd_collection(value, col);
ob_set_c(obj, u8, col);
}
}
}
}
static PyObject *fourd_call(PyObject *self, PyObject *args) {
PyObject *result = NULL;
char *arg1;
PyObject *arg2 = NULL;
if (PyArg_ParseTuple(args, "s|O", &arg1, &arg2)) {
PA_long32 method_id = 0;
std::string u8 = arg1;
CUTF16String u16;
u8_to_u16(u8, u16);
method_id = PA_GetMethodID((PA_Unichar *)u16.c_str());
int argc = 0;
if(method_id) {
PA_Variable params[1];
params[0] = PA_CreateVariable(eVK_Object);
PA_ObjectRef param1 = PA_CreateObject();
if(arg2) {
if(PyDict_Check(arg2)) {
python_type_to_fourd_type(arg2, param1);
argc++;
}
}
PA_SetObjectVariable(¶ms[0], param1);
PA_Variable status = PA_ExecuteMethodByID(method_id, params, argc);
result = fourd_type_to_python_type(status);
PA_ClearVariable(¶ms[0]);
}
}
return result;
}
static PyMethodDef EmbMethods[] = {
{
"call",
fourd_call,
METH_VARARGS,
"call 4D project method"
},
/* end of list */
{
NULL,
NULL,
0,
NULL
}
};
static PyModuleDef EmbModule = {
PyModuleDef_HEAD_INIT,
"fourd",
NULL,
-1,
EmbMethods,
NULL, NULL, NULL, NULL
};
static PyObject *PyInit_fourd(void) {
return PyModule_Create(&EmbModule);
}
static void OnStartup() {
std::wstring path;
if(getPythonHome(path)) {
//https://docs.python.org/3/c-api/init.html#pre-init-safe
Py_SetPath((wchar_t *)path.c_str());
Py_SetPythonHome((wchar_t *)path.c_str());
Py_NoSiteFlag = 1;
Py_IgnoreEnvironmentFlag = 1;
Py_NoUserSiteDirectory = 1;
PyImport_AppendInittab("fourd", &PyInit_fourd);
Py_InitializeEx(0);
}
}
static void OnExit() {
Py_FinalizeEx();
}
void PluginMain(PA_long32 selector, PA_PluginParameters params) {
try
{
switch(selector)
{
case kInitPlugin:
case kServerInitPlugin:
// PA_RunInMainProcess((PA_RunInMainProcessProcPtr)OnStartup, NULL);
OnStartup();
break;
case kDeinitPlugin:
case kServerDeinitPlugin:
// PA_RunInMainProcess((PA_RunInMainProcessProcPtr)OnExit, NULL);
OnExit();
break;
// --- python
case 1 :
python(params);
break;
}
}
catch(...)
{
}
}
#pragma mark -
static void json_to_python_object(Json::Value jsonValue, PyObject *obj) {
if(jsonValue.isObject())
{
for(Json::Value::const_iterator it = jsonValue.begin() ; it != jsonValue.end() ; it++)
{
JSONCPP_STRING k = it.name();
if(it->isInt()) {
PyObject *key = PyUnicode_FromStringAndSize(k.c_str(), (Py_ssize_t)k.length());
PyObject *value = PyLong_FromLong(it->asInt());
PyDict_SetItem(obj, key, value);
continue;
}
if(it->isDouble()) {
PyObject *key = PyUnicode_FromStringAndSize(k.c_str(), (Py_ssize_t)k.length());
PyObject *value = PyFloat_FromDouble(it->asDouble());
PyDict_SetItem(obj, key, value);
continue;
}
if(it->isString()) {
JSONCPP_STRING v = it->asString();
PyObject *key = PyUnicode_FromStringAndSize(k.c_str(), (Py_ssize_t)k.length());
PyObject *value = PyUnicode_FromStringAndSize(v.c_str(), (Py_ssize_t)v.length());
PyDict_SetItem(obj, key, value);
continue;
}
if(it->isBool()) {
PyObject *key = PyUnicode_FromStringAndSize(k.c_str(), (Py_ssize_t)k.length());
PyObject *value = it->asBool() ? Py_True : Py_False;
PyDict_SetItem(obj, key, value);
continue;
}
if(it->isNull()) {
PyObject *key = PyUnicode_FromStringAndSize(k.c_str(), (Py_ssize_t)k.length());
PyObject *value = Py_None;
PyDict_SetItem(obj, key, value);
continue;
}
if(it->isObject()) {
PyObject *locals = PyDict_New();
json_to_python_object(*it, locals);
continue;
}
if(it->isArray()) {
PyObject *locals = PyList_New(0);
json_to_python_object(*it, locals);
continue;
}
}
}
if(jsonValue.isArray())
{
for(Json::Value::const_iterator i = jsonValue.begin() ; i != jsonValue.end() ; i++)
{
Json::Value value = *i;
if(i->isInt()) {
PyObject *value = PyLong_FromLong(i->asInt());
PyList_Append(obj, value);
continue;
}
if(i->isDouble()) {
PyObject *value = PyFloat_FromDouble(i->asDouble());
PyList_Append(obj, value);
continue;
}
if(i->isString()) {
JSONCPP_STRING v = i->asString();
PyObject *value = PyUnicode_FromStringAndSize(v.c_str(), (Py_ssize_t)v.length());
PyList_Append(obj, value);
continue;
}
if(i->isBool()) {
PyObject *value = i->asBool() ? Py_True : Py_False;
PyList_Append(obj, value);
continue;
}
if(i->isNull()) {
PyObject *value = Py_None;
PyList_Append(obj, value);
continue;
}
if(i->isObject()) {
PyObject *value = PyDict_New();
json_to_python_object(*i, value);
PyList_Append(obj, value);
continue;
}
if(i->isArray()) {
PyObject *value = PyList_New(0);
json_to_python_object(*i, value);
PyList_Append(obj, value);
continue;
}
}
}
}
void python(PA_PluginParameters params) {
PA_ObjectRef status = PA_CreateObject();
ob_set_s(status, L"version", Py_GetVersion());
ob_set_s(status, L"build", Py_GetBuildInfo());
ob_set_s(status, L"copyright", Py_GetCopyright());
ob_set_s(status, L"compiler", Py_GetCompiler());
std::string python;
PA_Unistring *arg1 = PA_GetStringParameter(params, 1);
if(arg1){
CUTF16String u16 = (const PA_Unichar *)PA_GetUnistring(arg1);
u16_to_u8(u16, python);
}
PyObject *main = PyImport_AddModule("__main__");
PyObject *globals = PyModule_GetDict(main);
PyObject *locals = PyDict_New();
PA_ObjectRef variables = PA_GetObjectParameter(params, 2);
if(variables) {
Json::Value jsonValue;
if(fourd_object_to_json(variables, &jsonValue)) {
if(jsonValue.isObject())
{
json_to_python_object(jsonValue, locals);
}
}
}
PyObject *ref = NULL;
switch (PA_GetLongParameter(params, 3)) {
case 1: //python_eval
ref = PyRun_String(python.c_str(),
Py_eval_input,
globals,
locals);
break;
case 2: //python_line
ref = PyRun_String(python.c_str(),
Py_single_input,
globals,
locals);
break;
default://python_exec
ref = PyRun_String(python.c_str(),
Py_file_input,
globals,
locals);
break;
}
Py_DECREF(locals);
if (ref != NULL) {
PA_ObjectRef returnValue = PA_CreateObject();
python_type_to_fourd_type(ref, returnValue);
ob_set_o(status, L"returnValue", returnValue);
}
if(PyErr_Occurred()) {
//PyErr_Print() also clears the error indicator so don't use it!
PyObject *errType, *errValue, *errTraceback;
PyErr_Fetch(&errType, &errValue, &errTraceback);
if(errValue){
const char *u8 = PyUnicode_AsUTF8AndSize(errValue, NULL);
ob_set_s(status, L"errorMessage", u8);
}
Py_XDECREF(errType);
Py_XDECREF(errValue);
Py_XDECREF(errTraceback);
PyErr_Clear();//need this because a callback returning NULL is technically an error
}
PA_ReturnObject(params, status);
}
#pragma mark -
static void u16_to_u8(CUTF16String& u16, std::string& u8) {
#ifdef _WIN32
int len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)u16.c_str(), u16.length(), NULL, 0, NULL, NULL);
if(len){
std::vector<uint8_t> buf(len + 1);
if(WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)u16.c_str(), u16.length(), (LPSTR)&buf[0], len, NULL, NULL)){
u8 = std::string((const char *)&buf[0]);
}
}else{
u8 = std::string((const char *)"");
}
#else
CFStringRef str = CFStringCreateWithCharacters(kCFAllocatorDefault, (const UniChar *)u16.c_str(), u16.length());
if(str){
size_t size = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8) + sizeof(uint8_t);
std::vector<uint8_t> buf(size);
CFIndex len = 0;
CFStringGetBytes(str, CFRangeMake(0, CFStringGetLength(str)), kCFStringEncodingUTF8, 0, true, (UInt8 *)&buf[0], size, &len);
u8 = std::string((const char *)&buf[0], len);
CFRelease(str);
}
#endif
}
static void u8_to_u16(std::string& u8, CUTF16String& u16) {
#ifdef _WIN32
int len = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)u8.c_str(), u8.length(), NULL, 0);
if(len){
std::vector<uint8_t> buf((len + 1) * sizeof(PA_Unichar));
if(MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)u8.c_str(), u8.length(), (LPWSTR)&buf[0], len)){
u16 = CUTF16String((const PA_Unichar *)&buf[0]);
}
}else{
u16 = CUTF16String((const PA_Unichar *)L"");
}
#else
CFStringRef str = CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8 *)u8.c_str(), u8.length(), kCFStringEncodingUTF8, true);
if(str){
CFIndex len = CFStringGetLength(str);
std::vector<uint8_t> buf((len+1) * sizeof(PA_Unichar));
CFStringGetCharacters(str, CFRangeMake(0, len), (UniChar *)&buf[0]);
u16 = CUTF16String((const PA_Unichar *)&buf[0]);
CFRelease(str);
}
#endif
}