diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/jsonproc/Makefile | 2 | ||||
| -rw-r--r-- | tools/knarc/.gitignore | 1 | ||||
| -rw-r--r-- | tools/knarc/Makefile | 15 | ||||
| -rw-r--r-- | tools/knarc/Narc.cpp | 543 | ||||
| -rw-r--r-- | tools/knarc/Narc.h | 93 | ||||
| -rw-r--r-- | tools/knarc/Source.cpp | 108 | ||||
| -rw-r--r-- | tools/narccomp/.gitignore | 1 | ||||
| -rw-r--r-- | tools/narccomp/Makefile | 1 | ||||
| -rw-r--r-- | tools/nitrogfx/gfx.c | 91 | ||||
| -rw-r--r-- | tools/nitrogfx/gfx.h | 3 | ||||
| -rw-r--r-- | tools/nitrogfx/jasc_pal.c | 7 | ||||
| -rw-r--r-- | tools/nitrogfx/main.c | 53 | ||||
| -rw-r--r-- | tools/nitrogfx/util.c | 24 | ||||
| -rw-r--r-- | tools/nitrogfx/util.h | 2 | ||||
| -rw-r--r-- | tools/scaninc/Makefile | 2 |
15 files changed, 941 insertions, 5 deletions
diff --git a/tools/jsonproc/Makefile b/tools/jsonproc/Makefile index 721da102..622f00ab 100644 --- a/tools/jsonproc/Makefile +++ b/tools/jsonproc/Makefile @@ -1,5 +1,3 @@ -CXX := g++ - CXXFLAGS := -Wall -std=c++11 -O2 INCLUDES := -I . diff --git a/tools/knarc/.gitignore b/tools/knarc/.gitignore new file mode 100644 index 00000000..20bde9af --- /dev/null +++ b/tools/knarc/.gitignore @@ -0,0 +1 @@ +knarc diff --git a/tools/knarc/Makefile b/tools/knarc/Makefile new file mode 100644 index 00000000..62af834f --- /dev/null +++ b/tools/knarc/Makefile @@ -0,0 +1,15 @@ +CXXFLAGS := -std=c++17 -O2 -Wall -Wno-switch -lstdc++fs + +SRCS := Source.cpp Narc.cpp +HEADERS := Narc.h + +.PHONY: all clean + +all: knarc + @: + +knarc: $(SRCS) $(HEADERS) + $(CXX) $(SRCS) -o $@ $(LDFLAGS) $(CXXFLAGS) + +clean: + $(RM) knarc knarc.exe diff --git a/tools/knarc/Narc.cpp b/tools/knarc/Narc.cpp new file mode 100644 index 00000000..506e050f --- /dev/null +++ b/tools/knarc/Narc.cpp @@ -0,0 +1,543 @@ +#include "Narc.h" + +#include <algorithm> +#include <cstddef> +#include <cstdint> +#include <fstream> +#include <iomanip> +#include <ios> +#include <map> +#include <memory> +#include <regex> +#include <sstream> +#include <stack> +#include <string> +#include <vector> + +#if __GNUC__ <= 7 +#include <experimental/filesystem> +namespace fs = std::experimental::filesystem; +#else +#include <filesystem> +namespace fs = std::filesystem; +#endif + +using namespace std; + +void Narc::AlignDword(ofstream& ofs, uint8_t paddingChar) +{ + if ((ofs.tellp() % 4) != 0) + { + for (int i = 4 - (ofs.tellp() % 4); i-- > 0; ) + { + ofs.write(reinterpret_cast<char*>(&paddingChar), sizeof(uint8_t)); + } + } +} + +bool Narc::Cleanup(ifstream& ifs, const NarcError& e) +{ + ifs.close(); + + error = e; + + return false; +} + +bool Narc::Cleanup(ofstream& ofs, const NarcError& e) +{ + ofs.close(); + + error = e; + + return false; +} + +vector<fs::directory_entry> Narc::OrderedDirectoryIterator(const fs::path& path, bool recursive) const +{ + vector<fs::directory_entry> v; + + for (const auto& de : fs::directory_iterator(path)) + { + v.push_back(de); + } + + sort(v.begin(), v.end(), [](const fs::directory_entry& a, const fs::directory_entry& b) + { + // I fucking hate C++ + string aStr = a.path().filename().string(); + string bStr = b.path().filename().string(); + + for (size_t i = 0; i < aStr.size(); ++i) + { + aStr[i] = tolower(aStr[i]); + } + + for (size_t i = 0; i < bStr.size(); ++i) + { + bStr[i] = tolower(bStr[i]); + } + + return aStr < bStr; + }); + + if (recursive) + { + size_t vSize = v.size(); + + for (size_t i = 0; i < vSize; ++i) + { + if (is_directory(v[i])) + { + vector<fs::directory_entry> temp = OrderedDirectoryIterator(v[i], true); + + v.insert(v.end(), temp.begin(), temp.end()); + } + } + } + + return v; +} + +NarcError Narc::GetError() const +{ + return error; +} + +bool Narc::Pack(const fs::path& fileName, const fs::path& directory) +{ + ofstream ofs(fileName, ios::binary); + + if (!ofs.good()) { return Cleanup(ofs, NarcError::InvalidOutputFile); } + + vector<FileAllocationTableEntry> fatEntries; + uint16_t directoryCounter = 1; + + for (const auto& de : OrderedDirectoryIterator(directory, true)) + { + if (is_directory(de)) + { + ++directoryCounter; + } + else + { + fatEntries.push_back(FileAllocationTableEntry + { + .Start = 0x0, + .End = 0x0 + }); + + if (fatEntries.size() > 1) + { + fatEntries.back().Start = fatEntries.rbegin()[1].End; + + if ((fatEntries.rbegin()[1].End % 4) != 0) + { + fatEntries.back().Start += 4 - (fatEntries.rbegin()[1].End % 4); + } + } + + fatEntries.back().End = fatEntries.back().Start + static_cast<uint32_t>(file_size(de)); + } + } + + FileAllocationTable fat + { + .Id = 0x46415442, + .ChunkSize = sizeof(FileAllocationTable) + ((uint32_t)fatEntries.size() * sizeof(FileAllocationTableEntry)), + .FileCount = static_cast<uint16_t>(fatEntries.size()), + .Reserved = 0x0 + }; + + map<fs::path, string> subTables; + vector<fs::path> paths; + + directoryCounter = 0; + + for (const auto& de : OrderedDirectoryIterator(directory, true)) + { + if (!subTables.count(de.path().parent_path())) + { + subTables.insert({ de.path().parent_path(), "" }); + paths.push_back(de.path().parent_path()); + } + + if (is_directory(de)) + { + ++directoryCounter; + + subTables[de.path().parent_path()] += static_cast<uint8_t>(0x80 + de.path().filename().string().size()); + subTables[de.path().parent_path()] += de.path().filename().string(); + subTables[de.path().parent_path()] += (0xF000 + directoryCounter) & 0xFF; + subTables[de.path().parent_path()] += (0xF000 + directoryCounter) >> 8; + } + else + { + subTables[de.path().parent_path()] += static_cast<uint8_t>(de.path().filename().string().size()); + subTables[de.path().parent_path()] += de.path().filename().string(); + } + } + + for (auto& subTable : subTables) + { + subTable.second += '\0'; + } + + vector<FileNameTableEntry> fntEntries; + + if (!regex_match(fs::directory_iterator(directory)->path().string(), regex(".*_\\d{4,8}\\.bin"))) + { + fntEntries.push_back( + { + .Offset = (directoryCounter + 1) * sizeof(FileNameTableEntry), + .FirstFileId = 0x0, + .Utility = static_cast<uint16_t>(directoryCounter + 1) + }); + + for (uint16_t i = 0; i < directoryCounter; ++i) + { + fntEntries.push_back( + { + .Offset = fntEntries.back().Offset + subTables[paths[i]].size(), + .FirstFileId = fntEntries.back().FirstFileId, + .Utility = 0x0 + }); + + for (size_t j = 0; j < (subTables[paths[i]].size() - 1); ++j) + { + if (static_cast<uint8_t>(subTables[paths[i]][j]) <= 0x7F) + { + j += static_cast<uint8_t>(subTables[paths[i]][j]); + ++fntEntries.back().FirstFileId; + } + else if (static_cast<uint8_t>(subTables[paths[i]][j]) <= 0xFF) + { + j += static_cast<uint8_t>(subTables[paths[i]][j]) - 0x80 + 0x2; + } + } + + fntEntries.back().Utility = 0xF000 + (find(paths.begin(), paths.end(), paths[i + 1].parent_path()) - paths.begin()); + } + } + else + { + fntEntries.push_back( + { + .Offset = 0x4, + .FirstFileId = 0x0, + .Utility = 0x1 + }); + } + + FileNameTable fnt + { + .Id = 0x464E5442, + .ChunkSize = sizeof(FileNameTable) + (fntEntries.size() * sizeof(FileNameTableEntry)) + }; + + if (!regex_match(fs::directory_iterator(directory)->path().string(), regex(".*_\\d{4,8}\\.bin"))) + { + for (const auto& subTable : subTables) + { + fnt.ChunkSize += subTable.second.size(); + } + } + + if ((fnt.ChunkSize % 4) != 0) + { + fnt.ChunkSize += 4 - (fnt.ChunkSize % 4); + } + + FileImages fi + { + .Id = 0x46494D47, + .ChunkSize = sizeof(FileImages) + fatEntries.back().End + }; + + if ((fi.ChunkSize % 4) != 0) + { + fi.ChunkSize += 4 - (fi.ChunkSize % 4); + } + + Header header + { + .Id = 0x4352414E, + .ByteOrderMark = 0xFFFE, + .Version = 0x100, + .FileSize = sizeof(Header) + fat.ChunkSize + fnt.ChunkSize + fi.ChunkSize, + .ChunkSize = sizeof(Header), + .ChunkCount = 0x3 + }; + + ofs.write(reinterpret_cast<char*>(&header), sizeof(Header)); + ofs.write(reinterpret_cast<char*>(&fat), sizeof(FileAllocationTable)); + + for (auto& entry : fatEntries) + { + ofs.write(reinterpret_cast<char*>(&entry), sizeof(FileAllocationTableEntry)); + } + + ofs.write(reinterpret_cast<char*>(&fnt), sizeof(FileNameTable)); + + for (auto& entry : fntEntries) + { + ofs.write(reinterpret_cast<char*>(&entry), sizeof(FileNameTableEntry)); + } + + if (!regex_match(fs::directory_iterator(directory)->path().string(), regex(".*_\\d{4,8}\\.bin"))) + { + for (const auto& path : paths) + { + ofs << subTables[path]; + } + } + + AlignDword(ofs, 0xFF); + + ofs.write(reinterpret_cast<char*>(&fi), sizeof(FileImages)); + + for (const auto& de : OrderedDirectoryIterator(directory, true)) + { + if (is_directory(de)) + { + continue; + } + + ifstream ifs(de.path(), ios::binary | ios::ate); + + if (!ifs.good()) + { + ifs.close(); + + return Cleanup(ofs, NarcError::InvalidInputFile); + } + + streampos length = ifs.tellg(); + unique_ptr<char[]> buffer = make_unique<char[]>(static_cast<unsigned int>(length)); + + ifs.seekg(0); + ifs.read(buffer.get(), length); + ifs.close(); + + ofs.write(buffer.get(), length); + + AlignDword(ofs, 0xFF); + } + + ofs.close(); + + return error == NarcError::None ? true : false; +} + +bool Narc::Unpack(const fs::path& fileName, const fs::path& directory) +{ + ifstream ifs(fileName, ios::binary); + + if (!ifs.good()) { return Cleanup(ifs, NarcError::InvalidInputFile); } + + Header header; + ifs.read(reinterpret_cast<char*>(&header), sizeof(Header)); + + if (header.Id != 0x4352414E) { return Cleanup(ifs, NarcError::InvalidHeaderId); } + if (header.ByteOrderMark != 0xFFFE) { return Cleanup(ifs, NarcError::InvalidByteOrderMark); } + if ((header.Version != 0x0100) && (header.Version != 0x0000)) { return Cleanup(ifs, NarcError::InvalidVersion); } + if (header.ChunkSize != 0x10) { return Cleanup(ifs, NarcError::InvalidHeaderSize); } + if (header.ChunkCount != 0x3) { return Cleanup(ifs, NarcError::InvalidChunkCount); } + + FileAllocationTable fat; + ifs.read(reinterpret_cast<char*>(&fat), sizeof(FileAllocationTable)); + + if (fat.Id != 0x46415442) { return Cleanup(ifs, NarcError::InvalidFileAllocationTableId); } + if (fat.Reserved != 0x0) { return Cleanup(ifs, NarcError::InvalidFileAllocationTableReserved); } + + unique_ptr<FileAllocationTableEntry[]> fatEntries = make_unique<FileAllocationTableEntry[]>(fat.FileCount); + + for (uint16_t i = 0; i < fat.FileCount; ++i) + { + ifs.read(reinterpret_cast<char*>(&fatEntries.get()[i]), sizeof(FileAllocationTableEntry)); + } + + FileNameTable fnt; + vector<FileNameTableEntry> FileNameTableEntries; + ifs.read(reinterpret_cast<char*>(&fnt), sizeof(FileNameTable)); + + if (fnt.Id != 0x464E5442) { return Cleanup(ifs, NarcError::InvalidFileNameTableId); } + + vector<FileNameTableEntry> fntEntries; + + do + { + fntEntries.push_back(FileNameTableEntry()); + + ifs.read(reinterpret_cast<char*>(&fntEntries.back().Offset), sizeof(uint32_t)); + ifs.read(reinterpret_cast<char*>(&fntEntries.back().FirstFileId), sizeof(uint16_t)); + ifs.read(reinterpret_cast<char*>(&fntEntries.back().Utility), sizeof(uint16_t)); + } while (static_cast<uint32_t>(ifs.tellg()) < (header.ChunkSize + fat.ChunkSize + sizeof(FileNameTable) + fntEntries[0].Offset)); + + unique_ptr<string[]> fileNames = make_unique<string[]>(0xFFFF); + + for (size_t i = 0; i < fntEntries.size(); ++i) + { + ifs.seekg(static_cast<uint64_t>(header.ChunkSize) + fat.ChunkSize + sizeof(FileNameTable) + fntEntries[i].Offset); + + uint16_t fileId = 0x0000; + + for (uint8_t length = 0x80; length != 0x00; ifs.read(reinterpret_cast<char*>(&length), sizeof(uint8_t))) + { + if (length <= 0x7F) + { + for (uint8_t j = 0; j < length; ++j) + { + uint8_t c; + ifs.read(reinterpret_cast<char*>(&c), sizeof(uint8_t)); + + fileNames.get()[fntEntries[i].FirstFileId + fileId] += c; + } + + ++fileId; + } + else if (length == 0x80) + { + // Reserved + } + else if (length <= 0xFF) + { + length -= 0x80; + string directoryName; + + for (uint8_t j = 0; j < length; ++j) + { + uint8_t c; + ifs.read(reinterpret_cast<char*>(&c), sizeof(uint8_t)); + + directoryName += c; + } + + uint16_t directoryId; + ifs.read(reinterpret_cast<char*>(&directoryId), sizeof(uint16_t)); + + fileNames.get()[directoryId] = directoryName; + } + else + { + return Cleanup(ifs, NarcError::InvalidFileNameTableEntryId); + } + } + } + + if ((ifs.tellg() % 4) != 0) + { + ifs.seekg(4 - (ifs.tellg() % 4), ios::cur); + } + + FileImages fi; + ifs.read(reinterpret_cast<char*>(&fi), sizeof(FileImages)); + + if (fi.Id != 0x46494D47) { return Cleanup(ifs, NarcError::InvalidFileImagesId); } + + fs::create_directory(directory); + fs::current_path(directory); + + if (fnt.ChunkSize == 0x10) + { + for (uint16_t i = 0; i < fat.FileCount; ++i) + { + ifs.seekg(static_cast<uint64_t>(header.ChunkSize) + fat.ChunkSize + fnt.ChunkSize + 8 + fatEntries.get()[i].Start); + + unique_ptr<char[]> buffer = make_unique<char[]>(fatEntries.get()[i].End - fatEntries.get()[i].Start); + ifs.read(buffer.get(), fatEntries.get()[i].End - fatEntries.get()[i].Start); + + ostringstream oss; + oss << fileName.stem().string() << "_" << setfill('0') << setw(8) << i << ".bin"; + + ofstream ofs(oss.str(), ios::binary); + + if (!ofs.good()) + { + ofs.close(); + + return Cleanup(ifs, NarcError::InvalidOutputFile); + } + + ofs.write(buffer.get(), fatEntries.get()[i].End - fatEntries.get()[i].Start); + ofs.close(); + } + } + else + { + fs::path absolutePath = fs::absolute(fs::current_path()); + + for (size_t i = 0; i < fntEntries.size(); ++i) + { + fs::current_path(absolutePath); + stack<string> directories; + + for (uint16_t j = fntEntries[i].Utility; j > 0xF000; j = fntEntries[j - 0xF000].Utility) + { + directories.push(fileNames.get()[j]); + } + + for (; !directories.empty(); directories.pop()) + { + fs::create_directory(directories.top()); + fs::current_path(directories.top()); + } + + if (fntEntries[i].Utility >= 0xF000) + { + fs::create_directory(fileNames.get()[0xF000 + i]); + fs::current_path(fileNames.get()[0xF000 + i]); + } + + ifs.seekg(static_cast<uint64_t>(header.ChunkSize) + fat.ChunkSize + sizeof(FileNameTable) + fntEntries[i].Offset); + + uint16_t fileId = 0x0000; + + for (uint8_t length = 0x80; length != 0x00; ifs.read(reinterpret_cast<char*>(&length), sizeof(uint8_t))) + { + if (length <= 0x7F) + { + streampos savedPosition = ifs.tellg(); + + ifs.seekg(static_cast<uint64_t>(header.ChunkSize) + fat.ChunkSize + fnt.ChunkSize + 8 + fatEntries.get()[fntEntries[i].FirstFileId + fileId].Start); + + unique_ptr<char[]> buffer = make_unique<char[]>(fatEntries.get()[fntEntries[i].FirstFileId + fileId].End - fatEntries.get()[fntEntries[i].FirstFileId + fileId].Start); + ifs.read(buffer.get(), fatEntries.get()[fntEntries[i].FirstFileId + fileId].End - fatEntries.get()[fntEntries[i].FirstFileId + fileId].Start); + + ofstream ofs(fileNames.get()[fntEntries[i].FirstFileId + fileId], ios::binary); + + if (!ofs.good()) + { + ofs.close(); + + return Cleanup(ifs, NarcError::InvalidOutputFile); + } + + ofs.write(buffer.get(), fatEntries.get()[fntEntries[i].FirstFileId + fileId].End - fatEntries.get()[fntEntries[i].FirstFileId + fileId].Start); + ofs.close(); + + ifs.seekg(savedPosition); + ifs.seekg(length, ios::cur); + + ++fileId; + } + else if (length == 0x80) + { + // Reserved + } + else if (length <= 0xFF) + { + ifs.seekg(static_cast<uint64_t>(length) - 0x80 + 0x2, ios::cur); + } + else + { + return Cleanup(ifs, NarcError::InvalidFileNameTableEntryId); + } + } + } + } + + ifs.close(); + + return error == NarcError::None ? true : false; +} diff --git a/tools/knarc/Narc.h b/tools/knarc/Narc.h new file mode 100644 index 00000000..4516d2d5 --- /dev/null +++ b/tools/knarc/Narc.h @@ -0,0 +1,93 @@ +#pragma once + +#include <cstdint> +#include <fstream> +#include <string> +#include <vector> + +#if __GNUC__ <= 7 +#include <experimental/filesystem> +namespace fs = std::experimental::filesystem; +#else +#include <filesystem> +namespace fs = std::filesystem; +#endif + +enum class NarcError +{ + None, + InvalidInputFile, + InvalidHeaderId, + InvalidByteOrderMark, + InvalidVersion, + InvalidHeaderSize, + InvalidChunkCount, + InvalidFileAllocationTableId, + InvalidFileAllocationTableReserved, + InvalidFileNameTableId, + InvalidFileNameTableEntryId, + InvalidFileImagesId, + InvalidOutputFile +}; + +struct Header +{ + uint32_t Id; + uint16_t ByteOrderMark; + uint16_t Version; + uint32_t FileSize; + uint16_t ChunkSize; + uint16_t ChunkCount; +}; + +struct FileAllocationTable +{ + uint32_t Id; + uint32_t ChunkSize; + uint16_t FileCount; + uint16_t Reserved; +}; + +struct FileAllocationTableEntry +{ + uint32_t Start; + uint32_t End; +}; + +struct FileNameTable +{ + uint32_t Id; + uint32_t ChunkSize; +}; + +struct FileNameTableEntry +{ + uint32_t Offset; + uint16_t FirstFileId; + uint16_t Utility; +}; + +struct FileImages +{ + uint32_t Id; + uint32_t ChunkSize; +}; + +class Narc +{ + public: + NarcError GetError() const; + + bool Pack(const fs::path& fileName, const fs::path& directory); + bool Unpack(const fs::path& fileName, const fs::path& directory); + + private: + NarcError error = NarcError::None; + + void AlignDword(std::ofstream& ofs, uint8_t paddingChar); + + bool Cleanup(std::ifstream& ifs, const NarcError& e); + bool Cleanup(std::ofstream& ofs, const NarcError& e); + + std::vector<fs::directory_entry> OrderedDirectoryIterator(const fs::path& path, bool recursive) const; +}; diff --git a/tools/knarc/Source.cpp b/tools/knarc/Source.cpp new file mode 100644 index 00000000..51bb5320 --- /dev/null +++ b/tools/knarc/Source.cpp @@ -0,0 +1,108 @@ +#include <cstring> +#include <iostream> +#include <string> + +#include "Narc.h" + +using namespace std; + +void PrintError(NarcError error) +{ + switch (error) + { + case NarcError::None: cout << "ERROR: No error???" << endl; break; + case NarcError::InvalidInputFile: cout << "ERROR: Invalid input file" << endl; break; + case NarcError::InvalidHeaderId: cout << "ERROR: Invalid header ID" << endl; break; + case NarcError::InvalidByteOrderMark: cout << "ERROR: Invalid byte order mark" << endl; break; + case NarcError::InvalidVersion: cout << "ERROR: Invalid NARC version" << endl; break; + case NarcError::InvalidHeaderSize: cout << "ERROR: Invalid header size" << endl; break; + case NarcError::InvalidChunkCount: cout << "ERROR: Invalid chunk count" << endl; break; + case NarcError::InvalidFileAllocationTableId: cout << "ERROR: Invalid file allocation table ID" << endl; break; + case NarcError::InvalidFileAllocationTableReserved: cout << "ERROR: Invalid file allocation table reserved section" << endl; break; + case NarcError::InvalidFileNameTableId: cout << "ERROR: Invalid file name table ID" << endl; break; + case NarcError::InvalidFileNameTableEntryId: cout << "ERROR: Invalid file name table entry ID" << endl; break; + case NarcError::InvalidFileImagesId: cout << "ERROR: Invalid file images ID" << endl; break; + case NarcError::InvalidOutputFile: cout << "ERROR: Invalid output file" << endl; break; + default: cout << "ERROR: Unknown error???" << endl; break; + } +} + +int main(int argc, char* argv[]) +{ + if (argc != 5) + { + cout << "OVERVIEW: Knarc" << endl << endl; + cout << "USAGE: knarc [options] <inputs>" << endl << endl; + cout << "OPTIONS:" << endl; + cout << "\t-d\tDirectory to pack from/unpack to" << endl; + cout << "\t-p\tPack" << endl; + cout << "\t-u\tUnpack" << endl; + + return 1; + } + + string directory = ""; + string fileName = ""; + bool pack = false; + + for (int i = 1; i < argc; ++i) + { + if (!strcmp(argv[i], "-d")) + { + if (i == (argc - 1)) + { + cerr << "ERROR: No directory specified" << endl; + + return 1; + } + + directory = argv[++i]; + } + else if (!strcmp(argv[i], "-p")) + { + if (i == (argc - 1)) + { + cerr << "ERROR: No NARC specified to pack to" << endl; + + return 1; + } + + fileName = argv[++i]; + pack = true; + } + else if (!strcmp(argv[i], "-u")) + { + if (i == (argc - 1)) + { + cerr << "ERROR: No NARC specified to unpack from" << endl; + + return 1; + } + + fileName = argv[++i]; + } + } + + Narc narc; + + if (pack) + { + if (!narc.Pack(fileName, directory)) + { + PrintError(narc.GetError()); + + return 1; + } + } + else + { + if (!narc.Unpack(fileName, directory)) + { + PrintError(narc.GetError()); + + return 1; + } + } + + return 0; +} diff --git a/tools/narccomp/.gitignore b/tools/narccomp/.gitignore new file mode 100644 index 00000000..e96a03a5 --- /dev/null +++ b/tools/narccomp/.gitignore @@ -0,0 +1 @@ +narccomp diff --git a/tools/narccomp/Makefile b/tools/narccomp/Makefile index b484e7a1..6b404f32 100644 --- a/tools/narccomp/Makefile +++ b/tools/narccomp/Makefile @@ -1,4 +1,3 @@ -CXX = g++ CXXFLAGS := -O3 -std=c++11 .PHONY: all clean diff --git a/tools/nitrogfx/gfx.c b/tools/nitrogfx/gfx.c index f927deed..ac156575 100644 --- a/tools/nitrogfx/gfx.c +++ b/tools/nitrogfx/gfx.c @@ -1,5 +1,6 @@ // Copyright (c) 2015 YamaArashi +#include <string.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> @@ -322,6 +323,41 @@ void ReadGbaPalette(char *path, struct Palette *palette) free(data); } +void ReadNtrPalette(char *path, struct Palette *palette) +{ + int fileSize; + unsigned char *data = ReadWholeFile(path, &fileSize); + + if (memcmp(data, "RLCN", 4) != 0 && memcmp(data, "RPCN", 4) != 0) //NCLR / NCPR + { + FATAL_ERROR("Not a valid NCLR or NCPR palette file.\n"); + } + + unsigned char *paletteHeader = data + 0x10; + + if (memcmp(paletteHeader, "TTLP", 4) != 0) + { + FATAL_ERROR("No valid PLTT file after NCLR header.\n"); + } + + if ((fileSize - 0x28) % 2 != 0) + FATAL_ERROR("The file size (%d) is not a multiple of 2.\n", fileSize); + + palette->numColors = (fileSize - 0x28) / 2; //remove header and divide by 2 + + unsigned char *paletteData = paletteHeader + 0x18; + + for (int i = 0; i < palette->numColors; i++) + { + uint16_t paletteEntry = (paletteData[i * 2 + 1] << 8) | paletteData[i * 2]; + palette->colors[i].red = UPCONVERT_BIT_DEPTH(GET_GBA_PAL_RED(paletteEntry)); + palette->colors[i].green = UPCONVERT_BIT_DEPTH(GET_GBA_PAL_GREEN(paletteEntry)); + palette->colors[i].blue = UPCONVERT_BIT_DEPTH(GET_GBA_PAL_BLUE(paletteEntry)); + } + + free(data); +} + void WriteGbaPalette(char *path, struct Palette *palette) { FILE *fp = fopen(path, "wb"); @@ -342,3 +378,58 @@ void WriteGbaPalette(char *path, struct Palette *palette) fclose(fp); } + +void WriteNtrPalette(char *path, struct Palette *palette, bool ncpr) +{ + FILE *fp = fopen(path, "wb"); + + if (fp == NULL) + FATAL_ERROR("Failed to open \"%s\" for writing.\n", path); + + uint32_t size = palette->numColors * 2; + uint32_t extSize = size + (ncpr ? 0x10 : 0x18); + + //NCLR header + WriteGenericNtrHeader(fp, (ncpr ? "RPCN" : "RLCN"), extSize, !ncpr); + + unsigned char palHeader[0x18] = + { + 0x54, 0x54, 0x4C, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00 + }; + + //section size + palHeader[4] = extSize & 0xFF; + palHeader[5] = (extSize >> 8) & 0xFF; + palHeader[6] = (extSize >> 16) & 0xFF; + palHeader[7] = (extSize >> 24) & 0xFF; + + //bit depth + palHeader[8] = palette->bitDepth == 4 ? 0x03: 0x04; + + //size + palHeader[16] = size & 0xFF; + palHeader[17] = (size >> 8) & 0xFF; + palHeader[18] = (size >> 16) & 0xFF; + palHeader[19] = (size >> 24) & 0xFF; + + fwrite(palHeader, 1, 0x18, fp); + + unsigned char colours[palette->numColors * 2]; + //palette data + for (int i = 0; i < palette->numColors; i++) + { + unsigned char red = DOWNCONVERT_BIT_DEPTH(palette->colors[i].red); + unsigned char green = DOWNCONVERT_BIT_DEPTH(palette->colors[i].green); + unsigned char blue = DOWNCONVERT_BIT_DEPTH(palette->colors[i].blue); + + uint16_t paletteEntry = SET_GBA_PAL(red, green, blue); + + colours[i * 2] = paletteEntry & 0xFF; + colours[i * 2 + 1] = paletteEntry >> 8; + } + + fwrite(colours, 1, palette->numColors * 2, fp); + + fclose(fp); +} diff --git a/tools/nitrogfx/gfx.h b/tools/nitrogfx/gfx.h index 5355ced8..fc57380e 100644 --- a/tools/nitrogfx/gfx.h +++ b/tools/nitrogfx/gfx.h @@ -15,6 +15,7 @@ struct Color { struct Palette { struct Color colors[256]; int numColors; + int bitDepth; }; struct Image { @@ -31,6 +32,8 @@ void ReadImage(char *path, int tilesWidth, int bitDepth, int metatileWidth, int void WriteImage(char *path, int numTiles, int bitDepth, int metatileWidth, int metatileHeight, struct Image *image, bool invertColors); void FreeImage(struct Image *image); void ReadGbaPalette(char *path, struct Palette *palette); +void ReadNtrPalette(char *path, struct Palette *palette); void WriteGbaPalette(char *path, struct Palette *palette); +void WriteNtrPalette(char *path, struct Palette *palette, bool ncpr); #endif // GFX_H diff --git a/tools/nitrogfx/jasc_pal.c b/tools/nitrogfx/jasc_pal.c index e5ba9c3c..4f80f5d9 100644 --- a/tools/nitrogfx/jasc_pal.c +++ b/tools/nitrogfx/jasc_pal.c @@ -91,6 +91,8 @@ void ReadJascPalette(char *path, struct Palette *palette) if (palette->numColors < 1 || palette->numColors > 256) FATAL_ERROR("%d is an invalid number of colors. The number of colors must be in the range [1, 256].\n", palette->numColors); + palette->bitDepth = 4; + for (int i = 0; i < palette->numColors; i++) { ReadJascPaletteLine(fp, line); @@ -146,6 +148,11 @@ void ReadJascPalette(char *path, struct Palette *palette) palette->colors[i].red = red; palette->colors[i].green = green; palette->colors[i].blue = blue; + if (i >= 16) + { + if (red || green || blue) + palette->bitDepth = 8; + } } if (fgetc(fp) != EOF) diff --git a/tools/nitrogfx/main.c b/tools/nitrogfx/main.c index b9f4272c..7365cf39 100644 --- a/tools/nitrogfx/main.c +++ b/tools/nitrogfx/main.c @@ -214,6 +214,14 @@ void HandleGbaToJascPaletteCommand(char *inputPath, char *outputPath, int argc U WriteJascPalette(outputPath, &palette); } +void HandleNtrToJascPaletteCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) +{ + struct Palette palette; + + ReadNtrPalette(inputPath, &palette); + WriteJascPalette(outputPath, &palette); +} + void HandleJascToGbaPaletteCommand(char *inputPath, char *outputPath, int argc, char **argv) { int numColors = 0; @@ -251,6 +259,48 @@ void HandleJascToGbaPaletteCommand(char *inputPath, char *outputPath, int argc, WriteGbaPalette(outputPath, &palette); } +void HandleJascToNtrPaletteCommand(char *inputPath, char *outputPath, int argc, char **argv) +{ + int numColors = 0; + bool ncpr = false; + + for (int i = 3; i < argc; i++) + { + char *option = argv[i]; + + if (strcmp(option, "-num_colors") == 0) + { + if (i + 1 >= argc) + FATAL_ERROR("No number of colors following \"-num_colors\".\n"); + + i++; + + if (!ParseNumber(argv[i], NULL, 10, &numColors)) + FATAL_ERROR("Failed to parse number of colors.\n"); + + if (numColors < 1) + FATAL_ERROR("Number of colors must be positive.\n"); + } + else if (strcmp(option, "-ncpr") == 0) + { + ncpr = true; + } + else + { + FATAL_ERROR("Unrecognized option \"%s\".\n", option); + } + } + + struct Palette palette; + + ReadJascPalette(inputPath, &palette); + + if (numColors != 0) + palette.numColors = numColors; + + WriteNtrPalette(outputPath, &palette, ncpr); +} + void HandleLatinFontToPngCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) { struct Image image; @@ -496,7 +546,10 @@ int main(int argc, char **argv) { "png", "8bpp", HandlePngToGbaCommand }, { "png", "gbapal", HandlePngToGbaPaletteCommand }, { "gbapal", "pal", HandleGbaToJascPaletteCommand }, + { "NCLR", "pal", HandleNtrToJascPaletteCommand }, + { "NCPR", "pal", HandleNtrToJascPaletteCommand }, { "pal", "gbapal", HandleJascToGbaPaletteCommand }, + { "pal", "NCLR", HandleJascToNtrPaletteCommand }, { "latfont", "png", HandleLatinFontToPngCommand }, { "png", "latfont", HandlePngToLatinFontCommand }, { "hwjpnfont", "png", HandleHalfwidthJapaneseFontToPngCommand }, diff --git a/tools/nitrogfx/util.c b/tools/nitrogfx/util.c index 87abeb31..73a128a1 100644 --- a/tools/nitrogfx/util.c +++ b/tools/nitrogfx/util.c @@ -4,6 +4,7 @@ #include <stdlib.h> #include <string.h> #include <stdbool.h> +#include <stdint.h> #include <errno.h> #include <limits.h> #include "global.h" @@ -122,3 +123,26 @@ void WriteWholeFile(char *path, void *buffer, int bufferSize) fclose(fp); } + +void WriteGenericNtrHeader(FILE* fp, const char* magicNumber, uint32_t size, bool byteorder) +{ + unsigned char header[0x10] = + { 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFE, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00 }; + //magic number + memcpy(header, magicNumber, 4); + + //byte order + if (!byteorder) + { + memset(header + 4, 0, 2); + } + + //size + size += 0x10; //add header size + header[8] = size & 0xFF; + header[9] = (size >> 8) & 0xFF; + header[10] = (size >> 16) & 0xFF; + header[11] = (size >> 24) & 0xFF; + + fwrite(header, 1, 0x10, fp); +} diff --git a/tools/nitrogfx/util.h b/tools/nitrogfx/util.h index 6d7a9c21..f181b66e 100644 --- a/tools/nitrogfx/util.h +++ b/tools/nitrogfx/util.h @@ -4,11 +4,13 @@ #define UTIL_H #include <stdbool.h> +#include <stdint.h> bool ParseNumber(char *s, char **end, int radix, int *intValue); char *GetFileExtension(char *path); unsigned char *ReadWholeFile(char *path, int *size); unsigned char *ReadWholeFileZeroPadded(char *path, int *size, int padAmount); void WriteWholeFile(char *path, void *buffer, int bufferSize); +void WriteGenericNtrHeader(FILE* fp, const char* magicNumber, uint32_t size, bool byteorder); #endif // UTIL_H diff --git a/tools/scaninc/Makefile b/tools/scaninc/Makefile index 1516f159..6168acd6 100644 --- a/tools/scaninc/Makefile +++ b/tools/scaninc/Makefile @@ -1,5 +1,3 @@ -CXX = g++ - CXXFLAGS = -Wall -Werror -std=c++11 -O2 SRCS = scaninc.cpp c_file.cpp asm_file.cpp source_file.cpp |
