Beispiel #1
0
    SettingsMap* ConfigManager::GetSection(const Urho3D::String& section, bool create)
    {
        // Empty section gets assigned to main map
        if (section == Urho3D::String::EMPTY)
            return &_map;

        // Split section into submaps
        Urho3D::Vector<Urho3D::String> split;

        unsigned splitPos = 0;

        if (ConfigFile::ParseHeader(section).Empty())
        {
            return &_map;
        }

        // Split sections by '.' or '/'
        // Comments will ignore splits behind them
        while (splitPos != Urho3D::String::NPOS)
        {
            // Find next comment split
            auto commentSplitPos = splitPos;
            auto hashPos = section.Find("#", commentSplitPos);
            auto slashesPos = section.Find("//", commentSplitPos);
            commentSplitPos = (hashPos < slashesPos) ? hashPos : slashesPos;

            // Find next split
            auto lastSplitPos = splitPos;
            auto dotPos = section.Find(".", lastSplitPos);
            auto slashPos = section.Find("/", lastSplitPos);
            splitPos = (dotPos < slashPos) ? dotPos : slashPos;

            // Ignore splits after comments
            splitPos = (commentSplitPos <= splitPos) ? Urho3D::String::NPOS : splitPos;

            int length = splitPos - lastSplitPos;
            if (splitPos == Urho3D::String::NPOS)
            {
                length = section.Length() - lastSplitPos;
            }
            
            auto sub = section.Substring(lastSplitPos, length);

            if (sub != Urho3D::String::EMPTY)
                split.Push(sub);
        }

        SettingsMap* currentMap = &_map;
        for (auto itr = split.Begin(); itr != split.End(); itr++)
        {
            auto section = *itr;

            // Find section
            SettingsMap* newMap = 0;
            for (auto map_itr = currentMap->Begin(); map_itr != currentMap->End(); map_itr++)
            {
                if (map_itr->first_ == section)
                {
                    newMap = static_cast<SettingsMap*>(map_itr->second_.GetVoidPtr());

                    // Key exists, but is not a SettingsMap
                    if (!newMap)
                    {
                        return 0;
                    }

                    // Key exists
                    break;
                }
            }

            // Key does not exist
            if (!newMap)
            {
                if (create)
                {
                    currentMap->operator[](section) = new SettingsMap();
                    newMap = static_cast<SettingsMap*>((*currentMap)[section].GetVoidPtr());
                }
            }

            if (newMap)
            {
                currentMap = newMap;
            }
        }

        return currentMap;
    }
Beispiel #2
0
    void ConfigManager::SaveSettingsMap(Urho3D::String section, SettingsMap& map, ConfigFile& configFile)
    {
        // Save out parameters
        for (auto itr = map.Begin(); itr != map.End(); itr++)
        {
            // Skip over sub-sections
            if (itr->second_.GetType() == Urho3D::VAR_VOIDPTR)
            {
                continue;
            }

            auto value = itr->first_;

            // Set parameter
            if (itr->second_.GetType() == Urho3D::VAR_STRING)
                configFile.Set(section, value, itr->second_.GetString());

            if (itr->second_.GetType() == Urho3D::VAR_INT)
                configFile.Set(section, value, Urho3D::String(itr->second_.GetInt()));

            if (itr->second_.GetType() == Urho3D::VAR_BOOL)
                configFile.Set(section, value, Urho3D::String(itr->second_.GetBool()));

            if (itr->second_.GetType() == Urho3D::VAR_FLOAT)
                configFile.Set(section, value, Urho3D::String(itr->second_.GetFloat()));

            if (itr->second_.GetType() == Urho3D::VAR_VECTOR2)
                configFile.Set(section, value, Urho3D::String(itr->second_.GetVector2()));

            if (itr->second_.GetType() == Urho3D::VAR_VECTOR3)
                configFile.Set(section, value, Urho3D::String(itr->second_.GetVector3()));

            if (itr->second_.GetType() == Urho3D::VAR_VECTOR4)
                configFile.Set(section, value, Urho3D::String(itr->second_.GetVector4()));

            if (itr->second_.GetType() == Urho3D::VAR_QUATERNION)
                configFile.Set(section, value, Urho3D::String(itr->second_.GetQuaternion()));

            if (itr->second_.GetType() == Urho3D::VAR_COLOR)
                configFile.Set(section, value, Urho3D::String(itr->second_.GetColor()));

            if (itr->second_.GetType() == Urho3D::VAR_INTRECT)
                configFile.Set(section, value, Urho3D::String(itr->second_.GetIntRect()));

            if (itr->second_.GetType() == Urho3D::VAR_INTVECTOR2)
                configFile.Set(section, value, Urho3D::String(itr->second_.GetIntVector2()));

            if (itr->second_.GetType() == Urho3D::VAR_MATRIX3)
                configFile.Set(section, value, Urho3D::String(itr->second_.GetMatrix3()));

            if (itr->second_.GetType() == Urho3D::VAR_MATRIX3X4)
                configFile.Set(section, value, Urho3D::String(itr->second_.GetMatrix3x4()));

            if (itr->second_.GetType() == Urho3D::VAR_MATRIX4)
                configFile.Set(section, value, Urho3D::String(itr->second_.GetMatrix4()));
        }

        // Save out sub-sections
        for (auto itr = map.Begin(); itr != map.End(); itr++)
        {
            // Skip over parameter
            if (itr->second_.GetType() != Urho3D::VAR_VOIDPTR)
            {
                continue;
            }

            Urho3D::String path = section;
            path.Append(itr->first_);

            auto fsdf = map[section];
            auto value = static_cast<SettingsMap*>(itr->second_.GetVoidPtr());

            if (value)
            {
                // Save sub-section
                SaveSettingsMap(path, *value, configFile);
            }
        }
    }