first commit of some basics
This commit is contained in:
53
TSE_Base/CMakeLists.txt
Normal file
53
TSE_Base/CMakeLists.txt
Normal file
@@ -0,0 +1,53 @@
|
||||
#cmake version
|
||||
cmake_minimum_required(VERSION 3.31)
|
||||
|
||||
#project name
|
||||
project(TSE_Base)
|
||||
|
||||
#cpp settings
|
||||
find_program(CLANG_C NAMES clang)
|
||||
find_program(CLANG_CXX NAMES clang++)
|
||||
|
||||
if(CLANG_C AND CLANG_CXX)
|
||||
message(STATUS "foung Clang, using as Compiler")
|
||||
set(CMAKE_C_COMPILER ${CLANG_C} CACHE STRING "C Compiler" FORCE)
|
||||
set(CMAKE_CXX_COMPILER ${CLANG_CXX} CACHE STRING "C++ Compiler" FORCE)
|
||||
else()
|
||||
message(STATUS "Clang not found, using Standard-Compiler")
|
||||
endif()
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
|
||||
#project output settings
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/lib")
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${PROJECT_SOURCE_DIR}/lib/Debug")
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${PROJECT_SOURCE_DIR}/lib/Release")
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/lib")
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${PROJECT_SOURCE_DIR}/lib/Debug")
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE "${PROJECT_SOURCE_DIR}/lib/Release")
|
||||
|
||||
#source files
|
||||
file(GLOB CPP_SOURCE_TSE
|
||||
"${PROJECT_SOURCE_DIR}/src/*.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/*/*.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/*/*/*.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/*/*/*/*.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/src/*.c"
|
||||
"${PROJECT_SOURCE_DIR}/src/*/*.c"
|
||||
"${PROJECT_SOURCE_DIR}/src/*/*/*.c"
|
||||
"${PROJECT_SOURCE_DIR}/src/*/*/*/*.c"
|
||||
)
|
||||
|
||||
#includes
|
||||
include_directories(${PROJECT_SOURCE_DIR}/src)
|
||||
include_directories(${PROJECT_SOURCE_DIR}/include)
|
||||
|
||||
#project def
|
||||
if(Lib)
|
||||
add_library(TSE_Base SHARED ${CPP_SOURCE_TSE})
|
||||
else()
|
||||
add_library(TSE_Base STATIC ${CPP_SOURCE_TSE})
|
||||
endif()
|
||||
|
||||
#flags
|
||||
target_compile_options(TSE_Base PRIVATE -march=native)
|
||||
10664
TSE_Base/include/LuaBridge.h
Normal file
10664
TSE_Base/include/LuaBridge.h
Normal file
File diff suppressed because it is too large
Load Diff
82
TSE_Base/src/Debug.cpp
Normal file
82
TSE_Base/src/Debug.cpp
Normal 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
35
TSE_Base/src/Debug.hpp
Normal 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
|
||||
|
||||
|
||||
42
TSE_Base/src/LuaStateHandler.cpp
Normal file
42
TSE_Base/src/LuaStateHandler.cpp
Normal 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);
|
||||
}
|
||||
}
|
||||
22
TSE_Base/src/LuaStateHandler.hpp
Normal file
22
TSE_Base/src/LuaStateHandler.hpp
Normal 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
|
||||
68
TSE_Base/src/PathHelper.cpp
Normal file
68
TSE_Base/src/PathHelper.cpp
Normal 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
|
||||
}
|
||||
33
TSE_Base/src/PathHelper.hpp
Normal file
33
TSE_Base/src/PathHelper.hpp
Normal 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
17
TSE_Base/src/Types.hpp
Normal 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
10
TSE_Base/src/version.h
Normal 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; }
|
||||
Reference in New Issue
Block a user