first commit of some basics

This commit is contained in:
2026-01-17 09:13:59 +01:00
parent f9bc556ad9
commit c770c62400
41 changed files with 13610 additions and 0 deletions

82
TSE_Base/src/Debug.cpp Normal file
View File

@@ -0,0 +1,82 @@
#include "Debug.hpp"
#include "PathHelper.hpp"
std::ofstream TSE::Debug::logFile;
const std::string fileName = "log.txt";
const std::string fileNameOld = "lastlog.txt";
int luaPrintRedirect(lua_State* L)
{
int n = lua_gettop(L);
std::string out;
out.reserve(256);
for (int i = 1; i <= n; ++i) {
const char* s = lua_tostring(L, i);
if (s)
out += s;
else
out += "<non-string>";
if (i < n)
out += "\t";
}
TSE_LOG(out);
return 0;
}
void TSE::Debug::Init()
{
std::string path = "";
std::string oldpath = "";
GetAppDataPath(path);
CombinePaths(oldpath, path, fileNameOld);
CombinePaths(path, path, fileName);
if(FileExists(path))
{
if(FileExists(oldpath))
{
std::remove(oldpath.c_str());
}
std::rename(path.c_str(), oldpath.c_str());
}
CreateFileWriting(logFile, path);
luabridge::getGlobalNamespace(TSE_LUA_STATE)
.beginNamespace("debug")
.addFunction("log", TSE::Debug::Log)
.addFunction("warning", TSE::Debug::Warning)
.addFunction("error", TSE::Debug::Error)
.endNamespace();
lua_pushcfunction(TSE_LUA_STATE, luaPrintRedirect);
lua_setglobal(TSE_LUA_STATE, "print");
}
void TSE::Debug::Log(string msg)
{
std::cout << msg << std::endl;
logFile << msg << std::endl;
}
void TSE::Debug::Error(string msg)
{
std::cerr << "[Error] " << msg << std::endl;
logFile << "[Error] " << msg << std::endl;
}
void TSE::Debug::Warning(string msg)
{
std::cout << "[Warning] " << msg << std::endl;
logFile << "[Warning] " << msg << std::endl;
}
void TSE::Debug::Close()
{
logFile.flush();
logFile.close();
}

35
TSE_Base/src/Debug.hpp Normal file
View File

@@ -0,0 +1,35 @@
#pragma once
#include "LuaStateHandler.hpp"
#include "Types.hpp"
#define TSE_LOG(msg) TSE::Debug::Log(msg)
#define TSE_ERROR(msg) TSE::Debug::Error(msg)
#define TSE_WARNING(msg) TSE::Debug::Warning(msg)
namespace TSE
{
class Debug
{
public:
/// @brief initializes the log file in app data or .local, and binds lua functions
static void Init();
/// @brief logs soumething to the stdout, and into the log file
/// @param msg the message to be logged
static void Log(string msg);
/// @brief logs soumething to the stdout, and into the log file
/// @param msg the message to be logged
static void Error(string msg);
/// @brief logs soumething to the stdout, and into the log file
/// @param msg the message to be logged
static void Warning(string msg);
/// @brief closes the log file
static void Close();
private:
static std::ofstream logFile;
};
} // namespace TSE

View File

@@ -0,0 +1,42 @@
#include "LuaStateHandler.hpp"
#include "version.h"
#include "Debug.hpp"
void printVersionString()
{
TSE_LOG("TSE: " + TSE_VERSION_STRING);
}
void TSE::LuaStateHandler::InitLuaState()
{
L = luaL_newstate();
luaL_openlibs(L);
luabridge::getGlobalNamespace(TSE_LUA_STATE)
.beginNamespace("engine")
.addProperty("version", getVersionString)
.addFunction("printVersion", printVersionString)
.endNamespace();
}
void TSE::LuaStateHandler::RunLuaString(string cmd)
{
int res = luaL_dostring(L, cmd.c_str());
if (res != LUA_OK)
{
const char* err = lua_tostring(L, -1);
TSE_ERROR(err ? err : "<unknown lua error>");
lua_pop(L,1);
}
}
void TSE::LuaStateHandler::RunLuaScript(string path)
{
int res = luaL_dofile(L, path.c_str());
if (res != LUA_OK)
{
const char* err = lua_tostring(L, -1);
TSE_ERROR(err ? err : "<unknown lua error>");
lua_pop(L,1);
}
}

