Map converter now works 📦

This commit is contained in:
Antoine Pilote
2024-07-28 17:09:28 -04:00
parent d33c4f33df
commit 7c7cb0482b
6 changed files with 144 additions and 4 deletions

View File

@@ -4,6 +4,7 @@
#include <src/UI/ImUI.h>
#include <src/Core/Logger.h>
#include <regex>
#include <src/Threading/JobSystem.h>
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<std::string> MapImporterWindow::ScanUsedWads()
return std::vector<std::string>();
}
m_DetectedWads.clear();
using namespace Nuake;
const std::string mapContent = m_MapToImport->Read();
@@ -104,4 +176,55 @@ std::vector<std::string> MapImporterWindow::ScanUsedWads()
// If no match is found, return an empty string
return usedWads;
}
}
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();
}

View File

@@ -14,6 +14,10 @@ public:
private:
Ref<Nuake::File> m_MapToImport = nullptr;
std::vector<std::string> m_DetectedWads;
std::string m_OutputFile = "";
std::vector<std::string> ScanUsedWads();
std::string GetTransformedWadPath(const std::string& path);
};

View File

@@ -191,4 +191,4 @@ void TrenchbroomConfiguratorWindow::DrawPrefabItem(const Nuake::FGDPointEntity&
}
ImGui::PopFont();
}
}

View File

@@ -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;
}
}

View File

@@ -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);
};
}

View File

@@ -352,6 +352,7 @@ namespace Nuake
ConvertedTextures = std::vector<ConvertedTexture>();
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 + "/";