Add langugage impl, bug fixes

This commit is contained in:
Grinch_ 2022-02-11 12:38:14 +06:00
parent 97db976bdf
commit 0697811db8
8 changed files with 191 additions and 22 deletions

View File

@ -179,6 +179,7 @@ CheatMenu::CheatMenu()
m_fMenuSize.y = gConfig.GetValue("window.sizeY", screen::GetScreenHeight() / 1.2f); m_fMenuSize.y = gConfig.GetValue("window.sizeY", screen::GetScreenHeight() / 1.2f);
srand(CTimer::m_snTimeInMilliseconds); srand(CTimer::m_snTimeInMilliseconds);
Locale::Init("CheatMenu/locale/");
Events::processScriptsEvent += []() Events::processScriptsEvent += []()
{ {
if (!BY_GAME(FrontEndMenuManager.m_bMenuActive, FrontendMenuManager.m_bMenuVisible, FrontEndMenuManager.m_bMenuActive)) if (!BY_GAME(FrontEndMenuManager.m_bMenuActive, FrontendMenuManager.m_bMenuVisible, FrontEndMenuManager.m_bMenuActive))

View File

@ -1,14 +1,17 @@
#include "pch.h" #include "pch.h"
#include "json.h" #include "json.h"
CJson::CJson(const char* name) CJson::CJson(const char* name, bool pathPredefined)
{ {
if (name == "" || !std::filesystem::is_directory(PLUGIN_PATH((char*)"CheatMenu"))) if (name == "" || !std::filesystem::is_directory(PLUGIN_PATH((char*)"CheatMenu")))
{ {
return; return;
} }
m_FilePath = PLUGIN_PATH((char*)"/CheatMenu/json/") + std::string(name) + ".json"; if (!pathPredefined)
{
m_FilePath = PLUGIN_PATH((char*)"/CheatMenu/json/") + std::string(name) + ".json";
}
if (std::filesystem::exists(m_FilePath)) if (std::filesystem::exists(m_FilePath))
{ {

View File

@ -47,8 +47,7 @@ public:
} }
} }
template <> std::string GetValueStr(const std::string& key, const std::string& defaultVal)
std::string GetValue(std::string&& key, std::string&& defaultVal)
{ {
try try
{ {
@ -119,5 +118,5 @@ public:
Saves json data to disk Saves json data to disk
*/ */
void WriteToDisk(); void WriteToDisk();
CJson(const char* text = ""); CJson(const char* text, bool pathPredefined = false);
}; };

91
src/locale.cpp Normal file
View File

@ -0,0 +1,91 @@
#include "pch.h"
#include "locale.h"
#include <filesystem>
Locale::eReturnCodes Locale::Init(const char* path)
{
std::string localePath = path;
if (localePath.back() != '/')
{
localePath += '/';
}
#ifdef _GTA_
m_path = PLUGIN_PATH((char*)localePath.c_str());
#else
m_path = localePath;
#endif
if (!std::filesystem::exists(m_path))
{
#ifdef _GTA_
gLog << "Locale directory doesn't exist" << std::endl;
#endif
return eReturnCodes::DIR_NOT_FOUND;
}
/*
Get the list of available languages
We won't load them here, we'll load them when we need them
*/
#ifdef _GTA_
gLog << "Loading languages..." << std::endl;
#endif
for (auto& entry : std::filesystem::directory_iterator(m_path))
{
if (entry.path().extension() == ".json")
{
#ifdef _GTA_
gLog << "Found locale: " << entry.path().stem().string() << std::endl;
#endif
m_locales.push_back(entry.path().stem().string());
}
}
if (sizeof(m_locales) == 0)
{
#ifdef _GTA_
gLog << "No language files found" << std::endl;
#endif
return eReturnCodes::NO_LOCALE_FOUND;
}
return eReturnCodes::SUCCESS;
}
std::vector<std::string>& Locale::GetLocaleList()
{
return m_locales;
}
std::string Locale::GetText(std::string&& key, std::string&& defaultValue)
{
if (m_pJson == nullptr)
{
return defaultValue;
}
return m_pJson->GetValueStr(key, defaultValue);
}
Locale::eReturnCodes Locale::SetLocale(int index)
{
if(m_pJson)
{
delete m_pJson;
m_pJson = nullptr;
}
if (index < 0 || index >= m_locales.size())
{
return eReturnCodes::INVALID_INDEX;
}
std::string localeFile = m_locales[index];
localeFile += ".json";
std::string localePath = m_path + localeFile;
m_pJson = new CJson(localePath.c_str());
return eReturnCodes::SUCCESS;
}

57
src/locale.h Normal file
View File

@ -0,0 +1,57 @@
#pragma once
#include <vector>
#include <string>
#include "json.h"
/*
A custom i18n library
Loads strings from a json file
Requires the CJson class
*/
class Locale
{
private:
static inline std::vector<std::string> m_locales;
static inline std::string m_path;
static inline CJson *m_pJson = nullptr;
public:
enum class eReturnCodes
{
DIR_NOT_FOUND = 0, // Failed to find the language directory
NO_LOCALE_FOUND = 1, // Failed to find language files
INVALID_INDEX = 2, // Invalid langauge index for GetLocaleList()
SUCCESS = 3,
};
Locale() = delete;
Locale(Locale const&) = delete;
void operator=(Locale const&) = delete;
/*
Loads json files from the locale directory
Calling it multiple times will unload previous data
*/
static eReturnCodes Init(const char* path);
// Returns a vector of available languages
static std::vector<std::string>& GetLocaleList();
/*
Returns the string for the given key for the language specified
If the key is not found, returns the defaultValue
You need to call SetLanguage once before calling this function
By default, the language is set to "en"
*/
static std::string GetText(std::string&& key, std::string&& defaultValue);
/*
Sets the language to use
If the language is not found, the default language is used
index is the index of the language in the GetLocaleList() list
*/
static eReturnCodes SetLocale(int index);
};

View File

@ -350,21 +350,37 @@ void Menu::ProcessCommands()
void Menu::ShowPage() void Menu::ShowPage()
{ {
ImGui::Spacing();
if (ImGui::Button("Reset config", ImVec2(Ui::GetSize(2))))
{
gConfig.m_Data.clear();
SetHelpMessage("Config has been reset. Restart the game for it to take effect.", false, false, false);
}
ImGui::SameLine();
if (ImGui::Button("Reset size", ImVec2(Ui::GetSize(2))))
{
CheatMenu::ResetMenuSize();
}
ImGui::Spacing();
if (ImGui::BeginTabBar("Menu", ImGuiTabBarFlags_NoTooltip + ImGuiTabBarFlags_FittingPolicyScroll)) if (ImGui::BeginTabBar("Menu", ImGuiTabBarFlags_NoTooltip + ImGuiTabBarFlags_FittingPolicyScroll))
{ {
if (ImGui::BeginTabItem("Config"))
{
ImGui::Spacing();
if (ImGui::Button("Reset config", ImVec2(Ui::GetSize(2))))
{
gConfig.m_Data.clear();
SetHelpMessage("Config has been reset. Restart the game for it to take effect.", false, false, false);
}
ImGui::SameLine();
if (ImGui::Button("Reset size", ImVec2(Ui::GetSize(2))))
{
CheatMenu::ResetMenuSize();
}
ImGui::Spacing();
static int selected = 0;
if (Ui::ListBox("Language", Locale::GetLocaleList(), selected))
{
if (Locale::SetLocale(selected) == Locale::eReturnCodes::SUCCESS)
{
SetHelpMessage(Locale::GetText("Menu.LanguageChangeSuccess", "Language changed successfully!").c_str(), false, false, false);
}
else
{
SetHelpMessage(Locale::GetText("Menu.LanguageChangeFailed", "Failed to change language").c_str(), false, false, false);
}
}
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Overlay")) if (ImGui::BeginTabItem("Overlay"))
{ {
ImGui::Spacing(); ImGui::Spacing();

View File

@ -1,5 +1,7 @@
#include "pch.h" #include "pch.h"
CJson gConfig = CJson("config"); CJson gConfig = CJson("config");
eRenderer gRenderer = Render_Unknown;
std::ofstream gLog = std::ofstream("CheatMenu.log");
Hotkey aimSkinChanger; Hotkey aimSkinChanger;
Hotkey freeCam; Hotkey freeCam;

View File

@ -6,6 +6,7 @@
#define DISCORD_INVITE "https://discord.gg/ZzW7kmf" #define DISCORD_INVITE "https://discord.gg/ZzW7kmf"
#define GITHUB_LINK "https://github.com/user-grinch/Cheat-Menu" #define GITHUB_LINK "https://github.com/user-grinch/Cheat-Menu"
#define IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS
#define _GTA_
#ifdef GTASA #ifdef GTASA
#define BY_GAME(sa, vc, iii) sa #define BY_GAME(sa, vc, iii) sa
@ -73,6 +74,7 @@
#include "vkeys.h" #include "vkeys.h"
#include "resourcestore.h" #include "resourcestore.h"
#include "fontmgr.h" #include "fontmgr.h"
#include "locale.h"
using namespace plugin; using namespace plugin;
@ -83,10 +85,8 @@ enum eRenderer
Render_Unknown Render_Unknown
}; };
static eRenderer gRenderer = Render_Unknown; extern eRenderer gRenderer;
static std::ofstream gLog = std::ofstream("CheatMenu.log"); extern std::ofstream gLog;
// why doesn't this work?
// inline CJson gConfig = CJson("config");
extern CJson gConfig; extern CJson gConfig;
// Fix function clashes // Fix function clashes