From 1d92f91aee5e0eabd5ea629891fa02b75719a5e0 Mon Sep 17 00:00:00 2001 From: VDm Date: Sat, 8 Apr 2023 15:18:29 +0400 Subject: [PATCH] feat(app): add OpenArchives --- src/client/Client.cpp | 3 +++ src/util/Filesystem.cpp | 60 +++++++++++++++++++++++++++++++++++++++++ src/util/Filesystem.hpp | 2 ++ 3 files changed, 65 insertions(+) diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 8d423b7..6b94fb5 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -14,6 +14,7 @@ #include "ui/FrameScript.hpp" #include "ui/FrameXML.hpp" #include "world/World.hpp" +#include "util/Filesystem.hpp" #include #include #include @@ -156,6 +157,8 @@ int32_t InitializeGlobal() { // ClientServices::LoadCDKey(); + OpenArchives(); + ConsoleInitializeClientCommand(); ConsoleInitializeClientCVar("Config.wtf"); diff --git a/src/util/Filesystem.cpp b/src/util/Filesystem.cpp index d7fbf24..69193f2 100644 --- a/src/util/Filesystem.cpp +++ b/src/util/Filesystem.cpp @@ -1,8 +1,19 @@ #include "util/Filesystem.hpp" #include +#include +#include +#include #include #include +static const std::vector s_languages = { + "enUS", "enGB", "enTW", "zhTW", "esES", + "ruRU", "koKR", "ptPT", "esMX", "itIT", + "deDE", "frFR", "enCN", "zhCN", "ptBR" +}; + +void* g_mpqHandle = nullptr; + void OsCreateDirectory(const char* pathName, int32_t recursive) { // TODO } @@ -30,7 +41,56 @@ char* OsPathFindExtensionWithDot(char* pathName) { return result; } +void OpenPatches(const std::string& language, const std::string& fileName) { + std::string path("Data/" + language + "/"); + std::string fullPath; + for (int i = 1; i < 20; ++i) { + if (i < 2) { + fullPath = path + fileName + ".MPQ"; + } else { + fullPath = path + fileName + "-" + std::to_string(i) + ".MPQ"; + } + if (!SFileOpenPatchArchive(g_mpqHandle, fullPath.c_str(), nullptr, 0)) + return; + } +} + void OpenArchives() { + struct stat info = { 0 }; + std::string dataPath("Data/"); + if (stat(dataPath.c_str(), &info) != 0 || (info.st_mode & S_IFDIR) == 0) + return; + + if (!SFileOpenArchive((dataPath + "common.MPQ").c_str(), 0, STREAM_FLAG_READ_ONLY, &g_mpqHandle)) + return; + + if (!SFileOpenPatchArchive(g_mpqHandle, (dataPath + "common-2.MPQ").c_str(), nullptr, 0)) + return; + + if (!SFileOpenPatchArchive(g_mpqHandle, (dataPath + "expansion.MPQ").c_str(), nullptr, 0)) + return; + + if (!SFileOpenPatchArchive(g_mpqHandle, (dataPath + "lichking.MPQ").c_str(), nullptr, 0)) + return; + + std::string language; + + for (size_t i = 0; i < s_languages.size(); ++i) { + if (stat((dataPath + s_languages[i]).c_str(), &info) == 0 && (info.st_mode & S_IFDIR) != 0) { + language = s_languages[i]; + break; + } + } + + OpenPatches(language, "locale-" + language); + OpenPatches(language, "speech-" + language); + OpenPatches(language, "expansion-locale-" + language); + OpenPatches(language, "lichking-locale-" + language); + OpenPatches(language, "expansion-speech-" + language); + OpenPatches(language, "lichking-speech-" + language); + OpenPatches(language, "patch-" + language); + OpenPatches(language, "patch"); + OpenPatches(".", "patch"); } diff --git a/src/util/Filesystem.hpp b/src/util/Filesystem.hpp index f42406d..8341006 100644 --- a/src/util/Filesystem.hpp +++ b/src/util/Filesystem.hpp @@ -12,4 +12,6 @@ char* OsPathFindExtensionWithDot(char*); void OpenArchives(); +extern void* g_mpqHandle; + #endif