From 4a17e9ed9bc24354500a0143ac0ad93bcc70438f Mon Sep 17 00:00:00 2001
From: mazmazz <mar.marcoz@outlook.com>
Date: Thu, 3 Jan 2019 20:52:09 -0500
Subject: [PATCH] Remove dynamic openmpt loading

---
 src/Makefile                         |   3 +-
 src/sdl/Makefile.cfg                 |   1 -
 src/sdl/Srb2SDL-vc10.vcxproj         |   2 -
 src/sdl/Srb2SDL-vc10.vcxproj.filters |   6 --
 src/sdl/load_libraries.c             | 112 ---------------------------
 src/sdl/load_libraries.h             |  52 -------------
 src/sdl/mixer_sound.c                |  66 +++++++---------
 7 files changed, 31 insertions(+), 211 deletions(-)
 delete mode 100644 src/sdl/load_libraries.c
 delete mode 100644 src/sdl/load_libraries.h

diff --git a/src/Makefile b/src/Makefile
index 585caa72b6..d5bb9c7c1b 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -364,8 +364,7 @@ CFLAGS+=$(LIBGME_CFLAGS)
 endif
 
 ifdef HAVE_OPENMPT
-# make libopenmpt optional
-OPTS+=-DHAVE_OPENMPT -DOPENMPT_DYNAMIC
+OPTS+=-DHAVE_OPENMPT
 
 LIBOPENMPT_PKGCONFIG?=libopenmpt
 LIBOPENMPT_CFLAGS?=$(shell $(PKG_CONFIG) $(LIBOPENMPT_PKGCONFIG) --cflags)
diff --git a/src/sdl/Makefile.cfg b/src/sdl/Makefile.cfg
index b4d4f83e56..58c4d0861f 100644
--- a/src/sdl/Makefile.cfg
+++ b/src/sdl/Makefile.cfg
@@ -79,7 +79,6 @@ ifdef NOMIXER
 	i_sound_o=$(OBJDIR)/sdl_sound.o
 else
 	i_sound_o=$(OBJDIR)/mixer_sound.o
-	OBJS+=$(OBJDIR)/load_libraries.o
 	OPTS+=-DHAVE_MIXER
 	SDL_LDFLAGS+=-lSDL2_mixer
 endif
diff --git a/src/sdl/Srb2SDL-vc10.vcxproj b/src/sdl/Srb2SDL-vc10.vcxproj
index cb08f727ee..c6cde6d99c 100644
--- a/src/sdl/Srb2SDL-vc10.vcxproj
+++ b/src/sdl/Srb2SDL-vc10.vcxproj
@@ -292,7 +292,6 @@
     <ClInclude Include="endtxt.h" />
     <ClInclude Include="hwsym_sdl.h" />
     <ClInclude Include="i_ttf.h" />
-    <ClInclude Include="load_libraries.h" />
     <ClInclude Include="ogl_sdl.h" />
     <ClInclude Include="sdlmain.h" />
   </ItemGroup>
@@ -467,7 +466,6 @@
     <ClCompile Include="i_system.c" />
     <ClCompile Include="i_ttf.c" />
     <ClCompile Include="i_video.c" />
-    <ClCompile Include="load_libraries.c" />
     <ClCompile Include="mixer_sound.c" />
     <ClCompile Include="ogl_sdl.c" />
     <ClCompile Include="SDL_main\SDL_windows_main.c" />
diff --git a/src/sdl/Srb2SDL-vc10.vcxproj.filters b/src/sdl/Srb2SDL-vc10.vcxproj.filters
index d9d31e3bb7..ac0b031779 100644
--- a/src/sdl/Srb2SDL-vc10.vcxproj.filters
+++ b/src/sdl/Srb2SDL-vc10.vcxproj.filters
@@ -447,9 +447,6 @@
     <ClInclude Include="sdlmain.h">
       <Filter>SDLApp</Filter>
     </ClInclude>
-    <ClInclude Include="load_libraries.h">
-      <Filter>SDLApp</Filter>
-    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <CustomBuild Include="..\tmap.nas">
@@ -888,9 +885,6 @@
     <ClCompile Include="SDL_main\SDL_windows_main.c">
       <Filter>SDLApp</Filter>
     </ClCompile>
