MCPcopy Create free account
hub / github.com/DFHack/dfhack / RunCoreQueryLoop

Method RunCoreQueryLoop

library/LuaTools.cpp:1196–1282  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1194}
1195
1196bool DFHack::Lua::RunCoreQueryLoop(color_ostream &out, lua_State *state, DFHack::Lua::init_fn init)
1197{
1198 if (!lua_checkstack(state, 20))
1199 return false;
1200
1201 lua_State *thread;
1202 int rv;
1203 std::string prompt;
1204 std::string histfile;
1205
1206 DFHack::CommandHistory hist;
1207 std::string histname;
1208
1209 {
1210 CoreSuspender suspend;
1211
1212 int base = lua_gettop(state);
1213
1214 if (!init(out, state))
1215 {
1216 lua_settop(state, base);
1217 return false;
1218 }
1219
1220 // If not interactive, run without coroutine and bail out
1221 if (!out.is_console())
1222 return SafeCall(out, state, lua_gettop(state)-base-1, 0);
1223
1224 lua_rawgetp(state, LUA_REGISTRYINDEX, &DFHACK_QUERY_COROTABLE_TOKEN);
1225 lua_pushvalue(state, base+1);
1226 lua_remove(state, base+1);
1227 thread = Lua::NewCoroutine(state);
1228 lua_rawsetp(state, -2, thread);
1229 lua_pop(state, 1);
1230
1231 rv = resume_query_loop(out, thread, state, lua_gettop(state)-base, prompt, histfile);
1232 }
1233
1234 Console &con = static_cast<Console&>(out);
1235
1236 while (rv == LUA_YIELD)
1237 {
1238 if (histfile != histname)
1239 {
1240 if (!histname.empty())
1241 hist.save(std::filesystem::path{ histname });
1242
1243 hist.clear();
1244 histname = histfile;
1245
1246 if (!histname.empty())
1247 hist.load(std::filesystem::path{ histname });
1248 }
1249
1250 if (prompt.empty())
1251 prompt = ">> ";
1252
1253 std::string curline;

Callers

nothing calls this directly

Calls 15

lua_checkstackFunction · 0.85
lua_gettopFunction · 0.85
lua_settopFunction · 0.85
SafeCallFunction · 0.85
lua_rawgetpFunction · 0.85
lua_pushvalueFunction · 0.85
lua_rawsetpFunction · 0.85
resume_query_loopFunction · 0.85
lua_pushlstringFunction · 0.85
lua_pushnilFunction · 0.85
initFunction · 0.50
emptyMethod · 0.45

Tested by

no test coverage detected