Skip to content

Commit

Permalink
Fix crashes, refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
user-grinch committed Nov 11, 2024
1 parent e8d4386 commit cd83a86
Show file tree
Hide file tree
Showing 12 changed files with 489 additions and 59 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"*.rh": "cpp",
"any": "cpp",
"deque": "cpp",
"codecvt": "cpp"
"codecvt": "cpp",
"cwctype": "cpp"
}
}
23 changes: 14 additions & 9 deletions src/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ void Editor::ProcessContextMenu()
),
pSelectedArchive->EntryList.end()
);
pSelectedArchive->UpdateSelectList(FilterText);
pSelectedArchive->bUpdateSearch = true;
pContextEntry = nullptr;
}
if (ImGui::MenuItem("Export"))
Expand All @@ -270,7 +270,7 @@ void Editor::ProcessContextMenu()
e.bRename = false;
}
pContextEntry->bRename = true;
pSelectedArchive->UpdateSelectList(FilterText);
pSelectedArchive->bUpdateSearch = true;
}
pContextEntry = nullptr;
}
Expand Down Expand Up @@ -324,7 +324,7 @@ void Editor::ProcessWindow()
{
Utils::ConvertUtf8ToWide(buf, FilterText, sizeof(buf));
Utils::ToLowerCase(FilterText);
pSelectedArchive->UpdateSelectList(FilterText);
pSelectedArchive->bUpdateSearch = true;
}

blockHotkeys = ImGui::IsItemActive();
Expand All @@ -338,7 +338,12 @@ void Editor::ProcessWindow()
ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthFixed, 100 * scl);
ImGui::TableSetupColumn("Size", ImGuiTableColumnFlags_WidthFixed, 75 * scl);
ImGui::TableHeadersRow();


if (pSelectedArchive->bUpdateSearch) {
pSelectedArchive->UpdateSelectList(FilterText);
pSelectedArchive->bUpdateSearch = false;
}

float height = ImGui::GetItemRectSize().y;
ImGuiListClipper clipper(static_cast<int>(pSelectedArchive->SelectedList.size()), height);
while (clipper.Step())
Expand Down Expand Up @@ -403,7 +408,7 @@ void Editor::ProcessWindow()
pContextEntry = pContextEntry ? nullptr : pEntry;
}
ImGui::TableNextColumn();
str = std::format(L"{} kb ## {}", pEntry->Size*2, pEntry->FileName);
str = std::format(L"{} kb ## {}", pEntry->Sector*2, pEntry->FileName);
Utils::ConvertWideToUtf8(str.c_str(), buf, sizeof(buf));
if (ImGui::Selectable(buf, archive.EntryList[i].bSelected))
{
Expand Down Expand Up @@ -682,8 +687,8 @@ void Editor::ImportFiles()
std::wstring fileNames = WinDialogs::ImportFiles();
if (!fileNames.empty())
{
ArchiveInfo* info = new ArchiveInfo{pSelectedArchive, fileNames, eImgVer::Unknown, false};
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)&IMGArchive::ImportEntries, info, NULL, NULL);
ArchiveInfo info = {pSelectedArchive, fileNames, eImgVer::Unknown, false};
IMGArchive::ImportEntries(&info);
}
}

Expand All @@ -692,8 +697,8 @@ void Editor::ImportAndReplaceFiles()
std::wstring fileNames = WinDialogs::ImportFiles();
if (!fileNames.empty())
{
ArchiveInfo* info = new ArchiveInfo{pSelectedArchive, fileNames, eImgVer::Unknown, true};
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)&IMGArchive::ImportEntries, info, NULL, NULL);
ArchiveInfo info = {pSelectedArchive, fileNames, eImgVer::Unknown, true};
IMGArchive::ImportEntries(&info);
}
}

Expand Down
33 changes: 16 additions & 17 deletions src/imgarchive.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "pch.h"
#include "imgarchive.h"
#include "imgparser.h"
#include <filesystem>
#include "editor.h"
#include "parser/pc_v1.h"
#include "parser/pc_v2.h"