-    <ClCompile Include="load_libraries.c">
-      <Filter>SDLApp</Filter>
-    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <Image Include="Srb2SDL.ico">
diff --git a/src/sdl/load_libraries.c b/src/sdl/load_libraries.c
deleted file mode 100644
index 904a73c11e..0000000000
--- a/src/sdl/load_libraries.c
+++ /dev/null
@@ -1,112 +0,0 @@
-///
-/// Dynamic Library Loading
-///
-
-#include "../doomdef.h"
-
-#ifdef _MSC_VER
-#pragma warning(disable : 4214 4244)
-#endif
-#include "SDL.h"
-#ifdef _MSC_VER
-#pragma warning(default : 4214 4244)
-#endif
-
-#include "load_libraries.h"
-
-///
-/// OpenMPT Loading
-///
-
-#ifdef HAVE_OPENMPT
-
-openmpt_loader openmpt = {
-	0, NULL,
-	NULL, NULL, NULL, // errors
-	NULL, NULL, // module loading
-	NULL, // audio callback
-	NULL, NULL, NULL, // playback settings
-	NULL, NULL, NULL, NULL, NULL // positioning
-};
-
-#ifdef OPENMPT_DYNAMIC
-#define FUNCTION_LOADER(NAME, FUNC, SIG) \
-    openmpt.NAME = (SIG) SDL_LoadFunction(openmpt.handle, #FUNC); \
-    if (openmpt.NAME == NULL) { SDL_UnloadObject(openmpt.handle); openmpt.handle = NULL; return; }
-#else
-#define FUNCTION_LOADER(NAME, FUNC, SIG) \
-    openmpt.NAME = FUNC;
-#endif
-
-void load_openmpt(void)
-{
-	if (openmpt.loaded)
-		return;
-
-#ifdef OPENMPT_DYNAMIC
-#if defined(_WIN32) || defined(_WIN64)
-	openmpt.handle = SDL_LoadObject("libopenmpt.dll");
-#else
-	openmpt.handle = SDL_LoadObject("libopenmpt.so");
-#endif
-	if (openmpt.handle == NULL)
-	{
-		CONS_Printf("libopenmpt not found, not loading.\n");
-		return;
-	}
-#endif
-
-	// errors
-	FUNCTION_LOADER(module_error_get_last, openmpt_module_error_get_last, int (*) ( openmpt_module * mod ))
-	FUNCTION_LOADER(error_string, openmpt_error_string, const char *(*) ( int error ))
-	FUNCTION_LOADER(get_string, openmpt_get_string, const char *(*) ( const char * key ))
-
-	// module loading
-	FUNCTION_LOADER(module_destroy, openmpt_module_destroy, void (*) ( openmpt_module * mod ))
-	FUNCTION_LOADER(module_create_from_memory2, openmpt_module_create_from_memory2, openmpt_module *(*) ( const void * filedata, size_t filesize, openmpt_log_func logfunc, void * loguser, openmpt_error_func errfunc, void * erruser, int * error, const char * * error_message, const openmpt_module_initial_ctl * ctls ))
-
-	// audio callback
-	FUNCTION_LOADER(module_read_interleaved_stereo, openmpt_module_read_interleaved_stereo, size_t (*) ( openmpt_module * mod, int32_t samplerate, size_t count, int16_t * interleaved_stereo ))
-
-	// playback settings
-	FUNCTION_LOADER(module_set_render_param, openmpt_module_set_render_param, int (*) ( openmpt_module * mod, int param, int32_t value ))
-	FUNCTION_LOADER(module_set_repeat_count, openmpt_module_set_repeat_count, int (*) ( openmpt_module * mod, int32_t repeat_count ))
-	FUNCTION_LOADER(module_ctl_set, openmpt_module_ctl_set, int (*) ( openmpt_module * mod, const char * ctl, const char * value ))
-
-	// positioning
-	FUNCTION_LOADER(module_get_duration_seconds, openmpt_module_get_duration_seconds, double (*) ( openmpt_module * mod ))
-	FUNCTION_LOADER(module_get_position_seconds, openmpt_module_get_position_seconds, double (*) ( openmpt_module * mod ))
-	FUNCTION_LOADER(module_set_position_seconds, openmpt_module_set_position_seconds, double (*) ( openmpt_module * mod, double seconds ))
-	FUNCTION_LOADER(module_get_num_subsongs, openmpt_module_get_num_subsongs, int32_t (*) ( openmpt_module * mod ))
-	FUNCTION_LOADER(module_select_subsong, openmpt_module_select_subsong, int (*) ( openmpt_module * mod, int32_t subsong ))
-
-#ifdef OPENMPT_DYNAMIC
-	// this will be unset if a function failed to load
-	if (openmpt.handle == NULL)
-	{
-		CONS_Printf("libopenmpt found but failed to load.\n");
-		return;
-	}
-#endif
-
-	CONS_Printf("libopenmpt version: %s\n", openmpt.get_string("library_version"));
-	CONS_Printf("libopenmpt build date: %s\n", openmpt.get_string("build"));
-
-	openmpt.loaded = 1;
-}
-
-void unload_openmpt(void)
-{
-#ifdef OPENMPT_DYNAMIC
-	if (openmpt.loaded)
-	{
-		SDL_UnloadObject(openmpt.handle);
-		openmpt.handle = NULL;
-		openmpt.loaded = 0;
-	}
-#endif
-}
-
-#undef FUNCTION_LOADER
-
-#endif
diff --git a/src/sdl/load_libraries.h b/src/sdl/load_libraries.h
deleted file mode 100644
index 9f097fd47d..0000000000
--- a/src/sdl/load_libraries.h
+++ /dev/null
@@ -1,52 +0,0 @@
-///
-/// Dynamic Library Loading
-///
-
-///
-/// OpenMPT Loading
-///
-
-#ifdef HAVE_OPENMPT
-
-#include "libopenmpt/libopenmpt.h"
-
-// Dynamic loading inspired by SDL Mixer
-// Why: It's hard to compile for Windows without MSVC dependency, see https://trac.videolan.org/vlc/ticket/13055
-// So let's not force that on the user, and they can download it if they want.
-//
-// ADD FUNCTIONS HERE AS YOU USE THEM!!!!!
-typedef struct {
-	int loaded;
-	void *handle;
-
-	// errors
-	int (*module_error_get_last) ( openmpt_module * mod );
-	const char *(*error_string) ( int error );
-	const char *(*get_string) ( const char * key );
-
-	// module loading
-	void (*module_destroy) ( openmpt_module * mod );
-	openmpt_module *(*module_create_from_memory2) ( const void * filedata, size_t filesize, openmpt_log_func logfunc, void * loguser, openmpt_error_func errfunc, void * erruser, int * error, const char * * error_message, const openmpt_module_initial_ctl * ctls );
-
-	// audio callback
-	size_t (*module_read_interleaved_stereo) ( openmpt_module * mod, int32_t samplerate, size_t count, int16_t * interleaved_stereo );
-
-	// playback settings
-	int (*module_set_render_param) ( openmpt_module * mod, int param, int32_t value );
-	int (*module_set_repeat_count) ( openmpt_module * mod, int32_t repeat_count );
-	int (*module_ctl_set) ( openmpt_module * mod, const char * ctl, const char * value );
-
-	// positioning
-	double (*module_get_duration_seconds) ( openmpt_module * mod );
-	double (*module_get_position_seconds) ( openmpt_module * mod );
-	double (*module_set_position_seconds) ( openmpt_module * mod, double seconds );
-	int32_t (*module_get_num_subsongs) ( openmpt_module * mod );
-	int (*module_select_subsong) ( openmpt_module * mod, int32_t subsong );
-} openmpt_loader;
-
-extern openmpt_loader openmpt;
-
-void load_openmpt(void);
-void unload_openmpt(void);
-
-#endif
diff --git a/src/sdl/mixer_sound.c b/src/sdl/mixer_sound.c
index 53b86831a1..1ed044a14b 100644
--- a/src/sdl/mixer_sound.c
+++ b/src/sdl/mixer_sound.c
@@ -20,8 +20,6 @@
 #include "../z_zone.h"
 #include "../byteptr.h"
 
-#include "load_libraries.h"
-
 #ifdef _MSC_VER
 #pragma warning(disable : 4214 4244)
 #endif
@@ -174,7 +172,8 @@ void I_StartupSound(void)
 	}
 
 #ifdef HAVE_OPENMPT
