Browse Source

Added global playback forwards

Plugins can now listen in on when the plugin is playing back a song.
nosoop 8 years ago
parent
commit
743c635f05
2 changed files with 75 additions and 15 deletions
  1. 37 1
      scripting/include/round_end_music.inc
  2. 38 14
      scripting/round_end_music.sp

+ 37 - 1
scripting/include/round_end_music.inc

@@ -43,8 +43,44 @@ methodmap MusicEntry < KeyValues {
 
 typedef OnSongsRequested = function void ();
 
+/**
+ * Register a function in this plugin as a music source.
+ * 
+ * @param callback	A function called whenever music is requested.
+ */
 native void REM_RegisterSource(OnSongsRequested callback);
 
+/**
+ * Adds a song to the Round End Music plugin's queued playlist.
+ * This function can only be called during REM_RegisterSource.
+ * 
+ * @param song		A song to add.  This handle is cloned, so you will also need to close your
+ * 					copy of the handle.
+ * 
+ * @return			True if adding the song was successful, false if not (playlist full or song
+ *					with same filepath is already queued).
+ */
 native bool REM_AddSong(MusicEntry song);
 
-native int REM_GetActiveSongCount();
+/**
+ * Returns the maximum number of songs that can be played on this map.  Has the correct number
+ * after OnAutoConfigsBuffered, but you'll probably only end up using it in an OnSongsRequested
+ * callback.
+ */
+native int REM_GetActiveSongCount();
+
+/**
+ * Called when a song is about to be played.
+ * 
+ * @param song		The KeyValues data for the song about to be played.
+ * 
+ * @return			Plugin_Continue for default song playback, Plugin_Handled if another plugin
+ * 					handled the playback, Plugin_Stop if the song should not be considered
+ * 					played.
+ */
+forward Action OnRoundEndMusicWillPlay(MusicEntry song);
+
+/**
+ * Called when a song has been played (or playback is handled by another plugin).
+ */
+forward void OnRoundEndMusicPlayed(MusicEntry song);

+ 38 - 14
scripting/round_end_music.sp

@@ -12,7 +12,7 @@
 #pragma newdecls required
 #include <round_end_music>
 
-#define PLUGIN_VERSION "0.2.0"
+#define PLUGIN_VERSION "0.3.0"
 public Plugin myinfo = {
     name = "[CSRD] Round End Music",
     author = "nosoop",
@@ -28,6 +28,8 @@ ConVar g_ConVarMaxActiveSongs, g_ConVarEnabled;
 
 Handle g_RequestSongForward;
 
+Handle g_OnREMPlayedForward, g_OnREMPostPlayedForward;
+
 int g_nMaxActiveSongs = 5;
 bool g_bRoundEndMusicActive;
 
@@ -38,6 +40,10 @@ public void OnPluginStart() {
 	
 	g_RequestSongForward = CreateForward(ET_Ignore);
 	
+	g_OnREMPlayedForward = CreateGlobalForward("OnRoundEndMusicWillPlay", ET_Event, Param_Cell);
+	g_OnREMPostPlayedForward = CreateGlobalForward("OnRoundEndMusicPlayed", ET_Ignore,
+			Param_Cell);
+	
 	HookEvent("teamplay_round_win", OnRoundEnd, EventHookMode_PostNoCopy);
 	
 	RegAdminCmd("sm_playsong", AdminCmd_PlaySong, ADMFLAG_ROOT);
@@ -209,26 +215,44 @@ void PlayRoundEndMusic() {
 }
 
 void EmitRoundEndMusic(MusicEntry song) {
-	char filePath[PLATFORM_MAX_PATH];
-	song.GetFilePath(filePath, sizeof(filePath));
-	
-	PrintToServer("mock play song %s", filePath);
-	EmitSoundToAll(filePath);
+	Action result = FireOnRoundEndMusicPlayedEvent(song);
 	
-	// TODO create new forward OnRoundEndMusicPlayed(MusicEntry song) ?
-	// - Plugin_Continue uses default handling
-	// - Plugin_Handled does not
-	// - Plugin_Stop doesn't either, but the implication is that the entire endround is canceled
-	//   (i.e., it shouldn't move it to the played list)
+	if (result == Plugin_Continue) {
+		char filePath[PLATFORM_MAX_PATH];
+		song.GetFilePath(filePath, sizeof(filePath));
+		
+		PrintToServer("mock play song %s", filePath);
+		EmitSoundToAll(filePath);
+	}
 	
-	if (g_PlayedSongs.FindValue(song) == -1) {
-		g_PlayedSongs.Push(song);
+	if (result != Plugin_Stop) {
+		if (g_PlayedSongs.FindValue(song) == -1) {
+			g_PlayedSongs.Push(song);
+		}
+
+		g_ActiveSongs.Erase(0);
+		
+		FireOnREMPlayedPostEvent(song);
 	}
+}
+
+Action FireOnRoundEndMusicPlayedEvent(MusicEntry song) {
+	Action result;
+	Call_StartForward(g_OnREMPlayedForward);
+	Call_PushCell(song);
+	Call_Finish(result);
 	
-	g_ActiveSongs.Erase(0);
+	return result;
+}
+
+void FireOnREMPlayedPostEvent(MusicEntry song) {
+	Call_StartForward(g_OnREMPostPlayedForward);
+	Call_PushCell(song);
+	Call_Finish();
 }
 
 public void OnRoundEnd(Event event, const char[] name, bool dontBroadcast) {
+	// TODO timer
 	PlayRoundEndMusic();
 }