Skip to content

Commit

Permalink
Chore: Tidy condition in OnEntityLoad()
Browse files Browse the repository at this point in the history
  • Loading branch information
zach2good committed Feb 6, 2025
1 parent 4c5b8f8 commit 75b4fe3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
22 changes: 20 additions & 2 deletions src/common/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -926,14 +926,32 @@ std::unique_ptr<FILE> utils::openFile(std::string const& path, std::string const
return std::unique_ptr<FILE>(fopen(path.c_str(), mode.c_str()));
}

auto utils::isPrintableASCII(unsigned char ch, ASCIIMode mode) -> bool
{
if (mode == ASCIIMode::IncludeSpace)
{
return ch >= 0x20 && ch < 0x7F;
}
else // ASCIIMode::ExcludeSpace
{
return ch > 0x20 && ch < 0x7F;
}
}

auto utils::isStringPrintable(const std::string& str, ASCIIMode mode) -> bool
{
// clang-format off
return std::all_of(str.begin(), str.end(), [mode](unsigned char ch) { return isPrintableASCII(ch, mode); });
// clang-format on
}

std::string utils::toASCII(std::string const& target, unsigned char replacement)
{
std::string out;
out.reserve(target.size());
for (unsigned char ch : target)
{
bool isLetter = ch >= 0x20 && ch < 0x7F;
out += isLetter ? ch : replacement;
out += isPrintableASCII(ch, ASCIIMode::IncludeSpace) ? ch : replacement;
}
return out;
}
9 changes: 9 additions & 0 deletions src/common/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ std::set<std::filesystem::path> sorted_directory_iterator(std::string path_name)
namespace utils
{
auto openFile(std::string const& path, std::string const& mode) -> std::unique_ptr<FILE>;

enum class ASCIIMode
{
IncludeSpace,
ExcludeSpace,
};

auto isPrintableASCII(unsigned char ch, ASCIIMode mode) -> bool;
auto isStringPrintable(const std::string& str, ASCIIMode mode) -> bool;
auto toASCII(std::string const& target, unsigned char replacement = '\0') -> std::string;
} // namespace utils

Expand Down
18 changes: 2 additions & 16 deletions src/map/lua/luautils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,28 +831,14 @@ namespace luautils
std::string filename;
if (PEntity->objtype == TYPE_NPC)
{
// clang-format off
auto isNamePrintable = [](const std::string& name)
{
// Match non-printable ASCII
for (const char& c : name)
{
if ((c >= 0 && c <= 0x20) || c >= 0x7F)
{
return false;
}
}
return true;
};
// clang-format on

// Don't bother even trying to load the script if the NPC name is non printable,
// and therefore impossible for a filesystem to load.
// TODO: Change name to "0x%X" instead so non-printables could get a script?
if (!isNamePrintable(PEntity->getName()))
if (!utils::isPrintableString(PEntity->getName(), ASCIIMode::ExcludeSpace))
{
return;
}

std::string zone_name = PEntity->loc.zone->getName();
std::string npc_name = PEntity->getName();
filename = fmt::format("./scripts/zones/{}/npcs/{}.lua", zone_name, npc_name);
Expand Down

0 comments on commit 75b4fe3

Please sign in to comment.