-	load_openmpt();
+	CONS_Printf("libopenmpt version: %s\n", openmpt_get_string("library_version"));
+	CONS_Printf("libopenmpt build date: %s\n", openmpt_get_string("build"));
 #endif
 
 	sound_started = true;
@@ -201,8 +200,7 @@ void I_ShutdownSound(void)
 #endif
 #ifdef HAVE_OPENMPT
 	if (mod)
-		openmpt.module_destroy(mod);
-	unload_openmpt();
+		openmpt_module_destroy(mod);
 #endif
 }
 
@@ -671,9 +669,9 @@ static void mix_openmpt(void *udata, Uint8 *stream, int len)
 
 	(void)udata;
 
-	openmpt.module_set_render_param(mod, OPENMPT_MODULE_RENDER_INTERPOLATIONFILTER_LENGTH, cv_modfilter.value);
-	openmpt.module_set_repeat_count(mod, -1); // Always repeat
-	openmpt.module_read_interleaved_stereo(mod, SAMPLERATE, BUFFERSIZE, (short *)stream);
+	openmpt_module_set_render_param(mod, OPENMPT_MODULE_RENDER_INTERPOLATIONFILTER_LENGTH, cv_modfilter.value);
+	openmpt_module_set_repeat_count(mod, -1); // Always repeat
+	openmpt_module_read_interleaved_stereo(mod, SAMPLERATE, BUFFERSIZE, (short *)stream);
 
 	// apply volume to stream
 	for (i = 0, p = (short *)stream; i < len/2; i++, p++)
