198 lines
5.9 KiB
C++
198 lines
5.9 KiB
C++
/*
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#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<ftxui::Element> headElements;
|
|
|
|
headElements.push_back(ftxui::text(" Zip ") | 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<char> magic(signature.length());
|
|
uint16_t 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<char*>(&cargoVer), sizeof(cargoVer));
|
|
cargo.read(reinterpret_cast<char*>(&filesLen), sizeof(filesLen));
|
|
cargo.read(reinterpret_cast<char*>(&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<char*>(&fhTmp.nameLen), sizeof(fhTmp.nameLen));
|
|
|
|
std::vector<char> nameBuffor(fhTmp.nameLen);
|
|
cargo.read(nameBuffor.data(), fhTmp.nameLen);
|
|
fhTmp.nameFile = std::string(nameBuffor.begin(), nameBuffor.end());
|
|
|
|
cargo.read(reinterpret_cast<char*>(&fhTmp.hashName), sizeof(fhTmp.hashName));
|
|
cargo.read(reinterpret_cast<char*>(&fhTmp.offset), sizeof(fhTmp.offset));
|
|
cargo.read(reinterpret_cast<char*>(&fhTmp.size), sizeof(fhTmp.size));
|
|
cargo.read(reinterpret_cast<char*>(&fhTmp.crc), sizeof(fhTmp.crc));
|
|
cargo.read(reinterpret_cast<char*>(&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<ftxui::Element> cell;
|
|
|
|
ftxui::Element eZip;
|
|
|
|
//Dodawanie check boxa czy plik jest spakowany czy nie
|
|
if (zip == 1)
|
|
{
|
|
eZip = ftxui::text(" [x] ") | ftxui::color(ftxui::Color::Cyan);
|
|
}
|
|
else
|
|
{
|
|
eZip = ftxui::text(" [ ] ") | ftxui::color(ftxui::Color::White);
|
|
}
|
|
|
|
//Dodawanie komurek
|
|
cell.push_back(eZip);
|
|
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();
|
|
}
|