impr: Fix various issues with runtime-generated language strings

This commit is contained in:
WerWolv
2024-08-03 11:32:17 +02:00
parent efee128c1c
commit b2fc80f970
12 changed files with 108 additions and 73 deletions

View File

@@ -1199,7 +1199,7 @@ namespace hex {
class Service {
public:
Service(std::string name, std::jthread thread) : m_name(std::move(name)), m_thread(std::move(thread)) { }
Service(const UnlocalizedString &unlocalizedName, std::jthread thread) : m_unlocalizedName(std::move(unlocalizedName)), m_thread(std::move(thread)) { }
Service(const Service&) = delete;
Service(Service &&) = default;
~Service() {
@@ -1211,8 +1211,8 @@ namespace hex {
Service& operator=(const Service&) = delete;
Service& operator=(Service &&) = default;
[[nodiscard]] const std::string& getName() const {
return m_name;
[[nodiscard]] const UnlocalizedString& getUnlocalizedName() const {
return m_unlocalizedName;
}
[[nodiscard]] const std::jthread& getThread() const {
@@ -1220,7 +1220,7 @@ namespace hex {
}
private:
std::string m_name;
UnlocalizedString m_unlocalizedName;
std::jthread m_thread;
};
@@ -1235,13 +1235,13 @@ namespace hex {
}
void registerService(Lang name, const impl::Callback &callback) {
log::debug("Registered new background service: {}", name.get());
void registerService(const UnlocalizedString &unlocalizedName, const impl::Callback &callback) {
log::debug("Registered new background service: {}", unlocalizedName.get());
impl::s_services->emplace_back(
name,
unlocalizedName,
std::jthread([=](const std::stop_token &stopToken){
TaskManager::setCurrentThreadName(name);
TaskManager::setCurrentThreadName(Lang(unlocalizedName));
while (!stopToken.stop_requested()) {
callback();
std::this_thread::sleep_for(std::chrono::milliseconds(50));

View File

@@ -450,7 +450,7 @@ namespace hex {
// Do the destruction of the provider in the background once all tasks have finished
TaskManager::runWhenTasksFinished([providerToRemove] {
EventProviderDeleted::post(providerToRemove);
TaskManager::createBackgroundTask(Lang("Closing Provider"), [providerToRemove](Task &) {
TaskManager::createBackgroundTask("Closing Provider", [providerToRemove](Task &) {
eraseMutex.lock();
auto provider = std::move((*s_providersToRemove)[providerToRemove]);
s_providersToRemove->erase(providerToRemove);

View File

@@ -51,7 +51,7 @@ namespace hex {
if (value.empty())
continue;
s_currStrings->emplace(Lang::hash(key), value);
s_currStrings->emplace(LangConst::hash(key), value);
}
}
}
@@ -109,10 +109,11 @@ namespace hex {
}
Lang::Lang(const char *unlocalizedString) : m_entryHash(hash(unlocalizedString)) { }
Lang::Lang(const std::string &unlocalizedString) : m_entryHash(hash(unlocalizedString)) { }
Lang::Lang(const UnlocalizedString &unlocalizedString) : m_entryHash(hash(unlocalizedString.get())) { }
Lang::Lang(std::string_view unlocalizedString) : m_entryHash(hash(unlocalizedString)) { }
Lang::Lang(const char *unlocalizedString) : m_entryHash(LangConst::hash(unlocalizedString)), m_unlocalizedString(unlocalizedString) { }
Lang::Lang(const std::string &unlocalizedString) : m_entryHash(LangConst::hash(unlocalizedString)), m_unlocalizedString(unlocalizedString) { }
Lang::Lang(const LangConst &localizedString) : m_entryHash(localizedString.m_entryHash), m_unlocalizedString(localizedString.m_unlocalizedString) { }
Lang::Lang(const UnlocalizedString &unlocalizedString) : m_entryHash(LangConst::hash(unlocalizedString.get())), m_unlocalizedString(unlocalizedString.get()) { }
Lang::Lang(std::string_view unlocalizedString) : m_entryHash(LangConst::hash(unlocalizedString)), m_unlocalizedString(unlocalizedString) { }
Lang::operator std::string() const {
return get();
@@ -129,9 +130,32 @@ namespace hex {
const char *Lang::get() const {
const auto &lang = *LocalizationManager::s_currStrings;
auto it = lang.find(m_entryHash);
const auto it = lang.find(m_entryHash);
if (it == lang.end()) {
return m_unlocalizedString == nullptr ? "[ !!! INVALID LANGUAGE STRING !!! ]" : m_unlocalizedString;
return m_unlocalizedString.c_str();
} else {
return it->second.c_str();
}
}
LangConst::operator std::string() const {
return get();
}
LangConst::operator std::string_view() const {
return get();
}
LangConst::operator const char *() const {
return get();
}
const char *LangConst::get() const {
const auto &lang = *LocalizationManager::s_currStrings;
const auto it = lang.find(m_entryHash);
if (it == lang.end()) {
return m_unlocalizedString;
} else {
return it->second.c_str();
}

View File

@@ -63,8 +63,8 @@ namespace hex {
}
Task::Task(Lang name, u64 maxValue, bool background, std::function<void(Task &)> function)
: m_name(std::move(name)), m_maxValue(maxValue), m_function(std::move(function)), m_background(background) { }
Task::Task(const UnlocalizedString &unlocalizedName, u64 maxValue, bool background, std::function<void(Task &)> function)
: m_unlocalizedName(std::move(unlocalizedName)), m_maxValue(maxValue), m_function(std::move(function)), m_background(background) { }
Task::Task(hex::Task &&other) noexcept {
{
@@ -72,7 +72,7 @@ namespace hex {
std::scoped_lock otherLock(other.m_mutex);
m_function = std::move(other.m_function);
m_name = std::move(other.m_name);
m_unlocalizedName = std::move(other.m_unlocalizedName);
}
m_maxValue = u64(other.m_maxValue);
@@ -159,8 +159,8 @@ namespace hex {
return m_exceptionMessage;
}
const Lang &Task::getName() {
return m_name;
const UnlocalizedString &Task::getUnlocalizedName() {
return m_unlocalizedName;
}
u64 Task::getValue() const {
@@ -275,22 +275,22 @@ namespace hex {
try {
// Set the thread name to the name of the task
TaskManager::setCurrentThreadName(task->m_name);
TaskManager::setCurrentThreadName(Lang(task->m_unlocalizedName));
// Execute the task
task->m_function(*task);
log::debug("Task '{}' finished", task->m_name.get());
log::debug("Task '{}' finished", task->m_unlocalizedName.get());
} catch (const Task::TaskInterruptor &) {
// Handle the task being interrupted by user request
task->interruption();
} catch (const std::exception &e) {
log::error("Exception in task '{}': {}", task->m_name.get(), e.what());
log::error("Exception in task '{}': {}", task->m_unlocalizedName.get(), e.what());
// Handle the task throwing an uncaught exception
task->exception(e.what());
} catch (...) {
log::error("Exception in task '{}'", task->m_name.get());
log::error("Exception in task '{}'", task->m_unlocalizedName.get());
// Handle the task throwing an uncaught exception of unknown type
task->exception("Unknown Exception");
@@ -327,11 +327,11 @@ namespace hex {
s_tasksFinishedCallbacks.clear();
}
TaskHolder TaskManager::createTask(Lang name, u64 maxValue, bool background, std::function<void(Task&)> function) {
TaskHolder TaskManager::createTask(const UnlocalizedString &unlocalizedName, u64 maxValue, bool background, std::function<void(Task&)> function) {
std::scoped_lock lock(s_queueMutex);
// Construct new task
auto task = std::make_shared<Task>(std::move(name), maxValue, background, std::move(function));
auto task = std::make_shared<Task>(std::move(unlocalizedName), maxValue, background, std::move(function));
s_tasks.emplace_back(task);
@@ -344,14 +344,14 @@ namespace hex {
}
TaskHolder TaskManager::createTask(Lang name, u64 maxValue, std::function<void(Task &)> function) {
log::debug("Creating task {}", name);
return createTask(std::move(name), maxValue, false, std::move(function));
TaskHolder TaskManager::createTask(const UnlocalizedString &unlocalizedName, u64 maxValue, std::function<void(Task &)> function) {
log::debug("Creating task {}", unlocalizedName.get());
return createTask(std::move(unlocalizedName), maxValue, false, std::move(function));
}
TaskHolder TaskManager::createBackgroundTask(Lang name, std::function<void(Task &)> function) {
log::debug("Creating background task {}", name);
return createTask(std::move(name), 0, true, std::move(function));
TaskHolder TaskManager::createBackgroundTask(const UnlocalizedString &unlocalizedName, std::function<void(Task &)> function) {
log::debug("Creating background task {}", unlocalizedName.get());
return createTask(std::move(unlocalizedName), 0, true, std::move(function));
}
void TaskManager::collectGarbage() {