Skip to content

Commit c580a31

Browse files
authored
Improve config loading (notkoen#26)
notkoen#25
1 parent ba4a0b2 commit c580a31

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

addons/sourcemod/scripting/MusicName.sp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public Plugin myinfo =
1010
name = "Music Names",
1111
author = "koen, .Rushaway",
1212
description = "Display the name of music in chat",
13-
version = "1.3",
13+
version = "1.3.2",
1414
};
1515

1616
char g_sCurrentSong[256];
@@ -103,6 +103,8 @@ public void OnMapEnd()
103103
{
104104
delete g_songNames;
105105
delete g_fLastPlayedTime;
106+
g_bConfigLoaded = false;
107+
g_sCurrentSong = "";
106108
}
107109

108110
public void OnClientDisconnected(int client)
@@ -191,7 +193,7 @@ public Action Command_ToggleNP(int client, int args)
191193
//----------------------------------------------------------------------------------------------------
192194
public Action Command_DumpMusic(int client, int args)
193195
{
194-
if (!g_bConfigLoaded)
196+
if (!EnsureMusicState())
195197
{
196198
CReplyToCommand(client, "%t %t", "Chat Prefix", "No Config");
197199
return Plugin_Handled;
@@ -237,7 +239,7 @@ public Action Command_ReloadMusicnames(int client, int args)
237239
//----------------------------------------------------------------------------------------------------
238240
public Action Hook_AmbientSound(char sample[PLATFORM_MAX_PATH], int &entity, float &volume, int &level, int &pitch, float pos[3], int &flags, float &delay)
239241
{
240-
if (!g_bConfigLoaded)
242+
if (!EnsureMusicState())
241243
return Plugin_Continue;
242244

243245
// Using "volume 0" to stop music triggers sound hook
@@ -362,6 +364,24 @@ stock void StringToLowerCase(char[] input)
362364
}
363365
}
364366

367+
//----------------------------------------------------------------------------------------------------
368+
// Purpose: Ensure runtime state is valid and config is loaded
369+
//----------------------------------------------------------------------------------------------------
370+
stock bool EnsureMusicState()
371+
{
372+
if (g_songNames == null)
373+
g_songNames = new StringMap();
374+
375+
if (g_fLastPlayedTime == null)
376+
g_fLastPlayedTime = new StringMap();
377+
378+
if (g_bConfigLoaded)
379+
return true;
380+
381+
LoadConfig();
382+
return g_bConfigLoaded;
383+
}
384+
365385
//----------------------------------------------------------------------------------------------------
366386
// Purpose: Toggle feature
367387
//----------------------------------------------------------------------------------------------------
@@ -374,4 +394,4 @@ stock void ToggleFeature(int client)
374394
CPrintToChat(client, "%t %t", "Chat Prefix", "Display Status", "Enabled");
375395
else
376396
CPrintToChat(client, "%t %t", "Chat Prefix", "Display Status", "Disabled");
377-
}
397+
}

0 commit comments

Comments
 (0)