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

Chore: Tidy condition in OnEntityLoad() #6973

Open
wants to merge 1 commit into
base: base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading