1
0
Fork 0
This commit is contained in:
Henrik Hautakoski 2022-09-26 22:25:53 +02:00
parent 99ae7f2236
commit 7d042fa5bd
5 changed files with 9406 additions and 0 deletions

84
tools/archive/Archive.cpp Normal file
View file

@ -0,0 +1,84 @@
#include <iostream>
#include <Spectre/System/ByteOrder.h>
#include "Archive.h"
void Archive::add(const std::string& path, const std::vector<uint8_t>& data)
{
m_files[path] = data;
}
const std::map<std::string, std::vector<uint8_t>>& Archive::files() const
{
return m_files;
}
void Archive::read(const std::string& filename)
{
struct Header hdr;
sp::File fd(filename);
// Read header
fd.read(&hdr.sig, 2);
fd.read(&hdr.version, 1);
if (hdr.sig[0] == 0xAE && hdr.sig[1] == 0xEE) {
}
for(;;) {
struct Index idx = { { '\0' }, 0 };
std::vector<uint8_t> data;
if (fd.read(&idx.path, ARCHIVE_PATH_MAX_LEN) < 1) {
break;
}
if (fd.read(&idx.size, 4) < 1) {
break;
}
data.resize(idx.size);
if (fd.read(&data[0], idx.size) < 1) {
break;
}
m_files[idx.path] = data;
}
}
void Archive::write(const std::string& filename)
{
sp::core::Buffer<uint8_t> buffer;
sp::File file(filename, sp::File::Access::WRITE,
sp::File::CREATE | sp::File::TRUNCATE);
pack(buffer);
file.write(buffer.data);
file.close();
}
#define min(a,b) ((a) < (b) ? (a) : (b))
void Archive::pack(sp::core::Buffer<uint8_t>& buffer)
{
buffer.data.reserve(sizeof(struct Header));
// Sig
buffer.data.push_back(0xAE);
buffer.data.push_back(0xEE);
// Version
buffer.data.push_back(0x01);
for (auto file : m_files) {
struct Index idx = { { '\0', }, file.second.size() };
memcpy(idx.path, file.first.c_str(), min(file.first.size(), ARCHIVE_PATH_MAX_LEN - 1));
buffer.append(&idx.path, ARCHIVE_PATH_MAX_LEN);
buffer.append(&idx.size, 4);
buffer.append(&file.second[0], file.second.size());
}
}

51
tools/archive/Archive.h Normal file
View file

@ -0,0 +1,51 @@
#ifndef ARCHIVE_H
#define ARCHIVE_H
#include <Spectre/Core/Buffer.h>
#include <Spectre/System/File.h>
#include <string>
#include <vector>
#include <map>
#include <cstdint>
struct Header {
uint8_t sig[2];
uint8_t version;
// how many indexes we have.
// uint32_t num_index;
// uint32_t data_offset;
};
#define ARCHIVE_PATH_MAX_LEN 100
struct Index {
char path[ARCHIVE_PATH_MAX_LEN];
uint32_t size;
};
class Archive
{
public:
void add(const std::string& path, const std::vector<uint8_t>& data);
const std::map<std::string, std::vector<uint8_t>>& files() const;
void read(const std::string& filename);
void write(const std::string& filename);
protected:
void pack(sp::core::Buffer<uint8_t>& buffer);
protected:
std::map<std::string, std::vector<uint8_t>> m_files;
//std::vector<struct Index> m_idx;
//std::vector<uint8_t> m_data;
};
#endif /* ARCHIVE_H */

24
tools/archive/bam.lua Normal file
View file

@ -0,0 +1,24 @@
Import(".bam/path.lua")
local settings = CopySettings(global_settings, "Tool - Archive")
settings.cc.includes:Add(
"vendor/CLI11/include",
"include/",
RelPath("./")
)
-- Link with spectre.
settings.link.extrafiles:Add(libspectre)
-- Compile object files.
local obj = Compile(settings, {
RelPath("Archive.cpp"),
RelPath("main.cpp")
})
-- Link to executable.
local exe = Link(settings, PathJoin(paths.tools, "archive"), obj)
PseudoTarget("tool.archive", exe)

57
tools/archive/main.cpp Normal file
View file

@ -0,0 +1,57 @@
#include <Spectre/System/File.h>
#include <CLI11/CLI.hpp>
#include <iostream>
#include "archive.h"
int main(int argc, char **argv) {
Archive arch;
std::string filename;
std::string directory;
CLI::App app("Pack archive files");
app.add_option("file", filename, "file name")->required();
CLI::App *read = app.add_subcommand("read", "Read files");
CLI::App *write = app.add_subcommand("write", "Write files");
app.require_subcommand();
//app.add_option("directory", directory, "directory")->required();
CLI11_PARSE(app, argc, argv);
std::cout << filename << std::endl;
if (read->parsed()) {
arch.read(filename);
for (auto idx : arch.files()) {
std::string str;
str.assign(idx.second.begin(), idx.second.end());
std::cout << idx.first << ": " << str << std::endl;
}
} else {
std::string str = "Hello";
std::vector<uint8_t> data;
data.assign(str.begin(), str.end());
arch.add("some/path", data);
std::string str2 = "Hello Again";
std::vector<uint8_t> data2;
data2.assign(str2.begin(), str2.end());
arch.add("some/other/path", data2);
arch.write(filename);
}
return 0;
}

9190
vendor/CLI11/include/CLI11/CLI.hpp vendored Normal file

File diff suppressed because it is too large Load diff