Preprocessor purge round 14: NOHTTP

This commit is contained in:
Tamás Bálint Misius 2023-01-08 15:54:11 +01:00
parent 7ea839feb8
commit a2a079356a
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
13 changed files with 132 additions and 108 deletions

View File

@ -351,6 +351,7 @@ conf_data.set('APPDATA', get_option('app_data'))
conf_data.set('APPVENDOR', get_option('app_vendor'))
conf_data.set('LUACONSOLE', lua_variant != 'none' ? 'true' : 'false')
conf_data.set('GRAVFFT', enable_gravfft ? 'true' : 'false')
conf_data.set('NOHTTP', not enable_http ? 'true' : 'false')
data_files = []

View File

@ -2,7 +2,6 @@
#include <cstdint>
// Boolean macros (defined / not defined), would be great to get rid of them all.
#mesondefine NOHTTP
#mesondefine RENDERER
#mesondefine FONTEDITOR
#mesondefine DEBUG
@ -16,6 +15,7 @@ constexpr bool BETA = @BETA@;
constexpr bool SNAPSHOT = @SNAPSHOT@;
constexpr bool MOD = @MOD@;
constexpr bool GRAVFFT = @GRAVFFT@;
constexpr bool NOHTTP = @NOHTTP@;
constexpr bool LUACONSOLE = @LUACONSOLE@;
constexpr bool ALLOW_FAKE_NEWER_VERSION = @ALLOW_FAKE_NEWER_VERSION@;
constexpr bool USE_UPDATESERVER = @USE_UPDATESERVER@;

View File

