Skip to content

Commit

Permalink
Try to fix some crashes by replacing EndsWith with C++ implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenAlex committed Mar 26, 2023
1 parent 0a7c5e9 commit 61b1b96
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 32 deletions.
2 changes: 2 additions & 0 deletions include/Utils/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "Utils/FileUtils.hpp"

namespace Nya::Utils {
bool IsGif(StringW str);
std::string ToLowercase(std::string str);
List<StringW>* vectorToList(std::vector<StringW> values);
std::vector<StringW> listWToVector(List<StringW>* values);
int findStrIndexInList(List<StringW>* values, StringW string );
Expand Down
14 changes: 3 additions & 11 deletions src/Helpers/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "BSML/Animations/AnimationLoader.hpp"

#include "System/Uri.hpp"
#include "System/StringComparison.hpp"
#include "Utils/Utils.hpp"

#include "custom-types/shared/coroutine.hpp"

Expand Down Expand Up @@ -195,14 +195,6 @@ namespace BSML::Utilities {
}
}

bool IsAnimated(StringW str)
{
return str->EndsWith(".gif", System::StringComparison::OrdinalIgnoreCase) ||
str->EndsWith("_gif", System::StringComparison::OrdinalIgnoreCase) ||
str->EndsWith(".apng", System::StringComparison::OrdinalIgnoreCase)||
str->EndsWith("_apng", System::StringComparison::OrdinalIgnoreCase);
}

void SetImage(UnityEngine::UI::Image* image, StringW path) {
SetImage(image, path, true, ScaleOptions());
}
Expand Down Expand Up @@ -260,7 +252,7 @@ namespace BSML::Utilities {
bool isUri = System::Uri::TryCreate(path, System::UriKind::Absolute, byref(uri));
// animated just means ".gif || .apng"
// TODO: support for animated sprites in the future
if (IsAnimated(path) || (isUri && IsAnimated(uri->get_LocalPath()))) {
if (Nya::Utils::IsAnimated(path) || (isUri && Nya::Utils::IsAnimated(uri->get_LocalPath()))) {
DEBUG("Adding state updater");
auto stateUpdater = image->get_gameObject()->AddComponent<AnimationStateUpdater*>();
stateUpdater->image = image;
Expand All @@ -277,7 +269,7 @@ namespace BSML::Utilities {
if (onFinished) onFinished();
} else {
DEBUG("Data not found. starting fetch");
bool isGif = path->EndsWith("gif", System::StringComparison::OrdinalIgnoreCase) || (isUri && uri->get_LocalPath()->EndsWith("gif", System::StringComparison::OrdinalIgnoreCase));
bool isGif = Nya::Utils::IsGif(path) || (isUri && Nya::Utils::IsGif(uri->get_LocalPath()));

DEBUG("Creating callback");
auto onDataFinished = [stateUpdater, oldSprite, path, onFinished, animationController, isGif](bool success, ArrayW<uint8_t> data){
Expand Down
82 changes: 61 additions & 21 deletions src/Utils/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,34 @@
using namespace UnityEngine;
using namespace UnityEngine::Networking;

namespace fs = std::filesystem;


// Extensions for images
static std::unordered_set<std::string> ImageExtensions = {
".png",
".jpg",
".jpeg",
".gif",
".apng",
".webp",
".tiff",
".bmp"
};

// Extensions for animated images
static std::unordered_set<std::string> AnimatedImageExtensions = {
".gif",
".apng"
};

namespace Nya::Utils {
// Lowercase a string
std::string ToLowercase(std::string str) {
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c){ return std::tolower(c); });
return str;
}

/**
* @brief Converts vector to list
*
Expand Down Expand Up @@ -154,34 +181,45 @@ namespace Nya::Utils {
WARNING("IsAnimated got null string, possibly a bug");
return false;
}
return str->EndsWith(".gif", System::StringComparison::OrdinalIgnoreCase) ||
str->EndsWith("_gif", System::StringComparison::OrdinalIgnoreCase) ||
str->EndsWith(".apng", System::StringComparison::OrdinalIgnoreCase)||
str->EndsWith("_apng", System::StringComparison::OrdinalIgnoreCase);

auto path = fs::path(str);
auto extension = path.extension().string();

// Convert to lowercase
extension = ToLowercase(extension);

return AnimatedImageExtensions.contains(extension);
}

// Checks if the path is animated
bool IsGif(StringW str)
{
// Catch nulls
if (str == nullptr) {
WARNING("IsAnimated got null string, possibly a bug");
return false;
}

auto path = fs::path(str);
auto extension = path.extension().string();

return extension == ".gif";
}

bool IsImage(StringW str) {
// Catch nulls
if (str == nullptr) {
WARNING("IsImage got null string, possibly a bug");
return false;
}
return str->EndsWith(".gif", System::StringComparison::OrdinalIgnoreCase) ||
str->EndsWith("_gif", System::StringComparison::OrdinalIgnoreCase) ||
str->EndsWith(".apng", System::StringComparison::OrdinalIgnoreCase)||
str->EndsWith("_apng", System::StringComparison::OrdinalIgnoreCase)||
str->EndsWith(".png", System::StringComparison::OrdinalIgnoreCase) ||
str->EndsWith("_png", System::StringComparison::OrdinalIgnoreCase) ||
str->EndsWith(".jpeg", System::StringComparison::OrdinalIgnoreCase)||
str->EndsWith("_jpeg", System::StringComparison::OrdinalIgnoreCase) ||
str->EndsWith(".webp", System::StringComparison::OrdinalIgnoreCase)||
str->EndsWith("_webp", System::StringComparison::OrdinalIgnoreCase)||
str->EndsWith(".tiff", System::StringComparison::OrdinalIgnoreCase)||
str->EndsWith("_tiff", System::StringComparison::OrdinalIgnoreCase) ||
str->EndsWith(".jpg", System::StringComparison::OrdinalIgnoreCase)||
str->EndsWith("_jpg", System::StringComparison::OrdinalIgnoreCase) ||
str->EndsWith(".bmp", System::StringComparison::OrdinalIgnoreCase)||
str->EndsWith("_bmp", System::StringComparison::OrdinalIgnoreCase);


auto path = fs::path(str);
auto extension = path.extension().string();

// Convert to lowercase
extension = ToLowercase(extension);

return ImageExtensions.contains(extension);
}

// Got tired of pointers. If we can get a controller from a pointer, it means it's valid
Expand Down Expand Up @@ -259,5 +297,7 @@ namespace Nya::Utils {
}
coro(DownloadFileCoroutine(uri, path, onFinished));
}


}

0 comments on commit 61b1b96

Please sign in to comment.