Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config global function to extract values from a comma separated list #3267

Closed
BarbzYHOOL opened this issue Aug 7, 2020 · 5 comments
Closed

Comments

@BarbzYHOOL
Copy link
Member

BarbzYHOOL commented Aug 7, 2020

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: azerothcore/mod-learn-spells#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.

@Riztazz
Copy link

Riztazz commented Aug 8, 2020

Tokenizer tokens(moderatorList, ' ');

like this?;p change the token part to whatever your seperator is

@BarbzYHOOL
Copy link
Member Author

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

@FrancescoBorzi
Copy link
Contributor

yooo @Riztazz

@Riztazz
Copy link

Riztazz commented Aug 10, 2020

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

@Kitzunu
Copy link
Member

Kitzunu commented Sep 17, 2023

already possible see

Creatures.CustomIDs = "190010,55005,999991,25462,98888,601014,34567,34568"

@Kitzunu Kitzunu closed this as not planned Won't fix, can't repro, duplicate, stale Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants