-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e508839
Showing
44 changed files
with
8,558 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vscode | ||
build* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[submodule "third-party/lua"] | ||
path = third-party/lua | ||
url = https://github.com/lubgr/lua-cmake | ||
[submodule "third-party/moonscript"] | ||
path = third-party/moonscript | ||
url = https://github.com/leafo/moonscript.git | ||
[submodule "third-party/efsw"] | ||
path = third-party/efsw | ||
url = https://github.com/SpartanJ/efsw.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
cmake_minimum_required(VERSION 3.22) | ||
|
||
# Require C++ 17 | ||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
# Enable -fPIC flag | ||
set(CMAKE_POSITION_INDEPENDENT_CODE ON) | ||
|
||
# Enable IDE folders | ||
set_property(GLOBAL PROPERTY USE_FOLDERS ON) | ||
|
||
# Force x86_64 architecture on MacOSX | ||
if(APPLE) | ||
set(CMAKE_OSX_ARCHITECTURES "x86_64") | ||
|
||
# Disable deprecation warnings | ||
add_compile_options( | ||
-Wno-deprecated-declarations | ||
) | ||
endif() | ||
|
||
# Enable multithreaded compilation on Windows | ||
if(MSVC) | ||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" "/MP") | ||
endif() | ||
|
||
# Include extensions | ||
add_subdirectory(cmake) | ||
|
||
# CmakeRC compiler | ||
file(DOWNLOAD "https://raw.githubusercontent.com/vector-of-bool/cmrc/master/CMakeRC.cmake" | ||
"${CMAKE_BINARY_DIR}/CMakeRC.cmake") | ||
include("${CMAKE_BINARY_DIR}/CMakeRC.cmake") | ||
|
||
# Entropia File System Watcher | ||
add_subdirectory(third-party/efsw) | ||
|
||
# Include embedded lua | ||
add_subdirectory(third-party/lua) | ||
add_subdirectory(third-party/lpeg) | ||
|
||
# Add moonengine library | ||
add_subdirectory(moonengine) | ||
|
||
# Include garrysmod_common | ||
find_garrysmod_common() | ||
if(NOT GARRYSMOD_COMMON_FOUND) # Check if garrysmod_common has been found | ||
message(FATAL_ERROR "garrysmod_common not found") | ||
endif() | ||
|
||
project(gm_moonloader VERSION 1.0.0 LANGUAGES CXX) | ||
|
||
add_subdirectory(source) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Retro | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include(find_garrysmod_common.cmake) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
function(find_garrysmod_common) | ||
message(STATUS "Looking for garrysmod_common...") | ||
set(GARRYSMOD_COMMON_PATH "GARRYSMOD_COMMON_NOT_FOUND" CACHE PATH "Path to garrysmod_common (https://github.com/dankmolot/garrysmod_common/tree/master-cmake)") | ||
|
||
if(NOT IS_DIRECTORY ${GARRYSMOD_COMMON_PATH} OR NOT EXISTS ${GARRYSMOD_COMMON_PATH}/CMakeLists.txt OR ${GARRYSMOD_COMMON_PATH} STREQUAL ${CMAKE_CURRENT_LIST_DIR}) | ||
message(FATAL_ERROR "Invalid path to garrysmod_common. Please set valid GARRYSMOD_COMMON_PATH") | ||
endif() | ||
|
||
add_subdirectory(${GARRYSMOD_COMMON_PATH} ${CMAKE_BINARY_DIR}/garrysmod_common) | ||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
project(moonengine VERSION 1.0.0 LANGUAGES CXX) | ||
|
||
file(GLOB_RECURSE SOURCES RELATIVE ${CMAKE_CURRENT_LIST_DIR} src/*.cpp src/*.hpp src/*.h) | ||
source_group(TREE ${CMAKE_CURRENT_LIST_DIR}/src PREFIX "Sources" FILES ${SOURCES}) | ||
|
||
file(GLOB_RECURSE HEADERS RELATIVE ${CMAKE_CURRENT_LIST_DIR} include/*.hpp) | ||
source_group(TREE ${CMAKE_CURRENT_LIST_DIR}/include PREFIX "Headers" FILES ${HEADERS}) | ||
|
||
SET(MOONSCRIPT_ROOT ${CMAKE_CURRENT_LIST_DIR}/../third-party/moonscript) | ||
file(GLOB_RECURSE MOONSCRIPT_RESOURCES RELATIVE ${CMAKE_CURRENT_LIST_DIR} ${MOONSCRIPT_ROOT}/*.lua) | ||
|
||
cmrc_add_resource_library( | ||
moongengine_resources | ||
|
||
ALIAS moonengine::resources | ||
NAMESPACE MoonEngine | ||
WHENCE ${MOONSCRIPT_ROOT} | ||
|
||
${MOONSCRIPT_RESOURCES} | ||
) | ||
|
||
add_library(moonengine STATIC EXCLUDE_FROM_ALL ${SOURCES} ${HEADERS}) | ||
target_link_libraries(moonengine PRIVATE | ||
moonengine::resources | ||
lua::lib | ||
lpeg | ||
|
||
sourcesdk::tier1 | ||
) | ||
|
||
target_include_directories(moonengine PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include) | ||
target_include_directories(moonengine PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include/moonengine) | ||
target_include_directories(moonengine PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef MOONENGINE_ENGINE_HPP | ||
#define MOONENGINE_ENGINE_HPP | ||
|
||
#include <string> | ||
#include <string_view> | ||
|
||
struct lua_State; | ||
|
||
namespace MoonEngine { | ||
class Engine { | ||
lua_State* m_State = nullptr; | ||
bool m_Initialized = false; | ||
|
||
int m_ToLuaRef = 0; | ||
public: | ||
Engine(); | ||
~Engine(); | ||
|
||
bool IsInitialized() { return m_Initialized; } | ||
|
||
void RunLua(const char* luaCode); | ||
std::string CompileString(const char* moonCode, size_t len); | ||
std::string CompileString(std::string_view moonCode); | ||
}; | ||
} | ||
|
||
#endif // MOONENGINE_ENGINE_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include "moonengine/engine.hpp" | ||
#include "lua.hpp" | ||
#include "moonscript/entry.hpp" | ||
|
||
#include <string> | ||
#include <tier0/dbg.h> | ||
#include <lpeg.hpp> | ||
|
||
using namespace MoonEngine; | ||
|
||
int print(lua_State* L) { | ||
int top = lua_gettop(L); | ||
for (int i = 0; i < top; i++) { | ||
Msg("%s\t", luaL_tolstring(L, i + 1, 0)); | ||
} | ||
Msg("\n"); | ||
return 0; | ||
} | ||
|
||
int xpcall_errhandler(lua_State* L) { | ||
luaL_traceback(L, L, lua_tostring(L, 1), 0); | ||
Warning("%s\n", lua_tostring(L, -1)); | ||
lua_pop(L, 1); | ||
return 0; | ||
} | ||
|
||
Engine::Engine() { | ||
m_State = luaL_newstate(); // Create new Lua state | ||
luaL_openlibs(m_State); // Initialize Lua standard libraries | ||
|
||
lua_getfield(m_State, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); | ||
lua_pushcfunction(m_State, luaopen_lpeg); | ||
lua_setfield(m_State, -2, "lpeg"); | ||
|
||
lua_pushcfunction(m_State, print); | ||
lua_setglobal(m_State, "print"); | ||
|
||
lua_pushcfunction(m_State, luaopen_moonscript); | ||
if (lua_pcall(m_State, 0, 0, 0) != LUA_OK) { | ||
Warning("failed to preload moonscript: %s\n", lua_tostring(m_State, -1)); | ||
lua_pop(m_State, 1); | ||
return; | ||
} | ||
|
||
lua_getglobal(m_State, "require"); | ||
lua_pushstring(m_State, "moonscript.base"); | ||
if (lua_pcall(m_State, 1, 1, 0) != LUA_OK) { | ||
Warning("failed to load moonscript: %s\n", lua_tostring(m_State, -1)); | ||
lua_pop(m_State, 1); | ||
return; | ||
} | ||
|
||
lua_getfield(m_State, -1, "to_lua"); | ||
m_ToLuaRef = luaL_ref(m_State, LUA_REGISTRYINDEX); | ||
|
||
lua_pop(m_State, 1); // Pop moonscript table | ||
|
||
m_Initialized = true; | ||
} | ||
|
||
Engine::~Engine() { | ||
lua_close(m_State); | ||
m_State = nullptr; | ||
} | ||
|
||
void Engine::RunLua(const char* luaCode) { | ||
lua_pushcfunction(m_State, xpcall_errhandler); | ||
if (luaL_loadstring(m_State, luaCode) != LUA_OK) { | ||
Warning("failed to parse input: %s\n", lua_tostring(m_State, -1)); | ||
lua_pop(m_State, 2); | ||
return; | ||
}; | ||
if (lua_pcall(m_State, 0, 0, -2) != LUA_OK) { | ||
lua_pop(m_State, 1); | ||
}; | ||
lua_pop(m_State, 1); | ||
} | ||
|
||
std::string MoonEngine::Engine::CompileString(const char* moonCode, size_t len) { | ||
if (m_ToLuaRef == 0) { return {}; } | ||
|
||
lua_pushcfunction(m_State, xpcall_errhandler); | ||
lua_rawgeti(m_State, LUA_REGISTRYINDEX, m_ToLuaRef); | ||
lua_pushlstring(m_State, moonCode, len); | ||
if (lua_pcall(m_State, 1, 1, -3) != LUA_OK) { | ||
lua_pop(m_State, 2); | ||
return {}; | ||
} | ||
|
||
std::string luaCode = lua_tostring(m_State, -1); | ||
lua_pop(m_State, 2); | ||
return luaCode; | ||
} | ||
|
||
std::string Engine::CompileString(std::string_view moonCode) { | ||
return CompileString(moonCode.data(), moonCode.size()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Just lua headers reexports for C++. | ||
extern "C" { | ||
#include <lua.h> | ||
#include <lualib.h> | ||
#include <lauxlib.h> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "entry.hpp" | ||
#include "../lua.hpp" | ||
|
||
#include <tier0/dbg.h> | ||
#include <string> | ||
#include <string_view> | ||
#include <cmrc/cmrc.hpp> | ||
#include <algorithm> | ||
|
||
CMRC_DECLARE(MoonEngine); | ||
|
||
void preload_file(lua_State* L, std::string filePath) { | ||
auto fs = cmrc::MoonEngine::get_filesystem(); | ||
|
||
try { | ||
auto file = fs.open(filePath); | ||
lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); | ||
if (luaL_loadstring(L, file.begin()) != LUA_OK) { | ||
lua_pop(L, 2); | ||
throw; | ||
} | ||
|
||
// Modify our filePath, so it can be as package path | ||
filePath = filePath.substr(0, filePath.size() - 4); // Remove .lua extension | ||
std::replace(filePath.begin(), filePath.end(), '/', '.'); // Replace '/' to '.' | ||
|
||
lua_setfield(L, -2, filePath.c_str()); // Set our compiled string to preload table | ||
lua_pop(L, 1); // Pop preload table | ||
} catch (...) { | ||
Warning("[MoonEngine] Failed to preload file: %s\n", filePath.c_str()); | ||
} | ||
} | ||
|
||
void preload_folder(lua_State* L, std::string dir) { | ||
auto fs = cmrc::MoonEngine::get_filesystem(); | ||
for (const auto& f : fs.iterate_directory(dir)) { | ||
std::string filePath = dir + "/" + f.filename(); | ||
if (f.is_directory()) { | ||
preload_folder(L, filePath); | ||
continue; | ||
} | ||
|
||
preload_file(L, filePath); | ||
} | ||
} | ||
|
||
int luaopen_moonscript(lua_State* L) { | ||
preload_folder(L, "moonscript"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef MOONENGINE_MOONSCRIPT_ENTRY_HPP | ||
#define MOONENGINE_MOONSCRIPT_ENTRY_HPP | ||
|
||
struct lua_State; | ||
|
||
int luaopen_moonscript(lua_State *L); | ||
|
||
#endif // MOONENGINE_MOONSCRIPT_ENTRY_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Include every source file in source directory | ||
file(GLOB_RECURSE SOURCES RELATIVE ${CMAKE_CURRENT_LIST_DIR} *.cpp *.hpp) | ||
source_group(TREE ${CMAKE_CURRENT_LIST_DIR} PREFIX "Sources" FILES ${SOURCES}) | ||
|
||
add_library(moonloader SHARED ${SOURCES}) | ||
target_link_libraries(moonloader PRIVATE | ||
gmod::common | ||
gmod::detouring | ||
gmod::helpers_extended | ||
sourcesdk::common | ||
sourcesdk::tier0 | ||
sourcesdk::tier1 | ||
|
||
efsw | ||
moonengine | ||
) | ||
|
||
target_include_directories(moonloader PRIVATE ${CMAKE_CURRENT_LIST_DIR}) | ||
|
||
set_gmod_suffix_prefix(moonloader) | ||
|
||
# Autoinstall | ||
set(AUTOINSTALL "" CACHE PATH "Autoinstall path") | ||
if(IS_DIRECTORY ${AUTOINSTALL}) | ||
autoinstall(moonloader ${AUTOINSTALL}) | ||
endif() |
Oops, something went wrong.