diff --git a/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL.ncb b/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL.ncb index ee37b5d..10c54bd 100644 Binary files a/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL.ncb and b/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL.ncb differ diff --git a/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL.suo b/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL.suo index c91fdb1..4bdf792 100644 Binary files a/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL.suo and b/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL.suo differ diff --git a/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/FingerPrinterTWL.vcproj b/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/FingerPrinterTWL.vcproj index cd611b2..d2e39b0 100644 --- a/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/FingerPrinterTWL.vcproj +++ b/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/FingerPrinterTWL.vcproj @@ -219,6 +219,14 @@ RelativePath="..\..\MasterEditorTWL\MasterEditorTWL\keys.cpp" > + + + + @@ -263,6 +271,14 @@ RelativePath=".\resource.h" > + + + + diff --git a/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/Form1.cpp b/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/Form1.cpp index 33b7728..29cb6a1 100644 --- a/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/Form1.cpp +++ b/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/Form1.cpp @@ -5,6 +5,8 @@ #include #include #include +#include "MasterEditorTWL/split_tad.h" +#include "MasterEditorTWL/split_tad_util.h" #include "fingerprint_util.h" using namespace FingerPrinterTWL; @@ -22,12 +24,13 @@ void Form1::construct() System::String ^example = "Input Example\r\n\r\n" + "(1)String of ASCII Charactors\r\n" - + " *THIS IS FINGERPRINT\r\n\r\n" + + " *ABCD\r\n" + + " => register \"41424344\" (\'T\' \'E\' \'S\' \'T\')\r\n\r\n" + "(2)Hexadecimal Numbers\r\n" + " *5A6B78014A235A\r\n" + " *5a6b78014a235a\r\n" + " *5A 6B 78 01 4A 23 5A\r\n" - + " (2 charactor per 1 number)"; + + " => register \"5A6B78014A235A\""; this->tboxExample->Text = example; } @@ -59,8 +62,21 @@ void Form1::fingerprintRomHeader() } pin_ptr buf = &bytes[0]; // 解放の必要なし memcpy( this->rh->s.reserved_C, buf, 32 ); + SignRomHeader( this->rh ); // 署名 } +System::Boolean Form1::isFingerprint() +{ + int i; + for( i=0; i < 32; i++ ) + { + if( this->rh->s.reserved_C[i] != 0 ) + { + return true; + } + } + return false; +} // ------------------------------------------------------------------ // ファイル処理を共通化させる @@ -68,49 +84,97 @@ void Form1::fingerprintRomHeader() void Form1::commonOpenRom( System::String ^srcpath ) { + System::Boolean bTad = System::IO::Path::GetExtension(srcpath)->ToUpper()->Equals(".TAD"); + + if( bTad ) + { + System::Console::WriteLine( "*** Transform TAD => SRL ***" ); + System::String ^tmp = ".\\tmp" + System::DateTime::Now.ToString("yyyyMMddHHmmss") + ".srl"; + if( splitTad(srcpath, tmp) < 0 ) + { + throw gcnew Exception("Fail to transform TAD => SRL."); + } + this->srlbin = ReadBin(tmp); + System::IO::File::Delete(tmp); // 中間ファイルを削除する + } + else + { + this->srlbin = ReadBin(srcpath); + } memset( this->rh, 0, sizeof(ROM_Header) ); - ReadRomHeader( srcpath, this->rh ); - this->tboxFile->Text = srcpath; - this->tboxFile->SelectionStart = srcpath->Length; // 入りきらないときに右端を表示する + ExtractRomHeader( this->srlbin, this->rh ); } void Form1::commonSaveRom( System::String ^dstpath ) { + System::String ^bindir = System::IO::Path::GetDirectoryName( System::Reflection::Assembly::GetEntryAssembly()->Location ); + if( !bindir->EndsWith("\\") ) + { + bindir = bindir + "\\"; + } + System::String ^maketad_path = bindir + "maketad.exe"; + System::String ^srcpath = this->tboxFile->Text; if( !srcpath ) { throw gcnew Exception("The input ROM file has not read yet."); } - // ファイルをコピーしてROMヘッダを上書き - if( !srcpath->Equals( dstpath ) ) + if( this->rTad->Checked && !(this->rh->s.titleID_Hi && TITLE_ID_MEDIA_MASK) ) { - CopyFile( srcpath, dstpath ); + throw gcnew Exception("Cannot make TAD file for software intended for Game Card."); } - this->fingerprintRomHeader(); - WriteRomHeader( dstpath, this->rh ); -} + this->fingerprintRomHeader(); + OverwriteRomHeader( this->srlbin, this->rh ); + + if( this->rTad->Checked ) + { + System::Console::WriteLine( "*** Transform SRL => TAD ***" ); + System::String ^tmp = ".\\tmp" + System::DateTime::Now.ToString("yyyyMMddHHmmss") + ".srl"; + WriteBin( tmp, this->srlbin ); + makeTad( maketad_path, tmp, dstpath ); + } + else + { + WriteBin( dstpath, this->srlbin ); + } +} // ------------------------------------------------------------------ // ボタンが押されたときの処理 (catch文必須) // ------------------------------------------------------------------ -void Form1::procOpenRomButton() +void Form1::procOpenRomButton( System::String ^path ) { try { -// System::String ^rompath = OpenFileUsingDialog( this->prevDir, "rom format (*.srl;*.tad)|*.srl;*.tad|All files (*.*)|*.*" ); - System::String ^rompath = OpenFileUsingDialog( this->prevDir, "rom format (*.srl)|*.srl|All files (*.*)|*.*" ); + System::String ^rompath = nullptr; + if( !path ) + { + rompath = OpenFileUsingDialog( this->prevDir, "rom format (*.srl;*.tad)|*.srl;*.tad|All files (*.*)|*.*" ); + } + else + { + rompath = System::String::Copy(path); + } if( !rompath ) { return; } this->commonOpenRom( rompath ); - this->prevDir = System::IO::Path::GetDirectoryName( rompath ); + this->tboxFile->Text = rompath; + this->tboxFile->SelectionStart = rompath->Length; // 入りきらないときに右端を表示する + this->rSrl->Checked = true; + this->rTad->Checked = false; + if( !path ) + { + this->prevDir = System::IO::Path::GetDirectoryName( rompath ); // あらかじめパスが指定されたときには更新しない + } } catch( System::Exception ^ex ) { + this->tboxFile->Text = ""; // クリア this->errMsg( ex->Message ); } } @@ -119,7 +183,20 @@ void Form1::procSaveRomButton() { try { - System::String ^rompath = SaveFileUsingDialog( this->prevDir, "rom format (*.srl)|*.srl|All files (*.*)|*.*", ".srl" ); + System::String ^format = nullptr; + System::String ^ext = nullptr; + if( this->rTad->Checked ) + { + format = "rom format (*.tad)|*.tad|All files (*.*)|*.*"; + ext = ".tad"; + } + else + { + format = "rom format (*.srl)|*.srl|All files (*.*)|*.*"; + ext = ".srl"; + } + + System::String ^rompath = SaveFileUsingDialog( this->prevDir, format, ext ); if( !rompath ) { return; diff --git a/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/Form1.h b/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/Form1.h index c358888..c3b1b2a 100644 --- a/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/Form1.h +++ b/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/Form1.h @@ -69,6 +69,12 @@ namespace FingerPrinterTWL { private: System::Windows::Forms::Label^ labFP; + private: System::Windows::Forms::Button^ button1; + private: System::Windows::Forms::Label^ label1; + private: System::Windows::Forms::GroupBox^ gboxFormat; + private: System::Windows::Forms::RadioButton^ rTad; + private: System::Windows::Forms::RadioButton^ rSrl; + private: @@ -96,13 +102,19 @@ namespace FingerPrinterTWL { this->gboxType = (gcnew System::Windows::Forms::GroupBox()); this->rHex = (gcnew System::Windows::Forms::RadioButton()); this->rString = (gcnew System::Windows::Forms::RadioButton()); + this->button1 = (gcnew System::Windows::Forms::Button()); + this->label1 = (gcnew System::Windows::Forms::Label()); + this->gboxFormat = (gcnew System::Windows::Forms::GroupBox()); + this->rSrl = (gcnew System::Windows::Forms::RadioButton()); + this->rTad = (gcnew System::Windows::Forms::RadioButton()); this->gboxFP->SuspendLayout(); this->gboxType->SuspendLayout(); + this->gboxFormat->SuspendLayout(); this->SuspendLayout(); // // tboxFile // - this->tboxFile->Location = System::Drawing::Point(85, 31); + this->tboxFile->Location = System::Drawing::Point(85, 26); this->tboxFile->Name = L"tboxFile"; this->tboxFile->ReadOnly = true; this->tboxFile->Size = System::Drawing::Size(313, 19); @@ -111,7 +123,7 @@ namespace FingerPrinterTWL { // labFile // this->labFile->AutoSize = true; - this->labFile->Location = System::Drawing::Point(26, 34); + this->labFile->Location = System::Drawing::Point(26, 29); this->labFile->Name = L"labFile"; this->labFile->Size = System::Drawing::Size(53, 12); this->labFile->TabIndex = 1; @@ -119,7 +131,7 @@ namespace FingerPrinterTWL { // // butFile // - this->butFile->Location = System::Drawing::Point(404, 29); + this->butFile->Location = System::Drawing::Point(404, 24); this->butFile->Name = L"butFile"; this->butFile->Size = System::Drawing::Size(82, 23); this->butFile->TabIndex = 2; @@ -129,22 +141,23 @@ namespace FingerPrinterTWL { // // gboxFP // + this->gboxFP->Controls->Add(this->gboxFormat); this->gboxFP->Controls->Add(this->labFP); this->gboxFP->Controls->Add(this->labShorter); this->gboxFP->Controls->Add(this->butFP); this->gboxFP->Controls->Add(this->tboxExample); this->gboxFP->Controls->Add(this->tboxFP); this->gboxFP->Controls->Add(this->gboxType); - this->gboxFP->Location = System::Drawing::Point(28, 71); + this->gboxFP->Location = System::Drawing::Point(28, 74); this->gboxFP->Name = L"gboxFP"; - this->gboxFP->Size = System::Drawing::Size(458, 261); + this->gboxFP->Size = System::Drawing::Size(458, 299); this->gboxFP->TabIndex = 3; this->gboxFP->TabStop = false; // // labFP // this->labFP->AutoSize = true; - this->labFP->Location = System::Drawing::Point(20, 150); + this->labFP->Location = System::Drawing::Point(20, 191); this->labFP->Name = L"labFP"; this->labFP->Size = System::Drawing::Size(60, 12); this->labFP->TabIndex = 5; @@ -153,7 +166,7 @@ namespace FingerPrinterTWL { // labShorter // this->labShorter->AutoSize = true; - this->labShorter->Location = System::Drawing::Point(320, 204); + this->labShorter->Location = System::Drawing::Point(320, 245); this->labShorter->Name = L"labShorter"; this->labShorter->Size = System::Drawing::Size(124, 12); this->labShorter->TabIndex = 4; @@ -161,7 +174,7 @@ namespace FingerPrinterTWL { // // butFP // - this->butFP->Location = System::Drawing::Point(151, 225); + this->butFP->Location = System::Drawing::Point(144, 267); this->butFP->Name = L"butFP"; this->butFP->Size = System::Drawing::Size(150, 23); this->butFP->TabIndex = 3; @@ -172,16 +185,16 @@ namespace FingerPrinterTWL { // tboxExample // this->tboxExample->BackColor = System::Drawing::SystemColors::Info; - this->tboxExample->Location = System::Drawing::Point(232, 18); + this->tboxExample->Location = System::Drawing::Point(201, 18); this->tboxExample->Multiline = true; this->tboxExample->Name = L"tboxExample"; this->tboxExample->ReadOnly = true; - this->tboxExample->Size = System::Drawing::Size(212, 122); + this->tboxExample->Size = System::Drawing::Size(243, 159); this->tboxExample->TabIndex = 2; // // tboxFP // - this->tboxFP->Location = System::Drawing::Point(22, 165); + this->tboxFP->Location = System::Drawing::Point(22, 206); this->tboxFP->Multiline = true; this->tboxFP->Name = L"tboxFP"; this->tboxFP->Size = System::Drawing::Size(422, 36); @@ -193,7 +206,7 @@ namespace FingerPrinterTWL { this->gboxType->Controls->Add(this->rString); this->gboxType->Location = System::Drawing::Point(22, 18); this->gboxType->Name = L"gboxType"; - this->gboxType->Size = System::Drawing::Size(204, 113); + this->gboxType->Size = System::Drawing::Size(161, 77); this->gboxType->TabIndex = 0; this->gboxType->TabStop = false; this->gboxType->Text = L"Input Type"; @@ -201,7 +214,7 @@ namespace FingerPrinterTWL { // rHex // this->rHex->AutoSize = true; - this->rHex->Location = System::Drawing::Point(18, 68); + this->rHex->Location = System::Drawing::Point(12, 49); this->rHex->Name = L"rHex"; this->rHex->Size = System::Drawing::Size(137, 16); this->rHex->TabIndex = 1; @@ -213,19 +226,73 @@ namespace FingerPrinterTWL { // this->rString->AutoSize = true; this->rString->Checked = true; - this->rString->Location = System::Drawing::Point(18, 37); + this->rString->Location = System::Drawing::Point(12, 23); this->rString->Name = L"rString"; - this->rString->Size = System::Drawing::Size(160, 16); + this->rString->Size = System::Drawing::Size(112, 16); this->rString->TabIndex = 0; this->rString->TabStop = true; - this->rString->Text = L"String of ASCII Charactors"; + this->rString->Text = L"ASCII Charactors"; this->rString->UseVisualStyleBackColor = true; // + // button1 + // + this->button1->Location = System::Drawing::Point(328, 392); + this->button1->Name = L"button1"; + this->button1->Size = System::Drawing::Size(158, 23); + this->button1->TabIndex = 4; + this->button1->Text = L"About This Application"; + this->button1->UseVisualStyleBackColor = true; + // + // label1 + // + this->label1->AutoSize = true; + this->label1->Location = System::Drawing::Point(138, 50); + this->label1->Name = L"label1"; + this->label1->Size = System::Drawing::Size(260, 12); + this->label1->TabIndex = 5; + this->label1->Text = L"Supported ROM type: TWL application (SRL/TAD)"; + // + // gboxFormat + // + this->gboxFormat->Controls->Add(this->rTad); + this->gboxFormat->Controls->Add(this->rSrl); + this->gboxFormat->Location = System::Drawing::Point(22, 101); + this->gboxFormat->Name = L"gboxFormat"; + this->gboxFormat->Size = System::Drawing::Size(161, 76); + this->gboxFormat->TabIndex = 6; + this->gboxFormat->TabStop = false; + this->gboxFormat->Text = L"Output Format"; + // + // rSrl + // + this->rSrl->AutoSize = true; + this->rSrl->Checked = true; + this->rSrl->Location = System::Drawing::Point(12, 24); + this->rSrl->Name = L"rSrl"; + this->rSrl->Size = System::Drawing::Size(44, 16); + this->rSrl->TabIndex = 0; + this->rSrl->TabStop = true; + this->rSrl->Text = L"SRL"; + this->rSrl->UseVisualStyleBackColor = true; + // + // rTad + // + this->rTad->AutoSize = true; + this->rTad->Location = System::Drawing::Point(12, 49); + this->rTad->Name = L"rTad"; + this->rTad->Size = System::Drawing::Size(46, 16); + this->rTad->TabIndex = 1; + this->rTad->TabStop = true; + this->rTad->Text = L"TAD"; + this->rTad->UseVisualStyleBackColor = true; + // // Form1 // this->AllowDrop = true; this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::None; - this->ClientSize = System::Drawing::Size(503, 352); + this->ClientSize = System::Drawing::Size(512, 427); + this->Controls->Add(this->label1); + this->Controls->Add(this->button1); this->Controls->Add(this->gboxFP); this->Controls->Add(this->butFile); this->Controls->Add(this->labFile); @@ -238,6 +305,8 @@ namespace FingerPrinterTWL { this->gboxFP->PerformLayout(); this->gboxType->ResumeLayout(false); this->gboxType->PerformLayout(); + this->gboxFormat->ResumeLayout(false); + this->gboxFormat->PerformLayout(); this->ResumeLayout(false); this->PerformLayout(); @@ -249,7 +318,8 @@ namespace FingerPrinterTWL { // ================================================================================ private: ROM_Header *rh; - System::String ^prevDir; // 前に選択したディレクトリ + cli::array ^srlbin; // SRLのバイナリ + System::String ^prevDir; // 前に選択したディレクトリ private: void construct(); @@ -258,6 +328,7 @@ namespace FingerPrinterTWL { // フィンガープリント private: void fingerprintRomHeader(); + System::Boolean isFingerprint(); // ファイル処理を共通化させる private: @@ -266,7 +337,7 @@ namespace FingerPrinterTWL { // ボタンが押されたときの処理(例外catchを入れること) private: - void procOpenRomButton(); + void procOpenRomButton( System::String ^path ); void procSaveRomButton(); // ダイアログ @@ -282,7 +353,7 @@ namespace FingerPrinterTWL { private: System::Void butFile_Click(System::Object^ sender, System::EventArgs^ e) { - this->procOpenRomButton(); + this->procOpenRomButton(nullptr); } private: System::Void butFP_Click(System::Object^ sender, System::EventArgs^ e) @@ -303,7 +374,7 @@ namespace FingerPrinterTWL { { array ^files = dynamic_cast< array ^>(e->Data->GetData( DataFormats::FileDrop ) ); String ^path = files[0]; - this->commonOpenRom( path ); + this->procOpenRomButton( path ); // ボタンが押されたときと同じ挙動 } }; } diff --git a/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/fingerprint_util.h b/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/fingerprint_util.h index 4e6960a..02c90cd 100644 --- a/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/fingerprint_util.h +++ b/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/fingerprint_util.h @@ -7,8 +7,11 @@ // ファイル処理 // ---------------------------------------------------------------------- -// ファイルコピー -void CopyFile( System::String ^srcpath, System::String ^dstpath ); +// ファイル全体の読み込み +cli::array^ ReadBin( System::String ^path ); + +// ファイル全体のライト +void WriteBin( System::String ^path, cli::array ^bin ); // ROMヘッダのリード void ReadRomHeader( System::String ^srlpath, ROM_Header *dstrh ); @@ -16,6 +19,15 @@ void ReadRomHeader( System::String ^srlpath, ROM_Header *dstrh ); // ROMヘッダのライト void WriteRomHeader( System::String ^srlpath, ROM_Header *srcrh ); +// バイナリからのROMヘッダの抽出 +void ExtractRomHeader( cli::array ^bin, ROM_Header *dstrh ); + +// バイナリへのROMヘッダの上書き +void OverwriteRomHeader( cli::array ^bin, ROM_Header *srcrh ); + +// ファイルコピー +void CopyFile( System::String ^srcpath, System::String ^dstpath ); + // ---------------------------------------------------------------------- // 署名 // ---------------------------------------------------------------------- @@ -52,3 +64,13 @@ System::String^ OpenFileUsingDialog( System::String ^defdir, System::String ^fil // @arg [in] ファイルの拡張子が不正なときに追加するときの正しい拡張子 // @ret 取得したファイル名 エラーのとき nullptr System::String^ SaveFileUsingDialog( System::String ^defdir, System::String ^filter, System::String ^extension ); + +// ---------------------------------------------------------------------- +// 外部プログラムの実行 +// ---------------------------------------------------------------------- + +// maketadの実行 +// @arg [in] maketadのパス +// @arg [in] 入力SRLのパス +// @arg [in] 出力SRLのパス +void makeTad( System::String ^maketad_path, System::String ^srlpath, System::String ^tadpath ); diff --git a/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/fingerprinter_util.cpp b/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/fingerprinter_util.cpp index 4a26756..e24de53 100644 --- a/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/fingerprinter_util.cpp +++ b/build/tools/MasterEditor/FingerPrinterTWL/FingerPrinterTWL/fingerprinter_util.cpp @@ -14,6 +14,178 @@ using namespace MasterEditorTWL; // ファイル処理 // ---------------------------------------------------------------------- +// ファイル全体の読み込み +cli::array^ ReadBin( System::String ^path ) +{ + System::IntPtr ptr = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi( path ); + const char *pch = (const char*)ptr.ToPointer(); + FILE *fp = NULL; + System::Exception ^ex = nullptr; + cli::array ^bin = nullptr; + + try + { + if( fopen_s( &fp, pch, "rb" ) != NULL ) + { + ex = gcnew System::Exception( "Fail to open file:" + path ); + throw ex; + } + + // ファイルサイズ分のメモリ割り当て + fseek( fp, 0, SEEK_END ); + int size = ftell(fp); + bin = gcnew cli::array(size); + pin_ptr pbin = &bin[0]; + + fseek( fp, 0, SEEK_SET ); + if( fread( (void*)pbin, 1, size, fp ) != size ) + { + ex = gcnew System::Exception( "Fail to read data from " + path ); + throw ex; + } + } + finally + { + System::Runtime::InteropServices::Marshal::FreeHGlobal( ptr ); + if( fp ) fclose(fp); + if( ex ) + { + throw ex; + } + } + return bin; +} + +// ファイル全体のライト +void WriteBin( System::String ^path, cli::array ^bin ) +{ + System::IntPtr ptr = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi( path ); + const char *pch = (const char*)ptr.ToPointer(); + FILE *fp = NULL; + System::Exception ^ex = nullptr; + + try + { + if( fopen_s( &fp, pch, "w+b" ) != NULL ) // 上書き・バイナリ + { + ex = gcnew System::Exception( "Fail to open file:" + path ); + throw ex; + } + + int size = bin->Length; + pin_ptr pbin = &bin[0]; + + (void)fseek( fp, 0, SEEK_SET ); + if( fwrite( (const void*)pbin, 1, size, fp ) != size ) + { + ex = gcnew System::Exception( "Fail to write data to " + path ); + throw ex; + } + } + finally + { + System::Runtime::InteropServices::Marshal::FreeHGlobal( ptr ); + if( fp ) fclose( fp ); + if( ex ) + { + throw ex; + } + } +} + +// ROMヘッダのリード +void ReadRomHeader( System::String ^srlpath, ROM_Header *dstrh ) +{ + System::IntPtr ptr = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi( srlpath ); + const char *pchFile = (const char*)ptr.ToPointer(); + FILE *fp = NULL; + System::Exception ^ex = nullptr; + + try + { + if( fopen_s( &fp, pchFile, "rb" ) != NULL ) + { + ex = gcnew System::Exception( "Fail to open file:" + srlpath ); + throw ex; + } + fseek( fp, 0, SEEK_SET ); + if( fread( (void*)dstrh, 1, sizeof(ROM_Header), fp ) != sizeof(ROM_Header) ) + { + ex = gcnew System::Exception( "Fail to read data from " + srlpath ); + throw ex; + } + } + finally + { + System::Runtime::InteropServices::Marshal::FreeHGlobal( ptr ); + if( fp ) fclose(fp); + if( ex ) + { + throw ex; + } + } +} + +// ROMヘッダのライト +void WriteRomHeader( System::String ^srlpath, ROM_Header *srcrh ) +{ + System::IntPtr ptr = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi( srlpath ); + const char *pchFile = (const char*)ptr.ToPointer(); + FILE *fp = NULL; + System::Exception ^ex = nullptr; + + try + { + if( fopen_s( &fp, pchFile, "r+b" ) != NULL ) // 上書き・バイナリ + { + ex = gcnew System::Exception( "Fail to open file:" + srlpath ); + throw ex; + } + (void)fseek( fp, 0, SEEK_SET ); + + if( fwrite( (const void*)srcrh, 1, sizeof(ROM_Header), fp ) != sizeof(ROM_Header) ) + { + ex = gcnew System::Exception( "Fail to write data to " + srlpath ); + throw ex; + } + } + finally + { + System::Runtime::InteropServices::Marshal::FreeHGlobal( ptr ); + if( fp ) fclose( fp ); + if( ex ) + { + throw ex; + } + } +} + +// バイナリからのROMヘッダの抽出 +void ExtractRomHeader( cli::array ^bin, ROM_Header *dstrh ) +{ + int size = bin->Length; + pin_ptr pbin = &bin[0]; + + if( size < sizeof(ROM_Header) ) + { + throw gcnew System::Exception( "The binary size is less than size of the ROM Header." ); + } + memcpy( dstrh, pbin, sizeof(ROM_Header) ); +} + +// バイナリへのROMヘッダの上書き +void OverwriteRomHeader( cli::array ^bin, ROM_Header *srcrh ) +{ + int size = bin->Length; + pin_ptr pbin = &bin[0]; + + if( size < sizeof(ROM_Header) ) + { + throw gcnew System::Exception( "The binary size is less than size of the ROM Header." ); + } + memcpy( pbin, srcrh, sizeof(ROM_Header) ); +} + // ファイルコピー #define COPY_FILE_BUFSIZE (10*1024*1024) void CopyFile( System::String ^srcpath, System::String ^dstpath ) @@ -77,74 +249,6 @@ void CopyFile( System::String ^srcpath, System::String ^dstpath ) } } -// ROMヘッダのリード -void ReadRomHeader( System::String ^srlpath, ROM_Header *dstrh ) -{ - System::IntPtr ptr = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi( srlpath ); - const char *pchFile = (const char*)ptr.ToPointer(); - FILE *fp = NULL; - System::Exception ^ex = nullptr; - - try - { - if( fopen_s( &fp, pchFile, "rb" ) != NULL ) - { - ex = gcnew System::Exception( "Fail to open file:" + srlpath ); - throw ex; - } - fseek( fp, 0, SEEK_SET ); - if( fread( (void*)dstrh, 1, sizeof(ROM_Header), fp ) != sizeof(ROM_Header) ) - { - ex = gcnew System::Exception( "Fail to read data from " + srlpath ); - throw ex; - } - } - finally - { - System::Runtime::InteropServices::Marshal::FreeHGlobal( ptr ); - if( fp ) fclose(fp); - if( ex ) - { - throw ex; - } - } -} - -// ROMヘッダのライト -void WriteRomHeader( System::String ^srlpath, ROM_Header *srcrh ) -{ - System::IntPtr ptr = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi( srlpath ); - const char *pchFile = (const char*)ptr.ToPointer(); - FILE *fp = NULL; - System::Exception ^ex = nullptr; - - // ファイルにROMヘッダをライト - try - { - if( fopen_s( &fp, pchFile, "r+b" ) != NULL ) // 上書き・バイナリ - { - ex = gcnew System::Exception( "Fail to open file:" + srlpath ); - throw ex; - } - (void)fseek( fp, 0, SEEK_SET ); - - if( fwrite( (const void*)srcrh, 1, sizeof(ROM_Header), fp ) != sizeof(ROM_Header) ) - { - ex = gcnew System::Exception( "Fail to write data to " + srlpath ); - throw ex; - } - } - finally - { - System::Runtime::InteropServices::Marshal::FreeHGlobal( ptr ); - if( fp ) fclose( fp ); - if( ex ) - { - throw ex; - } - } -} - // ---------------------------------------------------------------------- // 署名 // ---------------------------------------------------------------------- @@ -321,7 +425,6 @@ System::String^ OpenFileUsingDialog( System::String ^defdir, System::String ^fil { return nullptr; } - defdir = System::IO::Path::GetDirectoryName( dlg->FileName ); // デフォルトディレクトリの更新 return System::String::Copy(dlg->FileName); } @@ -351,7 +454,6 @@ System::String^ SaveFileUsingDialog( System::String ^defdir, System::String ^fil { return nullptr; } - defdir = System::IO::Path::GetDirectoryName( dlg->FileName ); retfile = dlg->FileName; if( !System::String::IsNullOrEmpty(extension) && !(dlg->FileName->ToLower()->EndsWith( extension->ToLower() )) ) { @@ -359,3 +461,16 @@ System::String^ SaveFileUsingDialog( System::String ^defdir, System::String ^fil } return retfile; } + +// ---------------------------------------------------------------------- +// 外部プログラムの実行 +// ---------------------------------------------------------------------- + +// maketadの実行 +// @arg [in] maketadのパス +// @arg [in] 入力SRLのパス +// @arg [in] 出力SRLのパス +void makeTad( System::String ^maketad_path, System::String ^srlpath, System::String ^tadpath ) +{ + System::Diagnostics::Process::Start( maketad_path, "\"" + srlpath + "\" -o \"" + tadpath + "\"" ); +}