Subversion Repositories WoWGM

Rev

Rev 31 | Rev 34 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 31 Rev 32
Line 2... Line 2...
2
#pragma hdrstop
2
#pragma hdrstop
3
 
3
 
4
#include "ConsoleClient.h"
4
#include "ConsoleClient.h"
5
#include <Os/W32/OsFile.h>
5
#include <Os/W32/OsFile.h>
6
 
6
 
-
 
7
 
7
/****************************************************************************
8
/****************************************************************************
8
*
9
*
9
*   Client memory addresses
10
*   Client memory addresses
10
*
11
*
11
***/
12
***/
12
 
13
 
13
#define  CONSOLECOMMANDDESTROY  0x007685C0;
14
#define  CONSOLECOMMANDDESTROY  0x007685C0
14
 
15
 
15
 
16
 
16
/****************************************************************************
17
/****************************************************************************
17
*
18
*
18
*   Client symbol pointers
19
*   Client symbol pointers
Line 22... Line 23...
22
void (*ConsoleCommandDestroyPtr)() = *(void(*)())CONSOLECOMMANDDESTROY;
23
void (*ConsoleCommandDestroyPtr)() = *(void(*)())CONSOLECOMMANDDESTROY;
23
 
24
 
24
 
25
 
25
/****************************************************************************
26
/****************************************************************************
26
*
27
*
27
*   Privates
28
*   Private
28
*
29
*
29
***/
30
***/
30
 
31
 
31
static char s_fileName[260] = {};
32
static char s_fileName[260] = {};
32
 
33
 
Line 35... Line 36...
35
	// Remove the function trampoline
36
	// Remove the function trampoline
36
	DETOUR_INIT;
37
	DETOUR_INIT;
37
	DETOUR_DETACH(ConsoleCommandDestroyPtr,ConsoleCommandDestroy);
38
	DETOUR_DETACH(ConsoleCommandDestroyPtr,ConsoleCommandDestroy);
38
	DETOUR_COMMIT;
39
	DETOUR_COMMIT;
39
 
40
 
-
 
41
	// Unregister our own commands
40
	ConsoleCommandUnregister("new");
42
	ConsoleCommandUnregister("new");
41
	ConsoleCommandUnregister("run");
43
	ConsoleCommandUnregister("run");
42
	ConsoleCommandUnregister("end");
44
	ConsoleCommandUnregister("end");
43
	ConsoleCommandUnregister("ver");
45
	ConsoleCommandUnregister("ver");
44
 
46
 
45
	// Call the original function
47
	// Call the original function to handle clean-up
46
	ConsoleCommandDestroyPtr();
48
	ConsoleCommandDestroyPtr();
47
}
49
}
48
 
50
 