View File

@@ -0,0 +1,22 @@
#pragma once
#include <lua5.4/lua.hpp>
#include "LuaBridge.h"
#include "Types.hpp"
#define TSE_LUA_STATE TSE::LuaStateHandler::L
#define TSE_LUA_RUN_COMMAND(cmd) TSE::LuaStateHandler::RunLuaString(cmd)
#define TSE_LUA_RUN_SCRIPT(path) TSE::LuaStateHandler::RunLuaScript(path)
namespace TSE
{
class LuaStateHandler
{
public:
static inline lua_State* L;
static void InitLuaState();
static void RunLuaString(string cmd);
static void RunLuaScript(string path);
};
} // namespace TSE

View File

@@ -0,0 +1,68 @@
#include "PathHelper.hpp"
#include <filesystem>
#include <cstdlib>
#ifdef _WIN32
#include <windows.h>
#include <shlobj.h> // SHGetFolderPath
#pragma comment(lib, "shell32.lib")
#else
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#endif
bool TSE::FileExists(const std::string &path)
{
return std::filesystem::exists(path);
}
void TSE::OpenFileReading(std::ifstream &stream, const std::string &path)
{
stream.open(path, std::ios::in);
}
void TSE::CreateFileWriting(std::ofstream &stream, const std::string &path)
{
stream.open(path, std::ios::out);
}
void TSE::AppendFileWriting(std::ofstream &stream, const std::string &path)
{
stream.open(path, std::ios::app);
}
void TSE::GetAppDataPath(std::string &path)
{
#ifdef _WIN32
char appDataPath[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathA(nullptr, CSIDL_APPDATA, nullptr, 0, appDataPath))) {
path = std::string(appDataPath) + "\\TSE";
} else {
path = ".\\TSE"; // fallback
}
#else
const char* homeDir = getenv("HOME");
if (!homeDir) {
struct passwd* pw = getpwuid(getuid());
homeDir = pw->pw_dir;
}
path = std::string(homeDir) + "/.local/share/TSE";
#endif
if(!FileExists(path)){
std::filesystem::create_directory(path);
}
}
void TSE::CombinePaths(std::string &res, const std::string &path1, const std::string &path2)
{
#ifdef _WIN32
//check for path parts containing forwardlasches
res = path1 + "\\" + path2;
#else
//check for path parts containing backslasches
res = path1 + "/" + path2;
#endif
}

View File

@@ -0,0 +1,33 @@
#pragma once
#include "Types.hpp"
#include <fstream>
namespace TSE
{
/// @brief checks if a file exists at a path
/// @param path the path to check
/// @return file exists
bool FileExists(const std::string& path);
/// @brief opens a path for reading
/// @param stream the stream to be assigned
/// @param path the path to open
void OpenFileReading(std::ifstream& stream, const std::string& path);
/// @brief opens a path for writing
/// @param stream the stream to be assigned
/// @param path the path to open
void CreateFileWriting(std::ofstream& stream, const std::string& path);
/// @brief opens a path for appending
/// @param stream the stream to be assigned
/// @param path the path to open
void AppendFileWriting(std::ofstream& stream, const std::string& path);
/// @brief gets the platform specific tmp folder in appdata or .local
/// @param path the resulting path
void GetAppDataPath(std::string& path);
/// @brief combines two paths in a platform specific way
/// @param res the resulting path
/// @param path1 path part 1
/// @param path2 path part 2
void CombinePaths(std::string& res,const std::string& path1,const std::string& path2);
} // namespace TSE

17
TSE_Base/src/Types.hpp Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
#include <string>
namespace TSE
{
typedef std::string string;
typedef char byte;
typedef signed char sbyte;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
} // namespace TSE

10
TSE_Base/src/version.h Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
#include <string>
#define TSE_VERSION_MAJOR 0
#define TSE_VERSION_MINOR 0
#define TSE_VERSION_BUILD 1
#define TSE_VERSION_STRING std::to_string(TSE_VERSION_MAJOR) + "." + std::to_string(TSE_VERSION_MINOR) + "." + std::to_string(TSE_VERSION_BUILD)
std::string getVersionString() { return TSE_VERSION_STRING; }