Trong Posted 12 hours ago Posted 12 hours ago Helper App: expandcollapse popup#include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <Clipboard.au3> #include <File.au3> #include <Array.au3> #include <ListViewConstants.au3> ; ================================================================================================= ; Title .........: Files To Hex Generator ; Description ...: A tool to read multiple binary files and generate complete, formatted ; AutoIt functions containing the files' hex data with architecture detection. ; Supports both single and multiple file processing via GUI. ; Author(s) .....: Dao Van Trong - TRONG.PRO ; ================================================================================================= ; Global variables for GUI controls Global $g_hGUI, $g_hListView, $g_hEditOutput, $g_hInputChunkSize Global $g_aSelectedFiles[0][5] ; Array to store selected files info - FIXED: Initialize as 2D array Global $g_sGeneratedCode = "" ; Store the complete generated code ; Check if running in command line mode If $CmdLine[0] > 0 Then _ProcessCommandLine() Else _MainGUI() EndIf Func _ProcessCommandLine() Local $aFiles[0] ; Collect all valid files from command line arguments For $i = 1 To $CmdLine[0] If FileExists($CmdLine[$i]) Then _ArrayAdd($aFiles, $CmdLine[$i]) Else ConsoleWrite("Warning: File not found - " & $CmdLine[$i] & @CRLF) EndIf Next If UBound($aFiles) = 0 Then ConsoleWrite("Error: No valid files provided." & @CRLF) ConsoleWrite("Usage: " & @ScriptName & " <file1> [file2] [file3] ..." & @CRLF) Exit 1 EndIf Local $sOutputContent = "" Local $sOutputFile = "" If UBound($aFiles) = 1 Then ; Single file processing Local $sDrive, $sDir, $sFileName, $sExtension _PathSplit($aFiles[0], $sDrive, $sDir, $sFileName, $sExtension) $sOutputFile = $sDrive & $sDir & $sFileName & "_Embedded.au3" ConsoleWrite("Processing single file: " & $aFiles[0] & @CRLF) Local $sFuncName = _GenerateFunctionName($aFiles[0]) $sOutputContent = _GenerateHexFunction($aFiles[0], $sFuncName) If $sOutputContent = "" Then ConsoleWrite("Error: Failed to process file - " & $aFiles[0] & @CRLF) Exit 1 EndIf $sOutputContent &= _GenerateHelperFunction($sFuncName, $sFileName & $sExtension) Else ; Multiple files processing $sOutputFile = @ScriptDir & "\All_Embedded.au3" ConsoleWrite("Processing " & UBound($aFiles) & " files into: " & $sOutputFile & @CRLF) For $i = 0 To UBound($aFiles) - 1 ConsoleWrite(" Processing: " & $aFiles[$i] & @CRLF) Local $sFuncName = _GenerateFunctionName($aFiles[$i]) Local $sFuncContent = _GenerateHexFunction($aFiles[$i], $sFuncName) If $sFuncContent = "" Then ConsoleWrite(" Warning: Failed to process - " & $aFiles[$i] & @CRLF) ContinueLoop EndIf $sOutputContent &= $sFuncContent & @CRLF & @CRLF Next $sOutputContent &= _GenerateMasterHelperFunction($aFiles) EndIf ; Save the output file Local $hFile = FileOpen($sOutputFile, 2) If $hFile = -1 Then ConsoleWrite("Error: Could not create output file - " & $sOutputFile & @CRLF) Exit 1 EndIf FileWrite($hFile, $sOutputContent) FileClose($hFile) ConsoleWrite("Success: Output saved to - " & $sOutputFile & @CRLF) EndFunc ;==>_ProcessCommandLine Func _MainGUI() $g_hGUI = GUICreate("Files To Hex Generator by Dao Van Trong - TRONG.PRO", 800, 650) GUISetBkColor(0xF0F0F0) ; --- File Selection Section --- GUICtrlCreateLabel("1. File Selection:", 10, 15, 150, 20) GUICtrlSetFont(-1, 9, 600) Local $hBtnAddFiles = GUICtrlCreateButton("Add Files...", 10, 35, 100, 25) Local $hBtnRemoveSelected = GUICtrlCreateButton("Remove Selected", 120, 35, 120, 25) Local $hBtnClearAll = GUICtrlCreateButton("Clear All", 250, 35, 80, 25) ; ListView to display selected files $g_hListView = GUICtrlCreateListView("File Name|Size|Architecture|Function Name", 10, 70, 780, 150) GUICtrlSendMsg($g_hListView, $LVM_SETCOLUMNWIDTH, 0, 250) GUICtrlSendMsg($g_hListView, $LVM_SETCOLUMNWIDTH, 1, 100) GUICtrlSendMsg($g_hListView, $LVM_SETCOLUMNWIDTH, 2, 100) GUICtrlSendMsg($g_hListView, $LVM_SETCOLUMNWIDTH, 3, 320) ; --- Options Section --- GUICtrlCreateLabel("2. Generation Options:", 10, 235, 150, 20) GUICtrlSetFont(-1, 9, 600) GUICtrlCreateLabel("Line Length (chars):", 20, 260, 120, 20) $g_hInputChunkSize = GUICtrlCreateInput("4000", 150, 257, 100, 22) Local $hCheckAddHelpers = GUICtrlCreateCheckbox("Include helper functions", 270, 257, 150, 22) GUICtrlSetState($hCheckAddHelpers, $GUI_CHECKED) ; --- Generation Section --- Local $hBtnGenerate = GUICtrlCreateButton("3. Generate All Functions", 10, 290, 200, 35) GUICtrlSetFont(-1, 10, 700) GUICtrlSetBkColor(-1, 0x90EE90) Local $hBtnPreview = GUICtrlCreateButton("Preview Single", 220, 290, 100, 35) ; --- Output Section --- GUICtrlCreateLabel("Generated Code:", 10, 340, 150, 20) GUICtrlSetFont(-1, 9, 600) $g_hEditOutput = GUICtrlCreateEdit("", 10, 360, 780, 220, $WS_VSCROLL + $WS_HSCROLL) GUICtrlSetFont(-1, 9, 400, 0, "Consolas") ; --- Action Buttons --- Local $hBtnCopy = GUICtrlCreateButton("Copy to Clipboard", 10, 590, 130, 30) Local $hBtnSaveAll = GUICtrlCreateButton("Save All to File", 150, 590, 130, 30) Local $hBtnSaveSeparate = GUICtrlCreateButton("Save Separate Files", 290, 590, 130, 30) Local $hBtnExit = GUICtrlCreateButton("Exit", 660, 590, 130, 30) GUICtrlSetBkColor($hBtnExit, 0xFFB6C1) GUISetState(@SW_SHOW, $g_hGUI) ; --- GUI Event Loop --- While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $hBtnExit ExitLoop Case $hBtnAddFiles _HandleAddFiles() Case $hBtnRemoveSelected _HandleRemoveSelected() Case $hBtnClearAll _HandleClearAll() Case $hBtnGenerate _HandleGenerateAll($hCheckAddHelpers) Case $hBtnPreview _HandlePreviewSingle() Case $hBtnCopy _HandleCopyToClipboard() Case $hBtnSaveAll _HandleSaveAll($hCheckAddHelpers) Case $hBtnSaveSeparate _HandleSaveSeparate($hCheckAddHelpers) EndSwitch WEnd GUIDelete($g_hGUI) EndFunc ;==>_MainGUI Func _HandleAddFiles() Local $sFiles = FileOpenDialog("Select Files", @ScriptDir, "All Files (*.*)", 4) ; 4 = Multiple selection If @error Then Return Local $aNewFiles = StringSplit($sFiles, "|") If $aNewFiles[0] = 1 Then ; Single file selected _AddFileToList($aNewFiles[1]) Else ; Multiple files selected Local $sBasePath = $aNewFiles[1] For $i = 2 To $aNewFiles[0] Local $sFullPath = $sBasePath & "\" & $aNewFiles[$i] _AddFileToList($sFullPath) Next EndIf EndFunc ;==>_HandleAddFiles Func _AddFileToList($sFilePath) If Not FileExists($sFilePath) Then Return ; Check if file already exists in list For $i = 0 To UBound($g_aSelectedFiles) - 1 If UBound($g_aSelectedFiles, 1) > 0 And $g_aSelectedFiles[$i][0] = $sFilePath Then Return ; File already added Next ; Get file info Local $sFileName = _GetFileName($sFilePath) Local $iFileSize = FileGetSize($sFilePath) Local $sFileSize = _FormatFileSize($iFileSize) Local $sArchitecture = _DetectArchitecture($sFilePath) Local $sFuncName = _GenerateFunctionName($sFilePath) ; Add to array - FIXED: Use ReDim to resize and add new row Local $iCurrentSize = UBound($g_aSelectedFiles) ReDim $g_aSelectedFiles[$iCurrentSize + 1][5] $g_aSelectedFiles[$iCurrentSize][0] = $sFilePath $g_aSelectedFiles[$iCurrentSize][1] = $sFileName $g_aSelectedFiles[$iCurrentSize][2] = $sFileSize $g_aSelectedFiles[$iCurrentSize][3] = $sArchitecture $g_aSelectedFiles[$iCurrentSize][4] = $sFuncName ; Add to ListView Local $hItem = GUICtrlCreateListViewItem($sFileName & "|" & $sFileSize & "|" & $sArchitecture & "|" & $sFuncName, $g_hListView) EndFunc ;==>_AddFileToList Func _HandleRemoveSelected() Local $iSelected = GUICtrlSendMsg($g_hListView, $LVM_GETNEXTITEM, -1, $LVNI_SELECTED) If $iSelected = -1 Then Return ; No selection ; Remove from array - FIXED: Use proper 2D array deletion Local $iArraySize = UBound($g_aSelectedFiles) If $iSelected >= 0 And $iSelected < $iArraySize Then ; Shift elements down For $i = $iSelected To $iArraySize - 2 For $j = 0 To 4 $g_aSelectedFiles[$i][$j] = $g_aSelectedFiles[$i + 1][$j] Next Next ; Resize array ReDim $g_aSelectedFiles[$iArraySize - 1][5] EndIf ; Rebuild ListView _RefreshListView() EndFunc ;==>_HandleRemoveSelected Func _HandleClearAll() ReDim $g_aSelectedFiles[0][5] ; FIXED: Maintain 2D structure _RefreshListView() GUICtrlSetData($g_hEditOutput, "") $g_sGeneratedCode = "" EndFunc ;==>_HandleClearAll Func _RefreshListView() ; Clear ListView GUICtrlSendMsg($g_hListView, $LVM_DELETEALLITEMS, 0, 0) ; Repopulate ListView For $i = 0 To UBound($g_aSelectedFiles) - 1 If UBound($g_aSelectedFiles, 1) > 0 Then ; Check if array has elements Local $sData = $g_aSelectedFiles[$i][1] & "|" & $g_aSelectedFiles[$i][2] & "|" & $g_aSelectedFiles[$i][3] & "|" & $g_aSelectedFiles[$i][4] GUICtrlCreateListViewItem($sData, $g_hListView) EndIf Next EndFunc ;==>_RefreshListView Func _HandleGenerateAll($hCheckAddHelpers) If UBound($g_aSelectedFiles) = 0 Then MsgBox(48, "Warning", "Please add at least one file first.") Return EndIf Local $iChunkSize = Number(GUICtrlRead($g_hInputChunkSize)) If $iChunkSize <= 0 Then $iChunkSize = 4000 GUICtrlSetData($g_hEditOutput, "Generating code, please wait...") Sleep(100) Local $sOutput = "; Generated by Dao Van Trong - TRONG.PRO" & @CRLF $sOutput &= "; Total files: " & UBound($g_aSelectedFiles) & @CRLF $sOutput &= "; Generated on: " & @YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & @CRLF & @CRLF ; Generate hex functions for all files For $i = 0 To UBound($g_aSelectedFiles) - 1 Local $sFilePath = $g_aSelectedFiles[$i][0] Local $sFuncName = $g_aSelectedFiles[$i][4] Local $sFuncCode = _GenerateHexFunction($sFilePath, $sFuncName, "$sHexData", $iChunkSize) If $sFuncCode <> "" Then $sOutput &= $sFuncCode & @CRLF & @CRLF EndIf Next ; Add helper functions if requested If GUICtrlRead($hCheckAddHelpers) = $GUI_CHECKED Then If UBound($g_aSelectedFiles) = 1 Then $sOutput &= _GenerateHelperFunction($g_aSelectedFiles[0][4], $g_aSelectedFiles[0][1]) Else $sOutput &= _GenerateMasterHelperFunctionFromArray($g_aSelectedFiles) EndIf EndIf $g_sGeneratedCode = $sOutput GUICtrlSetData($g_hEditOutput, $sOutput) EndFunc ;==>_HandleGenerateAll Func _HandlePreviewSingle() Local $iSelected = GUICtrlSendMsg($g_hListView, $LVM_GETNEXTITEM, -1, $LVNI_SELECTED) If $iSelected = -1 Then MsgBox(48, "Warning", "Please select a file from the list first.") Return EndIf If $iSelected >= UBound($g_aSelectedFiles) Then MsgBox(48, "Error", "Invalid selection.") Return EndIf Local $iChunkSize = _ValidateChunkSize(GUICtrlRead($g_hInputChunkSize)) ;If $iChunkSize <= 0 Then $iChunkSize = 4000 GUICtrlSetData($g_hEditOutput, "Generating preview, please wait...") Sleep(100) Local $sFilePath = $g_aSelectedFiles[$iSelected][0] Local $sFuncName = $g_aSelectedFiles[$iSelected][4] Local $sOutput = "; Preview for: " & $g_aSelectedFiles[$iSelected][1] & @CRLF $sOutput &= "; Architecture: " & $g_aSelectedFiles[$iSelected][3] & @CRLF & @CRLF $sOutput &= _GenerateHexFunction($sFilePath, $sFuncName, "$sHexData", $iChunkSize) GUICtrlSetData($g_hEditOutput, $sOutput) EndFunc ;==>_HandlePreviewSingle Func _ValidateChunkSize($iChunkSize) If $iChunkSize < 100 Or $iChunkSize > 4000 Then ;MsgBox(48, "Warning", "Chunk size need 100-4000 chars", 3) Return 4000 ; default EndIf Return $iChunkSize EndFunc ;==>_ValidateChunkSize Func _HandleCopyToClipboard() Local $sCode = GUICtrlRead($g_hEditOutput) If StringStripWS($sCode, 8) = "" Then MsgBox(48, "Warning", "No code to copy. Please generate code first.") Return EndIf _ClipBoard_SetData($sCode) ToolTip("Code copied to clipboard!", @DesktopWidth / 2, @DesktopHeight / 2, "Success", 1, 1) Sleep(1500) ToolTip("") EndFunc ;==>_HandleCopyToClipboard Func _HandleSaveAll($hCheckAddHelpers) If UBound($g_aSelectedFiles) = 0 Or $g_sGeneratedCode = "" Then MsgBox(48, "Warning", "No code to save. Please generate code first.") Return EndIf Local $sSaveFile = FileSaveDialog("Save Combined Code", @ScriptDir, "AutoIt Scripts (*.au3)", 16, "All_Embedded.au3") If @error Then Return Local $hFile = FileOpen($sSaveFile, 2) If $hFile = -1 Then MsgBox(16, "Error", "Could not create file: " & $sSaveFile) Return EndIf FileWrite($hFile, $g_sGeneratedCode) FileClose($hFile) MsgBox(64, "Success", "All functions saved to: " & @CRLF & $sSaveFile) EndFunc ;==>_HandleSaveAll Func _HandleSaveSeparate($hCheckAddHelpers) If UBound($g_aSelectedFiles) = 0 Then MsgBox(48, "Warning", "No files to process.") Return EndIf Local $sSaveDir = FileSelectFolder("Select folder to save separate files", @ScriptDir) If @error Then Return Local $iChunkSize = Number(GUICtrlRead($g_hInputChunkSize)) If $iChunkSize <= 0 Then $iChunkSize = 4000 Local $bAddHelpers = (GUICtrlRead($hCheckAddHelpers) = $GUI_CHECKED) Local $iSaved = 0 For $i = 0 To UBound($g_aSelectedFiles) - 1 Local $sFilePath = $g_aSelectedFiles[$i][0] Local $sFileName = $g_aSelectedFiles[$i][1] Local $sFuncName = $g_aSelectedFiles[$i][4] Local $sDrive, $sDir, $sNameOnly, $sExt _PathSplit($sFilePath, $sDrive, $sDir, $sNameOnly, $sExt) Local $sSaveFile = $sSaveDir & "\" & $sNameOnly & "_Embedded.au3" Local $sCode = "; Generated from: " & $sFileName & @CRLF $sCode &= "; Architecture: " & $g_aSelectedFiles[$i][3] & @CRLF & @CRLF $sCode &= _GenerateHexFunction($sFilePath, $sFuncName, "$sHexData", $iChunkSize) If $bAddHelpers Then $sCode &= _GenerateHelperFunction($sFuncName, $sFileName) EndIf Local $hFile = FileOpen($sSaveFile, 2) If $hFile <> -1 Then FileWrite($hFile, $sCode) FileClose($hFile) $iSaved += 1 EndIf Next MsgBox(64, "Success", "Saved " & $iSaved & " of " & UBound($g_aSelectedFiles) & " files to:" & @CRLF & $sSaveDir) EndFunc ;==>_HandleSaveSeparate Func _GenerateFunctionName($sFilePath) Local $sDrive, $sDir, $sFileName, $sExtension _PathSplit($sFilePath, $sDrive, $sDir, $sFileName, $sExtension) Local $sCleanName = _SanitizeName($sFileName) Local $sArch = _DetectArchitecture($sFilePath) Return "_GetBinData_" & $sCleanName & "_" & $sArch EndFunc ;==>_GenerateFunctionName ; ----------------------------------------------------------------------------- ; Function: _DetectArchitecture ; Purpose : Wrapper to call detailed PE architecture detection ; ----------------------------------------------------------------------------- Func _DetectArchitecture($sFilePath) Local $aInfo = _GetDetailedFileInfo($sFilePath) If Not @error Then Return $aInfo[2] EndIf Return "Unknown" EndFunc ;==>_DetectArchitecture ; ============================================================================= ; Function: _DetectArchitecture_File ; Purpose : Read PE headers to identify CPU architecture (x86, x64, ARM, etc.) ; ============================================================================= Func _DetectArchitecture_File($sFilePath) If Not FileExists($sFilePath) Then Return SetError(1, 0, "FILE_NOT_FOUND") EndIf Local $hFile = FileOpen($sFilePath, 16) ; binary mode If $hFile = -1 Then Return SetError(2, 0, "CANNOT_OPEN_FILE") EndIf ; Read DOS header Local $bDOSHeader = FileRead($hFile, 64) If @error Then FileClose($hFile) Return SetError(3, 0, "CANNOT_READ_DOS_HEADER") EndIf ; Validate "MZ" If BinaryMid($bDOSHeader, 1, 2) <> "0x4D5A" Then FileClose($hFile) Return SetError(4, 0, "INVALID_DOS_SIGNATURE") EndIf ; Get PE header offset Local $iPEOffset = _BinaryToInt(BinaryMid($bDOSHeader, 61, 4)) FileSetPos($hFile, $iPEOffset, 0) Local $bPESig = FileRead($hFile, 4) If @error Or $bPESig <> "0x50450000" Then FileClose($hFile) Return SetError(5, 0, "INVALID_PE_SIGNATURE") EndIf ; Read COFF header Local $bCOFF = FileRead($hFile, 20) Local $iMachine = _BinaryToInt(BinaryMid($bCOFF, 1, 2)) FileClose($hFile) Switch $iMachine Case 0x014c Return "x86" Case 0x8664 Return "x64" Case 0x01c0, 0x01c4 Return "ARM" Case 0xAA64 Return "ARM64" Case 0x0200 Return "IA64" Case 0x0166, 0x0169 Return "MIPS" Case 0x01a2 Return "SH3" Case 0x01a3 Return "SH3DSP" Case 0x01a6 Return "SH4" Case 0x01a8 Return "SH5" Case 0x01c2 Return "THUMB" Case Else Return "UNKNOWN_0x" & Hex($iMachine, 4) EndSwitch EndFunc ;==>_DetectArchitecture_File ; ----------------------------------------------------------------------------- ; Function: _BinaryToInt ; Purpose : Convert a little-endian Binary to integer ; ----------------------------------------------------------------------------- Func _BinaryToInt($bData) Local $iResult = 0, $iLen = BinaryLen($bData) For $i = 1 To $iLen $iResult += Number(BinaryMid($bData, $i, 1)) * (256 ^ ($i - 1)) Next Return $iResult EndFunc ;==>_BinaryToInt ; ----------------------------------------------------------------------------- ; Function: _GetDetailedFileInfo ; Purpose : Retrieve filename, architecture, and size for a given file ; ----------------------------------------------------------------------------- Func _GetDetailedFileInfo($sFilePath) Local $sArch = _DetectArchitecture_File($sFilePath) If @error Then Return SetError(@error, @extended, $sArch) Local $aInfo[4] $aInfo[0] = 3 $aInfo[1] = StringRegExpReplace($sFilePath, ".*\\", "") $aInfo[2] = $sArch $aInfo[3] = FileGetSize($sFilePath) Return $aInfo EndFunc ;==>_GetDetailedFileInfo Func _GenerateHexFunction($sFilePath, $sFuncName, $sVarName = "$sHexData", $iChunkSize = 4000) Local $bData = FileRead($sFilePath) If @error Or $bData = "" Then ConsoleWrite("Error reading file: " & $sFilePath & @CRLF) Return "" EndIf ;Local $sHexWithPrefix = StringTrimRight(StringToBinary($bData), 2) ;Local $sHex = StringReplace($sHexWithPrefix, "0x", "") Local $sHexWithPrefix = StringToBinary($bData) Local $sHex = StringMid($sHexWithPrefix, 3) ; Loại bỏ "0x" ; Ensure variable name starts with $ If StringLeft($sVarName, 1) <> "$" Then $sVarName = "$" & $sVarName ; Calculate safe chunk size Local $iLineOverhead = StringLen("Local " & $sVarName & " = '0x'") Local $iMaxSafeChunkSize = 4096 - $iLineOverhead If $iChunkSize > $iMaxSafeChunkSize Then $iChunkSize = $iMaxSafeChunkSize ; Get filename for comment Local $sFileName = _GetFileName($sFilePath) ; Build the function Local $sOutput = "" $sOutput &= "Func " & $sFuncName & "()" & @CRLF $sOutput &= @TAB & '; This function holds the hex data for ' & $sFileName & @CRLF $sOutput &= @TAB & '; File size: ' & _FormatFileSize(FileGetSize($sFilePath)) & @CRLF $sOutput &= @TAB & '; Architecture: ' & _DetectArchitecture($sFilePath) & @CRLF $sOutput &= @TAB & '; Generated by Dao Van Trong - TRONG.PRO' & @CRLF $sOutput &= @TAB & "Local " & $sVarName & " = '0x" & StringMid($sHex, 1, $iChunkSize) & "'" & @CRLF For $i = $iChunkSize + 1 To StringLen($sHex) Step $iChunkSize $sOutput &= @TAB & $sVarName & " &= '" & StringMid($sHex, $i, $iChunkSize) & "'" & @CRLF Next $sOutput &= @CRLF $sOutput &= @TAB & "Return " & $sVarName & @CRLF $sOutput &= "EndFunc ;==>" & $sFuncName & @CRLF Return $sOutput EndFunc ;==>_GenerateHexFunction Func _GenerateHelperFunction($sFuncName, $sOriginalFileName) Local $sHelperFunc = @CRLF & @CRLF $sHelperFunc &= '; =================================================================' & @CRLF $sHelperFunc &= '; Helper function to deploy the file from hex data.' & @CRLF $sHelperFunc &= '; =================================================================' & @CRLF $sHelperFunc &= 'Func _DeployFileFromHex()' & @CRLF $sHelperFunc &= @TAB & 'Local $sHexData = ' & $sFuncName & '()' & @CRLF $sHelperFunc &= @TAB & 'If $sHexData = "" Then' & @CRLF $sHelperFunc &= @TAB & @TAB & 'MsgBox(16, "Error", "Hex data is empty. Cannot deploy file.")' & @CRLF $sHelperFunc &= @TAB & @TAB & 'Return SetError(1, 0, "")' & @CRLF $sHelperFunc &= @TAB & 'EndIf' & @CRLF $sHelperFunc &= @CRLF $sHelperFunc &= @TAB & 'Local $sOutputFilename = "' & $sOriginalFileName & '"' & @CRLF $sHelperFunc &= @TAB & 'Local $sTempFilePath = @TempDir & "\" & $sOutputFilename' & @CRLF $sHelperFunc &= @TAB & 'Local $hFile = FileOpen($sTempFilePath, 18) ; 18 = Overwrite + Create Path' & @CRLF $sHelperFunc &= @TAB & 'If $hFile = -1 Then' & @CRLF $sHelperFunc &= @TAB & @TAB & 'MsgBox(16, "Error", "Failed to open file for writing at: " & $sTempFilePath)' & @CRLF $sHelperFunc &= @TAB & @TAB & 'Return SetError(2, 0, "")' & @CRLF $sHelperFunc &= @TAB & 'EndIf' & @CRLF $sHelperFunc &= @CRLF $sHelperFunc &= @TAB & 'FileWrite($hFile, BinaryToString($sHexData))' & @CRLF $sHelperFunc &= @TAB & 'FileClose($hFile)' & @CRLF $sHelperFunc &= @CRLF $sHelperFunc &= @TAB & 'If Not FileExists($sTempFilePath) Then' & @CRLF $sHelperFunc &= @TAB & @TAB & 'MsgBox(16, "Error", "Failed to write file to: " & $sTempFilePath)' & @CRLF $sHelperFunc &= @TAB & @TAB & 'Return SetError(3, 0, "")' & @CRLF $sHelperFunc &= @TAB & 'EndIf' & @CRLF $sHelperFunc &= @CRLF $sHelperFunc &= @TAB & 'MsgBox(64, "Success", "File deployed successfully to:" & @CRLF & $sTempFilePath)' & @CRLF $sHelperFunc &= @TAB & 'Return $sTempFilePath' & @CRLF $sHelperFunc &= 'EndFunc ;==>_DeployFileFromHex' & @CRLF & @CRLF $sHelperFunc &= '; Example usage: _DeployFileFromHex()' & @CRLF Return $sHelperFunc EndFunc ;==>_GenerateHelperFunction Func _GenerateMasterHelperFunction($aFiles) Local $sHelperFunc = @CRLF & @CRLF $sHelperFunc &= '; =================================================================' & @CRLF $sHelperFunc &= '; Master helper function to deploy all embedded files.' & @CRLF $sHelperFunc &= '; =================================================================' & @CRLF $sHelperFunc &= 'Func _DeployAllFiles()' & @CRLF $sHelperFunc &= @TAB & 'Local $aResults[0][2] ; [filepath, success/error]' & @CRLF $sHelperFunc &= @CRLF For $i = 0 To UBound($aFiles) - 1 Local $sFuncName = _GenerateFunctionName($aFiles[$i]) Local $sFileName = _GetFileName($aFiles[$i]) $sHelperFunc &= @TAB & '; Deploy ' & $sFileName & @CRLF $sHelperFunc &= @TAB & 'Local $sHexData' & $i & ' = ' & $sFuncName & '()' & @CRLF $sHelperFunc &= @TAB & 'If $sHexData' & $i & ' <> "" Then' & @CRLF $sHelperFunc &= @TAB & @TAB & 'Local $sOutputPath' & $i & ' = @TempDir & "\' & $sFileName & '"' & @CRLF $sHelperFunc &= @TAB & @TAB & 'Local $hFile' & $i & ' = FileOpen($sOutputPath' & $i & ', 18)' & @CRLF $sHelperFunc &= @TAB & @TAB & 'If $hFile' & $i & ' <> -1 Then' & @CRLF $sHelperFunc &= @TAB & @TAB & @TAB & 'FileWrite($hFile' & $i & ', BinaryToString($sHexData' & $i & '))' & @CRLF $sHelperFunc &= @TAB & @TAB & @TAB & 'FileClose($hFile' & $i & ')' & @CRLF $sHelperFunc &= @TAB & @TAB & @TAB & '_ArrayAdd($aResults, $sOutputPath' & $i & ' & "|Success")' & @CRLF $sHelperFunc &= @TAB & @TAB & 'Else' & @CRLF $sHelperFunc &= @TAB & @TAB & @TAB & '_ArrayAdd($aResults, "' & $sFileName & '|Failed to create file")' & @CRLF $sHelperFunc &= @TAB & @TAB & 'EndIf' & @CRLF $sHelperFunc &= @TAB & 'Else' & @CRLF $sHelperFunc &= @TAB & @TAB & '_ArrayAdd($aResults, "' & $sFileName & '|Failed to get hex data")' & @CRLF $sHelperFunc &= @TAB & 'EndIf' & @CRLF $sHelperFunc &= @CRLF Next $sHelperFunc &= @TAB & '; Display results' & @CRLF $sHelperFunc &= @TAB & 'Local $sReport = "Deployment Results:" & @CRLF & @CRLF' & @CRLF $sHelperFunc &= @TAB & 'For $i = 0 To UBound($aResults) - 1' & @CRLF $sHelperFunc &= @TAB & @TAB & 'Local $aParts = StringSplit($aResults[$i], "|")' & @CRLF $sHelperFunc &= @TAB & @TAB & '$sReport &= $aParts[1] & " - " & $aParts[2] & @CRLF' & @CRLF $sHelperFunc &= @TAB & 'Next' & @CRLF $sHelperFunc &= @CRLF $sHelperFunc &= @TAB & 'MsgBox(64, "Deployment Complete", $sReport)' & @CRLF $sHelperFunc &= @TAB & 'Return $aResults' & @CRLF $sHelperFunc &= 'EndFunc ;==>_DeployAllFiles' & @CRLF & @CRLF $sHelperFunc &= '; Example usage: _DeployAllFiles()' & @CRLF Return $sHelperFunc EndFunc ;==>_GenerateMasterHelperFunction Func _GenerateMasterHelperFunctionFromArray($aSelectedFiles) Local $sHelperFunc = @CRLF & @CRLF $sHelperFunc &= '; =================================================================' & @CRLF $sHelperFunc &= '; Master helper function to deploy all embedded files.' & @CRLF $sHelperFunc &= '; =================================================================' & @CRLF $sHelperFunc &= 'Func _DeployAllFiles()' & @CRLF $sHelperFunc &= @TAB & 'Local $aResults[0][3] ; [filename, filepath, status]' & @CRLF $sHelperFunc &= @CRLF For $i = 0 To UBound($aSelectedFiles) - 1 Local $sFileName = $aSelectedFiles[$i][1] Local $sFuncName = $aSelectedFiles[$i][4] $sHelperFunc &= @TAB & '; Deploy ' & $sFileName & ' (' & $aSelectedFiles[$i][3] & ')' & @CRLF $sHelperFunc &= @TAB & 'Local $sHexData' & $i & ' = ' & $sFuncName & '()' & @CRLF $sHelperFunc &= @TAB & 'If $sHexData' & $i & ' <> "" Then' & @CRLF $sHelperFunc &= @TAB & @TAB & 'Local $sOutputPath' & $i & ' = @TempDir & "\' & $sFileName & '"' & @CRLF $sHelperFunc &= @TAB & @TAB & 'Local $hFile' & $i & ' = FileOpen($sOutputPath' & $i & ', 18)' & @CRLF $sHelperFunc &= @TAB & @TAB & 'If $hFile' & $i & ' <> -1 Then' & @CRLF $sHelperFunc &= @TAB & @TAB & @TAB & 'FileWrite($hFile' & $i & ', BinaryToString($sHexData' & $i & '))' & @CRLF $sHelperFunc &= @TAB & @TAB & @TAB & 'FileClose($hFile' & $i & ')' & @CRLF $sHelperFunc &= @TAB & @TAB & @TAB & 'If FileExists($sOutputPath' & $i & ') Then' & @CRLF $sHelperFunc &= @TAB & @TAB & @TAB & @TAB & '_ArrayAdd($aResults, "' & $sFileName & '|" & $sOutputPath' & $i & ' & "|Success")' & @CRLF $sHelperFunc &= @TAB & @TAB & @TAB & 'Else' & @CRLF $sHelperFunc &= @TAB & @TAB & @TAB & @TAB & '_ArrayAdd($aResults, "' & $sFileName & '|N/A|Write failed")' & @CRLF $sHelperFunc &= @TAB & @TAB & @TAB & 'EndIf' & @CRLF $sHelperFunc &= @TAB & @TAB & 'Else' & @CRLF $sHelperFunc &= @TAB & @TAB & @TAB & '_ArrayAdd($aResults, "' & $sFileName & '|N/A|Cannot create file")' & @CRLF $sHelperFunc &= @TAB & @TAB & 'EndIf' & @CRLF $sHelperFunc &= @TAB & 'Else' & @CRLF $sHelperFunc &= @TAB & @TAB & '_ArrayAdd($aResults, "' & $sFileName & '|N/A|No hex data")' & @CRLF $sHelperFunc &= @TAB & 'EndIf' & @CRLF $sHelperFunc &= @CRLF Next $sHelperFunc &= @TAB & '; Display results' & @CRLF $sHelperFunc &= @TAB & 'Local $sReport = "Deployment Results (" & UBound($aResults) & " files):" & @CRLF & @CRLF' & @CRLF $sHelperFunc &= @TAB & 'For $i = 0 To UBound($aResults) - 1' & @CRLF $sHelperFunc &= @TAB & @TAB & 'Local $aParts = StringSplit($aResults[$i], "|")' & @CRLF $sHelperFunc &= @TAB & @TAB & 'If $aParts[0] >= 3 Then' & @CRLF $sHelperFunc &= @TAB & @TAB & @TAB & '$sReport &= "• " & $aParts[1] & ": " & $aParts[3]' & @CRLF $sHelperFunc &= @TAB & @TAB & @TAB & 'If $aParts[3] = "Success" Then $sReport &= " → " & $aParts[2]' & @CRLF $sHelperFunc &= @TAB & @TAB & @TAB & '$sReport &= @CRLF' & @CRLF $sHelperFunc &= @TAB & @TAB & 'EndIf' & @CRLF $sHelperFunc &= @TAB & 'Next' & @CRLF $sHelperFunc &= @CRLF $sHelperFunc &= @TAB & 'MsgBox(64, "Deployment Complete", $sReport)' & @CRLF $sHelperFunc &= @TAB & 'Return $aResults' & @CRLF $sHelperFunc &= 'EndFunc ;==>_DeployAllFiles' & @CRLF & @CRLF $sHelperFunc &= '; Example usage: _DeployAllFiles()' & @CRLF Return $sHelperFunc EndFunc ;==>_GenerateMasterHelperFunctionFromArray ; Helper functions Func _SanitizeName($sInput) If StringLeft($sInput, 1) = "$" Then $sInput = StringTrimLeft($sInput, 1) Return StringRegExpReplace($sInput, "[^a-zA-Z0-9_]", "") EndFunc ;==>_SanitizeName Func _GetFileName($sFilePath) Local $sDrive, $sDir, $sFileName, $sExtension _PathSplit($sFilePath, $sDrive, $sDir, $sFileName, $sExtension) Return $sFileName & $sExtension EndFunc ;==>_GetFileName Func _GetFileExtension($sFilePath) Local $sDrive, $sDir, $sFileName, $sExtension _PathSplit($sFilePath, $sDrive, $sDir, $sFileName, $sExtension) Return $sExtension EndFunc ;==>_GetFileExtension Func _FormatFileSize($iBytes) If $iBytes < 1024 Then Return $iBytes & " B" ElseIf $iBytes < 1024 * 1024 Then Return Round($iBytes / 1024, 1) & " KB" ElseIf $iBytes < 1024 * 1024 * 1024 Then Return Round($iBytes / (1024 * 1024), 1) & " MB" Else Return Round($iBytes / (1024 * 1024 * 1024), 2) & " GB" EndIf EndFunc ;==>_FormatFileSize ! Parsix and argumentum 2 Regards,
argumentum Posted 7 hours ago Posted 7 hours ago Q & A Q: Why no compression in your code ? A: Because it all depends. If you convert this to an executable, aut2exe will use internal compression and the resultant file is smaller if the "file function" is not pre-compressed. Parsix 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now