From 41964e084dbb36376d19bc6ef88e705c8dc4d71d Mon Sep 17 00:00:00 2001
From: mazmazz <mar.marcoz@outlook.com>
Date: Tue, 18 Sep 2018 10:32:38 -0400
Subject: [PATCH] MusicPlus core: Lua separation fixes

---
 src/lua_baselib.c | 38 +++++++++++++++++++++-----------------
 src/lua_hook.h    |  1 -
 src/lua_hooklib.c |  1 -
 src/s_sound.c     |  2 +-
 4 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/src/lua_baselib.c b/src/lua_baselib.c
index 08f6e3051a..f34e65421b 100644
--- a/src/lua_baselib.c
+++ b/src/lua_baselib.c
@@ -954,14 +954,9 @@ static int lib_pRestoreMusic(lua_State *L)
 	INLEVEL
 	if (!player)
 		return LUA_ErrInvalid(L, "player_t");
-	if (!player || P_IsLocalPlayer(player))
-	{
+	else if (P_IsLocalPlayer(player))
 		P_RestoreMusic(player);
-		lua_pushboolean(L, true);
-	}
-	else
-		lua_pushnil(L);
-	return 1;
+	return 0;
 }
 
 static int lib_pSpawnShieldOrb(lua_State *L)
@@ -2241,13 +2236,8 @@ static int lib_sChangeMusic(lua_State *L)
 	fadeinms = (UINT32)luaL_optinteger(L, 7, 0);
 
 	if (!player || P_IsLocalPlayer(player))
-	{
 		S_ChangeMusicEx(music_name, music_flags, looping, position, prefadems, fadeinms);
-		lua_pushboolean(L, true);
-	}
-	else
-		lua_pushnil(L);
-	return 1;
+	return 0;
 }
 
 static int lib_sSpeedMusic(lua_State *L)
@@ -2263,10 +2253,23 @@ static int lib_sSpeedMusic(lua_State *L)
 			return LUA_ErrInvalid(L, "player_t");
 	}
 	if (!player || P_IsLocalPlayer(player))
-		lua_pushboolean(L, S_SpeedMusic(speed));
-	else
-		lua_pushnil(L);
-	return 1;
+		S_SpeedMusic(speed);
+	return 0;
+}
+
+static int lib_sStopMusic(lua_State *L)
+{
+	player_t *player = NULL;
+	NOHUD
+	if (!lua_isnone(L, 1) && lua_isuserdata(L, 1))
+	{
+		player = *((player_t **)luaL_checkudata(L, 1, META_PLAYER));
+		if (!player)
+			return LUA_ErrInvalid(L, "player_t");
+	}
+	if (!player || P_IsLocalPlayer(player))
+		S_StopMusic();
+	return 0;
 }
 
 static int lib_sOriginPlaying(lua_State *L)
@@ -2646,6 +2649,7 @@ static luaL_Reg lib[] = {
 	{"S_StopSound",lib_sStopSound},
 	{"S_ChangeMusic",lib_sChangeMusic},
 	{"S_SpeedMusic",lib_sSpeedMusic},
+	{"S_StopMusic",lib_sStopMusic},
 	{"S_OriginPlaying",lib_sOriginPlaying},
 	{"S_IdPlaying",lib_sIdPlaying},
 	{"S_SoundPlaying",lib_sSoundPlaying},
diff --git a/src/lua_hook.h b/src/lua_hook.h
index fb793019b7..822edf79fe 100644
--- a/src/lua_hook.h
+++ b/src/lua_hook.h
@@ -48,7 +48,6 @@ enum hook {
 	hook_MobjMoveBlocked,
 	hook_MapThingSpawn,
 	hook_FollowMobj,
-	hook_MusicChange,
 
 	hook_MAX // last hook
 };
diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c
index 55bcb76c0b..53886f7bad 100644
--- a/src/lua_hooklib.c
+++ b/src/lua_hooklib.c
@@ -59,7 +59,6 @@ const char *const hookNames[hook_MAX+1] = {
 	"MobjMoveBlocked",
 	"MapThingSpawn",
 	"FollowMobj",
-	"MusicChange",
 	NULL
 };
 
diff --git a/src/s_sound.c b/src/s_sound.c
index 1cb1d117d6..864af7165a 100644
--- a/src/s_sound.c
+++ b/src/s_sound.c
@@ -38,7 +38,7 @@ extern INT32 msg_id;
 #include "p_local.h" // camera info
 #include "fastcmp.h"
 
-#ifdef HAVE_BLUA
+#if defined(HAVE_BLUA) && defined(HAVE_LUA_MUSICPLUS)
 #include "lua_hook.h" // MusicChange hook
 #endif
 
-- 
GitLab