@ -1,7 +1,6 @@
font_conf_data = conf_data
font_conf_data.set('FONTEDITOR', true)
font_conf_data.set('RENDERER', false)
font_conf_data.set('NOHTTP', true)
configure_file(
input: config_template,
output: 'Config.h',

View File

@ -1,7 +1,6 @@
powder_conf_data = conf_data
powder_conf_data.set('FONTEDITOR', false)
powder_conf_data.set('RENDERER', false)
powder_conf_data.set('NOHTTP', not enable_http)
configure_file(
input: config_template,
output: 'Config.h',

View File

@ -1,7 +1,6 @@
render_conf_data = conf_data
render_conf_data.set('FONTEDITOR', false)
render_conf_data.set('RENDERER', true)
render_conf_data.set('NOHTTP', true)
configure_file(
input: config_template,
output: 'Config.h',

View File

@ -58,9 +58,10 @@ inline ByteString IntroText()
#ifdef REALISTIC
sb << " REALISTIC";
#endif
#ifdef NOHTTP
sb << " NOHTTP";
#endif
if constexpr (NOHTTP)
{
sb << " NOHTTP";
}
#ifdef DEBUG
sb << " DEBUG";
#endif

View File

@ -22,7 +22,7 @@
#include "LuaSlider.h"
#include "LuaTextbox.h"
#include "LuaWindow.h"
#include "LuaTCPSocket.h"
#include "LuaSocket.h"
#include "LuaHttp.h"
#include "LuaSDLKeys.h"
#include "PowderToy.h"
@ -222,9 +222,7 @@ LuaScriptInterface::LuaScriptInterface(GameController * c, GameModel * m):
initPlatformAPI();
initEventAPI();
initHttpAPI();
#ifndef NOHTTP
initSocketAPI();
#endif
initBZ2API(l);
@ -4648,12 +4646,10 @@ LuaScriptInterface::~LuaScriptInterface()
lua_close(l);
}
#ifndef NOHTTP
void LuaScriptInterface::initSocketAPI()
{
LuaTCPSocket::Open(l);
LuaSocket::Open(l);
}
#endif
void tpt_lua_pushByteString(lua_State *L, const ByteString &str)
{

77
src/lua/LuaSocket.cpp Normal file
View File

@ -0,0 +1,77 @@
#include "LuaSocket.h"
#include <stdint.h>
#include <algorithm>
#ifdef WIN
# include <windows.h>
# include <float.h>
#else
# include <sys/time.h>
# include <time.h>
#endif
#include "LuaScriptInterface.h"
#include "Misc.h"
namespace LuaSocket
{
double Now()
{
#ifdef WIN
FILETIME rt;
GetSystemTimeAsFileTime(&rt);
return (rt.dwLowDateTime + (uint64_t(rt.dwHighDateTime) << 32) - uint64_t(116444736000000000ULL)) / 1e7;
#else
struct timeval rt;
gettimeofday(&rt, (struct timezone *)NULL);
return rt.tv_sec + rt.tv_usec / 1e6;
#endif
}
static int GetTime(lua_State *l)
{
lua_pushnumber(l, Now());
return 1;
}
void Timeout(double timeout)
{
#ifdef WIN
if (timeout < 0.0) timeout = 0.0;
if (timeout < DBL_MAX / 1000.0) timeout *= 1000.0;
if (timeout > INT_MAX) timeout = INT_MAX;
::Sleep(int(timeout));
#else
struct timespec req, rem;
if (timeout < 0.0) timeout = 0.0;
if (timeout > INT_MAX) timeout = INT_MAX;
req.tv_sec = int(timeout);
req.tv_nsec = int((timeout - req.tv_sec) * 1000000000);
if (req.tv_nsec > 999999999) req.tv_nsec = 999999999;
while (nanosleep(&req, &rem))
{
req.tv_sec = rem.tv_sec;
req.tv_nsec = rem.tv_nsec;
}
#endif
}
static int Sleep(lua_State *l)
{
Timeout(luaL_checknumber(l, 1));
return 0;
}
void Open(lua_State *l)
{
lua_newtable(l);
struct luaL_Reg socketMethods[] = {
{ "sleep", LuaSocket::Sleep },
{ "gettime", LuaSocket::GetTime },
{ NULL, NULL },
};
luaL_register(l, NULL, socketMethods);
lua_setglobal(l, "socket");
OpenTCP(l);
}
}

11
src/lua/LuaSocket.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include "Config.h"
#include "LuaCompat.h"
namespace LuaSocket
{
double Now();
void Timeout(double timeout);
void Open(lua_State *l);
void OpenTCP(lua_State *l);
}

View File

@ -1,12 +1,10 @@
#include "LuaTCPSocket.h"
#include "LuaSocket.h"
#ifndef NOHTTP
# include "common/String.h"
# include <curl/curl.h>
# include <vector>
# include <stdexcept>
# include <cstring>
#endif
#include "common/String.h"
#include <curl/curl.h>
#include <vector>
#include <stdexcept>
#include <cstring>
#include <stdint.h>
#include <algorithm>
#ifdef WIN
@ -22,56 +20,8 @@
#include "client/http/CurlError.h"
#include "Misc.h"
namespace LuaTCPSocket
namespace LuaSocket
{
static double Now()
{
#ifdef WIN
FILETIME rt;
GetSystemTimeAsFileTime(&rt);
return (rt.dwLowDateTime + (uint64_t(rt.dwHighDateTime) << 32) - uint64_t(116444736000000000ULL)) / 1e7;
#else
struct timeval rt;
gettimeofday(&rt, (struct timezone *)NULL);
return rt.tv_sec + rt.tv_usec / 1e6;
#endif
}
static int GetTime(lua_State *l)
{
lua_pushnumber(l, Now());
return 1;
}
static void Timeout(double timeout)
{
#ifdef WIN
if (timeout < 0.0) timeout = 0.0;
if (timeout < DBL_MAX / 1000.0) timeout *= 1000.0;
if (timeout > INT_MAX) timeout = INT_MAX;
::Sleep(int(timeout));
#else
struct timespec req, rem;
if (timeout < 0.0) timeout = 0.0;
if (timeout > INT_MAX) timeout = INT_MAX;
req.tv_sec = int(timeout);
req.tv_nsec = int((timeout - req.tv_sec) * 1000000000);
if (req.tv_nsec > 999999999) req.tv_nsec = 999999999;
while (nanosleep(&req, &rem))
{
req.tv_sec = rem.tv_sec;
req.tv_nsec = rem.tv_nsec;
}
#endif
}
static int Sleep(lua_State *l)
{
Timeout(luaL_checknumber(l, 1));
return 0;
}
#ifndef NOHTTP
enum Status
{
StatusReady,
@ -663,43 +613,33 @@ namespace LuaTCPSocket
}
return luaL_error(l, "unknown direction");
}
#endif
void Open(lua_State *l)
void OpenTCP(lua_State *l)
{
#ifndef NOHTTP
luaL_newmetatable(l, "TCPSocket");
lua_pushcfunction(l, LuaTCPSocket::GC);
lua_pushcfunction(l, LuaSocket::GC);
lua_setfield(l, -2, "__gc");
lua_newtable(l);
struct luaL_Reg tcpSocketIndexMethods[] = {
{ "connect", LuaTCPSocket::Connect },
{ "close", LuaTCPSocket::Close },
{ "send", LuaTCPSocket::Send },
{ "receive", LuaTCPSocket::Receive },
{ "lasterror", LuaTCPSocket::LastError },
{ "status", LuaTCPSocket::GetStatus },
{ "getpeername", LuaTCPSocket::GetPeerName },
{ "getsockname", LuaTCPSocket::GetSockName },
{ "settimeout", LuaTCPSocket::SetTimeout },
{ "setoption", LuaTCPSocket::SetOption },
{ "shutdown", LuaTCPSocket::Shutdown },
{ "connect", LuaSocket::Connect },
{ "close", LuaSocket::Close },
{ "send", LuaSocket::Send },
{ "receive", LuaSocket::Receive },
{ "lasterror", LuaSocket::LastError },
{ "status", LuaSocket::GetStatus },
{ "getpeername", LuaSocket::GetPeerName },
{ "getsockname", LuaSocket::GetSockName },
{ "settimeout", LuaSocket::SetTimeout },
{ "setoption", LuaSocket::SetOption },
{ "shutdown", LuaSocket::Shutdown },
{ NULL, NULL },
};
luaL_register(l, NULL, tcpSocketIndexMethods);
lua_setfield(l, -2, "__index");
lua_pop(l, 1);
#endif
lua_newtable(l);
struct luaL_Reg socketMethods[] = {
#ifndef NOHTTP
{ "tcp", LuaTCPSocket::New },
#endif
{ "sleep", LuaTCPSocket::Sleep },
{ "gettime", LuaTCPSocket::GetTime },
{ NULL, NULL },
};
luaL_register(l, NULL, socketMethods);
lua_setglobal(l, "socket");
lua_getglobal(l, "socket");
lua_pushcfunction(l, LuaSocket::New);
lua_setfield(l, -2, "tcp");
lua_pop(l, 1);
}
}

View File

@ -0,0 +1,8 @@
#include "LuaSocket.h"
namespace LuaSocket
{
void OpenTCP(lua_State *l)
{
}
}

View File

@ -1,8 +0,0 @@
#pragma once
#include "Config.h"
#include "LuaCompat.h"
namespace LuaTCPSocket
{
void Open(lua_State *l);
}

View File

@ -13,11 +13,12 @@ luaconsole_files = files(
'LuaSmartRef.cpp',
'LuaTextbox.cpp',
'LuaWindow.cpp',
'LuaSocket.cpp',
)
if enable_http
luaconsole_files += files(
'LuaTCPSocket.cpp',
)
luaconsole_files += files('LuaSocketTCPHttp.cpp')
else
luaconsole_files += files('LuaSocketTCPNoHttp.cpp')
endif
subdir('luascripts')