aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/kernel/kernel_script.cpp
blob: 27a221d45f1e8e18dcb4dcfacdbfc2327b4973ee (plain)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

/*
 * This code is based on Broken Sword 2.5 engine
 *
 * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
 *
 * Licensed under GNU GPL v2
 *
 */

#include "sword25/kernel/common.h"
#include "sword25/kernel/kernel.h"
#include "sword25/kernel/filesystemutil.h"
#include "sword25/kernel/resmanager.h"
#include "sword25/kernel/persistenceservice.h"
#include "sword25/script/script.h"
#include "sword25/script/luabindhelper.h"

namespace Sword25 {

// Marks a function that should never be used
static int dummyFuncError(lua_State *L) {
	error("Dummy function invoked by LUA");
	return 1;
}

static int getMilliTicks(lua_State *L) {
	Kernel *pKernel = Kernel::getInstance();
	assert(pKernel);

	lua_pushnumber(L, pKernel->getMilliTicks());

	return 1;
}

static int getTimer(lua_State *L) {
	Kernel *pKernel = Kernel::getInstance();
	assert(pKernel);

	lua_pushnumber(L, static_cast<lua_Number>(pKernel->getMilliTicks()) / 1000.0);

	return 1;
}

static int startService(lua_State *L) {
	// This function is used by system/boot.lua to init all services.
	// However, we do nothing here, as we just hard code the init sequence.
	lua_pushbooleancpp(L, true);

	return 1;
}

static int sleep(lua_State *L) {
	Kernel *pKernel = Kernel::getInstance();
	assert(pKernel);
	pKernel->sleep(static_cast<uint>(luaL_checknumber(L, 1) * 1000));
	return 0;
}

static int crash(lua_State *L) {
	Kernel *pKernel = Kernel::getInstance();
	assert(pKernel);
	pKernel->crash();
	return 0;
}

static int executeFile(lua_State *L) {
	Kernel *pKernel = Kernel::getInstance();
	assert(pKernel);
	ScriptEngine *pSE = pKernel->getScript();
	assert(pSE);

	lua_pushbooleancpp(L, pSE->executeFile(luaL_checkstring(L, 1)));

	return 0;
}

static int getUserdataDirectory(lua_State *L) {
	lua_pushstring(L, FileSystemUtil::getUserdataDirectory().c_str());
	return 1;
}

static int getPathSeparator(lua_State *L) {
	lua_pushstring(L, FileSystemUtil::getPathSeparator().c_str());
	return 1;
}

static int fileExists(lua_State *L) {
	lua_pushbooleancpp(L, FileSystemUtil::fileExists(luaL_checkstring(L, 1)));
	return 1;
}

static int createDirectory(lua_State *L) {
	// ScummVM engines cannot create directories, so we do nothing here.
	lua_pushbooleancpp(L, false);
	return 1;
}

static int getWinCode(lua_State *L) {
	lua_pushstring(L, "ScummVM");
	return 1;
}

static int getSubversionRevision(lua_State *L) {
	// ScummVM is 1337
	lua_pushnumber(L, 1337);
	return 1;
}

static int getUsedMemory(lua_State *L) {
	// It doesn't really matter what this call returns,
	// as it's used in a debug function.
	lua_pushnumber(L, 0);
	return 1;
}

static const char *KERNEL_LIBRARY_NAME = "Kernel";

static const luaL_reg KERNEL_FUNCTIONS[] = {
	{"DisconnectService", dummyFuncError},
	{"GetActiveServiceIdentifier", dummyFuncError},
	{"GetSuperclassCount", dummyFuncError},
	{"GetSuperclassIdentifier", dummyFuncError},
	{"GetServiceCount", dummyFuncError},
	{"GetServiceIdentifier", dummyFuncError},
	{"GetMilliTicks", getMilliTicks},
	{"GetTimer", getTimer},
	{"StartService", startService},
	{"Sleep", sleep},
	{"Crash", crash},
	{"ExecuteFile", executeFile},
	{"GetUserdataDirectory", getUserdataDirectory},
	{"GetPathSeparator", getPathSeparator},
	{"FileExists", fileExists},
	{"CreateDirectory", createDirectory},
	{"GetWinCode", getWinCode},
	{"GetSubversionRevision", getSubversionRevision},
	{"GetUsedMemory", getUsedMemory},
	{0, 0}
};

static int isVisible(lua_State *L) {
	// This function apparently is not used by the game scripts
	lua_pushbooleancpp(L, true);

	return 1;
}

static int setVisible(lua_State *L) {
	// This function apparently is not used by the game scripts
//	pWindow->setVisible(lua_tobooleancpp(L, 1));

	return 0;
}

static int getX(lua_State *L) {
	// This function apparently is not used by the game scripts
	lua_pushnumber(L, 0);

	return 1;
}

static int getY(lua_State *L) {
	// This function apparently is not used by the game scripts
	lua_pushnumber(L, 0);

	return 1;
}

static int setX(lua_State *L) {
	// This is called by system/boot.lua with -1 as value.
//	pWindow->setX(static_cast<int>(luaL_checknumber(L, 1)));

	return 0;
}

static int setY(lua_State *L) {
	// This is called by system/boot.lua with -1 as value.
//	pWindow->setY(static_cast<int>(luaL_checknumber(L, 1)));

	return 0;
}

static int getWidth(lua_State *L) {
	// This function apparently is not used by the game scripts
	lua_pushnumber(L, 800);

	return 1;
}

static int getHeight(lua_State *L) {
	// This function apparently is not used by the game scripts
	lua_pushnumber(L, 600);

	return 1;
}

static int setWidth(lua_State *L) {
	// This is called by system/boot.lua with 800 as value.
//	pWindow->setWidth(static_cast<int>(luaL_checknumber(L, 1)));

	return 0;
}

static int setHeight(lua_State *L) {
	// This is called by system/boot.lua with 600 as value.
//	pWindow->setHeight(static_cast<int>(luaL_checknumber(L, 1)));

	return 0;
}

static int getTitle(lua_State *L) {
	// This function apparently is not used by the game scripts
	lua_pushstring(L, "");

	return 1;
}

static int setTitle(lua_State *L) {
	// This is called by system/boot.lua and system/menu.lua, to
	// set the window title to the (localized) game name.
	// FIXME: Should we call OSystem::setWindowCaption() here?
//	pWindow->setTitle(luaL_checkstring(L, 1));

	return 0;
}

static int processMessages(lua_State *L) {
	// This is called by the main loop in system/boot.lua,
	// and the game keeps running as true is returned here.
	// It terminates if we return false.

	// TODO: We could do more stuff here if desired...

	// TODO: We could always return true here, and leave quit handling
	// to the closeWanted() opcode; see also the TODO comment in there.

	lua_pushbooleancpp(L, !Engine::shouldQuit());

	return 1;
}

static int closeWanted(lua_State *L) {
	// This is called by system/interface.lua to determine whether the
	// user requested the game to close (e.g. by clicking the 'close' widget
	// of the game window). As a consequence (i.e. this function returns true),
	// a quit confirmation dialog is shown.

	// TODO: ScummVM currently has a bug / misfeature where some engines provide
	// quit confirmation dialogs, some don't; in addition, we have a global confirmation
	// dialog (but the user has to explicitly activate that in the config).
	// Anyway, this can lead to *two* confirmation dialogs being shown.
	// If it wasn't for that, we could simply check for Engine::shouldQuit() here,
	// and then invoke EventMan::resetQuit. But currently this would result in
	// the user seeing two confirmation dialogs. Bad.
	lua_pushbooleancpp(L, false);

	return 1;
}

static const char *WINDOW_LIBRARY_NAME = "Window";

static const luaL_reg WINDOW_FUNCTIONS[] = {
	{"IsVisible", isVisible},
	{"SetVisible", setVisible},
	{"GetX", getX},
	{"SetX", setX},
	{"GetY", getY},
	{"SetY", setY},
	{"GetClientX", dummyFuncError},
	{"GetClientY", dummyFuncError},
	{"GetWidth", getWidth},
	{"GetHeight", getHeight},
	{"SetWidth", setWidth},
	{"SetHeight", setHeight},
	{"GetTitle", getTitle},
	{"SetTitle", setTitle},
	{"ProcessMessages", processMessages},
	{"CloseWanted", closeWanted},
	{"WaitForFocus", dummyFuncError},
	{"HasFocus", dummyFuncError},
	{0, 0}
};

static int precacheResource(lua_State *L) {
	Kernel *pKernel = Kernel::getInstance();
	assert(pKernel);
	ResourceManager *pResource = pKernel->getResourceManager();
	assert(pResource);

#ifdef PRECACHE_RESOURCES
	lua_pushbooleancpp(L, pResource->precacheResource(luaL_checkstring(L, 1)));
#else
	lua_pushbooleancpp(L, true);
#endif

	return 1;
}

static int forcePrecacheResource(lua_State *L) {
	Kernel *pKernel = Kernel::getInstance();
	assert(pKernel);
	ResourceManager *pResource = pKernel->getResourceManager();
	assert(pResource);

#ifdef PRECACHE_RESOURCES
	lua_pushbooleancpp(L, pResource->precacheResource(luaL_checkstring(L, 1), true));
#else
	lua_pushbooleancpp(L, true);
#endif

	return 1;
}

static int getMaxMemoryUsage(lua_State *L) {
	Kernel *pKernel = Kernel::getInstance();
	assert(pKernel);
	ResourceManager *pResource = pKernel->getResourceManager();
	assert(pResource);

	// This is used for debugging, so it doesn't really matter.
	// The default value set by the scripts is 256000000 bytes
	lua_pushnumber(L, 256000000);

	return 1;
}

static int setMaxMemoryUsage(lua_State *L) {
	Kernel *pKernel = Kernel::getInstance();
	assert(pKernel);
	ResourceManager *pResource = pKernel->getResourceManager();
	assert(pResource);

	// This call is ignored, we set a limit on the number of
	// simultaneous resources loaded instead.

	return 0;
}

static int emptyCache(lua_State *L) {
	Kernel *pKernel = Kernel::getInstance();
	assert(pKernel);
	ResourceManager *pResource = pKernel->getResourceManager();
	assert(pResource);

	pResource->emptyCache();

	return 0;
}

static int dumpLockedResources(lua_State *L) {
	Kernel *pKernel = Kernel::getInstance();
	assert(pKernel);
	ResourceManager *pResource = pKernel->getResourceManager();
	assert(pResource);

	pResource->dumpLockedResources();

	return 0;
}

static const char *RESOURCE_LIBRARY_NAME = "Resource";

static const luaL_reg RESOURCE_FUNCTIONS[] = {
	{"PrecacheResource", precacheResource},
	{"ForcePrecacheResource", forcePrecacheResource},
	{"GetMaxMemoryUsage", getMaxMemoryUsage},
	{"SetMaxMemoryUsage", setMaxMemoryUsage},
	{"EmptyCache", emptyCache},
	{"IsLogCacheMiss", dummyFuncError},
	{"SetLogCacheMiss", dummyFuncError},
	{"DumpLockedResources", dumpLockedResources},
	{0, 0}
};

static int reloadSlots(lua_State *L) {
	PersistenceService::getInstance().reloadSlots();
	lua_pushnil(L);
	return 1;
}

static int getSlotCount(lua_State *L) {
	lua_pushnumber(L, PersistenceService::getInstance().getSlotCount());
	return 1;
}

static int isSlotOccupied(lua_State *L) {
	lua_pushbooleancpp(L, PersistenceService::getInstance().isSlotOccupied(static_cast<uint>(luaL_checknumber(L, 1)) - 1));
	return 1;
}

static int getSavegameDirectory(lua_State *L) {
	lua_pushstring(L, PersistenceService::getInstance().getSavegameDirectory().c_str());
	return 1;
}

static int isSavegameCompatible(lua_State *L) {
	lua_pushbooleancpp(L, PersistenceService::getInstance().isSavegameCompatible(
	                       static_cast<uint>(luaL_checknumber(L, 1)) - 1));
	return 1;
}

static int getSavegameDescription(lua_State *L) {
	lua_pushstring(L, PersistenceService::getInstance().getSavegameDescription(
	                   static_cast<uint>(luaL_checknumber(L, 1)) - 1).c_str());
	return 1;
}

static int getSavegameFilename(lua_State *L) {
	lua_pushstring(L, PersistenceService::getInstance().getSavegameFilename(static_cast<uint>(luaL_checknumber(L, 1)) - 1).c_str());
	return 1;
}

static int loadGame(lua_State *L) {
	lua_pushbooleancpp(L, PersistenceService::getInstance().loadGame(static_cast<uint>(luaL_checknumber(L, 1)) - 1));
	return 1;
}

static int saveGame(lua_State *L) {
	lua_pushbooleancpp(L, PersistenceService::getInstance().saveGame(static_cast<uint>(luaL_checknumber(L, 1)) - 1, luaL_checkstring(L, 2)));
	return 1;
}

static const char *PERSISTENCE_LIBRARY_NAME = "Persistence";

static const luaL_reg PERSISTENCE_FUNCTIONS[] = {
	{"ReloadSlots", reloadSlots},
	{"GetSlotCount", getSlotCount},
	{"IsSlotOccupied", isSlotOccupied},
	{"GetSavegameDirectory", getSavegameDirectory},
	{"IsSavegameCompatible", isSavegameCompatible},
	{"GetSavegameDescription", getSavegameDescription},
	{"GetSavegameFilename", getSavegameFilename},
	{"LoadGame", loadGame},
	{"SaveGame", saveGame},
	{0, 0}
};

bool Kernel::registerScriptBindings() {
	ScriptEngine *pScript = getScript();
	assert(pScript);
	lua_State *L = static_cast<lua_State *>(pScript->getScriptObject());
	assert(L);

	if (!LuaBindhelper::addFunctionsToLib(L, KERNEL_LIBRARY_NAME, KERNEL_FUNCTIONS)) return false;
	if (!LuaBindhelper::addFunctionsToLib(L, WINDOW_LIBRARY_NAME, WINDOW_FUNCTIONS)) return false;
	if (!LuaBindhelper::addFunctionsToLib(L, RESOURCE_LIBRARY_NAME, RESOURCE_FUNCTIONS)) return false;
	if (!LuaBindhelper::addFunctionsToLib(L, PERSISTENCE_LIBRARY_NAME, PERSISTENCE_FUNCTIONS)) return false;

	return true;
}

} // End of namespace Sword25