Hey,
Seem to be having some strange issue with kv.ImportFromFile, It's returning true (Like the import was sucessfull but not actual importing from what i can see). When i do
kv.GotoFirstSubKey(true) it doesn't actually find a sub key to parse. This problem has got me a bit stumped and no idea why it's happening.
This is the keyvalues parsing code:
Code:
/*
* SourceMod Radio
*
* Adds a radio player to SourceMod that supports direct station links or streaming audio
*
* Coded by dubbeh - www.dubbeh.net
*
* Licensed under the GPLv3
*
*/
void GetRadioStationsFromFile()
{
KeyValues kv = null;
char szLocalPath[PLATFORM_MAX_PATH];
char szBuffer[512];
int iParentGenreID = 0;
int iParentGenreCount = 0;
int iChildGenreID = 0;
int iChildGenreCount = 0;
bool bHasChildren = false;
g_bStationsParsing = true;
kv = new KeyValues("Radio Stations");
BuildPath(Path_SM, szLocalPath, sizeof(szLocalPath), "configs/%s", g_szRadioStationsBaseFile);
LogMessage("[SM-RADIO] Importing KeyValues from file \"%s\"", szLocalPath);
if (kv.ImportFromFile(szLocalPath))
{
// This is incase we need to append Genre or Station ID's to the Key Values file
// Keeps the index's persistant across server reboots when admins add new stations or genres without one.
g_bWriteKVToDisk = false;
kv.GetString("Off Page", g_szRadioOffPage, sizeof(g_szRadioOffPage), "about:blank");
kv.GetString("Volume Wrapper", g_szVolumeWrapperURL, sizeof(g_szVolumeWrapperURL), "https://dubbeh.net/sm/rvw.html");
kv.GetString("Browse Fix", g_szBrowseFixURL, sizeof(g_szBrowseFixURL), "https://dubbeh.net/sm/rbf.php");
kv.GetString("Shoutcast Player", g_szShoutcastURL, sizeof(g_szShoutcastURL), "https://radio.dubbeh.net/index.php");
g_bUseGenres = view_as<bool>(kv.GetNum("Use Genres", 1));
g_iRadioPlayType = kv.GetNum("Radio Play Type", RADIO_PLAY_TYPE_SHOUTCAST);
if ((g_iRadioPlayType < RADIO_PLAY_TYPE_DIRECT) || (g_iRadioPlayType > RADIO_PLAY_TYPE_SHOUTCAST))
g_iRadioPlayType = RADIO_PLAY_TYPE_SHOUTCAST;
// Make sure we're always using genres in SHOUTCAST type
if (g_iRadioPlayType == RADIO_PLAY_TYPE_SHOUTCAST)
g_bUseGenres = true;
kv.GotoFirstSubKey(true);
iParentGenreCount = 0;
iChildGenreCount = 0;
g_Genres = RadioGenres();
g_Stations = RadioStations();
do {
if (g_bUseGenres)
{
kv.GetSectionName(szBuffer, sizeof(szBuffer));
if (szBuffer[0])
{
iParentGenreID = kv.GetNum("Genre ID", 0);
LogMessage("[SM-Radio] Parent genre found: Name=%s GenreID=%d", szBuffer, iParentGenreID);
IsGenreIDValid(kv, iParentGenreID);
RadioGenre parentgenre = RadioGenre();
parentgenre.m_iID = iParentGenreID;
parentgenre.m_iParentID = 0;
parentgenre.SetName(szBuffer);
bHasChildren = view_as<bool>(kv.GetNum("Has Children", 0));
parentgenre.m_bHasChildren = bHasChildren;
g_Genres.AddItem(parentgenre);
kv.GotoFirstSubKey(true);
if (bHasChildren)
{
iChildGenreCount = 0;
// Run over the child genres
do {
kv.GetSectionName(szBuffer, sizeof(szBuffer));
if (szBuffer[0])
{
RadioGenre childgenre = RadioGenre();
iChildGenreID = kv.GetNum("Genre ID", 0);
LogMessage("[SM-Radio] Child genre found: Name=%s GenreID=%d", szBuffer, iChildGenreID);
IsGenreIDValid(kv, iChildGenreID);
childgenre.m_iID = iChildGenreID;
childgenre.m_iParentID = iParentGenreID;
childgenre.SetName(szBuffer);
childgenre.m_bHasChildren = false;
g_Genres.AddItem(childgenre);
iChildGenreCount++;
kv.GotoFirstSubKey(true);
do {
ParseStationEntry(kv, iChildGenreID);
} while (kv.GotoNextKey(true));
kv.GoBack();
}
else
{
LogMessage("[SM-RADIO] Child genre at index \"%d\" Has no name set. Please correct this error in the config file", iChildGenreCount+1);
}
} while (kv.GotoNextKey(true));
kv.GoBack();
}
else
{
do {
ParseStationEntry(kv, iParentGenreID);
} while (kv.GotoNextKey(true));
kv.GoBack();
}
iParentGenreCount++;
}
else
{
LogMessage("[SM-RADIO] Parent genre at index \"%d\" Has no name set. Please correct this error in the config file", iParentGenreCount+1);
}
}
else
{
if (kv.GetSectionName(szBuffer, sizeof(szBuffer)))
{
ParseStationEntry(kv, 0);
}
}
} while (kv.GotoNextKey(true));
kv.GoBack();
// Invalid Genre or Station ID's found - Need to update with fixed version
if (g_bWriteKVToDisk)
{
kv.Rewind();
kv.ExportToFile(szLocalPath);
}
}
else
{
SetFailState("[SM-RADIO] Error unable to load \"%s\". Make sure you have the \"%s\" in that location.", szLocalPath, g_szRadioStationsBaseFile);
}
g_bStationsParsing = false;
delete kv;
}
void ParseStationEntry(KeyValues kv, int iGenreID)
{
char szSectionName[128] = "";
char szStreamURL[128] = "";
int iStationID = 0;
kv.GetSectionName(szSectionName, sizeof(szSectionName));
if (szSectionName[0])
{
iStationID = kv.GetNum("Station ID", 0);
// Does the current Station have an ID set?
if (iStationID == 0)
{
// Station has no pre-defined index - need to write to disk
iStationID = GetURandomInt();
// Off chance we get 0
if (iStationID == 0)
iStationID = GetURandomInt();
kv.SetNum("Station ID", iStationID);
g_bWriteKVToDisk = true;
}
if (g_iRadioPlayType != RADIO_PLAY_TYPE_SHOUTCAST)
kv.GetString("Stream URL", szStreamURL, sizeof(szStreamURL), "");
if (g_iRadioPlayType != RADIO_PLAY_TYPE_SHOUTCAST && !szStreamURL[0])
{
g_bWriteKVToDisk = false;
LogMessage("[SM-RADIO] Station \"%s\" has no Stream URL set. Please correct this problem and reload map.", szSectionName);
return;
}
RadioStation station = RadioStation();
station.m_iID = iStationID;
station.SetName(szSectionName);
if(szStreamURL[0])
station.SetStreamURL(szStreamURL);
station.m_iGenreID = iGenreID;
station.m_iLastUpdateTime = 0;
g_Stations.AddItem(station);
//LogMessage("[SM-Radio] Station found: StationID=%d GenreID=%d Name=%s", iStationID, iGenreID, szSectionName);
}
else
{
g_bWriteKVToDisk = false;
//LogMessage("[SM-RADIO] Station Section after station ID \"%d\" - Has no section name set. Please correct this error and reload the map", g_iStationCount + 1);
}
}
void IsGenreIDValid(KeyValues kv, int iGenreID)
{
// Does the current genre have an ID set?
if (iGenreID == 0)
{
iGenreID = GetURandomInt();
// Off chance we get 0
if (iGenreID == 0)
iGenreID = GetURandomInt();
// Genre has no pre-defined index - need to write to disk
kv.SetNum("Genre ID", iGenreID);
g_bWriteKVToDisk = true;
}
}
"ShoutCast Player" is set to localhost but returning the default value in GetString. Which shouldn't be happening with ImportFromFile returning true. Hopefully anyone can help with this very strange behaviour and hopefully fill me in on what it could be. Attached the KeyValues file at the base of the post for reference.
Thanks, dubbeh
P.S: The g_szRadioStationsBaseFile is set to "radiostations.txt"