Skip to content

Commit

Permalink
Lazily load drivers in the router
Browse files Browse the repository at this point in the history
Rather than in DllMain, where it may not be safe to make calls to various
OpenAL drivers.
  • Loading branch information
kcat committed Dec 31, 2024
1 parent 00f5438 commit 6118ae7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
9 changes: 9 additions & 0 deletions router/alc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ namespace {

using namespace std::string_view_literals;

std::once_flag InitOnce;
void LoadDrivers() { std::call_once(InitOnce, []{ LoadDriverList(); }); }

struct FuncExportEntry {
const char *funcName;
void *address;
Expand Down Expand Up @@ -423,6 +426,8 @@ void InitCtxFuncs(DriverIface &iface)

ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *devicename) noexcept
{
LoadDrivers();

ALCdevice *device{nullptr};
std::optional<ALCuint> idx;

Expand Down Expand Up @@ -686,6 +691,8 @@ ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *e

ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *device, ALCenum param) noexcept
{
LoadDrivers();

if(device)
{
if(const auto idx = maybe_get(DeviceIfaceMap, device))
Expand Down Expand Up @@ -867,6 +874,8 @@ ALC_API void ALC_APIENTRY alcGetIntegerv(ALCdevice *device, ALCenum param, ALCsi
ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *devicename, ALCuint frequency,
ALCenum format, ALCsizei buffersize) noexcept
{
LoadDrivers();

ALCdevice *device{nullptr};
std::optional<ALCuint> idx;

Expand Down
21 changes: 9 additions & 12 deletions router/router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,12 @@ bool GetLoadedModuleDirectory(const WCHAR *name, std::wstring *moddir)
return !moddir->empty();
}

} // namespace

void LoadDriverList()
{
TRACE("Initializing router v0.1-{} {}", ALSOFT_GIT_COMMIT_HASH, ALSOFT_GIT_BRANCH);

if(auto list = al::getenv(L"ALROUTER_ACCEPT"))
{
std::wstring_view namelist{*list};
Expand Down Expand Up @@ -375,14 +379,12 @@ void LoadDriverList()
* directory, app's path, or system path (don't want to do duplicate
* searches, or increase the priority of the app or system path).
*/
if(!dll_path.empty() &&
(cwd_path.empty() || dll_path != cwd_path) &&
(proc_path.empty() || dll_path != proc_path) &&
(sys_path.empty() || dll_path != sys_path))
if(!dll_path.empty() && (cwd_path.empty() || dll_path != cwd_path)
&& (proc_path.empty() || dll_path != proc_path)
&& (sys_path.empty() || dll_path != sys_path))
SearchDrivers(dll_path);
if(!cwd_path.empty() &&
(proc_path.empty() || cwd_path != proc_path) &&
(sys_path.empty() || cwd_path != sys_path))
if(!cwd_path.empty() && (proc_path.empty() || cwd_path != proc_path)
&& (sys_path.empty() || cwd_path != sys_path))
SearchDrivers(cwd_path);
if(!proc_path.empty() && (sys_path.empty() || proc_path != sys_path))
SearchDrivers(proc_path);
Expand Down Expand Up @@ -410,8 +412,6 @@ void LoadDriverList()
std::swap(*DriverList.begin(), *(DriverList.begin()+1));
}

} // namespace

BOOL APIENTRY DllMain(HINSTANCE, DWORD reason, void*)
{
switch(reason)
Expand All @@ -437,9 +437,6 @@ BOOL APIENTRY DllMain(HINSTANCE, DWORD reason, void*)
else
LogLevel = static_cast<eLogLevel>(l);
}
TRACE("Initializing router v0.1-{} {}", ALSOFT_GIT_COMMIT_HASH, ALSOFT_GIT_BRANCH);
LoadDriverList();

break;

case DLL_THREAD_ATTACH:
Expand Down
3 changes: 3 additions & 0 deletions router/router.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,7 @@ extern gsl::owner<std::FILE*> LogFile;
} \
} while(0)


void LoadDriverList();

#endif /* ROUTER_ROUTER_H */

0 comments on commit 6118ae7

Please sign in to comment.