Raised This Month: $32 Target: $400
 8% 

Read, Process, Write KeyValues


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Shaman
Senior Member
Join Date: Dec 2006
Location: Istanbul, Turkey
Old 07-12-2007 , 13:15   Read, Process, Write KeyValues
Reply With Quote #1

Here is what I have to do:
I have a files full of skills like this:
Code:
"Skills"
{
    "1"
    {
        "name"            "Sprint"
        "type"            "Passive"
        "skill"            "sprint"
        "levels"        "7"
        "level1"        "5"
        "level2"        "10"
        "level3"        "10"
        "level4"        "15"
        "level5"        "20"
        "level6"        "20"
        "level7"        "30"
    }
    "2"
    {
        "name"            "HealthRegeneration"
        "type"            "Active"
        "skill"            "hregen"
        "levels"        "7"
        "level1"        "1"
        "level2"        "2"
        "level3"        "3"
        "level4"        "3"
        "level5"        "4"
        "level6"        "4"
        "level7"        "5"
    }
    "...id..."
    {
        "name"            "...name..."
        "type"            "...type..."
        "skill"            "...skill..."
        "levels"        "...levels..."
        "level1"        "..."
         "level..."        "..."
    }
}
I want to read all skills from the file above, sort them (A->Z) and save to another file like this:
Code:
"Skill_Data"
{
    "HealthRegeneration"
    {
        "id"            "2"
    }
    "Sprint"
    {
        "id"            "1"
    }
     "...name..."
     {
         "id"            "...id..."
     }
}
Then I will read this file again and make menus. To prevent duplicate entries I always have to delete contents of the "Skill_Data" before writing to it.

I made this, but I can't compile it:
Code:
stock ScanSkills()
    {
    //
    kv= CreateKeyValues("Skills");
    //
    BuildPath(Path_SM, kv_file, PLATFORM_MAX_PATH, "configs/plugin.heroessource.skills.txt");
    //Get KV's
    if(!FileToKeyValues(kv,kv_file))
        SetFailState("[Heroes: Source]Failed to load 'Skills'!");
    //Rewind to top
    KvRewind(kv);
    //
    if(!KvGotoFirstSubKey(kv))
        return -1;
    //
    new skillcount=0;
    new skillid;
    decl String:s_skillid[32];
    //
    do
        {
        //
        KvGetSectionName(kv, s_skillid, sizeof(s_skillid));
        skillid=StringToInt(s_skillid);
        //
        KvGetString(kv, "name", skillnames[skillid], sizeof(skillnames));
        //
        skillcount++;
        }
        while(KvGotoNextKey(kv))
    //
    CloseHandle(kv);
    //
    kv= CreateKeyValues("Skill_Data");
    //
    BuildPath(Path_SM, kv_file, PLATFORM_MAX_PATH, "data/plugin.heroessource.skilldata.txt");
    //Get KV's
    if(!FileToKeyValues(kv,kv_file))
        SetFailState("[Heroes: Source]Failed to load 'Skill_Data'!");
    //Rewind to top
    KvRewind(kv);
    //
    if(!KvGotoFirstSubKey(kv))
        return -2;
    //Delete all sections
    for(;;)
        {
        decl String:name[4];
        KvGetString(kv, name, sizeof(name));
        if(name[0]=='\0')
            {
            if(KvDeleteThis(kv)<1)
                {
                break;
                }
            }
            else if(!KvGotoNextKey(kv))
            {
            break;
            }
        }
    //Rewind to top
    KvRewind(kv);
    //
    for(new i=1; i<=skillcount; i++)
        {
        KvJumpToKey(kv, skillnames[i], true);
        KvSetNum(kv, "id", i);
        KvGoBack(kv);
        }
    //
    KeyValuesToFile(kv, kv_file);
    //
    CloseHandle(kv);
    return true;
    }
I got the "Full Deletion" and "Iterative Lookup" codes from "KeyValues (SourceMod Scripting)" article in the wiki.

