- Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathJSMenu.cpp
More file actions
Latest commit
183 lines (146 loc) · 5.87 KB
/
Copy pathJSMenu.cpp
File metadata and controls
183 lines (146 loc) · 5.87 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
#include"JSMenu.h"
//#include "Control.h"
#include"JSControl.h"
#include"Constants.h"
#include"Helpers.h"
#include"D2BS.h"
#include"Profile.h"
JSAPI_FUNC(my_login) {
JS_SET_RVAL(cx, vp, JSVAL_VOID);
if (ClientState() != ClientStateMenu)
returnJS_TRUE;
constwchar_t* profile = NULL;
char* error;
if (!JSVAL_IS_STRING(JS_ARGV(cx, vp)[0])) {
if (Vars.szProfile != NULL) {
profile = Vars.szProfile;
} else
THROW_ERROR(cx, "Invalid profile specified!");
} else {
profile = JS_GetStringCharsZ(cx, JSVAL_TO_STRING(JS_ARGV(cx, vp)[0]));
wcscpy_s(Vars.szProfile, 256, profile);
}
if (!profile)
THROW_ERROR(cx, "Could not get profile!");
if (!Profile::ProfileExists(profile))
THROW_ERROR(cx, "Profile does not exist!");
Profile* prof = newProfile(profile);
if (prof->login(&error) != 0) {
delete prof;
THROW_ERROR(cx, error);
}
delete prof;
returnJS_TRUE;
}
JSAPI_FUNC(my_selectChar) {
JS_SET_RVAL(cx, vp, JSVAL_NULL);
if (argc != 1 || !JSVAL_IS_STRING(JS_ARGV(cx, vp)[0]))
THROW_ERROR(cx, "Invalid parameters specified to selectCharacter");
constwchar_t* profile = JS_GetStringCharsZ(cx, JS_ValueToString(cx, JS_ARGV(cx, vp)[0]));
if (!Profile::ProfileExists(profile))
THROW_ERROR(cx, "Invalid profile specified");
wchar_t charname[24], file[_MAX_FNAME + MAX_PATH];
swprintf_s(file, _countof(file), L"%sd2bs.ini", Vars.szPath);
GetPrivateProfileStringW(profile, L"character", L"ERROR", charname, _countof(file), file);
JS_SET_RVAL(cx, vp, BOOLEAN_TO_JSVAL(OOG_SelectCharacter(charname)));
returnJS_TRUE;
}
JSAPI_FUNC(my_createGame) {
JS_SET_RVAL(cx, vp, JSVAL_NULL);
if (ClientState() != ClientStateMenu)
returnJS_TRUE;
constwchar_t *name = NULL, *pass = NULL;
jschar *jsname = NULL, *jspass = NULL;
int32 diff = 3;
if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "W/Wi", &jsname, &jspass, &diff)) {
JS_ReportError(cx, "Invalid arguments specified to createGame");
returnJS_FALSE;
}
name = JS_GetStringCharsZ(cx, JSVAL_TO_STRING(JS_ARGV(cx, vp)[0]));
pass = JS_GetStringCharsZ(cx, JSVAL_TO_STRING(JS_ARGV(cx, vp)[1]));
if (!pass)
pass = L"";
if (wcslen(name) > 15 || wcslen(pass) > 15)
THROW_ERROR(cx, "Invalid game name or password length");
if (!OOG_CreateGame(name, pass, diff))
THROW_ERROR(cx, "createGame failed");
returnJS_TRUE;
}
JSAPI_FUNC(my_joinGame) {
JS_SET_RVAL(cx, vp, JSVAL_NULL);
if (ClientState() != ClientStateMenu)
returnJS_TRUE;
jschar *jsname = NULL, *jspass = NULL;
constwchar_t *name = NULL, *pass = NULL;
if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "W/W", &jsname, &jspass)) {
JS_ReportError(cx, "Invalid arguments specified to createGame");
returnJS_FALSE;
}
name = JS_GetStringCharsZ(cx, JSVAL_TO_STRING(JS_ARGV(cx, vp)[0]));
pass = JS_GetStringCharsZ(cx, JSVAL_TO_STRING(JS_ARGV(cx, vp)[1]));
if (!pass)
pass = L"";
if (wcslen(name) > 15 || wcslen(pass) > 15)
THROW_ERROR(cx, "Invalid game name or password length");
if (!OOG_JoinGame(name, pass))
THROW_ERROR(cx, "joinGame failed");
returnJS_TRUE;
}
JSAPI_FUNC(my_addProfile) {
JS_SET_RVAL(cx, vp, JSVAL_NULL);
// validate the args...
constwchar_t *profile, *mode, *gateway, *username, *password, *charname;
profile = mode = gateway = username = password = charname = NULL;
int spdifficulty = 3;
if (argc < 6 || argc > 7)
THROW_ERROR(cx, "Invalid arguments passed to addProfile");
constwchar_t** args[] = {&profile, &mode, &gateway, &username, &password, &charname};
for (uint i = 0; i < 6; i++) {
if (!JSVAL_IS_STRING(JS_ARGV(cx, vp)[i])) {
THROW_ERROR(cx, "Invalid argument passed to addProfile");
} else
*args[i] = JS_GetStringCharsZ(cx, JSVAL_TO_STRING(JS_ARGV(cx, vp)[i]));
}
for (int i = 0; i < 6; i++) {
if (!(*args[i]))
THROW_ERROR(cx, "Failed to convert string");
}
if (argc == 7)
spdifficulty = JSVAL_TO_INT(JS_ARGV(cx, vp)[6]);
if (spdifficulty > 3 || spdifficulty < 0)
THROW_ERROR(cx, "Invalid argument passed to addProfile");
wchar_t file[_MAX_FNAME + _MAX_PATH];
swprintf_s(file, _countof(file), L"%sd2bs.ini", Vars.szPath);
if (!Profile::ProfileExists(*args[0])) {
wchar_t settings[600] = L"";
swprintf_s(settings, _countof(settings), L"mode=%s\tgateway=%s\tusername=%s\tpassword=%s\tcharacter=%s\tspdifficulty=%d\t", mode, gateway, username, password,
charname, spdifficulty);
StringReplace(settings, '\t', '\0', 600);
WritePrivateProfileSectionW(*args[0], settings, file);
}
returnJS_TRUE;
}
JSAPI_FUNC(my_getOOGLocation) {
JS_SET_RVAL(cx, vp, JSVAL_NULL);
if (ClientState() != ClientStateMenu)
returnJS_TRUE;
JS_SET_RVAL(cx, vp, INT_TO_JSVAL(OOG_GetLocation()));
returnJS_TRUE;
}
JSAPI_FUNC(my_createCharacter) {
if (ClientState() != ClientStateMenu)
returnJS_TRUE;
constwchar_t* name = NULL;
jschar* jsname = NULL;
int32 type = -1;
JSBool hc = JS_FALSE, ladder = JS_FALSE;
JS_BeginRequest(cx);
if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "Wi/bb", &jsname, &type, &hc, &ladder)) {
JS_EndRequest(cx);
THROW_ERROR(cx, "Failed to Convert Args createCharacter");
}
JS_EndRequest(cx);
name = JS_GetStringCharsZ(cx, JSVAL_TO_STRING(JS_ARGV(cx, vp)[0]));
JS_SET_RVAL(cx, vp, BOOLEAN_TO_JSVAL(!!OOG_CreateCharacter(name, type, !!hc, !!ladder)));
returnJS_TRUE;
}