diff options
author | dshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573> | 2007-06-27 07:49:52 +0000 |
---|---|---|
committer | dshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573> | 2007-06-27 07:49:52 +0000 |
commit | 9ba7349f93aeca1bb45a083235f3576f5096a995 (patch) | |
tree | 278d98ebd1d6811ef8d2e5496e3630cb87054c2c /crawl-ref/source | |
parent | ea6a050bf6b67faf4f9cce0cd8ee802f75ba21e3 (diff) | |
download | crawl-ref-9ba7349f93aeca1bb45a083235f3576f5096a995.tar.gz crawl-ref-9ba7349f93aeca1bb45a083235f3576f5096a995.zip |
The dungeon builder now caches compiled Lua chunks instead of Lua source in the
.dsc to save on compile time (at the expense of larger .dsc files).
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@1663 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'crawl-ref/source')
-rw-r--r-- | crawl-ref/source/clua.cc | 13 | ||||
-rw-r--r-- | crawl-ref/source/clua.h | 1 | ||||
-rw-r--r-- | crawl-ref/source/files.cc | 5 | ||||
-rw-r--r-- | crawl-ref/source/luadgn.cc | 101 | ||||
-rw-r--r-- | crawl-ref/source/luadgn.h | 13 | ||||
-rw-r--r-- | crawl-ref/source/mapdef.cc | 4 | ||||
-rw-r--r-- | crawl-ref/source/maps.cc | 8 | ||||
-rw-r--r-- | crawl-ref/source/maps.h | 2 | ||||
-rw-r--r-- | crawl-ref/source/sqldbm.cc | 6 |
9 files changed, 122 insertions, 31 deletions
diff --git a/crawl-ref/source/clua.cc b/crawl-ref/source/clua.cc index 8ea71c26e4..6a4e54074e 100644 --- a/crawl-ref/source/clua.cc +++ b/crawl-ref/source/clua.cc @@ -31,6 +31,7 @@ #include "skills2.h" #include "spl-util.h" #include "stuff.h" +#include "travel.h" #include <cstring> #include <map> @@ -202,13 +203,18 @@ void CLua::init_throttle() } } -int CLua::loadstring(const char *s, const char *context) +int CLua::loadbuffer(const char *buf, size_t size, const char *context) { - const int err = luaL_loadbuffer(state(), s, strlen(s), context); + const int err = luaL_loadbuffer(state(), buf, size, context); set_error(err, state()); return err; } +int CLua::loadstring(const char *s, const char *context) +{ + return loadbuffer(s, strlen(s), context); +} + int CLua::execstring(const char *s, const char *context) { int err = 0; @@ -711,6 +717,7 @@ LUARET1(you_levitating, boolean, LUARET1(you_flying, boolean, player_is_levitating() && wearing_amulet(AMU_CONTROLLED_FLIGHT)) LUARET1(you_transform, string, transform_name()) +LUARET1(you_where, string, level_id::current().describe().c_str()) LUAWRAP(you_stop_activity, interrupt_activity(AI_FORCE_INTERRUPT)) void lua_push_floor_items(lua_State *ls); @@ -783,6 +790,8 @@ static const struct luaL_reg you_lib[] = { "stop_activity", you_stop_activity }, { "floor_items", you_floor_items }, + + { "where", you_where }, { NULL, NULL }, }; diff --git a/crawl-ref/source/clua.h b/crawl-ref/source/clua.h index 2fd6bf835e..b9190021b6 100644 --- a/crawl-ref/source/clua.h +++ b/crawl-ref/source/clua.h @@ -63,6 +63,7 @@ public: void setregistry(const char *name); void getregistry(const char *name); + int loadbuffer(const char *buf, size_t size, const char *context); int loadstring(const char *str, const char *context); int execstring(const char *str, const char *context = "init.txt"); int execfile(const char *filename, bool trusted = false, diff --git a/crawl-ref/source/files.cc b/crawl-ref/source/files.cc index cdfd51895a..ad55283bfc 100644 --- a/crawl-ref/source/files.cc +++ b/crawl-ref/source/files.cc @@ -1648,10 +1648,9 @@ std::string readString(FILE *file, int cap) { if (length <= cap) { - char *buf = new char[length + 1]; + char *buf = new char[length]; read2(file, buf, length); - buf[length] = 0; - const std::string s = buf; + const std::string s(buf, length); delete [] buf; return (s); } diff --git a/crawl-ref/source/luadgn.cc b/crawl-ref/source/luadgn.cc index bce024b312..3c73a194c7 100644 --- a/crawl-ref/source/luadgn.cc +++ b/crawl-ref/source/luadgn.cc @@ -10,6 +10,8 @@ #include "files.h" #include "luadgn.h" #include "mapdef.h" +#include "stuff.h" +#include <sstream> // Lua interpreter for the dungeon builder. CLua dlua(false); @@ -40,29 +42,52 @@ static int dlua_stringtable(lua_State *ls, const std::vector<std::string> &s) // dlua_chunk dlua_chunk::dlua_chunk(const std::string &_context) - : file(), chunk(), context(_context), first(-1), last(-1), error() + : file(), chunk(), compiled(), context(_context), first(-1), + last(-1), error() { clear(); } void dlua_chunk::write(FILE *outf) const { - writeString(outf, chunk, LUA_CHUNK_MAX_SIZE); - if (!chunk.empty()) + if (compiled.empty() && chunk.empty()) { - writeString(outf, file); - writeLong(outf, first); + writeByte(outf, CT_EMPTY); + return; } + + if (!compiled.empty()) + { + writeByte(outf, CT_COMPILED); + writeString(outf, compiled, LUA_CHUNK_MAX_SIZE); + } + else + { + writeByte(outf, CT_SOURCE); + writeString(outf, chunk, LUA_CHUNK_MAX_SIZE); + } + + writeString(outf, file); + writeLong(outf, first); } void dlua_chunk::read(FILE *inf) { - chunk = readString(inf, LUA_CHUNK_MAX_SIZE); - if (!chunk.empty()) + clear(); + chunk_t type = static_cast<chunk_t>(readByte(inf)); + switch (type) { - file = readString(inf); - first = readLong(inf); + case CT_EMPTY: + return; + case CT_SOURCE: + chunk = readString(inf, LUA_CHUNK_MAX_SIZE); + break; + case CT_COMPILED: + compiled = readString(inf, LUA_CHUNK_MAX_SIZE); + break; } + file = readString(inf); + first = readLong(inf); } void dlua_chunk::clear() @@ -71,6 +96,7 @@ void dlua_chunk::clear() chunk.clear(); first = last = -1; error.clear(); + compiled.clear(); } void dlua_chunk::set_file(const std::string &s) @@ -97,18 +123,48 @@ void dlua_chunk::set_chunk(const std::string &s) chunk = s; } -int dlua_chunk::check_op(CLua *interp, int err) +int dlua_chunk::check_op(CLua &interp, int err) { - error = interp->error; + error = interp.error; return (err); } +static int dlua_compiled_chunk_writer(lua_State *ls, const void *p, + size_t sz, void *ud) +{ + std::ostringstream &out = *static_cast<std::ostringstream*>(ud); + out.write((const char *) p, sz); + return (0); +} + int dlua_chunk::load(CLua &interp) { - if (trimmed_string(chunk).empty()) + if (!compiled.empty()) + return check_op( interp, + interp.loadbuffer(compiled.c_str(), compiled.length(), + context.c_str()) ); + + if (empty()) + { + chunk.clear(); return (-1000); - return check_op(&interp, - interp.loadstring(chunk.c_str(), context.c_str())); + } + + int err = check_op( interp, + interp.loadstring(chunk.c_str(), context.c_str()) ); + if (err) + return (err); + std::ostringstream out; + err = lua_dump(interp, dlua_compiled_chunk_writer, &out); + if (err) + { + const char *e = lua_tostring(interp, -1); + error = e? e : "Unknown error compiling chunk"; + lua_pop(interp, 2); + } + compiled = out.str(); + chunk.clear(); + return (err); } int dlua_chunk::load_call(CLua &interp, const char *fn) @@ -119,7 +175,7 @@ int dlua_chunk::load_call(CLua &interp, const char *fn) if (err) return (err); - return check_op(&interp, !interp.callfn(fn, fn? 1 : 0, 0)); + return check_op(interp, !interp.callfn(fn, fn? 1 : 0, 0)); } std::string dlua_chunk::orig_error() const @@ -575,6 +631,20 @@ static int dgn_name(lua_State *ls) PLUARET(string, map->name.c_str()); } +static int dgn_grid(lua_State *ls) +{ + const int x = luaL_checkint(ls, 1), y = luaL_checkint(ls, 2); + if (!map_bounds(x, y)) + luaL_error(ls, + make_stringf("(%d,%d) is out of bounds (%d-%d,%d-%d)", + x, y, + X_BOUND_1, X_BOUND_2, + Y_BOUND_1, Y_BOUND_2).c_str()); + if (lua_isnumber(ls, 3)) + grd[x][y] = static_cast<dungeon_feature_type>(luaL_checkint(ls, 3)); + PLUARET(number, grd[x][y]); +} + static const struct luaL_reg dgn_lib[] = { { "default_depth", dgn_default_depth }, @@ -596,6 +666,7 @@ static const struct luaL_reg dgn_lib[] = { "kfeat", dgn_kfeat }, { "kitem", dgn_kitem }, { "kmons", dgn_kmons }, + { "grid", dgn_grid }, { NULL, NULL } }; diff --git a/crawl-ref/source/luadgn.h b/crawl-ref/source/luadgn.h index 6c619af05e..241983c0ba 100644 --- a/crawl-ref/source/luadgn.h +++ b/crawl-ref/source/luadgn.h @@ -21,14 +21,23 @@ class dlua_chunk private: std::string file; std::string chunk; + std::string compiled; std::string context; int first, last; // First and last lines of the original source. + enum chunk_t + { + CT_EMPTY, + CT_SOURCE, + CT_COMPILED + }; + private: - int check_op(CLua *, int); + int check_op(CLua &, int); std::string rewrite_chunk_prefix(const std::string &line) const; std::string get_chunk_prefix(const std::string &s) const; - + void release_compiled_chunk(); + public: mutable std::string error; diff --git a/crawl-ref/source/mapdef.cc b/crawl-ref/source/mapdef.cc index a2615f9b7b..9c3a7953b7 100644 --- a/crawl-ref/source/mapdef.cc +++ b/crawl-ref/source/mapdef.cc @@ -862,7 +862,7 @@ void map_def::reinit() void map_def::write_full(FILE *outf) { cache_offset = ftell(outf); - writeShort(outf, 0x1eaf); // Level indicator. + writeShort(outf, MAP_CACHE_VERSION); // Level indicator. writeString(outf, name); prelude.write(outf); main.write(outf); @@ -880,7 +880,7 @@ void map_def::read_full(FILE *inf) // reloading the index), but it's easier to save the game at this // point and let the player reload. - if (readShort(inf) != 0x1eaf || readString(inf) != name) + if (readShort(inf) != MAP_CACHE_VERSION || readString(inf) != name) save_game(true, make_stringf("Level file cache for %s is out-of-sync! " "Please reload your game.", diff --git a/crawl-ref/source/maps.cc b/crawl-ref/source/maps.cc index 011bbe45df..e60031195d 100644 --- a/crawl-ref/source/maps.cc +++ b/crawl-ref/source/maps.cc @@ -397,8 +397,6 @@ void reset_map_parser() static bool checked_des_index_dir = false; -#define DESCACHE_VER 1002 - static void check_des_index_dir() { if (checked_des_index_dir) @@ -428,7 +426,7 @@ static bool verify_file_version(const std::string &file) const long ver = readLong(inf); fclose(inf); - return (ver == DESCACHE_VER); + return (ver == MAP_CACHE_VERSION); } static bool verify_map_index(const std::string &base) @@ -509,7 +507,7 @@ static void write_map_full(const std::string &filebase, size_t vs, size_t ve) if (!outf) end(1, true, "Unable to open %s for writing", cfile.c_str()); - writeLong(outf, DESCACHE_VER); + writeLong(outf, MAP_CACHE_VERSION); for (size_t i = vs; i < ve; ++i) vdefs[i].write_full(outf); fclose(outf); @@ -522,7 +520,7 @@ static void write_map_index(const std::string &filebase, size_t vs, size_t ve) if (!outf) end(1, true, "Unable to open %s for writing", cfile.c_str()); - writeLong(outf, DESCACHE_VER); + writeLong(outf, MAP_CACHE_VERSION); writeShort(outf, ve > vs? ve - vs : 0); for (size_t i = vs; i < ve; ++i) vdefs[i].write_index(outf); diff --git a/crawl-ref/source/maps.h b/crawl-ref/source/maps.h index 87b3c933cd..d14119dd7d 100644 --- a/crawl-ref/source/maps.h +++ b/crawl-ref/source/maps.h @@ -59,4 +59,6 @@ extern depth_ranges lc_default_depths; extern dlua_chunk lc_global_prelude; extern bool lc_run_global_prelude; +const int MAP_CACHE_VERSION = 1003; + #endif diff --git a/crawl-ref/source/sqldbm.cc b/crawl-ref/source/sqldbm.cc index 40d5867ca0..fee11a9d46 100644 --- a/crawl-ref/source/sqldbm.cc +++ b/crawl-ref/source/sqldbm.cc @@ -1,7 +1,9 @@ /* - * File: sqldbm.c + * File: sqldbm.cc * Summary: dbm wrapper for SQLite * Written by: Darshan Shaligram + * + * Modified for Crawl Reference by $Author: dshaligram $ on $Date: 2007-06-26T17:43:23.491102Z $ */ #include "AppHdr.h" @@ -276,4 +278,4 @@ int dbm_store(SQL_DBM *db, const sql_datum &key, const sql_datum &value, int) return (err); } -#endif +#endif // USE_SQLITE_DBM |