ソースを参照

coreaudio-encoder: Refactor windows import

Refactors the windows import library section so that it uses a list, and
remove a slightly unnecessary macro in release_lib
jp9000 5 年 前
コミット
1b5a331c56
1 ファイル変更37 行追加29 行削除
  1. 37 29
      plugins/coreaudio-encoder/windows-imports.h

+ 37 - 29
plugins/coreaudio-encoder/windows-imports.h

@@ -361,49 +361,57 @@ static HMODULE audio_toolbox = NULL;
 
 static void release_lib(void)
 {
-#define RELEASE_LIB(x)          \
-	if (x) {                \
-		FreeLibrary(x); \
-		x = NULL;       \
+	if (audio_toolbox) {
+		FreeLibrary(audio_toolbox);
+		audio_toolbox = NULL;
 	}
-
-	RELEASE_LIB(audio_toolbox);
-#undef RELEASE_LIB
 }
 
-static bool load_lib(void)
+static bool load_from_shell_path(REFKNOWNFOLDERID rfid, const wchar_t *subpath)
 {
-	PWSTR common_path;
-	if (SHGetKnownFolderPath(FOLDERID_ProgramFilesCommon, 0, NULL,
-				 &common_path) != S_OK) {
-		CA_LOG(LOG_WARNING, "Could not retrieve common files path");
+	wchar_t *sh_path;
+	if (SHGetKnownFolderPath(rfid, 0, NULL, &sh_path) != S_OK) {
+		CA_LOG(LOG_WARNING, "Could not retrieve shell path");
 		return false;
 	}
 
-	struct dstr path = {0};
-	dstr_printf(&path, "%S\\Apple\\Apple Application Support", common_path);
-	CoTaskMemFree(common_path);
+	wchar_t path[MAX_PATH];
+	_snwprintf(path, MAX_PATH, L"%s\\%s\\%s", sh_path, subpath,
+		   L"CoreAudioToolbox.dll");
+	CoTaskMemFree(sh_path);
 
-	wchar_t *w_path = dstr_to_wcs(&path);
-	dstr_free(&path);
+	audio_toolbox = LoadLibraryW(path);
+	return !!audio_toolbox;
+}
+
+static bool load_lib(void)
+{
+	/* -------------------------------------------- */
+	/* attempt to load from path                    */
 
-	SetDllDirectory(w_path);
-	bfree(w_path);
+	audio_toolbox = LoadLibraryW(L"CoreAudioToolbox.dll");
+	if (!!audio_toolbox)
+		return true;
 
-#define LOAD_LIB(x, n)            \
-	x = LoadLibrary(TEXT(n)); \
-	if (!x)                   \
-		CA_LOG(LOG_DEBUG, "Failed loading library '" n "'");
+	/* -------------------------------------------- */
+	/* attempt to load from known install locations */
 
-	LOAD_LIB(audio_toolbox, "CoreAudioToolbox.dll");
-#undef LOAD_LIB
+	struct path_list_t {
+		REFKNOWNFOLDERID rfid;
+		const wchar_t *subpath;
+	};
 
-	SetDllDirectory(NULL);
+	path_list_t path_list[] = {
+		{FOLDERID_ProgramFilesCommon,
+		 L"Apple\\Apple Application Support"},
+	};
 
-	if (audio_toolbox)
-		return true;
+	for (auto &val : path_list) {
+		if (load_from_shell_path(val.rfid, val.subpath)) {
+			return true;
+		}
+	}
 
-	release_lib();
 	return false;
 }