Thank you for your posts.
__________________
Shaman is offline
Send a message via ICQ to Shaman Send a message via AIM to Shaman Send a message via MSN to Shaman Send a message via Yahoo to Shaman
BAILOPAN
Join Date: Jan 2004
Old 07-13-2007 , 11:37   Re: Read, Process, Write KeyValues
Reply With Quote #2

You can't compile it, or it's not working? What are your compile errors?
__________________
egg
BAILOPAN is offline
Shaman
Senior Member
Join Date: Dec 2006
Location: Istanbul, Turkey
Old 07-14-2007 , 04:27   Re: Read, Process, Write KeyValues
Reply With Quote #3

Now I don't remember what were the compile errors. I didn't got any replies so I had to change the code and I made this:
Code:
stock ScanSkills()
    {
    //
    kv= CreateKeyValues("Skills");
    //
    BuildPath(Path_SM, kv_file, PLATFORM_MAX_PATH, "configs/plugin.heroessource.skills.txt");
    //Get KV's
    if(!FileToKeyValues(kv,kv_file))
        SetFailState("[Heroes: Source]Failed to load 'Skills'!");
    //Rewind to top
    KvRewind(kv);
    //
    decl String:key[3];
    new skillcount=0;
    for(new i=1; i<MAXSKILLS; i++)
        {
        IntToString(i, key, sizeof(key));
        if(!KvJumpToKey(kv, key, false))
            break;
        
        KvGetString(kv, key, skillnames[i], sizeof(skillnames));
        skillcount++;
        }
    //
    CloseHandle(kv);
    //
    kv= CreateKeyValues("Skill_Data");
    //
    BuildPath(Path_SM, kv_file, PLATFORM_MAX_PATH, "data/plugin.heroessource.skilldata.txt");
    //
    if(!FileToKeyValues(kv,kv_file))
        SetFailState("[Heroes: Source]Failed to load 'Skill_Data'!");
    //Rewind to top
    KvRewind(kv);
    //
    for(new i=1; i<MAXSKILLS; i++)
        {
        IntToString(i, key, sizeof(key));
        if(!KvDeleteKey(kv, key))
            break;
        }
    //
    for(new i=1; i<=skillcount; i++)
        {
        IntToString(i, key, sizeof(key));
        if(!KvJumpToKey(kv, skillnames[i], true))
            break;
        KvSetNum(kv, "id", i);
        }
    //
    new bool:result=KeyValuesToFile(kv, kv_file);
    //
    CloseHandle(kv);
    //
    return result;
    }
This code doesn't give me any errors when compiling or in game, but it doesn't work. I just need to make this:
1-Get "id->name" from a file
2-Make them "name->id"
3-Sort names A to Z
4-Save the result to another file.
5-Read all names and make a menu.
__________________
Shaman is offline
Send a message via ICQ to Shaman Send a message via AIM to Shaman Send a message via MSN to Shaman Send a message via Yahoo to Shaman
BAILOPAN
Join Date: Jan 2004
Old 07-14-2007 , 10:16   Re: Read, Process, Write KeyValues
Reply With Quote #4

Which part doesn't work? For starters I think this:
Code:
        KvGetString(kv, key, skillnames[i], sizeof(skillnames));         skillcount++;

Must be:
Code:
        KvGetString(kv, "name", skillnames[i], sizeof(skillnames));         skillcount++;         KvGoBack(kv);
__________________
egg
BAILOPAN is offline
Shaman
Senior Member
Join Date: Dec 2006
Location: Istanbul, Turkey
Old 07-15-2007 , 05:50   Re: Read, Process, Write KeyValues
Reply With Quote #5

I did what you said, but still nothing happens. No errors, no output, nothing
__________________

Last edited by Shaman; 07-15-2007 at 07:14.
Shaman is offline
Send a message via ICQ to Shaman Send a message via AIM to Shaman Send a message via MSN to Shaman Send a message via Yahoo to Shaman
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 19:54.


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