/*******************************************************************************/

#include "Kamiloxnumetal's MHPB PKR3 EditorFrm.h"

/*******************************************************************************/

////Header Include Start
#include "Images/Self_Kamiloxnumetal_s_MHPB_PKR3_EditorFrm_XPM.xpm"
////Header Include End

/*******************************************************************************/

////Event Table Start
BEGIN_EVENT_TABLE(Kamiloxnumetal_s_MHPB_PKR3_EditorFrm,wxFrame)
	////Manual Code Start
	////Manual Code End
	
	EVT_CLOSE(Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::OnClose)
	EVT_BUTTON(ID_BTNPREVIEW,Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::BTNPreviewClick)
	EVT_BUTTON(ID_BTNSAVEFILE,Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::BTNSaveFileClick)
	EVT_BUTTON(ID_BTNMEDIAPKR,Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::BTNMediaPkrClick)
	EVT_BUTTON(ID_BTNOPENDATA,Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::BTNOpenDataClick)
	EVT_BUTTON(ID_BTNIMPORT,Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::BTNImportClick)
	EVT_BUTTON(ID_BTNEXPORT,Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::BTNExportClick)
	EVT_LISTBOX(ID_LSTFILES,Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::LSTFilesSelected)
	EVT_LISTBOX_DCLICK(ID_LSTFILES,Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::LSTFilesDoubleClicked)
	EVT_LISTBOX(ID_LSTDIRS,Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::LSTDirsSelected)
END_EVENT_TABLE()
////Event Table End

/*******************************************************************************/

Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::Kamiloxnumetal_s_MHPB_PKR3_EditorFrm(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &position, const wxSize& size, long style)
: wxFrame(parent, id, title, position, size, style)
{
	CreateGUIControls();
	HTMLBmp -> Show(false);
	EDTTxt -> Show(false);
	BTNPreview -> Enable(false);
	for(unsigned short x = 0; x < 1510; x++)
	{
        file[x].fdata = NULL;
    }
	EDTConsole -> AppendText("Program initialized...");
}

