Azerothcore-wotlk: Config global function to extract values from a comma separated list

Created on 7 Aug 2020  路  4Comments  路  Source: azerothcore/azerothcore-wotlk

Concept

We need a function in the config manager to easily extract values from a string list.
Ex: "1,2,3,4" or "1, 2, 3, 4"

In the module conf:
MyModule.List = "1, 2, 3, 4"

In the module cpp:

sConfigMgr->GetUint32ListDefault("MyModule.List", "")

Actually, everytime we want to make a list, we have to recreate a similar function in our modules. This is very bad
Maybe we can also have an argument to choose the delimiter and by default it's a comma ,.

This is what I tried unsuccessfully:

On a suggestion by Malow:

common/Configuration/Config.cpp

// Wrapper around GetStringDefault to get values from a comma separated list from a string
std::vector<uint32> ConfigMgr::GetUint32ListDefault(const char* name, const std::string &def, bool logUnused /*= true*/)
{
    string input = GetStringDefault(name, def, logUnused);

    vector<uint32> list;

    while (input.find(", ") != std::string::npos)
    {
        list.push_back(stoi(input.substr(0, s.find(", "))));
        input = input.substr(2);
    }

    // sLog->outError("============= %u", list);
    return list;
}

common/Configuration/Config.h

    std::vector<uint32> GetUint32ListDefault(const char* name, const std::string& def, bool logUnused = true);

The Config.h fails at compilation but I don't know how all this works so I can't fix it.

Also, I don't know if vector is the right data type to use as in the module I wanted that for, the list is in that form:

std::unordered_set<uint32> m_ignoreSpells = { 64380, 23885, 23880 };

unordered_set and not vector

The idea was to put this in the config, but also add a secondary list of optional spells and then merge them together. cf: https://github.com/azerothcore/mod-learn-spells/issues/17

Conclusion

Once done, it could also be used in mod-autobalance and replace the function created there LoadForcedCreatureIdsFromString()


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

CORE Fix included Good first issue Help wanted Suggestion

All 4 comments

there is another function to tokenize too, if I recall right, but anyway need to add it to the core and test it, which I didn't manage to do, needs someone who knows cpp

yooo @Riztazz

But you already have a function for it, why do you need another one?
In theory you could make a wrapper for it that deduces data types but use-cases are so very low that i don't think it's worth investing time into, just tokenize it on startup/reload and you're good to go.

Tokenizer toks(LoadConfigString, ',');
for (auto&& token : toks)
{
    DataType value = do your cast to type;
    container.insert/push_back/whatever
}

Hello Shin!

edit: Validate that string is not empty before tokenizing

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Maurowin picture Maurowin  路  3Comments

fragcamp picture fragcamp  路  4Comments

lineagedr picture lineagedr  路  3Comments

cts17 picture cts17  路  3Comments

dev-master21 picture dev-master21  路  3Comments