Skip to content

Commit 69d173c

Browse files
committed
GOOMod now creates a project
1 parent 37c30e6 commit 69d173c

File tree

8 files changed

+79
-30
lines changed

8 files changed

+79
-30
lines changed

goomod/include/GarrysMod/Module.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@
55
namespace GarrysMod {
66
template <typename ModuleClass> class Module : public ModuleHelper {
77
public:
8+
static inline ModuleFactory CreateModuleFactory(const std::string& module_name) noexcept {
9+
return [=] { return std::make_shared<ModuleClass>(module_name); };
10+
}
11+
12+
#ifdef GMOD_MODULE_NAME
813
static inline ModuleFactory CreateModuleFactory() noexcept {
9-
return [] { return std::make_shared<ModuleClass>(); };
14+
return CreateModuleFactory(GMOD_MODULE_NAME);
1015
}
16+
#endif
1117

1218
protected:
1319
typedef int (ModuleClass::*MemberFunctionType)(Lua::ILuaBase *LUA);
1420
typedef std::function<int(ModuleClass *, Lua::ILuaBase *)>
1521
MemberFunctionalType;
1622

17-
bool EnableThink(Lua::ILuaBase *LUA) {
23+
inline Module(const std::string& module_name) : ModuleHelper(module_name) {}
24+
25+
inline bool EnableThink(Lua::ILuaBase *LUA) {
1826
const int lua_ref =
1927
CreateFunctionLuaReference(LUA, &Module::Think, m_member_functions);
2028
return ModuleHelper::EnableThink(

goomod/include/GarrysMod/ModuleBase.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,5 @@ class ModuleBase : public std::enable_shared_from_this<ModuleBase> {
148148
friend class ModuleHelper;
149149
};
150150

151-
typedef std::function<std::shared_ptr<ModuleBase>()> ModuleFactory;
152-
153-
extern const ModuleFactory ModuleFactorySingleton;
151+
typedef std::function<std::shared_ptr<ModuleBase>()> SubModuleFactory;
154152
} // namespace GarrysMod

goomod/include/GarrysMod/ModuleHelper.hpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@
22

33
#include "ModuleBase.hpp"
44

5+
#include <functional>
6+
#include <memory>
7+
#include <string>
58
#include <vector>
69

710
namespace GarrysMod {
811
class ModuleHelper : public ModuleBase {
12+
public:
13+
int Initialize(Lua::ILuaBase* LUA) override;
14+
int Deinitialize(Lua::ILuaBase* LUA) override;
15+
16+
const std::string& ModuleName() const;
17+
918
protected:
10-
int Initialize(Lua::ILuaBase *LUA) override;
11-
int Deinitialize(Lua::ILuaBase *LUA) override;
19+
ModuleHelper(const std::string& module_name);
1220

1321
void
14-
AddSubModuleFactory(std::function<std::shared_ptr<ModuleBase>()> &&factory);
22+
AddSubModuleFactory(SubModuleFactory&& factory);
1523

1624
bool EnableThink(Lua::ILuaBase *LUA, MemberFunctionWrapperType wrapper,
1725
int function_reference) const;
@@ -20,10 +28,14 @@ class ModuleHelper : public ModuleBase {
2028
int Think(Lua::ILuaBase *LUA) override;
2129

2230
private:
31+
const std::string m_module_name;
2332
bool m_think_enabled = false;
2433

25-
std::vector<std::function<std::shared_ptr<ModuleBase>()>>
26-
m_submodule_factories;
34+
std::vector<SubModuleFactory> m_submodule_factories;
2735
std::vector<std::shared_ptr<ModuleBase>> m_submodules;
2836
};
37+
38+
typedef std::function<std::shared_ptr<ModuleHelper>()> ModuleFactory;
39+
40+
extern const ModuleFactory ModuleFactorySingleton;
2941
} // namespace GarrysMod

goomod/include/GarrysMod/SubModule.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace GarrysMod {
66
template <typename SubModuleClass> class SubModule : public ModuleBase {
77
public:
8-
static inline ModuleFactory CreateSubModuleFactory() {
8+
static inline SubModuleFactory CreateSubModuleFactory() {
99
return [] { return std::make_shared<SubModuleClass>(); };
1010
}
1111

goomod/premake5.lua

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
local current_dir = _SCRIPT_DIR
22

33
function IncludeGOOMod()
4+
local refcount = IncludePackage("goomod")
5+
6+
local _project = project()
7+
48
externalincludedirs(current_dir .. "/include")
5-
files({
6-
current_dir .. "/include/GarrysMod/*.hpp",
7-
current_dir .. "/source/*.cpp"
8-
})
9-
vpaths({
10-
["Header files/goomod/*"] = current_dir .. "/include/GarrysMod/*.hpp",
11-
["Source files/goomod/*"] = current_dir .. "/source/*.cpp"
12-
})
9+
-- GCC requires manual ordering of links, where libraries on the left depend on libraries on the right
10+
-- Since goomod depends on helpers, the latter must appear after
11+
links({"goomod", "helpers"})
12+
13+
if refcount == 1 then
14+
dofile(current_dir .. "/premake5_create_project.lua")
15+
project(_project.name)
16+
end
1317
end

goomod/premake5_create_project.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
group("garrysmod_common")
2+
project("goomod")
3+
kind("StaticLib")
4+
location("../projects/" .. os.target() .. "/" .. _ACTION)
5+
targetdir("%{prj.location}/%{cfg.architecture}/%{cfg.buildcfg}")
6+
debugdir("%{prj.location}/%{cfg.architecture}/%{cfg.buildcfg}")
7+
objdir("!%{prj.location}/%{cfg.architecture}/%{cfg.buildcfg}/intermediate/%{prj.name}")
8+
defines({"IS_SERVERSIDE=true", "GMOD_ALLOW_LIGHTUSERDATA"})
9+
includedirs({"../include", "include", "include/GarrysMod"})
10+
files({
11+
"../include/Platform.hpp",
12+
"include/**.h",
13+
"include/**.hpp",
14+
"source/*.cpp"
15+
})
16+
vpaths({
17+
["Header files/*"] = {"../include/**.hpp", "include/**.h", "include/**.hpp"},
18+
["Source files/*"] = "source/*.cpp"
19+
})
20+
21+
IncludeHelpers()

goomod/source/ModuleExports.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
#include "GarrysMod/Lua/Interface.h"
22
#include "GarrysMod/Lua/LuaInterface.h"
3-
#include "GarrysMod/ModuleBase.hpp"
3+
#include "GarrysMod/ModuleHelper.hpp"
44

55
#include "lua.hpp"
66

77
namespace GarrysMod {
88
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
9-
static std::shared_ptr<ModuleBase> s_module_singleton;
9+
static std::shared_ptr<ModuleHelper> s_module_singleton;
1010

1111
static int Cleanup(Lua::ILuaBase *LUA,
1212
const bool error_on_invalid_module = false) {
13+
const auto module_singleton = s_module_singleton;
14+
1315
auto *lua_interface = dynamic_cast<Lua::ILuaInterface *>(LUA);
14-
if (!s_module_singleton) {
16+
if (!module_singleton) {
1517
if (error_on_invalid_module) {
16-
lua_interface->ErrorNoHalt("[%s] module isn't instantiated\n",
17-
GMOD_MODULE_NAME);
18+
lua_interface->ErrorNoHalt("[unknown] module isn't instantiated\n");
1819
}
1920

2021
return 0;
2122
}
2223

2324
int result = 0;
2425
try {
25-
result = s_module_singleton->Deinitialize(LUA);
26+
result = module_singleton->Deinitialize(LUA);
2627
} catch (const std::exception &e) {
2728
lua_interface->ErrorNoHalt("[%s] deinitialization failure: %s\n",
28-
GMOD_MODULE_NAME, e.what());
29+
module_singleton->ModuleName().c_str(), e.what());
2930
}
3031

3132
s_module_singleton.reset();

goomod/source/ModuleHelper.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ int ModuleHelper::Deinitialize(Lua::ILuaBase *LUA) {
3232
return 0;
3333
}
3434

35-
void ModuleHelper::AddSubModuleFactory(
36-
std::function<std::shared_ptr<ModuleBase>()> &&factory) {
35+
const std::string& ModuleHelper::ModuleName() const {
36+
return m_module_name;
37+
}
38+
39+
ModuleHelper::ModuleHelper(const std::string& module_name) : m_module_name(module_name) {}
40+
41+
void ModuleHelper::AddSubModuleFactory(SubModuleFactory&& factory) {
3742
m_submodule_factories.emplace_back(std::move(factory));
3843
}
3944

@@ -71,7 +76,7 @@ bool ModuleHelper::EnableThink(Lua::ILuaBase *LUA,
7176
}
7277

7378
LUA->PushString("Think");
74-
LUA->PushString("module." GMOD_MODULE_NAME ".Think");
79+
LUA->PushFormattedString("module.%s.Think", ModuleName().c_str());
7580
PushMemberFunction(LUA, wrapper, function_reference);
7681

7782
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
@@ -118,7 +123,7 @@ bool ModuleHelper::DisableThink(Lua::ILuaBase *LUA) const {
118123
}
119124

120125
LUA->PushString("Think");
121-
LUA->PushString("module." GMOD_MODULE_NAME ".Think");
126+
LUA->PushFormattedString("module.%s.Think", ModuleName().c_str());
122127

123128
if (LUA->PCall(2, 0, -4) != 0) {
124129
dynamic_cast<Lua::ILuaInterface *>(LUA)->ErrorNoHalt("\n%s\n\n",

0 commit comments

Comments
 (0)