WAD Converter now creates .material file in the proper directory

This commit is contained in:
Antoine Pilote
2023-09-24 12:57:14 -04:00
parent 9ed40ca652
commit 513bf6aa0e
14 changed files with 97 additions and 34 deletions

View File

@@ -27,7 +27,7 @@ void MaterialEditor::Draw(Ref<Nuake::Material> material)
std::string fileData = material->Serialize().dump(4);
FileSystem::BeginWriteFile(FileSystem::Root + material->Path);
FileSystem::BeginWriteFile(material->Path);
FileSystem::WriteLine(fileData);
FileSystem::EndWriteFile();
}

View File

@@ -198,7 +198,7 @@ void EditorSelectionPanel::DrawMaterialPanel(Ref<Nuake::Material> material)
{
std::string fileData = material->Serialize().dump(4);
FileSystem::BeginWriteFile(FileSystem::Root + material->Path);
FileSystem::BeginWriteFile(material->Path);
FileSystem::WriteLine(fileData);
FileSystem::EndWriteFile();
}

View File

@@ -338,7 +338,7 @@ namespace Nuake
if (file->GetExtension() == ".wad")
{
if (ImGui::MenuItem("Extract to PNG"))
if (ImGui::MenuItem("Convert to Materials"))
{
Nuake::ExtractWad(file->GetAbsolutePath(), FileSystem::Root);
}
@@ -471,7 +471,7 @@ namespace Nuake
material->IsEmbedded = false;
auto jsonData = material->Serialize();
FileSystem::BeginWriteFile(finalPath);
FileSystem::BeginWriteFile(finalPath, true);
FileSystem::WriteLine(jsonData.dump(4));
FileSystem::EndWriteFile();
@@ -496,7 +496,7 @@ namespace Nuake
if(!String::IsDigit(fileName[0]))
{
FileSystem::BeginWriteFile(path);
FileSystem::BeginWriteFile(path, true);
FileSystem::WriteLine(TEMPLATE_SCRIPT_FIRST + fileName + TEMPLATE_SCRIPT_SECOND);
FileSystem::EndWriteFile();

View File

@@ -329,7 +329,7 @@ namespace Nuake
{
if (!FileSystem::FileExists(_RecentProjectFilePath, true))
{
FileSystem::BeginWriteFile(_RecentProjectFilePath);
FileSystem::BeginWriteFile(_RecentProjectFilePath, true);
FileSystem::WriteLine(_RecentProjectFileDefaultContent);
FileSystem::EndWriteFile();
}
@@ -358,7 +358,7 @@ namespace Nuake
j["Projects"][i] = project.Serialize();
}
FileSystem::BeginWriteFile(_RecentProjectFilePath);
FileSystem::BeginWriteFile(_RecentProjectFilePath, true);
FileSystem::WriteLine(j.dump(4));
FileSystem::EndWriteFile();
}

View File

@@ -84,12 +84,17 @@ namespace Nuake
}
}
bool FileSystem::DirectoryExists(const std::string path)
bool FileSystem::DirectoryExists(const std::string& path)
{
return false;
return std::filesystem::exists(path) && std::filesystem::is_directory(path);
}
bool FileSystem::FileExists(const std::string path, bool absolute)
bool FileSystem::MakeDirectory(const std::string& path, bool absolute)
{
return std::filesystem::create_directory(absolute ? path : FileSystem::Root + path);
}
bool FileSystem::FileExists(const std::string& path, bool absolute)
{
std::string fullPath = absolute ? path : FileSystem::Root + path;
@@ -154,10 +159,10 @@ namespace Nuake
}
std::ofstream FileSystem::fileWriter;
bool FileSystem::BeginWriteFile(const std::string path)
bool FileSystem::BeginWriteFile(const std::string path, bool absolute)
{
fileWriter = std::ofstream();
fileWriter.open(path);
fileWriter.open(absolute ? path : FileSystem::Root + path);
return false;
}

