NoJobEngine 2.0.7
Loading...
Searching...
No Matches
ProjectScriptManager.cpp
Go to the documentation of this file.
4
5#ifdef _WIN32
6#define NOMINMAX
7#include <Windows.h>
8#endif
9
10#include <system_error>
11#include <utility>
12
13namespace NoJob
14{
15 namespace
16 {
17#ifdef _WIN32
18 HMODULE s_Module = nullptr;
19#endif
20 std::vector<std::string> s_Names;
21 std::vector<BTCallbackMetadata> s_BTCallbackCatalog;
22 thread_local std::vector<BTCallbackMetadata>* s_CollectingBTCallbacks = nullptr;
23
24 void CollectBTCallback(const char* name, int isCondition)
25 {
26 if (s_CollectingBTCallbacks && name && *name)
27 s_CollectingBTCallbacks->push_back({name, isCondition != 0});
28 }
30
31 void Log(std::string message)
32 {
33 if (s_Log) s_Log(std::move(message));
34 }
35 }
36
38 {
39 s_Log = std::move(logger);
40 }
41
42 void ProjectScriptManager::RegisterScript(ScriptDefinition definition)
43 {
44 s_Names.push_back(definition.Name);
45 ScriptRegistry::Register(std::move(definition));
46 }
47
49 {
50 for (const auto& name : s_Names)
52 s_Names.clear();
53 s_BTCallbackCatalog.clear();
54
55#ifdef _WIN32
56 if (s_Module)
57 {
58 FreeLibrary(s_Module);
59 s_Module = nullptr;
60 }
61#endif
62 }
63
64 bool ProjectScriptManager::Load(const std::filesystem::path& projectRoot)
65 {
66 Unload();
67#ifndef _WIN32
68 (void)projectRoot;
69 Log("[Scripts] Project script DLL loading is currently implemented for Windows.");
70 return false;
71#else
72 std::filesystem::path dll;
73 const auto outputDirectory = projectRoot / "out" / "ProjectScripts";
74 std::error_code ec;
75 std::filesystem::file_time_type newestTime{};
76
77 if (std::filesystem::exists(outputDirectory, ec))
78 {
79 for (auto it = std::filesystem::recursive_directory_iterator(
80 outputDirectory,
81 std::filesystem::directory_options::skip_permission_denied, ec);
82 !ec && it != std::filesystem::recursive_directory_iterator(); ++it)
83 {
84 if (!it->is_regular_file(ec)) continue;
85 const auto candidate = it->path();
86 if (candidate.extension() != ".dll") continue;
87 const std::string stem = candidate.stem().string();
88 if (stem.rfind("NoJobProjectScripts_", 0) != 0) continue;
89
90 const auto writeTime = std::filesystem::last_write_time(candidate, ec);
91 if (ec) { ec.clear(); continue; }
92 if (dll.empty() || writeTime > newestTime)
93 {
94 dll = candidate;
95 newestTime = writeTime;
96 }
97 }
98 }
99
100 if (dll.empty())
101 {
102 Log("[Scripts] Versioned ProjectScripts DLL not found under: " +
103 outputDirectory.string());
104 return false;
105 }
106
107 Log("[Scripts] Loading versioned DLL: " + dll.string());
108 s_Module = LoadLibraryW(dll.wstring().c_str());
109 if (!s_Module)
110 {
111 Log("[Scripts] LoadLibrary failed (Win32 error " +
112 std::to_string(GetLastError()) + ").");
113 return false;
114 }
115
116 using RegisterProjectScriptFn = void(*)(void(*)(ScriptDefinition));
117 const auto scripts = AssetManager::GetAssetsDirectory() / "Scripts";
118 if (std::filesystem::exists(scripts, ec))
119 {
120 for (const auto& entry : std::filesystem::directory_iterator(scripts, ec))
121 {
122 if (entry.path().extension() != ".cpp") continue;
123 const auto symbol = "NoJobRegister_" + entry.path().stem().string();
124 auto fn = reinterpret_cast<RegisterProjectScriptFn>(
125 GetProcAddress(s_Module, symbol.c_str()));
126 if (fn) fn(&ProjectScriptManager::RegisterScript);
127
128 // C ABI only: copy names while the DLL is loaded. No STL
129 // containers, callbacks or script instances cross this boundary.
130 using EnumerateBTMetadataFn = void(*)(void(*)(const char*, int));
131 const auto metadataSymbol = "NoJobBTMetadata_" + entry.path().stem().string();
132 auto metadataFn = reinterpret_cast<EnumerateBTMetadataFn>(
133 GetProcAddress(s_Module, metadataSymbol.c_str()));
134 if (metadataFn)
135 {
136 s_CollectingBTCallbacks = &s_BTCallbackCatalog;
137 metadataFn(&CollectBTCallback);
138 s_CollectingBTCallbacks = nullptr;
139 }
140 }
141 }
142
143 Log("[Scripts] Loaded " + std::to_string(s_Names.size()) + " project script(s).");
144 return true;
145#endif
146 }
147
149 {
150 return s_BTCallbackCatalog;
151 }
152
154 {
155#ifdef _WIN32
156 return s_Module != nullptr;
157#else
158 return false;
159#endif
160 }
161
163 {
164 return s_Names.size();
165 }
166}
AssetManager declarations for the Asset subsystem.
Project script discovery, compilation and runtime loading.
Registration and creation of native gameplay scripts.
static const std::filesystem::path & GetAssetsDirectory()
static bool Load(const std::filesystem::path &projectRoot)
static void SetLogger(LogFunction logger)
std::function< void(std::string)> LogFunction
static std::vector< BTCallbackMetadata > BehaviorTreeCallbackCatalog()
static void Register(ScriptDefinition definition)
static void Unregister(const std::string &name)