MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / LoadDefaultDLSFile

Function LoadDefaultDLSFile

src/music/dmusic.cpp:842–1055  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

840}
841
842static std::optional<std::string_view> LoadDefaultDLSFile(std::optional<std::string_view> user_dls)
843{
844 DMUS_PORTCAPS caps{};
845 caps.dwSize = sizeof(DMUS_PORTCAPS);
846 _port->GetCaps(&caps);
847
848 /* Nothing to unless it is a synth with instrument download that doesn't come with GM voices by default. */
849 if ((caps.dwFlags & (DMUS_PC_DLS | DMUS_PC_DLS2)) != 0 && (caps.dwFlags & DMUS_PC_GMINHARDWARE) == 0) {
850 DLSFile dls_file;
851
852 if (!user_dls.has_value()) {
853 /* Try loading the default GM DLS file stored in the registry. */
854 HKEY hkDM;
855 if (SUCCEEDED(RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\DirectMusic", 0, KEY_READ, &hkDM))) {
856 wchar_t dls_path[MAX_PATH];
857 DWORD buf_size = sizeof(dls_path); // Buffer size as to be given in bytes!
858 if (SUCCEEDED(RegQueryValueEx(hkDM, L"GMFilePath", nullptr, nullptr, (LPBYTE)dls_path, &buf_size))) {
859 wchar_t expand_path[MAX_PATH * 2];
860 ExpandEnvironmentStrings(dls_path, expand_path, static_cast<DWORD>(std::size(expand_path)));
861 if (!dls_file.LoadFile(FS2OTTD(expand_path))) Debug(driver, 1, "Failed to load default GM DLS file from registry");
862 }
863 RegCloseKey(hkDM);
864 }
865
866 /* If we couldn't load the file from the registry, try again at the default install path of the GM DLS file. */
867 if (dls_file.instruments.empty()) {
868 static const wchar_t *DLS_GM_FILE = L"%windir%\\System32\\drivers\\gm.dls";
869 wchar_t path[MAX_PATH];
870 ExpandEnvironmentStrings(DLS_GM_FILE, path, static_cast<DWORD>(std::size(path)));
871
872 if (!dls_file.LoadFile(FS2OTTD(path))) return "Can't load GM DLS collection";
873 }
874 } else {
875 if (!dls_file.LoadFile(*user_dls)) return "Can't load GM DLS collection";
876 }
877
878 /* Get download port and allocate download IDs. */
879 IDirectMusicPortDownload *download_port = nullptr;
880 if (FAILED(_port->QueryInterface(IID_IDirectMusicPortDownload, (LPVOID *)&download_port))) return "Can't get download port";
881
882 DWORD dlid_wave = 0, dlid_inst = 0;
883 if (FAILED(download_port->GetDLId(&dlid_wave, (DWORD)dls_file.waves.size())) || FAILED(download_port->GetDLId(&dlid_inst, (DWORD)dls_file.instruments.size()))) {
884 download_port->Release();
885 return "Can't get enough DLS ids";
886 }
887
888 DWORD dwAppend = 0;
889 download_port->GetAppend(&dwAppend);
890
891 /* Download wave data. */
892 for (DWORD i = 0; i < dls_file.waves.size(); i++) {
893 IDirectMusicDownload *dl_wave = nullptr;
894 if (FAILED(download_port->AllocateBuffer((DWORD)(sizeof(WAVE_DOWNLOAD) + dwAppend * dls_file.waves[i].fmt.wf.nBlockAlign + dls_file.waves[i].data.size()), &dl_wave))) {
895 download_port->Release();
896 return "Can't allocate wave download buffer";
897 }
898
899 WAVE_DOWNLOAD *wave;

Callers 1

StartMethod · 0.85

Calls 11

DownloadArticulationDataFunction · 0.85
GetBufferMethod · 0.80
push_backMethod · 0.80
sizeFunction · 0.50
FS2OTTDFunction · 0.50
LoadFileMethod · 0.45
emptyMethod · 0.45
sizeMethod · 0.45
ReleaseMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected