game-music-emu: Merge branch 'master' of bitbucket.org:mpyne/game-music-emu (f3828)

From ed4ed86f5bb71d313176e798d3f8559a1f1d2af9 Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Thu, 5 Oct 2023 14:33:32 +0300
Subject: [PATCH 1/3] fixes to unrar library use.

---
 CMakeLists.txt  |  6 +++++-
 gme/Spc_Emu.cpp | 17 ++++++++++++++---
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5cc62c4..aca1ea9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -81,7 +81,11 @@ endif()
 # libraries.  Ensure CMake looks only for static libs if we're doing a static
 # build.  See https://stackoverflow.com/a/44738756
 if(NOT BUILD_SHARED_LIBS)
+  if(MSVC)
+    set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib")
+  else()
     set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
+  endif()
 endif()
 
 # Check for GCC/Clang "visibility" support.
@@ -103,7 +107,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
     if (USE_GME_SPC)
         list(APPEND CMAKE_MODULE_PATH "${CMAKE_HOME_DIRECTORY}/cmake")
         find_package(UNRAR QUIET)
-        if (UNRAR_FOUND)
+        if (UNRAR_FOUND AND NOT WIN32)
             # POSIX Threading for unRAR
             set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
         endif()
diff --git a/gme/Spc_Emu.cpp b/gme/Spc_Emu.cpp
index 39c8a05..18f8de8 100644
--- a/gme/Spc_Emu.cpp
+++ b/gme/Spc_Emu.cpp
@@ -8,13 +8,23 @@
 #include <algorithm>
 
 #ifdef RARDLL
+#ifndef _WIN32
 #define PASCAL
 #define CALLBACK
+#define UINT unsigned int
 #define LONG long
 #define HANDLE void *
 #define LPARAM intptr_t
-#define UINT __attribute__((unused)) unsigned int
+#else
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include <windows.h>
+#endif
 #include <dll.hpp>
