| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name script.cpp - The configuration language. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2008 by Lutz Sammer, Jimmy Salmon and Joris Dauphin. |
| 13 |
// |
| 14 |
// This program is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
// |
| 28 |
|
| 29 |
//@{ |
| 30 |
|
| 31 |
/*---------------------------------------------------------------------------- |
| 32 |
-- Includes |
| 33 |
----------------------------------------------------------------------------*/ |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <string.h> |
| 37 |
#include <signal.h> |
| 38 |
|
| 39 |
#include "stratagus.h" |
| 40 |
|
| 41 |
#include "unittype.h" |
| 42 |
#include "iolib.h" |
| 43 |
#include "iocompat.h" |
| 44 |
#include "script.h" |
| 45 |
#include "missile.h" |
| 46 |
#include "upgrade.h" |
| 47 |
#include "construct.h" |
| 48 |
#include "map.h" |
| 49 |
#include "ui.h" |
| 50 |
#include "interface.h" |
| 51 |
#include "ai.h" |
| 52 |
#include "trigger.h" |
| 53 |
#include "editor.h" |
| 54 |
#include "sound.h" |
| 55 |
#include "netconnect.h" |
| 56 |
#include "network.h" |
| 57 |
#include "spells.h" |
| 58 |
#include "actions.h" |
| 59 |
#include "video.h" |
| 60 |
#include "upgrade_structs.h" |
| 61 |
#include "player.h" |
| 62 |
#include "replay.h" |
| 63 |
|
| 64 |
/*---------------------------------------------------------------------------- |
| 65 |
-- Variables |
| 66 |
----------------------------------------------------------------------------*/ |
| 67 |
|
| 68 |
lua_State *Lua; /// Structure to work with lua files. |
| 69 |
|
| 70 |
std::string CclStartFile; /// CCL start file |
| 71 |
std::string UserDirectory; |
| 72 |
int CclInConfigFile; /// True while config file parsing |
| 73 |
bool SaveGameLoading; /// If a Saved Game is Loading |
| 74 |
std::string CurrentLuaFile; /// Lua file currently being interpreted |
| 75 |
|
| 76 |
int NoRandomPlacementMultiplayer = 0; /// Disable the random placement of players in muliplayer mode |
| 77 |
|
| 78 |
/*---------------------------------------------------------------------------- |
| 79 |
-- Functions |
| 80 |
----------------------------------------------------------------------------*/ |
| 81 |
|
| 82 |
/** |
| 83 |
** FIXME: docu |
| 84 |
*/ |
| 85 |
static void lstop(lua_State *l, lua_Debug *ar) |
| 86 |
{ |
| 87 |
(void)ar; // unused arg. |
| 88 |
lua_sethook(l, NULL, 0, 0); |
| 89 |
luaL_error(l, "interrupted!"); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
** FIXME: docu |
| 94 |
*/ |
| 95 |
static void laction(int i) |
| 96 |
{ |
| 97 |
// if another SIGINT happens before lstop, |
| 98 |
// terminate process (default action) |
| 99 |
signal(i, SIG_DFL); |
| 100 |
lua_sethook(Lua, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
** Print error message and possibly exit. |
| 105 |
** |
| 106 |
** @param pname Source of the error. |
| 107 |
** @param msg error message to print. |
| 108 |
** @param exit exit the program |
| 109 |
*/ |
| 110 |
static void l_message(const char *pname, const char *msg, bool exit) |
| 111 |
{ |
| 112 |
if (pname) { |
| 113 |
fprintf(stderr, "%s: ", pname); |
| 114 |
} |
| 115 |
fprintf(stderr, "%s\n", msg); |
| 116 |
if (exit) { |
| 117 |
::exit(1); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
** Check error status, and print error message and exit |
| 123 |
** if status is different of 0. |
| 124 |
** |
| 125 |
** @param status status of the last lua call. (0: success) |
| 126 |
** @param exitOnError exit the program on error |
| 127 |
** |
| 128 |
** @return 0 in success, else exit. |
| 129 |
*/ |
| 130 |
static int report(int status, bool exitOnError) |
| 131 |
{ |
| 132 |
const char *msg; |
| 133 |
|
| 134 |
if (status) { |
| 135 |
msg = lua_tostring(Lua, -1); |
| 136 |
if (msg == NULL) { |
| 137 |
msg = "(error with no message)"; |
| 138 |
} |
| 139 |
l_message(NULL, msg, exitOnError); |
| 140 |
lua_pop(Lua, 1); |
| 141 |
} |
| 142 |
return status; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
** Gets a backtrace of the Lua call stack, by calling the |
| 147 |
** debug.traceback function. |
| 148 |
** |
| 149 |
** @return Number of return values in the Lua stack; always 1. |
| 150 |
** |
| 151 |
** @par Parameters in the Lua stack: |
| 152 |
** @li message (string) The error message last thrown. |
| 153 |
** |
| 154 |
** @par Return values in the Lua stack: |
| 155 |
** @li trace (string) Stack backtrace, with @a message prepended. |
| 156 |
*/ |
| 157 |
int luatraceback(lua_State *L) |
| 158 |
{ |
| 159 |
lua_pushliteral(L, "debug"); |
| 160 |
lua_gettable(L, LUA_GLOBALSINDEX); |
| 161 |
if (!lua_istable(L, -1)) { |
| 162 |
lua_pop(L, 1); |
| 163 |
return 1; |
| 164 |
} |
| 165 |
lua_pushliteral(L, "traceback"); |
| 166 |
lua_gettable(L, -2); |
| 167 |
if (!lua_isfunction(L, -1)) { |
| 168 |
lua_pop(L, 2); |
| 169 |
return 1; |
| 170 |
} |
| 171 |
lua_pushvalue(L, 1); // pass error message |
| 172 |
lua_pushnumber(L, 2); // skip this function and traceback |
| 173 |
lua_call(L, 2, 1); // call debug.traceback |
| 174 |
return 1; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
** Call a lua function |
| 179 |
** |
| 180 |
** @param narg Number of arguments |
| 181 |
** @param clear Clear the return value(s) |
| 182 |
** @param exitOnError Exit the program when an error occurs |
| 183 |
** |
| 184 |
** @return 0 in success, else exit. |
| 185 |
*/ |
| 186 |
int LuaCall(int narg, int clear, bool exitOnError) |
| 187 |
{ |
| 188 |
int status; |
| 189 |
int base; |
| 190 |
|
| 191 |
base = lua_gettop(Lua) - narg; // function index |
| 192 |
lua_pushcfunction(Lua, luatraceback); // push traceback function |
| 193 |
lua_insert(Lua, base); // put it under chunk and args |
| 194 |
signal(SIGINT, laction); |
| 195 |
status = lua_pcall(Lua, narg, (clear ? 0 : LUA_MULTRET), base); |
| 196 |
signal(SIGINT, SIG_DFL); |
| 197 |
lua_remove(Lua, base); // remove traceback function |
| 198 |
|
| 199 |
return report(status, exitOnError); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
** Load a file into a buffer |
| 204 |
*/ |
| 205 |
static void LuaLoadBuffer(const std::string &file, std::string &buffer) |
| 206 |
{ |
| 207 |
CFile fp; |
| 208 |
int size; |
| 209 |
int oldsize; |
| 210 |
int location; |
| 211 |
int read; |
| 212 |
char *buf; |
| 213 |
|
| 214 |
buffer.clear(); |
| 215 |
|
| 216 |
if (fp.open(file.c_str(), CL_OPEN_READ) == -1) { |
| 217 |
fprintf(stderr, "Can't open file '%s': %s\n", |
| 218 |
file.c_str(), strerror(errno)); |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
size = 10000; |
| 223 |
buf = new char[size]; |
| 224 |
if (!buf) { |
| 225 |
fprintf(stderr, "Out of memory\n"); |
| 226 |
ExitFatal(-1); |
| 227 |
} |
| 228 |
location = 0; |
| 229 |
for (;;) { |
| 230 |
read = fp.read(&buf[location], size - location); |
| 231 |
if (read != size - location) { |
| 232 |
location += read; |
| 233 |
break; |
| 234 |
} |
| 235 |
location += read; |
| 236 |
oldsize = size; |
| 237 |
size = size * 2; |
| 238 |
char *newb = new char[size]; |
| 239 |
if (!newb) { |
| 240 |
fprintf(stderr, "Out of memory\n"); |
| 241 |
ExitFatal(-1); |
| 242 |
} |
| 243 |
memcpy(newb, buf, oldsize); |
| 244 |
delete[] buf; |
| 245 |
buf = newb; |
| 246 |
} |
| 247 |
fp.close(); |
| 248 |
|
| 249 |
buffer.assign(buf, location); |
| 250 |
delete[] buf; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
** Load a file and execute it |
| 255 |
** |
| 256 |
** @param file File to load and execute |
| 257 |
** |
| 258 |
** @return 0 for success, else exit. |
| 259 |
*/ |
| 260 |
int LuaLoadFile(const std::string &file) |
| 261 |
{ |
| 262 |
int status; |
| 263 |
std::string PreviousLuaFile; |
| 264 |
std::string buf; |
| 265 |
|
| 266 |
PreviousLuaFile = CurrentLuaFile; |
| 267 |
CurrentLuaFile = file; |
| 268 |
|
| 269 |
LuaLoadBuffer(file, buf); |
| 270 |
if (buf.empty()) { |
| 271 |
return -1; |
| 272 |
} |
| 273 |
|
| 274 |
// "@" at the beginning of the chunk name makes Lua format |
| 275 |
// it as a file name in backtraces. |
| 276 |
if (!(status = luaL_loadbuffer(Lua, buf.c_str(), buf.size(), |
| 277 |
('@' + file).c_str()))) { |
| 278 |
LuaCall(0, 1); |
| 279 |
} else { |
| 280 |
report(status, true); |
| 281 |
} |
| 282 |
CurrentLuaFile = PreviousLuaFile; |
| 283 |
|
| 284 |
return status; |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
** Get the directory of the current lua file |
| 289 |
*/ |
| 290 |
static int CclGetCurrentLuaPath(lua_State *l) |
| 291 |
{ |
| 292 |
LuaCheckArgs(l, 0); |
| 293 |
|
| 294 |
std::string path = CurrentLuaFile; |
| 295 |
size_t index = path.rfind('/'); |
| 296 |
|
| 297 |
if (index != std::string::npos) { |
| 298 |
path = path.substr(0, index); |
| 299 |
lua_pushstring(l, path.c_str()); |
| 300 |
} else { |
| 301 |
lua_pushstring(l, ""); |
| 302 |
} |
| 303 |
return 1; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
** Save preferences |
| 308 |
** |
| 309 |
** @param l Lua state. |
| 310 |
*/ |
| 311 |
static int CclSavePreferences(lua_State *l) |
| 312 |
{ |
| 313 |
LuaCheckArgs(l, 0); |
| 314 |
SavePreferences(); |
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
** Load a file and execute it. |
| 320 |
** |
| 321 |
** @param l Lua state. |
| 322 |
** |
| 323 |
** @return 0 in success, else exit. |
| 324 |
*/ |
| 325 |
static int CclLoad(lua_State *l) |
| 326 |
{ |
| 327 |
char buf[1024]; |
| 328 |
|
| 329 |
LuaCheckArgs(l, 1); |
| 330 |
LibraryFileName(LuaToString(l, 1), buf, sizeof(buf)); |
| 331 |
if (LuaLoadFile(buf) == -1) { |
| 332 |
DebugPrint("Load failed: %s\n" _C_ LuaToString(l, 1)); |
| 333 |
} |
| 334 |
return 0; |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
** Load a file into a buffer and return it. |
| 339 |
** |
| 340 |
** @param l Lua state. |
| 341 |
** |
| 342 |
** @return buffer or nil on failure |
| 343 |
*/ |
| 344 |
static int CclLoadBuffer(lua_State *l) |
| 345 |
{ |
| 346 |
char file[1024]; |
| 347 |
std::string buf; |
| 348 |
|
| 349 |
LuaCheckArgs(l, 1); |
| 350 |
LibraryFileName(LuaToString(l, 1), file, sizeof(file)); |
| 351 |
LuaLoadBuffer(file, buf); |
| 352 |
if (!buf.empty()) { |
| 353 |
lua_pushstring(l, buf.c_str()); |
| 354 |
return 1; |
| 355 |
} |
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
** Load the SavedGameInfo Header |
| 361 |
** |
| 362 |
** @param l Lua state. |
| 363 |
*/ |
| 364 |
static int CclSavedGameInfo(lua_State *l) |
| 365 |
{ |
| 366 |
const char *value; |
| 367 |
|
| 368 |
LuaCheckArgs(l, 1); |
| 369 |
LuaCheckTable(l, 1); |
| 370 |
|
| 371 |
lua_pushnil(l); |
| 372 |
while (lua_next(l, 1)) { |
| 373 |
value = LuaToString(l, -2); |
| 374 |
|
| 375 |
if (!strcmp(value, "SaveFile")) { |
| 376 |
if (strcpy_s(CurrentMapPath, sizeof(CurrentMapPath), LuaToString(l, -1)) != 0) { |
| 377 |
LuaError(l, "SaveFile too long"); |
| 378 |
} |
| 379 |
std::string buf = StratagusLibPath; |
| 380 |
buf += "/"; |
| 381 |
buf += LuaToString(l, -1); |
| 382 |
if (LuaLoadFile(buf) == -1) { |
| 383 |
DebugPrint("Load failed: %s\n" _C_ value); |
| 384 |
} |
| 385 |
} else if (!strcmp(value, "SyncHash")) { |
| 386 |
SyncHash = LuaToNumber(l, -1); |
| 387 |
} else if (!strcmp(value, "SyncRandSeed")) { |
| 388 |
SyncRandSeed = LuaToNumber(l, -1); |
| 389 |
} else { |
| 390 |
LuaError(l, "Unsupported tag: %s" _C_ value); |
| 391 |
} |
| 392 |
lua_pop(l, 1); |
| 393 |
} |
| 394 |
|
| 395 |
return 0; |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
** Convert lua string to char*. |
| 400 |
** It also checks the type and exits in case of error. |
| 401 |
** |
| 402 |
** @note char* could be invalidated with lua garbage collector. |
| 403 |
** |
| 404 |
** @param l Lua state. |
| 405 |
** @param index Argument number. |
| 406 |
** |
| 407 |
** @return char* from lua. |
| 408 |
*/ |
| 409 |
const char *LuaToString(lua_State *l, int index) |
| 410 |
{ |
| 411 |
luaL_checktype(l, index, LUA_TSTRING); |
| 412 |
return lua_tostring(l, index); |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
** Convert lua string in a table to char*. |
| 417 |
** It also checks the type and exits in case of error. |
| 418 |
** |
| 419 |
** @note char* could be invalidated with lua garbage collector. |
| 420 |
** |
| 421 |
** @param l Lua state. |
| 422 |
** @param tableIndex Index number of the table. |
| 423 |
** @param stringIndex Index number of the string in the table. |
| 424 |
** |
| 425 |
** @return char* from lua. |
| 426 |
*/ |
| 427 |
const char *LuaToString(lua_State *l, int tableIndex, int stringIndex) |
| 428 |
{ |
| 429 |
const char *str; |
| 430 |
lua_rawgeti(l, tableIndex, stringIndex); |
| 431 |
luaL_checktype(l, -1, LUA_TSTRING); |
| 432 |
str = lua_tostring(l, -1); |
| 433 |
lua_pop(l, 1); |
| 434 |
return str; |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
** Convert lua number to int. |
| 439 |
** It also checks the type and exits in case of error. |
| 440 |
** |
| 441 |
** @param l Lua state. |
| 442 |
** @param index Argument number. |
| 443 |
** |
| 444 |
** @return int from lua. |
| 445 |
*/ |
| 446 |
int LuaToNumber(lua_State *l, int index) |
| 447 |
{ |
| 448 |
luaL_checktype(l, index, LUA_TNUMBER); |
| 449 |
return static_cast<int>(lua_tonumber(l, index)); |
| 450 |
} |
| 451 |
|
| 452 |
/** |
| 453 |
** Convert lua number in a table to int. |
| 454 |
** It also checks the type and exits in case of error. |
| 455 |
** |
| 456 |
** @param l Lua state. |
| 457 |
** @param tableIndex Index number of the table |
| 458 |
** @param numberIndex Index number of the string in the table. |
| 459 |
** |
| 460 |
** @return int from lua. |
| 461 |
*/ |
| 462 |
int LuaToNumber(lua_State *l, int tableIndex, int numberIndex) |
| 463 |
{ |
| 464 |
int num; |
| 465 |
lua_rawgeti(l, tableIndex, numberIndex); |
| 466 |
luaL_checktype(l, -1, LUA_TNUMBER); |
| 467 |
num = static_cast<int>(lua_tonumber(l, -1)); |
| 468 |
lua_pop(l, 1); |
| 469 |
return num; |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
** Convert lua boolean to bool. |
| 474 |
** It also checks the type and exits in case of error. |
| 475 |
** |
| 476 |
** @param l Lua state. |
| 477 |
** @param index Argument number. |
| 478 |
** |
| 479 |
** @return 1 for true, 0 for false from lua. |
| 480 |
*/ |
| 481 |
bool LuaToBoolean(lua_State *l, int index) |
| 482 |
{ |
| 483 |
luaL_checktype(l, index, LUA_TBOOLEAN); |
| 484 |
return lua_toboolean(l, index) != 0; |
| 485 |
} |
| 486 |
|
| 487 |
/** |
| 488 |
** Perform CCL garbage collection |
| 489 |
** |
| 490 |
** @param fast set this flag to disable slow GC (during game) |
| 491 |
*/ |
| 492 |
void CclGarbageCollect(int fast) |
| 493 |
{ |
| 494 |
DebugPrint("Garbage collect (before): %d\n" _C_ |
| 495 |
lua_gc(Lua, LUA_GCCOUNT, 0)); |
| 496 |
|
| 497 |
lua_gc(Lua, LUA_GCCOLLECT, 0); |
| 498 |
|
| 499 |
DebugPrint("Garbage collect (after): %d\n" _C_ |
| 500 |
lua_gc(Lua, LUA_GCCOUNT, 0)); |
| 501 |
} |
| 502 |
|
| 503 |
/*............................................................................ |
| 504 |
.. Config |
| 505 |
............................................................................*/ |
| 506 |
|
| 507 |
/** |
| 508 |
** Return the stratagus library path. |
| 509 |
** |
| 510 |
** @param l Lua state. |
| 511 |
** |
| 512 |
** @return Current libray path. |
| 513 |
*/ |
| 514 |
static int CclStratagusLibraryPath(lua_State *l) |
| 515 |
{ |
| 516 |
lua_pushstring(l, StratagusLibPath.c_str()); |
| 517 |
return 1; |
| 518 |
} |
| 519 |
|
| 520 |
/** |
| 521 |
** Return a table with the filtered items found in the subdirectory. |
| 522 |
*/ |
| 523 |
static int CclFilteredListDirectory(lua_State *l, int type, int mask) |
| 524 |
{ |
| 525 |
char directory[256]; |
| 526 |
const char *userdir; |
| 527 |
std::vector<FileList> flp; |
| 528 |
int n; |
| 529 |
int i; |
| 530 |
int j; |
| 531 |
int pathtype; |
| 532 |
|
| 533 |
LuaCheckArgs(l, 1); |
| 534 |
userdir = lua_tostring(l, 1); |
| 535 |
n = strlen(userdir); |
| 536 |
|
| 537 |
pathtype = 0; // path relative to stratagus dir |
| 538 |
if (n > 0 && *userdir == '~') { |
| 539 |
// path relative to user preferences directory |
| 540 |
pathtype = 1; |
| 541 |
} |
| 542 |
|
| 543 |
// security: disallow all special characters |
| 544 |
if (strpbrk(userdir, ":*?\"<>|") != 0 || strstr(userdir, "..") != 0) { |
| 545 |
LuaError(l, "Forbidden directory"); |
| 546 |
} |
| 547 |
|
| 548 |
if (pathtype == 1) { |
| 549 |
++userdir; |
| 550 |
sprintf_s(directory, sizeof(directory), "%s/%s", UserDirectory.c_str(), userdir); |
| 551 |
} else { |
| 552 |
sprintf_s(directory, sizeof(directory), "%s/%s", StratagusLibPath.c_str(), userdir); |
| 553 |
} |
| 554 |
lua_pop(l, 1); |
| 555 |
lua_newtable(l); |
| 556 |
n = ReadDataDirectory(directory, NULL, flp); |
| 557 |
for (i = 0, j = 0; i < n; ++i) { |
| 558 |
if ((flp[i].type & mask) == type) { |
| 559 |
lua_pushnumber(l, j + 1); |
| 560 |
lua_pushstring(l, flp[i].name); |
| 561 |
lua_settable(l, 1); |
| 562 |
++j; |
| 563 |
} |
| 564 |
delete[] flp[i].name; |
| 565 |
} |
| 566 |
|
| 567 |
return 1; |
| 568 |
} |
| 569 |
|
| 570 |
/** |
| 571 |
** Return a table with the files or directories found in the subdirectory. |
| 572 |
*/ |
| 573 |
static int CclListDirectory(lua_State *l) |
| 574 |
{ |
| 575 |
return CclFilteredListDirectory(l, 0, 0); |
| 576 |
} |
| 577 |
|
| 578 |
/** |
| 579 |
** Return a table with the files found in the subdirectory. |
| 580 |
*/ |
| 581 |
static int CclListFilesInDirectory(lua_State *l) |
| 582 |
{ |
| 583 |
return CclFilteredListDirectory(l, 0x1, 0x1); |
| 584 |
} |
| 585 |
|
| 586 |
/** |
| 587 |
** Return a table with the files found in the subdirectory. |
| 588 |
*/ |
| 589 |
static int CclListDirsInDirectory(lua_State *l) |
| 590 |
{ |
| 591 |
return CclFilteredListDirectory(l, 0x0, 0x1); |
| 592 |
} |
| 593 |
|
| 594 |
/** |
| 595 |
** Set the local player name |
| 596 |
** |
| 597 |
** @param l Lua state. |
| 598 |
*/ |
| 599 |
static int CclSetLocalPlayerName(lua_State *l) |
| 600 |
{ |
| 601 |
LuaCheckArgs(l, 1); |
| 602 |
LocalPlayerName = LuaToString(l, 1); |
| 603 |
return 0; |
| 604 |
} |
| 605 |
|
| 606 |
/** |
| 607 |
** Get the local player name |
| 608 |
** |
| 609 |
** @param l Lua state. |
| 610 |
*/ |
| 611 |
static int CclGetLocalPlayerName(lua_State *l) |
| 612 |
{ |
| 613 |
LuaCheckArgs(l, 0); |
| 614 |
lua_pushstring(l, LocalPlayerName.c_str()); |
| 615 |
return 1; |
| 616 |
} |
| 617 |
|
| 618 |
/** |
| 619 |
** Removes Randomization of Player position in Multiplayer mode |
| 620 |
** |
| 621 |
** @param l Lua state. |
| 622 |
*/ |
| 623 |
static int CclNoRandomPlacementMultiplayer(lua_State *l) |
| 624 |
{ |
| 625 |
LuaCheckArgs(l, 0); |
| 626 |
NoRandomPlacementMultiplayer = 1; |
| 627 |
return 0; |
| 628 |
} |
| 629 |
|
| 630 |
/** |
| 631 |
** Set God mode. |
| 632 |
** |
| 633 |
** @param l Lua state. |
| 634 |
*/ |
| 635 |
static int CclSetGodMode(lua_State *l) |
| 636 |
{ |
| 637 |
LuaCheckArgs(l, 1); |
| 638 |
GodMode = LuaToBoolean(l, 1); |
| 639 |
return 0; |
| 640 |
} |
| 641 |
|
| 642 |
/** |
| 643 |
** Get God mode. |
| 644 |
** |
| 645 |
** @param l Lua state. |
| 646 |
** |
| 647 |
** @return God mode. |
| 648 |
*/ |
| 649 |
static int CclGetGodMode(lua_State *l) |
| 650 |
{ |
| 651 |
LuaCheckArgs(l, 0); |
| 652 |
lua_pushboolean(l, GodMode); |
| 653 |
return 1; |
| 654 |
} |
| 655 |
|
| 656 |
/** |
| 657 |
** Set building speed. |
| 658 |
** |
| 659 |
** @param l Lua state. |
| 660 |
*/ |
| 661 |
static int CclSetSpeedBuild(lua_State *l) |
| 662 |
{ |
| 663 |
LuaCheckArgs(l, 1); |
| 664 |
SpeedBuild = LuaToNumber(l, 1); |
| 665 |
return 0; |
| 666 |
} |
| 667 |
|
| 668 |
/** |
| 669 |
** Get building speed. |
| 670 |
** |
| 671 |
** @param l Lua state. |
| 672 |
** |
| 673 |
** @return Building speed. |
| 674 |
*/ |
| 675 |
static int CclGetSpeedBuild(lua_State *l) |
| 676 |
{ |
| 677 |
LuaCheckArgs(l, 0); |
| 678 |
lua_pushnumber(l, SpeedBuild); |
| 679 |
return 1; |
| 680 |
} |
| 681 |
|
| 682 |
/** |
| 683 |
** Set training speed. |
| 684 |
** |
| 685 |
** @param l Lua state. |
| 686 |
*/ |
| 687 |
static int CclSetSpeedTrain(lua_State *l) |
| 688 |
{ |
| 689 |
LuaCheckArgs(l, 1); |
| 690 |
SpeedTrain = LuaToNumber(l, 1); |
| 691 |
return 0; |
| 692 |
} |
| 693 |
|
| 694 |
/** |
| 695 |
** Get training speed. |
| 696 |
** |
| 697 |
** @param l Lua state. |
| 698 |
** |
| 699 |
** @return Training speed. |
| 700 |
*/ |
| 701 |
static int CclGetSpeedTrain(lua_State *l) |
| 702 |
{ |
| 703 |
LuaCheckArgs(l, 0); |
| 704 |
lua_pushnumber(l, SpeedTrain); |
| 705 |
return 1; |
| 706 |
} |
| 707 |
|
| 708 |
/** |
| 709 |
** For debug increase all speeds. |
| 710 |
** |
| 711 |
** @param l Lua state. |
| 712 |
*/ |
| 713 |
static int CclSetSpeeds(lua_State *l) |
| 714 |
{ |
| 715 |
LuaCheckArgs(l, 1); |
| 716 |
SpeedBuild = SpeedTrain = LuaToNumber(l, 1); |
| 717 |
return 0; |
| 718 |
} |
| 719 |
|
| 720 |
/** |
| 721 |
** Define default names for the resources. |
| 722 |
** |
| 723 |
** @param l Lua state. |
| 724 |
*/ |
| 725 |
static int CclDefineDefaultResourceNames(lua_State *l) |
| 726 |
{ |
| 727 |
int i; |
| 728 |
int args; |
| 729 |
|
| 730 |
for (i = 0; i < MaxCosts; ++i) { |
| 731 |
DefaultResourceNames[i].clear(); |
| 732 |
} |
| 733 |
args = lua_gettop(l); |
| 734 |
for (i = 0; i < MaxCosts && i < args; ++i) { |
| 735 |
DefaultResourceNames[i] = LuaToString(l, i + 1); |
| 736 |
} |
| 737 |
return 0; |
| 738 |
} |
| 739 |
|
| 740 |
/** |
| 741 |
** Define default names for the resources. |
| 742 |
** |
| 743 |
** @param l Lua state. |
| 744 |
*/ |
| 745 |
static int CclDefineDefaultDisplayResourceNames(lua_State *l) |
| 746 |
{ |
| 747 |
int i; |
| 748 |
int args; |
| 749 |
|
| 750 |
for (i = 0; i < MaxCosts; ++i) { |
| 751 |
DefaultDisplayResourceNames[i].clear(); |
| 752 |
} |
| 753 |
args = lua_gettop(l); |
| 754 |
for (i = 0; i < MaxCosts && i < args; ++i) { |
| 755 |
DefaultDisplayResourceNames[i] = LuaToString(l, i + 1); |
| 756 |
} |
| 757 |
return 0; |
| 758 |
} |
| 759 |
|
| 760 |
/** |
| 761 |
** Compiled with sound. |
| 762 |
** |
| 763 |
** @param l Lua state. |
| 764 |
*/ |
| 765 |
static int CclGetCompileFeature(lua_State *l) |
| 766 |
{ |
| 767 |
const char *str; |
| 768 |
|
| 769 |
LuaCheckArgs(l, 1); |
| 770 |
|
| 771 |
str = LuaToString(l, 1); |
| 772 |
if (CompileOptions.find(str) != std::string::npos) { |
| 773 |
lua_pushboolean(l, 1); |
| 774 |
} else { |
| 775 |
lua_pushboolean(l, 0); |
| 776 |
} |
| 777 |
|
| 778 |
return 1; |
| 779 |
} |
| 780 |
|
| 781 |
/*............................................................................ |
| 782 |
.. Commands |
| 783 |
............................................................................*/ |
| 784 |
|
| 785 |
/** |
| 786 |
** Send command to ccl. |
| 787 |
** |
| 788 |
** @param command Zero terminated command string. |
| 789 |
** @param exitOnError Exit if an error occurs. |
| 790 |
*/ |
| 791 |
int CclCommand(const std::string &command, bool exitOnError) |
| 792 |
{ |
| 793 |
int status; |
| 794 |
|
| 795 |
if (!(status = luaL_loadbuffer(Lua, command.c_str(), command.size(), command.c_str()))) { |
| 796 |
LuaCall(0, 1, exitOnError); |
| 797 |
} else { |
| 798 |
report(status, exitOnError); |
| 799 |
} |
| 800 |
return status; |
| 801 |
} |
| 802 |
|
| 803 |
/*............................................................................ |
| 804 |
.. Setup |
| 805 |
............................................................................*/ |
| 806 |
|
| 807 |
extern int tolua_stratagus_open(lua_State *tolua_S); |
| 808 |
|
| 809 |
/** |
| 810 |
** Initialize Lua |
| 811 |
*/ |
| 812 |
static void InitLua() |
| 813 |
{ |
| 814 |
// For security we don't load all libs |
| 815 |
static const luaL_Reg lualibs[] = { |
| 816 |
{"", luaopen_base}, |
| 817 |
// {LUA_LOADLIBNAME, luaopen_package}, |
| 818 |
{LUA_TABLIBNAME, luaopen_table}, |
| 819 |
// {LUA_IOLIBNAME, luaopen_io}, |
| 820 |
// {LUA_OSLIBNAME, luaopen_os}, |
| 821 |
{LUA_STRLIBNAME, luaopen_string}, |
| 822 |
{LUA_MATHLIBNAME, luaopen_math}, |
| 823 |
{LUA_DBLIBNAME, luaopen_debug}, |
| 824 |
{NULL, NULL} |
| 825 |
}; |
| 826 |
|
| 827 |
Lua = luaL_newstate(); |
| 828 |
|
| 829 |
for (const luaL_Reg *lib = lualibs; lib->func; ++lib) { |
| 830 |
lua_pushcfunction(Lua, lib->func); |
| 831 |
lua_pushstring(Lua, lib->name); |
| 832 |
lua_call(Lua, 1, 0); |
| 833 |
} |
| 834 |
|
| 835 |
tolua_stratagus_open(Lua); |
| 836 |
lua_settop(Lua, 0); // discard any results |
| 837 |
} |
| 838 |
|
| 839 |
/** |
| 840 |
** Initialize ccl and load the config file(s). |
| 841 |
*/ |
| 842 |
void InitCcl(void) |
| 843 |
{ |
| 844 |
InitLua(); |
| 845 |
|
| 846 |
lua_register(Lua, "CompileFeature", CclGetCompileFeature); |
| 847 |
lua_register(Lua, "LibraryPath", CclStratagusLibraryPath); |
| 848 |
lua_register(Lua, "ListDirectory", CclListDirectory); |
| 849 |
lua_register(Lua, "ListFilesInDirectory", CclListFilesInDirectory); |
| 850 |
lua_register(Lua, "ListDirsInDirectory", CclListDirsInDirectory); |
| 851 |
lua_register(Lua, "SetLocalPlayerName", CclSetLocalPlayerName); |
| 852 |
lua_register(Lua, "GetLocalPlayerName", CclGetLocalPlayerName); |
| 853 |
lua_register(Lua, "SetGodMode", CclSetGodMode); |
| 854 |
lua_register(Lua, "GetGodMode", CclGetGodMode); |
| 855 |
|
| 856 |
lua_register(Lua, "SetSpeedBuild", CclSetSpeedBuild); |
| 857 |
lua_register(Lua, "GetSpeedBuild", CclGetSpeedBuild); |
| 858 |
lua_register(Lua, "SetSpeedTrain", CclSetSpeedTrain); |
| 859 |
lua_register(Lua, "GetSpeedTrain", CclGetSpeedTrain); |
| 860 |
lua_register(Lua, "SetSpeeds", CclSetSpeeds); |
| 861 |
|
| 862 |
lua_register(Lua, "DefineDefaultResourceNames", CclDefineDefaultResourceNames); |
| 863 |
lua_register(Lua, "DefineDefaultDisplayResourceNames", CclDefineDefaultDisplayResourceNames); |
| 864 |
lua_register(Lua, "NoRandomPlacementMultiplayer", CclNoRandomPlacementMultiplayer); |
| 865 |
|
| 866 |
lua_register(Lua, "SavePreferences", CclSavePreferences); |
| 867 |
lua_register(Lua, "Load", CclLoad); |
| 868 |
lua_register(Lua, "LoadBuffer", CclLoadBuffer); |
| 869 |
lua_register(Lua, "GetCurrentLuaPath", CclGetCurrentLuaPath); |
| 870 |
lua_register(Lua, "SavedGameInfo", CclSavedGameInfo); |
| 871 |
|
| 872 |
ReplayCclRegister(); |
| 873 |
IconCclRegister(); |
| 874 |
MissileCclRegister(); |
| 875 |
PlayerCclRegister(); |
| 876 |
MapCclRegister(); |
| 877 |
ConstructionCclRegister(); |
| 878 |
DecorationCclRegister(); |
| 879 |
UnitTypeCclRegister(); |
| 880 |
UpgradesCclRegister(); |
| 881 |
SelectionCclRegister(); |
| 882 |
GroupCclRegister(); |
| 883 |
UnitCclRegister(); |
| 884 |
SoundCclRegister(); |
| 885 |
UserInterfaceCclRegister(); |
| 886 |
AiCclRegister(); |
| 887 |
TriggerCclRegister(); |
| 888 |
SpellCclRegister(); |
| 889 |
} |
| 890 |
|
| 891 |
/** |
| 892 |
** Serializes Lua objects to Lua source code. |
| 893 |
*/ |
| 894 |
class CSerializeLua |
| 895 |
{ |
| 896 |
public: |
| 897 |
CSerializeLua(lua_State *l) : lstate(l), indent(0) {} |
| 898 |
|
| 899 |
/** Whether a value was successfully serialized. */ |
| 900 |
enum Result { |
| 901 |
/** The value was serialized as requested. */ |
| 902 |
OK, |
| 903 |
|
| 904 |
/** The value contains something that cannot be |
| 905 |
** serialized. The caller should skip the global |
| 906 |
** variable it is currently trying to serialize. |
| 907 |
*/ |
| 908 |
SKIP, |
| 909 |
|
| 910 |
/** Something went seriously wrong and the whole |
| 911 |
** serialization should be aborted. |
| 912 |
*/ |
| 913 |
FAIL |
| 914 |
}; |
| 915 |
|
| 916 |
Result AppendLuaFields(int table_index, bool is_root); |
| 917 |
char *StrDupOutput() const; |
| 918 |
|
| 919 |
private: |
| 920 |
/** The Lua state from which we're serializing the values. */ |
| 921 |
lua_State *const lstate; |
| 922 |
|
| 923 |
/** The Lua source code being generated. |
| 924 |
** |
| 925 |
** This is an std::vector rather than an std::string |
| 926 |
** because appending characters to std::string gets slow |
| 927 |
** in some implementations when the string is long. |
| 928 |
*/ |
| 929 |
std::vector<char> output; |
| 930 |
|
| 931 |
/** Current indentation level. */ |
| 932 |
int indent; |
| 933 |
|
| 934 |
inline void AppendChar(char c); |
| 935 |
inline void AppendChars(const char *str, size_t len); |
| 936 |
inline void AppendCString(const char *str); |
| 937 |
inline void AppendIndent(); |
| 938 |
void AppendQuotedString(const char *str, size_t len); |
| 939 |
|
| 940 |
static bool IsBlacklistedGlobal(const char *key); |
| 941 |
static bool IsIdentifier(const char *key, size_t len); |
| 942 |
|
| 943 |
Result AppendLuaTable(int table_index); |
| 944 |
Result AppendLuaAsString(int index); |
| 945 |
Result AppendLuaValue(int index); |
| 946 |
}; |
| 947 |
|
| 948 |
/** |
| 949 |
** Append a character to the Lua source code, |
| 950 |
** without any kind of quoting or escaping. |
| 951 |
** |
| 952 |
** @param c |
| 953 |
** The character. |
| 954 |
*/ |
| 955 |
inline void CSerializeLua::AppendChar(char c) |
| 956 |
{ |
| 957 |
this->output.push_back(c); |
| 958 |
} |
| 959 |
|
| 960 |
/** |
| 961 |
** Append an array of characters to the Lua source code, |
| 962 |
** without any kind of quoting or escaping. |
| 963 |
** |
| 964 |
** @param str |
| 965 |
** The address of the first character. |
| 966 |
** @param str |
| 967 |
** The length of the array, in bytes. |
| 968 |
*/ |
| 969 |
inline void CSerializeLua::AppendChars(const char *str, size_t len) |
| 970 |
{ |
| 971 |
this->output.insert(this->output.end(), str, str + len); |
| 972 |
} |
| 973 |
|
| 974 |
/** |
| 975 |
** Append a null-terminated string to the Lua source code, |
| 976 |
** without any kind of quoting or escaping. |
| 977 |
** |
| 978 |
** @param str |
| 979 |
** The address of the first character of the string. |
| 980 |
*/ |
| 981 |
inline void CSerializeLua::AppendCString(const char *str) |
| 982 |
{ |
| 983 |
this->AppendChars(str, strlen(str)); |
| 984 |
} |
| 985 |
|
| 986 |
/** |
| 987 |
** Append the current indentation to the Lua source code. |
| 988 |
*/ |
| 989 |
inline void CSerializeLua::AppendIndent() |
| 990 |
{ |
| 991 |
this->output.insert(this->output.end(), this->indent, '\t'); |
| 992 |
} |
| 993 |
|
| 994 |
/** |
| 995 |
** Append a string literal to the Lua source code, with quotes |
| 996 |
** around it, and any unusual bytes encoded as backslash escapes. |
| 997 |
** |
| 998 |
** @param str |
| 999 |
** The address of the first character of the string. |
| 1000 |
** @param len |
| 1001 |
** The length of the string, in bytes. |
| 1002 |
*/ |
| 1003 |
void CSerializeLua::AppendQuotedString(const char *str, size_t len) |
| 1004 |
{ |
| 1005 |
const unsigned char *src; |
| 1006 |
|
| 1007 |
this->AppendChar('"'); |
| 1008 |
|
| 1009 |
for (src = (const unsigned char *)str; len > 0; ++src, --len) { |
| 1010 |
if (*src == '"' || *src == '\\') { // " -> \" |
| 1011 |
this->AppendChar('\\'); |
| 1012 |
this->AppendChar(*src); |
| 1013 |
} else if (*src < 32 || *src > 127) { // 0xA -> \010 |
| 1014 |
char esc[5]; |
| 1015 |
sprintf_s(esc, 5, "\\%03d", *src); |
| 1016 |
this->AppendChars(esc, 4); |
| 1017 |
} else { |
| 1018 |
this->AppendChar(*src); |
| 1019 |
} |
| 1020 |
} |
| 1021 |
|
| 1022 |
this->AppendChar('"'); |
| 1023 |
} |
| 1024 |
|
| 1025 |
/** |
| 1026 |
** Check whether a global Lua variable is blacklisted from being saved. |
| 1027 |
** |
| 1028 |
** @param key name of the variable in the global environment. |
| 1029 |
** |
| 1030 |
** @return true if the variable is blacklisted and must not be saved. |
| 1031 |
** false if the variable may be saved. |
| 1032 |
*/ |
| 1033 |
bool CSerializeLua::IsBlacklistedGlobal(const char *key) |
| 1034 |
{ |
| 1035 |
return (!strcmp(key, "assert") || |
| 1036 |
!strcmp(key, "gcinfo") || |
| 1037 |
!strcmp(key, "getfenv") || |
| 1038 |
!strcmp(key, "unpack") || |
| 1039 |
!strcmp(key, "tostring") || |
| 1040 |
!strcmp(key, "tonumber") || |
| 1041 |
!strcmp(key, "setmetatable") || |
| 1042 |
!strcmp(key, "require") || |
| 1043 |
!strcmp(key, "pcall") || |
| 1044 |
!strcmp(key, "rawequal") || |
| 1045 |
!strcmp(key, "collectgarbage") || |
| 1046 |
!strcmp(key, "type") || |
| 1047 |
!strcmp(key, "getmetatable") || |
| 1048 |
!strcmp(key, "next") || |
| 1049 |
!strcmp(key, "print") || |
| 1050 |
!strcmp(key, "xpcall") || |
| 1051 |
!strcmp(key, "rawset") || |
| 1052 |
!strcmp(key, "setfenv") || |
| 1053 |
!strcmp(key, "rawget") || |
| 1054 |
!strcmp(key, "newproxy") || |
| 1055 |
!strcmp(key, "ipairs") || |
| 1056 |
!strcmp(key, "loadstring") || |
| 1057 |
!strcmp(key, "dofile") || |
| 1058 |
!strcmp(key, "_VERSION") || |
| 1059 |
!strcmp(key, "pairs") || |
| 1060 |
!strcmp(key, "__pow") || |
| 1061 |
!strcmp(key, "error") || |
| 1062 |
!strcmp(key, "loadfile") || |
| 1063 |
!strcmp(key, "arg") || |
| 1064 |
!strcmp(key, "_LOADED") || |
| 1065 |
!strcmp(key, "loadlib") || |
| 1066 |
!strcmp(key, "string") || |
| 1067 |
!strcmp(key, "os") || |
| 1068 |
!strcmp(key, "io") || |
| 1069 |
!strcmp(key, "debug") || |
| 1070 |
!strcmp(key, "coroutine") || |
| 1071 |
!strcmp(key, "_G") || |
| 1072 |
!strcmp(key, "AiTypes") || // data-dep |
| 1073 |
!strcmp(key, "AllowAll") || // Lua const |
| 1074 |
!strcmp(key, "AllowedUnits") || // data-dep |
| 1075 |
!strcmp(key, "DownButton") || // #define |
| 1076 |
!strcmp(key, "EditorCommandLine") || // enum |
| 1077 |
!strcmp(key, "EditorEditing") || // enum |
| 1078 |
!strcmp(key, "EditorNotRunning") || // enum |
| 1079 |
!strcmp(key, "EditorStarted") || // enum |
| 1080 |
!strcmp(key, "EnergyCost") || // #define |
| 1081 |
!strcmp(key, "FontColors") || // has meta |
| 1082 |
!strcmp(key, "Fonts") || // has meta |
| 1083 |
!strcmp(key, "ForbidAll") || // Lua const |
| 1084 |
!strcmp(key, "GameDefeat") || // enum |
| 1085 |
!strcmp(key, "GameDraw") || // enum |
| 1086 |
!strcmp(key, "GameNoResult") || // enum |
| 1087 |
!strcmp(key, "GameQuitToMenu") || // enum |
| 1088 |
!strcmp(key, "GameRestart") || // enum |
| 1089 |
!strcmp(key, "GameVictory") || // enum |
| 1090 |
!strcmp(key, "Icons") || // has meta |
| 1091 |
!strcmp(key, "LeftButton") || // #define |
| 1092 |
!strcmp(key, "MagmaCost") || // #define |
| 1093 |
!strcmp(key, "MaxCosts") || // #define |
| 1094 |
!strcmp(key, "MaxFontColors") || // #define |
| 1095 |
!strcmp(key, "MiddleButton") || // #define |
| 1096 |
!strcmp(key, "NoButton") || // #define |
| 1097 |
!strcmp(key, "PlayerComputer") || // enum |
| 1098 |
!strcmp(key, "PlayerMax") || // #define |
| 1099 |
!strcmp(key, "PlayerNeutral") || // enum |
| 1100 |
!strcmp(key, "PlayerNobody") || // enum |
| 1101 |
!strcmp(key, "PlayerNumNeutral") || // #define |
| 1102 |
!strcmp(key, "PlayerPerson") || // enum |
| 1103 |
!strcmp(key, "PlayerRescueActive") || // enum |
| 1104 |
!strcmp(key, "PlayerRescuePassive") || // enum |
| 1105 |
!strcmp(key, "RightButton") || // #define |
| 1106 |
!strcmp(key, "SettingsGameTypeFreeForAll") || // enum |
| 1107 |
!strcmp(key, "SettingsGameTypeLeftVsRight") || // enum |
| 1108 |
!strcmp(key, "SettingsGameTypeManTeamVsMachine") || // enum |
| 1109 |
!strcmp(key, "SettingsGameTypeManVsMachine") || // enum |
| 1110 |
!strcmp(key, "SettingsGameTypeMapDefault") || // enum |
| 1111 |
!strcmp(key, "SettingsGameTypeMelee") || // enum |
| 1112 |
!strcmp(key, "SettingsGameTypeTopVsBottom") || // enum |
| 1113 |
!strcmp(key, "SettingsPresetMapDefault") || // enum |
| 1114 |
!strcmp(key, "UnitMax") || // #define |
| 1115 |
!strcmp(key, "UpButton") || // #define |
| 1116 |
!strcmp(key, "default_objectives") || // per-lang |
| 1117 |
!strcmp(key, "playlist") || // per-user |
| 1118 |
!strcmp(key, "preferences")); // per-user |
| 1119 |
// other string to protected ? |
| 1120 |
} |
| 1121 |
|
| 1122 |
/** |
| 1123 |
** Check whether the given string is valid for a Lua identifier. |
| 1124 |
** |
| 1125 |
** @param key |
| 1126 |
** The address of the first character of the string. |
| 1127 |
** @param len |
| 1128 |
** The length of the string, in bytes. |
| 1129 |
** |
| 1130 |
** The @a len parameter exists so that strings containing |
| 1131 |
** embedded null characters can be passed to this function |
| 1132 |
** and properly detected as invalid identifiers. |
| 1133 |
** |
| 1134 |
** @return |
| 1135 |
** true if the string is OK as a Lua identifier. |
| 1136 |
** false if the string cannot be used as a Lua identifier. |
| 1137 |
*/ |
| 1138 |
bool CSerializeLua::IsIdentifier(const char *key, size_t len) |
| 1139 |
{ |
| 1140 |
if (len < 1) { |
| 1141 |
return false; |
| 1142 |
} |
| 1143 |
|
| 1144 |
if (!isalpha(static_cast<unsigned char>(key[0])) |
| 1145 |
&& key[0] != '_') { |
| 1146 |
return false; |
| 1147 |
} |
| 1148 |
|
| 1149 |
for (size_t i = 0; i < len; ++i) { |
| 1150 |
if (!isalnum(static_cast<unsigned char>(key[i])) |
| 1151 |
&& key[i] != '_') { |
| 1152 |
return false; |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
return true; |
| 1157 |
} |
| 1158 |
|
| 1159 |
/** |
| 1160 |
** Append a Lua string or number value to the Lua source code. |
| 1161 |
** |
| 1162 |
** @param index |
| 1163 |
** The index of the value in the Lua stack. |
| 1164 |
** This function does not modify the value in the stack. |
| 1165 |
** |
| 1166 |
** @return |
| 1167 |
** CSerializeLua::OK or CSerializeLua::FAIL. |
| 1168 |
** This function never returns CSerializeLua::SKIP. |
| 1169 |
*/ |
| 1170 |
CSerializeLua::Result CSerializeLua::AppendLuaAsString(int index) |
| 1171 |
{ |
| 1172 |
// Make a copy so that lua_tolstring won't replace the |
| 1173 |
// original number with a string. |
| 1174 |
lua_pushvalue(this->lstate, index); |
| 1175 |
size_t len; |
| 1176 |
const char *str = lua_tolstring(this->lstate, -1, &len); |
| 1177 |
if (str == NULL) { |
| 1178 |
lua_pop(this->lstate, 1); |
| 1179 |
return FAIL; |
| 1180 |
} else { |
| 1181 |
this->AppendChars(str, len); |
| 1182 |
lua_pop(this->lstate, 1); |
| 1183 |
return OK; |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
/** |
| 1188 |
** Append a Lua table to the Lua source code, with braces around it. |
| 1189 |
** |
| 1190 |
** @param index |
| 1191 |
** The index of the table in the Lua stack. |
| 1192 |
** This function does not modify the value in the stack. |
| 1193 |
** |
| 1194 |
** @return |
| 1195 |
** CSerializeLua::OK, CSerializeLua::FAIL, or CSerializeLua::SKIP. |
| 1196 |
*/ |
| 1197 |
CSerializeLua::Result CSerializeLua::AppendLuaTable(int index) |
| 1198 |
{ |
| 1199 |
if (lua_getmetatable(this->lstate, index)) { |
| 1200 |
lua_pop(this->lstate, 1); // pop the metatable |
| 1201 |
return SKIP; |
| 1202 |
} |
| 1203 |
|
| 1204 |
this->AppendChar('{'); |
| 1205 |
this->indent++; |
| 1206 |
Result result = this->AppendLuaFields(index, false); |
| 1207 |
this->indent--; |
| 1208 |
this->AppendChar('}'); |
| 1209 |
return result; |
| 1210 |
} |
| 1211 |
|
| 1212 |
/** |
| 1213 |
** Append a Lua value to the Lua source code. |
| 1214 |
** |
| 1215 |
** @param index |
| 1216 |
** The index of the value in the Lua stack. |
| 1217 |
** This function does not modify the value in the stack. |
| 1218 |
** |
| 1219 |
** @return |
| 1220 |
** CSerializeLua::OK, CSerializeLua::FAIL, or CSerializeLua::SKIP. |
| 1221 |
*/ |
| 1222 |
CSerializeLua::Result CSerializeLua::AppendLuaValue(int index) |
| 1223 |
{ |
| 1224 |
int type = lua_type(this->lstate, index); |
| 1225 |
switch (type) { |
| 1226 |
case LUA_TNIL: |
| 1227 |
this->AppendCString("nil"); |
| 1228 |
return OK; |
| 1229 |
case LUA_TNUMBER: |
| 1230 |
// Let Lua do the conversion. |
| 1231 |
return this->AppendLuaAsString(index); |
| 1232 |
case LUA_TBOOLEAN: |
| 1233 |
if (lua_toboolean(this->lstate, index)) { |
| 1234 |
this->AppendCString("true"); |
| 1235 |
} else { |
| 1236 |
this->AppendCString("false"); |
| 1237 |
} |
| 1238 |
return OK; |
| 1239 |
|
| 1240 |
case LUA_TSTRING: |
| 1241 |
{ |
| 1242 |
size_t len; |
| 1243 |
const char *str = lua_tolstring(this->lstate, index, &len); |
| 1244 |
this->AppendQuotedString(str, len); |
| 1245 |
return OK; |
| 1246 |
} |
| 1247 |
case LUA_TTABLE: |
| 1248 |
return this->AppendLuaTable(index); |
| 1249 |
case LUA_TFUNCTION: |
| 1250 |
// Could be done with string.dump(function) |
| 1251 |
// and debug.getinfo(function).name (could be nil for anonymous function) |
| 1252 |
// But not useful yet. |
| 1253 |
return SKIP; |
| 1254 |
case LUA_TUSERDATA: |
| 1255 |
case LUA_TTHREAD: |
| 1256 |
case LUA_TLIGHTUSERDATA: |
| 1257 |
case LUA_TNONE: |
| 1258 |
default : // no other cases |
| 1259 |
return SKIP; |
| 1260 |
} |
| 1261 |
} |
| 1262 |
|
| 1263 |
/** |
| 1264 |
** Append the fields (keys and values) of a Lua table to the Lua |
| 1265 |
** source code. |
| 1266 |
** |
| 1267 |
** @param table_index |
| 1268 |
** The index of the table in the Lua stack. |
| 1269 |
** @param is_root |
| 1270 |
** True if the table is a global environment and this function |
| 1271 |
** should generate a series of assignment statements. |
| 1272 |
** False if the table is something else and this function should |
| 1273 |
** generate a fieldlist for a tableconstructor. |
| 1274 |
** |
| 1275 |
** @return |
| 1276 |
** CSerializeLua::OK if it appended the fields as requested. |
| 1277 |
** CSerializeLua::FAIL if something went seriously wrong and the |
| 1278 |
** whole serialization should be aborted. |
| 1279 |
** CSerializeLua::SKIP if is_root is false and the table contains |
| 1280 |
** something that cannot be serialized. |
| 1281 |
** |
| 1282 |
** If is_root is true and the table contains a field that cannot be |
| 1283 |
** serialized, then this function just skips that field and continues |
| 1284 |
** serializing the rest of the table. This is so that the global |
| 1285 |
** environment can be serialized even though it contains functions. |
| 1286 |
*/ |
| 1287 |
CSerializeLua::Result CSerializeLua::AppendLuaFields(int table_index, |
| 1288 |
bool is_root) |
| 1289 |
{ |
| 1290 |
if (!lua_checkstack(this->lstate, 10)) |
| 1291 |
return FAIL; |
| 1292 |
const int top_on_entry = lua_gettop(this->lstate); |
| 1293 |
Assert(lua_istable(this->lstate, table_index)); |
| 1294 |
|
| 1295 |
lua_Number seq = 1; |
| 1296 |
bool is_first = true; |
| 1297 |
lua_pushnil(this->lstate); |
| 1298 |
while (lua_next(this->lstate, table_index)) { |
| 1299 |
const int key_index = top_on_entry + 1; |
| 1300 |
const int value_index = top_on_entry + 2; |
| 1301 |
Result result; |
| 1302 |
|
| 1303 |
// If there's any problem with the field, then we |
| 1304 |
// remove it from the output vector by truncating |
| 1305 |
// back to this size. |
| 1306 |
const size_t undo = this->output.size(); |
| 1307 |
|
| 1308 |
if (!is_root) { |
| 1309 |
if (is_first) { |
| 1310 |
this->AppendChar('\n'); |
| 1311 |
} else { |
| 1312 |
this->AppendCString(",\n"); |
| 1313 |
} |
| 1314 |
this->AppendIndent(); |
| 1315 |
} |
| 1316 |
|
| 1317 |
// Append the key and and equals sign, if necessary. |
| 1318 |
bool increment_seq = false; |
| 1319 |
const int key_type = lua_type(this->lstate, key_index); |
| 1320 |
if (is_root) { |
| 1321 |
if (key_type != LUA_TSTRING) { |
| 1322 |
goto skip_field; |
| 1323 |
} |
| 1324 |
|
| 1325 |
size_t key_len; |
| 1326 |
const char *key_str = lua_tolstring(this->lstate, |
| 1327 |
key_index, |
| 1328 |
&key_len); |
| 1329 |
if (!IsIdentifier(key_str, key_len) |
| 1330 |
|| IsBlacklistedGlobal(key_str)) { |
| 1331 |
goto skip_field; |
| 1332 |
} |
| 1333 |
|
| 1334 |
this->AppendChars(key_str, key_len); |
| 1335 |
this->AppendCString(" = "); |
| 1336 |
} else if (key_type == LUA_TSTRING) { |
| 1337 |
size_t key_len; |
| 1338 |
const char *key_str = lua_tolstring(this->lstate, |
| 1339 |
key_index, |
| 1340 |
&key_len); |
| 1341 |
if (IsIdentifier(key_str, key_len)) { |
| 1342 |
this->AppendChars(key_str, key_len); |
| 1343 |
} else { |
| 1344 |
this->AppendChar('['); |
| 1345 |
this->AppendQuotedString(key_str, key_len); |
| 1346 |
this->AppendChar(']'); |
| 1347 |
} |
| 1348 |
this->AppendCString(" = "); |
| 1349 |
} else if (key_type == LUA_TNUMBER) { |
| 1350 |
if (lua_tonumber(this->lstate, key_index) == seq) { |
| 1351 |
// Increment seq only after the value |
| 1352 |
// has been successfully serialized. |
| 1353 |
increment_seq = true; |
| 1354 |
} else { |
| 1355 |
this->AppendChar('['); |
| 1356 |
result = this->AppendLuaAsString(key_index); |
| 1357 |
if (result != OK) { |
| 1358 |
return result; |
| 1359 |
} |
| 1360 |
this->AppendCString("] = "); |
| 1361 |
} |
| 1362 |
} else { |
| 1363 |
goto skip_field; |
| 1364 |
} |
| 1365 |
|
| 1366 |
result = this->AppendLuaValue(value_index); |
| 1367 |
switch (result) { |
| 1368 |
case OK: |
| 1369 |
if (is_root) { |
| 1370 |
this->AppendCString(";\n"); |
| 1371 |
} |
| 1372 |
is_first = false; |
| 1373 |
if (increment_seq) |
| 1374 |
++seq; |
| 1375 |
break; |
| 1376 |
case SKIP: |
| 1377 |
skip_field: |
| 1378 |
this->output.resize(undo); |
| 1379 |
if (!is_root) { |
| 1380 |
lua_settop(this->lstate, top_on_entry); |
| 1381 |
return SKIP; |
| 1382 |
} |
| 1383 |
break; |
| 1384 |
case FAIL: |
| 1385 |
default: |
| 1386 |
lua_settop(this->lstate, top_on_entry); |
| 1387 |
return result; |
| 1388 |
} |
| 1389 |
|
| 1390 |
lua_pop(this->lstate, 1); // pop the value |
| 1391 |
} |
| 1392 |
|
| 1393 |
if (!is_first && !is_root) { |
| 1394 |
this->AppendChar(' '); |
| 1395 |
} |
| 1396 |
|
| 1397 |
return OK; |
| 1398 |
} |
| 1399 |
|
| 1400 |
/** |
| 1401 |
** Copy the Lua source code to a new string. |
| 1402 |
** |
| 1403 |
** @return The Lua source code as a string. |
| 1404 |
** The caller must eventually free it with delete[]. |
| 1405 |
*/ |
| 1406 |
char *CSerializeLua::StrDupOutput() const |
| 1407 |
{ |
| 1408 |
const size_t size = this->output.size(); |
| 1409 |
char *const str = new char[size + 1]; |
| 1410 |
if (size > 0) { |
| 1411 |
memcpy(str, &this->output[0], size); |
| 1412 |
} |
| 1413 |
str[size] = '\0'; |
| 1414 |
return str; |
| 1415 |
} |
| 1416 |
|
| 1417 |
/** |
| 1418 |
** For saving lua state (table, number, string, bool, not function). |
| 1419 |
** |
| 1420 |
** @param l lua_State to save. |
| 1421 |
** @param is_root true to save global variables, |
| 1422 |
** false to save and pop the table at top of stack. |
| 1423 |
** |
| 1424 |
** @return NULL if nothing could be saved. |
| 1425 |
** else a string that could be executed in lua to restore lua state |
| 1426 |
*/ |
| 1427 |
char *SaveGlobal(lua_State *l, bool is_root) |
| 1428 |
{ |
| 1429 |
CSerializeLua serialize(l); |
| 1430 |
if (is_root) { |
| 1431 |
lua_pushvalue(l, LUA_GLOBALSINDEX); |
| 1432 |
} |
| 1433 |
CSerializeLua::Result result |
| 1434 |
= serialize.AppendLuaFields(lua_gettop(l), is_root); |
| 1435 |
lua_pop(l, 1); |
| 1436 |
if (result == CSerializeLua::OK) { |
| 1437 |
return serialize.StrDupOutput(); |
| 1438 |
} else { |
| 1439 |
return NULL; |
| 1440 |
} |
| 1441 |
} |
| 1442 |
|
| 1443 |
/** |
| 1444 |
** Create directories containing user settings and data. |
| 1445 |
** |
| 1446 |
** More specifically: logs, saved games, preferences |
| 1447 |
*/ |
| 1448 |
void CreateUserDirectories(void) |
| 1449 |
{ |
| 1450 |
std::string directory; |
| 1451 |
UserDirectory = ""; |
| 1452 |
|
| 1453 |
std::string s; |
| 1454 |
#ifdef USE_WIN32 |
| 1455 |
s = getenv("APPDATA"); |
| 1456 |
#else |
| 1457 |
s = getenv("HOME"); |
| 1458 |
#endif |
| 1459 |
if (!s.empty()) { |
| 1460 |
UserDirectory = s + "/"; |
| 1461 |
} |
| 1462 |
|
| 1463 |
UserDirectory += STRATAGUS_HOME_PATH; |
| 1464 |
makedir(UserDirectory.c_str(), 0777); |
| 1465 |
|
| 1466 |
// Create specific subdirectories |
| 1467 |
directory = UserDirectory + "logs/"; |
| 1468 |
makedir(directory.c_str(), 0777); |
| 1469 |
directory = UserDirectory + "save/"; |
| 1470 |
makedir(directory.c_str(), 0777); |
| 1471 |
directory = UserDirectory + "patches/"; |
| 1472 |
makedir(directory.c_str(), 0777); |
| 1473 |
} |
| 1474 |
|
| 1475 |
/** |
| 1476 |
** Save user preferences |
| 1477 |
*/ |
| 1478 |
void SavePreferences(void) |
| 1479 |
{ |
| 1480 |
FILE *fd; |
| 1481 |
std::string path; |
| 1482 |
|
| 1483 |
lua_pushstring(Lua, "preferences"); |
| 1484 |
lua_gettable(Lua, LUA_GLOBALSINDEX); |
| 1485 |
if (lua_type(Lua, -1) == LUA_TTABLE) { |
| 1486 |
path = UserDirectory + "preferences.lua"; |
| 1487 |
|
| 1488 |
fd = fopen(path.c_str(), "w"); |
| 1489 |
if (!fd) { |
| 1490 |
return; |
| 1491 |
} |
| 1492 |
|
| 1493 |
char *s = SaveGlobal(Lua, false); |
| 1494 |
fprintf(fd, "preferences = {%s}\n", s); |
| 1495 |
delete[] s; |
| 1496 |
|
| 1497 |
fclose(fd); |
| 1498 |
} |
| 1499 |
} |
| 1500 |
|
| 1501 |
/** |
| 1502 |
** Load stratagus config file. |
| 1503 |
*/ |
| 1504 |
void LoadCcl(void) |
| 1505 |
{ |
| 1506 |
char buf[PATH_MAX]; |
| 1507 |
|
| 1508 |
// |
| 1509 |
// Load and evaluate configuration file |
| 1510 |
// |
| 1511 |
CclInConfigFile = 1; |
| 1512 |
LibraryFileName(CclStartFile.c_str(), buf, sizeof(buf)); |
| 1513 |
if (access(buf, R_OK)) { |
| 1514 |
fprintf(stderr, "Maybe you need to specify another gamepath with '-d /path/to/datadir'?\n"); |
| 1515 |
ExitFatal(-1); |
| 1516 |
} |
| 1517 |
|
| 1518 |
ShowLoadProgress("Script %s\n", buf); |
| 1519 |
LuaLoadFile(buf); |
| 1520 |
CclInConfigFile = 0; |
| 1521 |
CclGarbageCollect(0); // Cleanup memory after load |
| 1522 |
} |
| 1523 |
|
| 1524 |
/** |
| 1525 |
** Save CCL Module. |
| 1526 |
** |
| 1527 |
** @param file Save file. |
| 1528 |
*/ |
| 1529 |
void SaveCcl(CFile *file) |
| 1530 |
{ |
| 1531 |
file->printf("SetGodMode(%s)\n", GodMode ? "true" : "false"); |
| 1532 |
|
| 1533 |
file->printf("SetSpeedBuild(%d)\n", SpeedBuild); |
| 1534 |
file->printf("SetSpeedTrain(%d)\n", SpeedTrain); |
| 1535 |
} |
| 1536 |
|
| 1537 |
//@} |