/*
* This file is part of VoidArchiveTool.
*
* Copyright (C) 2025 Yanczi
*
* Void Archive Toolis free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
#include "ViewCargo.h"
ViewCargo::ViewCargo()
:signature(SIGNATURE)
, version(VERSION)
, filesLen(0)
, tablePos(0)
{
// TODO Auto-generated constructor stub
}
//-----------------------------------------------------------------------------
// Wywoływanie
//-----------------------------------------------------------------------------
bool ViewCargo::View(const std::string& path)
{
//Sprawdź czy plik istnieje
if (!std::filesystem::exists(path))
{
std::cerr << "Error: The given file is not exist!" << std::endl;
return false;
}
//Sprawdź czy plik jets plikiem
if (!std::filesystem::is_regular_file(path))
{
std::cerr << "Error: The given file is a directory!" << std::endl;
return false;
}
//Sprawdź czy kontener jest prawidłowy
if (!CheckCargoFile(path))
{
std::cerr << "Nie prawidlowa struktura kontenera Void" << std::endl;
return false;
}
//Table Header
std::vector headElements;
headElements.push_back(ftxui::text(" Compressed ") | ftxui::bold);
headElements.push_back(ftxui::text(" Encrypted ") | ftxui::bold);
headElements.push_back(ftxui::text("Nazwa pliku") | ftxui::bold | ftxui::size(ftxui::WIDTH, ftxui::EQUAL, 56));
headElements.push_back(ftxui::text("Hash Name") | ftxui::bold | ftxui::size(ftxui::WIDTH, ftxui::EQUAL, 20));
filesList.push_back(hbox(std::move(headElements)));
//Pobieranie listy plików
GetFileList(path);
//Renderowanie listy plików
RenderList();
return true;
}
//-----------------------------------------------------------------------------
// Sprawdzenie poprawności kontenera
//-----------------------------------------------------------------------------
bool ViewCargo::CheckCargoFile(const std::string& path)
{
std::vector magic(signature.length());
short cargoVer = 0;
std::ifstream cargo(path, std::ios::binary);
if (!cargo.is_open())
{
std::cerr << "Error: Failed to open container" << std::endl;
return false;
}
//---------------------------------------------------------------
// Odczytywanie pierwszych 11 bajtów nagłówka pliku
// 6 pierwszych to sygnatura
// 1 wersja kontenera
// 4 ilość plików w kontenerze
//---------------------------------------------------------------
cargo.read(magic.data(), magic.size());
cargo.read(reinterpret_cast(&cargoVer), sizeof(cargoVer));
cargo.read(reinterpret_cast(&filesLen), sizeof(filesLen));
cargo.read(reinterpret_cast(&tablePos), sizeof(tablePos));
//Sprawdź czy kontener ma poprawną sygnature
if (std::string(magic.begin(), magic.end()) != signature)
{
std::cerr << "Error: Corrupted Cargo" << std::endl;
cargo.close();
return false;
}
//Sprawdź spójność wersji kontenera
if (cargoVer != version)
{
std::cerr << "Error: Wrong cargo version" << std::endl;
cargo.close();
return false;
}
cargo.close();
return true;
}
//-----------------------------------------------------------------------------
// Pobieranie listy plików z kontenera
//-----------------------------------------------------------------------------
void ViewCargo::GetFileList(const std::string& path)
{
std::ifstream cargo(path, std::ios::binary);
cargo.seekg(tablePos);
for (uint32_t i = 0; i < filesLen; ++i)
{
FilesTable fhTmp;
cargo.read(reinterpret_cast(&fhTmp.nameLen), sizeof(fhTmp.nameLen));
std::vector nameBuffor(fhTmp.nameLen);
cargo.read(nameBuffor.data(), fhTmp.nameLen);
fhTmp.nameFile = std::string(nameBuffor.begin(), nameBuffor.end());
cargo.read(reinterpret_cast(&fhTmp.hashName), sizeof(fhTmp.hashName));
cargo.read(reinterpret_cast(&fhTmp.offset), sizeof(fhTmp.offset));
cargo.read(reinterpret_cast(&fhTmp.size), sizeof(fhTmp.size));
cargo.read(reinterpret_cast(&fhTmp.crc), sizeof(fhTmp.crc));
cargo.read(reinterpret_cast(&fhTmp.flag), sizeof(fhTmp.flag));
//Tworzenie wierszy tabeli
CreateTableRow(fhTmp.nameFile, fhTmp.flag, fhTmp.hashName);
}
cargo.close();
}
//-----------------------------------------------------------------------------
// Generowanie wierszy do tabeli
//-----------------------------------------------------------------------------
void ViewCargo::CreateTableRow(const std::string& file, const uint8_t& zip, const uint64_t& hash)
{
//Zamiania crc liczbowej na hex string
std::stringstream ss;
ss << "0x" << std::hex << std::uppercase << hash;
std::vector cell;
ftxui::Element eZip;
ftxui::Element eEnc;
// Ustawianie checkboxów
switch (zip)
{
case 1:
eZip = ftxui::text(" [x] ") | ftxui::color(ftxui::Color::Cyan);
eEnc = ftxui::text(" [ ] ") | ftxui::color(ftxui::Color::White);
break;
case 2:
eZip = ftxui::text(" [ ] ") | ftxui::color(ftxui::Color::White);
eEnc = ftxui::text(" [x] ") | ftxui::color(ftxui::Color::Cyan);
break;
case 3:
eZip = ftxui::text(" [x] ") | ftxui::color(ftxui::Color::Cyan);
eEnc = ftxui::text(" [x] ") | ftxui::color(ftxui::Color::Cyan);
break;
default:
eZip = ftxui::text(" [ ] ") | ftxui::color(ftxui::Color::White);
eEnc = ftxui::text(" [ ] ") | ftxui::color(ftxui::Color::White);
break;
}
//Dodawanie komurek
cell.push_back(eZip);
cell.push_back(eEnc | ftxui::size(ftxui::WIDTH, ftxui::EQUAL, 7));
cell.push_back(ftxui::text(file) | ftxui::size(ftxui::WIDTH, ftxui::EQUAL, 56));
cell.push_back(ftxui::text(ss.str()) | ftxui::size(ftxui::WIDTH, ftxui::EQUAL, 20));
//Konwersja komurek na wiersz
filesList.push_back(ftxui::hbox(std::move(cell)));
}
//-----------------------------------------------------------------------------
// Renderowanie listy plików
//-----------------------------------------------------------------------------
void ViewCargo::RenderList()
{
//Dodawanie wierszy do kolumn
ftxui::Element table = ftxui::vbox(std::move(filesList));
auto screen = ftxui::Screen::Create(ftxui::Dimension::Fit(table));
ftxui::Render(screen, table);
screen.Print();
}