Awoo-Installer/source/install/sdmc_xci.cpp

74 lines
2.3 KiB
C++
Raw Normal View History

#include "install/sdmc_xci.hpp"
2019-11-29 03:23:11 -05:00
#include "error.hpp"
#include "debug.h"
2019-11-30 13:58:39 +01:00
#include "nx/nca_writer.h"
#include "ui/instPage.hpp"
#include "util/lang.hpp"
2019-11-29 03:23:11 -05:00
namespace tin::install::xci
{
SDMCXCI::SDMCXCI(std::string path)
2019-11-29 03:23:11 -05:00
{
m_xciFile = fopen((path).c_str(), "rb");
if (!m_xciFile)
THROW_FORMAT("can't open file at %s\n", path.c_str());
}
SDMCXCI::~SDMCXCI()
2019-11-29 03:23:11 -05:00
{
fclose(m_xciFile);
}
void SDMCXCI::StreamToPlaceholder(std::shared_ptr<nx::ncm::ContentStorage>& contentStorage, NcmContentId ncaId)
2019-11-29 03:23:11 -05:00
{
2019-11-30 13:58:39 +01:00
const HFS0FileEntry* fileEntry = this->GetFileEntryByNcaId(ncaId);
std::string ncaFileName = this->GetFileEntryName(fileEntry);
2019-11-30 18:12:59 +01:00
LOG_DEBUG("Retrieving %s\n", ncaFileName.c_str());
2019-11-30 13:58:39 +01:00
size_t ncaSize = fileEntry->fileSize;
NcaWriter writer(ncaId, contentStorage);
float progress;
u64 fileStart = GetDataOffset() + fileEntry->dataOffset;
u64 fileOff = 0;
size_t readSize = 0x400000; // 4MB buff
auto readBuffer = std::make_unique<u8[]>(readSize);
try
{
inst::ui::instPage::setInstInfoText("inst.info_page.top_info0"_lang + ncaFileName + "...");
inst::ui::instPage::setInstBarPerc(0);
2019-11-30 13:58:39 +01:00
while (fileOff < ncaSize)
{
progress = (float) fileOff / (float) ncaSize;
if (fileOff % (0x400000 * 3) == 0) {
2019-11-30 18:12:59 +01:00
LOG_DEBUG("> Progress: %lu/%lu MB (%d%s)\r", (fileOff / 1000000), (ncaSize / 1000000), (int)(progress * 100.0), "%");
inst::ui::instPage::setInstBarPerc((double)(progress * 100.0));
2019-11-30 13:58:39 +01:00
}
if (fileOff + readSize >= ncaSize) readSize = ncaSize - fileOff;
this->BufferData(readBuffer.get(), fileOff + fileStart, readSize);
writer.write(readBuffer.get(), readSize);
fileOff += readSize;
}
inst::ui::instPage::setInstBarPerc(100);
2019-11-30 13:58:39 +01:00
}
catch (std::exception& e)
{
2019-11-30 18:12:59 +01:00
LOG_DEBUG("something went wrong: %s\n", e.what());
2019-11-30 13:58:39 +01:00
}
writer.close();
2019-11-29 03:23:11 -05:00
}
void SDMCXCI::BufferData(void* buf, off_t offset, size_t size)
2019-11-29 03:23:11 -05:00
{
fseeko(m_xciFile, offset, SEEK_SET);
fread(buf, 1, size, m_xciFile);
}
}