| 1 |
/* |
| 2 |
Copyright (c) 2009 Peter "Corsix" Cawley |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 |
this software and associated documentation files (the "Software"), to deal in |
| 6 |
the Software without restriction, including without limitation the rights to |
| 7 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 9 |
so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in all |
| 12 |
copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "../Src/main.h" |
| 24 |
|
| 25 |
#include "config.h" |
| 26 |
|
| 27 |
#include <SDL.h> |
| 28 |
#include <SDL_mixer.h> |
| 29 |
|
| 30 |
#include <cstdio> |
| 31 |
#include <memory> |
| 32 |
|
| 33 |
#include "../Src/bootstrap.h" |
| 34 |
#include "../Src/lua.hpp" |
| 35 |
#include "../Src/sdl_core.h" |
| 36 |
#include "../Src/th_lua.h" |
| 37 |
#ifdef WITH_UPDATE_CHECK |
| 38 |
#include <curl/curl.h> |
| 39 |
#endif |
| 40 |
#ifdef WITH_TRACY |
| 41 |
#include <tracy/Tracy.hpp> |
| 42 |
#include <tracy/TracyLua.hpp> |
| 43 |
#endif |
| 44 |
|
| 45 |
// Template magic for checking type equality |
| 46 |
template <typename T1, typename T2> |
| 47 |
struct types_equal { |
| 48 |
enum { |
| 49 |
result = -1, |
| 50 |
}; |
| 51 |
}; |
| 52 |
|
| 53 |
template <typename T1> |
| 54 |
struct types_equal<T1, T1> { |
| 55 |
enum { |
| 56 |
result = 1, |
| 57 |
}; |
| 58 |
}; |
| 59 |
|
| 60 |
#ifdef WITH_TRACY |
| 61 |
|
| 62 |
/// Replacement Lua Hook for Tracy Lua instrumentation |
| 63 |
/** |
| 64 |
* As of this implementation the lua hook included in Tracy does not handle |
| 65 |
* errors caught by pcall, so we implement our own hook that does. See: |
| 66 |
* https://github.com/wolfpld/tracy/issues/1320 |
| 67 |
*/ |
| 68 |
void l_tracy_hook(lua_State* L, lua_Debug* ar) { |
| 69 |
using namespace tracy; |
| 70 |
static int depth = 0; |
| 71 |
if (ar->event == LUA_HOOKCALL) { |
| 72 |
lua_getinfo(L, "Snl", ar); |
| 73 |
depth++; |
| 74 |
|
| 75 |
char src[256]; |
| 76 |
detail::LuaShortenSrc(src, ar->short_src); |
| 77 |
|
| 78 |
const auto srcloc = Profiler::AllocSourceLocation( |
| 79 |
ar->currentline, src, ar->name ? ar->name : ar->short_src); |
| 80 |
TracyQueuePrepare(QueueType::ZoneBeginAllocSrcLoc); |
| 81 |
MemWrite(&item->zoneBegin.time, Profiler::GetTime()); |
| 82 |
MemWrite(&item->zoneBegin.srcloc, srcloc); |
| 83 |
TracyQueueCommit(zoneBeginThread); |
| 84 |
} else if (ar->event == LUA_HOOKRET) { |
| 85 |
do { |
| 86 |
depth--; |
| 87 |
TracyQueuePrepare(QueueType::ZoneEnd); |
| 88 |
MemWrite(&item->zoneEnd.time, Profiler::GetTime()); |
| 89 |
TracyQueueCommit(zoneEndThread); |
| 90 |
} while (lua_getstack(L, depth, ar) == 0); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
#endif |
| 95 |
|
| 96 |
//! Program entry point |
| 97 |
/*! |
| 98 |
Prepares a Lua state for, and catches errors from, lua_init(). By |
| 99 |
executing in Lua mode as soon as possible, errors can be nicely caught |
| 100 |
sooner, hence this function does as little as possible and leaves the rest |
| 101 |
for lua_init(). |
| 102 |
*/ |
| 103 |
int main(int argc, char** argv) { |
| 104 |
struct compile_time_lua_check { |
| 105 |
// Lua 5.1, not 5.0, is required |
| 106 |
int lua_5_point_1_required[LUA_VERSION_NUM >= 501 ? 1 : -1]; |
| 107 |
|
| 108 |
// Lua numbers must be doubles so that the mantissa has at least |
| 109 |
// 32 bits (floats only have 24 bits) |
| 110 |
int number_is_double[types_equal<lua_Number, double>::result]; |
| 111 |
}; |
| 112 |
|
| 113 |
#ifdef WITH_UPDATE_CHECK |
| 114 |
curl_global_init(CURL_GLOBAL_DEFAULT); |
| 115 |
#endif |
| 116 |
|
| 117 |
bool bRun = true; |
| 118 |
|
| 119 |
while (bRun) { |
| 120 |
lua_state_unique_ptr L(luaL_newstate()); |
| 121 |
if (L == nullptr) { |
| 122 |
std::fprintf(stderr, |
| 123 |
"Fatal error starting CorsixTH: " |
| 124 |
"Cannot open Lua state.\n"); |
| 125 |
return 0; |
| 126 |
} |
| 127 |
#ifdef WITH_TRACY |
| 128 |
tracy::LuaRegister(L.get()); |
| 129 |
#endif |
| 130 |
#ifdef TRACY_ENABLE |
| 131 |
lua_sethook(L.get(), l_tracy_hook, LUA_MASKCALL | LUA_MASKRET, 0); |
| 132 |
#endif |
| 133 |
lua_atpanic(L.get(), lua_panic); |
| 134 |
luaL_openlibs(L.get()); |
| 135 |
lua_settop(L.get(), 0); |
| 136 |
lua_pushcfunction(L.get(), lua_stacktrace); |
| 137 |
lua_pushcfunction(L.get(), lua_init); |
| 138 |
|
| 139 |
// Move command line parameters onto the Lua stack |
| 140 |
lua_checkstack(L.get(), argc); |
| 141 |
for (int i = 0; i < argc; ++i) { |
| 142 |
lua_pushstring(L.get(), argv[i]); |
| 143 |
} |
| 144 |
|
| 145 |
if (lua_pcall(L.get(), argc, 0, 1) != 0) { |
| 146 |
const char* err = lua_tostring(L.get(), -1); |
| 147 |
if (err != nullptr) { |
| 148 |
std::fprintf(stderr, "%s\n", err); |
| 149 |
} else { |
| 150 |
std::fprintf(stderr, |
| 151 |
"An error has occurred in CorsixTH:\n" |
| 152 |
"Uncaught non-string Lua error\n"); |
| 153 |
} |
| 154 |
lua_pushcfunction(L.get(), bootstrap_lua_error_report); |
| 155 |
lua_insert(L.get(), -2); |
| 156 |
if (lua_pcall(L.get(), 1, 0, 0) != 0) { |
| 157 |
std::fprintf(stderr, "%s\n", lua_tostring(L.get(), -1)); |
| 158 |
} |
| 159 |
} |
| 160 |
mainloop(L.get()); |
| 161 |
|
| 162 |
lua_getfield(L.get(), LUA_REGISTRYINDEX, "_RESTART"); |
| 163 |
bRun = lua_toboolean(L.get(), -1) != 0; |
| 164 |
|
| 165 |
// Destroy the lua_State before SDL so that any SDL resource owned by |
| 166 |
// Lua can be freed first. |
| 167 |
L.reset(nullptr); |
| 168 |
while (Mix_QuerySpec(nullptr, nullptr, nullptr)) { |
| 169 |
Mix_CloseAudio(); |
| 170 |
} |
| 171 |
SDL_Quit(); |
| 172 |
|
| 173 |
if (bRun) { |
| 174 |
std::printf("\n\nRestarting...\n\n\n"); |
| 175 |
} |
| 176 |
} |
| 177 |
#ifdef WITH_UPDATE_CHECK |
| 178 |
curl_global_cleanup(); |
| 179 |
#endif |
| 180 |
return 0; |
| 181 |
} |