IMGArchive::IMGArchive(std::wstring Path, bool CreateNew)
{
Expand All @@ -11,7 +12,7 @@ IMGArchive::IMGArchive(std::wstring Path, bool CreateNew)
this->FileName = Path;
this->bCreateNew = true;
AddLogMessage(L"Created archive");
Parser = Parser::Get();
Parser = ParserPCv1::Get();
}
else
{
Expand All @@ -20,8 +21,10 @@ IMGArchive::IMGArchive(std::wstring Path, bool CreateNew)
switch(this->ImageVersion)
{
case eImgVer::One:
Parser = ParserPCv1::Get();
break;
case eImgVer::Two:
Parser = Parser::Get();
Parser = ParserPCv2::Get();
break;
default:
Parser = nullptr;
Expand Down Expand Up @@ -73,9 +76,15 @@ std::wstring IMGArchive::GetFileType(const wchar_t* name)

void IMGArchive::ExportEntry(EntryInfo *pEntry, std::wstring filePath, bool log)
{
if (Parser)
if (pEntry->bImported) {
std::filesystem::copy_file(pEntry->Path, filePath, std::filesystem::copy_options::overwrite_existing);
}
else
{
Parser->Export(this, pEntry, filePath, log);
if (Parser)
{
Parser->Export(this, pEntry, filePath, log);
}
}
}

Expand Down Expand Up @@ -165,22 +174,12 @@ void IMGArchive::ImportEntries(ArchiveInfo *pInfo)
}
}

size_t total = list.size();
for (size_t i = 0; i != total; ++i)
for (size_t i = 0; i != list.size(); ++i)
{
pInfo->pArc->ImportEntry(list[i], pInfo->removeExisting);
pInfo->pArc->ProgressBar.Percentage = (static_cast<float>(i)+1)/ static_cast<float>(total);

if (pInfo->pArc->ProgressBar.bCancel)
{
pInfo->pArc->ProgressBar.bCancel = false;
pInfo->pArc->AddLogMessage(L"Rebuilding failed");
pInfo->pArc->ProgressBar.bInUse = false;
}
}
pInfo->pArc->UpdateSelectList(Editor::GetFilterText());
pInfo->pArc->AddLogMessage(L"Imported entries");
delete pInfo;
pInfo->pArc->bUpdateSearch = true;
}

