forked from coremaze/Cube-World-Commands-Mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
426 lines (374 loc) · 14.3 KB
/
main.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
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
#include "main.h"
#include "cwmods/cwmods.h"
#include <wchar.h>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
int serverAccessState = 0;
bool P2PRequestLogging = false;
bool blacklisting = false;
std::map<std::string, std::vector<long long>> home_map;
float GetMilliseconds(int hour, int minute) {
return (hour * 60 * 60 * 1000) + (minute * 60 * 1000);
}
void CommandsModMessage(const wchar_t* message) {
cube::Game* game = cube::GetGame();
game->PrintMessage(L"[");
game->PrintMessage(L"CommandsMod", 255, 140, 0);
game->PrintMessage(L"] ");
game->PrintMessage(message);
}
int DotsToMapCoord(long long coord) {
return (coord - 0x2000000) / (cube::DOTS_PER_BLOCK * cube::BLOCKS_PER_MAP_CHUNK);
}
long long MapCoordToDots(int coord) {
return ((long long)coord * cube::DOTS_PER_BLOCK * cube::BLOCKS_PER_MAP_CHUNK) + 0x2000000;
}
// https://stackoverflow.com/questions/9435385/split-a-string-using-c11
std::vector<std::string> split(const std::string &s, char delim) {
std::stringstream ss(s);
std::string item;
std::vector<std::string> elems;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
void InitHomeMap(){
std::ifstream myfile("Mods\\CommandsMod\\home.txt");
if(!myfile.is_open()) {
return;
}
std::string line;
while (getline(myfile, line)) {
std::vector<std::string> elements = split(line, ' ');
if (elements.size() == 4) {
std::string name = elements.at(0);
std::string x = elements.at(1);
std::string y = elements.at(2);
std::string z = elements.at(3);
try {
std::vector<long long> coords {std::stoll(x), std::stoll(y), std::stoll(z)};
home_map[name] = coords;
} catch ( ... ) {
continue;
}
}
}
}
void SetHomeMap(std::string alias, long long x, long long y, long long z) {
std::vector<long long> coords {x, y, z};
home_map[alias] = coords;
std::ofstream outfile("Mods\\CommandsMod\\home.txt");
if(outfile.is_open()) {
for(auto item = home_map.begin();item != home_map.end(); item++) {
auto value = item->second;
outfile<<item->first<<' '<<item->second[0]<<' '<<item->second[1]<<' '<<item->second[2]<<std::endl;
}
outfile.close();
}
}
std::vector<long long> GetHomePosition(std::string alias) {
auto iter = home_map.find(alias);
if(iter != home_map.end()) {
return iter->second;
}
return std::vector<long long>();
}
long long GetSteamIDFromAlias(std::string alias) {
std::ifstream myfile("Mods\\CommandsMod\\join.txt");
if(!myfile.is_open()) {
return -1;
}
std::string line;
while (getline(myfile, line)) {
std::vector<std::string> elements = split(line, ' ');
if (elements.size() == 2) {
std::string name = elements.at(0);
if (name.compare(alias)) {
continue;
}
std::string strID = elements.at(1);
long long ID;
try {
ID = std::stoll(strID, nullptr);
} catch ( ... ) {
continue;
}
return ID;
}
}
return 0;
}
bool IsBlockedID(long long steamID) {
std::ifstream myfile("Mods\\CommandsMod\\blacklist.txt");
if(!myfile.is_open()) {
return false;
}
std::string line;
while (getline(myfile, line)) {
long long ID;
try {
ID = std::stoll(line, nullptr);
} catch ( ... ) {
continue;
}
if (steamID == ID) {
return true;
}
}
return false;
}
void PrintCommandName(const wchar_t* name) {
cube::Game* game = cube::GetGame();
game->PrintMessage(name, 66, 161, 245);
}
void PrintCommandArg(const wchar_t* arg) {
cube::Game* game = cube::GetGame();
game->PrintMessage(arg, 139, 209, 42);
}
void PrintCommand(const wchar_t* command, const wchar_t* args, const wchar_t* message) {
cube::Game* game = cube::GetGame();
PrintCommandName(command);
game->PrintMessage(L" ");
if (wcslen(args) > 0) {
PrintCommandArg(args);
game->PrintMessage(L" ");
}
game->PrintMessage(L"- ");
game->PrintMessage(message);
game->PrintMessage(L"\n");
}
void Help(int page) {
wchar_t response[256];
switch (page) {
case 1:
swprintf(response, L"Help %d\n", page);
CommandsModMessage(response);
PrintCommand(L"/help", L"[number]", L"get help");
PrintCommand(L"/coords", L"", L"display world coords");
PrintCommand(L"/tp", L"<x> <y>", L"teleport in terms of map coords");
PrintCommand(L"/tp", L"<name>", L"teleport to the specified player location");
PrintCommand(L"/settime", L"<hour>:<minute>", L"set time");
PrintCommand(L"/name", L"<name>", L"change your name");
break;
case 2:
swprintf(response, L"Help %d\n", page);
CommandsModMessage(response);
PrintCommand(L"/join", L"<Steam ID>", L"Attempt to connect by ID");
PrintCommand(L"/join", L"<alias>", L"Attempt to connect by alias");
PrintCommand(L"/server", L"open", L"Allow anyone to connect");
PrintCommand(L"/server", L"close", L"Stop allowing anyone to connect");
PrintCommand(L"/server", L"block", L"Refuse all new sessions");
PrintCommand(L"/server", L"blacklist", L"Enable blacklisting of new sessions");
PrintCommand(L"/server", L"log", L"Log new session requests");
break;
case 3:
swprintf(response, L"Help %d\n", page);
CommandsModMessage(response);
PrintCommand(L"/tpmap", L"", L"Teleport to cursor position on map");
PrintCommand(L"/sethome", L"<alias>", L"Set player position as home");
PrintCommand(L"/home", L"<alias>", L"Teleport to home position by alias");
//PrintCommand(L"/gui", L"chat <width> <height>", L"Set GUI Widget size");
break;
default:
CommandsModMessage( L"Invalid page number!\n");
break;
}
}
EXPORT int HandleChat(wchar_t* msg) {
cube::Game* game = cube::GetGame();
cube::Host* host = &game->host;
cube::World* world = &host->world;
wchar_t response[256];
int targetx, targety;
long int targetHour, targetMinute;
int page;
long long steamID;
if (!wcscmp(msg, L"/help")) {
Help(1);
return 1;
} else if ( swscanf(msg, L"/help %d", &page) == 1) {
Help(page);
return 1;
} else if ( swscanf(msg, L"/settime %d:%d", &targetHour, &targetMinute) == 2) {
printf("%ld:%ld\n", targetHour, targetMinute);
float targetMS = GetMilliseconds(targetHour, targetMinute);
float maxTime = GetMilliseconds(24, 0);
float minTime = GetMilliseconds(0, 0);
if (targetMS > maxTime)
targetMS = maxTime;
else if (targetMS < minTime)
targetMS = minTime;
world->time = targetMS;
swprintf(response, L"Changing time to %d:%02d.\n", targetHour % 24, targetMinute % 60);
CommandsModMessage(response);
return 1;
} else if (!wcscmp(msg, L"/coords")) {
cube::Creature* player = game->GetPlayer();
swprintf(response, L"World coordinates:\nX: %lld\nY: %lld\nZ: %lld\n", player->position.x, player->position.y, player->position.z);
CommandsModMessage(response);
return 1;
} else if ( swscanf(msg, L"/tp %d %d", &targetx, &targety) == 2) {
cube::Creature* player = game->GetPlayer();
player->position.x = MapCoordToDots(targetx);
player->position.y = MapCoordToDots(targety);
CommandsModMessage(L"Teleporting.\n");
return 1;
} else if ( swscanf(msg, L"/gui chat %d %d", &targetx, &targety) == 2) {
// TODO We can't yet update the position of chat without doing things like changing the window size.
game->gui.chat_widget->width = targetx;
game->gui.chat_widget->height = targety;
CommandsModMessage(L"GUI Chat Widget size changed.\n");
return 1;
} else if ( !wcsncmp(msg, L"/tp ", 4) ) {
wchar_t* wideName = &msg[4];
char* cName = new char[wcslen(wideName)+1];
int len = wcstombs(cName, wideName, 0x100);
if (len < 1) {
CommandsModMessage(L"Invalid name.\n");
return 1;
}
for(cube::Creature* creature: game->world->creatures) {
if (!stricmp(creature->name, cName)) {
cube::Creature* player = game->GetPlayer();
player->position = creature->position;
CommandsModMessage(L"Teleporting.\n");
return 1;
}
}
CommandsModMessage(L"Target not found.\n");
return 1;
} else if (!wcscmp(msg, L"/tpmap")) {
if(game->worldmap->cursor_position.x != 0x7FFFFFFF0000) {
LongVector3 position = game->worldmap->cursor_position;
cube::Creature* player = game->GetPlayer();
player->position = position;
CommandsModMessage(L"Teleporting.\n");
return 1;
}
CommandsModMessage(L"Please open map.\n");
return 1;
} else if ( !wcsncmp(msg, L"/sethome ", 9) ) {
wchar_t* wideAlias = &msg[9];
char* cAlias = new char[wcslen(wideAlias)+1];
int len = wcstombs(cAlias, wideAlias, 0x100);
if (len < 1) {
CommandsModMessage(L"Invalid alias.\n");
return 1;
}
std::string alias(cAlias);
cube::Creature* player = game->GetPlayer();
SetHomeMap(alias, player->position.x, player->position.y, player->position.z);
CommandsModMessage(L"Done.\n");
return 1;
} else if ( !wcsncmp(msg, L"/home ", 6) ) {
wchar_t* wideAlias = &msg[6];
char* cAlias = new char[wcslen(wideAlias)+1];
int len = wcstombs(cAlias, wideAlias, 0x100);
if (len < 1) {
CommandsModMessage(L"Invalid alias.\n");
return 1;
}
std::string alias(cAlias);
std::vector<long long> position = GetHomePosition(alias);
if(position.size() < 3) {
CommandsModMessage(L"Invalid alias.\n");
return 1;
}
cube::Creature* player = game->GetPlayer();
player->position.x = position[0];
player->position.y = position[1];
player->position.z = position[2];
CommandsModMessage(L"Teleporting.\n");
return 1;
} else if ( !wcsncmp(msg, L"/name ", 6) ) {
cube::Creature* player = game->GetPlayer();
wchar_t* wideName = &msg[6];
char newName[18];
memset(newName, 0, sizeof(newName));
int len = wcstombs(newName, wideName, 16);
if (len < 1) {
CommandsModMessage(L"Invalid name.\n");
} else if (len >= 16) {
CommandsModMessage(L"Name too long!\n");
} else {
strcpy(player->name, newName);
swprintf(response, L"You are now known as %s.\n", player->name);
CommandsModMessage(response);
}
return 1;
} else if (swscanf(msg, L"/join %lld", &steamID) == 1) {
game->client.JoinSteamID(steamID);
swprintf(response, L"Attempting to join:\n%lld\n", steamID);
CommandsModMessage(response);
return 1;
} else if ( !wcsncmp(msg, L"/join ", 6) ) {
wchar_t* wideAlias = &msg[6];
char* cAlias = new char[wcslen(wideAlias)+1];
int len = wcstombs(cAlias, wideAlias, 0x100);
if (len < 1) {
CommandsModMessage(L"Invalid alias.\n");
return 1;
}
std::string alias(cAlias);
steamID = GetSteamIDFromAlias(alias);
if (steamID > 0) {
swprintf(response, L"Attempting to join %ls\n(%lld)\n", wideAlias, steamID);
CommandsModMessage(response);
game->client.JoinSteamID(steamID);
} else {
swprintf(response, L"Unable to identify %ls\n", wideAlias);
CommandsModMessage(response);
}
delete[] cAlias;
return 1;
} else if (!wcscmp(msg, L"/server open")) {
serverAccessState = 2;
CommandsModMessage(L"The server is now opened to the public.\n");
return 1;
} else if (!wcscmp(msg, L"/server block")) {
serverAccessState = 1;
CommandsModMessage(L"The server is now blocking all P2P requests.\n");
return 1;
} else if (!wcscmp(msg, L"/server close")) {
serverAccessState = 0;
CommandsModMessage(L"The server is now closed to the public.\n");
return 1;
} else if (!wcscmp(msg, L"/server log")) {
P2PRequestLogging = !P2PRequestLogging;
swprintf(response, L"Request logging is now %s.\n", P2PRequestLogging ? "enabled" : "disabled");
CommandsModMessage(response);
return 1;
} else if (!wcscmp(msg, L"/server blacklist")) {
/*
TODO: Try to close existing blacklisted connections?
HandleP2PRequest will only be triggered if the session is NEW.
There is an implicit acceptance of the request if the server has already communicated
with the client who is trying to connect. Therefore, we probably need to attempt to
close any of the blacklisted connections in order to properly ban someone.
*/
blacklisting = !blacklisting;
swprintf(response, L"Blacklisting is now %s.\n", blacklisting ? "enabled" : "disabled");
CommandsModMessage(response);
return 1;
}
return 0;
}
EXPORT HandleP2PRequest(long long steamID) {
wchar_t response[256];
if (P2PRequestLogging) {
swprintf(response, L"Incoming request from:\n%lld", steamID);
CommandsModMessage(response);
}
if (blacklisting && IsBlockedID(steamID)) {
return 1;
}
return serverAccessState;
}
EXPORT void ModInitialize() {
InitHomeMap();
}