Skip to content

Commit

Permalink
fix lua-https bit here
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtleP committed Nov 3, 2023
1 parent e8357e1 commit 0275447
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/common/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ namespace love
static constexpr Version LOVE_FRAMEWORK(__LOVE_VERSION__);

static constexpr const char* CODENAME = "Mysterious Mysteries";
static constexpr std::array<const char*, 1> COMPATIBILITY = { __APP_VERSION__ };
static constexpr std::array<const char*, 2> COMPATIBILITY = { __APP_VERSION__, nullptr };
} // namespace love
7 changes: 7 additions & 0 deletions include/modules/love/love.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ namespace love
int GetVersion(lua_State* L);

int IsVersionCompatible(lua_State* L);

int SetGammaCorrect(lua_State* L);
} // namespace love

extern "C"
{
extern int luaopen_https(lua_State*);
}
8 changes: 4 additions & 4 deletions include/utilities/log/logfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
class Log
{
public:
static Log& Instance()
static Log& Instance(bool enable = false)
{
static Log instance;
static Log instance(enable);
return instance;
}

Expand Down Expand Up @@ -51,9 +51,9 @@ class Log
static inline const char* FILENAME = "debug.log";
static constexpr const char* BUFFER_FORMAT = "%s(%zu:%zu): `%s`:\n%s\n\n";

Log() : file(nullptr)
Log(bool enable) : file(nullptr)
{
if (m_enabled)
if (m_enabled || enable)
this->file = fopen(FILENAME, "w");
}

Expand Down
69 changes: 64 additions & 5 deletions source/modules/love/love.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <common/luax.hpp>
#include <common/version.hpp>

#include <common/HTTPSCommon.h>
#include <luasocket.hpp>

#include <modules/love/love.hpp>
Expand Down Expand Up @@ -32,6 +31,8 @@ static constexpr char nestlink_lua[] = {
#include "scripts/nestlink.lua"
};

#include <modules/graphics/graphics.tcc>

#include <modules/audio/wrap_audio.hpp>
#include <modules/data/wrap_data.hpp>
#include <modules/event/wrap_event.hpp>
Expand Down Expand Up @@ -85,9 +86,34 @@ static constexpr luaL_Reg modules[] =
// clang-format on

#include <modules/system_ext.hpp>
#include <utilities/log/logfile.hpp>

static void addCompatibilityAlias(lua_State* L, const char* module, const char* name,
const char* alias)
{
lua_getglobal(L, module);

if (lua_istable(L, -1))
{
lua_getfield(L, -1, alias);
bool hasAlias = !lua_isnoneornil(L, -1);
lua_pop(L, 1);

if (!hasAlias)
{
lua_getfield(L, -1, name);
lua_setfield(L, -2, alias);
}
}

lua_pop(L, 1);
}

int love::Initialize(lua_State* L)
{
for (size_t i = 0; modules[i].name != nullptr; i++)
luax::Preload(L, modules[i].func, modules[i].name);

luax::InsistPinnedThread(L);
luax::InsistGlobal(L, "love");

Expand Down Expand Up @@ -130,23 +156,49 @@ int love::Initialize(lua_State* L)
lua_pushnumber(L, LOVE_POTION.micro);
lua_setfield(L, -2, "_potion_version_revision");

lua_pushcfunction(L, GetVersion);
lua_newtable(L);
for (int index = 0; love::COMPATIBILITY[index] != nullptr; index++)
{
lua_pushstring(L, love::COMPATIBILITY[index]);
lua_rawseti(L, -2, index + 1);
}
lua_setfield(L, -2, "_version_compat");

lua_pushcfunction(L, love::GetVersion);
lua_setfield(L, -2, "getVersion");

lua_pushcfunction(L, IsVersionCompatible);
lua_pushcfunction(L, love::IsVersionCompatible);
lua_setfield(L, -2, "isVersionCompatible");

for (size_t i = 0; modules[i].name != nullptr; i++)
luax::Preload(L, modules[i].func, modules[i].name);
lua_pushstring(L, System<Console::Which>::GetOS());
lua_setfield(L, -2, "_os");

lua_pushstring(L, __CONSOLE__);
lua_setfield(L, -2, "_console");

luax::Require(L, "love.data");
lua_pop(L, 1);

#if LUA_VERSION_NUM <= 501
addCompatibilityAlias(L, "math", "fmod", "mod");
addCompatibilityAlias(L, "string", "gmatch", "gfind");
#endif

love::luasocket::preload(L);

luax::Preload(L, luaopen_luautf8, "utf8");
luax::Preload(L, luaopen_https, "https");

{
lua_atpanic(L, [](lua_State* L) -> int {
auto location = std::source_location::current();
const auto* message = "PANIC: unprotected error in call to Lua API (%s)\n";
Log::Instance(true).Write(location, message, lua_tostring(L, -1));

return 0;
});
}

return 1;
}

Expand All @@ -160,6 +212,13 @@ int love::GetVersion(lua_State* L)
return 4;
}

int love::SetGammaCorrect(lua_State* L)
{
love::Graphics<>::SetGammaCorrect((bool)lua_toboolean(L, 1));

return 0;
}

int love::IsVersionCompatible(lua_State* L)
{
Version check {};
Expand Down

0 comments on commit 0275447

Please sign in to comment.