49
//===========================================================================
51
//===========================================================================
50
BOOL ValidateFileName (const char* fileName) {
52
BOOL ValidateFileName (char const* fileName) {
51
  if (fileName && *fileName) {
53
  if (fileName && *fileName) {
52
    if (strstr(fileName,"..") || strstr(fileName,"\\")) {
54
    if (strstr(fileName,"..") || strstr(fileName,"\\")) {
53
      ConsoleWrite("File Name cannot contain '\\' or '..'",ERROR_COLOR);
55
      ConsoleWrite("File Name cannot contain '\\' or '..'",ERROR_COLOR);
54
      return FALSE;
56
      return FALSE;
55
    }
57
    }
56
 
58
 
57
    const char* fileExtension = strrchr(fileName,'.');
59
    char const* fileExtension = strrchr(fileName,'.');
58
    if (fileExtension&& *fileExtension) {
60
    if (fileExtension&& *fileExtension) {
59
      if (_strcmpi(fileExtension,".wtf")) {
61
      if (_strcmpi(fileExtension,".wtf")) {
60
        ConsoleWrite("Only '.wtf' extensions are allowed",ERROR_COLOR);
62
        ConsoleWrite("Only '.wtf' extensions are allowed",ERROR_COLOR);
61
        return FALSE;
63
        return FALSE;
62
      }
64
      }
Line 98... Line 100...
98
*   Command definitions
100
*   Command definitions
99
*
101
*
100
***/
102
***/
101
 
103
 
102
//===========================================================================
104
//===========================================================================
103
BOOL  ConsoleCommand_CreateExec (const char* cmd, const char* arguments) {
105
BOOL ConsoleCommand_CreateExec (char const* cmd, char const* arguments) {
104
	if (ValidateFileName(arguments)) {
106
	if (ValidateFileName(arguments)) {
105
		char folder[FILENAME_MAX] = {};
107
		char folder[FILENAME_MAX] = {};
106
		char filePath[FILENAME_MAX] = {};
108
		char filePath[FILENAME_MAX] = {};
107
 
109
 
108
		SStrCopy(s_fileName,arguments,FILENAME_MAX);
110
		SStrCopy(s_fileName,arguments,FILENAME_MAX);
Line 123... Line 125...
123
				if (OsDirectoryExists(folder)) {
125
				if (OsDirectoryExists(folder)) {
124
					g_ExecCreateMode = EM_RECORDING;
126
					g_ExecCreateMode = EM_RECORDING;
125
					ConsoleWrite("Begin Typing the commands",ECHO_COLOR);
127
					ConsoleWrite("Begin Typing the commands",ECHO_COLOR);
126
				}
128
				}
127
				else {
129
				else {
128
					ConsoleWrite("Error! WTF folder does not exist.",ERROR_COLOR);
-
 
129
					g_ExecCreateMode = EM_NOTACTIVE;
130
					g_ExecCreateMode = EM_NOTACTIVE;
-
 
131
					ConsoleWrite("Error! WTF folder does not exist.",ERROR_COLOR);
130
				}
132
				}
131
			}
133
			}
132
		}
134
		}
133
	}
135
	}
-
 
136
 
134
	return FALSE;
137
	return FALSE;
135
}
138
}
136
 
139
 
137
//===========================================================================
140
//===========================================================================
138
BOOL ConsoleCommand_CloseExec (const char *cmd, const char *arguments) {
141
BOOL ConsoleCommand_CloseExec (char const* cmd, char const* arguments) {
139
	BOOL result = FALSE;
142
	BOOL result = FALSE;
-
 
143
 
140
	if (g_ExecCreateMode < EM_APPEND || g_ExecCreateMode > EM_WRITEFILE)
144
	if (g_ExecCreateMode < EM_APPEND || g_ExecCreateMode > EM_WRITEFILE)
141
	{
145
	{
142
		ConsoleWrite("You must type 'new' 'filename' to begin creating an Exec file",WARNING_COLOR);
146
		ConsoleWrite("You must type 'new' 'filename' to begin creating an Exec file",WARNING_COLOR);
143
		result = TRUE;
147
		result = TRUE;
144
	}
148
	}
145
	else
149
	else
146
	{
150
	{
147
		char filePath[FILENAME_MAX] = {};
151
		char filePath[FILENAME_MAX] = {};
148
		SStrCopy(filePath, s_fileName, FILENAME_MAX);
152
		SStrCopy(filePath, s_fileName, FILENAME_MAX);
149
 
153
 
150
		if (CreateWTFFilePath(filePath, FILENAME_MAX))
154
		if (CreateWTFFilePath(filePath, FILENAME_MAX)) {
151
		{
-
 
152
			HANDLE hFile = OsCreateFile(filePath,FILE_ALL_ACCESS,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,(HANDLE)0x3F3F3F3F);
155
			HANDLE hFile = OsCreateFile(filePath,FILE_ALL_ACCESS,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,(HANDLE)0x3F3F3F3F);
153
			if (hFile == INVALID_HANDLE_VALUE)
156
			if (hFile == INVALID_HANDLE_VALUE)
154
			{
-
 
155
				ConsoleWrite("Error trying to create the file.",ERROR_COLOR);
157
				ConsoleWrite("Error trying to create the file.",ERROR_COLOR);
156
			}
-
 
157
			else
158
			else {
158
			{
-
 
159
				if (g_ExecCreateMode == EM_APPEND)
159
				if (g_ExecCreateMode == EM_APPEND)
160
					OsSetFilePointer(hFile,CREATE_ALWAYS,0i64);
160
					OsSetFilePointer(hFile,CREATE_ALWAYS,0i64);
161
 
161
 
162
				DWORD count = 0;
162
				DWORD count = 0;
163
				DWORD bufLen = SStrLen(g_ExecBuffer);
163
				DWORD bufLen = SStrLen(g_ExecBuffer);
164
				COLOR_T color;
164
				COLOR_T color;
165
				const char *str;
165
				const char *str;
-
 
166
 
166
				OsWriteFile(hFile,g_ExecBuffer,bufLen,&count);
167
				OsWriteFile(hFile,g_ExecBuffer,bufLen,&count);
-
 
168
				
167
				if (count)
169
				if (count) {
168
				{
-
 
169
					color = ECHO_COLOR;
170
					color = ECHO_COLOR;
170
					str = "File written successfully";
171
					str = "File written successfully";
171
				}
172
				}
172
				else
173
				else {
173
				{
-
 
174
					color = ERROR_COLOR;
174
					color = ERROR_COLOR;
175
					str = "Error Writing ExecFile";
175
					str = "Error Writing ExecFile";
176
				}
176
				}
-
 
177
 
177
				ConsoleWrite(str,color);
178
				ConsoleWrite(str,color);
178
				OsCloseFile(hFile);
179
				OsCloseFile(hFile);
179
			}
180
			}
180
		}
181
		}
-
 
182
 
181
		g_ExecCreateMode = EM_NOTACTIVE;
183
		g_ExecCreateMode = EM_NOTACTIVE;
182
		memset(g_ExecBuffer,'\0',sizeof(g_ExecBuffer));
184
		memset(g_ExecBuffer,'\0',sizeof(g_ExecBuffer));
183
		result = TRUE;
185
		result = TRUE;
184
	}
186
	}
185
 
187
 
186
	return result;
188
	return result;
187
}
189
}
188
 
190
 
189
//===========================================================================
191
//===========================================================================
190
BOOL ConsoleCommand_RunExec (const char *command, const char *arguments) {
192
BOOL ConsoleCommand_RunExec (char const* command, char const* arguments) {
191
	BOOL result = FALSE;
193
	BOOL result = FALSE;
192
	char filename[FILENAME_MAX] = { '\0' };
194
	char filename[FILENAME_MAX] = { '\0' };
193
	char tmp[FILENAME_MAX] = { '\0' };
195
	char tmp[FILENAME_MAX] = { '\0' };
194
	char lineBuffer[128] = { '\0' };
196
	char lineBuffer[128] = { '\0' };
195
	char param1[32] = { '\0' };
197
	char param1[32] = { '\0' };
196
	DWORD bytes;
198
	DWORD bytes;
197
 
-
 
198
	int verbose = 0;
199
	int verbose = 0;
-
 
200
 
199
	if (sscanf_s(arguments, "%260s %32s", filename, sizeof(filename), param1, sizeof(param1)) != 0)
201
	if (sscanf_s(arguments, "%260s %32s", filename, sizeof(filename), param1, sizeof(param1)) != 0) {
200
	{
-
 
201
		if (!_strcmpi(param1, "verbose"))
202
		if (!_strcmpi(param1, "verbose"))
202
			verbose = 1;
203
			verbose = 1;
-
 
204
		
203
		result = CreateWTFFilePath(filename, 260u);
205
		result = CreateWTFFilePath(filename, 260u);
204
		if (result)
206
		if (result) {
205
		{
-
 
206
			HANDLE hFile = OsCreateFile(filename, GENERIC_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
207
			HANDLE hFile = OsCreateFile(filename, GENERIC_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
207
			DWORD fileSize = GetFileSize(hFile, NULL);
208
			DWORD fileSize = GetFileSize(hFile, NULL);
-
 
209
			
208
			char *readData = (char *)calloc(1, fileSize +1);
210
			char* readData = (char *)calloc(1, fileSize+1);
209
			if (!readData)
211
			if (!readData) {
210
			{
-
 
211
				ConsoleWriteA("ConsoleCommand_RunExec, Line %d: call to calloc() failed", ERROR_COLOR, __LINE__);
212
				ConsoleWriteA("ConsoleCommand_RunExec, Line %d: call to calloc() failed", ERROR_COLOR, __LINE__);
212
				return 0;
213
				return FALSE;
213
			}
214
			}
-
 
215
 
214
			if (OsReadFile(hFile, readData, fileSize, &bytes))
216
			if (OsReadFile(hFile, readData, fileSize, &bytes)) {
215
			{
-
 
216
				char *bufferPtr = (char *)calloc(1, bytes + 1);
217
				char* bufferPtr = (char *)calloc(1, bytes+1);
217
				if (!bufferPtr)
218
				if (!bufferPtr) {
218
				{
-
 
219
					ConsoleWriteA("ConsoleCommand_RunExec, Line %d: call to calloc() failed", ERROR_COLOR, __LINE__);
219
					ConsoleWriteA("ConsoleCommand_RunExec, Line %d: call to calloc() failed", ERROR_COLOR, __LINE__);
220
					return 0;
220
					return FALSE;
221
				}
221
				}
-
 
222
 
222
				memcpy((void*)bufferPtr, readData, bytes);
223
				memcpy((void*)bufferPtr, readData, bytes);
-
 
224
				
223
				if (bufferPtr)
225
				if (bufferPtr) {
224
				{
-
 
225
					char *token = 0;
226
					char* token = 0;
226
					char *nextToken = 0;
227
					char* nextToken = 0;
-
 
228
 
227
					token = strtok_s(bufferPtr, "\r\n", &nextToken);
229
					token = strtok_s(bufferPtr, "\r\n", &nextToken);
228
					if (token)
230
					if (token) {
229
					{
-
 
230
						do
231
						do {
231
						{
-
 
232
							SStrCopy(lineBuffer, token, 128u);
232
							SStrCopy(lineBuffer, token, 128u);
233
							if (lineBuffer[0])
-
 
234
							{
233
							
235
								if (verbose)
234
							if (lineBuffer[0]) {
236
								{
235
								if (verbose) {
237
									_snprintf(tmp, FILENAME_MAX, "Executing ->%s", lineBuffer);
236
									_snprintf(tmp, FILENAME_MAX, "Executing ->%s", lineBuffer);
238
									ConsoleWrite(tmp, ECHO_COLOR);
237
									ConsoleWrite(tmp, ECHO_COLOR);
239
								}
238
								}
-
 
239
 
240
								ConsoleCommandExecute(lineBuffer, 0);
240
								ConsoleCommandExecute(lineBuffer, 0);
241
							}
241
							}
-
 
242
							
242
							token = strtok_s(NULL, "\r\n", &nextToken);
243
							token = strtok_s(NULL, "\r\n", &nextToken);
243
						} while (token && *token);
244
						} while (token && *token);
244
					}
245
					}
245
					else
246
					else
246
					{
-
 
247
						result = 0;
247
						result = FALSE;
248
					}
248
 
249
					free((void*)bufferPtr);
249
					free((void*)bufferPtr);
250
				}
250
				}
251
				else
251
				else
252
				{
-
 
253
					result = 0;
252
					result = FALSE;
254
				}
-
 
255
			}
253
			}
256
			else
254
			else {
257
			{
-
 
258
				if (*command)
255
				if (*command)
259
					_snprintf(tmp, FILENAME_MAX, "Unable to load file %s", filename);
256
					_snprintf(tmp, FILENAME_MAX, "Unable to load file %s", filename);
260
				else
257
				else
261
					_snprintf(tmp, FILENAME_MAX, "Unknown command: %s", arguments);
258
					_snprintf(tmp, FILENAME_MAX, "Unknown command: %s", arguments);
-
 
259
				
262
				ConsoleWrite(tmp, ERROR_COLOR);
260
				ConsoleWrite(tmp, ERROR_COLOR);
263
				result = 0;
261
				result = FALSE;
264
			}
262
			}
-
 
263
 
265
			OsCloseFile(hFile);
264
			OsCloseFile(hFile);
266
			free(readData);
265
			free(readData);
267
		}
266
		}
268
	}
267
	}
269
	else
268
	else {
270
	{
-
 
271
		ConsoleWrite("Invalid number of parameters", ERROR_COLOR);
269
		ConsoleWrite("Invalid number of parameters", ERROR_COLOR);
272
		ConsoleCommandWriteHelp(command);
270
		ConsoleCommandWriteHelp(command);
273
	}
271
	}
274
 
272
 
275
	return result;
273
	return result;
Line 286... Line 284...
286
}
284
}
287
 
285
 
288
 
286
 
289
/****************************************************************************
287
/****************************************************************************
290
*
288
*
291
*   External functions
289
*   External
292
*
290
*
293
***/
291
***/
294
 
292
 
295
//===========================================================================
293
//===========================================================================
296
void ConsoleCommandInitialize () {
294
void ConsoleCommandInitialize () {
297
	// This function is called internally,
-
 
298
	// so only setup a trampoline for ConsoleCommandDestroy()
-
 
299
	DETOUR_INIT;
295
	DETOUR_INIT;
300
	DETOUR_ATTACH(ConsoleCommandDestroyPtr,ConsoleCommandDestroy);
296
	DETOUR_ATTACH(ConsoleCommandDestroyPtr,ConsoleCommandDestroy);
301
	DETOUR_COMMIT;
297
	DETOUR_COMMIT;
302
 
298
 
303
	// Replace the client's "ver" command with our own
-
 
304
	ConsoleCommandUnregister("ver");
299
	ConsoleCommandUnregister("ver");
305
	ConsoleCommandRegister("ver",ConsoleCommand_Ver,DEFAULT,NOHELP); 
300
	ConsoleCommandRegister("ver",ConsoleCommand_Ver,DEFAULT,NOHELP);
306
 
-
 
307
	ConsoleCommandRegister("new",ConsoleCommand_CreateExec,CONSOLE,"[File Name] starts recording a new script");
301
	ConsoleCommandRegister("new",ConsoleCommand_CreateExec,CONSOLE,"[File Name] starts recording a new script");
308
	ConsoleCommandRegister("run",ConsoleCommand_RunExec,CONSOLE,"[File Name] Runs a wtf file from the WTF folder");
302
	ConsoleCommandRegister("run",ConsoleCommand_RunExec,CONSOLE,"[File Name] Runs a wtf file from the WTF folder");
309
	ConsoleCommandRegister("end",ConsoleCommand_CloseExec,CONSOLE,"Stops recording script");
303
	ConsoleCommandRegister("end",ConsoleCommand_CloseExec,CONSOLE,"Stops recording script");
310
}
304
}
-
 
305