+#ifndef ERAR_SUCCESS
+#define ERAR_SUCCESS 0
+#endif
 #endif
 
 /* Copyright (C) 2004-2006 Shay Green. This module is free software; you
@@ -286,6 +296,7 @@ extern gme_type_t const gme_spc_type = &gme_spc_type_;
 #ifdef RARDLL
 static int CALLBACK call_rsn(UINT msg, LPARAM UserData, LPARAM P1, LPARAM P2)
 {
+	(void) msg;
 	byte **bp = (byte **)UserData;
 	unsigned char *addr = (unsigned char *)P1;
 	memcpy( *bp, addr, P2 );
@@ -314,7 +325,7 @@ struct Rsn_File : Spc_File
 		int count = 0;
 		unsigned biggest = 0;
 		blargg_vector<byte> temp;
-		HANDLE PASCAL rar = RAROpenArchive( &data );
+		HANDLE rar = RAROpenArchive( &data );
 		struct RARHeaderData head;
 		for ( ; RARReadHeader( rar, &head ) == ERAR_SUCCESS; count++ )
 		{
@@ -512,7 +523,7 @@ blargg_err_t Rsn_Emu::load_archive( const char* path )
 	// get the file count and unpacked size
 	long pos = 0;
 	int count = 0;
-	HANDLE PASCAL rar = RAROpenArchive( &data );
+	HANDLE rar = RAROpenArchive( &data );
 	struct RARHeaderData head;
 	for ( ; RARReadHeader( rar, &head ) == ERAR_SUCCESS; count++ )
 	{

From 99f8b1457bcf7fa6dc4d0354c4918ef341e4318c Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <sezeroz@gmail.com>
Date: Thu, 5 Oct 2023 14:37:02 +0300
Subject: [PATCH 2/3] cmake: make unrar-depending RSN an option

---
 CMakeLists.txt     | 4 ++++
 gme/CMakeLists.txt | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index aca1ea9..2615ad5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -46,6 +46,10 @@ if (NOT DEFINED USE_GME_SPC)
     SET(USE_GME_SPC 1 CACHE BOOL "Enable SNES SPC music emulation")
 endif()
 
+if (NOT DEFINED GME_UNRAR_RSN)
+    option(GME_UNRAR_RSN "Enable SPC RSN format (optional, requires UnRAR library)" ON)
+endif()
+
 if (NOT DEFINED GME_SPC_ISOLATED_ECHO_BUFFER)
     option(GME_SPC_ISOLATED_ECHO_BUFFER "Enable isolated echo buffer on SPC emulator to allow correct playing of \"dodgy\" SPC files made for various ROM hacks ran on ZSNES" OFF)
 endif()
diff --git a/gme/CMakeLists.txt b/gme/CMakeLists.txt
index 46a58bd..5050f04 100644
--- a/gme/CMakeLists.txt
+++ b/gme/CMakeLists.txt
@@ -181,7 +181,7 @@ else()
     message("ZLib library not found, disabling support for compressed formats such as VGZ")
 endif()
 
-if(USE_GME_SPC)
+if(USE_GME_SPC AND GME_UNRAR_RSN)
     if(UNRAR_FOUND)
         message(" ** unRAR library located, the RSN file format will be supported")
         target_compile_definitions(gme PRIVATE -DRARDLL)

From 8696670a4cc09a27156083b27e9b95084f103051 Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <sezeroz@gmail.com>
Date: Thu, 5 Oct 2023 17:55:00 +0300
Subject: [PATCH 3/3] better detection of unrar library header

---
 cmake/FindUNRAR.cmake | 12 +++++++++++-
 gme/CMakeLists.txt    |  5 +++++
 gme/Spc_Emu.cpp       |  8 ++++++--
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/cmake/FindUNRAR.cmake b/cmake/FindUNRAR.cmake
index ec37701..5eafdcc 100644
--- a/cmake/FindUNRAR.cmake
+++ b/cmake/FindUNRAR.cmake
@@ -21,8 +21,18 @@ set(UNRAR_NAMES_DEBUG unrar)
 
 # Try each search configuration.
 foreach(search ${_UNRAR_SEARCHES})
-  find_path(UNRAR_INCLUDE_DIR NAMES dll.hpp ${${search}} PATH_SUFFIXES include unrar)
+  find_path(UNRAR_INCLUDE_DIR NAMES unrar.h ${${search}} PATH_SUFFIXES include unrar)
 endforeach()
+if(UNRAR_INCLUDE_DIR)
+  set(RAR_HDR_UNRAR_H 1)
+else()
+  foreach(search ${_UNRAR_SEARCHES})
+    find_path(UNRAR_INCLUDE_DIR NAMES dll.hpp ${${search}} PATH_SUFFIXES include unrar)
+  endforeach()
+  if(UNRAR_INCLUDE_DIR)
+    set(RAR_HDR_DLL_HPP 1)
+  endif()
+endif()
 
 # Allow UNRAR_LIBRARY to be set manually, as the location of the unrar library
 if(NOT UNRAR_LIBRARY)
diff --git a/gme/CMakeLists.txt b/gme/CMakeLists.txt
index 5050f04..eb0bc97 100644
--- a/gme/CMakeLists.txt
+++ b/gme/CMakeLists.txt
@@ -185,6 +185,11 @@ if(USE_GME_SPC AND GME_UNRAR_RSN)
     if(UNRAR_FOUND)
         message(" ** unRAR library located, the RSN file format will be supported")
         target_compile_definitions(gme PRIVATE -DRARDLL)
+        if(RAR_HDR_UNRAR_H)
+            target_compile_definitions(gme PRIVATE -DRAR_HDR_UNRAR_H)
+        elseif(RAR_HDR_DLL_HPP)
+            target_compile_definitions(gme PRIVATE -DRAR_HDR_DLL_HPP)
+        endif()
         target_include_directories(gme PRIVATE ${UNRAR_INCLUDE_DIRS})
         target_link_libraries(gme ${UNRAR_LIBRARIES})
         # Is not to be installed though
diff --git a/gme/Spc_Emu.cpp b/gme/Spc_Emu.cpp
index 18f8de8..9f9d0a6 100644
--- a/gme/Spc_Emu.cpp
+++ b/gme/Spc_Emu.cpp
@@ -21,7 +21,11 @@
 #endif
 #include <windows.h>
 #endif
+#if defined RAR_HDR_UNRAR_H
+#include <unrar.h>
+#elif defined RAR_HDR_DLL_HPP
 #include <dll.hpp>
+#endif
 #ifndef ERAR_SUCCESS
 #define ERAR_SUCCESS 0
 #endif
@@ -345,7 +349,7 @@ struct Rsn_File : Spc_File
 		byte *bp;
 		data.OpenMode = RAR_OM_EXTRACT;
 		rar = RAROpenArchive( &data );
-		RARSetCallback( rar, call_rsn, (intptr_t)&bp );
+		RARSetCallback( rar, call_rsn, (LPARAM)&bp );
 		for ( count = 0, pos = 0; RARReadHeader( rar, &head ) == ERAR_SUCCESS; )
 		{
 			bp = &temp[0];
@@ -538,7 +542,7 @@ blargg_err_t Rsn_Emu::load_archive( const char* path )
 	byte *bp = &rsn[0];
 	data.OpenMode = RAR_OM_EXTRACT;
 	rar = RAROpenArchive( &data );
-	RARSetCallback( rar, call_rsn, (intptr_t)&bp );
+	RARSetCallback( rar, call_rsn, (LPARAM)&bp );
 	for ( count = 0, pos = 0; RARReadHeader( rar, &head ) == ERAR_SUCCESS; )
 	{
 		RARProcessFile( rar, RAR_TEST, 0, 0 );