From 7c7cb0482bd409668e797983021879dd3bfff003 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sun, 28 Jul 2024 17:09:28 -0400 Subject: [PATCH] =?UTF-8?q?Map=20converter=20now=20works=20=F0=9F=93=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/src/Windows/MapImporterWindow.cpp | 127 +++++++++++++++++- Editor/src/Windows/MapImporterWindow.h | 6 +- .../Windows/TrenchbroomConfiguratorWindow.cpp | 2 +- Nuake/src/Core/String.cpp | 11 ++ Nuake/src/Core/String.h | 1 + Nuake/src/Scene/Systems/WadConverter.cpp | 1 + 6 files changed, 144 insertions(+), 4 deletions(-) diff --git a/Editor/src/Windows/MapImporterWindow.cpp b/Editor/src/Windows/MapImporterWindow.cpp index 1d89947a..c6b56176 100644 --- a/Editor/src/Windows/MapImporterWindow.cpp +++ b/Editor/src/Windows/MapImporterWindow.cpp @@ -4,6 +4,7 @@ #include #include #include +#include void MapImporterWindow::Draw() { @@ -64,12 +65,81 @@ void MapImporterWindow::Draw() ImGui::EndDragDropTarget(); } - auto wads = ScanUsedWads(); + if (Nuake::UI::PrimaryButton("Scan WADs")) + { + m_MapToImport = nullptr; + } + + for (auto& w : ScanUsedWads()) + { + auto pathSplits = String::Split(std::string(w.begin(), w.end() - 4), '/'); + std::string WadName = pathSplits[std::size(pathSplits) - 1]; + std::string TargetDirectory = "/Materials/" + WadName + "/"; + } if (Nuake::UI::PrimaryButton("Export")) { + if (m_OutputFile.empty()) + { + std::string outputFile = FileDialog::SaveFile("*.map"); + if (!String::EndsWith(outputFile, ".map")) + { + outputFile += ".map"; + } + m_OutputFile = FileSystem::AbsoluteToRelative(outputFile); + } + + std::string mapContent = m_MapToImport->Read(); + std::istringstream stream(mapContent); + + std::ostringstream modifiedMapContent; + + std::regex pattern(R"(\(\s*-?\d+\s*-?\d+\s*-?\d+\s*\)\s*\(\s*-?\d+\s*-?\d+\s*-?\d+\s*\)\s*\(\s*-?\d+\s*-?\d+\s*-?\d+\s*\)\s*([^\s]+))"); + + std::string line; + while (std::getline(stream, line)) + { + std::smatch match; + if (std::regex_search(line, match, pattern)) + { + // If a match is found, return the captured value + std::string result = match[1].str(); + std::string result2 = match[0].str(); + result2.erase(result2.end() - result.length(), result2.end()); + + std::string transformWad = GetTransformedWadPath(result); + if (transformWad.empty()) + { + Logger::Log("Couldn't find texture: " + result, "map importer", CRITICAL); + } + + line = result2 + std::regex_replace(line, pattern, transformWad); + } + + modifiedMapContent << line << std::endl; + } + + FileSystem::BeginWriteFile(m_OutputFile); + FileSystem::WriteLine(modifiedMapContent.str()); + FileSystem::EndWriteFile(); + m_MapToImport = nullptr; } + + ImGui::SameLine(); + + ImGui::TextUnformatted(m_OutputFile.c_str()); + + if (ImGui::IsItemClicked()) + { + std::string outputFile = FileDialog::SaveFile("*.map"); + if (!String::EndsWith(outputFile, ".map")) + { + outputFile += ".map"; + } + + m_OutputFile = FileSystem::AbsoluteToRelative(outputFile); + } } ImGui::End(); } @@ -81,6 +151,8 @@ std::vector MapImporterWindow::ScanUsedWads() return std::vector(); } + m_DetectedWads.clear(); + using namespace Nuake; const std::string mapContent = m_MapToImport->Read(); @@ -104,4 +176,55 @@ std::vector MapImporterWindow::ScanUsedWads() // If no match is found, return an empty string return usedWads; -} \ No newline at end of file +} + +std::string MapImporterWindow::GetTransformedWadPath(const std::string& path) +{ + const std::string& baseTextureDir = "/textures/"; + + using namespace Nuake; + std::regex backslash_pattern("\\\\"); + for (const auto& entry : std::filesystem::recursive_directory_iterator(FileSystem::Root + baseTextureDir)) + { + // Check if the current entry is a regular file and matches the file name + const std::string stem = String::ToLower(entry.path().stem().string()); + std::string upperInput = String::ToLower(path); + + std::string prefix = ""; + if (String::BeginsWith(upperInput, "*")) + { + prefix = "star_"; + } + else if (String::BeginsWith(upperInput, "+")) + { + prefix = "plus_"; + } + else if (String::BeginsWith(upperInput, "-")) + { + prefix = "minu_"; + } + else if (String::BeginsWith(upperInput, "/")) + { + prefix = "divd_"; + } + + if (!prefix.empty()) + { + upperInput = prefix + std::string(upperInput.begin() + 1, upperInput.end()); + } + + if (entry.is_regular_file() && stem == upperInput) + { + std::filesystem::path relativePath = std::filesystem::relative(entry.path(), FileSystem::Root + "/textures/"); + std::filesystem::path pathWithoutExtension = relativePath; + pathWithoutExtension = pathWithoutExtension.parent_path(); // Remove the file name + + pathWithoutExtension /= relativePath.stem(); // Add the file name without extension + Logger::Log("Map importer found matching material: " + pathWithoutExtension.string(), "map importer", VERBOSE); + + return std::regex_replace(pathWithoutExtension.string(), backslash_pattern, "/"); + } + } + + return std::string(); +} diff --git a/Editor/src/Windows/MapImporterWindow.h b/Editor/src/Windows/MapImporterWindow.h index a000dbe1..61cbc315 100644 --- a/Editor/src/Windows/MapImporterWindow.h +++ b/Editor/src/Windows/MapImporterWindow.h @@ -14,6 +14,10 @@ public: private: Ref m_MapToImport = nullptr; - + std::vector m_DetectedWads; + std::string m_OutputFile = ""; std::vector ScanUsedWads(); + + std::string GetTransformedWadPath(const std::string& path); + }; \ No newline at end of file diff --git a/Editor/src/Windows/TrenchbroomConfiguratorWindow.cpp b/Editor/src/Windows/TrenchbroomConfiguratorWindow.cpp index 3dd5e4bf..d3c0d0fb 100644 --- a/Editor/src/Windows/TrenchbroomConfiguratorWindow.cpp +++ b/Editor/src/Windows/TrenchbroomConfiguratorWindow.cpp @@ -191,4 +191,4 @@ void TrenchbroomConfiguratorWindow::DrawPrefabItem(const Nuake::FGDPointEntity& } ImGui::PopFont(); -} \ No newline at end of file +} diff --git a/Nuake/src/Core/String.cpp b/Nuake/src/Core/String.cpp index 48630c94..45383b78 100644 --- a/Nuake/src/Core/String.cpp +++ b/Nuake/src/Core/String.cpp @@ -88,4 +88,15 @@ namespace Nuake return result; } + + std::string String::ToLower(const std::string& str) + { + std::string result = str; // Create a copy of the input string + + // Transform each character to lowercase + std::transform(result.begin(), result.end(), result.begin(), + [](unsigned char c) { return std::tolower(c); }); + + return result; + } } \ No newline at end of file diff --git a/Nuake/src/Core/String.h b/Nuake/src/Core/String.h index 8d06e3ef..d4cae7eb 100644 --- a/Nuake/src/Core/String.h +++ b/Nuake/src/Core/String.h @@ -20,5 +20,6 @@ namespace Nuake static float ToFloat(const std::string& string); static std::string ToUpper(const std::string& string); + static std::string ToLower(const std::string& string); }; } diff --git a/Nuake/src/Scene/Systems/WadConverter.cpp b/Nuake/src/Scene/Systems/WadConverter.cpp index 427bca95..8f103662 100644 --- a/Nuake/src/Scene/Systems/WadConverter.cpp +++ b/Nuake/src/Scene/Systems/WadConverter.cpp @@ -352,6 +352,7 @@ namespace Nuake ConvertedTextures = std::vector(); auto pathSplits = String::Split(std::string(wadPath.begin(), wadPath.end() - 4), '/'); + pathSplits = String::Split(pathSplits[std::size(pathSplits) - 1], '\\'); WadName = pathSplits[std::size(pathSplits) - 1]; TargetDirectory = "/textures/" + WadName + "/";