Skip to content

Commit

Permalink
Main script config fixes
Browse files Browse the repository at this point in the history
* Fix general issue where setStrings could overwrite getStrings data
  before copying it, resulting in corruption; noticed when loading main
  scripts from file while having --script argument
* Properly trim main scripts with repeat count
  • Loading branch information
Hual committed Sep 24, 2024
1 parent b4d411c commit dc6ec26
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
4 changes: 1 addition & 3 deletions Server/Components/LegacyConfig/config_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,17 +487,15 @@ class LegacyConfigComponent final : public ILegacyConfigComponent, public Consol
processFallback(logger, config, typeIt->second, name, line.substr(idx + 1));
}
}
size_t gmcount = 0;
DynamicArray<StringView> list;
for (int i = 0; i < gamemodes_.size(); ++i)
{
if (gamemodes_[i] != "")
{
++gmcount;
list.emplace_back(gamemodes_[i]);
}
}
if (gmcount != 0)
if (!list.empty())
{
config.setStrings("pawn.main_scripts", list);
}
Expand Down
12 changes: 7 additions & 5 deletions Server/Components/Pawn/Manager/Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "Manager.hpp"
#include "../PluginManager/PluginManager.hpp"
#include "../utils.hpp"
#include <utils.hpp>

#ifdef WIN32
#include <Windows.h>
Expand Down Expand Up @@ -416,25 +417,26 @@ bool PawnManager::Load(DynamicArray<StringView> const& mainScripts)
}
gamemodes_.clear();
gamemodeIndex_ = 0;
for (auto const& i : mainScripts)
for (StringView script : mainScripts)
{
script = trim(script);
// Split the mode name and count.
auto space = i.find_last_of(' ');
auto space = script.find_last_of(' ');
if (space == std::string::npos)
{
repeats_.push_back(1);
gamemodes_.push_back(String(i));
gamemodes_.push_back(String(script));
}
else
{
int count = 0;
auto conv = std::from_chars(i.data() + space + 1, i.data() + i.size(), count, 10);
auto conv = std::from_chars(script.data() + space + 1, script.data() + script.size(), count, 10);
if (conv.ec == std::errc::invalid_argument || conv.ec == std::errc::result_out_of_range || count < 1)
{
count = 1;
}
repeats_.push_back(count);
gamemodes_.push_back(String(i.substr(0, space)));
gamemodes_.push_back(String(trim(script.substr(0, space))));
}
}
gamemodeRepeat_ = repeats_[0];
Expand Down
5 changes: 3 additions & 2 deletions Server/Source/core_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,11 +553,12 @@ class Config final : public IEarlyConfig

void setStrings(StringView key, Span<const StringView> value) override
{
auto& vec = processed[String(key)].emplace<DynamicArray<String>>();
DynamicArray<String> newStrings;
for (const StringView v : value)
{
vec.emplace_back(String(v));
newStrings.emplace_back(String(v));
}
processed[String(key)].emplace<DynamicArray<String>>(std::move(newStrings));
}

void addBan(const BanEntry& entry) override
Expand Down

0 comments on commit dc6ec26

Please sign in to comment.