-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnsPython.c
More file actions
263 lines (237 loc) · 7.81 KB
/
Copy pathnsPython.c
File metadata and controls
263 lines (237 loc) · 7.81 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
#include <windows.h>
#include <commctrl.h>
#include "exdll.h"
#include <Python.h>
HINSTANCE g_hInstance;
HWND g_hwndParent;
HWND g_hwndList, g_hwndProgress;
static void LogMessage(const char *pStr);
static void callPython(int eval_type);
static void SetProgressRange(int iLowLim, int iHighLim);
static void SetProgressPos(int iPos);
//--- python extension ---
const struct {char *name; int enumcode;} string_to_enum[] = {
{"0", INST_0},
{"1", INST_1},
{"2", INST_2},
{"3", INST_3},
{"4", INST_4},
{"5", INST_5},
{"6", INST_6},
{"7", INST_7},
{"8", INST_8},
{"9", INST_9},
{"R0", INST_R0},
{"R1", INST_R1},
{"R2", INST_R2},
{"R3", INST_R3},
{"R4", INST_R4},
{"R5", INST_R5},
{"R6", INST_R6},
{"R7", INST_R7},
{"R8", INST_R8},
{"R9", INST_R9},
{"CMDLINE", INST_CMDLINE},
{"INSTDIR", INST_INSTDIR},
{"OUTDIR", INST_OUTDIR},
{"EXEDIR", INST_EXEDIR},
{"LANGUAGE", INST_LANG},
};
#define __doc__log "log(string)\n"\
"Write Messages to the NSIS log window."
static PyObject*
py_log(PyObject *self, PyObject *args)
{
char *logtext;
if (!PyArg_ParseTuple(args, "s", &logtext))
return 0;
LogMessage(logtext);
Py_INCREF(Py_None);
return Py_None;
}
#define __doc__messagebox "messagebox(string, title='NSIS Python')\n"\
"Pop up a message box."
static PyObject*
py_messagebox(PyObject *self, PyObject *args)
{
char *text;
char *title = "NSIS Python";
if (!PyArg_ParseTuple(args, "s|s", &text, &title))
return 0;
MessageBox(g_hwndParent, text, title, MB_OK);
Py_INCREF(Py_None);
return Py_None;
}
int
my_strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2++)
if (*s1++ == 0)
return (0);
return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
}
#define __doc__getvar "getvar(varname_string)\n"\
"Get a variable fom NSIS. The contents of a variable is always a string."
static PyObject*
py_getvar(PyObject *self, PyObject *args)
{
int i;
char *varname;
if (!PyArg_ParseTuple(args, "s", &varname))
return 0;
if (varname[0] == '$') varname++; //vars may start with "$" but not required
for( i=0; i<26; i++) {
if (my_strcmp(varname, string_to_enum[i].name) == 0) {
return PyString_FromString(getuservariable(string_to_enum[i].enumcode));
}
}
PyErr_Format(PyExc_NameError, "There is no NSIS variable named '$%s'.", varname);
return NULL;
}
#define __doc__setvar "setvar(varname_string, value)\n"\
"Set a variable fom NSIS. The contents of a variable is always a string."
static PyObject*
py_setvar(PyObject *self, PyObject *args)
{
int i;
char *varname;
char *value;
if (!PyArg_ParseTuple(args, "ss", &varname, &value))
return 0;
if (varname[0] == '$') varname++; //vars may start with "$" but not required
for( i=0; i<26; i++) {
if (my_strcmp(varname, string_to_enum[i].name) == 0) {
setuservariable(string_to_enum[i].enumcode, value);
Py_INCREF(Py_None);
return Py_None;
}
}
PyErr_Format(PyExc_NameError, "There is no NSIS variable named '$%s'.", varname);
return NULL;
}
#define __doc__getParent "getParent()\n"\
"Get the parent's handle of the installer."
static PyObject*
py_getParent(PyObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
return PyLong_FromLong((long)g_hwndParent);
}
#define __doc__setProgressRange "setProgressRange()\n"\
"Set the low and high limit of the progress bar."
static PyObject*
py_setProgressRange(PyObject *self, PyObject *args)
{
int low, high;
if (!PyArg_ParseTuple(args, "ii", &low, &high))
return 0;
SetProgressRange(low, high);
Py_INCREF(Py_None);
return Py_None;
}
#define __doc__setProgressPos "setProgressPos()\n"\
"Set the position of the progress bar."
static PyObject*
py_setProgressPos(PyObject *self, PyObject *args)
{
int pos;
if (!PyArg_ParseTuple(args, "i", &pos))
return 0;
SetProgressPos(pos);
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef ext_methods[] = {
{"log", py_log, METH_VARARGS, __doc__log},
{"messagebox", py_messagebox, METH_VARARGS, __doc__messagebox},
{"getvar", py_getvar, METH_VARARGS, __doc__getvar},
{"setvar", py_setvar, METH_VARARGS, __doc__setvar},
{"getParent", py_getParent, METH_VARARGS, __doc__getParent},
{"setProgressRange",py_setProgressRange, METH_VARARGS, __doc__setProgressRange},
{"setProgressPos", py_setProgressPos, METH_VARARGS, __doc__setProgressPos},
//End maeker
{0, 0}
};
//--- NSIS functions ---
void __declspec(dllexport) eval(HWND hwndParent, int string_size, char *variables, stack_t **stacktop) {
g_hwndParent = hwndParent;
EXDLL_INIT();
callPython(Py_eval_input);
}
void __declspec(dllexport) exec(HWND hwndParent, int string_size, char *variables, stack_t **stacktop) {
g_hwndParent = hwndParent;
EXDLL_INIT();
callPython(Py_file_input);
}
void __declspec(dllexport) SetOptimized(HWND hwndParent, int string_size, char *variables, stack_t **stacktop) {
g_hwndParent = hwndParent;
EXDLL_INIT();
Py_OptimizeFlag++;
}
void __declspec(dllexport) UnsetOptimized(HWND hwndParent, int string_size, char *variables, stack_t **stacktop) {
g_hwndParent = hwndParent;
EXDLL_INIT();
if (Py_OptimizeFlag > 0)
Py_OptimizeFlag--;
}
BOOL WINAPI _DllMainCRTStartup(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved) {
g_hInstance = hInst;
return TRUE;
}
//--- internal helper functions ---
static void callPython(int eval_type) {
PyObject *m, *d, *v, *result;
char *command = (char *)GlobalAlloc(GPTR, sizeof(char)*g_stringsize + 1);
g_hwndList = FindWindowEx(FindWindowEx(g_hwndParent, NULL, "#32770", NULL), NULL, "SysListView32", NULL);
g_hwndProgress = FindWindowEx(FindWindowEx(g_hwndParent, NULL, "#32770", NULL), NULL, "msctls_progress32", NULL);
Py_Initialize();
Py_InitModule("nsis", ext_methods);
popstring(command);
m = PyImport_AddModule("__main__");
if (m == NULL) {
pushstring("error");
return;
}
d = PyModule_GetDict(m);
v = PyRun_String(command, eval_type, d, d);
if (v == NULL) {
PyObject *ptype, *pvalue, *ptraceback;
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
if (pvalue) {
LogMessage("Python Exception:");
LogMessage(PyString_AsString(PyObject_Str(pvalue)));
}
pushstring("error");
PyErr_Clear();
return;
}
result = PyObject_Str(v);
pushstring(PyString_AsString(result));
Py_DECREF(result);
Py_DECREF(v);
Py_Finalize();
GlobalFree(command);
}
// Tim Kosse's LogMessage
static void LogMessage(const char *pStr) {
LVITEM item={0};
int nItemCount;
if (!g_hwndList) return;
if (!lstrlen(pStr)) return;
nItemCount=SendMessage(g_hwndList, LVM_GETITEMCOUNT, 0, 0);
item.mask=LVIF_TEXT;
item.pszText=(char *)pStr;
item.cchTextMax=0;
item.iItem=nItemCount;
ListView_InsertItem(g_hwndList, &item);
ListView_EnsureVisible(g_hwndList, item.iItem, 0);
}
static void SetProgressRange(int iLowLim, int iHighLim) {
if (!g_hwndProgress) return;
SendMessage(g_hwndProgress, PBM_SETRANGE32, (WPARAM)iLowLim, (LPARAM)iHighLim);
}
static void SetProgressPos(int iPos) {
if (!g_hwndProgress) return;
SendMessage(g_hwndProgress, PBM_SETPOS, (WPARAM)iPos, 0);
}