void IMGArchive::AddLogMessage(std::wstring &&message)
Expand Down
3 changes: 2 additions & 1 deletion src/imgarchive.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct EntryInfo
{
// archive data
uint32_t Offset = 0; // in sectors (each sector is 2048 bytes)
uint32_t Size = 0; // in sectors (each sector is 2048 bytes)
uint32_t Sector = 0; // in sectors (each sector is 2048 bytes)
wchar_t FileName[24]; // file name in the archive

// editor data
Expand Down Expand Up @@ -64,6 +64,7 @@ class IMGArchive

bool bOpen = true;
bool bCreateNew;
bool bUpdateSearch = false;

IMGArchive(std::wstring Path, bool CreateNew = false);

Expand Down
32 changes: 18 additions & 14 deletions src/imgparser.cpp → src/imgparser.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void Parser::Open(IMGArchive *pArc)
{
EntryInfo entry;
fread(&entry.Offset, sizeof(entry.Offset), 1, fp);
fread(&entry.Size, sizeof(entry.Size), 1, fp);
fread(&entry.Sector, sizeof(entry.Sector), 1, fp);

char buf[24];
fread(buf, sizeof(buf), 1, fp);
Expand Down Expand Up @@ -76,8 +76,8 @@ void Parser::Export(IMGArchive *pMgr, EntryInfo *pEntry, const std::wstring& fil

if (pOut && pImg)
{
size_t offset = pEntry->Offset*2048;
size_t size = pEntry->Size*2048;
size_t offset = pEntry->Offset*SECTOR_SZ;
size_t size = pEntry->Sector*SECTOR_SZ;

_fseeki64(pImg, offset, 0);
char *buf = new char[size + 1];
Expand Down Expand Up @@ -139,7 +139,7 @@ void Parser::Import(IMGArchive *pArc, const std::wstring &path, bool replace)
info.Path = path;
info.bImported = true;
info.Type = IMGArchive::GetFileType(info.FileName);
info.Size = static_cast<uint32_t>(std::filesystem::file_size(path))/2048; // bytes -> sector
info.Sector = GetFileSz(path) / SECTOR_SZ;
pArc->EntryList.push_back(std::move(info));
}

Expand Down Expand Up @@ -188,35 +188,34 @@ void Parser::Save(ArchiveInfo *pInfo)
if (e.bImported)
{
fFile = _wfopen(e.Path.c_str(), L"rb");
_fseeki64(fFile, 0, SEEK_END);
size = _ftelli64(fFile);
_fseeki64(fFile, 0, SEEK_SET);
size = GetFileSz(e.Path);
e.Sector = size / SECTOR_SZ;
}
else
{
_fseeki64(fIn, e.Offset*2048, SEEK_SET);
size = e.Size*2048;
_fseeki64(fIn, e.Offset * SECTOR_SZ, SEEK_SET);
size = e.Sector * SECTOR_SZ;
}

char *buf = new char[size + 1];
if (buf)
{
fread(buf, size, 1, e.bImported? fFile : fIn);
e.Offset = static_cast<uint32_t>(offset/2048);
e.Offset = static_cast<uint32_t>(offset/SECTOR_SZ);
char nameBuf[24];
Utils::ConvertWideToUtf8(e.FileName, nameBuf, sizeof(nameBuf));
Utils::ConvertWideToUtf8(e.FileName, nameBuf, 24);
if (outVer == eImgVer::One)
{
fwrite(&e.Offset, sizeof(e.Offset), 1, fDir);
fwrite(&e.Size, sizeof(e.Size), 1, fDir);
fwrite(nameBuf, sizeof(nameBuf), 1, fDir);
fwrite(&e.Sector, sizeof(e.Sector), 1, fDir);
fwrite(nameBuf, 24, 1, fDir);
}
else
{
long dirOffset = static_cast<long>(0x8 + 0x20 * index);
_fseeki64(fImg, dirOffset, SEEK_SET);
fwrite(&e.Offset, sizeof(e.Offset), 1, fImg);
fwrite(&e.Size, sizeof(e.Size), 1, fImg);
fwrite(&e.Sector, sizeof(e.Sector), 1, fImg);
fwrite(nameBuf, sizeof(nameBuf), 1, fImg);
_fseeki64(fImg, offset, SEEK_SET);
}
Expand Down Expand Up @@ -293,4 +292,9 @@ void Parser::Save(ArchiveInfo *pInfo)
pInfo->pArc->ImageVersion = outVer;
}
delete pInfo;
}

size_t Parser::GetFileSz(const std::wstring& path) {
size_t sz = std::filesystem::file_size(path);
return max(SECTOR_SZ, sz + (SECTOR_SZ - sz % SECTOR_SZ) % SECTOR_SZ);
}
20 changes: 3 additions & 17 deletions src/imgparser.h → src/imgparser.h2
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
#pragma once
#include "imgarchive.h"

class IParser
{
public:
// Opens the archive & feeds data
virtual void Open(IMGArchive *pMgr) = 0;

// Exports an entity form the archive
virtual void Export(IMGArchive *pMgr, EntryInfo *pEntry, const std::wstring& filePath, bool logMsg = true) = 0;

// Imports an entity to the archive
virtual void Import(IMGArchive *pArc, const std::wstring &path, bool replace) = 0;

// Rebuilds the archive & saves changes
virtual void Save(ArchiveInfo *pInfo) = 0;
};
#include "iparser.h"

/*
The original IMG format
Expand All @@ -38,4 +22,6 @@ class Parser : public IParser
void Export(IMGArchive *pMgr, EntryInfo *pEntry, const std::wstring& filePath, bool logMsg = true);
void Import(IMGArchive *pArc, const std::wstring &path, bool replace);
void Save(ArchiveInfo *pInfo);

size_t GetFileSz(const std::wstring& path);
};
22 changes: 22 additions & 0 deletions src/parser/iparser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

#pragma once
#include "../imgarchive.h"

class IParser
{
public:
// Opens the archive & feeds data
virtual void Open(IMGArchive *pMgr) = 0;

// Exports an entity form the archive
virtual void Export(IMGArchive *pMgr, EntryInfo *pEntry, const std::wstring& filePath, bool logMsg = true) = 0;

// Imports an entity to the archive
virtual void Import(IMGArchive *pArc, const std::wstring &path, bool replace) = 0;

// Returns true if a valid archive format
virtual bool IsValid() = 0;

// Rebuilds the archive & saves changes
virtual void Save(ArchiveInfo *pInfo) = 0;
};
Loading

0 comments on commit cd83a86

Please sign in to comment.