12 #include <string_view>
19 #include "opthelpers.h"
25 eLogLevel LogLevel
{eLogLevel::Error
};
26 gsl::owner
<std::FILE*> LogFile
;
30 std::vector
<std::wstring
> gAcceptList
;
31 std::vector
<std::wstring
> gRejectList
;
34 void AddModule(HMODULE module
, const std::wstring_view name
)
36 for(auto &drv
: DriverList
)
38 if(drv
->Module
== module
)
40 TRACE("Skipping already-loaded module %p\n", decltype(std::declval
<void*>()){module
});
46 TRACE("Skipping similarly-named module %.*ls\n", al::sizei(name
), name
.data());
51 if(!gAcceptList
.empty())
53 auto iter
= std::find_if(gAcceptList
.cbegin(), gAcceptList
.cend(),
54 [name
](const std::wstring_view accept
)
55 { return al::case_compare(name
, accept
) == 0; });
56 if(iter
== gAcceptList
.cend())
58 TRACE("%.*ls not found in ALROUTER_ACCEPT, skipping\n", al::sizei(name
), name
.data());
63 if(!gRejectList
.empty())
65 auto iter
= std::find_if(gRejectList
.cbegin(), gRejectList
.cend(),
66 [name
](const std::wstring_view accept
)
67 { return al::case_compare(name
, accept
) == 0; });
68 if(iter
!= gRejectList
.cend())
70 TRACE("%.*ls found in ALROUTER_REJECT, skipping\n", al::sizei(name
), name
.data());
76 DriverList
.emplace_back(std::make_unique
<DriverIface
>(name
, module
));
77 DriverIface
&newdrv
= *DriverList
.back();
79 /* Load required functions. */
81 #define LOAD_PROC(x) do { \
82 newdrv.x = reinterpret_cast<decltype(newdrv.x)>(reinterpret_cast<void*>( \
83 GetProcAddress(module, #x))); \
86 ERR("Failed to find entry point for %s in %.*ls\n", #x, al::sizei(name), name.data()); \
90 LOAD_PROC(alcCreateContext
);
91 LOAD_PROC(alcMakeContextCurrent
);
92 LOAD_PROC(alcProcessContext
);
93 LOAD_PROC(alcSuspendContext
);
94 LOAD_PROC(alcDestroyContext
);
95 LOAD_PROC(alcGetCurrentContext
);
96 LOAD_PROC(alcGetContextsDevice
);
97 LOAD_PROC(alcOpenDevice
);
98 LOAD_PROC(alcCloseDevice
);
99 LOAD_PROC(alcGetError
);
100 LOAD_PROC(alcIsExtensionPresent
);
101 LOAD_PROC(alcGetProcAddress
);
102 LOAD_PROC(alcGetEnumValue
);
103 LOAD_PROC(alcGetString
);
104 LOAD_PROC(alcGetIntegerv
);
105 LOAD_PROC(alcCaptureOpenDevice
);
106 LOAD_PROC(alcCaptureCloseDevice
);
107 LOAD_PROC(alcCaptureStart
);
108 LOAD_PROC(alcCaptureStop
);
109 LOAD_PROC(alcCaptureSamples
);
112 LOAD_PROC(alDisable
);
113 LOAD_PROC(alIsEnabled
);
114 LOAD_PROC(alGetString
);
115 LOAD_PROC(alGetBooleanv
);
116 LOAD_PROC(alGetIntegerv
);
117 LOAD_PROC(alGetFloatv
);
118 LOAD_PROC(alGetDoublev
);
119 LOAD_PROC(alGetBoolean
);
120 LOAD_PROC(alGetInteger
);
121 LOAD_PROC(alGetFloat
);
122 LOAD_PROC(alGetDouble
);
123 LOAD_PROC(alGetError
);
124 LOAD_PROC(alIsExtensionPresent
);
125 LOAD_PROC(alGetProcAddress
);
126 LOAD_PROC(alGetEnumValue
);
127 LOAD_PROC(alListenerf
);
128 LOAD_PROC(alListener3f
);
129 LOAD_PROC(alListenerfv
);
130 LOAD_PROC(alListeneri
);
131 LOAD_PROC(alListener3i
);
132 LOAD_PROC(alListeneriv
);
133 LOAD_PROC(alGetListenerf
);
134 LOAD_PROC(alGetListener3f
);
135 LOAD_PROC(alGetListenerfv
);
136 LOAD_PROC(alGetListeneri
);
137 LOAD_PROC(alGetListener3i
);
138 LOAD_PROC(alGetListeneriv
);
139 LOAD_PROC(alGenSources
);
140 LOAD_PROC(alDeleteSources
);
141 LOAD_PROC(alIsSource
);
142 LOAD_PROC(alSourcef
);
143 LOAD_PROC(alSource3f
);
144 LOAD_PROC(alSourcefv
);
145 LOAD_PROC(alSourcei
);
146 LOAD_PROC(alSource3i
);
147 LOAD_PROC(alSourceiv
);
148 LOAD_PROC(alGetSourcef
);
149 LOAD_PROC(alGetSource3f
);
150 LOAD_PROC(alGetSourcefv
);
151 LOAD_PROC(alGetSourcei
);
152 LOAD_PROC(alGetSource3i
);
153 LOAD_PROC(alGetSourceiv
);
154 LOAD_PROC(alSourcePlayv
);
155 LOAD_PROC(alSourceStopv
);
156 LOAD_PROC(alSourceRewindv
);
157 LOAD_PROC(alSourcePausev
);
158 LOAD_PROC(alSourcePlay
);
159 LOAD_PROC(alSourceStop
);
160 LOAD_PROC(alSourceRewind
);
161 LOAD_PROC(alSourcePause
);
162 LOAD_PROC(alSourceQueueBuffers
);
163 LOAD_PROC(alSourceUnqueueBuffers
);
164 LOAD_PROC(alGenBuffers
);
165 LOAD_PROC(alDeleteBuffers
);
166 LOAD_PROC(alIsBuffer
);
167 LOAD_PROC(alBufferData
);
168 LOAD_PROC(alDopplerFactor
);
169 LOAD_PROC(alDopplerVelocity
);
170 LOAD_PROC(alSpeedOfSound
);
171 LOAD_PROC(alDistanceModel
);
174 std::array
<ALCint
,2> alc_ver
{0, 0};
175 newdrv
.alcGetIntegerv(nullptr, ALC_MAJOR_VERSION
, 1, &alc_ver
[0]);
176 newdrv
.alcGetIntegerv(nullptr, ALC_MINOR_VERSION
, 1, &alc_ver
[1]);
177 if(newdrv
.alcGetError(nullptr) == ALC_NO_ERROR
)
178 newdrv
.ALCVer
= MAKE_ALC_VER(alc_ver
[0], alc_ver
[1]);
181 WARN("Failed to query ALC version for %.*ls, assuming 1.0\n", al::sizei(name
),
183 newdrv
.ALCVer
= MAKE_ALC_VER(1, 0);
187 #define LOAD_PROC(x) do { \
188 newdrv.x = reinterpret_cast<decltype(newdrv.x)>(reinterpret_cast<void*>( \
189 GetProcAddress(module, #x))); \
192 WARN("Failed to find optional entry point for %s in %.*ls\n", #x, al::sizei(name), \
196 LOAD_PROC(alBufferf
);
197 LOAD_PROC(alBuffer3f
);
198 LOAD_PROC(alBufferfv
);
199 LOAD_PROC(alBufferi
);
200 LOAD_PROC(alBuffer3i
);
201 LOAD_PROC(alBufferiv
);
202 LOAD_PROC(alGetBufferf
);
203 LOAD_PROC(alGetBuffer3f
);
204 LOAD_PROC(alGetBufferfv
);
205 LOAD_PROC(alGetBufferi
);
206 LOAD_PROC(alGetBuffer3i
);
207 LOAD_PROC(alGetBufferiv
);
210 #define LOAD_PROC(x) do { \
211 newdrv.x = reinterpret_cast<decltype(newdrv.x)>( \
212 newdrv.alcGetProcAddress(nullptr, #x)); \
215 ERR("Failed to find entry point for %s in %.*ls\n", #x, al::sizei(name), name.data()); \
219 if(newdrv
.alcIsExtensionPresent(nullptr, "ALC_EXT_thread_local_context"))
221 LOAD_PROC(alcSetThreadContext
);
222 LOAD_PROC(alcGetThreadContext
);
228 DriverList
.pop_back();
231 TRACE("Loaded module %p, %.*ls, ALC %d.%d\n", decltype(std::declval
<void*>()){module
},
232 al::sizei(name
), name
.data(), newdrv
.ALCVer
>>8, newdrv
.ALCVer
&255);
236 void SearchDrivers(const std::wstring_view path
)
238 TRACE("Searching for drivers in %.*ls...\n", al::sizei(path
), path
.data());
239 std::wstring srchPath
{path
};
240 srchPath
+= L
"\\*oal.dll";
242 WIN32_FIND_DATAW fdata
{};
243 HANDLE srchHdl
{FindFirstFileW(srchPath
.c_str(), &fdata
)};
244 if(srchHdl
== INVALID_HANDLE_VALUE
) return;
249 srchPath
+= std::data(fdata
.cFileName
);
250 TRACE("Found %ls\n", srchPath
.c_str());
252 HMODULE mod
{LoadLibraryW(srchPath
.c_str())};
254 WARN("Could not load %ls\n", srchPath
.c_str());
256 AddModule(mod
, std::data(fdata
.cFileName
));
257 } while(FindNextFileW(srchHdl
, &fdata
));
261 bool GetLoadedModuleDirectory(const WCHAR
*name
, std::wstring
*moddir
)
263 HMODULE module
{nullptr};
267 module
= GetModuleHandleW(name
);
268 if(!module
) return false;
271 moddir
->assign(256, '\0');
272 DWORD res
{GetModuleFileNameW(module
, moddir
->data(), static_cast<DWORD
>(moddir
->size()))};
273 if(res
>= moddir
->size())
276 moddir
->append(256, '\0');
277 res
= GetModuleFileNameW(module
, moddir
->data(), static_cast<DWORD
>(moddir
->size()));
278 } while(res
>= moddir
->size());
282 auto sep0
= moddir
->rfind('/');
283 auto sep1
= moddir
->rfind('\\');
284 if(sep0
< moddir
->size() && sep1
< moddir
->size())
285 moddir
->resize(std::max(sep0
, sep1
));
286 else if(sep0
< moddir
->size())
287 moddir
->resize(sep0
);
288 else if(sep1
< moddir
->size())
289 moddir
->resize(sep1
);
293 return !moddir
->empty();
296 void LoadDriverList()
298 if(auto list
= al::getenv(L
"ALROUTER_ACCEPT"))
300 std::wstring_view namelist
{*list
};
301 while(!namelist
.empty())
303 auto seppos
= namelist
.find(',');
305 gAcceptList
.emplace_back(namelist
.substr(0, seppos
));
306 if(seppos
< namelist
.size())
307 namelist
.remove_prefix(seppos
+1);
309 namelist
.remove_prefix(namelist
.size());
312 if(auto list
= al::getenv(L
"ALROUTER_REJECT"))
314 std::wstring_view namelist
{*list
};
315 while(!namelist
.empty())
317 auto seppos
= namelist
.find(',');
319 gRejectList
.emplace_back(namelist
.substr(0, seppos
));
320 if(seppos
< namelist
.size())
321 namelist
.remove_prefix(seppos
+1);
323 namelist
.remove_prefix(namelist
.size());
327 std::wstring dll_path
;
328 if(GetLoadedModuleDirectory(L
"OpenAL32.dll", &dll_path
))
329 TRACE("Got DLL path %ls\n", dll_path
.c_str());
331 std::wstring cwd_path
;
332 if(DWORD pathlen
{GetCurrentDirectoryW(0, nullptr)})
335 cwd_path
.resize(pathlen
);
336 pathlen
= GetCurrentDirectoryW(pathlen
, cwd_path
.data());
337 } while(pathlen
>= cwd_path
.size());
338 cwd_path
.resize(pathlen
);
340 if(!cwd_path
.empty() && (cwd_path
.back() == '\\' || cwd_path
.back() == '/'))
342 if(!cwd_path
.empty())
343 TRACE("Got current working directory %ls\n", cwd_path
.c_str());
345 std::wstring proc_path
;
346 if(GetLoadedModuleDirectory(nullptr, &proc_path
))
347 TRACE("Got proc path %ls\n", proc_path
.c_str());
349 std::wstring sys_path
;
350 if(UINT pathlen
{GetSystemDirectoryW(nullptr, 0)})
353 sys_path
.resize(pathlen
);
354 pathlen
= GetSystemDirectoryW(sys_path
.data(), pathlen
);
355 } while(pathlen
>= sys_path
.size());
356 sys_path
.resize(pathlen
);
358 if(!sys_path
.empty() && (sys_path
.back() == '\\' || sys_path
.back() == '/'))
360 if(!sys_path
.empty())
361 TRACE("Got system path %ls\n", sys_path
.c_str());
363 /* Don't search the DLL's path if it is the same as the current working
364 * directory, app's path, or system path (don't want to do duplicate
365 * searches, or increase the priority of the app or system path).
368 (!cwd_path
[0] || dll_path
!= cwd_path
) &&
369 (!proc_path
[0] || dll_path
!= proc_path
) &&
370 (!sys_path
[0] || dll_path
!= sys_path
))
371 SearchDrivers(dll_path
);
373 (!proc_path
[0] || cwd_path
!= proc_path
) &&
374 (!sys_path
[0] || cwd_path
!= sys_path
))
375 SearchDrivers(cwd_path
);
376 if(proc_path
[0] && (!sys_path
[0] || proc_path
!= sys_path
))
377 SearchDrivers(proc_path
);
379 SearchDrivers(sys_path
);
384 BOOL APIENTRY
DllMain(HINSTANCE
, DWORD reason
, void*)
388 case DLL_PROCESS_ATTACH
:
389 if(auto logfname
= al::getenv("ALROUTER_LOGFILE"))
391 gsl::owner
<std::FILE*> f
{fopen(logfname
->c_str(), "w")};
393 ERR("Could not open log file: %s\n", logfname
->c_str());
397 if(auto loglev
= al::getenv("ALROUTER_LOGLEVEL"))
400 long l
{strtol(loglev
->c_str(), &end
, 0)};
401 if(!end
|| *end
!= '\0')
402 ERR("Invalid log level value: %s\n", loglev
->c_str());
403 else if(l
< al::to_underlying(eLogLevel::None
)
404 || l
> al::to_underlying(eLogLevel::Trace
))
405 ERR("Log level out of range: %s\n", loglev
->c_str());
407 LogLevel
= static_cast<eLogLevel
>(l
);
409 TRACE("Initializing router v0.1-%s %s\n", ALSOFT_GIT_COMMIT_HASH
, ALSOFT_GIT_BRANCH
);
414 case DLL_THREAD_ATTACH
:
416 case DLL_THREAD_DETACH
:
419 case DLL_PROCESS_DETACH
: