Skip to content

Commit

Permalink
Finished adding files to a pck
Browse files Browse the repository at this point in the history
  • Loading branch information
hhyyrylainen committed May 8, 2020
1 parent 977e641 commit ef63fff
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 3 deletions.
42 changes: 40 additions & 2 deletions src/PckTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,55 @@ int PckTool::Run()

return 0;
} else if(Opts.Action == "add" || Opts.Action == "a") {
std::cout << "TODO: add\n";
std::unique_ptr<PckFile> pck;

if(Opts.Files.empty()) {
std::cout << "ERROR: no files specified\n";
return 1;
}

if(TargetExists()) {
std::cout << "Target pck exists, loading it before adding new files\n";

pck = LoadPck();

if(!pck) {
std::cout << "ERROR: couldn't load existing target pck. Please change the "
"target or delete the existing file.\n";
return 2;
}
} else {
pck = std::make_unique<PckFile>(Opts.Pack);
}

for(const auto& entry : Opts.Files) {
if(!pck->AddFilesFromFilesystem(entry, Opts.RemovePrefix)) {
std::cout << "ERROR: failed to process file to add: " << entry << "\n";
return 3;
}
}

if(!pck->Save()) {
std::cout << "Failed to save pck\n";
return 2;
}

std::cout << "Writing / updating pck finished\n";
return 0;
}

std::cout << "ERROR: unknown action: " << Opts.Action << "\n";
return 1;
}
// ------------------------------------ //
bool PckTool::TargetExists()
{
return std::filesystem::exists(Opts.Pack);
}

bool PckTool::RequireTargetFileExists()
{
if(!std::filesystem::exists(Opts.Pack)) {
if(!TargetExists()) {
std::cout << "ERROR: specified pck file doesn't exist: " << Opts.Pack << "\n";
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/PckTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class PckTool {
int Run();

private:
bool TargetExists();

bool RequireTargetFileExists();

std::unique_ptr<PckFile> LoadPck();
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ int main(int argc, char* argv[])
output = result["output"].as<std::string>();
}

if(result.count("removePrefix")) {
if(result.count("remove-prefix")) {
removePrefix = result["remove-prefix"].as<std::string>();
}

Expand Down
71 changes: 71 additions & 0 deletions src/pck/PckFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,78 @@ void PckFile::AddFile(ContainedFile&& file)
{
Contents[file.Path] = std::move(file);
}
// ------------------------------------ //
bool PckFile::AddFilesFromFilesystem(const std::string& path, const std::string& stripPrefix)
{
if(!std::filesystem::exists(path)) {
return false;
}

if(!std::filesystem::is_directory(path)) {
// Just a single file
AddSingleFile(path, PreparePckPath(path, stripPrefix));
return true;
}

for(const auto& entry : std::filesystem::recursive_directory_iterator(path)) {
if(entry.is_directory())
continue;

const std::string entryPath = entry.path().string();

AddSingleFile(entryPath, PreparePckPath(entryPath, stripPrefix));
}

return true;
}

void PckFile::AddSingleFile(const std::string& filesystemPath, const std::string& pckPath)
{
std::cout << "Adding " << filesystemPath << " as " << pckPath << "\n";

ContainedFile file;

const auto size = std::filesystem::file_size(filesystemPath);

file.Path = pckPath;
file.Offset = -1;
file.Size = size;

// Seems like Godot pcks don't currently use hashes, so we don't need to do this
// file.MD5 = {0};

file.GetData = [filesystemPath, size]() {
std::ifstream reader(filesystemPath, std::ios::in);

if(!reader.good()) {
std::cout << "ERROR: opening for reading: " << filesystemPath << "\n";
throw std::runtime_error("can't read source file");
}

std::string data;
data.resize(size);

reader.read(data.data(), size);

return data;
};

Contents[pckPath] = file;
}

std::string PckFile::PreparePckPath(std::string path, const std::string& stripPrefix)
{
if(stripPrefix.size() > 0 && path.find(stripPrefix) == 0) {
path = path.substr(stripPrefix.size());
}

while(path.size() > 0 && path.front() == '/') {
path.erase(path.begin());
}

return GODOT_RES_PATH + path;
}
// ------------------------------------ //
void PckFile::ChangePath(const std::string& path)
{
File.reset();
Expand Down
7 changes: 7 additions & 0 deletions src/pck/PckFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class PckFile {

void AddFile(ContainedFile&& file);

//! \brief Adds recursively files from path to this pck
bool AddFilesFromFilesystem(const std::string& path, const std::string& stripPrefix);

void AddSingleFile(const std::string& filesystemPath, const std::string& pckPath);

std::string PreparePckPath(std::string path, const std::string& stripPrefix);

void ChangePath(const std::string& path);

std::string ReadContainedFileContents(uint64_t offset, uint64_t size);
Expand Down

0 comments on commit ef63fff

Please sign in to comment.