/*******************************************************************************/

Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::~Kamiloxnumetal_s_MHPB_PKR3_EditorFrm()
{
	for(unsigned short x = 0; x < 1510; x++)
	{
        delete[] file[x].fdata;
    }
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::CreateGUIControls()
{
	////GUI Items Creation Start

	DLGCaution =  new wxMessageDialog(this, wxT("You are going to overwrite a compressed file with an uncompressed file, do this at your own risk! \n (NOTE: This program can't import compressed files so this is the only way to overwrite this file) "), wxT("Caution!"), wxICON_EXCLAMATION);

	CHKPreview = new wxCheckBox(this, ID_CHKPREVIEW, wxT("Always Preview Compressed *.BMP Files"), wxPoint(2, 555), wxSize(220, 17), 0, wxDefaultValidator, wxT("CHKPreview"));

	DLGOpenDataPkr =  new wxFileDialog(this, wxT("Choose a PKR3 \"Data.pkr\" file:"), wxT(""), wxT("data.pkr"), wxT("*.pkr"), wxOPEN | wxFILE_MUST_EXIST);

	DLGImport =  new wxFileDialog(this, wxT("Choose a file to import"), wxT(""), wxT(""), wxT("*.*"), wxOPEN | wxFILE_MUST_EXIST);

	DLGOpenMediaPkr =  new wxFileDialog(this, wxT("Choose a PKR3 \"Media.pkr\" file:"), wxT(""), wxT("media.pkr"), wxT("*.pkr"), wxOPEN | wxFILE_MUST_EXIST);

	DLGExport =  new wxFileDialog(this, wxT("Choose destination file"), wxT(""), wxT(""), wxT("*.*"), wxSAVE);

	DLGSaveFile =  new wxFileDialog(this, wxT("Save PKR3 File"), wxT(""), wxT(""), wxT("*.pkr"), wxSAVE);

	BTNPreview = new wxButton(this, ID_BTNPREVIEW, wxT("Preview a Compressed *.BMP File"), wxPoint(-1, 538), wxSize(317, 16), 0, wxDefaultValidator, wxT("BTNPreview"));

	EDTTxt = new wxTextCtrl(this, ID_EDTTXT, wxT(""), wxPoint(317, 25), wxSize(474, 430), wxTE_READONLY | wxTE_MULTILINE, wxDefaultValidator, wxT("EDTTxt"));

	HTMLBmp = new wxHtmlWindow(this, ID_HTMLBMP, wxPoint(318, 24), wxSize(472, 432));

	BTNSaveFile = new wxButton(this, ID_BTNSAVEFILE, wxT("Save PKR3 File"), wxPoint(645, 0), wxSize(145, 25), 0, wxDefaultValidator, wxT("BTNSaveFile"));
	BTNSaveFile->Enable(false);

	BTNMediaPkr = new wxButton(this, ID_BTNMEDIAPKR, wxT("Open \"Media.Pkr\" PKR3 File"), wxPoint(482, 0), wxSize(161, 25), 0, wxDefaultValidator, wxT("BTNMediaPkr"));

	BTNOpenData = new wxButton(this, ID_BTNOPENDATA, wxT("Open \"Data.Pkr\" PKR3 File"), wxPoint(318, 0), wxSize(164, 25), 0, wxDefaultValidator, wxT("BTNOpenData"));

	EDTConsole = new wxTextCtrl(this, ID_EDTCONSOLE, wxT(""), wxPoint(316, 457), wxSize(475, 115), wxTE_READONLY | wxTE_MULTILINE, wxDefaultValidator, wxT("EDTConsole"));
	EDTConsole->SetForegroundColour(wxColour(wxT("WHITE")));
	EDTConsole->SetBackgroundColour(wxColour(wxT("BLACK")));
	EDTConsole->SetFont(wxFont(9, wxSWISS, wxNORMAL, wxNORMAL, false, wxT("Arial")));

	BTNImport = new wxButton(this, ID_BTNIMPORT, wxT("Import File"), wxPoint(159, 521), wxSize(157, 17), 0, wxDefaultValidator, wxT("BTNImport"));
	BTNImport->Enable(false);

	BTNExport = new wxButton(this, ID_BTNEXPORT, wxT("Export File"), wxPoint(0, 521), wxSize(158, 17), 0, wxDefaultValidator, wxT("BTNExport"));
	BTNExport->Enable(false);

	wxArrayString arrayStringFor_LSTFiles;
	LSTFiles = new wxListBox(this, ID_LSTFILES, wxPoint(131, 0), wxSize(187, 520), arrayStringFor_LSTFiles, wxLB_SINGLE);

	wxArrayString arrayStringFor_LSTDirs;
	LSTDirs = new wxListBox(this, ID_LSTDIRS, wxPoint(0, 0), wxSize(130, 520), arrayStringFor_LSTDirs, wxLB_SINGLE);

	SetTitle(wxT("Kamiloxnumetal's MHPB PKR3 Editor"));
	SetIcon(Self_Kamiloxnumetal_s_MHPB_PKR3_EditorFrm_XPM);
	SetSize(8,8,800,600);
	Center();
	
	////GUI Items Creation End
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::OnClose(wxCloseEvent& event)
{
	Destroy();
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::Print(const wxChar* txt)
{
    EDTConsole -> AppendText(txt);
}

/*******************************************************************************/

unsigned short Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::GetExtension(char* text)
{
    Str = text;
    Str = Str.SubString(Str.Length() - 3,Str.Length() - 1);
    if(Str.MakeUpper() == "WAV")
        return 1;
    else if(Str.MakeUpper() == "BMP")
        return 2;
    else
        return 0;
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::MakeFileList(unsigned short index)
{
    LSTFiles -> Clear();
    for(unsigned short x = dir[index].dcount; x < dir[index].dcount + dir[index].dfiles; x++)
    {
        LSTFiles -> Append(file[x].fname);
    }
    if(dir[index].dfiles == 0)
    {
        BTNExport -> Enable(false);
        BTNImport -> Enable(false);
    }
    else
    {
        BTNExport -> Enable();
        BTNImport -> Enable();
    }
    BTNPreview -> Enable(false);
    HTMLBmp -> Show(false);
	EDTTxt -> Show(false);
    LSTFiles -> SetSelection(0);
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::ReadPkr()
{
    wxFileDialog* AuxDialog;
    if(pkr)
        AuxDialog = DLGOpenMediaPkr;
    else
        AuxDialog = DLGOpenDataPkr;
    if(AuxDialog -> ShowModal() == wxID_OK)
    {
        if(FHandler.Open(AuxDialog -> GetPath(),wxFile::read))
        {
            FHandler.Read(&aux,4);
            if(aux == 0x33524b50)
            {
                FHandler.Read(&aux,4); // Read master header offset
                FHandler.Seek(aux+4,wxFromStart); // Jump to master header zone
                FHandler.Read(&aux,4);
                if((aux == 3 && pkr) || (aux == 33 && !pkr))
                {
                    FHandler.Read(&aux,4);
                    if((aux == 20 && pkr) || (aux == 1510 && !pkr))
                    {
                        if(pkr)
                            Print("\nMHPB \"Media.pkr\" file identified, found 3 folders and 20 files");
                        else
                            Print("\nMHPB \"Data.pkr\" file identified, found 33 folders and 1510 files");
                        BTNOpenData -> Enable(false);
                        BTNMediaPkr -> Enable(false);
                        Print("\nReading Directory Header...");
                        // Read Directory Header
                        unsigned short size;
                        if(pkr)
                           size = 3;
                        else
                           size = 33;
                        for(unsigned short x = 0; x < size; x++)
                        {
                            FHandler.Read(dir[x].dname,32); // Dir name
                            FHandler.Read(&(dir[x].dcount),4); // Files count
                            FHandler.Read(&(dir[x].dfiles),4); // Files amount
                            LSTDirs -> Append(dir[x].dname);
                        }
                        LSTDirs -> SetSelection(0);
                        Print("\nReading Files Header...");
                        // Read Files Header
                        if(pkr)
                            size = 20;
                        else
                            size = 1510;
                        for(unsigned short x = 0; x < size; x++)
                        {
                            FHandler.Read(file[x].fname,32); // File name
                            FHandler.Read(&(file[x].crc32),4); // File CRC32
                            FHandler.Read(&aux,4); // File compression flag
                            if(aux == 0xfffffffe)
                                file[x].zip = false; // Uncompressed 0xfffffffe
                            else
                                file[x].zip = true; // Compressed 0x00000002
                            FHandler.Read(&(file[x].foffset),4); // File offset
                            FHandler.Read(&(file[x].usize),4); // Uncompressed size
                            FHandler.Read(&(file[x].csize),4); // Compressed size
                        }
                        Print("\nReading Files Data...");
                        // Read Files Data
                        for(unsigned short x = 0; x < size; x++)
                        {
                            FHandler.Seek(file[x].foffset,wxFromStart);
                            file[x].fdata = new unsigned char[file[x].csize];
                            FHandler.Read(file[x].fdata,file[x].csize);
                        }
                        MakeFileList(0);
                        Print("\nPKR3 File succesfully readed and loaded to memory");
                        BTNSaveFile -> Enable();
                    }
                    else
                    {
                        if(pkr)
                            Print("\nFile is a PKR3 file but is not MHPB \"Media.pkr\"");
                        else
                            Print("\nFile is a PKR3 file but is not MHPB \"Data.pkr\"");
                    }
                }
                else
                {
                    if(pkr)
                        Print("\nFile is a PKR3 file but is not MHPB \"Media.pkr\"");
                    else
                        Print("\nFile is a PKR3 file but is notMHPB \"Data.pkr\"");
                }    
            }
            else
            {
                Print("\nFile isn't a MHPB PKR3 file...");
            }   
        }
        else
        {
            Print("\nCan't open file...");
        }
        FHandler.Close();
    }
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::BTNOpenDataClick(wxCommandEvent& event)
{
    pkr = false;
	ReadPkr();
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::BTNMediaPkrClick(wxCommandEvent& event)
{
    pkr = true;
	ReadPkr();
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::LSTDirsSelected(wxCommandEvent& event)
{
	MakeFileList(LSTDirs -> GetSelection());
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::LSTFilesAction()
{
    BTNPreview -> Enable(false);
    HTMLBmp -> Show(false);
	EDTTxt -> Show(false);
    switch(GetExtension(file[dir[LSTDirs -> GetSelection()].dcount + LSTFiles -> GetSelection()].fname))
    {
        case 1: 
            if(!file[dir[LSTDirs -> GetSelection()].dcount + LSTFiles -> GetSelection()].zip)
            {
                FHandler.Open("Temp.Wav",wxFile::write);
                FHandler.Write(file[dir[LSTDirs -> GetSelection()].dcount + LSTFiles -> GetSelection()].fdata,file[dir[LSTDirs -> GetSelection()].dcount + LSTFiles -> GetSelection()].csize);
                FHandler.Close();
                PlaySound((LPCSTR)"Temp.Wav", NULL, SND_FILENAME | SND_ASYNC); 
            }
        break;
        case 2: 
            if(!file[dir[LSTDirs -> GetSelection()].dcount + LSTFiles -> GetSelection()].zip)
            {
                FHandler.Open("Temp.Bmp",wxFile::write);
                FHandler.Write(file[dir[LSTDirs -> GetSelection()].dcount + LSTFiles -> GetSelection()].fdata,file[dir[LSTDirs -> GetSelection()].dcount + LSTFiles -> GetSelection()].csize);
                FHandler.Close();
                HTMLBmp -> SetPage("<HTML><BODY Bgcolor=\"Grey\"><IMG SRC=\"Temp.Bmp\"></IMG></BODY></HTML>");
                HTMLBmp -> Show();
            }
            else
            {
                if(CHKPreview -> GetValue())
                    PreviewCBMP();
                else
                {
                    EDTTxt -> SetValue("THE *.BMP FILE IS COMPRESSED!\nUSE: \"Preview a Compressed *.BMP File\" BUTTON TO VIEW THE FILE");
                    EDTTxt -> Show();
                    BTNPreview -> Enable(); 
                }
            }
        break;
        default:
                EDTTxt -> SetValue("UNSUPPORTED FILE FORMAT CAN'T PREVIEW...");
                EDTTxt -> Show();
        break;
    }
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::LSTFilesSelected(wxCommandEvent& event)
{
	LSTFilesAction();
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::LSTFilesDoubleClicked(wxCommandEvent& event)
{
	LSTFilesAction();
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::writezip(const wxChar* path,unsigned short dindex,unsigned short findex,const wxChar* name)
{
    Print("Opening Compressed File...Wait");
    while(!(FHandler.Open(path,wxFile::write)));
    unsigned int dhoffset = 0;
    // Generate Initial Zip Header
    aux = 0x04034b50;
    FHandler.Write(&aux,4);
    dhoffset+=4;
    saux = 0x0014;
    FHandler.Write(&saux,2);
    dhoffset+=2;
    saux = 0x0000;
    FHandler.Write(&saux,2);
    dhoffset+=2;
    saux = 0x0008;
    FHandler.Write(&saux,2);
    dhoffset+=2;
    saux = 0x0000;
    FHandler.Write(&saux,2);
    dhoffset+=2;
    FHandler.Write(&saux,2);
    dhoffset+=2;
    FHandler.Write(&(file[dir[dindex].dcount + findex].crc32),4);
    dhoffset+=4;
    FHandler.Write(&(file[dir[dindex].dcount + findex].csize),4);
    dhoffset+=4;
    FHandler.Write(&(file[dir[dindex].dcount + findex].usize),4);
    dhoffset+=4;
    Str = name;
    saux = Str.Length();
    FHandler.Write(&saux,2);
    dhoffset+=2;
    saux = 0x0000;
    FHandler.Write(&saux,2);
    dhoffset+=2;
    FHandler.Write(Str,Str.Length());
    dhoffset+=Str.Length();
    // Write File Data
    FHandler.Write(&(file[dir[dindex].dcount + findex].fdata[2]),file[dir[dindex].dcount + findex].csize - 6);
    dhoffset+=(file[dir[dindex].dcount + findex].csize - 6);
    // Generate Ending Zip Header
    aux = 0x02014b50;
    FHandler.Write(&aux,4);
    saux = 0x0014;
    FHandler.Write(&saux,2);
    FHandler.Write(&saux,2);
    saux = 0x0000;
    FHandler.Write(&saux,2);
    saux = 0x0008;
    FHandler.Write(&saux,2);
    saux = 0x0000;
    FHandler.Write(&saux,2);
    FHandler.Write(&saux,2);
    FHandler.Write(&(file[dir[dindex].dcount + findex].crc32),4);
    FHandler.Write(&(file[dir[dindex].dcount + findex].csize),4);
    FHandler.Write(&(file[dir[dindex].dcount + findex].usize),4);
    saux = Str.Length();
    FHandler.Write(&saux,2);
    saux = 0x0000;
    FHandler.Write(&saux,2);
    FHandler.Write(&saux,2);
    FHandler.Write(&saux,2);
    FHandler.Write(&saux,2);
    aux = 0x00000020;
    FHandler.Write(&aux,4);
    aux = 0x00000000;
    FHandler.Write(&aux,4);
    FHandler.Write(Str,Str.Length());
    aux = 0x06054b50;
    FHandler.Write(&aux,4);
    FHandler.Write(&saux,2);
    FHandler.Write(&saux,2);
    saux = 0x0001;
    FHandler.Write(&saux,2);
    FHandler.Write(&saux,2);
    aux = 46 + Str.Length();
    FHandler.Write(&aux,4);
    FHandler.Write(&dhoffset,4);
    saux = 0x0000;
    FHandler.Write(&saux,2);
    FHandler.Close();
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::PreviewCBMP()
{
    BTNPreview -> Enable(false);
    HTMLBmp -> Show(false);
	EDTTxt -> Show(false);
	unsigned char temp = 0;
	Print("\n");
	writezip("unzip/Temp.Zip",LSTDirs -> GetSelection(),LSTFiles -> GetSelection(),"Temp.Bmp");
    FHandler.Open("unzip.bin",wxFile::write);
    FHandler.Write(&temp,1);
    FHandler.Close();
    while(!(FHandler.Open("unzip.bat",wxFile::write)));
    FHandler.Write("@echo off\ndel /f /q \"Temp.Bmp\"\n\"unzip/unzip.exe\" \"unzip/Temp.Zip\"\necho EXTRACTED >> Unzip.bin\nexit");
    FHandler.Close();
    system("start unzip.bat");
    while(true)
    {
        if(FHandler.Open("unzip.bin",wxFile::read))
        {
            if(FHandler.Length() > 1)
            {
                FHandler.Close();   
                break;
            }
        }
        FHandler.Close();   
    }
    HTMLBmp -> SetPage("<HTML><BODY BgColor=\"Grey\"><IMG SRC=\"Temp.Bmp\"></IMG></BODY></HTML>");
    HTMLBmp -> Show();
    Print("\nCompressed File Opened!");
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::BTNPreviewClick(wxCommandEvent& event)
{
    PreviewCBMP();
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::BTNExportClick(wxCommandEvent& event)
{
    if(file[dir[LSTDirs -> GetSelection()].dcount + LSTFiles -> GetSelection()].zip)
    {
        Str = file[dir[LSTDirs -> GetSelection()].dcount + LSTFiles -> GetSelection()].fname;
        Str += ".zip";
        DLGExport -> SetFilename(Str);
        DLGExport -> SetWildcard("*.zip");
    }
    else
    {
        DLGExport -> SetFilename(file[dir[LSTDirs -> GetSelection()].dcount + LSTFiles -> GetSelection()].fname);
        DLGExport -> SetWildcard("*.*");
    }
    if(DLGExport -> ShowModal() == wxID_OK)
	{
        if(FHandler.Open(DLGExport -> GetPath(),wxFile::write))
        {
            Print("\nExporting File...");
            if(file[dir[LSTDirs -> GetSelection()].dcount + LSTFiles -> GetSelection()].zip)
            {
                FHandler.Close();
                Print(", ");
                writezip(DLGExport -> GetPath(),LSTDirs -> GetSelection(),LSTFiles -> GetSelection(),file[dir[LSTDirs -> GetSelection()].dcount + LSTFiles -> GetSelection()].fname);
            }
            else
            {
                FHandler.Write(file[dir[LSTDirs -> GetSelection()].dcount + LSTFiles -> GetSelection()].fdata,file[dir[LSTDirs -> GetSelection()].dcount + LSTFiles -> GetSelection()].csize);
                FHandler.Close();
            }
            Print(", File Exported!");
        }    
        else
        {
            Print("\nCan't open destination file...");
            FHandler.Close();
        }
    }
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::BTNImportClick(wxCommandEvent& event)
{
    unsigned short dindex = LSTDirs -> GetSelection();
	unsigned short findex = LSTFiles -> GetSelection();
    if(file[dir[dindex].dcount + findex].zip)
        DLGCaution -> ShowModal();
	if(DLGImport -> ShowModal() == wxID_OK)
	{
        Print("\nImporting File...Wait, ");
        if(FHandler.Open(DLGImport -> GetPath(),wxFile::read))
        {
	        unsigned char auxc = 0;
	        unsigned int hexcrc32 = 0;
	        char strcrc32[9];
	        char *finalptr;
	        while(!(AuxHandler.Open("crc32.bat",wxFile::write)));
	        Str = "\"crc32/Enfis_SFV.exe\" -f=\"crc32.bin\" -p=\"";
	        Str += DLGImport -> GetDirectory();
	        Str += "\" \"";
	        Str += DLGImport -> GetFilename();
	        Str += "\"\nexit";
	        AuxHandler.Write(Str);
	        AuxHandler.Close();
	        Str = DLGImport -> GetDirectory();
	        Str += "\\crc32.bin";
	        while(!(AuxHandler.Open(Str,wxFile::write)));
	        AuxHandler.Write(&auxc,1);
	        AuxHandler.Close();
	        FHandler.Close();
	        system("start crc32.bat");
	        while(true)
	        {
                if(AuxHandler.Open(Str,wxFile::read))
                {
                    if(AuxHandler.Length() > 1)
                    {
                        AuxHandler.Seek(50 + (DLGImport -> GetFilename()).Length(),wxFromStart);
                        AuxHandler.Read(strcrc32,8);
                        strcrc32[8] = '\0';
                        AuxHandler.Close();   
                        hexcrc32 = strtoul(strcrc32,&finalptr,16);
                        break;
                    }
                }
                AuxHandler.Close();
            }
            if(FHandler.Open(DLGImport -> GetPath(),wxFile::read))
            {
               delete[] file[dir[dindex].dcount + findex].fdata;
	           file[dir[dindex].dcount + findex].fdata = NULL;
	           file[dir[dindex].dcount + findex].zip = false;
	           file[dir[dindex].dcount + findex].csize = FHandler.Length();
	           file[dir[dindex].dcount + findex].usize = FHandler.Length();
	           file[dir[dindex].dcount + findex].crc32 = hexcrc32;
	           file[dir[dindex].dcount + findex].fdata = new unsigned char[file[dir[dindex].dcount + findex].csize];
               FHandler.Read(file[dir[dindex].dcount + findex].fdata,file[dir[dindex].dcount + findex].csize);
               Str = "del /f /q ";
	           Str += DLGImport -> GetDirectory();
	           Str += "\\crc32.bin";
	           system(Str);
	           Print(", File Imported!");
            }
            else
            {
                Print(", Can't open source file!");
            }
        }
        else
            Print(", Can't open source file!");
        FHandler.Close();
    }
}

/*******************************************************************************/

void Kamiloxnumetal_s_MHPB_PKR3_EditorFrm::BTNSaveFileClick(wxCommandEvent& event)
{
	if(DLGSaveFile -> ShowModal() == wxID_OK)
	{
        Print("\nSaving File...");
        if(FHandler.Open(DLGSaveFile -> GetPath(),wxFile::write))
        {
            unsigned int offset = 0;
            unsigned int dirs_n;
            unsigned int files_n;
            Print("\nWriting Master PKR3 Header...");
            if(pkr)
            {
                dirs_n = 3;
                files_n = 20;
            }
            else
            {
                dirs_n = 33;
                files_n = 1510;
            }
            aux = 0x33524b50;
            FHandler.Write(&aux,4);
            offset += 8;
            FHandler.Write(&offset,4);
            aux = 0x00000004;
            FHandler.Write(&aux,4);
            FHandler.Write(&dirs_n,4);
            FHandler.Write(&files_n,4);
            offset += 12;
            Print("\nWriting Directory Header...");
            for(unsigned short x = 0; x < dirs_n; x++)
            {
                FHandler.Write(dir[x].dname,32);
                FHandler.Write(&(dir[x].dcount),4);
                FHandler.Write(&(dir[x].dfiles),4);
                offset += 40;
            }
            offset += 52*files_n;
            Print("\nWriting Files Header...");
            for(unsigned short x = 0; x < files_n; x++)
            {
                FHandler.Write(file[x].fname,32);
                FHandler.Write(&(file[x].crc32),4);
                if(file[x].zip)
                    aux = 0x00000002;
                else
                    aux = 0xfffffffe;
                FHandler.Write(&aux,4);
                FHandler.Write(&offset,4);
                FHandler.Write(&(file[x].usize),4);
                FHandler.Write(&(file[x].csize),4);
                offset += file[x].csize;
            }
            Print("\nWriting Files Data...");
            for(unsigned short x = 0; x < files_n; x++)
            {
                FHandler.Write(file[x].fdata,file[x].csize);
            }
            Print("\nMHPB PKR3 File Saved Successfully!");
        }
        else
        {
            Print("\nCan't open the destination file!");
        }
        FHandler.Close();
    }
}

/*******************************************************************************/
