|
| 1 | +; ============================================================================ |
| 2 | +; CitraConfigHelpers.ahk - v2 Helper Functions for Citra Configuration |
| 3 | +; Replaces tf.ahk dependency with native v2 functions |
| 4 | +; Author: Migrated from v1 - 2025-12-26 |
| 5 | +; ============================================================================ |
| 6 | + |
| 7 | +#Requires AutoHotkey v2.0 |
| 8 | + |
| 9 | +; ============================================================================ |
| 10 | +; RegExEscape(str) - Escape regex special characters |
| 11 | +; |
| 12 | +; Escapes special regex characters to allow literal string matching |
| 13 | +; |
| 14 | +; Parameters: |
| 15 | +; str - String to escape |
| 16 | +; |
| 17 | +; Returns: |
| 18 | +; Escaped string safe for use in regex patterns |
| 19 | +; ============================================================================ |
| 20 | +RegExEscape(str) { |
| 21 | + static specials := "()[]{}?*+|^$.\" |
| 22 | + out := "" |
| 23 | + for char in StrSplit(str) |
| 24 | + out .= InStr(specials, char) ? "\" char : char |
| 25 | + return out |
| 26 | +} |
| 27 | + |
| 28 | +; ============================================================================ |
| 29 | +; SetKey(content, key, value) - Set or replace INI key value |
| 30 | +; |
| 31 | +; Modifies an INI-style configuration string by setting a key=value pair. |
| 32 | +; If the key exists, replaces its value. If not, appends the key=value. |
| 33 | +; |
| 34 | +; Parameters: |
| 35 | +; content - Configuration file content as string |
| 36 | +; key - Key name (e.g., "resolution_factor") |
| 37 | +; value - Value to set |
| 38 | +; |
| 39 | +; Returns: |
| 40 | +; Modified configuration content |
| 41 | +; ============================================================================ |
| 42 | +SetKey(content, key, value) { |
| 43 | + pat := "m)^(" . RegExEscape(key) . ")\s*=.*$" |
| 44 | + if RegExMatch(content, pat) |
| 45 | + return RegExReplace(content, pat, "$1=" value, , 1) |
| 46 | + else |
| 47 | + return content "`n" key "=" value |
| 48 | +} |
| 49 | + |
| 50 | +; ============================================================================ |
| 51 | +; LoadConfig(configFile) - Load configuration file |
| 52 | +; |
| 53 | +; Reads configuration file into string for manipulation |
| 54 | +; |
| 55 | +; Parameters: |
| 56 | +; configFile - Path to configuration file |
| 57 | +; |
| 58 | +; Returns: |
| 59 | +; File content as string, or empty string if file doesn't exist |
| 60 | +; ============================================================================ |
| 61 | +LoadConfig(configFile) { |
| 62 | + cfg := "" |
| 63 | + if FileExist(configFile) |
| 64 | + cfg := FileRead(configFile) |
| 65 | + return cfg |
| 66 | +} |
| 67 | + |
| 68 | +; ============================================================================ |
| 69 | +; SaveConfig(cfg, configFile) - Save configuration file |
| 70 | +; |
| 71 | +; Writes configuration string to file with backup |
| 72 | +; |
| 73 | +; Parameters: |
| 74 | +; cfg - Configuration content to write |
| 75 | +; configFile - Path to configuration file |
| 76 | +; |
| 77 | +; Returns: |
| 78 | +; true on success, false on failure |
| 79 | +; ============================================================================ |
| 80 | +SaveConfig(cfg, configFile) { |
| 81 | + ; Create backup before modifying |
| 82 | + if FileExist(configFile) { |
| 83 | + try { |
| 84 | + FileCopy(configFile, configFile . ".bak", 1) |
| 85 | + } catch { |
| 86 | + ; Backup failed, but continue anyway |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + ; Write new config |
| 91 | + try { |
| 92 | + if FileExist(configFile) |
| 93 | + FileDelete(configFile) |
| 94 | + FileAppend(cfg, configFile) |
| 95 | + return true |
| 96 | + } catch as err { |
| 97 | + MsgBox("Error saving config: " . err.Message, "Config Save Error", 16) |
| 98 | + return false |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +; ============================================================================ |
| 103 | +; ReplaceInFile(filePath, searchText, replaceText) - Replace text in file |
| 104 | +; |
| 105 | +; Simplified replacement of TF_Replace functionality |
| 106 | +; Replaces all occurrences of searchText with replaceText in a file |
| 107 | +; |
| 108 | +; Parameters: |
| 109 | +; filePath - Path to file to modify |
| 110 | +; searchText - Text to search for |
| 111 | +; replaceText - Text to replace with |
| 112 | +; |
| 113 | +; Returns: |
| 114 | +; true on success, false on failure |
| 115 | +; ============================================================================ |
| 116 | +ReplaceInFile(filePath, searchText, replaceText) { |
| 117 | + try { |
| 118 | + cfg := LoadConfig(filePath) |
| 119 | + if (cfg = "") |
| 120 | + return false |
| 121 | + |
| 122 | + cfg := StrReplace(cfg, searchText, replaceText) |
| 123 | + return SaveConfig(cfg, filePath) |
| 124 | + } catch { |
| 125 | + return false |
| 126 | + } |
| 127 | +} |
0 commit comments