File: low_level_system.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (409 lines) | stat: -rw-r--r-- 9,980 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "hpl1/engine/impl/SqScript.h"
#include "hpl1/engine/libraries/angelscript/add-ons/scriptarray.h"
#include "hpl1/engine/libraries/angelscript/add-ons/scriptstdstring.h"
#include "hpl1/engine/libraries/angelscript/angelscript.h"
#include "hpl1/engine/system/String.h"

#include "hpl1/debug.h"
#include "hpl1/hpl1.h"

#include "common/system.h"

namespace hpl {

static void scriptEngineLog(const asSMessageInfo *msg) {
	switch (msg->type) {
	case asMSGTYPE_ERROR:
		Hpl1::logError(Hpl1::kDebugScripts, "%s (%d, %d) : %s\n", msg->section, msg->row, msg->col, msg->message);
		break;
	case asMSGTYPE_WARNING:
		Hpl1::logWarning(Hpl1::kDebugScripts, "%s (%d, %d) : %s\n", msg->section, msg->row, msg->col, msg->message);
		break;
	case asMSGTYPE_INFORMATION:
		Hpl1::logInfo(Hpl1::kDebugScripts, "%s (%d, %d) : %s\n", msg->section, msg->row, msg->col, msg->message);
	}
}

LowLevelSystem::LowLevelSystem() {
	_scriptEngine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
	RegisterScriptArray(_scriptEngine, true);
	_scriptEngine->SetMessageCallback(asFunctionPtr(scriptEngineLog), nullptr, asCALL_CDECL);
	RegisterStdString(_scriptEngine);
	_handleCount = 0;
}

LowLevelSystem::~LowLevelSystem() {
	/*Release all runnings contexts */
	cleanupRegisteredString();
	_scriptEngine->Release();
	// perhaps not the best thing to skip :)
	// if(gpLogWriter)	hplDelete(gpLogWriter);
	// gpLogWriter = NULL;
}

static void commonLog(int level, const char *fmt, va_list args) {
	char buffer[256];
	vsnprintf(buffer, 256, fmt, args);
	debugN(level, "%s", buffer);
}

void Error(const char *fmt, ...) {
	va_list args;
	va_start(args, fmt);
	commonLog(Hpl1::kDebugLevelError, fmt, args);
	va_end(args);
}

void Warning(const char *fmt, ...) {
	va_list args;
	va_start(args, fmt);
	commonLog(Hpl1::kDebugLevelWarning, fmt, args);
	va_end(args);
}

void Log(const char *fmt, ...) {
	va_list args;
	va_start(args, fmt);
	commonLog(Hpl1::kDebugLevelLog, fmt, args);
	va_end(args);
}

static bool gbUpdateLogIsActive;
void SetUpdateLogActive(bool abX) {
	gbUpdateLogIsActive = abX;
}

void LogUpdate(const char *fmt, ...) {
#if 0
	if (!gbUpdateLogIsActive)
		return;

	char text[2048];
	va_list ap;
	if (fmt == NULL)
		return;
	va_start(ap, fmt);
	vsnprintf(text, 2048, fmt, ap);
	va_end(ap);

	tString sMess = "";
	sMess += text;
	gUpdateLogWriter.Write(sMess);
#endif
}

//-----------------------------------------------------------------------

void CopyTextToClipboard(const tWString &text) {
	g_system->setTextInClipboard(text);
}

tWString LoadTextFromClipboard() {
	Common::U32String text = g_system->getTextFromClipboard();
	return text;
}

//-----------------------------------------------------------------------

void CreateMessageBoxW(eMsgBoxType eType, const wchar_t *caption, const wchar_t *fmt, va_list ap) {
}

void CreateMessageBoxW(eMsgBoxType eType, const wchar_t *caption, const wchar_t *fmt, ...) {
}

void CreateMessageBoxW(const wchar_t *caption, const wchar_t *fmt, ...) {
}

//-----------------------------------------------------------------------

/*static cDate DateFromGMTIme(struct tm *apClock) {
	cDate date;

	date.seconds = apClock->tm_sec;
	date.minutes = apClock->tm_min;
	date.hours = apClock->tm_hour;
	date.month_day = apClock->tm_mday;
	date.month = apClock->tm_mon;
	date.year = 1900 + apClock->tm_year;
	date.week_day = apClock->tm_wday;
	date.year_day = apClock->tm_yday;

	return date;
}*/

//-----------------------------------------------------------------------

void OpenBrowserWindow(const tWString &URL) {
	g_system->openUrl(URL);
}

//-----------------------------------------------------------------------

tWString GetSystemSpecialPath(eSystemPath aPathType) {
#if 0
#if defined(WIN32)
		int type;
		switch(aPathType)
		{
		case eSystemPath_Personal:	type = CSIDL_PERSONAL;
			break;
		default: return _W("");
		}

		TCHAR sPath[1024];
		if(SUCCEEDED(SHGetFolderPath(NULL,
			type | CSIDL_FLAG_CREATE,
			NULL,0,sPath)))
		{
			return tWString(sPath);
		}
		else
		{
			return _W("");
		}
#else
		switch (aPathType)
		{
		case eSystemPath_Personal: {
			const char *home = getenv("HOME");
			return cString::To16Char(tString(home));
		}
		default:
			return _W("");
		}
#endif
#endif
	return Common::U32String("");
}

//-----------------------------------------------------------------------

bool FileExists(const tWString &fileName) {
#if 0
#ifdef WIN32
		FILE *f = _wfopen(fileName.c_str(),_W("r"));
#else
		FILE *f = fopen(cString::To8Char(fileName).c_str(),"r");
#endif
		if(f==NULL)
		{
			return false;
		}

		fclose(f);
		return true;
#endif
	return false;
}

//-----------------------------------------------------------------------

void RemoveFile(const tWString &filePath) {
#if 0
#ifdef WIN32
		_wremove(filePath.c_str());
#else
		remove(cString::To8Char(filePath).c_str());
#endif
#endif
}

//-----------------------------------------------------------------------

bool CloneFile(const tWString &srcFileName, const tWString &destFileName,
			   bool abFailIfExists) {
#if 0
#ifdef WIN32
		return CopyFile(srcFileName.c_str(),destFileName.c_str(),abFailIfExists)==TRUE;
#else
		if (abFailIfExists && FileExists(destFileName)) {
			return true;
		}
		std::ifstream IN (cString::To8Char(srcFileName).c_str(), std::ios::binary);
		std::ofstream OUT (cString::To8Char(destFileName).c_str(), std::ios::binary);
		OUT << IN.rdbuf();
		OUT.flush();
		return true;
#endif
#endif
	return false;
}

//-----------------------------------------------------------------------

bool CreateFolder(const tWString &path) {
#if 0
#ifdef WIN32
		return CreateDirectory(path.c_str(),NULL)==TRUE;
#else
		return mkdir(cString::To8Char(path).c_str(),0755)==0;
#endif
#endif
	return false;
}

bool FolderExists(const tWString &path) {
#if 0
#ifdef WIN32
		return GetFileAttributes(path.c_str())==FILE_ATTRIBUTE_DIRECTORY;
#else
		struct stat statbuf;
		return (stat(cString::To8Char(path).c_str(), &statbuf) != -1);
#endif
#endif
	return false;
}

bool IsFileLink(const tWString &path) {
#if 0
		// Symbolic Links Not Supported under Windows
#ifndef WIN32
		struct stat statbuf;
		if (lstat(cString::To8Char(path).c_str(), &statbuf) == 0) {
			return statbuf.st_mode == S_IFLNK;
		} else {
			return false;
		}
#else
			return false;
#endif
#endif
	return false;
}

bool LinkFile(const tWString &pointsTo, const tWString &link) {
#if 0
		// Symbolic Links Not Supported under Windows
#ifndef WIN32
		return (symlink(cString::To8Char(pointsTo).c_str(), cString::To8Char(link).c_str()) == 0);
#else
		return false;
#endif
#endif
	return false;
}

bool RenameFile(const tWString &from, const tWString &to) {
#if 0
#ifdef WIN32
		return false;
#else
		return (rename(cString::To8Char(from).c_str(), cString::To8Char(to).c_str()) == 0);
#endif
#endif
	return false;
}

//-----------------------------------------------------------------------

cDate FileModifiedDate(const tWString &filePath) {
#if 0
  		struct tm* pClock;
#ifdef WIN32
		struct _stat attrib;
		_wstat(filePath.c_str(), &attrib);
#else
		struct stat attrib;
		stat(cString::To8Char(filePath).c_str(), &attrib);
#endif

		pClock = gmtime(&(attrib.st_mtime));	// Get the last modified time and put it into the time structure

		cDate date = DateFromGMTIme(pClock);

		return date;
#endif
	return {};
}

//-----------------------------------------------------------------------

cDate FileCreationDate(const tWString &filePath) {
#if 0
  		struct tm* pClock;
#ifdef WIN32
		struct _stat attrib;
		_wstat(filePath.c_str(), &attrib);
#else
		struct stat attrib;
		stat(cString::To8Char(filePath).c_str(), &attrib);
#endif

		pClock = gmtime(&(attrib.st_ctime));	// Get the last modified time and put it into the time structure

		cDate date = DateFromGMTIme(pClock);

		return date;
#endif

	return {};
}

//-----------------------------------------------------------------------

void SetWindowCaption(const tString &name) {
	g_system->setWindowCaption(Common::U32String(name.c_str()));
}

unsigned long GetApplicationTime() {
	return g_engine->getTotalPlayTime();
}

unsigned long LowLevelSystem::getTime() {
	return g_engine->getTotalPlayTime();
}

cDate LowLevelSystem::getDate() {
	TimeDate td;
	g_system->getTimeAndDate(td);
	return {td.tm_sec, td.tm_min, td.tm_hour, td.tm_mday, td.tm_mon, td.tm_year - 100, td.tm_wday, 0};
}

iScript *LowLevelSystem::createScript(const tString &name) {
	return hplNew(cSqScript, (name, _scriptEngine, _handleCount++));
}

bool LowLevelSystem::addScriptFunc(const tString &funcDecl, asGENFUNC_t pFunc, int callConv) {
	if (_scriptEngine->RegisterGlobalFunction(funcDecl.c_str(),
											  asFUNCTION(pFunc), callConv) < 0) {
		Hpl1::logError(Hpl1::kDebugScripts, "Couldn't add script function '%s'\n", funcDecl.c_str());
		return false;
	}

	return true;
}

bool LowLevelSystem::addScriptVar(const tString &varDecl, void *pVar) {
	if (_scriptEngine->RegisterGlobalProperty(varDecl.c_str(), pVar) < 0) {
		Error("Couldn't add var '%s'\n", varDecl.c_str());
		return false;
	}

	return true;
}

void LowLevelSystem::sleep(const unsigned int alMillisecs) {
}

} // namespace hpl