0% found this document useful (0 votes)
5 views4 pages

Lua Server Initialization Code

The document outlines a C++ program that implements a server using Winsock to receive Lua scripts over TCP. It initializes a Lua state, sets up a listening socket on port 5454, and processes incoming client connections to execute received scripts. The program also includes a mechanism for managing internal flags and a thread for server operations, ensuring proper cleanup on termination.

Uploaded by

clncult.02
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Lua Server Initialization Code

The document outlines a C++ program that implements a server using Winsock to receive Lua scripts over TCP. It initializes a Lua state, sets up a listening socket on port 5454, and processes incoming client connections to execute received scripts. The program also includes a mechanism for managing internal flags and a thread for server operations, ensuring proper cleanup on termination.

Uploaded by

clncult.02
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <lua.

hpp>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <string>
#include <vector>
#include <thread>
#include <memory>
#include <iostream> // For debug output

#include "[Link]"
#include "[Link]"
#include "[Link]"
#include "overlay/[Link]"
#include "[Link]"

#pragma comment(lib, "ws2_32.lib")


#pragma comment(lib, "[Link]") // Adjust to your Luau/Lua library

// Global Lua state


lua_State* LuaState = nullptr;

// Placeholder for MaxCaps


#ifndef MaxCaps
#define MaxCaps 0xFFFFFFFFFFFFFFFF
#endif

#define PORT "5454"

// Event to signal server initialization


HANDLE hServerReady = nullptr;

// Placeholder for disable_flags


void disable_flags() {
// Assuming Offsets::InternalFastFlags is defined in [Link]
*reinterpret_cast<BYTE*>(Offsets::InternalFastFlags::EnableLoadModule) = TRUE;
*reinterpret_cast<BYTE*>(Offsets::InternalFastFlags::DebugCheckRenderThreading)
= FALSE;

*reinterpret_cast<BYTE*>(Offsets::InternalFastFlags::RenderDebugCheckThreading2) =
FALSE;

*reinterpret_cast<BYTE*>(Offsets::InternalFastFlags::DisableCorescriptLoadstring) =
FALSE;

*reinterpret_cast<BYTE*>(Offsets::InternalFastFlags::LuaStepIntervalMsOverrideEnabl
ed) = FALSE;

*reinterpret_cast<BYTE*>(Offsets::InternalFastFlags::LockViolationInstanceCrash) =
FALSE;
}

// Placeholder for Scheduler


struct Scheduler {
static lua_State* GetLuaState() {
return luaL_newstate(); // Replace with actual implementation
}
};
bool recvAll(SOCKET sock, char* buffer, int totalBytes) {
int received = 0;
while (received < totalBytes) {
int result = recv(sock, buffer + received, totalBytes - received, 0);
if (result <= 0) return false;
received += result;
}
return true;
}

void ServerThread() {
WSADATA wsaData;
struct addrinfo* result = nullptr, hints = {};
SOCKET ListenSocket = INVALID_SOCKET, ClientSocket = INVALID_SOCKET;

if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {


std::cerr << "WSAStartup failed: " << WSAGetLastError() << std::endl;
return;
}

hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;

if (getaddrinfo(nullptr, PORT, &hints, &result) != 0) {


std::cerr << "getaddrinfo failed: " << WSAGetLastError() << std::endl;
WSACleanup();
return;
}

ListenSocket = socket(result->ai_family, result->ai_socktype, result-


>ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
std::cerr << "socket failed: " << WSAGetLastError() << std::endl;
freeaddrinfo(result);
WSACleanup();
return;
}

if (bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen) ==


SOCKET_ERROR) {
std::cerr << "bind failed: " << WSAGetLastError() << std::endl;
closesocket(ListenSocket);
freeaddrinfo(result);
WSACleanup();
return;
}

freeaddrinfo(result);

if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR) {


std::cerr << "listen failed: " << WSAGetLastError() << stdendl;
closesocket(ListenSocket);
WSACleanup();
return;
}

std::cout << "Server listening on port " << PORT << std::endl;
SetEvent(hServerReady); // Signal that the server is ready

while (true) {
ClientSocket = accept(ListenSocket, nullptr, nullptr);
if (ClientSocket == INVALID_SOCKET) {
std::cerr << "accept failed: " << WSAGetLastError() << std::endl;
continue;
}

uint32_t scriptSizeNet = 0;
if (!recvAll(ClientSocket, reinterpret_cast<char*>(&scriptSizeNet),
sizeof(uint32_t))) {
std::cerr << "Failed to receive script size" << std::endl;
closesocket(ClientSocket);
continue;
}

uint32_t scriptSize = ntohl(scriptSizeNet);


if (scriptSize == 0 || scriptSize > 10 * 1024 * 1024) {
std::cerr << "Invalid script size: " << scriptSize << std::endl;
closesocket(ClientSocket);
continue;
}

std::vector<char> buffer(scriptSize + 1, 0);


if (!recvAll(ClientSocket, [Link](), scriptSize)) {
std::cerr << "Failed to receive script data" << std::endl;
closesocket(ClientSocket);
continue;
}

std::string receivedScript([Link](), scriptSize);


if (Execution) {
Execution->Execute(LuaState, receivedScript);
}
closesocket(ClientSocket);
}

closesocket(ListenSocket);
WSACleanup();
}

void Start() {
disable_flags();

LuaState = Scheduler::GetLuaState();
if (!LuaState) {
std::cerr << "Failed to initialize LuaState" << std::endl;
return;
}
luaL_openlibs(LuaState);

LuaState->userdata = new lua_userdata_t; // Assuming userdata is a struct


LuaState->userdata->Identity = 8;
LuaState->userdata->Capabilities = MaxCaps;

Environment->Initialize(LuaState);
luaL_sandboxthread(LuaState); // Replace with actual sandboxing if defined
Execution = std::make_unique<BytecodeEncoderClass::CExecution>();

if (Execution) {
Execution->Execute(LuaState, "print(\"InfernoX module Loaded\")");
}

std::thread(gui::overlay::render).detach();
}

DWORD WINAPI InitThread(LPVOID lpParam) {


hServerReady = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!hServerReady) {
std::cerr << "Failed to create event: " << GetLastError() << std::endl;
return 1;
}

std::thread serverThread(ServerThread);
WaitForSingleObject(hServerReady, INFINITE); // Wait for server to be ready
CloseHandle(hServerReady);
hServerReady = nullptr;

Start(); // Start other initialization after server is ready

[Link](); // Ensure thread cleanup


return 0;
}

BOOL APIENTRY DllMain(HMODULE Module, DWORD Reason, LPVOID Reserved) {


switch (Reason) {
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(Module);
CreateThread(NULL, 0, InitThread, NULL, 0, NULL);
break;
case DLL_PROCESS_DETACH:
if (LuaState) {
lua_close(LuaState);
LuaState = nullptr;
}
if (hServerReady) {
CloseHandle(hServerReady);
}
break;
}
return TRUE;
}

You might also like