View File

@@ -38,13 +38,14 @@ namespace Nuake
static void ScanDirectory(Ref<Directory> directory);
static void GetDirectories();
static bool DirectoryExists(const std::string path);
static bool FileExists(const std::string path, bool absolute = false);
static bool MakeDirectory(const std::string& path, bool absolute = false);
static bool DirectoryExists(const std::string& path);
static bool FileExists(const std::string& path, bool absolute = false);
static std::string ReadFile(const std::string& path, bool absolute = false);
static std::ofstream fileWriter;
static bool BeginWriteFile(const std::string path);
static bool BeginWriteFile(const std::string path, bool absolute = false);
static bool WriteLine(const std::string line);
static void EndWriteFile();
static uintmax_t DeleteFileFromPath(const std::string& path);

View File

@@ -11,6 +11,7 @@
#include "src/Rendering/Buffers/VertexBufferLayout.h"
#include <future>
#include <src/Resource/ResourceLoader.h>
namespace Nuake
{
@@ -153,8 +154,23 @@ namespace Nuake
bool Mesh::Deserialize(const json& j)
{
m_Material = CreateRef<Material>();
m_Material->Deserialize(j["Material"]);
bool loadedMaterialFile = false;
if (j["Material"].contains("Path"))
{
const std::string materialPath = j["Material"]["Path"];
if (!materialPath.empty())
{
Ref<Material> newMaterial = ResourceLoader::LoadMaterial(materialPath);
m_Material = newMaterial;
loadedMaterialFile = true;
}
}
if(!loadedMaterialFile)
{
m_Material = CreateRef<Material>();
m_Material->Deserialize(j["Material"]);
}
m_Indices.reserve(j["Indices"].size());
for (auto& i : j["Indices"])

View File

@@ -33,7 +33,7 @@ namespace Nuake {
bool FGDFile::Save()
{
// Write to file.
FileSystem::BeginWriteFile(FileSystem::Root + Path);
FileSystem::BeginWriteFile(Path);
FileSystem::WriteLine(Serialize().dump(4));
FileSystem::EndWriteFile();

View File

@@ -4,7 +4,7 @@
namespace Nuake {
bool FGDSerializer::BeginFGDFile(const std::string path)
{
FileSystem::BeginWriteFile(path);
FileSystem::BeginWriteFile(path, true);
return true;
}

View File

@@ -33,7 +33,7 @@ namespace Nuake {
{
Path = path;
FileSystem::BeginWriteFile(path);
FileSystem::BeginWriteFile(path, true);
FileSystem::WriteLine(Serialize().dump(4));
FileSystem::EndWriteFile();
}

View File

@@ -2,6 +2,7 @@
#include "../Core/Input.h"
#include <GLFW/glfw3.h>
#include <glm/trigonometric.hpp>
#include "src/Core/Logger.h"
namespace Nuake

View File

@@ -357,7 +357,7 @@ namespace Nuake
{
std::string fileContent = Serialize().dump(4);
FileSystem::BeginWriteFile(FileSystem::Root + path);
FileSystem::BeginWriteFile(path);
FileSystem::WriteLine(fileContent);
FileSystem::EndWriteFile();

View File

@@ -6,10 +6,14 @@
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "src/Vendors/stb_image/stb_image_write.h"
#include <filesystem>
#include <src/Rendering/Textures/Material.h>
namespace Nuake
{
std::string TargetDirectory = "";
std::string WadName = "";
std::vector<std::string> ConvertedTextures;
unsigned char host_quakepal[768] =
{
@@ -297,22 +301,26 @@ namespace Nuake
// if the image contains fullbright pixels, the output filename
// will be given the '_fbr' prefix.
bool fullbright = false;
std::vector<uint32_t> textureData;
textureData.resize(width * height);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
unsigned char pix = pixels[y * width + x];
textureData[y * width + x] = COL_ReadPalette(pix);
}
}
const std::string finalFilePath = TargetDirectory + ExpandFileName(lump_name, false);
stbi_write_png(finalFilePath.c_str(), width, height, 4, textureData.data(), width * 4);
const std::string lumpName = ExpandFileName(lump_name, false);
const std::string finalFilePath = TargetDirectory + lumpName;
stbi_write_png((FileSystem::Root + finalFilePath).c_str(), width, height, 4, textureData.data(), width * 4);
delete[] pixels;
ConvertedTextures.push_back(std::string(lumpName));
return true;
}
@@ -331,13 +339,16 @@ namespace Nuake
return;
}
auto pathSplits = String::Split(std::string(wadPath.begin(), wadPath.end() - 4), '\\');
std::string wadName = pathSplits[std::size(pathSplits) - 1];
TargetDirectory = targetDirectory + "/textures/" + wadName + "/";
ConvertedTextures = std::vector<std::string>();
if (!std::filesystem::exists(TargetDirectory))
auto pathSplits = String::Split(std::string(wadPath.begin(), wadPath.end() - 4), '\\');
WadName = pathSplits[std::size(pathSplits) - 1];
TargetDirectory = "/textures/" + WadName + "/";
if (const std::string absoluteDirPath = FileSystem::RelativeToAbsolute(TargetDirectory);
!FileSystem::DirectoryExists(absoluteDirPath))
{
std::filesystem::create_directory(TargetDirectory);
FileSystem::MakeDirectory(absoluteDirPath);
}
WadOpenRead(wadPath);
@@ -396,6 +407,33 @@ namespace Nuake
printf("Extracted %d entries, with %d failures\n",
num_lumps - failures - skipped, failures);
CreateMaterials();
ConvertedTextures.clear();
}
void CreateMaterials()
{
const std::string& materialFolderPath = "/Materials/" + WadName + "/";
if(!FileSystem::DirectoryExists(materialFolderPath))
{
FileSystem::MakeDirectory(materialFolderPath);
}
for (auto& t : ConvertedTextures)
{
Ref<Material> material = CreateRef<Material>();
material->IsEmbedded = false;
material->SetAlbedo(TextureManager::Get()->GetTexture(FileSystem::RelativeToAbsolute(TargetDirectory + t)));
auto jsonData = material->Serialize();
const std::string materialFilePath = materialFolderPath + std::string(t.begin(), t.end() - 4) + ".material";
FileSystem::BeginWriteFile(materialFilePath);
FileSystem::WriteLine(jsonData.dump(4));
FileSystem::EndWriteFile();
}
}
void WadOpenRead(const std::string& filename)

View File

@@ -1,15 +1,15 @@
#pragma once
#include <string>
#include <vector>
namespace Nuake
{
#define MAKE_RGB(r,g,b) (uint32_t)(((r) << 16) | ((g) << 8) | (b) | (255<<24))
// John Carmack said the quake palette.lmp can be considered public domain because it is not an important asset to id, so I include it here as a fallback if no external palette file is found.
// John Carmack said the quake palette.lmp can be considered public domain because it is not an important asset to id, so I include it here as a fallback if no external palette file is found.
#define CMP_LZSS 1
#define CMP_NONE 0
#define TYP_LABEL 1
@@ -20,7 +20,6 @@ namespace Nuake
#define TYP_QPIC 66
#define TYP_QTEX 65
#define TYP_SOUND 67
#define MIP_LEVELS 4
typedef struct miptex_s
{
@@ -83,6 +82,8 @@ namespace Nuake
}
pic_header_t;
int WAD2_EntryType(int entry);
bool WAD2_ReadData(int entry, int offset, int length, void *buffer);
void ExtractWad(const std::string& wadFilePath, const std::string& outputDir);
@@ -90,4 +91,5 @@ namespace Nuake
bool MIP_ExtractPicture(int entry, const char *lump_name);
void WAD2_CloseRead(void);
void WadOpenRead(const std::string& filename);
void CreateMaterials();
}