-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInterpreterController.cpp
155 lines (133 loc) · 3.45 KB
/
InterpreterController.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
#include "stdafx.h"
#include "InterpreterController.h"
#include <windows.h>
#include <Tlhelp32.h>
#include <atlconv.h>
#include <afxconv.h>
#include <string>
extern "C" {
#include "instead\instead.h"
}
#pragma setlocale("Russian_Russia.1251")
CString InterpreterController::m_gameFile=L"";
CString InterpreterController::m_lastCommand=L"";
bool InterpreterController::m_wasCommand=false;
InterpreterController::InterpreterController(void)
{
}
InterpreterController::~InterpreterController(void)
{
}
/*
static void killProcessByName(CString filename)
{
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof (pEntry);
BOOL hRes = Process32First(hSnapShot, &pEntry);
while (hRes)
{
if (wcscmp(pEntry.szExeFile, filename) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
(DWORD) pEntry.th32ProcessID);
if (hProcess != NULL)
{
TerminateProcess(hProcess, 9);
CloseHandle(hProcess);
}
}
hRes = Process32Next(hSnapShot, &pEntry);
}
CloseHandle(hSnapShot);
}
*/
void InterpreterController::startGameFile(CString gameFile, CString gameName, int autolog)
{
startGameFile(gameFile,gameName,L"",autolog);
}
HANDLE hMachine = NULL;
DWORD tidMachine = 0;
void InterpreterController::startGameFile(CString gameFile, CString gameName, CString saveFile, int autolog)
{
//TODO: äîáàâèòü ñòàðò èãðû
CT2A ascii(gameFile);
instead_done();
instead_set_debug(1);
if (instead_init(ascii)==0)
{
char *str;
if (instead_load(&str) == 0)
{
m_gameFile = gameFile;
m_lastCommand = L"";
m_wasCommand = true;
}
}
}
CString InterpreterController::RunInterpreter(CString command)
{
CString Result;
//Äîáàâëÿåì êîìàíäó, íà âûäà÷ó â èíòåðïðåòàòîð
CT2A ascii(command);
//writeToCommonOut(ascii);
m_lastCommand = command;
m_wasCommand = true;
return Result;
}
void InterpreterController::endInterpreter()
{
//killProcessByName(L"t2r32.exe");
}
//Çàãðóçêà ñîõðàíåíèÿ â èíòåðïðåòàòîð
bool InterpreterController::loadSave(CString fname)
{
startGameFile(m_gameFile,fname,1);
return true;
}
bool InterpreterController::saveGame(CString fname)
{
return CopyFile(L"temp\\last.sav",fname,FALSE);
}
CString InterpreterController::lastCommand()
{
return m_lastCommand;
}
void InterpreterController::clearNewCommandFlag()
{
m_wasCommand = false;
}
bool InterpreterController::wasNewCommand()
{
return m_wasCommand;
}
static bool Utf8ToCString( CString& cstr, const char* utf8Str )
{
size_t utf8StrLen = strlen(utf8Str);
if( utf8StrLen == 0 )
{
cstr.Empty();
return true;
}
LPWSTR ptr = cstr.GetBuffer(utf8StrLen+1);
// CString is UNICODE string so we decode
int newLen = MultiByteToWideChar(
CP_UTF8, 0,
utf8Str, utf8StrLen, ptr, utf8StrLen+1
);
if( !newLen )
{
cstr.ReleaseBuffer(0);
return false;
}
cstr.ReleaseBuffer(newLen);
return true;
}
std::string utf8_encode(const std::wstring &wstr)
{
if( wstr.empty() ) return std::string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo( size_needed, 0 );
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}