Raised This Month: $51 Target: $400
 12% 

Efficient Data Storage/Retrieval


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
CrimsonGT
Veteran Member
Join Date: Oct 2007
Location: Gainesville, FL
Old 04-19-2009 , 14:44   Efficient Data Storage/Retrieval
Reply With Quote #1

Alright, so initially I was using a sourcemod plugin to open a keyvalues file, and pull out the settings for game modes. However, now more than one plugin need this information and I decided it would be much easier to just open this config file once, store the settings in c++ in a struct or something similar, and create a native to pull out the data I needed. I have hit a road block though, as I didnt completely think through how to set it up, so I am hoping theres something similar I can do.

Basically I have this, which opens the config file, pulls out the settings and sticks them in my struct.

Code:
void CBaseGameRules::ParseGameSettings()
{
    char path[256];
    KeyValues *kv = new KeyValues("GameSettings");

    g_pSM->BuildPath(Path_SM, path, sizeof(path)-1, "rocketarena/configs/gamemode_settings.cfg");
    kv->LoadFromFile(filesystem, path);

    KeyValues *cv = kv->FindKey(g_szGameMode);
    if(cv)
    {
        gamesettings_t.default_health = cv->GetInt("default_health");
        gamesettings_t.default_armor = cv->GetInt("default_armor");
        gamesettings_t.powerups_enabled = cv->GetInt("enable_powerups");
        gamesettings_t.selfdamage_enabled = cv->GetInt("enable_selfdamage");
        gamesettings_t.falldamage_enabled = cv->GetInt("enable_falldamage");

        gamesettings_t.max_rocketammo = cv->GetInt("max_rocketammo");
        gamesettings_t.max_grenadeammo = cv->GetInt("max_grenadeammo");
        gamesettings_t.max_railgunammo = cv->GetInt("max_railgunammo");
        gamesettings_t.max_lightningammo = cv->GetInt("max_lightningammo");
        gamesettings_t.max_shotgunammo = cv->GetInt("max_shotgunammo");
        gamesettings_t.max_plasmaammo = cv->GetInt("max_plasmaammo");
        gamesettings_t.max_machinegunammo = cv->GetInt("max_machinegunammo");

        gamesettings_t.enable_rocketlauncher = cv->GetInt("enable_rocketlauncher");
        gamesettings_t.enable_grenadelauncher = cv->GetInt("enable_grenadelauncher");
        gamesettings_t.enable_railgun = cv->GetInt("enable_railgun");
        gamesettings_t.enable_lightninggun = cv->GetInt("enable_lightninggun");
        gamesettings_t.enable_shotgun = cv->GetInt("enable_shotgun");
        gamesettings_t.enable_plasmagun = cv->GetInt("enable_plasmagun");
        gamesettings_t.enable_machinegun = cv->GetInt("enable_machinegun");
        gamesettings_t.enable_melee = cv->GetInt("enable_melee");
    }
}
Then I was going to try and do something like this with a native to retrieve the data I needed, but it doesnt work since 'str' isnt a member of the struct. So I am wondering if something similar is possible, or if im going to have to go with a much slower method such as hash maps, or something not as clean like arrays.

Code:
cell_t GetGamemodeSetting(IPluginContext *pContext, const cell_t *params)
{
    //Get the setting string pointer for Param[1]
    char *str;
    pContext->LocalToString(params[1], &str);

    int ret = 0;
    //ret = *(&gamesettings_t.str();
    ret = gamesettings_t.str();
    return ret;
}
__________________

Last edited by CrimsonGT; 04-19-2009 at 14:48.
CrimsonGT is offline
BAILOPAN
Join Date: Jan 2004
Old 04-19-2009 , 16:35   Re: Efficient Data Storage/Retrieval
Reply With Quote #2

First, ask yourself whether it's really necessary to expose all (or any) of these things (i.e., are you solving an existing problem, or introducing an artificial problem and then solving it?)

Then, decide what is best for users. String queries to a native is kind of awkward and lacks type checking. Another idea is something like what SourceMod does with the "magic" MaxClients public variable. If it doesn't change very often, you can just pump updates to plugins and they won't have to query anything through natives.
__________________
egg

Last edited by BAILOPAN; 04-19-2009 at 16:38.
BAILOPAN is offline
CrimsonGT
Veteran Member
Join Date: Oct 2007
Location: Gainesville, FL
Old 04-20-2009 , 03:05   Re: Efficient Data Storage/Retrieval
Reply With Quote #3

Well I realized I was extremely overcomplicating things, and by just making the keyvalue pointer global, I could just access that as needed, rather than moving them from the keyvalues to some other storage. Not sure if its the best method, but its working and I dont have to continually access it so I cant imagine its very resource intensive.

Thank you for the help.
__________________
CrimsonGT is offline
BAILOPAN
Join Date: Jan 2004
Old 04-20-2009 , 04:19   Re: Efficient Data Storage/Retrieval
Reply With Quote #4

KeyValues are pretty slow, I wouldn't use them for fast access, but I assume you've made that judgment call based on your use case.
__________________
egg
BAILOPAN is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:30.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode