Browse Source

Changed source registration to global forward

It was really cumbersome to have to ask all of the plugins to watch for
library updates.  A global forward would be more efficient.
nosoop 8 years ago
parent
commit
06804dfa23

+ 5 - 9
scripting/include/round_end_music.inc

@@ -55,15 +55,6 @@ 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.
@@ -107,3 +98,8 @@ forward Action OnRoundEndMusicWillPlay(MusicEntry song);
  * Called when a song has been played (or playback is handled by another plugin).
  */
 forward void OnRoundEndMusicPlayed(MusicEntry song);
+
+/**
+ * Called when the Round End Music plugin is available and in need of songs.
+ */
+forward void OnRoundEndSongsRequested();

+ 2 - 14
scripting/round_end_music.sp

@@ -12,7 +12,7 @@
 #pragma newdecls required
 #include <round_end_music>
 
-#define PLUGIN_VERSION "0.5.0"
+#define PLUGIN_VERSION "0.6.0"
 public Plugin myinfo = {
     name = "[CSRD] Round End Music",
     author = "nosoop",
@@ -39,7 +39,7 @@ public void OnPluginStart() {
 	g_ActiveSongs = new ArrayList();
 	g_PlayedSongs = new ArrayList();
 	
-	g_RequestSongForward = CreateForward(ET_Ignore);
+	g_RequestSongForward = CreateGlobalForward("OnRoundEndSongsRequested", ET_Ignore);
 	
 	g_OnREMPlayedForward = CreateGlobalForward("OnRoundEndMusicWillPlay", ET_Event, Param_Cell);
 	g_OnREMPostPlayedForward = CreateGlobalForward("OnRoundEndMusicPlayed", ET_Ignore,
@@ -290,23 +290,11 @@ float GetBonusRoundTime() {
 public APLRes AskPluginLoad2(Handle self, bool late, char[] error, int err_max) {
 	RegPluginLibrary("round-end-music");
 	
-	CreateNative("REM_RegisterSource", Native_RegisterSource);
 	CreateNative("REM_AddSong", Native_AddSong);
 	CreateNative("REM_GetActiveSongCount", Native_GetActiveSongCount);
 	CreateNative("REM_SongWasRecentlyPlayed", Native_SongWasRecentlyPlayed);
 }
 
-public int Native_RegisterSource(Handle hPlugin, int nArgs) {
-	Function callback = GetNativeFunction(1);
-	
-	// preemptive removal because it could be forwarded multiple times?
-	for (int i = 0; i < GetForwardFunctionCount(g_RequestSongForward); i++) {
-		RemoveFromForward(g_RequestSongForward, hPlugin, callback);
-	}
-	AddToForward(g_RequestSongForward, hPlugin, callback);
-	return 1;
-}
-
 public int Native_AddSong(Handle hPlugin, int nArgs) {
 	if (g_bQueueLocked) {
 		ThrowNativeError(1, "Queue is currently locked -- are you calling REM_AddSong outside "

+ 3 - 18
scripting/round_end_music_sample_provider.sp

@@ -9,7 +9,7 @@
 #pragma newdecls required
 #include <round_end_music>
 
-#define PLUGIN_VERSION "0.1.3"
+#define PLUGIN_VERSION "0.1.4"
 public Plugin myinfo = {
     name = "Round End Music: Sample Source and Handler",
     author = "nosoop",
@@ -30,7 +30,8 @@ char SONGS[][][] = {
 	{ "Throwback Galaxy", "Super Mario Galaxy 2", "nomnomnomsongs/throwback.mp3" },
 };
 
-public void HardcodedArrayMusicSource() {
+public void OnRoundEndSongsRequested() {
+	// Load song data from array
 	for (int i = 0; i < sizeof(SONGS); i++) {
 		MusicEntry entry = GenerateSong(SONGS[i][0], SONGS[i][1], SONGS[i][2]);
 		
@@ -61,19 +62,3 @@ public void OnRoundEndMusicPlayed(MusicEntry song) {
 	PrintToServer("[rem-sample] Played %s (replay? %b)", filePath,
 			REM_SongWasRecentlyPlayed(song));
 }
-
-void OnRoundEndMusicAvailable() {
-	REM_RegisterSource(HardcodedArrayMusicSource);
-}
-
-public void OnAllPluginsLoaded() {
-	if (LibraryExists("round-end-music")) {
-		OnRoundEndMusicAvailable();
-	}
-}
-
-public void OnLibraryAdded(const char[] name) {
-	if (StrEqual(name, "round-end-music")) {
-		OnRoundEndMusicAvailable();
-	}
-}