Hello,
So for some very odd reason, it seems as though when I am parsing through KeyValues, the do loop loops twice when I have generated the text file that I want the KeyValues to use.
Here is my code so you can better understand what I mean:
PHP Code:
#include <sourcemod>
public void OnPluginStart()
{
BrowseKeyValues();
}
void BrowseKeyValues()
{
KeyValues kv = new KeyValues("KV Test");
char config[256];
BuildPath(Path_SM, config, sizeof(config), "configs/kvtest.cfg");
if (!FileExists(config))
{
kv.JumpToKey("Section One", true);
kv.SetString("key", "value1");
kv.Rewind();
kv.JumpToKey("Section Two", true);
kv.SetString("key", "value2");
kv.Rewind();
kv.ExportToFile(config);
LogMessage("Generated the config file %s!", config);
}
if (!kv.ImportFromFile(config) || !kv.GotoFirstSubKey())
{
delete kv;
SetFailState("Failure parsing the config file %s!");
}
char section[32], value[32];
do {
kv.GetSectionName(section, sizeof(section));
kv.GetString("key", value, sizeof(value));
LogMessage("KV Test: (Section: %s) (Key: %s)", section, value);
} while kv.GotoNextKey();
delete kv;
}
/*
Config file contents:
"KV Test"
{
"Section One"
{
"key" "value1"
}
"Section Two"
{
"key" "value2"
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
Loading the plugin with creating the config file:
sm plugins load test
L 05/28/2020 - 13:18:07: [test.smx] Generated the config file addons/sourcemod/configs/kvtest.cfg!
L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section One) (Key: value1)
L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section Two) (Key: value2)
L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section One) (Key: value1)
L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section Two) (Key: value2)
[SM] Loaded plugin test.smx successfully.
//////////////////////////////////////////////////////////////////////////////////////////////////////
Loading the plugin without creating the config file:
sm plugins load test
L 05/28/2020 - 13:19:49: [test.smx] KV Test: (Section: Section One) (Key: value1)
L 05/28/2020 - 13:19:49: [test.smx] KV Test: (Section: Section Two) (Key: value2)
[SM] Loaded plugin test.smx successfully.
*/
Config file contents:
Code:
"KV Test"
{
"Section One"
{
"key" "value1"
}
"Section Two"
{
"key" "value2"
}
}
Output loading the plugin with creating the config file:
Code:
sm plugins load test
L 05/28/2020 - 13:18:07: [test.smx] Generated the config file addons/sourcemod/configs/kvtest.cfg!
L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section One) (Key: value1)
L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section Two) (Key: value2)
L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section One) (Key: value1)
L 05/28/2020 - 13:18:07: [test.smx] KV Test: (Section: Section Two) (Key: value2)
[SM] Loaded plugin test.smx successfully.
Output when loading the plugin without creating the config file:
Code:
sm plugins load test
L 05/28/2020 - 13:19:49: [test.smx] KV Test: (Section: Section One) (Key: value1)
L 05/28/2020 - 13:19:49: [test.smx] KV Test: (Section: Section Two) (Key: value2)
[SM] Loaded plugin test.smx successfully.
Any advice would be greatly appreciated!
Thank you!