-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJSProfile.cpp
224 lines (193 loc) · 5.71 KB
/
JSProfile.cpp
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
#include "JSProfile.h"
#include "Profile.h"
#include "Helpers.h"
enum profile_id
{
PROFILE_TYPE,
PROFILE_IP,
PROFILE_USERNAME,
PROFILE_GATEWAY,
PROFILE_CHARACTER,
PROFILE_DIFFICULTY,
PROFILE_MAXLOGINTIME,
PROFILE_MAXCHARSELTIME
};
static JSClassID profile_class_id;
static void js_profile_finalizer(JSRuntime *rt, JSValue val)
{
Profile *profile = (Profile *)JS_GetOpaque(val, profile_class_id);
if (profile)
{
delete profile;
}
}
static JSClassDef js_profile_class = {
"Profile",
.finalizer = js_profile_finalizer,
};
static JSValue js_profile_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
{
Profile *prof = nullptr;
// ProfileType pt;
wchar_t *str1;
wchar_t *str2;
wchar_t *str3;
wchar_t *str4;
uint32_t uType = -1;
uint32_t uDiff = -1;
// unsigned int i;
str1 = str2 = str3 = str4 = nullptr;
// pt = PROFILETYPE_INVALID;
try
{
// Profile()
if (argc == 0)
{
if (wcslen(Vars.szProfile) > 0)
prof = new Profile();
else
JS_THROW_SINGLE_LINE(ctx, "No active profile!");
}
// Profile(name) - get the named profile
else if (argc == 1 && JS_IsString(argv[0]))
{
JS_ToUnicodeString(ctx, &str1, argv[0]);
prof = new Profile(str1);
free(str1);
}
// Profile(ProfileType.singlePlayer, charname, diff)
else if (argc == 3 && JS_IsNumber(argv[0]))
{
JS_ToUint32(ctx, &uType, argv[0]);
JS_ToUnicodeString(ctx, &str1, argv[1]);
switch (uType)
{
case PROFILETYPE_SINGLEPLAYER:
JS_ToUint32(ctx, &uDiff, argv[2]);
prof = new Profile(PROFILETYPE_SINGLEPLAYER, str1, uDiff);
break;
case PROFILETYPE_TCPIP_HOST:
JS_ToUint32(ctx, &uDiff, argv[2]);
prof = new Profile(PROFILETYPE_TCPIP_HOST, str1, uDiff);
break;
case PROFILETYPE_TCPIP_JOIN:
{
// Profile(ProfileType.tcpIpJoin, charname, ip)
JS_ToUnicodeString(ctx, &str2, argv[2]);
prof = new Profile(PROFILETYPE_TCPIP_JOIN, str1, str2);
free(str2);
}
break;
}
free(str1);
}
// Profile(ProfileType.battleNet, account, pass, charname, gateway)
else if (argc == 5 && JS_IsNumber(argv[0]))
{
JS_ToUint32(ctx, &uType, argv[0]);
JS_ToUnicodeString(ctx, &str1, argv[1]);
JS_ToUnicodeString(ctx, &str2, argv[2]);
JS_ToUnicodeString(ctx, &str3, argv[3]);
JS_ToUnicodeString(ctx, &str4, argv[4]);
switch (uType)
{
case PROFILETYPE_BATTLENET:
prof = new Profile(PROFILETYPE_BATTLENET, str1, str2, str3, str4);
break;
case PROFILETYPE_OPEN_BATTLENET:
prof = new Profile(PROFILETYPE_OPEN_BATTLENET, str1, str2, str3, str4);
break;
}
free(str1);
free(str2);
free(str3);
free(str4);
}
else
JS_THROW_SINGLE_LINE(ctx, "Invalid parameters.");
}
catch (char *ex)
{
JS_THROW_ERROR(ctx, ex);
}
JSValue obj = BuildObject(ctx, profile_class_id, prof);
if (JS_IsException(obj))
{
delete prof;
prof = nullptr;
JS_THROW_ERROR(ctx, "Failed to create profile object");
}
return obj;
}
JSAPI_FUNC(profile_login)
{
char *error;
Profile *prof;
prof = (Profile *)JS_GetOpaque(this_val, profile_class_id);
if (prof->login(&error) != 0)
JS_THROW_SINGLE_LINE(ctx, error);
return JS_TRUE;
}
JSAPI_PGM(profile_getProperty)
{
Profile *prof;
prof = (Profile *)JS_GetOpaque(this_val, profile_class_id);
switch (magic)
{
case PROFILE_TYPE:
return JS_NewUint32(ctx, prof->type);
break;
case PROFILE_IP:
return JS_NewUTF8String(ctx, prof->ip);
break;
case PROFILE_USERNAME:
return JS_NewUTF8String(ctx, prof->username);
break;
case PROFILE_GATEWAY:
return JS_NewUTF8String(ctx, prof->gateway);
break;
case PROFILE_CHARACTER:
return JS_NewUTF8String(ctx, prof->charname);
break;
case PROFILE_DIFFICULTY:
return JS_NewUint32(ctx, prof->diff);
break;
case PROFILE_MAXLOGINTIME:
return JS_NewUint32(ctx, prof->maxLoginTime);
break;
case PROFILE_MAXCHARSELTIME:
return JS_NewUint32(ctx, prof->maxCharTime);
break;
}
return JS_TRUE;
}
static const JSCFunctionListEntry js_profile_proto_funcs[] = {
JS_CGETSET_MAGIC_DEF("type", profile_getProperty, NULL, PROFILE_TYPE),
JS_CGETSET_MAGIC_DEF("ip", profile_getProperty, NULL, PROFILE_IP),
JS_CGETSET_MAGIC_DEF("username", profile_getProperty, NULL, PROFILE_USERNAME),
JS_CGETSET_MAGIC_DEF("gateway", profile_getProperty, NULL, PROFILE_GATEWAY),
JS_CGETSET_MAGIC_DEF("character", profile_getProperty, NULL, PROFILE_CHARACTER),
JS_CGETSET_MAGIC_DEF("difficulty", profile_getProperty, NULL, PROFILE_DIFFICULTY),
JS_CGETSET_MAGIC_DEF("maxLoginTime", profile_getProperty, NULL, PROFILE_MAXLOGINTIME),
JS_CGETSET_MAGIC_DEF("maxCharacterSelectTime", profile_getProperty, NULL, PROFILE_MAXCHARSELTIME),
JS_CFUNC_DEF("login", 0, profile_login),
};
int js_module_profile_init(JSContext *ctx, JSModuleDef *m)
{
JSValue profile_proto, profile_class;
/* create the Room class */
JS_NewClassID(&profile_class_id);
JS_NewClass(JS_GetRuntime(ctx), profile_class_id, &js_profile_class);
profile_proto = JS_NewObject(ctx);
profile_class = JS_NewCFunction2(ctx, js_profile_ctor, "Profile", 0, JS_CFUNC_constructor, 0);
JS_SetPropertyFunctionList(ctx, profile_proto, js_profile_proto_funcs, ARRAYSIZE(js_profile_proto_funcs));
JS_SetConstructor(ctx, profile_class, profile_proto);
JS_SetClassProto(ctx, profile_class_id, profile_proto);
JS_SetModuleExport(ctx, m, "Profile", profile_class);
return TRUE;
}
int js_module_profile_export(JSContext *ctx, JSModuleDef *m)
{
JS_AddModuleExport(ctx, m, "Profile");
return TRUE;
}