| 82 | } window; |
| 83 | |
| 84 | bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) |
| 85 | { |
| 86 | thread_local struct { std::unique_ptr<lua_State, Functor<lua_close>> L{ luaL_newstate() }; operator lua_State*() { return L.get(); } } L; |
| 87 | thread_local auto _ = (luaL_openlibs(L), luaL_dostring(L, "function ProcessSentence() end")); |
| 88 | thread_local int revCount = 0; |
| 89 | |
| 90 | if (::revCount > revCount) |
| 91 | { |
| 92 | revCount = ::revCount; |
| 93 | luaL_dostring(L, "ProcessSentence = nil"); |
| 94 | if (luaL_dostring(L, script.Copy().c_str()) != LUA_OK) |
| 95 | { |
| 96 | sentence += L"\n" + FormatString(LUA_ERROR, StringToWideString(lua_tolstring(L, 1, nullptr))); |
| 97 | lua_settop(L, 0); |
| 98 | return logErrors; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (lua_getglobal(L, "ProcessSentence") != LUA_TFUNCTION) |
| 103 | { |
| 104 | sentence += L"\n" + FormatString(LUA_ERROR, L"ProcessSentence is not a function"); |
| 105 | lua_settop(L, 0); |
| 106 | return logErrors; |
| 107 | } |
| 108 | lua_pushstring(L, WideStringToString(sentence).c_str()); |
| 109 | lua_createtable(L, 0, 0); |
| 110 | for (auto info = sentenceInfo.infoArray; info->name; ++info) |
| 111 | { |
| 112 | lua_pushstring(L, info->name); |
| 113 | lua_pushinteger(L, info->value); |
| 114 | lua_settable(L, 3); |
| 115 | } |
| 116 | if (lua_pcallk(L, 2, 1, 0, NULL, NULL) != LUA_OK) |
| 117 | { |
| 118 | sentence += L"\n" + FormatString(LUA_ERROR, StringToWideString(lua_tolstring(L, 1, nullptr))); |
| 119 | lua_settop(L, 0); |
| 120 | return logErrors; |
| 121 | } |
| 122 | if (const char* newSentence = lua_tolstring(L, 1, nullptr)) |
| 123 | { |
| 124 | sentence = StringToWideString(newSentence); |
| 125 | lua_settop(L, 0); |
| 126 | return true; |
| 127 | } |
| 128 | lua_settop(L, 0); |
| 129 | return false; |
| 130 | } |
no test coverage detected