@@ -762,7 +760,7 @@ boolean I_SetSongSpeed(float speed)
 	{
 		char modspd[16];
 		sprintf(modspd, "%g", speed);
-		openmpt.module_ctl_set(mod, "play.tempo_factor", modspd);
+		openmpt_module_ctl_set(mod, "play.tempo_factor", modspd);
 		return true;
 	}
 #else
@@ -811,7 +809,7 @@ UINT32 I_GetSongLength(void)
 #endif
 #ifdef HAVE_OPENMPT
 	if (mod)
-		return (UINT32)(openmpt.module_get_duration_seconds(mod) * 1000.);
+		return (UINT32)(openmpt_module_get_duration_seconds(mod) * 1000.);
 	else
 #endif
 	if (!music || I_SongType() == MU_MOD || I_SongType() == MU_MID)
@@ -904,7 +902,7 @@ boolean I_SetSongPosition(UINT32 position)
 	{
 		// This isn't 100% correct because we don't account for loop points because we can't get them.
 		// But if you seek past end of song, OpenMPT seeks to 0. So adjust the position anyway.
-		openmpt.module_set_position_seconds(mod, (double)(get_adjusted_position(position)/1000.0L)); // returns new position
+		openmpt_module_set_position_seconds(mod, (double)(get_adjusted_position(position)/1000.0L)); // returns new position
 		return true;
 	}
 	else
@@ -970,8 +968,8 @@ UINT32 I_GetSongPosition(void)
 	if (mod)
 		// This will be incorrect if we adjust for length because we can't get loop points.
 		// So return unadjusted. See note in SetMusicPosition: we adjust for that.
-		return (UINT32)(openmpt.module_get_position_seconds(mod)*1000.);
-		//return get_adjusted_position((UINT32)(openmpt.module_get_position_seconds(mod)*1000.));
+		return (UINT32)(openmpt_module_get_position_seconds(mod)*1000.);
+		//return get_adjusted_position((UINT32)(openmpt_module_get_position_seconds(mod)*1000.));
 	else
 #endif
 	if (!music || I_SongType() == MU_MID)
@@ -1141,27 +1139,23 @@ boolean I_LoadSong(char *data, size_t len)
 	{
 		case MUS_MODPLUG_UNUSED:
 		case MUS_MOD:
-			if (openmpt.loaded)
+			mod = openmpt_module_create_from_memory2(data, len, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
+			if (!mod)
 			{
-				mod = openmpt.module_create_from_memory2(data, len, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
-				if (!mod)
-				{
-					mod_err = openmpt.module_error_get_last(mod);
-					mod_err_str = openmpt.error_string(mod_err);
-					CONS_Alert(CONS_ERROR, "openmpt_module_create_from_memory2: %s\n", mod_err_str);
-					Mix_FreeMusic(music);
-					music = NULL;
-					return false;
-				}
-				else
-				{
-					Mix_FreeMusic(music);
-					music = NULL;
-					return true;
-				}
-				break;
+				mod_err = openmpt_module_error_get_last(mod);
+				mod_err_str = openmpt_error_string(mod_err);
+				CONS_Alert(CONS_ERROR, "openmpt_module_create_from_memory2: %s\n", mod_err_str);
+				Mix_FreeMusic(music);
+				music = NULL;
+				return false;
 			}
-			// else, fall through
+			else
+			{
+				Mix_FreeMusic(music);
+				music = NULL;
+				return true;
+			}
+			break;
 		case MUS_WAV:
 		case MUS_MID:
 		case MUS_OGG:
@@ -1274,7 +1268,7 @@ void I_UnloadSong(void)
 #ifdef HAVE_OPENMPT
 	if (mod)
 	{
-		openmpt.module_destroy(mod);
+		openmpt_module_destroy(mod);
 		mod = NULL;
 	}
 #endif
@@ -1300,7 +1294,7 @@ boolean I_PlaySong(boolean looping)
 #ifdef HAVE_OPENMPT
 	if (mod)
 	{
-		openmpt.module_select_subsong(mod, 0);
+		openmpt_module_select_subsong(mod, 0);
 		current_subsong = 0;
 		Mix_HookMusic(mix_openmpt, mod);
 		return true;
@@ -1444,9 +1438,9 @@ boolean I_SetSongTrack(int track)
 		if (current_subsong == track)
 			return false;
 		SDL_LockAudio();
-		if (track >= 0 && track < openmpt.module_get_num_subsongs(mod))
+		if (track >= 0 && track < openmpt_module_get_num_subsongs(mod))
 		{
-			openmpt.module_select_subsong(mod, track);
+			openmpt_module_select_subsong(mod, track);
 			current_subsong = track;
 			SDL_UnlockAudio();
 			return true;
-- 
GitLab