Skip to content

Commit f88bf86

Browse files
committed
load comps starting with $ sooner and with RTLD_GLOBAL on linux
1 parent 8c24656 commit f88bf86

File tree

2 files changed

+100
-30
lines changed

2 files changed

+100
-30
lines changed

Server/Source/core_impl.hpp

Lines changed: 98 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,10 +1030,10 @@ class Core final : public ICore, public PlayerConnectEventHandler, public Consol
10301030
}
10311031
}
10321032

1033-
IComponent* loadComponent(const ghc::filesystem::path& path)
1033+
IComponent* loadComponent(const ghc::filesystem::path& path, bool highPriority = false)
10341034
{
10351035
printLn("Loading component %s", path.filename().u8string().c_str());
1036-
auto componentLib = LIBRARY_OPEN(path.u8string().c_str());
1036+
auto componentLib = highPriority ? LIBRARY_OPEN_GLOBAL(path.u8string().c_str()) : LIBRARY_OPEN(path.u8string().c_str());
10371037
if (componentLib == nullptr)
10381038
{
10391039
printLn("\tFailed to load component: %s.", utils::GetLastErrorAsString().c_str());
@@ -1083,27 +1083,69 @@ class Core final : public ICore, public PlayerConnectEventHandler, public Consol
10831083

10841084
auto componentsCfg = config.getStrings("components");
10851085
auto excludeCfg = config.getStrings("exclude");
1086+
1087+
Impl::DynamicArray<ghc::filesystem::path> highPriorityComponents;
1088+
Impl::DynamicArray<ghc::filesystem::path> normalComponents;
1089+
1090+
const auto shouldLoad = [&](const ghc::filesystem::path& p)
1091+
{
1092+
if (excludeCfg && !excludeCfg->empty())
1093+
{
1094+
ghc::filesystem::path rel = ghc::filesystem::relative(p, path);
1095+
rel.replace_extension();
1096+
// Is this in the "don't load" list?
1097+
const auto isExcluded = [rel = std::move(rel)](const String& exclude)
1098+
{
1099+
return ghc::filesystem::path(exclude) == rel;
1100+
};
1101+
if (std::find_if(excludeCfg->begin(), excludeCfg->end(), isExcluded)
1102+
!= excludeCfg->end())
1103+
{
1104+
return false;
1105+
}
1106+
}
1107+
return true;
1108+
};
1109+
10861110
if (!componentsCfg || componentsCfg->empty())
10871111
{
10881112
for (auto& de : ghc::filesystem::recursive_directory_iterator(path))
10891113
{
10901114
ghc::filesystem::path p = de.path();
1115+
if (p.filename().string().at(0) == '$')
1116+
{
1117+
highPriorityComponents.push_back(p);
1118+
}
1119+
else
1120+
{
1121+
normalComponents.push_back(p);
1122+
}
1123+
}
1124+
1125+
for (auto& p : highPriorityComponents)
1126+
{
10911127
if (p.extension() == LIBRARY_EXT)
10921128
{
1093-
if (excludeCfg && !excludeCfg->empty())
1129+
if (!shouldLoad(p))
10941130
{
1095-
ghc::filesystem::path rel = ghc::filesystem::relative(p, path);
1096-
rel.replace_extension();
1097-
// Is this in the "don't load" list?
1098-
const auto isExcluded = [rel = std::move(rel)](const String& exclude)
1099-
{
1100-
return ghc::filesystem::path(exclude) == rel;
1101-
};
1102-
if (std::find_if(excludeCfg->begin(), excludeCfg->end(), isExcluded)
1103-
!= excludeCfg->end())
1104-
{
1105-
continue;
1106-
}
1131+
continue;
1132+
}
1133+
1134+
IComponent* component = loadComponent(p, true);
1135+
if (component)
1136+
{
1137+
addComponent(component);
1138+
}
1139+
}
1140+
}
1141+
1142+
for (auto& p : normalComponents)
1143+
{
1144+
if (p.extension() == LIBRARY_EXT)
1145+
{
1146+
if (!shouldLoad(p))
1147+
{
1148+
continue;
11071149
}
11081150

11091151
IComponent* component = loadComponent(p);
@@ -1113,6 +1155,7 @@ class Core final : public ICore, public PlayerConnectEventHandler, public Consol
11131155
}
11141156
}
11151157
}
1158+
11161159
}
11171160
else
11181161
{
@@ -1124,35 +1167,60 @@ class Core final : public ICore, public PlayerConnectEventHandler, public Consol
11241167
file.replace_extension("");
11251168
}
11261169

1127-
if (excludeCfg && !excludeCfg->empty())
1170+
if (file.filename().string().at(0) == '$')
11281171
{
1129-
ghc::filesystem::path rel = ghc::filesystem::relative(file, path);
1130-
rel.replace_extension();
1131-
// Is this in the "don't load" list?
1132-
const auto isExcluded = [rel = std::move(rel)](const String& exclude)
1133-
{
1134-
return ghc::filesystem::path(exclude) == rel;
1135-
};
1136-
if (std::find_if(excludeCfg->begin(), excludeCfg->end(), isExcluded)
1137-
!= excludeCfg->end())
1172+
highPriorityComponents.push_back(file);
1173+
}
1174+
else
1175+
{
1176+
normalComponents.push_back(file);
1177+
}
1178+
}
1179+
1180+
for (auto& p : highPriorityComponents)
1181+
{
1182+
if (!shouldLoad(p))
1183+
{
1184+
continue;
1185+
}
1186+
1187+
// Now load it.
1188+
p.replace_extension(LIBRARY_EXT);
1189+
if (ghc::filesystem::exists(p))
1190+
{
1191+
IComponent* component = loadComponent(p, true);
1192+
if (component)
11381193
{
1139-
continue;
1194+
addComponent(component);
11401195
}
11411196
}
1197+
else
1198+
{
1199+
printLn("Loading component %s", p.filename().u8string().c_str());
1200+
printLn("\tCould not find component");
1201+
}
1202+
}
1203+
1204+
for (auto& p : normalComponents)
1205+
{
1206+
if (!shouldLoad(p))
1207+
{
1208+
continue;
1209+
}
11421210

11431211
// Now load it.
1144-
file.replace_extension(LIBRARY_EXT);
1145-
if (ghc::filesystem::exists(file))
1212+
p.replace_extension(LIBRARY_EXT);
1213+
if (ghc::filesystem::exists(p))
11461214
{
1147-
IComponent* component = loadComponent(file);
1215+
IComponent* component = loadComponent(p);
11481216
if (component)
11491217
{
11501218
addComponent(component);
11511219
}
11521220
}
11531221
else
11541222
{
1155-
printLn("Loading component %s", file.filename().u8string().c_str());
1223+
printLn("Loading component %s", p.filename().u8string().c_str());
11561224
printLn("\tCould not find component");
11571225
}
11581226
}

Server/Source/util.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct IUnknown;
2020
#include <timeapi.h>
2121
#define SET_TICKER_RESOLUTION(ms) timeBeginPeriod(ms)
2222
#define LIBRARY_OPEN(path) LoadLibrary(path)
23+
#define LIBRARY_OPEN_GLOBAL(path) LIBRARY_OPEN(path)
2324
#define LIBRARY_GET_ADDR GetProcAddress
2425
#define LIBRARY_FREE FreeLibrary
2526
static LARGE_INTEGER initialTime;
@@ -32,6 +33,7 @@ static LARGE_INTEGER yo;
3233
#include <unistd.h>
3334
#define SET_TICKER_RESOLUTION(ms)
3435
#define LIBRARY_OPEN(path) dlopen(path, RTLD_LAZY | RTLD_LOCAL)
36+
#define LIBRARY_OPEN_GLOBAL(path) dlopen(path, RTLD_LAZY | RTLD_GLOBAL)
3537
#define LIBRARY_GET_ADDR dlsym
3638
#define LIBRARY_FREE dlclose
3739
static timeval initialTime;

0 commit comments

Comments
 (0)