|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +/** |
| 5 | + * Patches a DayZ Server executable. |
| 6 | + * @param {string} filePath Path to the .exe file |
| 7 | + */ |
| 8 | +function patchFile(filePath) { |
| 9 | + if (!fs.existsSync(filePath)) { |
| 10 | + console.error(`Error: File not found at ${filePath}`); |
| 11 | + return false; |
| 12 | + } |
| 13 | + |
| 14 | + console.log(`Reading ${filePath}...`); |
| 15 | + let buffer; |
| 16 | + try { |
| 17 | + buffer = fs.readFileSync(filePath); |
| 18 | + } catch (err) { |
| 19 | + console.error(`Error reading file: ${err.message}`); |
| 20 | + return false; |
| 21 | + } |
| 22 | + |
| 23 | + // Create backup if it doesn't exist or we want to overwrite |
| 24 | + const backupPath = filePath + '.bak'; |
| 25 | + try { |
| 26 | + // Only create backup if it doesn't exist, to preserve the "original" clean copy |
| 27 | + if (!fs.existsSync(backupPath)) { |
| 28 | + fs.writeFileSync(backupPath, buffer); |
| 29 | + console.log(`Backup created: ${backupPath}`); |
| 30 | + } else { |
| 31 | + console.log(`Using existing backup: ${backupPath}`); |
| 32 | + } |
| 33 | + } catch (err) { |
| 34 | + console.error(`Error creating backup: ${err.message}`); |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Scans for a hex pattern and applies a patch if found. |
| 40 | + * @param {string} patternStr Hex string pattern (use '?' for wildcards) |
| 41 | + * @param {number[]|Buffer} patchBytes Bytes to write |
| 42 | + * @param {string} label Descriptive name for the patch |
| 43 | + */ |
| 44 | + function scanAndPatch(patternStr, patchBytes, label) { |
| 45 | + const pattern = patternStr.split(' ').map(x => x === '?' ? null : parseInt(x, 16)); |
| 46 | + |
| 47 | + for (let i = 0; i <= buffer.length - pattern.length; i++) { |
| 48 | + let match = true; |
| 49 | + for (let j = 0; j < pattern.length; j++) { |
| 50 | + if (pattern[j] !== null && buffer[i + j] !== pattern[j]) { |
| 51 | + match = false; |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + if (match) { |
| 56 | + const patchBuf = Buffer.isBuffer(patchBytes) ? patchBytes : Buffer.from(patchBytes); |
| 57 | + patchBuf.copy(buffer, i); |
| 58 | + console.log(`Applied patch: ${label}`); |
| 59 | + return true; |
| 60 | + } |
| 61 | + } |
| 62 | + console.log(`Pattern not found for: ${label}`); |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + let patched = false; |
| 67 | + |
| 68 | + // 1. Patch BattlEye Init |
| 69 | + if (scanAndPatch( |
| 70 | + "40 53 55 56 57 41 54 48 81 EC ? ? ? ? 45 33 E4 48 8B D9 44 89", |
| 71 | + [0xB0, 0x01, 0xC3], |
| 72 | + "BattlEye Init" |
| 73 | + )) patched = true; |
| 74 | + |
| 75 | + // 2. Patch VAC Check |
| 76 | + if (scanAndPatch( |
| 77 | + "74 44 0F B7 C8 E8 ? ? ? ? 8B 13 44 0F B7 C0 44 89 4C 24 ? 48", |
| 78 | + [0xEB], |
| 79 | + "VAC Check" |
| 80 | + )) patched = true; |
| 81 | + |
| 82 | + // 3. Patch Title |
| 83 | + const newTitle = Buffer.alloc(15, 0); |
| 84 | + Buffer.from("Patched version").copy(newTitle); |
| 85 | + if (scanAndPatch( |
| 86 | + "43 6F 6E 73 6F 6C 65 20 76", |
| 87 | + newTitle, |
| 88 | + "Server Title" |
| 89 | + )) patched = true; |
| 90 | + |
| 91 | + if (patched) { |
| 92 | + try { |
| 93 | + fs.writeFileSync(filePath, buffer); |
| 94 | + console.log("File patched and saved successfully."); |
| 95 | + return true; |
| 96 | + } catch (err) { |
| 97 | + console.error(`Error saving patched file: ${err.message}`); |
| 98 | + return false; |
| 99 | + } |
| 100 | + } |
| 101 | + return false; |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Restores the original DayZ Server executable from backup. |
| 106 | + * @param {string} filePath Path to the .exe file |
| 107 | + */ |
| 108 | +function restoreFile(filePath) { |
| 109 | + const backupPath = filePath + '.bak'; |
| 110 | + if (fs.existsSync(backupPath)) { |
| 111 | + try { |
| 112 | + // Check if the current file is actually different (optional, but safer) |
| 113 | + if (fs.existsSync(filePath)) { |
| 114 | + fs.unlinkSync(filePath); |
| 115 | + } |
| 116 | + fs.copyFileSync(backupPath, filePath); |
| 117 | + // We keep the backup so we can patch again later |
| 118 | + console.log(`Restored original file from ${backupPath}`); |
| 119 | + return true; |
| 120 | + } catch (err) { |
| 121 | + console.error(`Error restoring file: ${err.message}`); |
| 122 | + return false; |
| 123 | + } |
| 124 | + } else { |
| 125 | + console.log("No backup found to restore."); |
| 126 | + return false; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +module.exports = { |
| 131 | + patchFile, |
| 132 | + restoreFile |
| 133 | +}; |
| 134 | + |
| 135 | +// CLI Execution |
| 136 | +if (require.main === module) { |
| 137 | + const target = process.argv[2]; |
| 138 | + if (target) { |
| 139 | + patchFile(path.resolve(target)); |
| 140 | + } else { |
| 141 | + console.log("Usage: node patcher.js <path_to_dayz_server_exe>"); |
| 142 | + } |
| 143 | +} |
0 commit comments