/********************************************************************************
 * File: utils.hpp
 * Author: ppkantorski
 * Description:
 *   This header file contains utility functions and macros used in the
 *   Ultrahand Overlay project. These functions and macros include definitions for
 *   various button keys, path variables, and command interpretation and execution.
 *
 *   For the latest updates and contributions, visit the project's GitHub repository.
 *   (GitHub Repository: https://github.com/ppkantorski/Ultrahand-Overlay)
 *
 *   Note: Please be aware that this notice cannot be altered or removed. It is a part
 *   of the project's documentation and must remain intact.
 *
 *  Licensed under GPLv2
 *  Copyright (c) 2023-2026 ppkantorski
 ********************************************************************************/

#pragma once
#include <ultra.hpp>
#include <tesla.hpp>
#include <switch.h>
#include <payload.hpp> // Studious Pancake
#include <util.hpp> // Studious Pancake

#include <stdio.h>
#include <fnmatch.h>
#include <numeric>
#include <queue>
#include <mutex>
#include <condition_variable>


using namespace ult;

static std::atomic<bool> abortCommand(false);
static std::atomic<bool> triggerExit(false);

std::atomic<bool> exitingUltrahand{false};
std::atomic<bool> isDownloadCommand{false};
// Set (once, right where "open" actually queues its launch — never on a failed/no-op
// "open") by the "open" command handler below. Consumed and reset in main.cpp's
// handleInterpreterCompletion() to show LAUNCH_SYMBOL instead of CHECKMARK_SYMBOL on the
// list item, since the usual "this finished, here's your checkmark" feedback is misleading
// for a command that's actually about to hand off to another overlay.
std::atomic<bool> lastCommandLaunchesOverlay{false};
std::atomic<bool> commandSuccess{false};
inline void setCommandFailed();
inline void setCommandResult(bool result);
std::atomic<bool> refreshPage{false};
std::atomic<bool> refreshPackage{false};
std::atomic<bool> skipJumpReset{false};
std::atomic<bool> interpreterLogging{false};


std::atomic<bool> goBackAfter{false};
std::atomic<bool> refreshReturnAfter{false};
std::atomic<bool> triggerReturnToPackages{false};

std::atomic<bool> usingErista{util::IsErista()};
std::atomic<bool> usingMariko{util::IsMariko()};

// Device info globals
static char amsVersion[12];
static char hosVersion[12];

//static std::string memoryType;
static std::string memoryVendor = UNAVAILABLE_SELECTION;
static std::string memoryModel = UNAVAILABLE_SELECTION;
static std::string memorySize = UNAVAILABLE_SELECTION;
static std::string hekateVersion = UNAVAILABLE_SELECTION;
static std::string ovlloaderVersion = UNAVAILABLE_SELECTION; 
static uint32_t cpuSpeedo0, cpuSpeedo2, socSpeedo0; // CPU, GPU, SOC
static uint32_t cpuIDDQ, gpuIDDQ, socIDDQ;
static bool usingEmunand = true;
static bool is8GBEnabled = false;


// For persistent versions and colors across nested packages (when not specified)
std::string packageRootLayerTitle;
std::string packageRootLayerName;
std::string packageRootLayerVersion;
std::string packageRootLayerColor;


/**
 * @brief Ultrahand-Overlay Configuration Paths
 *
 * This block of code defines string variables for various configuration and directory paths
 * used in the Ultrahand-Overlay project. These paths include:
 *
 * - `PACKAGE_FILENAME`: The name of the package file ("package.ini").
 * - `CONFIG_FILENAME`: The name of the configuration file ("config.ini").
 * - `SETTINGS_PATH`: The base path for Ultrahand settings ("sdmc:/config/ultrahand/").
 * - `ULTRAHAND_CONFIG_INI_PATH`: The full path to the Ultrahand settings configuration file.
 * - `PACKAGE_PATH`: The base directory for packages ("sdmc:/switch/.packages/").
 * - `OVERLAY_PATH`: The base directory for overlays ("sdmc:/switch/.overlays/").
 * - `TESLA_CONFIG_INI_PATH`: The full path to the Tesla settings configuration file.
 *
 * These paths are used within the Ultrahand-Overlay project to manage configuration files
 * and directories.
 */

std::vector<std::string> getOverlayNames() {
    std::vector<std::string> names;
    const auto iniData = ult::getParsedDataFromIniFile(ult::OVERLAYS_INI_FILEPATH);
    for (const auto& [sectionName, _] : iniData) {
        names.push_back(sectionName);
    }
    return names;
}

std::vector<std::string> getPackageNames() {
    std::vector<std::string> names;
    const auto iniData = ult::getParsedDataFromIniFile(ult::PACKAGES_INI_FILEPATH);
    for (const auto& [sectionName, _] : iniData) {
        names.push_back(sectionName);
    }
    return names;
}



void removeKeyComboFromOthers(const std::string& keyCombo, const std::string& currentOverlay) {
    // Declare variables once for reuse across both scopes
    std::string existingCombo;
    std::string comboListStr;
    std::vector<std::string> comboList;
    bool modified;
    std::string newComboStr;
    
    // Process overlays first
    {
        auto overlaysIniData = ult::getParsedDataFromIniFile(ult::OVERLAYS_INI_FILEPATH);
        bool overlaysModified = false;
        
        const auto overlayNames = getOverlayNames();
        
        for (const auto& overlayName : overlayNames) {
            auto overlayIt = overlaysIniData.find(overlayName);
            if (overlayIt == overlaysIniData.end()) continue; // Skip if overlay not in INI
            
            auto& overlaySection = overlayIt->second;
            
            // 1. Remove from main key_combo field if it matches
            auto keyComboIt = overlaySection.find("key_combo");
            if (keyComboIt != overlaySection.end()) {
                existingCombo = keyComboIt->second;
                if (!existingCombo.empty() && tsl::hlp::comboStringToKeys(existingCombo) == tsl::hlp::comboStringToKeys(keyCombo)) {
                    overlaySection["key_combo"] = "";
                    overlaysModified = true;
                }
            }
            
            // 2. Remove from mode_combos list - clear ALL instances of this combo
            auto modeCombosIt = overlaySection.find("mode_combos");
            if (modeCombosIt != overlaySection.end()) {
                comboListStr = modeCombosIt->second;
            } else {
                comboListStr = "";
            }
            
            comboList = splitIniList(comboListStr);
            modified = false;
            
            for (size_t i = 0; i < comboList.size(); ++i) {
                if (!comboList[i].empty() && tsl::hlp::comboStringToKeys(comboList[i]) == tsl::hlp::comboStringToKeys(keyCombo)) {
                    comboList[i] = "";  // Clear ALL instances
                    modified = true;
                }
            }
            
            // Only update if something was actually removed
            if (modified) {
                newComboStr = "(" + joinIniList(comboList) + ")";
                overlaySection["mode_combos"] = newComboStr;
                overlaysModified = true;
            }
        }
        
        // Write back if modified, then clear memory
        if (overlaysModified) {
            ult::saveIniFileData(ult::OVERLAYS_INI_FILEPATH, overlaysIniData);
        }
        // overlaysIniData automatically cleared when scope ends
    }
    
    // Process packages second (overlays INI data is already cleared)
    {
        auto packagesIniData = ult::getParsedDataFromIniFile(ult::PACKAGES_INI_FILEPATH);
        bool packagesModified = false;
        
        auto packageNames = getPackageNames();
        
        for (const auto& packageName : packageNames) {
            auto packageIt = packagesIniData.find(packageName);
            if (packageIt == packagesIniData.end()) continue; // Skip if package not in INI
            
            auto& packageSection = packageIt->second;
            auto keyComboIt = packageSection.find("key_combo");
            if (keyComboIt != packageSection.end()) {
                existingCombo = keyComboIt->second; // Reusing the same variable
                if (!existingCombo.empty() && tsl::hlp::comboStringToKeys(existingCombo) == tsl::hlp::comboStringToKeys(keyCombo)) {
                    packageSection["key_combo"] = "";
                    packagesModified = true;
                }
            }
        }
        
        // Write back if modified, then clear memory
        if (packagesModified) {
            ult::saveIniFileData(ult::PACKAGES_INI_FILEPATH, packagesIniData);
        }
        // packagesIniData automatically cleared when scope ends
    }
}

// Define default key combos (same as UltrahandSettingsMenu)
static const std::array<std::string, 28> defaultCombos = {
    // Primary (Default) Combos – top priority, always first
    "ZL+ZR+DDOWN",
    "ZL+ZR+DRIGHT",
    "ZL+ZR+DUP",
    "ZL+ZR+DLEFT",

    // Basic Combos – L/R with D-pad
    "L+R+DDOWN",
    "L+R+DRIGHT",
    "L+R+DUP",
    "L+R+DLEFT",
    "L+DDOWN",
    "R+DDOWN",

    // Menu Key Combos – involving PLUS/MINUS
    "ZL+ZR+PLUS",
    "L+R+PLUS",
    "ZL+ZR+MINUS",
    "L+R+MINUS",
    "ZL+MINUS",
    "ZR+MINUS",
    "ZL+PLUS",
    "ZR+PLUS",
    "MINUS+PLUS",

    // Stick Click Combos
    "LS+RS",
    "L+DDOWN+RS",
    "L+R+LS",
    "L+R+RS",

    // Advanced Combos – unlikely to conflict, good for extra features
    "ZL+ZR+LS",
    "ZL+ZR+RS",
    "ZL+ZR+L",
    "ZL+ZR+R",
    "ZL+ZR+LS+RS"
};

// Global constant map for button and arrow placeholders
const std::unordered_map<std::string, std::string> symbolPlaceholders = {
    {"{A}", ""},
    {"{B}", ""},
    {"{X}", ""},
    {"{Y}", ""},
    {"{L}", ""},
    {"{R}", ""},
    {"{ZL}", ""},
    {"{ZR}", ""},
    {"{DUP}", ""},
    {"{DDOWN}", ""},
    {"{DLEFT}", ""},
    {"{DRIGHT}", ""},
    {"{LS}", ""},
    {"{RS}", ""},
    {"{PLUS}", ""},
    {"{MINUS}", ""},
    {"{UP_ARROW}", ""},
    {"{DOWN_ARROW}", ""},
    {"{LEFT_ARROW}", ""},
    {"{RIGHT_ARROW}", ""},
    {"{RIGHT_UP_ARROW}", ""},
    {"{RIGHT_DOWN_ARROW}", ""},
    {"{LEFT_UP_ARROW}", ""},
    {"{LEFT_DOWN_ARROW}", ""},
    {"{POWER}", ""},
    {"{HOME}", ""},
    {"{CAPTURE}", ""},
    {"{REFRESH_SYMBOL}", ""},
    {"{WARNING_SYMBOL}", ""},
    {"{INFO_SYMBOL}", ""},
    {"{DIVIDER_SYMBOL}", ""}
};



bool parseVersion(const char* version, int& major, int& minor, int& patch) {
    major = 0;
    minor = 0;
    patch = 0;
    const char* ptr = version;

    // Parse major version
    while (*ptr >= '0' && *ptr <= '9') {
        major = major * 10 + (*ptr - '0');
        ++ptr;
    }
    if (*ptr != '.') return false;
    ++ptr;

    // Parse minor version
    while (*ptr >= '0' && *ptr <= '9') {
        minor = minor * 10 + (*ptr - '0');
        ++ptr;
    }
    if (*ptr != '.') return false;
    ++ptr;

    // Parse patch version
    while (*ptr >= '0' && *ptr <= '9') {
        patch = patch * 10 + (*ptr - '0');
        ++ptr;
    }
    return *ptr == '\0';
}

bool isVersionGreaterOrEqual(const char* currentVersion, const char* requiredVersion) {
    int currMajor, currMinor, currPatch;
    int reqMajor, reqMinor, reqPatch;

    if (!parseVersion(currentVersion, currMajor, currMinor, currPatch) ||
        !parseVersion(requiredVersion, reqMajor, reqMinor, reqPatch)) {
        return false; // Invalid format
    }

    // Compare each component
    if (currMajor > reqMajor) return true;
    if (currMajor < reqMajor) return false;
    if (currMinor > reqMinor) return true;
    if (currMinor < reqMinor) return false;
    return currPatch >= reqPatch;
}

inline bool checkVersionCondition(const std::string& condition, const std::string& currentVersion) {
    if (condition.empty()) return true;

    const char* cur = currentVersion.c_str();
    std::string op, ver;

    if (condition.size() > 1 && condition[0] == '>' && condition[1] == '=') {
        op = ">="; ver = condition.substr(2);
    } else if (condition.size() > 1 && condition[0] == '<' && condition[1] == '=') {
        op = "<="; ver = condition.substr(2);
    } else if (condition[0] == '>') {
        op = ">"; ver = condition.substr(1);
    } else if (condition[0] == '<') {
        op = "<"; ver = condition.substr(1);
    } else {
        op = "=="; ver = condition;
    }

    const char* req = ver.c_str();
    if (op == "==") return isVersionGreaterOrEqual(cur, req) && isVersionGreaterOrEqual(req, cur);
    if (op == ">=") return isVersionGreaterOrEqual(cur, req);
    if (op == "<=") return isVersionGreaterOrEqual(req, cur);
    if (op == ">")  return isVersionGreaterOrEqual(cur, req) && !isVersionGreaterOrEqual(req, cur);
    if (op == "<")  return isVersionGreaterOrEqual(req, cur) && !isVersionGreaterOrEqual(cur, req);
    return true;
}


#define FUSE_CPU_SPEEDO_0_CALIB 0x114
//#define FUSE_CPU_SPEEDO_1_CALIB 0x12C
#define FUSE_CPU_SPEEDO_2_CALIB 0x130

#define FUSE_SOC_SPEEDO_0_CALIB 0x134
//#define FUSE_SOC_SPEEDO_1_CALIB 0x138
//#define FUSE_SOC_SPEEDO_2_CALIB 0x13C

#define FUSE_CPU_IDDQ_CALIB 0x118
#define FUSE_SOC_IDDQ_CALIB 0x140
#define FUSE_GPU_IDDQ_CALIB 0x228


void writeFuseIni(const std::string& outputPath, const char* data = nullptr) {
    // Use stdio.h functions for file operations
    FILE* outFile = fopen(outputPath.c_str(), "w");
    if (outFile) {
        // Write the header in one call instead of three separate fwrite calls
        fputs("[", outFile);
        fputs(FUSE_STR.c_str(), outFile); 
        fputs("]\n", outFile);
        
        if (data) {
            // fprintf is necessary here for actual formatting - keep these
            fprintf(outFile, "cpu_speedo_0=%u\n", *reinterpret_cast<const uint32_t*>(data + FUSE_CPU_SPEEDO_0_CALIB));
            fprintf(outFile, "cpu_speedo_2=%u\n", *reinterpret_cast<const uint32_t*>(data + FUSE_CPU_SPEEDO_2_CALIB));
            fprintf(outFile, "soc_speedo_0=%u\n", *reinterpret_cast<const uint32_t*>(data + FUSE_SOC_SPEEDO_0_CALIB));
            fprintf(outFile, "cpu_iddq=%u\n", *reinterpret_cast<const uint32_t*>(data + FUSE_CPU_IDDQ_CALIB) * 4);
            fprintf(outFile, "soc_iddq=%u\n", *reinterpret_cast<const uint32_t*>(data + FUSE_SOC_IDDQ_CALIB) * 4);
            fprintf(outFile, "gpu_iddq=%u\n", *reinterpret_cast<const uint32_t*>(data + FUSE_GPU_IDDQ_CALIB) * 5);
            // Detect RAM size at write time and persist it
            {
                u64 totalMem = 0;
                const char* ramSizeGB = "4";
                if (R_SUCCEEDED(svcGetSystemInfo(&totalMem, 0, INVALID_HANDLE, 0)) &&
                    totalMem > (6ULL * 1024 * 1024 * 1024))
                    ramSizeGB = "8";
                fprintf(outFile, "ram_size_gb=%s\n", ramSizeGB);
            }
            fputs("disable_reload=false\n", outFile);
        } else {
            // Single fputs call instead of seven separate ones
            fputs("cpu_speedo_0=\n"
                  "cpu_speedo_2=\n"
                  "soc_speedo_0=\n"
                  "cpu_iddq=\n"
                  "soc_iddq=\n"
                  "gpu_iddq=\n"
                  "ram_size_gb=\n"
                  "disable_reload=false\n", outFile);
        }
        fclose(outFile);
    }
}


void fuseDumpToIni(const std::string& outputPath = FUSE_DATA_INI_PATH) {
    if (isFile(outputPath)) return;

    u64 pid = 0;
    if (R_FAILED(pmdmntGetProcessId(&pid, 0x0100000000000006))) {
        writeFuseIni(outputPath);
        return;
    }

    Handle debug;
    if (R_FAILED(svcDebugActiveProcess(&debug, pid))) {
        writeFuseIni(outputPath);
        return;
    }

    MemoryInfo mem_info = {0};
    u32 pageinfo = 0;
    u64 addr = 0;

    char stack[0x10] = {0};
    const char compare[0x10] = {0};
    char dump[0x400] = {0};

    while (true) {
        if (R_FAILED(svcQueryDebugProcessMemory(&mem_info, &pageinfo, debug, addr)) || mem_info.addr < addr) {
            break;
        }

        if (mem_info.type == MemType_Io && mem_info.size == 0x1000) {
            if (R_FAILED(svcReadDebugProcessMemory(stack, debug, mem_info.addr, sizeof(stack)))) {
                break;
            }

            if (std::memcmp(stack, compare, sizeof(stack)) == 0) {
                if (R_FAILED(svcReadDebugProcessMemory(dump, debug, mem_info.addr + 0x800, sizeof(dump)))) {
                    break;
                }

                writeFuseIni(outputPath, dump);
                svcCloseHandle(debug);
                return;
            }
        }

        addr = mem_info.addr + mem_info.size;
    }

    svcCloseHandle(debug);
    writeFuseIni(outputPath);
}


std::string getLocalIpAddress() {
    Result rc;
    u32 ipAddress;
    
    // Initialize nifm service
    rc = nifmInitialize(NifmServiceType_System);
    if (R_FAILED(rc)) {
        // Failed to initialize, return default
        return UNAVAILABLE_SELECTION;
    }
    
    // Get the current IP address
    rc = nifmGetCurrentIpAddress(&ipAddress);
    if (R_SUCCEEDED(rc)) {
        // Convert the IP address to a string
        char ipStr[16];
        snprintf(ipStr, sizeof(ipStr), "%u.%u.%u.%u",
                 ipAddress & 0xFF,
                 (ipAddress >> 8) & 0xFF,
                 (ipAddress >> 16) & 0xFF,
                 (ipAddress >> 24) & 0xFF);
        nifmExit();
        return std::string(ipStr);
    } else {
        // Clean up before returning default
        nifmExit();
        return UNAVAILABLE_SELECTION;
    }
}

std::string getMasterVolumeLevel() {
    audctlInitialize();
    float currentVolume = 0.0f;
    Result rc = audctlGetSystemOutputMasterVolume(&currentVolume);
    audctlExit();
    
    if (R_SUCCEEDED(rc)) {
        int volumePercentage = static_cast<int>(currentVolume * 100.0f + 0.5f);
        return ult::to_string(volumePercentage);
    }
    
    return "100"; // Default to 100 if failed
}

std::string getBacklightLevel() {
    lblInitialize();
    float currentBrightness = 0.0f;
    Result rc = lblGetCurrentBrightnessSetting(&currentBrightness);
    lblExit();
    
    if (R_SUCCEEDED(rc)) {
        int brightnessPercentage = static_cast<int>(currentBrightness * 100.0f + 0.5f);
        return ult::to_string(brightnessPercentage);
    }
    
    return "100"; // Default to 100 if failed
}

// Function to remove all empty command strings
void removeEmptyCommands(std::vector<std::vector<std::string>>& commands) {
    commands.erase(std::remove_if(commands.begin(), commands.end(),
        [](const std::vector<std::string>& vec) noexcept {
            return vec.empty();
        }),
        commands.end());
}




// Define the helper function
void formatVersion(uint64_t packed_version, int shift1, int shift2, int shift3, char* version_str) {
    sprintf(version_str, "%d.%d.%d",
            static_cast<uint8_t>((packed_version >> shift1) & 0xFF),
            static_cast<uint8_t>((packed_version >> shift2) & 0xFF),
            static_cast<uint8_t>((packed_version >> shift3) & 0xFF));
}


const char* getMemoryType(uint64_t packed_version) {
    switch (packed_version) {
        case 0: return "Samsung_K4F6E304HB-MGCH_4 GB LPDDR4 3200 Mbps";
        case 1: return "Hynix_H9HCNNNBPUMLHR-NLE_4 GB LPDDR4 3200 Mbps";
        case 2: return "Micron_MT53B512M32D2NP-062 WT:C_4 GB LPDDR4 3200 Mbps";
        case 3: return "Hynix_H9HCNNNBKMMLXR-NEE_4 GB LPDDR4X 4266 Mbps";
        case 4: return "Samsung_K4FHE3D4HM-MGCH_6GB LPDDR4 3200 Mbps";
        case 5: return "Hynix_H9HCNNNBKMMLXR-NEE_4 GB LPDDR4X 4266 Mbps";
        case 6: return "Hynix_H9HCNNNBKMMLXR-NEE_4 GB LPDDR4X 4266 Mbps";
        case 7: return "Samsung_K4FBE3D4HM-MGXX_8 GB LPDDR4 3200 Mbps";
        case 8: return "Samsung_K4U6E3S4AM-MGCJ_4 GB LPDDR4X 3733 Mbps";
        case 9: return "Samsung_K4UBE3D4AM-MGCJ_8 GB LPDDR4X 3733 Mbps";
        case 10: return "Hynix_H9HCNNNBKMMLHR-NME_4 GB LPDDR4X 3733 Mbps";
        case 11: return "Micron_MT53E512M32D2NP-046 WT:E_4 GB LPDDR4X 4266 Mbps";
        case 12: return "Samsung_K4U6E3S4AM-MGCJ_4 GB LPDDR4X 3733 Mbps";
        case 13: return "Samsung_K4UBE3D4AM-MGCJ_8 GB LPDDR4X 3733 Mbps";
        case 14: return "Hynix_H9HCNNNBKMMLHR-NME_4 GB LPDDR4X 3733 Mbps";
        case 15: return "Micron_MT53E512M32D2NP-046 WT:E_4 GB LPDDR4X 4266 Mbps";
        case 17: return "Samsung_K4U6E3S4AA-MGCL_4 GB LPDDR4X 4266 Mbps";
        case 18: return "Samsung_K4UBE3D4AA-MGCL_8 GB LPDDR4X 4266 Mbps";
        case 19: return "Samsung_K4U6E3S4AA-MGCL_4 GB LPDDR4X 4266 Mbps";
        case 20: return "Samsung_K4U6E3S4AB-MGCL_4 GB LPDDR4X 4266 Mbps";
        case 21: return "Samsung_K4U6E3S4AB-MGCL_4 GB LPDDR4X 4266 Mbps";
        case 22: return "Samsung_K4U6E3S4AB-MGCL_4 GB LPDDR4X 4266 Mbps";
        case 23: return "Samsung_K4UBE3D4AA-MGCL_8 GB LPDDR4X 4266 Mbps";
        case 24: return "Samsung_K4U6E3S4AA-MGCL_4 GB LPDDR4X 4266 Mbps";
        case 25: return "Micron_MT53E512M32D2NP-046 WT:F_4 GB LPDDR4X 4266 Mbps";
        case 26: return "Micron_MT53E512M32D2NP-046 WT:F_4 GB LPDDR4X 4266 Mbps";
        case 27: return "Micron_MT53E512M32D2NP-046 WT:F_4 GB LPDDR4X 4266 Mbps";
        case 28: return "Samsung_K4UBE3D4AA-MGCL_8 GB LPDDR4X 4266 Mbps";
        case 29: return "Hynix_H54G46CYRBX267_4 GB LPDDR4X 4266 Mbps";
        case 30: return "Hynix_H54G46CYRBX267_4 GB LPDDR4X 4266 Mbps";
        case 31: return "Hynix_H54G46CYRBX267_4 GB LPDDR4X 4266 Mbps";
        case 32: return "Micron_MT53E512M32D1NP-046 WT:B_4 GB LPDDR4X 4266 Mbps";
        case 33: return "Micron_MT53E512M32D1NP-046 WT:B_4 GB LPDDR4X 4266 Mbps";
        case 34: return "Micron_MT53E512M32D1NP-046 WT:B_4 GB LPDDR4X 4266 Mbps";

        case 16: return "Hynix_NMEM-DIE (Copper)_null";
        case 35: return "null_H54G46CYRBX267_null";
        case 36: return "Magnesium_MT53E512M32D1NP-046 WT:B_null";
        case 37: return "Samsung_K4FBE3D4HM-GHCL_null";
        case 38: return "Samsung_K4FBE3D4HM-GFCL_null";
        case 39: return "Samsung_K4FBE3D4HM-TFCL_null";
        case 40: return "Samsung_K4FBE3D4HM-MGCJ_null";
        case 41: return "Samsung_K4FBE3D4HM-THCL_null";
        case 42: return "Samsung_K4FBE3D4HM-SGCL_null";
        default: return "";
    }
}



const char* getStorageInfo(const std::string& storageType) {
    s64 freeSpace = 0;
    s64 totalSpace = 0;
    char* buffer = (char*)malloc(64);  // Increased size for percentage info

    FsFileSystem fs;
    Result result;

    // Open the appropriate file system based on storage type
    if (storageType == "emmc")
        result = fsOpenBisFileSystem(&fs, FsBisPartitionId_User, "");
    else if (storageType == "sdmc")
        result = fsOpenContentStorageFileSystem(&fs, FsContentStorageId_SdCard);
    else
        return buffer;

    if (R_FAILED(result)) {
        return buffer;
    }

    // Get free space on the storage
    result = fsFsGetFreeSpace(&fs, "/", &freeSpace);
    if (R_FAILED(result)) {
        fsFsClose(&fs);
        return buffer;
    }

    // Get total space on the storage
    result = fsFsGetTotalSpace(&fs, "/", &totalSpace);
    if (R_FAILED(result)) {
        fsFsClose(&fs);
        return buffer;
    }

    // Calculate the used space and usage percentage
    const s64 usedSpace = totalSpace - freeSpace;
    const float usedSpaceGB = usedSpace / (1024.0f * 1024.0f * 1024.0f);
    const float totalSpaceGB = totalSpace / (1024.0f * 1024.0f * 1024.0f);
    const int percentUsed = (totalSpace > 0) ? static_cast<int>((usedSpace * 100) / totalSpace) : 0;

    // Format the string with percentage
    snprintf(buffer, 64, "%.1f GB / %.1f GB (%d%%)", usedSpaceGB, totalSpaceGB, percentUsed);

    fsFsClose(&fs);
    return buffer;
}



void unpackDeviceInfo() {
    u64 packed_version;
    splGetConfig((SplConfigItem)2, &packed_version);
    const std::string memoryType = getMemoryType(packed_version);
    
    if (!memoryType.empty()) {
        const std::vector<std::string> memoryData = splitString(memoryType, "_");
        if (memoryData.size() > 0) memoryVendor = memoryData[0];
        if (memoryData.size() > 1) memoryModel = memoryData[1];
        if (memoryData.size() > 2) memorySize = memoryData[2];
    }
    splGetConfig((SplConfigItem)65000, &packed_version);
    
    // Format AMS version
    formatVersion(packed_version, 56, 48, 40, amsVersion);
    
    // Format HOS version
    formatVersion(packed_version, 24, 16, 8, hosVersion);

    splGetConfig((SplConfigItem)65007, &packed_version);
    usingEmunand = (packed_version != 0);

    // Cache hekate version once at startup (extractVersionFromBinary reads the entire binary)
    const std::string _hekateVer = extractVersionFromBinary("sdmc:/bootloader/update.bin");
    hekateVersion = _hekateVer.empty() ? UNAVAILABLE_SELECTION : _hekateVer;

    // nx-volloader version
    const std::string _ovlloaderVer = cleanVersionLabel(loaderInfo);
    ovlloaderVersion = _ovlloaderVer.empty() ? UNAVAILABLE_SELECTION : _ovlloaderVer;

    fuseDumpToIni();
    
    if (isFile(FUSE_DATA_INI_PATH)) {
        // Load INI data once instead of 6 separate file reads
        const auto fuseSection = getKeyValuePairsFromSection(FUSE_DATA_INI_PATH, FUSE_STR);
        const auto end = fuseSection.end();
        
        auto getValue = [&](const char* key) -> u32 {
            auto it = fuseSection.find(key);
            return (it != end && !it->second.empty()) ? ult::stoi(it->second) : 0;
        };
        
        cpuSpeedo0 = getValue("cpu_speedo_0");
        cpuSpeedo2 = getValue("cpu_speedo_2");
        socSpeedo0 = getValue("soc_speedo_0");
        cpuIDDQ = getValue("cpu_iddq");
        socIDDQ = getValue("soc_iddq");
        gpuIDDQ = getValue("gpu_iddq");

        auto itRamSize = fuseSection.find("ram_size_gb");
        if (itRamSize != end && !itRamSize->second.empty()) {
            is8GBEnabled = (itRamSize->second == "8");
        } else {
            // Key missing (old fuse file) — detect live and patch the file
            u64 totalMem = 0;
            is8GBEnabled = R_SUCCEEDED(svcGetSystemInfo(&totalMem, 0, INVALID_HANDLE, 0)) &&
                           totalMem > (6ULL * 1024 * 1024 * 1024);
            setIniFileValue(FUSE_DATA_INI_PATH, FUSE_STR, "ram_size_gb", is8GBEnabled ? "8" : "4");
        }
    }
}


// Escapes regex special chars except '*'
static std::string resolveWildcardFromKnownPath(
    const std::string& oldPattern,
    const std::string& resolvedPath,
    const std::string& newPattern
) {
    // Helper function to check if paths match and extract wildcard values
    auto matchAndExtract = [](const std::string& pattern, const std::string& path, std::vector<std::string>& captures) -> bool {
        size_t patternPos = 0;
        size_t pathPos = 0;
        size_t nextPatternPos;
        char nextChar;
        size_t foundPos;
        bool found;
        char patternChar;
        char pathChar;
        bool match;

        while (patternPos < pattern.size() && pathPos < path.size()) {
            if (pattern[patternPos] == '*') {
                // Find the next non-wildcard character in pattern
                nextPatternPos = patternPos + 1;
                while (nextPatternPos < pattern.size() && pattern[nextPatternPos] == '*') {
                    nextPatternPos++; // Skip consecutive wildcards
                }
                
                if (nextPatternPos >= pattern.size()) {
                    // Wildcard is at the end, capture everything remaining
                    captures.push_back(path.substr(pathPos));
                    return true;
                }
                
                // Find the next literal character after the wildcard
                nextChar = pattern[nextPatternPos];
                
                // Find where this character appears in the path
                foundPos = pathPos;
                found = false;
                
                // Look for the next literal sequence starting from current path position
                while (foundPos < path.size()) {
                    if ((nextChar == '/' || nextChar == '\\') && 
                        (path[foundPos] == '/' || path[foundPos] == '\\')) {
                        found = true;
                        break;
                    } else if (nextChar != '/' && nextChar != '\\' && 
                               std::tolower(path[foundPos]) == std::tolower(nextChar)) {
                        found = true;
                        break;
                    }
                    foundPos++;
                }
                
                if (!found) {
                    return false;
                }
                
                // Capture everything between current position and found position
                captures.push_back(path.substr(pathPos, foundPos - pathPos));
                pathPos = foundPos;
                patternPos = nextPatternPos;
            } else {
                // Literal character matching (case-insensitive, handle path separators)
                patternChar = pattern[patternPos];
                pathChar = path[pathPos];
                
                match = false;
                if ((patternChar == '/' || patternChar == '\\') && 
                    (pathChar == '/' || pathChar == '\\')) {
                    match = true;
                } else if (std::tolower(patternChar) == std::tolower(pathChar)) {
                    match = true;
                }
                
                if (!match) {
                    return false;
                }
                
                patternPos++;
                pathPos++;
            }
        }
        
        // Handle remaining wildcards at the end of pattern
        while (patternPos < pattern.size() && pattern[patternPos] == '*') {
            captures.push_back("");
            patternPos++;
        }
        
        // Pattern should be fully consumed, path can have remaining characters
        return patternPos >= pattern.size();
    };
    
    // Try to match the pattern at different positions in the resolved path (simulating .* anchors)
    std::vector<std::string> captures;
    bool matched = false;
    std::string subPath;

    for (size_t startPos = 0; startPos <= resolvedPath.size(); ++startPos) {
        captures.clear();
        subPath = resolvedPath.substr(startPos);
        
        if (matchAndExtract(oldPattern, subPath, captures)) {
            matched = true;
            break;
        }
    }
    
    if (!matched) {
        return newPattern;
    }
    
    // Replace wildcards in newPattern with captured values
    std::string result = newPattern;
    size_t pos;
    for (size_t i = 0; i < captures.size(); ++i) {
        pos = result.find('*');
        if (pos == std::string::npos) break;
        result.replace(pos, 1, captures[i]);
    }
    
    return result;
}


/**
 * @brief Shuts off all connected controllers.
 *
 * This function disconnects all connected controllers by utilizing the Bluetooth manager (btm) service.
 * It checks the firmware version and uses the appropriate function to get the device condition and disconnects
 * the controllers.
 */
void powerOffAllControllers() {
    Result rc;
    static s32 g_connected_count = 0;
    static BtdrvAddress g_addresses[8] = {};
    
    // Initialize Bluetooth manager
    rc = btmInitialize();
    if (R_FAILED(rc)) {
        setCommandFailed();
        return;
    }
    
    BtmConnectedDeviceV13 connected_devices[8];
    rc = btmGetDeviceCondition(BtmProfile_None, connected_devices, 8, &g_connected_count);
    if (R_SUCCEEDED(rc)) {
        for (s32 i = 0; i != g_connected_count; ++i) {
            g_addresses[i] = connected_devices[i].address;
        }
    } else {
        setCommandFailed();
    }
    
    if (R_SUCCEEDED(rc)) {
        for (int i = 0; i != g_connected_count; ++i) {
            rc = btmHidDisconnect(g_addresses[i]);
            if (R_FAILED(rc)) {
                setCommandFailed();
            }
        }
    } else {
        setCommandFailed();
    }
    
    // Exit Bluetooth manager
    btmExit();
}




/**
 * @brief Synchronize Tesla and Ultrahand key combos.
 *
 * This function synchronizes key combos between Tesla and Ultrahand settings.
 * - If Tesla has a combo and Ultrahand doesn't: copy Tesla → Ultrahand
 * - If Ultrahand has a combo and Tesla doesn't or differs: copy Ultrahand → Tesla
 * - If neither has a combo: do nothing
 * 
 * @return true if neither config has a combo, false otherwise
 */
bool copyTeslaKeyComboToUltrahand() {
    // Check for Tesla key combo
    bool hasTeslaKeyCombo = false;
    std::string teslaKeyCombo;
    if (isFile(TESLA_CONFIG_INI_PATH)) {
        teslaKeyCombo = parseValueFromIniSection(TESLA_CONFIG_INI_PATH, TESLA_STR, KEY_COMBO_STR);
        hasTeslaKeyCombo = !teslaKeyCombo.empty();
    }
    
    // Check for Ultrahand key combo
    bool hasUltrahandKeyCombo = false;
    std::string ultrahandKeyCombo;
    if (isFile(ULTRAHAND_CONFIG_INI_PATH)) {
        ultrahandKeyCombo = parseValueFromIniSection(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME, KEY_COMBO_STR);
        hasUltrahandKeyCombo = !ultrahandKeyCombo.empty();
    }
    
    // Apply sync logic
    if (hasTeslaKeyCombo && !hasUltrahandKeyCombo) {
        // Tesla has combo, Ultrahand doesn't → copy Tesla to Ultrahand
        setIniFileValue(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME, KEY_COMBO_STR, teslaKeyCombo);
    } else if (hasUltrahandKeyCombo && (!hasTeslaKeyCombo || teslaKeyCombo != ultrahandKeyCombo)) {
        // Ultrahand has combo and (Tesla doesn't OR they differ) → copy Ultrahand to Tesla
        setIniFileValue(TESLA_CONFIG_INI_PATH, TESLA_STR, KEY_COMBO_STR, ultrahandKeyCombo);
    }
    
    // Return true if neither has a combo
    if (!hasTeslaKeyCombo && !hasUltrahandKeyCombo) {
        return true;
    }
    
    tsl::impl::parseOverlaySettings();
    return false;
}


// Constants for overlay module
constexpr int OverlayLoaderModuleId = 348;
constexpr Result ResultSuccess = MAKERESULT(0, 0);
constexpr Result ResultParseError = MAKERESULT(OverlayLoaderModuleId, 1);
constexpr uint32_t ULTR_SIGNATURE  = 0x52544C55; // "ULTR"


/**
 * @brief Retrieves overlay module information from a given file (ultra-optimized).
 *
 * @param filePath The path to the overlay module file.
 * @return A tuple containing the result code, module name, and display version.
 */
std::tuple<Result, std::string, std::string, bool, bool> getOverlayInfo(const std::string& filePath) {
    FILE* file = fopen(filePath.c_str(), "rb");
    if (!file) return {ResultParseError, "", "", false, false};

    // Get file size
    fseek(file, 0, SEEK_END);
    const long fileSize = ftell(file);
    if (fileSize < static_cast<long>(sizeof(NroStart) + sizeof(NroHeader))) {
        fclose(file);
        return {ResultParseError, "", "", false, false};
    }
    const size_t fileSz = static_cast<size_t>(fileSize);

    // --- Strategy: Read front chunk that likely contains header + MOD0 ---
    // Most NRO files have MOD0 within first 8-16KB
    constexpr size_t FRONT_READ_SIZE = 8192;  // 16KB
    const size_t frontReadSize = (fileSz < FRONT_READ_SIZE) ? fileSz : FRONT_READ_SIZE;
    
    uint8_t* frontBuf = static_cast<uint8_t*>(malloc(frontReadSize));
    if (!frontBuf) {
        fclose(file);
        return {ResultParseError, "", "", false, false};
    }

    fseek(file, 0, SEEK_SET);
    if (fread(frontBuf, 1, frontReadSize, file) != frontReadSize) {
        free(frontBuf);
        fclose(file);
        return {ResultParseError, "", "", false, false};
    }

    // Validate and extract NRO header from buffer
    if (frontReadSize < sizeof(NroStart) + sizeof(NroHeader)) {
        free(frontBuf);
        fclose(file);
        return {ResultParseError, "", "", false, false};
    }

    NroHeader nroHeader;
    std::memcpy(&nroHeader, frontBuf + sizeof(NroStart), sizeof(NroHeader));
    
    if (nroHeader.size == 0 || nroHeader.size >= fileSz) {
        free(frontBuf);
        fclose(file);
        return {ResultParseError, "", "", false, false};
    }

    // --- Detect MOD0 LNY2 via shared helper (also used by usingLNY2 in tesla.cpp) ---
    const uint32_t mod0_rel    = *reinterpret_cast<const uint32_t*>(frontBuf + 0x4);
    const uint32_t text_offset = *reinterpret_cast<const uint32_t*>(frontBuf + 0x20);
    bool usesNewLibNX = detectLNY2FromBuffers(frontBuf, frontReadSize,
                                              text_offset, mod0_rel, fileSz, file);

    free(frontBuf);

    // --- Read NACP metadata ---
    if (nroHeader.size + sizeof(NroAssetHeader) > fileSz) {
        fclose(file);
        return {ResultParseError, "", "", false, usesNewLibNX};
    }

    fseek(file, nroHeader.size, SEEK_SET);
    NroAssetHeader assetHeader;
    if (fread(&assetHeader, sizeof(NroAssetHeader), 1, file) != 1 ||
        assetHeader.nacp.offset > fileSz - nroHeader.size ||
        nroHeader.size + assetHeader.nacp.offset + sizeof(NacpStruct) > fileSz) {
        fclose(file);
        return {ResultParseError, "", "", false, usesNewLibNX};
    }

    fseek(file, nroHeader.size + assetHeader.nacp.offset, SEEK_SET);
    NacpStruct nacp;
    if (fread(&nacp, sizeof(NacpStruct), 1, file) != 1) {
        fclose(file);
        return {ResultParseError, "", "", false, usesNewLibNX};
    }

    // --- Check ULTR signature (last 4 bytes of file) ---
    uint32_t last4 = 0;
    bool usingLibUltrahand = false;
    
    if (fileSz >= 4) {
        fseek(file, -4, SEEK_END);
        usingLibUltrahand = (fread(&last4, sizeof(last4), 1, file) == 1) && 
                           (last4 == ULTR_SIGNATURE);
    }

    fclose(file);

    // --- Extract strings ---
    const char* nameEnd = static_cast<const char*>(std::memchr(nacp.lang_data.lang[0].name, '\0', sizeof(nacp.lang_data.lang[0].name)));
    const size_t nameLen = nameEnd ? (nameEnd - nacp.lang_data.lang[0].name) : sizeof(nacp.lang_data.lang[0].name);
    
    const char* versionEnd = static_cast<const char*>(std::memchr(nacp.display_version, '\0', sizeof(nacp.display_version)));
    const size_t versionLen = versionEnd ? (versionEnd - nacp.display_version) : sizeof(nacp.display_version);

    return {
        ResultSuccess,
        std::string(nacp.lang_data.lang[0].name, nameLen),
        std::string(nacp.display_version, versionLen),
        usingLibUltrahand,
        usesNewLibNX
    };
}

void addHeader(auto& list, const std::string& headerText) {
    list->addItem(new tsl::elm::CategoryHeader(headerText));
}

void addBasicListItem(auto& list, const std::string& itemText, bool isMini = false) {
    list->addItem(new tsl::elm::ListItem(itemText, "", isMini));
}

void addDummyListItem(auto& list, s32 index = -1) {
    list->addItem(new tsl::elm::DummyListItem(), 0, index);
}

void addSelectionIsEmptyDrawer(auto& list) {
    //addDummyListItem(list);
    auto* warning = new tsl::elm::CustomDrawer([](tsl::gfx::Renderer* renderer, u16, u16, u16, u16){
        // Icon
        const size_t iconX = (448 - renderer->getTextDimensions("\uE150", false, 90).first) / 2;
        renderer->drawString("\uE150", false, iconX, 324, 90, tsl::defaultTextColor);

        // Text
        const size_t textX = (448 - renderer->getTextDimensions(SELECTION_IS_EMPTY, false, 25).first) / 2;
        renderer->drawString(SELECTION_IS_EMPTY, false, textX, 410, 25, tsl::defaultTextColor);
    });
    list->addItem(warning);
}

bool applyPlaceholderReplacements(std::vector<std::string>& cmd, const std::string& hexPath, const std::string& iniPath, const std::string& listString, const std::string& listPath, const std::string& jsonString, const std::string& jsonPath, const std::string& packagePath = "");

std::string getFirstSectionText(const std::vector<std::vector<std::string>>& tableData, const std::string& packagePath) {
    std::string message;
    std::string listFileSourcePath;
    std::string hexPath, iniPath, listString, listPath, jsonString, jsonPath;
    
    bool inEristaSection = false;
    bool inMarikoSection = false;
    std::vector<std::string> lines;
    
    std::vector<std::string> cmd;
    for (const auto& commands : tableData) {
        cmd = commands;  // Make a copy if you need to modify it

        if (cmd.empty()) {
            continue;
        }

        const std::string& commandName = cmd[0];

        if (commandName == "erista:") {
            inEristaSection = true;
            inMarikoSection = false;
            continue;
        } else if (commandName == "mariko:") {
            inEristaSection = false;
            inMarikoSection = true;
            continue;
        }

        if ((inEristaSection && !inMarikoSection && usingErista) ||
            (!inEristaSection && inMarikoSection && usingMariko) ||
            (!inEristaSection && !inMarikoSection)) {

            // Apply placeholder replacements if necessary
            applyPlaceholderReplacements(cmd, hexPath, iniPath, listString, listPath, jsonString, jsonPath, packagePath);

            const size_t cmdSize = cmd.size();

            if (commandName == "list_file_source" && listFileSourcePath.empty()) {
                listFileSourcePath = cmd[1];
                preprocessPath(listFileSourcePath, packagePath);

                // Read lines from the file
                lines = readListFromFile(listFileSourcePath);

                // Return the first line if available
                if (!lines.empty()) {
                    return lines[0];
                }
            } else if (commandName == LIST_STR) {
                if (cmdSize >= 2) {
                    listString = cmd[1];
                    removeQuotes(listString);
                    // Process listString if needed
                }
            } else if (commandName == LIST_FILE_STR) {
                if (cmdSize >= 2) {
                    listPath = cmd[1];
                    preprocessPath(listPath, packagePath);
                    // Read from listPath if needed
                }
            } else if (commandName == JSON_STR) {
                if (cmdSize >= 2) {
                    jsonString = cmd[1];
                    removeQuotes(jsonString);
                    // Process jsonString if needed
                }
            } else if (commandName == JSON_FILE_STR) {
                if (cmdSize >= 2) {
                    jsonPath = cmd[1];
                    preprocessPath(jsonPath, packagePath);
                    // Read from jsonPath if needed
                }
            } else if (commandName == INI_FILE_STR) {
                if (cmdSize >= 2) {
                    iniPath = cmd[1];
                    preprocessPath(iniPath, packagePath);
                    // Read from iniPath if needed
                }
            } else if (commandName == HEX_FILE_STR) {
                if (cmdSize >= 2) {
                    hexPath = cmd[1];
                    preprocessPath(hexPath, packagePath);
                    // Read from hexPath if needed
                }
            } else {
                // Default case: This is where sectionLines are populated in addTable
                // Return cmd[0] as the first section text
                return cmd[0];
            }
        }
    }

    // If no section text is found, return an empty string or a default value
    return "";  // Or return a default message if appropriate
}


void addGap(tsl::elm::List* list, s32 gapHeight) {
    list->addItem(new tsl::elm::CustomDrawer(
        [](tsl::gfx::Renderer* renderer, s32 x, s32 y, s32 w, s32 h) {
            // Empty drawer - just creates space
        }
    ), gapHeight);
}


// ─── Helper: flatten + placeholder + wrap & expand ─────────────────────────────
static bool buildTableDrawerLines(
    const std::vector<std::vector<std::string>>& tableData,
    std::vector<std::string>&                    sectionLines,
    std::vector<std::string>&                    infoLines,
    const std::string&                           packagePath,
    size_t                                       columnOffset,
    size_t                                       startGap,
    size_t                                       newlineGap,
    const std::string&                           wrappingMode,
    const std::string&                           alignment,
    bool                                         useWrappedTextIndent,
    std::vector<std::string>&                    outSection,
    std::vector<std::string>&                    outInfo,
    std::vector<s32>&                            outY,
    std::vector<int>&                            outX
) {
    static constexpr size_t lineHeight = 16;
    static constexpr size_t fontSize = 16;
    const size_t xMax = tsl::cfg::FramebufferWidth - 95 -1;
    const std::string indent = "└ ";
    const float indentWidth = tsl::gfx::calculateStringWidth(indent, fontSize, false);

    outSection.clear();
    outInfo.clear();
    outY.clear();
    outX.clear();

    size_t curY = startGap;
    bool anyReplacementsMade = false;

    // Translation helper — called once up front, no lock held during wrapping
    auto getTranslated = [](const std::string& s) -> std::string {
        if (s.empty()) return s;
        std::shared_lock<std::shared_mutex> readLock(tsl::gfx::s_translationCacheMutex);
        auto it = ult::translationCache.find(s);
        return (it != ult::translationCache.end()) ? it->second : s;
    };

    auto processLines = [&](const std::vector<std::string>& lines, const std::vector<std::string>& infos) {
        std::string infoText;
        int xPos;
        float lineInfoWidth;
        std::vector<std::string> wrappedLines;
        std::vector<std::string> wrappedInfoLines;
        for (size_t i = 0; i < lines.size(); ++i) {
            const std::string& baseText = lines[i];
            const std::string& infoTextRaw = (i < infos.size()) ? infos[i] : "";
            infoText = (infoTextRaw.find(NULL_STR) != std::string::npos) ? UNAVAILABLE_SELECTION : infoTextRaw;

            wrappedLines = tsl::wrapText(
                baseText,
                xMax - 8,
                wrappingMode,
                useWrappedTextIndent,
                indent, indentWidth,
                fontSize
            );

            // Wrap the info/values column.
            // The label column's right edge is at (x+12) + (xMax-8) = x + xMax + 4.
            // The info column starts at x + columnOffset, so its wrap width to reach
            // the same right boundary is: (xMax + 4) - columnOffset.
            // Pass empty indent/"" and 0 indentWidth (no indent for values column, and
            // passing a non-zero indentWidth can affect effective line width inside
            // wrapText even when useIndent=false, causing uneven line breaks).
            wrappedInfoLines = tsl::wrapText(
                infoText,
                static_cast<float>(xMax) + 4.0f - static_cast<float>(columnOffset),
                wrappingMode,
                false,
                "", 0.0f,
                fontSize
            );

            const size_t rowCount = std::max(wrappedLines.size(), wrappedInfoLines.size());

            for (size_t j = 0; j < rowCount; ++j) {
                const std::string secLine  = (j < wrappedLines.size())     ? wrappedLines[j]     : "";
                const std::string infoLine = (j < wrappedInfoLines.size()) ? wrappedInfoLines[j] : "";

                outSection.push_back(secLine);
                outInfo.push_back(infoLine);

                lineInfoWidth = tsl::gfx::calculateStringWidth(infoLine, fontSize, false);

                xPos = 0;
                if (alignment == LEFT_STR) {
                    xPos = static_cast<int>(columnOffset);
                } else if (alignment == RIGHT_STR) {
                    xPos = static_cast<int>(xMax - lineInfoWidth + (columnOffset - 160 + 1));
                } else {
                    xPos = static_cast<int>(columnOffset + (xMax - lineInfoWidth) / 2);
                }

                outX.push_back(xPos);
                outY.push_back(static_cast<s32>(curY));
                curY += lineHeight + newlineGap;
            }
        }
    };

    if (!tableData.empty()) {
        std::vector<std::string> baseSection;
        std::vector<std::string> baseInfo;
        std::vector<std::string> lines;

        std::string listFileSourcePath;
        std::string hexPath, iniPath, listString, listPath, jsonString, jsonPath;
        bool inErista = false, inMariko = false;

        for (const auto& cmds : tableData) {
            if (cmds.empty()) continue;
            const auto& name = cmds[0];

            if (name == "erista:") {
                inErista = true;
                inMariko = false;
                continue;
            }
            if (name == "mariko:") {
                inErista = false;
                inMariko = true;
                continue;
            }

            if ((inErista && usingErista) || (inMariko && usingMariko) || (!inErista && !inMariko)) {
                auto cmd = cmds;

                if (applyPlaceholderReplacements(
                    cmd, hexPath, iniPath,
                    listString, listPath,
                    jsonString, jsonPath, packagePath
                )) {
                    anyReplacementsMade = true;
                }

                if (cmd[0] == "list_file_source" && cmd.size() >= 2 && listFileSourcePath.empty()) {
                    listFileSourcePath = cmd[1];
                    preprocessPath(listFileSourcePath, packagePath);
                    lines = readListFromFile(listFileSourcePath, 0, true);
                    for (const auto& line : lines) {
                        baseSection.push_back(getTranslated(line));
                        baseInfo.push_back("");
                    }
                }
                else if (cmd[0] == LIST_STR && cmd.size() >= 2) {
                    listString = cmd[1];
                    removeQuotes(listString);
                }
                else if (cmd[0] == LIST_FILE_STR && cmd.size() >= 2) {
                    listPath = cmd[1];
                    preprocessPath(listPath, packagePath);
                }
                else if (cmd[0] == JSON_STR && cmd.size() >= 2) {
                    jsonString = cmd[1];
                    removeQuotes(jsonString);
                }
                else if (cmd[0] == JSON_FILE_STR && cmd.size() >= 2) {
                    jsonPath = cmd[1];
                    preprocessPath(jsonPath, packagePath);
                }
                else if (cmd[0] == INI_FILE_STR && cmd.size() >= 2) {
                    iniPath = cmd[1];
                    preprocessPath(iniPath, packagePath);
                }
                else if (cmd[0] == HEX_FILE_STR && cmd.size() >= 2) {
                    hexPath = cmd[1];
                    preprocessPath(hexPath, packagePath);
                }
                else {
                    if (!cmd[0].empty() && cmd[0][0] == ';') continue;
                    if (cmd[0].empty() && cmd.size() > 2) {
                        // {json(...)} prefix resolved to "" — shift: cmd[1]→col1, cmd[2]→col2
                        baseSection.push_back(getTranslated(cmd[1]));
                        baseInfo.push_back(getTranslated(cmd.size() > 2 ? cmd[2] : ""));
                    } else {
                        baseSection.push_back(getTranslated(cmd[0]));
                        baseInfo.push_back(getTranslated(cmd.size() > 1 ? cmd[1] : ""));
                    }
                }
            }
        }
        processLines(baseSection, baseInfo);
    } else {
        {
            std::shared_lock<std::shared_mutex> readLock(tsl::gfx::s_translationCacheMutex);
            for (auto& s : sectionLines) {
                auto it = ult::translationCache.find(s);
                if (it != ult::translationCache.end()) s = it->second;
            }
            for (auto& s : infoLines) {
                auto it = ult::translationCache.find(s);
                if (it != ult::translationCache.end()) s = it->second;
            }
        }
        processLines(sectionLines, infoLines);
    }

    return anyReplacementsMade;
}


// Helper function moved outside - no lambda creation overhead
static tsl::Color getRawColor(const std::string& c, tsl::Color defaultColor) {
    // Check most common/special case first
    if (c == DEFAULT_STR) return defaultColor;
    
    // Quick length check to eliminate impossible matches
    const size_t len = c.length();
    
    // Group by length to reduce comparisons
    switch (len) {
        case 4:
            if (c == "text") return tsl::defaultTextColor;
            if (c == "info") return tsl::infoTextColor;
            break;
            
        case 6:
            if (c == "header") return tsl::headerTextColor;
            break;
            
        case 7:
            if (c[0] == 'w' && c == "warning") return tsl::warningTextColor;
            if (c[0] == 's' && c == "section") return tsl::sectionTextColor;
            if (c[0] == 'b' && c == "bad_ram") return tsl::badRamTextColor;
            break;
            
        case 8:
            if (c == "on_value") return tsl::onTextColor;
            break;
            
        case 9:
            if (c == "off_value") return tsl::offTextColor;
            break;
            
        case 11:
            if (c[0] == 'h' && c == "healthy_ram") return tsl::healthyRamTextColor;
            if (c[0] == 'n' && c == "neutral_ram") return tsl::neutralRamTextColor;
            break;
    }
    
    return tsl::RGB888(c);
}


void drawTable(
    tsl::elm::List*      list,
    const std::vector<std::vector<std::string>>& tableData,
    std::vector<std::string>&             sectionLines,
    std::vector<std::string>&             infoLines,
    size_t columnOffset             = 164,
    size_t startGap                 = 20,
    size_t endGap                   = 9+2,
    size_t newlineGap               = 4,
    const std::string& tableSectionTextColor       = DEFAULT_STR,
    const std::string& tableInfoTextColor          = DEFAULT_STR,
    const std::string& tableInfoTextHighlightColor = DEFAULT_STR,
    const std::string& alignment                   = LEFT_STR,
    bool hideTableBackground        = false,
    bool useHeaderIndent            = false,
    bool isPolling                  = false,
    bool isScrollable               = true,
    const std::string& wrappingMode               = "none",
    bool useWrappedTextIndent        = false,
    const std::string& packagePath          = "",
    const std::string& bgColor              = DEFAULT_STR,
    bool drawBorder                  = true
) {
    // Prebuild initial buffers
    std::vector<std::string> cacheExpSec, cacheExpInfo;
    std::vector<s32>         cacheYOff;
    std::vector<s32>         cacheXOff;

    const bool usingPlaceholders = buildTableDrawerLines(
        tableData, sectionLines, infoLines, packagePath,
        columnOffset, startGap, newlineGap,
        wrappingMode, alignment, useWrappedTextIndent,
        cacheExpSec, cacheExpInfo, cacheYOff, cacheXOff
    ) && isPolling;

    // Resolve colors once outside the render loop
    const auto secRaw = getRawColor(tableSectionTextColor, tsl::sectionTextColor);
    const auto infoRaw = getRawColor(tableInfoTextColor, tsl::infoTextColor);
    const auto hiliteRaw = getRawColor(tableInfoTextHighlightColor, tsl::infoTextColor);

    // Custom background color (;bg_color=): only the RGB channels are resolved
    // here -- the alpha channel is taken live from the theme's table background
    // color at draw time (in TableDrawer::draw), so opacity/fade settings and
    // theme changes still apply normally.
    const bool hasCustomBgColor = (bgColor != DEFAULT_STR);
    const auto customBgRaw = hasCustomBgColor ? getRawColor(bgColor, tsl::Color(0,0,0,0)) : tsl::Color(0,0,0,0);
    
    // Pre-calculate color comparison
    const bool sameCol = (tableInfoTextColor == tableInfoTextHighlightColor);

    // Use nanoseconds for high-performance timing
    u64 lastUpdateNS = ult::nowNs();
    static constexpr u64 ONE_SECOND_NS = 1000000000ULL;

    // Pre-compute height before moving the vectors into the closure
    const u32 itemHeight = static_cast<u32>(
        16 * cacheExpSec.size()
        + newlineGap * (cacheExpSec.empty() ? 0 : cacheExpSec.size() - 1)
        + endGap
    );

    list->addItem(new tsl::elm::TableDrawer(
        [=,
         cacheExpSec  = std::move(cacheExpSec),
         cacheExpInfo = std::move(cacheExpInfo),
         cacheYOff    = std::move(cacheYOff),
         cacheXOff    = std::move(cacheXOff)]
        (tsl::gfx::Renderer* renderer, s32 x, s32 y, s32 w, s32 h) mutable {

            if (usingPlaceholders) {
                const u64 currentNS = ult::nowNs();
                
                if ((currentNS - lastUpdateNS) >= ONE_SECOND_NS) {
                    buildTableDrawerLines(
                        tableData, sectionLines, infoLines, packagePath,
                        columnOffset, startGap, newlineGap,
                        wrappingMode, alignment, useWrappedTextIndent,
                        cacheExpSec, cacheExpInfo, cacheYOff, cacheXOff
                    );
                    lastUpdateNS = currentNS;
                }
            }

            // Minimal branching, maximum cache efficiency
            if (useHeaderIndent) {
                if (!ult::useSwitch2Style) renderer->drawRect(x-2 +5, y, 4, 22, renderer->aWithOpacity(tsl::headerSeparatorColor));
                else renderer->drawRoundedRectSingleThreaded(x-2 +5, y, 4, 22, 2, renderer->aWithOpacity(tsl::headerSeparatorColor));
            }

            // Convert to renderer colors once per frame
            const auto secColor = (secRaw);
            const auto infoColor = (infoRaw);
            const auto dividerColor = (tsl::textSeparatorColor);
            
            const size_t count = cacheExpSec.size();
            const s32 baseX = (useHeaderIndent) ? x+12+5: x + 12 +1;

            // ── Viewport culling ──────────────────────────────────────────────
            // TableDrawer::draw calls enableScissoring(0, 88, w, FramebufferHeight-163)
            // before invoking this lambda, so any row whose pixels fall entirely
            // outside [kClipTop, kClipBottom) produces no visible output.
            // drawString still runs full glyph-lookup work (mutex lock, shared_ptr
            // atomic ops per character) for every row it is called on — including
            // off-screen ones — because the scissor clip happens deep inside
            // renderGlyph, after all setup is done.
            //
            // cacheYOff is monotonically increasing (processLines increments curY
            // by lineHeight+newlineGap every step), so:
            //   • Binary-search for the first row whose bottom edge reaches kClipTop.
            //   • Break as soon as a row's baseline is at or past kClipBottom —
            //     all subsequent rows are further down and also invisible.
            //
            // Result: O(log N) skip of off-screen rows above + O(1) exit below,
            // reducing drawString calls from O(total) to O(visible) every frame.
            static constexpr s32 kRowH     = 16;   // lineHeight / fontSize constant
            static constexpr s32 kClipTop  = 88+10;
            const s32 kClipBottom = kClipTop
                + static_cast<s32>(tsl::cfg::FramebufferHeight) - 73 - 97 + 2 + 5;

            // Lower-bound binary search: first row where bottom edge > kClipTop.
            size_t firstVis = 0;
            {
                size_t lo = 0, hi = count;
                while (lo < hi) {
                    const size_t mid = (lo + hi) >> 1;
                    if (y + cacheYOff[mid] + kRowH <= kClipTop)
                        lo = mid + 1;
                    else
                        hi = mid;
                }
                firstVis = lo;
            }

            if (sameCol) {
                // Fastest path: same colors, minimal function calls
                for (size_t i = firstVis; i < count; ++i) {
                    const s32 yPos = y + cacheYOff[i];
                    if (yPos >= kClipBottom) break;
                    renderer->drawStringWithColoredSections(cacheExpSec[i], false, tsl::s_dividerSpecialChars, baseX, yPos, 16, secColor, dividerColor);
                    // When useHeaderIndent, right-align the info value to the same
                    // anchor as CategoryHeader::draw (getX()+2+getWidth()-valueWidth-5).
                    // Lambda x=getX()+4, w=getWidth()+4, so that anchor is x+w-valueWidth-11.
                    const s32 infoX = useHeaderIndent
                        ? (x + w - renderer->getTextDimensions(cacheExpInfo[i], false, 16).first - 11)
                        : (x + cacheXOff[i]);
                    renderer->drawStringWithColoredSections(cacheExpInfo[i], false, tsl::s_dividerSpecialChars, infoX, yPos, 16, infoColor, dividerColor);
                }
            } else {
                // Different colors path
                const auto hiliteColor = hiliteRaw;
                
                for (size_t i = firstVis; i < count; ++i) {
                    const s32 yPos = y + cacheYOff[i];
                    if (yPos >= kClipBottom) break;
                    renderer->drawStringWithColoredSections(cacheExpSec[i], false, tsl::s_dividerSpecialChars, baseX, yPos, 16, secColor, dividerColor);
                    // When useHeaderIndent, right-align the info value to the same
                    // anchor as CategoryHeader::draw (getX()+2+getWidth()-valueWidth-5).
                    // Lambda x=getX()+4, w=getWidth()+4, so that anchor is x+w-valueWidth-11.
                    const s32 infoX = useHeaderIndent
                        ? (x + w - renderer->getTextDimensions(cacheExpInfo[i], false, 16).first - 11)
                        : (x + cacheXOff[i]);
                    renderer->drawStringWithHighlight(
                        cacheExpInfo[i], false, infoX, yPos, 16,
                        infoColor, hiliteColor
                    );
                }
            }
        },
        hideTableBackground,
        endGap,
        isScrollable,
        drawBorder,
        hasCustomBgColor,
        customBgRaw
    ), itemHeight);
}


// ─── addTable simply forwards through ───────────────────────────────────────────
void addTable(
    tsl::elm::List*       list,
    const std::vector<std::vector<std::string>>& tableData,
    const std::string&                     packagePath,
    const size_t&                          columnOffset                = 164,
    const size_t&                          tableStartGap               = 20,
    const size_t&                          tableEndGap                 = 9+2,
    const size_t&                          tableSpacing                = 0,
    const std::string&                     tableSectionTextColor       = DEFAULT_STR,
    const std::string&                     tableInfoTextColor          = DEFAULT_STR,
    const std::string&                     tableInfoTextHighlightColor = DEFAULT_STR,
    const std::string&                     tableAlignment              = RIGHT_STR,
    const bool&                            hideTableBackground         = false,
    const bool&                            useHeaderIndent             = false,
    const bool&                            isPolling                   = false,
    const bool&                            isScrollable                = true,
    const std::string&                     wrappingMode                = "none",
    const bool&                            useWrappedTextIndent        = false,
    const std::string&                     tableBgColor                = DEFAULT_STR,
    const bool&                            tableDrawBorder             = true
) {
    std::vector<std::string> sectionLines, infoLines;
    drawTable(
        list, tableData,
        sectionLines, infoLines,
        columnOffset, tableStartGap, tableEndGap, tableSpacing,
        tableSectionTextColor, tableInfoTextColor, tableInfoTextHighlightColor,
        tableAlignment, hideTableBackground, useHeaderIndent,
        isPolling, isScrollable, wrappingMode, useWrappedTextIndent,
        packagePath, tableBgColor, tableDrawBorder
    );
}


void addHelpInfo(tsl::elm::List* list) {
    addHeader(list, USER_GUIDE);

    std::vector<std::string> sectionLines = {
        SETTINGS_MENU,
        SCRIPT_OVERLAY,
        STAR_FAVORITE,
        APP_SETTINGS
    };

    std::vector<std::string> infoLines = {
        "\uE0B5 (" + ON_MAIN_MENU + ")",
        "\uE0B6 (" + ON_A_COMMAND + ")",
        "\uE0E2 (" + ON_OVERLAY_PACKAGE + ")",
        "\uE0E3 (" + ON_OVERLAY_PACKAGE + ")"
    };

    size_t maxInfoWidth = 0;
    for (const auto& s : infoLines)
        maxInfoWidth = std::max(maxInfoWidth, static_cast<size_t>(tsl::gfx::calculateStringWidth(s, 16, false)));

    std::vector<std::vector<std::string>> dummyTableData;
    drawTable(list, dummyTableData, sectionLines, infoLines,
        static_cast<size_t>(tsl::cfg::FramebufferWidth) - 90 - maxInfoWidth,
        20, 9+2, 4);
}


// Returns the last Unicode codepoint in a UTF-8 string, or 0 on failure
static u32 lastCodepoint(const std::string& s) {
    if (s.empty()) return 0;
    const u8* ptr = reinterpret_cast<const u8*>(s.c_str());
    const u8* end = ptr + s.size();
    u32 cp = 0;
    while (ptr < end) {
        u32 tmp;
        ssize_t w = decode_utf8(&tmp, ptr);
        if (w <= 0) break;
        cp = tmp;
        ptr += w;
    }
    return cp;
}


void addPackageInfo(tsl::elm::List* list, auto& packageHeader, std::string type = PACKAGE_STR, std::string defaultLang = "en") {
    addHeader(list, (type == PACKAGE_STR ? PACKAGE_INFO : OVERLAY_INFO));
    static constexpr size_t xOffset = 120;
    static constexpr size_t fontSize = 16;
    const float infoMaxWidth = static_cast<float>(tsl::cfg::FramebufferWidth - 95 - xOffset);
    std::vector<std::string> sectionLines;
    std::vector<std::string> infoLines;
    auto addField = [&](const std::string& header, const std::string& text, const std::string& mode) {
        if (text.empty()) return;
        sectionLines.push_back(header);
        const size_t headerLen = header.length();
        const auto lines = tsl::wrapText(text, infoMaxWidth, mode, false, "", 0, fontSize);
        for (size_t i = 0; i < lines.size(); i++) {
            infoLines.push_back(lines[i]);
            if (i + 1 < lines.size())
                sectionLines.push_back(std::string(headerLen, ' '));
        }
    };
    auto getTranslated = [](const std::string& s) -> std::string {
        std::shared_lock<std::shared_mutex> readLock(tsl::gfx::s_translationCacheMutex);
        auto it = ult::translationCache.find(s);
        return (it != ult::translationCache.end()) ? it->second : s;
    };
    std::string creatorHeader = _CREATOR;
    
    const bool hasComma =
        packageHeader.creator.find(',')  != std::string::npos ||
        packageHeader.creator.find("，") != std::string::npos ||
        packageHeader.creator.find("、") != std::string::npos;
    
    if (type == OVERLAY_STR || !hasComma) {
        if (auto pos = creatorHeader.find('('); pos != std::string::npos)
            creatorHeader.resize(pos);
    }
    
    addField(_TITLE,        packageHeader.title,                  "none");
    addField(_VERSION,      packageHeader.version,                "none");
    addField(creatorHeader, packageHeader.creator,                WORD_STR);
    addField(_ABOUT,        getTranslated(packageHeader.about),   AUTO_STR);
    addField(_CREDITS,      getTranslated(packageHeader.credits), WORD_STR);
    std::vector<std::vector<std::string>> dummyTableData;
    drawTable(list, dummyTableData, sectionLines, infoLines, xOffset, 20, 9+2, 3, DEFAULT_STR, DEFAULT_STR, DEFAULT_STR, LEFT_STR, false, false, true);
}

// Load once at startup, store globally
std::vector<u8> devImageData;
constexpr s32 devImageWidth  = 121;
constexpr s32 devImageHeight = 89;
constexpr size_t devImageFrameSize = devImageWidth * devImageHeight * 2; // RGBA4444

bool loadDevImages() {
    const std::string p1 = ASSETS_PATH + "ppkantorski-1.rgba";
    const std::string p2 = ASSETS_PATH + "ppkantorski-2.rgba";
    if (!isFile(p1) || !isFile(p2)) return false;
    devImageData.resize(devImageFrameSize * 2);
    constexpr size_t srcSize = devImageWidth * devImageHeight * 4;
    return loadRGBA8888toRGBA4444(p1, devImageData.data(), srcSize) &&
           loadRGBA8888toRGBA4444(p2, devImageData.data() + devImageFrameSize, srcSize);
}

static u64  creatorStartTick = 0, nextBlinkTick = 0, blinkEndTick = 0;
static bool creatorAnimDone  = false;
static u64  devImageDismissTick = 0;   // non-zero once a tap triggers dismiss
static bool devImageDismissing  = false; // true = sliding down, false = waiting
static bool devImageTouchHeld   = false; // finger held in landing zone → pause slide-in timer
static s32  devImageDismissStartY = static_cast<s32>(557.0f + devImageHeight); // Y where dismiss was triggered
static s32  devImageCurrentDrawY  = static_cast<s32>(557.0f + devImageHeight); // updated every frame
static u64  devImageLastTick    = 0;     // previous frame tick for hold-pause delta

void drawDevImage(tsl::gfx::Renderer* renderer) {
    if (devImageData.size() < devImageFrameSize * 2) return;

    const u64 now = armGetSystemTick();
    if (creatorStartTick == 0) {
        creatorStartTick = now;
        devImageLastTick = now;
    }
    // Per-frame delta used to freeze the slide-in timer while a touch is held
    const u64 delta = now - devImageLastTick;
    devImageLastTick = now;

    constexpr float targetY = 557.0f, startY = targetY + devImageHeight;
    constexpr float duration = 0.5f, delay = 0.2f;
    s32 drawY;

    // Dismiss branch is checked first so a mid-slide-in tap is handled correctly
    if (devImageDismissTick != 0) {
        const float elapsed = armTicksToNs(now - devImageDismissTick) / 1e9f;
        if (devImageDismissing) {
            // Slide down from wherever dismiss was triggered (not necessarily targetY)
            const float t = std::min(elapsed / duration, 1.0f);
            const float ease = 1.0f - (1.0f - t) * (1.0f - t) * (1.0f - t);
            drawY = static_cast<s32>(devImageDismissStartY + (startY - devImageDismissStartY) * ease);
            if (t >= 1.0f) {
                drawY = static_cast<s32>(startY);
                devImageDismissing  = false;
                devImageDismissTick = now; // restart timer for wait phase
            }
        } else {
            // Hold off-screen for 0.5 s, then restart the slide-up
            drawY = static_cast<s32>(startY);
            if (elapsed >= 0.5f) {
                devImageDismissTick = 0;
                creatorStartTick    = now;
                devImageLastTick    = now;
                creatorAnimDone     = false;
                nextBlinkTick = blinkEndTick = 0;
            }
        }
    } else if (!creatorAnimDone) {
        // Freeze the slide-in timer while the user's finger is held in the landing zone
        if (devImageTouchHeld)
            creatorStartTick += delta;

        const float elapsed = armTicksToNs(now - creatorStartTick) / 1e9f;
        if (elapsed < delay) {
            drawY = static_cast<s32>(startY);
        } else {
            const float t = std::min((elapsed - delay) / duration, 1.0f);
            const float ease = 1.0f - (1.0f - t) * (1.0f - t) * (1.0f - t);
            drawY = static_cast<s32>(startY + (targetY - startY) * ease);
            if (t >= 1.0f) {
                creatorAnimDone = true;
                drawY = static_cast<s32>(targetY);
                const float firstBlink = 1.0f + (rand() % 300) / 100.0f;
                nextBlinkTick = now + armNsToTicks(static_cast<u64>(firstBlink * 1e9f));
            }
        }
    } else {
        drawY = static_cast<s32>(targetY);
    }

    // Expose current position so the touch handler can use it for hit-testing
    devImageCurrentDrawY = drawY;

    bool useFrame2 = devImageDismissing; // use 2nd image while sliding down
    if (creatorAnimDone && !devImageDismissing) {
        if (blinkEndTick > 0 && now < blinkEndTick) {
            useFrame2 = true;
        } else if (blinkEndTick > 0 && now >= blinkEndTick) {
            blinkEndTick = 0;
            nextBlinkTick = now + armNsToTicks(static_cast<u64>((1.0f + (rand() % 600) / 100.0f) * 1e9f));
        } else if (nextBlinkTick > 0 && now >= nextBlinkTick) {
            blinkEndTick  = now + armNsToTicks(150000000ULL);
            nextBlinkTick = 0;
            useFrame2 = true;
        }
    }
    const u8* imageData = devImageData.data() + (useFrame2 ? devImageFrameSize : 0);
    renderer->drawBitmapRGBA4444(258, drawY, devImageWidth, devImageHeight, imageData, tsl::gfx::Renderer::s_opacity);
}


/**
 * @brief Ultrahand-Overlay Protected Folders
 *
 * This block of code defines two vectors containing paths to protected folders used in the
 * Ultrahand-Overlay project. These folders are designated as protected to prevent certain
 * operations that may pose security risks.
 *
 * The two vectors include:
 *
 * - `protectedFolders`: Paths to standard protected folders.
 * - `ultraProtectedFolders`: Paths to ultra protected folders with stricter security.
 *
 * These protected folder paths are used within the Ultrahand-Overlay project to enforce
 * safety conditions and ensure that certain operations are not performed on sensitive
 * directories.
 */

bool isDangerousCombination(const std::string& originalPath) {
    // Early exit: Check for double wildcards first (cheapest check)
    if (originalPath.find("**") != std::string::npos) {
        return true;
    }
    
    // Early exit: Check if path contains wildcards at all
    const bool hasWildcards = originalPath.find('*') != std::string::npos;
    
    // 1) Normalize repeated wildcards only if wildcards exist
    std::string patternPath;
    if (hasWildcards) {
        bool lastWasStar = false;
        for (char c : originalPath) {
            if (c == '*') {
                if (!lastWasStar) {
                    patternPath += c;
                    lastWasStar = true;
                }
                // skip extra '*'
            } else {
                patternPath += c;
                lastWasStar = false;
            }
        }
    } else {
        patternPath = originalPath; // No wildcards, use as-is
    }
    
    // 2) Define folder sets with precomputed lengths for faster comparisons
    struct FolderInfo {
        const char* path;
        size_t length;
    };
    
    static constexpr FolderInfo albumFolders[] = {
        {"sdmc:/Nintendo/Album/", 20},
        {"sdmc:/emuMMC/RAW1/Nintendo/Album/", 33}
    };
    
    static constexpr FolderInfo ultraProtectedFolders[] = {
        {"sdmc:/Nintendo/Contents/", 23},
        {"sdmc:/Nintendo/save/", 18},
        {"sdmc:/emuMMC/RAW1/Nintendo/Contents/", 36},
        {"sdmc:/emuMMC/RAW1/Nintendo/save/", 31}
    };
    
    static constexpr FolderInfo restrictedWildcardFolders[] = {
        {"sdmc:/config/", 13},
        {"sdmc:/bootloader/", 18},
        {"sdmc:/atmosphere/", 18},
        {"sdmc:/switch/", 14}
    };
    
    static constexpr FolderInfo protectedFolders[] = {
        {"sdmc:/Nintendo/", 16},
        {"sdmc:/emuMMC/", 13},
        {"sdmc:/emuMMC/RAW1/", 18}
    };
    
    // 3) Check other always dangerous patterns (cheap string searches)
    static constexpr std::array<const char*, 3> alwaysDangerousPatterns = {
        "~",
        "null*",
        "*null"
    };
    
    for (const auto& pat : alwaysDangerousPatterns) {
        if (patternPath.find(pat) != std::string::npos) {
            return true;
        }
    }
    
    // 4) Root folder wildcard check (only if wildcards exist)
    if (hasWildcards) {
        static constexpr const char rootPrefix[] = "sdmc:/";
        static constexpr size_t rootLen = 6; // Length of "sdmc:/"
        
        if (patternPath.length() >= rootLen && 
            patternPath.compare(0, rootLen, rootPrefix) == 0) {
            size_t nextSlash = patternPath.find('/', rootLen);
            if (nextSlash == std::string::npos) nextSlash = patternPath.size();
            const size_t wildcardPos = patternPath.find('*', rootLen);
            if (wildcardPos != std::string::npos && wildcardPos < nextSlash) {
                return true; // wildcard in top-level directory disallowed
            }
        } else {
            // Disallow wildcards outside sdmc:/
            return true;
        }
    }
    
    // 5) Check ultra-protected folders (highest priority)
    for (const auto& folder : ultraProtectedFolders) {
        if (patternPath.length() >= folder.length &&
            patternPath.compare(0, folder.length, folder.path) == 0) {
            return true;
        }
    }
    
    // 6) Check restricted wildcard folders (only if wildcards exist)
    std::string relative;
    if (hasWildcards) {
        for (const auto& folder : restrictedWildcardFolders) {
            if (patternPath.length() >= folder.length &&
                patternPath.compare(0, folder.length, folder.path) == 0) {
                
                // OPTIMIZED: Create relative string only when processing restricted folders
                relative = patternPath.substr(folder.length);
                
                // If relative is empty or just '*', it means "the whole folder" or "all files"
                if (relative.empty() || relative == "*" || relative == "*/") {
                    return true; // block broad delete or targeting folder itself
                }
                
                // Otherwise allow wildcards deeper inside folder
                return false;
            }
        }
    }
    
    // 7) Check protected folders
    bool isAlbum;
    
    for (const auto& folder : protectedFolders) {
        if (patternPath.length() >= folder.length &&
            patternPath.compare(0, folder.length, folder.path) == 0) {
            
            // Check if this is an album folder (exception to protection)
            isAlbum = false;
            for (const auto& albumFolder : albumFolders) {
                if (patternPath.length() >= albumFolder.length &&
                    patternPath.compare(0, albumFolder.length, albumFolder.path) == 0) {
                    isAlbum = true;
                    break;
                }
            }
            
            if (isAlbum) {
                return false; // wildcards allowed in album folders
            }
            
            // Check for wildcards in protected folder (only if wildcards exist)
            if (hasWildcards) {
                // OPTIMIZED: Create relative string only when processing protected folders
                relative = patternPath.substr(folder.length);
                if (relative.find('*') != std::string::npos) {
                    return true; // wildcard in protected folder disallowed
                }
            }
            return false;
        }
    }
    
    // 8) Otherwise, no dangerous combination detected
    return false;
}


// Function to populate selectedItemsListOff from a JSON array based on a key
void populateSelectedItemsListFromJson(const std::string& sourceType, const std::string& jsonStringOrPath, const std::string& jsonKey, std::vector<std::string>& selectedItemsList) {
    selectedItemsList.clear();

    // Check for empty JSON source strings
    if (jsonStringOrPath.empty()) {
        return;
    }
    // Use a unique_ptr to manage JSON object with appropriate deleter
    std::unique_ptr<json_t, JsonDeleter> jsonData(nullptr, JsonDeleter());
    // Convert JSON string or read from file based on the source type
    if (sourceType == JSON_STR) {
        jsonData.reset(stringToJson(jsonStringOrPath));
    } else if (sourceType == JSON_FILE_STR) {
        jsonData.reset(readJsonFromFile(jsonStringOrPath));
    }
    // Early return if jsonData is null or not an array
    if (!jsonData) {
        return;
    }
    
    cJSON* jsonArray = reinterpret_cast<cJSON*>(jsonData.get());
    if (!cJSON_IsArray(jsonArray)) {
        return;
    }
    
    // Prepare for efficient insertion
    const int arraySize = cJSON_GetArraySize(jsonArray);
    
    // Store the key as a const char* to avoid repeated c_str() calls
    const char* jsonKeyCStr = jsonKey.c_str();
    
    // Iterate over the JSON array
    for (int i = 0; i < arraySize; ++i) {
        cJSON* item = cJSON_GetArrayItem(jsonArray, i);
        if (cJSON_IsObject(item)) {
            cJSON* keyValue = cJSON_GetObjectItemCaseSensitive(item, jsonKeyCStr);
            if (cJSON_IsString(keyValue) && keyValue->valuestring) {
                selectedItemsList.emplace_back(keyValue->valuestring);
            }
        }
    }
}



/**
 * @brief Replaces a placeholder with a replacement string in the input.
 *
 * This function replaces all occurrences of a specified placeholder with the
 * provided replacement string in the input string.
 *
 * @param input The input string.
 * @param placeholder The placeholder to replace.
 * @param replacement The string to replace the placeholder with.
 * @return The input string with placeholders replaced by the replacement string.
 */
inline void applyPlaceholderReplacement(std::string& input, const std::string& placeholder, const std::string& replacement) {
    const size_t pos = input.find(placeholder);
    if (pos == std::string::npos) {
        return;  // Returns original string directly if no placeholder is found
    }
    input.replace(pos, placeholder.length(), replacement);
}




void applyReplaceIniPlaceholder(std::string& arg, const std::string& commandName, const std::string& iniPath) {
    const std::string searchString = "{" + commandName + "(";
    
    // Early exit: Check if placeholder pattern exists before doing any INI work
    if (arg.find(searchString) == std::string::npos) {
        return; // No placeholders found, nothing to do
    }
    
    // Pre-declare variables outside the loop
    size_t startPos = 0;
    size_t endPos;
    size_t commaPos;
    size_t entryIndex;
    std::string placeholderContent;
    std::string replacement;
    std::string iniSection;
    std::string iniKey;
    
    // Cache section names to avoid re-parsing INI file multiple times
    std::vector<std::string> sectionNames;
    bool sectionsLoaded = false;
    
    // Build result incrementally to avoid expensive string replacement operations
    std::string result;
    size_t lastPos = 0;
    const size_t searchStringLen = searchString.length();
    
    // Process all occurrences of the placeholder
    while ((startPos = arg.find(searchString, lastPos)) != std::string::npos) {
        // Depth-aware scan for the matching ")}" so that nested {ini_file(...)}
        // placeholders inside the argument list do not fool us into stopping early.
        // e.g. {ini_file({ini_file(0)},{ini_file(软件版本,{ini_file(0)})})}
        //                                                             ^
        //       naive find(")}", startPos) would stop here (depth 2), not at the
        //       real closing ")}" of the outermost placeholder.
        {
            int depth = 1;
            size_t scan = startPos + searchStringLen;
            endPos = std::string::npos;
            while (scan + 1 < arg.size()) {
                if (arg.compare(scan, searchStringLen, searchString) == 0) {
                    // Nested opener of the same type — go deeper
                    ++depth;
                    scan += searchStringLen;
                } else if (arg[scan] == ')' && arg[scan + 1] == '}') {
                    if (--depth == 0) {
                        endPos = scan;
                        break;
                    }
                    scan += 2;
                } else {
                    ++scan;
                }
            }
        }
        if (endPos == std::string::npos || endPos <= startPos) {
            // Invalid/unmatched placeholder — skip past the opener and continue
            result.append(arg, lastPos, startPos + searchStringLen - lastPos);
            lastPos = startPos + searchStringLen;
            continue;
        }
        
        // Append text before placeholder
        result.append(arg, lastPos, startPos - lastPos);
        
        placeholderContent = arg.substr(startPos + searchStringLen, 
                                       endPos - startPos - searchStringLen);
        trim(placeholderContent);

        // If placeholderContent still contains unresolved inner placeholders, skip
        // the entire outer placeholder and leave it intact for a later resolution pass.
        // Without this guard, the comma-split and integer-check below would operate on
        // literal placeholder text (e.g. "{ini_file(0)}") instead of resolved values,
        // producing null instead of deferring correctly.
        if (placeholderContent.find('{') != std::string::npos) {
            result.append(searchString);  // re-emit the opener
            result.append(placeholderContent);
            result.append(")}");          // re-emit the closer
            lastPos = endPos + 2;
            continue;
        }

        // Find the top-level comma (depth-aware: skip commas inside nested placeholders).
        // A plain find(',') would mis-split on e.g. "sec,{ini_file(a,b)}" — finding the
        // comma inside the inner placeholder first and producing a broken iniSection.
        // The unresolved-inner guard above now prevents us from ever reaching here with
        // '{' still present, but the depth-aware scan is kept as a correct-by-construction
        // safeguard for any future call paths that may bypass the guard.
        commaPos = std::string::npos;
        {
            int d = 0;
            for (size_t i = 0; i < placeholderContent.size(); ++i) {
                if (placeholderContent[i] == '{') { ++d; continue; }
                if (placeholderContent[i] == '}') { --d; continue; }
                if (d == 0 && placeholderContent[i] == ',') { commaPos = i; break; }
            }
        }
        
        if (commaPos != std::string::npos) {
            // Handle section,key format
            iniSection = placeholderContent.substr(0, commaPos);
            trim(iniSection);
            removeQuotes(iniSection);
            
            iniKey = placeholderContent.substr(commaPos + 1);
            trim(iniKey);
            removeQuotes(iniKey);
            
            replacement = returnOrNull(parseValueFromIniSection(iniPath, iniSection, iniKey));
        } else {
            // Check if the content is an integer (section index lookup)
            if (std::all_of(placeholderContent.begin(), placeholderContent.end(), ::isdigit)) {
                if (!isValidNumber(placeholderContent)) {
                    replacement = NULL_STR;
                } else {
                    entryIndex = ult::stoi(placeholderContent);
                    
                    // Load section names only once when needed
                    if (!sectionsLoaded) {
                        sectionNames = parseSectionsFromIniPattern(iniPath);
                        sectionsLoaded = true;
                    }
                    
                    if (entryIndex < sectionNames.size()) {
                        replacement = sectionNames[entryIndex];
                    } else {
                        replacement = NULL_STR;
                    }
                }
            } else {
                // Content is non-numeric and has no comma — not a valid ini_file argument.
                // Leave the whole placeholder intact rather than collapsing it to null,
                // so a later resolution pass can still act on it if the content resolves.
                result.append(searchString);
                result.append(placeholderContent);
                result.append(")}");
                lastPos = endPos + 2;
                continue;
            }
        }
        
        // Append the replacement
        result.append(replacement);
        
        // Update lastPos to continue after this placeholder
        lastPos = endPos + 2;
    }
    
    // Append remaining text after last placeholder
    result.append(arg, lastPos);
    
    // Replace original string with result
    arg = std::move(result);
}


/**
 * @brief Replaces a JSON source placeholder with the actual JSON source.
 *
 * Optimized version with variables moved to usage scope to avoid repeated allocations.
 * Supports "null" key as a fallback default for failed lookups.
 * Array path segments accept numeric indices or field=value matches (case-sensitive;
 * first match wins), e.g. {json_file(0,assets,name=Test.nro,browser_download_url)}.
 *
 * @param arg The input string containing the placeholder.
 * @param commandName The name of the JSON command (e.g., "json", "json_file").
 * @param jsonPathOrString The path to the JSON file or the JSON string itself.
 * @return std::string The input string with the placeholder replaced by the actual JSON source,
 *                   or the original input string if replacement failed or jsonDict is nullptr.
 */
std::string replaceJsonPlaceholder(const std::string& arg, const std::string& commandName, const std::string& jsonPathOrString) {
    // Early exit: Check if placeholder pattern exists before doing any JSON work
    const std::string searchString = "{" + commandName + "(";
    if (arg.find(searchString) == std::string::npos) {
        return arg; // No placeholders found, return original
    }
    
    // Load JSON data only if we have placeholders to process
    std::unique_ptr<json_t, JsonDeleter> jsonDict;
    if (commandName == "json" || commandName == "json_source") {
        jsonDict.reset(stringToJson(jsonPathOrString));
    } else if (commandName == "json_file" || commandName == "json_file_source") {
        jsonDict.reset(readJsonFromFile(jsonPathOrString));
    }
    if (!jsonDict) {
        return arg; // Return original string if JSON data couldn't be loaded
    }
    
    // Build result incrementally to avoid expensive string replacement operations
    std::string result;
    
    size_t lastPos = 0;
    size_t startPos = arg.find(searchString);
    
    const size_t searchStringLen = searchString.length();
    
    // Pre-declare variables outside loops to avoid repeated allocations
    size_t endPos, nextPos, commaPos, index;
    std::string key;
    bool validValue;
    
    // Keep reference to root for "null" fallback lookups
    cJSON* root = reinterpret_cast<cJSON*>(jsonDict.get());
    
    while (startPos != std::string::npos) {
        endPos = arg.find(")}", startPos);
        if (endPos == std::string::npos) {
            break; // Break if no closing tag is found
        }
        
        // Append text before placeholder
        result.append(arg, lastPos, startPos - lastPos);
        
        nextPos = startPos + searchStringLen;
        cJSON* value = root; // Start from root
        validValue = true;
        
        while (nextPos < endPos && validValue) {
            commaPos = arg.find(',', nextPos);
            if (commaPos == std::string::npos || commaPos > endPos) {
                commaPos = endPos; // Set to endPos if no comma is found or it's beyond endPos
            }
            
            // Reuse string capacity for key
            key.assign(arg, nextPos, commaPos - nextPos);
            
            if (cJSON_IsObject(value)) {
                value = cJSON_GetObjectItemCaseSensitive(value, key.c_str()); // Navigate through object
            } else if (cJSON_IsArray(value)) {
                // Array path: numeric index (e.g. 12) or field=value match (e.g. name=Test.nro).
                // Dots in match values are literal (not nested keys). First case-sensitive match wins.
                const size_t eqPos = key.find('=');
                if (eqPos != std::string::npos && eqPos > 0) {
                    const std::string field(key, 0, eqPos);
                    cJSON* found = nullptr;
                    const int arraySize = cJSON_GetArraySize(value);
                    for (int i = 0; i < arraySize; ++i) {
                        cJSON* item = cJSON_GetArrayItem(value, i);
                        if (!cJSON_IsObject(item)) {
                            continue;
                        }
                        cJSON* fieldItem = cJSON_GetObjectItemCaseSensitive(item, field.c_str());
                        if (cJSON_IsString(fieldItem) && fieldItem->valuestring &&
                            key.compare(eqPos + 1, std::string::npos, fieldItem->valuestring) == 0) {
                            found = item;
                            break;
                        }
                    }
                    value = found; // nullptr on no match → same fallback as a bad index
                } else {
                    index = std::stoul(key); // Convert key to index for arrays
                    value = cJSON_GetArrayItem(value, index);
                }
            } else {
                validValue = false; // Set validValue to false if value is neither object nor array
            }
            nextPos = commaPos + 1; // Move next position past the comma
        }
        
        if (validValue && value && cJSON_IsString(value) && value->valuestring) {
            // Append the value as-is, even if it's an empty string
            result.append(value->valuestring);
        } else if (validValue && value && cJSON_IsNumber(value)) {
            // Handle numeric JSON values
            double d = value->valuedouble;
            if (d == (double)(long long)d) {
                result.append(std::to_string((long long)d));
            } else {
                char buf[32];
                snprintf(buf, sizeof(buf), "%g", d);
                result.append(buf);
            }
        } else if (validValue && value && cJSON_IsBool(value)) {
            // Handle boolean JSON values
            result.append(cJSON_IsTrue(value) ? TRUE_STR : FALSE_STR);
        } else {
            // Key doesn't exist or isn't a string - try "null" fallback
            cJSON* fallbackValue = cJSON_GetObjectItemCaseSensitive(root, NULL_STR.c_str());
            if (fallbackValue && cJSON_IsString(fallbackValue) && fallbackValue->valuestring) {
                result.append(fallbackValue->valuestring);
            } else {
                result.append(NULL_STR);
            }
        }
        
        lastPos = endPos + 2;
        startPos = arg.find(searchString, lastPos); // Find next occurrence from last position
    }
    
    // Append remaining text after last placeholder
    result.append(arg, lastPos);
    
    return result; // Return the modified string
}

// Helper function to replace placeholders
void replaceAllPlaceholders(std::string& source, const std::string& placeholder, const std::string& replacement) {
    size_t pos = 0;
    while ((pos = source.find(placeholder, pos)) != std::string::npos) {
        source.replace(pos, placeholder.size(), replacement);
        pos += replacement.size();
    }
}



// Helper function to replace all placeholders in a single pass
bool replacePlaceholdersInArg(std::string& source, const std::unordered_map<std::string, std::string>& replacements) {
    bool replaced = false;
    size_t pos;
    for (const auto& [placeholder, replacement] : replacements) {
        pos = 0;
        while ((pos = source.find(placeholder, pos)) != std::string::npos) {
            source.replace(pos, placeholder.length(), replacement);
            pos += replacement.length();  // Move past the replacement to avoid infinite loop
            replaced = true;
        }
    }
    return replaced;
}


// Optimized getSourceReplacement function
std::vector<std::vector<std::string>> getSourceReplacement(const std::vector<std::vector<std::string>>& commands,
    const std::string& entry, size_t entryIndex, const std::string& packagePath = "") {

    bool inEristaSection = false;
    bool inMarikoSection = false;

    std::vector<std::vector<std::string>> modifiedCommands;
    std::string listString, listPath, jsonString, jsonPath, iniPath, iniFilePath;
    std::string hexFilePath;
    bool usingFileSource = false;

    std::string fileName = getNameFromPath(entry);
    if (!isDirectory(entry)) {
        dropExtension(fileName);
    }

    std::vector<std::string> modifiedCmd;
    std::string commandName;
    std::string modifiedArg;
    size_t startPos, endPos;
    std::string replacement;

    std::string path;
    std::string raw;

    const std::string indexStr = ult::to_string(entryIndex);



    for (const auto& cmd : commands) {
        if (cmd.empty()) {
            continue;
        }

        modifiedCmd.clear();
        commandName = cmd[0];

        if (commandName == "download") {
            isDownloadCommand.store(true, std::memory_order_release);
        }

        if (stringToLowercase(commandName) == "erista:") {
            inEristaSection = true;
            inMarikoSection = false;
            continue;
        } else if (stringToLowercase(commandName) == "mariko:") {
            inEristaSection = false;
            inMarikoSection = true;
            continue;
        }

        if ((inEristaSection && usingErista) ||
            (inMarikoSection && usingMariko) ||
            (!inEristaSection && !inMarikoSection))
        {
            // Update source paths/values from this command before processing args.
            // Done unconditionally (no .empty() guard) so sources can be redefined mid-section.
            // Each source arg is fully resolved using whatever ini_file/hex_file/etc. are current
            // at this point in the section, so placeholders can be composed inside any source.
            if (commandName == "file_source") {
                usingFileSource = true;
            } else if (cmd.size() >= 2) {
                auto resolveArg = [&](std::string val) -> std::string {
                    std::vector<std::string> tmp = { std::move(val) };
                    applyPlaceholderReplacements(tmp, hexFilePath, iniFilePath, listString, listPath, jsonString, jsonPath, packagePath);
                    return std::move(tmp[0]);
                };
                if (commandName == "ini_file") {
                    iniFilePath = resolveArg(cmd[1]);
                    preprocessPath(iniFilePath, packagePath);
                } else if (commandName == "hex_file") {
                    hexFilePath = resolveArg(cmd[1]);
                    preprocessPath(hexFilePath, packagePath);
                } else if (commandName == "list_file_source") {
                    listPath = resolveArg(cmd[1]);
                    preprocessPath(listPath, packagePath);
                } else if (commandName == "ini_file_source") {
                    iniPath = resolveArg(cmd[1]);
                    preprocessPath(iniPath, packagePath);
                } else if (commandName == "json_file_source") {
                    jsonPath = resolveArg(cmd[1]);
                    preprocessPath(jsonPath, packagePath);
                } else if (commandName == "json" || commandName == "json_source") {
                    // Raw capture only — content resolved sequentially by interpretAndExecuteCommands
                    jsonString = cmd[1];
                    removeQuotes(jsonString);
                } else if (commandName == "list" || commandName == "list_source") {
                    // Raw capture only — content resolved sequentially by interpretAndExecuteCommands
                    listString = cmd[1];
                    removeQuotes(listString);
                }
            }

            // Apply placeholder replacements to each arg.
            // NOTE: {ini_file(...)} and {hex_file(...)} are intentionally NOT resolved here for
            // regular command args. interpretAndExecuteCommands resolves them sequentially via
            // applyPlaceholderReplacements, so set-ini-val writes are visible to subsequent
            // {ini_file(...)} reads in the same section. Resolving them here upfront would bake
            // in stale values read before any commands in the section have executed.
            for (const auto& arg : cmd) {
                modifiedArg = arg;

                // Resolve local source-entry placeholders first (not in any global map).
                replaceAllPlaceholders(modifiedArg, "{file_source}", entry);
                replaceAllPlaceholders(modifiedArg, "{file_name}", fileName);
                path = getParentDirNameFromPath(entry);
                removeQuotes(path);
                replaceAllPlaceholders(modifiedArg, "{folder_name}", path);
                replaceAllPlaceholders(modifiedArg, "{index}", indexStr);


                // {list_source(...)} block — uses *_source path (index into current selection list)
                if (modifiedArg.find("{list_source(") != std::string::npos) {
                    applyPlaceholderReplacement(modifiedArg, "*", indexStr);
                    startPos = modifiedArg.find("{list_source(");
                    endPos   = modifiedArg.find(")}", startPos + 13);
                    if (endPos != std::string::npos && endPos > startPos) {
                        const auto& listItems = stringToList(listString);
                        raw = (entryIndex < listItems.size()) ? listItems[entryIndex] : "";
                        replacement = returnOrNull(raw);
                        modifiedArg.replace(startPos, endPos - startPos + 2, replacement);
                    }
                }

                // {list_file_source(...)} block
                if (modifiedArg.find("{list_file_source(") != std::string::npos) {
                    applyPlaceholderReplacement(modifiedArg, "*", indexStr);
                    startPos = modifiedArg.find("{list_file_source(");
                    endPos = modifiedArg.find(")}", startPos + 18);
                    if (endPos != std::string::npos && endPos > startPos) {
                        raw = getEntryFromListFile(listPath, entryIndex);
                        replacement = returnOrNull(raw);
                        modifiedArg.replace(startPos, endPos - startPos + 2, replacement);
                    }
                }

                // {ini_file_source(...)} block
                if (modifiedArg.find("{ini_file_source(") != std::string::npos) {
                    applyPlaceholderReplacement(modifiedArg, "*", indexStr);
                    applyReplaceIniPlaceholder(modifiedArg, "ini_file_source", iniPath);
                }

                // {json_source(...)} block
                if (modifiedArg.find("{json_source(") != std::string::npos) {
                    applyPlaceholderReplacement(modifiedArg, "*", indexStr);
                    startPos = modifiedArg.find("{json_source(");
                    endPos   = modifiedArg.find(")}", startPos + 13);
                    if (endPos != std::string::npos && endPos > startPos) {
                        raw = replaceJsonPlaceholder(
                            modifiedArg.substr(startPos, endPos - startPos + 2),
                            "json_source",
                            jsonString
                        );
                        replacement = returnOrNull(raw);
                        modifiedArg.replace(startPos, endPos - startPos + 2, replacement);
                    }
                }

                // {json_file_source(...)} block
                if (modifiedArg.find("{json_file_source(") != std::string::npos) {
                    applyPlaceholderReplacement(modifiedArg, "*", indexStr);
                    startPos = modifiedArg.find("{json_file_source(");
                    endPos   = modifiedArg.find(")}", startPos + 18);
                    if (endPos != std::string::npos && endPos > startPos) {
                        raw = replaceJsonPlaceholder(
                            modifiedArg.substr(startPos, endPos - startPos + 2),
                            "json_file_source",
                            jsonPath
                        );
                        replacement = returnOrNull(raw);
                        modifiedArg.replace(startPos, endPos - startPos + 2, replacement);
                    }
                }

                modifiedCmd.push_back(std::move(modifiedArg));
            }

            // Capture json/list inline values so subsequent *_source placeholders can use them
            // for source-path composition (e.g. json_file_source './{json(key)}/').
            // NOTE: Do NOT resolve {ini_file(...)}, {json(...)}, etc. here.
            // interpretAndExecuteCommands resolves all regular command args sequentially at
            // execution time, so writes from prior commands (download, set-ini-val, etc.) are
            // visible to subsequent {ini_file(...)}/{json(...)} reads in the same script.
            if ((commandName == "json" || commandName == "json_source") && modifiedCmd.size() >= 2) {
                jsonString = modifiedCmd[1];
                removeQuotes(jsonString);
            }
            if ((commandName == "list" || commandName == "list_source") && modifiedCmd.size() >= 2) {
                listString = modifiedCmd[1];
                removeQuotes(listString);
            }
            modifiedCommands.emplace_back(std::move(modifiedCmd));
        }
    }

    if (usingFileSource) {
        modifiedCommands.insert(modifiedCommands.begin(), { "file_name", fileName });
        modifiedCommands.insert(modifiedCommands.begin(), { "folder_name", path });
        modifiedCommands.insert(modifiedCommands.begin(), { "sourced_path", entry });
    }

    return modifiedCommands;
}


std::string getCurrentTimestamp(const std::string& format) {
    // Try using standard POSIX time() function
    time_t seconds = time(nullptr);
    const u32 milliseconds = (ult::nowNs() / 1000000ULL) % 1000; // We lose millisecond precision with this method
    
    std::string modifiedFormat = format;
    bool hasMilliseconds = false;
    
    const size_t pos = modifiedFormat.find("%f");
    if (pos != std::string::npos) {
        modifiedFormat.erase(pos, 2);
        hasMilliseconds = true;
    }
    
    char buffer[30];
    if (!std::strftime(buffer, sizeof(buffer), modifiedFormat.c_str(), std::localtime(&seconds))) {
        return "";
    }
    
    std::string formattedTime(buffer);
    StringStream oss;
    
    if (formattedTime.find("%s") != std::string::npos) {
        oss << static_cast<long long>(seconds); 
        formattedTime.replace(formattedTime.find("%s"), 2, oss.str());
        oss.clear();
    }
    
    if (hasMilliseconds) {
        std::string millisecondsStr = ult::to_string(milliseconds);
        if (millisecondsStr.length() < 3) {
            millisecondsStr.insert(0, 3 - millisecondsStr.length(), '0');
        }
        if (pos > 0 && format[pos - 1] == '.') {
            formattedTime += millisecondsStr;
        } else {
            formattedTime += "." + millisecondsStr;
        }
    }
    
    return formattedTime;
}





// Helper function to skip spaces
void skipSpaces(const std::string& expression, size_t& pos) {
    while (pos < expression.length() && std::isspace(expression[pos])) {
        ++pos;
    }
}

// Helper function to parse a number or a nested expression in parentheses
double parseExpression(const std::string& expression, size_t& pos, bool& valid);

double parseNumber(const std::string& expression, size_t& pos, bool& valid) {
    skipSpaces(expression, pos);

    // Check if the expression starts with a '(' indicating a nested expression
    if (expression[pos] == '(') {
        ++pos;  // Skip the '('
        const double result = parseExpression(expression, pos, valid);
        if (expression[pos] == ')') {
            ++pos;  // Skip the ')'
        } else {
            valid = false;  // Unmatched parentheses
        }
        return result;
    }

    // Parse a number
    double result = 0.0f;
    bool hasDecimal = false;
    double decimalPlace = 0.1f;
    bool isNegative = false;

    if (expression[pos] == '-') {
        isNegative = true;
        ++pos;
    }

    while (pos < expression.length() && (std::isdigit(expression[pos]) || expression[pos] == '.')) {
        if (expression[pos] == '.') {
            hasDecimal = true;
            ++pos;
            continue;
        }

        if (hasDecimal) {
            result += (expression[pos] - '0') * decimalPlace;
            decimalPlace *= 0.1f;
        } else {
            result = result * 10.0f + (expression[pos] - '0');
        }
        ++pos;
    }

    if (isNegative) {
        result = -result;
    }

    valid = true;
    return result;
}

// Function to evaluate an expression, which may include parentheses
double parseExpression(const std::string& expression, size_t& pos, bool& valid) {
    skipSpaces(expression, pos);

    double result = parseNumber(expression, pos, valid);
    if (!valid) return 0;

    char op;
    double operand;
    while (pos < expression.length()) {
        skipSpaces(expression, pos);
        if (pos >= expression.length()) break;

        op = expression[pos++];
        skipSpaces(expression, pos);

        operand = parseNumber(expression, pos, valid);
        if (!valid) return 0;

        switch (op) {
            case '+':
                result += operand;
                break;
            case '-':
                result -= operand;
                break;
            case '*':
                result *= operand;
                break;
            case '/':
                if (operand == 0) {
                    valid = false;  // Division by zero
                    return 0;
                }
                result /= operand;
                break;
            case '%':
                if (result != static_cast<double>(static_cast<int>(result)) ||
                    operand != static_cast<double>(static_cast<int>(operand))) {
                    valid = false;  // Modulus only valid for integers
                    return 0;
                }
                result = static_cast<int>(result) % static_cast<int>(operand);
                break;
            default:
                valid = false;
                return 0;
        }
    }

    valid = true;
    return result;
}

// Function to evaluate a complete expression (this will call parseExpression)
double evaluateExpression(const std::string& expression, bool& valid) {
    size_t pos = 0;
    return parseExpression(expression, pos, valid);
}

// Handle Math Placeholder with Parentheses, Modulus, and Optional Integer Support
std::string handleMath(const std::string& placeholder) {
    const size_t startPos = placeholder.find('(') + 1;
    const size_t endPos = placeholder.find(')');

    if (startPos == std::string::npos || endPos == std::string::npos || startPos >= endPos) {
        return NULL_STR;
    }

    std::string mathExpression = placeholder.substr(startPos, endPos - startPos);
    removeQuotes(mathExpression);

    const size_t commaPos = mathExpression.find(',');
    bool forceInteger = false;

    if (commaPos != std::string::npos) {
        std::string secondParam = mathExpression.substr(commaPos + 1);
        trim(secondParam);
        forceInteger = (secondParam == TRUE_STR);
        mathExpression = mathExpression.substr(0, commaPos);
    }

    size_t pos;
    // Add spaces around operators to ensure proper parsing
    for (const char op : {'+', '-', '*', '/'}) {
        pos = 0;
        while ((pos = mathExpression.find(op, pos)) != std::string::npos) {
            if (pos > 0 && mathExpression[pos - 1] != ' ') {
                mathExpression.insert(pos, " ");
                pos++;
            }
            if (pos + 1 < mathExpression.size() && mathExpression[pos + 1] != ' ') {
                mathExpression.insert(pos + 1, " ");
            }
            pos += 2; // Move past the operator and space
        }
    }

    bool valid = false;
    const double result = evaluateExpression(mathExpression, valid);

    if (!valid) {
        return NULL_STR;
    }

    // Format the result with StringStream
    StringStream oss;
    if (forceInteger || result == static_cast<double>(static_cast<int>(result))) {
        oss << static_cast<int>(result);  // Integer output if required
    } else {
        // Manually format to two decimal places for double output
        const int intPart = static_cast<int>(result);
        const int decimalPart = static_cast<int>((result - intPart) * 100);  // Get two decimal places

        oss << intPart << ".";

        // Handle cases where decimal part has only one digit (e.g., 3.1 should be 3.10)
        if (decimalPart < 10) {
            oss << "0";
        }
        oss << decimalPart;
    }

    return oss.str();
}


std::string handleLength(const std::string& placeholder) {
    const size_t startPos = placeholder.find('(') + 1;
    const size_t endPos = placeholder.find(')');
    
    if (startPos != std::string::npos && endPos != std::string::npos && endPos > startPos) {
        std::string str = placeholder.substr(startPos, endPos - startPos);
        trim(str);  // Remove any extra spaces
        removeQuotes(str);  // If your strings are wrapped in quotes, remove them
        
        // Return the length of the string as a string
        return ult::to_string(str.length());
    }
    
    return placeholder;  // If invalid, return the original placeholder
}


// Finds the next occurrence of ANY function-style placeholder opener from `starts`,
// beginning at `from`. On tie (same index), it prefers the LONGEST token.
// Returns npos if none found, and sets matchedLen accordingly.
static size_t findNextOpenToken(const std::string& s,
                                size_t from,
                                const std::vector<std::string>& starts,
                                size_t& matchedLen) {

    size_t bestPos = std::string::npos;
    matchedLen = 0;

    for (const auto& tok : starts) {
        const size_t pos = s.find(tok, from);
        if (pos == std::string::npos) continue;

        if (bestPos == std::string::npos || pos < bestPos ||
            (pos == bestPos && tok.size() > matchedLen)) {
            bestPos = pos;
            matchedLen = tok.size();
        }
    }
    return bestPos;
}

// Starting at `startPos` which points to the FIRST character of the opener of the
// CURRENT placeholder (e.g., the '{' in "{slice("), find the matching closing
// pair for that placeholder. We count nested openers of ANY placeholder type
// and match on ")}" closes.
//
// Return value: index of the ')' in the matching closing ")}" (so you can substr
// with +2 to include ")}"). Returns npos if unmatched.
static size_t findMatchingClose(const std::string& s,
                                size_t startPos,
                                const std::vector<std::string>& starts,
                                size_t outerOpenLen) {

    // We have already seen one opener at startPos
    int depth = 1;
    size_t scan = startPos + outerOpenLen;

    size_t openLen;
    bool openerFirst;

    while (scan < s.size()) {
        // Find next opener (any) and next closer ")}"
        const size_t nextCloser = s.find(")}", scan);

        openLen = 0;
        const size_t nextOpener = findNextOpenToken(s, scan, starts, openLen);

        // Decide which token comes first
        if (nextCloser == std::string::npos && nextOpener == std::string::npos) {
            return std::string::npos; // no more tokens
        }

        openerFirst = false;
        if (nextOpener != std::string::npos) {
            if (nextCloser == std::string::npos) openerFirst = true;
            else openerFirst = (nextOpener < nextCloser);
        }

        if (openerFirst) {
            // Found a nested opener
            ++depth;
            scan = nextOpener + openLen; // continue after this opener
        } else {
            // Found a closer
            --depth;
            if (depth == 0) {
                return nextCloser; // return index of ')'
            }
            scan = nextCloser + 2; // continue after ")}"
        }
    }

    return std::string::npos; // unmatched
}


/**
 * @brief Recursively resolves nested placeholders in a string from innermost to outermost.
 *
 * This function processes function-style placeholders (e.g., {math(...)}, {split(...)}, {slice(...)})
 * by recursively resolving their inner content first, then applying the outer transformation.
 * It uses a universal approach that attempts to resolve all placeholders it can, and intelligently
 * skips outer placeholders when inner placeholders cannot be resolved (e.g., source placeholders
 * that require external context from getSourceReplacement).
 *
 * Resolution Process:
 * 1. Finds a placeholder pattern like {function(...)}
 * 2. Extracts and recursively resolves the inner content
 * 3. If inner content couldn't be resolved (unchanged and still contains placeholders),
 *    skips the outer placeholder and leaves it intact for later resolution
 * 4. If inner content was successfully resolved, applies the outer function transformation
 * 5. Continues until no more replacements can be made
 *
 * Examples:
 * - {split({math(5+5)},",",0)} → Resolves math first (10), then split → Final result
 * - {split({list_source(*)},",",0)} → Inner can't resolve → Entire placeholder preserved
 * - {math({ram_vendor}+5)} → ram_vendor already resolved → Processes math → Final result
 *
 * @param arg The string to process, modified in-place with resolved placeholders
 * @param placeholders Vector of placeholder patterns and their resolver functions.
 *                     Each pair contains: opener pattern (e.g., "{math(") and resolver lambda
 * @return true if any replacements were made, false otherwise
 *
 * @note Simple placeholders like {ram_vendor}, {hos_version} should be resolved by
 *       replacePlaceholdersInArg() before calling this function.
 * @note Source placeholders like {list_source(*)}, {file_source} are resolved in
 *       getSourceReplacement() and will be skipped by this function.
 */
static bool replacePlaceholdersRecursivelyImpl(
    std::string& arg,
    const std::vector<std::pair<std::string, std::function<std::string(const std::string&)>>>& placeholders,
    const std::vector<std::string>& starts) {
    
    bool anyReplacementsMade = false;
    bool replacedThisPass;
    size_t searchPos;
    std::string replacement;
    std::string inner;
    std::string resolvedPlaceholder;

    for (;;) {
        replacedThisPass = false;

        for (size_t t = 0; t < placeholders.size(); ++t) {
            const auto& opener = placeholders[t].first;
            const auto& replacer = placeholders[t].second;
            const size_t openerLen = opener.size();

            searchPos = 0;
            while (true) {
                const size_t startPos = arg.find(opener, searchPos);
                if (startPos == std::string::npos) break;

                const size_t closePos = findMatchingClose(arg, startPos, starts, openerLen);
                if (closePos == std::string::npos) {
                    searchPos = startPos + openerLen;
                    continue;
                }

                const size_t innerStart = startPos + openerLen;
                const size_t innerLen   = closePos - innerStart;
                const size_t fullLen    = closePos - startPos + 2;

                bool innerChanged = false;
                
                // Skip recursion entirely if inner is empty or has no placeholders
                if (innerLen > 0) {
                    inner.assign(arg, innerStart, innerLen);
                    if (inner.find('{') != std::string::npos) {
                        innerChanged = replacePlaceholdersRecursivelyImpl(inner, placeholders, starts);
                        
                        // If recursion made no changes and inner still has unresolved placeholders,
                        // skip this outer placeholder
                        if (!innerChanged) {
                            searchPos = startPos + openerLen;
                            continue;
                        }
                    }
                }

                // Build the resolved placeholder
                if (!innerChanged) {
                    resolvedPlaceholder.assign(arg, startPos, fullLen);
                } else {
                    resolvedPlaceholder.clear();
                    resolvedPlaceholder.reserve(openerLen + inner.size() + 2);
                    resolvedPlaceholder += opener;
                    resolvedPlaceholder += inner;
                    resolvedPlaceholder += ")}";
                }

                replacement = replacer(resolvedPlaceholder);

                if (replacement.size() == resolvedPlaceholder.size() && replacement == resolvedPlaceholder) {
                    searchPos = startPos + openerLen;
                    continue;
                }

                arg.replace(startPos, fullLen, replacement);
                anyReplacementsMade = true;
                replacedThisPass = true;
                searchPos = startPos + replacement.size();
            }
        }

        if (!replacedThisPass) break;
    }

    return anyReplacementsMade;
}

bool replacePlaceholdersRecursively(
    std::string& arg,
    const std::vector<std::pair<std::string, std::function<std::string(const std::string&)>>>& placeholders) {
    
    if (arg.find('{') == std::string::npos) return false;

    std::vector<std::string> starts;
    starts.reserve(placeholders.size());
    for (const auto& pr : placeholders) {
        starts.push_back(pr.first);
    }
    return replacePlaceholdersRecursivelyImpl(arg, placeholders, starts);
}


std::unordered_map<std::string, std::string> generalPlaceholders;
void updateGeneralPlaceholders() {
    // {ovl_language}: the currently selected overlay language code
    // ("en", "es", "ja", "zh-cn", ...).  Read fresh from config.ini on every
    // update so a language change is reflected the next time placeholders are
    // resolved (this function runs before each placeholder pass).
    std::string ovlLanguage = parseValueFromIniSection(
        ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME, DEFAULT_LANG_STR);
    if (ovlLanguage.empty()) ovlLanguage = "en";

    generalPlaceholders = {
        {"{ovl_language}", ovlLanguage},
        {"{ram_vendor}", memoryVendor},
        {"{ram_model}", memoryModel},
        {"{ram_size_gb}", is8GBEnabled ? "8" : "4"},
        {"{ams_version}", amsVersion},
        {"{hos_version}", hosVersion},
        {"{hekate_version}", hekateVersion},
        {"{ovlloader_version}", ovlloaderVersion},
        {"{ultrahand_version}", APP_VERSION},
        {"{package_version}", packageRootLayerVersion},
        {"{cpu_speedo}", ult::to_string(cpuSpeedo0)},
        {"{cpu_iddq}", ult::to_string(cpuIDDQ)},
        {"{gpu_speedo}", ult::to_string(cpuSpeedo2)},
        {"{gpu_iddq}", ult::to_string(gpuIDDQ)},
        {"{soc_speedo}", ult::to_string(socSpeedo0)},
        {"{soc_iddq}", ult::to_string(socIDDQ)},
        {"{title_id}", getTitleIdAsString()},
        {"{build_id}", getBuildIdAsString()},
        {"{local_ip}", getLocalIpAddress()},
        {"{volume}", getMasterVolumeLevel()},
        {"{backlight}", getBacklightLevel()}
    };
}

// Extracts the argument string from inside a "name(...)" placeholder token.
// Returns true and sets `out` on success; returns false and sets `out` to
// NULL_STR when either paren is absent.  Used by the lambdas below to
// eliminate the repeated find/bounds-check/substr boilerplate.
// CRC32 (ISO 3309 / ITU-T V.42) — table-driven Sarwate, no extra libs needed.
// The 256-entry table is evaluated at compile time; the loop is branchless.
static std::string crc32File(const std::string& path) {
    struct Table {
        uint32_t v[256];
        constexpr Table() : v{} {
            for (uint32_t i = 0; i < 256; ++i) {
                uint32_t c = i;
                for (int k = 0; k < 8; ++k)
                    c = (c >> 1) ^ (0xEDB88320u & -(c & 1u));
                v[i] = c;
            }
        }
    };
    static constexpr Table T{};

    FILE* f = fopen(path.c_str(), "rb");
    if (!f) return NULL_STR;

    uint32_t crc = 0xFFFFFFFFu;
    uint8_t buf[4096];
    size_t n;
    while ((n = fread(buf, 1, sizeof(buf), f)) > 0) {
        for (size_t i = 0; i < n; ++i)
            crc = (crc >> 8) ^ T.v[(crc ^ buf[i]) & 0xFFu];
    }
    fclose(f);

    crc ^= 0xFFFFFFFFu;
    char hex[9];
    snprintf(hex, sizeof(hex), "%08X", crc);
    return std::string(hex);
}

static bool getPlaceholderContent(const std::string& placeholder, std::string& out) {
    const size_t open = placeholder.find('(');
    if (open == std::string::npos) { out = NULL_STR; return false; }
    const size_t close = placeholder.find(')', open + 1);
    if (close == std::string::npos) { out = NULL_STR; return false; }
    out = placeholder.substr(open + 1, close - open - 1);
    return true;
}

bool applyPlaceholderReplacements(std::vector<std::string>& cmd, const std::string& hexPath, 
                                 const std::string& iniPath, const std::string& listString, 
                                 const std::string& listPath, const std::string& jsonString, 
                                 const std::string& jsonPath, const std::string& packagePath) {
    bool replacementsMade = false;

    // Shared helpers for conditional placeholders.
    //
    // parse2: {if_xxx(VALUE, TRUE_FALLBACK [, FALSE_FALLBACK])}
    //   value      = arg 1  (the subject, typically a resolved placeholder)
    //   trueFb     = arg 2  (returned when condition is TRUE)
    //   falseFb    = arg 3  (optional; returned when condition is FALSE; defaults to value)
    static const auto parse2 = [](const std::string& ph,
                                   std::string& value, std::string& trueFb,
                                   std::string& falseFb, bool& hasFalseFb) -> bool {
        const size_t open  = ph.find('(');
        const size_t close = ph.rfind(')');
        if (open == std::string::npos || close == std::string::npos || close <= open + 1)
            return false;
        const std::string inner = ph.substr(open + 1, close - open - 1);
        const size_t firstComma = inner.find(',');
        if (firstComma == std::string::npos) return false;
        const size_t lastComma  = inner.rfind(',');
        value = inner.substr(0, firstComma);
        removeQuotes(value);
        if (firstComma == lastComma) {
            // 2-arg form: no false fallback
            trueFb = inner.substr(firstComma + 1);
            removeQuotes(trueFb);
            hasFalseFb = false;
        } else {
            // 3-arg form: false fallback present
            trueFb  = inner.substr(firstComma + 1, lastComma - firstComma - 1);
            falseFb = inner.substr(lastComma + 1);
            removeQuotes(trueFb);
            removeQuotes(falseFb);
            hasFalseFb = true;
        }
        return true;
    };
    // parse3: {if_xxx(VALUE, COMPARE_VALUE, TRUE_FALLBACK [, FALSE_FALLBACK])}
    //   value      = arg 1  (the subject)
    //   needle     = arg 2  (what to compare against)
    //   trueFb     = arg 3  (returned when condition is TRUE)
    //   falseFb    = arg 4  (optional; returned when condition is FALSE; defaults to value)
    static const auto parse3 = [](const std::string& ph,
                                   std::string& value, std::string& needle,
                                   std::string& trueFb, std::string& falseFb,
                                   bool& hasFalseFb) -> bool {
        const size_t open  = ph.find('(');
        const size_t close = ph.rfind(')');
        if (open == std::string::npos || close == std::string::npos || close <= open + 1)
            return false;
        const std::string inner = ph.substr(open + 1, close - open - 1);
        const size_t c1 = inner.find(',');         // end of value
        if (c1 == std::string::npos) return false;
        const size_t c2 = inner.find(',', c1 + 1); // end of needle
        if (c2 == std::string::npos) return false;
        const size_t cl = inner.rfind(',');         // start of false_fb (if any)
        value  = inner.substr(0, c1);
        needle = inner.substr(c1 + 1, c2 - c1 - 1);
        removeQuotes(value);
        removeQuotes(needle);
        if (cl == c2) {
            // 3-arg form: no false fallback
            trueFb = inner.substr(c2 + 1);
            removeQuotes(trueFb);
            hasFalseFb = false;
        } else {
            // 4-arg form: false fallback present
            trueFb  = inner.substr(c2 + 1, cl - c2 - 1);
            falseFb = inner.substr(cl + 1);
            removeQuotes(trueFb);
            removeQuotes(falseFb);
            hasFalseFb = true;
        }
        return true;
    };
    // compareVersions: alphanumeric version-aware comparison.
    //   Separators: '.' and '_' are neutral (extra = pre-release, sorts lower).
    //               '-' introduces pre-release labels  -> sorts LOWER than bare release.
    //               '+' introduces build/revision labels -> sorts HIGHER than bare release.
    //   Segments that are both pure digits are compared numerically (so "10" > "9").
    //   Non-digit segments are compared lexicographically.
    //   Returns -1 if a < b, 0 if a == b, +1 if a > b.
    //   Examples: "2.2.4" > "2.0.1",  "1.10.0" > "1.9.0",
    //             "2.0.1-rc" < "2.0.1",  "2.0.1+r23" > "2.0.1",
    //             "2.0.1+r23" > "2.0.1-rc"
    static const auto compareVersions = [](const std::string& a, const std::string& b) -> int {
        static const auto isSep = [](char c) {
            return c == '.' || c == '-' || c == '_' || c == '+';
        };
        // sepTier: '+ '= build metadata (sorts higher), '-'= pre-release (sorts lower),
        //          '.'/'_'/' ' = neutral (extra segments = pre-release = lower)
        static const auto sepTier = [](char c) -> int {
            if (c == '+') return  1;
            if (c == '-') return -1;
            return -1; // '.', '_', or no separator -> pre-release convention
        };
        static const auto isAllDigits = [](const std::string& s, size_t start, size_t end) {
            for (size_t i = start; i < end; ++i)
                if (!std::isdigit(static_cast<unsigned char>(s[i]))) return false;
            return start < end;
        };
        size_t ai = 0, bi = 0;
        const size_t alen = a.size(), blen = b.size();
        char aSep = 0, bSep = 0; // separator that introduced the current segment
        while (true) {
            // Skip to start of next segment, recording which separator we crossed
            while (ai < alen && isSep(a[ai])) { aSep = a[ai]; ++ai; }
            while (bi < blen && isSep(b[bi])) { bSep = b[bi]; ++bi; }
            if (ai >= alen && bi >= blen) break;
            // One side exhausted: the other has extra segments.
            // The separator that introduced those extra segments determines sort order.
            if (ai >= alen) return -sepTier(bSep); // b has extra: tier > 0 means b > a -> -1
            if (bi >= blen) return  sepTier(aSep); // a has extra: tier > 0 means a > b -> +1
            // Both have segments. If they crossed different separator tiers, tier wins.
            if (ai > 0 && bi > 0) { // not the very first segment
                const int ta = sepTier(aSep), tb = sepTier(bSep);
                if (ta != tb) return (ta > tb) ? 1 : -1;
            }
            // Find end of current segment
            size_t ae = ai; while (ae < alen && !isSep(a[ae])) ++ae;
            size_t be = bi; while (be < blen && !isSep(b[be])) ++be;
            if (isAllDigits(a, ai, ae) && isAllDigits(b, bi, be)) {
                const long va = std::strtol(a.c_str() + ai, nullptr, 10);
                const long vb = std::strtol(b.c_str() + bi, nullptr, 10);
                if (va < vb) return -1;
                if (va > vb) return  1;
            } else {
                const int cmp = a.compare(ai, ae - ai, b, bi, be - bi);
                if (cmp < 0) return -1;
                if (cmp > 0) return  1;
            }
            ai = ae;
            bi = be;
        }
        return 0;
    };

    // parseNum: true when the whole token parses as a number (int or float).
    // Leading/trailing spaces are tolerated; inf/nan and non-numeric tokens are
    // rejected up front so they fall through to lexicographic comparison.
    static const auto parseNum = [](const std::string& s, double& out) -> bool {
        const char* c = s.c_str();
        while (*c == ' ' || *c == '\t') ++c;
        const char afterSign = (*c == '+' || *c == '-') ? c[1] : *c;
        if (!((afterSign >= '0' && afterSign <= '9') || afterSign == '.'))
            return false;
        char* end = nullptr;
        out = std::strtod(c, &end);
        if (end == c) return false;
        while (*end == ' ' || *end == '\t') ++end;
        return *end == '\0';
    };
    // compareAlnum: numeric compare when both sides are numbers, else
    // lexicographic. Returns -1 / 0 / +1.
    static const auto compareAlnum = [](const std::string& a, const std::string& b) -> int {
        double na, nb;
        if (parseNum(a, na) && parseNum(b, nb))
            return (na < nb) ? -1 : (na > nb ? 1 : 0);
        const int c = a.compare(b);
        return (c < 0) ? -1 : (c > 0 ? 1 : 0);
    };

    // Shared {if_...} resolvers. Each placeholder entry below is a thin forwarder
    // into one of these, so the parse + fallback boilerplate is emitted once
    // rather than duplicated per operator.
    //   ifNull   : parse2; true-branch when (value == null) matches wantNull
    //   ifEqual  : parse3; true-branch when (value == compare) matches wantEqual
    //   ifCompare: parse3; true-branch when rel(cmp(value, compare)) holds
    static const auto ifNull = [](const std::string& ph, bool wantNull) -> std::string {
        std::string v, tFb, fFb; bool hasFb;
        if (!parse2(ph, v, tFb, fFb, hasFb)) return NULL_STR;
        return ((v == NULL_STR) == wantNull) ? tFb : (hasFb ? fFb : v);
    };
    static const auto ifEqual = [](const std::string& ph, bool wantEqual) -> std::string {
        std::string v, needle, tFb, fFb; bool hasFb;
        if (!parse3(ph, v, needle, tFb, fFb, hasFb)) return NULL_STR;
        return ((v == needle) == wantEqual) ? tFb : (hasFb ? fFb : v);
    };
    static const auto ifCompare = [](const std::string& ph,
                                     int  (*cmp)(const std::string&, const std::string&),
                                     bool (*rel)(int)) -> std::string {
        std::string v, needle, tFb, fFb; bool hasFb;
        if (!parse3(ph, v, needle, tFb, fFb, hasFb)) return NULL_STR;
        return rel(cmp(v, needle)) ? tFb : (hasFb ? fFb : v);
    };
    static const auto relGt = [](int c) { return c >  0; };
    static const auto relLt = [](int c) { return c <  0; };
    static const auto relGe = [](int c) { return c >= 0; };
    static const auto relLe = [](int c) { return c <= 0; };

    std::vector<std::pair<std::string, std::function<std::string(const std::string&)>>> placeholders = {
        {"{hex_file(", [&](const std::string& placeholder) { 
            if (hexPath.empty() || !isFileOrDirectory(hexPath)) return NULL_STR;
            std::string result = replaceHexPlaceholder(placeholder, hexPath);
            return returnOrNull(result);
        }},
        {"{ini_file(", [&](const std::string& placeholder) { 
            if (iniPath.empty() || !isFileOrDirectory(iniPath)) return NULL_STR;
            std::string result = placeholder;
            applyReplaceIniPlaceholder(result, INI_FILE_STR, iniPath); 
            return result;
        }},
        {"{list(", [&](const std::string& placeholder) {
            std::string indexStr;
            if (!getPlaceholderContent(placeholder, indexStr) || !isValidNumber(indexStr))
                return NULL_STR;
            const auto& items = stringToList(listString);
            const size_t idx = ult::stoi(indexStr);
            if (idx >= items.size()) return NULL_STR;
            return returnOrNull(items[idx]);
        }},
        {"{list_file(", [&](const std::string& placeholder) {
            std::string indexStr;
            if (!getPlaceholderContent(placeholder, indexStr) || !isValidNumber(indexStr))
                return NULL_STR;
            if (listPath.empty() || !isFileOrDirectory(listPath)) return NULL_STR;
            return returnOrNull(getEntryFromListFile(listPath, ult::stoi(indexStr)));
        }},
        {"{json(", [&](const std::string& placeholder) { 
            return replaceJsonPlaceholder(placeholder, JSON_STR, jsonString);
        }},
        {"{json_file(", [&](const std::string& placeholder) { 
            if (jsonPath.empty() || !isFileOrDirectory(jsonPath)) return NULL_STR;
            return replaceJsonPlaceholder(placeholder, JSON_FILE_STR, jsonPath);
        }},
        {"{timestamp(", [&](const std::string& placeholder) {
            std::string format;
            if (!getPlaceholderContent(placeholder, format)) return NULL_STR;
            if (format.empty()) format = "%Y-%m-%d %H:%M:%S";
            removeQuotes(format);
            return returnOrNull(getCurrentTimestamp(format));
        }},
        {"{decimal_to_hex(", [&](const std::string& placeholder) {
            std::string params;
            if (!getPlaceholderContent(placeholder, params)) return NULL_STR;

            const size_t commaPos = params.find(",");
            std::string decimalValue, order;
            if (commaPos != std::string::npos) {
                decimalValue = params.substr(0, commaPos);
                order = params.substr(commaPos + 1);
                order.erase(0, order.find_first_not_of(" \t\n\r"));
                order.erase(order.find_last_not_of(" \t\n\r") + 1);
            } else {
                decimalValue = params;
            }

            if (order.empty()) {
                return returnOrNull(decimalToHex(decimalValue));
            } else {
                if (!isValidNumber(order)) return NULL_STR;
                return returnOrNull(decimalToHex(decimalValue, ult::stoi(order)));
            }
        }},
        {"{ascii_to_hex(", [&](const std::string& placeholder) {
            std::string inner;
            if (!getPlaceholderContent(placeholder, inner)) return NULL_STR;
            return returnOrNull(asciiToHex(inner));
        }},
        {"{hex_to_rhex(", [&](const std::string& placeholder) {
            std::string inner;
            if (!getPlaceholderContent(placeholder, inner)) return NULL_STR;
            return returnOrNull(hexToReversedHex(inner));
        }},
        {"{hex_to_decimal(", [&](const std::string& placeholder) {
            std::string inner;
            if (!getPlaceholderContent(placeholder, inner)) return NULL_STR;
            return returnOrNull(hexToDecimal(inner));
        }},
        {"{base64_decode(", [&](const std::string& placeholder) {
            std::string inner;
            if (!getPlaceholderContent(placeholder, inner)) return NULL_STR;
            return returnOrNull(decodeBase64ToString(inner));
        }},
        {"{random(", [&](const std::string& placeholder) {
            std::srand(std::time(0));
            std::string parameters;
            if (!getPlaceholderContent(placeholder, parameters)) return NULL_STR;
            const size_t commaPos = parameters.find(',');
            if (commaPos == std::string::npos) return returnOrNull(placeholder);
            std::string lowStr  = parameters.substr(0, commaPos);
            std::string highStr = parameters.substr(commaPos + 1);
            if (!isValidNumber(lowStr) || !isValidNumber(highStr)) return NULL_STR;
            const int lowValue  = ult::stoi(lowStr);
            const int highValue = ult::stoi(highStr);
            return returnOrNull(ult::to_string(lowValue + rand() % (highValue - lowValue + 1)));
        }},
        {"{slice(", [&](const std::string& placeholder) {
            const size_t startPos = placeholder.find('(');
            const size_t endPos = placeholder.rfind(')');
            if (startPos == std::string::npos || endPos == std::string::npos || endPos <= startPos + 1) {
                return NULL_STR;
            }
        
            const std::string parameters = placeholder.substr(startPos + 1, endPos - startPos - 1);
            const size_t firstComma = parameters.find(',');
            const size_t secondComma = (firstComma == std::string::npos) ? 
                std::string::npos : parameters.find(',', firstComma + 1);
            if (firstComma == std::string::npos || secondComma == std::string::npos) {
                return NULL_STR;
            }
        
            std::string strPart    = parameters.substr(0, firstComma);
            std::string startIndex = parameters.substr(firstComma + 1, secondComma - firstComma - 1);
            std::string endIndex   = parameters.substr(secondComma + 1);
        
            trim(strPart);
            removeQuotes(strPart);
            trim(startIndex);
            removeQuotes(startIndex);
            trim(endIndex);
            removeQuotes(endIndex);
        
            if (startIndex.empty() || endIndex.empty() ||
                !isValidNumber(startIndex) || !isValidNumber(endIndex)) {
                return NULL_STR;
            }
        
            const size_t sliceStart = static_cast<size_t>(ult::stoi(startIndex));
            const size_t sliceEnd   = static_cast<size_t>(ult::stoi(endIndex));
        
            if (sliceEnd <= sliceStart || sliceStart >= strPart.length()) {
                return NULL_STR;
            }
        
            return returnOrNull(sliceString(strPart, sliceStart, sliceEnd));
        }},
        {"{split(", [&](const std::string& placeholder) {
            const size_t openParen = placeholder.find('(');
            const size_t closeParen = placeholder.rfind(')');
        
            if (openParen == std::string::npos || closeParen == std::string::npos || 
                closeParen <= openParen) {
                return NULL_STR;
            }
        
            const std::string parameters = placeholder.substr(openParen + 1, 
                closeParen - openParen - 1);
        
            const size_t firstCommaPos = parameters.find(',');
            const size_t lastCommaPos  = parameters.find_last_of(',');
        
            if (firstCommaPos == std::string::npos || lastCommaPos == std::string::npos ||
                firstCommaPos == lastCommaPos) {
                return NULL_STR;
            }
        
            std::string str = parameters.substr(0, firstCommaPos);
            std::string delimiter = parameters.substr(firstCommaPos + 1, 
                lastCommaPos - firstCommaPos - 1);
            std::string indexStr = parameters.substr(lastCommaPos + 1);
        
            trim(str);
            removeQuotes(str);
            trim(delimiter);
            removeQuotes(delimiter);
            trim(indexStr);
        
            if (indexStr.empty() || !isValidNumber(indexStr)) {
                return NULL_STR;
            }
        
            std::string result = splitStringAtIndex(str, delimiter, ult::stoi(indexStr));
            return result.empty() ? NULL_STR : result;
        }},
        {"{math(", [&](const std::string& placeholder) { return handleMath(placeholder); }},
        {"{length(", [&](const std::string& placeholder) { return handleLength(placeholder); }},
        {"{ovl_version(", [&](const std::string& placeholder) {
            std::string ovlPath;
            if (!getPlaceholderContent(placeholder, ovlPath)) return NULL_STR;
            removeQuotes(ovlPath);
            trim(ovlPath);
            if (ovlPath.empty()) return NULL_STR;
            preprocessPath(ovlPath, packagePath);
            const auto& [result, _ovlName, version, _lib, _ams] = getOverlayInfo(ovlPath);
            return result != ResultSuccess ? NULL_STR : returnOrNull(version);
        }},
        {"{crc32(", [&](const std::string& placeholder) {
            std::string filePath;
            if (!getPlaceholderContent(placeholder, filePath)) return NULL_STR;
            removeQuotes(filePath);
            trim(filePath);
            if (filePath.empty()) return NULL_STR;
            preprocessPath(filePath, packagePath);
            return crc32File(filePath);
        }},
        // Conditional placeholders -- inner args are fully resolved before these fire.
        // The FALSE_FALLBACK is optional in all forms.  When omitted, the false branch
        // returns VALUE unchanged.  When present, it is the last comma-delimited arg.
        //
        // {if_null(VALUE, TRUE_FALLBACK [, FALSE_FALLBACK])}
        // {if_!null(VALUE, TRUE_FALLBACK [, FALSE_FALLBACK])}
        //   "null" is the literal string returned by unresolved/missing placeholders.
        //
        // {if_==(VALUE, COMPARE_VALUE, TRUE_FALLBACK [, FALSE_FALLBACK])}
        // {if_!=(VALUE, COMPARE_VALUE, TRUE_FALLBACK [, FALSE_FALLBACK])}
        //   Exact string equality after removeQuotes on all args.
        //
        // {if_>(VALUE, COMPARE_VALUE, TRUE_FALLBACK [, FALSE_FALLBACK])}
        // {if_<(VALUE, COMPARE_VALUE, TRUE_FALLBACK [, FALSE_FALLBACK])}
        // {if_>=(VALUE, COMPARE_VALUE, TRUE_FALLBACK [, FALSE_FALLBACK])}
        // {if_<=(VALUE, COMPARE_VALUE, TRUE_FALLBACK [, FALSE_FALLBACK])}
        //   When both values parse as numbers they are compared numerically
        //   (as floats, so "9" < "10" and "1.5" > "1.05"); otherwise they are
        //   compared lexicographically, byte by byte and case-sensitive.
        //
        // {if_version_>(VALUE, COMPARE_VALUE, TRUE_FALLBACK [, FALSE_FALLBACK])}
        // {if_version_<(VALUE, COMPARE_VALUE, TRUE_FALLBACK [, FALSE_FALLBACK])}
        // {if_version_>=(VALUE, COMPARE_VALUE, TRUE_FALLBACK [, FALSE_FALLBACK])}
        // {if_version_<=(VALUE, COMPARE_VALUE, TRUE_FALLBACK [, FALSE_FALLBACK])}
        //   Version-aware comparison: numeric segments compared as integers,
        //   '-' suffix = pre-release (lower), '+' suffix = build metadata (higher).
        {"{if_null(",       [](const std::string& ph) { return ifNull(ph, true);  }},
        {"{if_!null(",      [](const std::string& ph) { return ifNull(ph, false); }},
        {"{if_==(",         [](const std::string& ph) { return ifEqual(ph, true);  }},
        {"{if_!=(",         [](const std::string& ph) { return ifEqual(ph, false); }},
        {"{if_>(",          [](const std::string& ph) { return ifCompare(ph, compareAlnum,    relGt); }},
        {"{if_<(",          [](const std::string& ph) { return ifCompare(ph, compareAlnum,    relLt); }},
        {"{if_>=(",         [](const std::string& ph) { return ifCompare(ph, compareAlnum,    relGe); }},
        {"{if_<=(",         [](const std::string& ph) { return ifCompare(ph, compareAlnum,    relLe); }},
        {"{if_version_>(",  [](const std::string& ph) { return ifCompare(ph, compareVersions, relGt); }},
        {"{if_version_<(",  [](const std::string& ph) { return ifCompare(ph, compareVersions, relLt); }},
        {"{if_version_>=(", [](const std::string& ph) { return ifCompare(ph, compareVersions, relGe); }},
        {"{if_version_<=(", [](const std::string& ph) { return ifCompare(ph, compareVersions, relLe); }},
    };

    updateGeneralPlaceholders();

    // Iterate through each command and replace placeholders
    for (auto& arg : cmd) {
        // Replace general placeholders
        if (replacePlaceholdersInArg(arg, generalPlaceholders)) {
            replacementsMade = true;
        }

        // Replace button/arrow placeholders from the global map
        if (replacePlaceholdersInArg(arg, symbolPlaceholders)) {
            replacementsMade = true;
        }

        // Normalize {timestamp} → {timestamp()} so the recursive handler picks it up
        replaceAllPlaceholders(arg, "{timestamp}", "{timestamp()}");

        // Resolve nested placeholders - only if all internals resolve
        if (replacePlaceholdersRecursively(arg, placeholders)) {
            replacementsMade = true;
        }
    }
    
    return replacementsMade;
}



// forward declarations
void processCommand(const std::vector<std::string>& cmd, const std::string& packagePath, const std::string& selectedCommand);
inline bool txtLineExists(const std::string& filePath, const std::string& line);
inline bool hexValMatches(const std::string& filePath, uint32_t offset, std::string expectedHex);
inline bool hexValMatchesCustom(const std::string& filePath, const std::string& customAsciiPattern, const std::string& offsetStr, std::string expectedHex);
inline bool moduleExists(u64 programId);
inline bool moduleIsActive(u64 programId);


/**
 * @brief Apply placeholder replacements to a list of commands and handle control flow commands.
 *
 * This function iterates over a list of commands, where each command is a vector of strings,
 * and applies the `applyPlaceholderReplacements` function to handle the placeholders.
 * It also handles control flow commands like "erista:" and "mariko:".
 * Optimized for minimal memory usage with in-place compaction.
 *
 * @param commands A list of commands, where each command is represented as a vector of strings.
 * @param packagePath The path to the package (optional).
 * @param selectedCommand A specific command to execute (optional).
 * @return Returns true if the placeholders were successfully applied to the commands, false otherwise.
 */
bool applyPlaceholderReplacementsToCommands(std::vector<std::vector<std::string>>& commands, 
                                           const std::string& packagePath = "", 
                                           const std::string& selectedCommand = "") {
    
    std::string listString, listPath, jsonString, jsonPath, hexPath, iniPath;
    bool inEristaSection = false;
    bool inMarikoSection = false;
    
    // Two-pointer technique: readIndex reads all commands, writeIndex writes kept commands
    size_t writeIndex = 0;
    
    for (size_t readIndex = 0; readIndex < commands.size(); ++readIndex) {
        auto& cmd = commands[readIndex];
        
        if (cmd.empty()) {
            // Skip empty commands, don't increment writeIndex
            cmd.shrink_to_fit();
            continue;
        }

        const std::string_view commandName = cmd[0];

        // Handle control flow commands
        if (commandName == "erista:") {
            inEristaSection = true;
            inMarikoSection = false;
            cmd = {};
            continue; // Don't keep this command
        } else if (commandName == "mariko:") {
            inEristaSection = false;
            inMarikoSection = true;
            cmd = {};
            continue; // Don't keep this command
        } else if ((!commandName.empty() && commandName.front() == ';') ||
                   (commandName.size() >= 7 && commandName.substr(commandName.size() - 7) == "_source") ||
                   commandName == "logging") {
            cmd = {};
            continue; // Don't keep this command
        }

        // Skip commands not relevant to current hardware
        if ((inEristaSection && !usingErista) || (inMarikoSection && !usingMariko)) {
            cmd = {};
            continue; // Don't keep this command
        }

        // Apply placeholder replacements
        applyPlaceholderReplacements(cmd, hexPath, iniPath, listString, listPath, jsonString, jsonPath, packagePath);

        // Handle special commands
        const size_t cmdSize = cmd.size();
        bool shouldKeep = true;

        if (commandName == LIST_STR && cmdSize >= 2) {
            listString = cmd[1];
            removeQuotes(listString);
            shouldKeep = false;
        } else if (commandName == LIST_FILE_STR && cmdSize >= 2) {
            listPath = cmd[1];
            preprocessPath(listPath, packagePath);
            shouldKeep = false;
        } else if (commandName == JSON_STR && cmdSize >= 2) {
            jsonString = cmd[1];
            removeQuotes(jsonString);
            shouldKeep = false;
        } else if (commandName == JSON_FILE_STR && cmdSize >= 2) {
            jsonPath = cmd[1];
            preprocessPath(jsonPath, packagePath);
            shouldKeep = false;
        } else if (commandName == INI_FILE_STR && cmdSize >= 2) {
            iniPath = cmd[1];
            preprocessPath(iniPath, packagePath);
            shouldKeep = false;
        } else if (commandName == HEX_FILE_STR && cmdSize >= 2) {
            hexPath = cmd[1];
            preprocessPath(hexPath, packagePath);
            shouldKeep = false;
        } else if (commandName == "filter" || commandName == "file_name" || 
                   commandName == "folder_name" || commandName == "sourced_path") {
            shouldKeep = false;
        }

        if (!shouldKeep) {
            cmd = {};
            continue; // Don't keep this command
        }

        // Apply quoting logic to arguments for commands we're keeping
        for (size_t i = 1; i < cmd.size(); ++i) {
            std::string& argument = cmd[i];

            if (argument.empty()) {
                argument = "''";
                continue;
            }

            if (argument.size() >= 2 &&
                ((argument.front() == '"' && argument.back() == '"') ||
                 (argument.front() == '\'' && argument.back() == '\''))) {
                continue;
            }

            if (argument.find(' ') == std::string::npos) {
                continue;
            }

            // Optimized quoting - fewer string operations
            const bool hasSingleQuote = argument.find('\'') != std::string::npos;
            const bool hasDoubleQuote = argument.find('"') != std::string::npos;
            
            if (hasSingleQuote && !hasDoubleQuote) {
                argument.insert(0, 1, '"');
                argument.push_back('"');
            } else {
                argument.insert(0, 1, '\'');
                argument.push_back('\'');
            }
        }

        // Keep this command - move it to the write position
        if (writeIndex != readIndex) {
            commands[writeIndex] = std::move(cmd);
            cmd.shrink_to_fit();
        }
        ++writeIndex;
    }
    
    // Resize to keep only the commands we want
    commands.resize(writeIndex);
    commands.shrink_to_fit();  // Always - maximize memory efficiency

    return true;
}



/**
 * @brief Interpret and execute a list of commands.
 *
 * This function interprets and executes a list of commands based on their names and arguments.
 * Optimized for minimal memory usage by clearing processed commands immediately.
 *
 * @param commands A list of commands, where each command is represented as a vector of strings.
 */
bool interpretAndExecuteCommands(std::vector<std::vector<std::string>>&& commands, 
                                const std::string& packagePath = "", 
                                const std::string& selectedCommand = "") {
    
    #if USING_LOGGING_DIRECTIVE
    if (!packagePath.empty()) {
        disableLogging = !(parseValueFromIniSection(PACKAGES_INI_FILEPATH, getNameFromPath(packagePath), USE_LOGGING_STR) == TRUE_STR);
        logFilePath = packagePath + "log.txt";
    }
    #endif

    // Load and apply buffer configuration from INI file
    const auto bufferSection = getKeyValuePairsFromSection(ULTRAHAND_CONFIG_INI_PATH, MEMORY_STR);
    
    if (!bufferSection.empty()) {
        struct BufferConfig {
            const char* key;
            size_t* target;
        };
        
        const BufferConfig configs[] = {
            {"copy_buffer_size", &COPY_BUFFER_SIZE},
            {"unzip_read_buffer", &UNZIP_READ_BUFFER},
            {"unzip_write_buffer", &UNZIP_WRITE_BUFFER},
            {"download_read_buffer", &DOWNLOAD_READ_BUFFER},
            {"download_write_buffer", &DOWNLOAD_WRITE_BUFFER},
            {"hex_buffer_size", &HEX_BUFFER_SIZE}
        };
        
        for (const auto& config : configs) {
            const auto it = bufferSection.find(config.key);
            if (it != bufferSection.end()) {
                *config.target = ult::stoi(it->second);
            }
        }
    }

    // Initialize state variables
    bool inEristaSection = false;
    bool inMarikoSection = false;
    bool inTrySection = false;
    
    // String buffers for command processing
    std::string listString, listPath, jsonString, jsonPath, hexPath, iniPath;
    
    #if USING_LOGGING_DIRECTIVE
    std::string messageBuffer;
    #endif

    // Reset global state
    commandSuccess.store(true, std::memory_order_release);
    refreshPage.store(false, std::memory_order_release);
    refreshPackage.store(false, std::memory_order_release);
    interpreterLogging.store(false, std::memory_order_release);

    // Process commands one by one, clearing each after processing
    for (size_t i = 0; i < commands.size(); ++i) {
        // Check for abort signal
        if (abortCommand.exchange(false, std::memory_order_acq_rel)) {
            commandSuccess.store(false, std::memory_order_release);
            commands = {};
            #if USING_LOGGING_DIRECTIVE
            disableLogging = true;
            logFilePath = defaultLogFilePath;
            #endif
            return commandSuccess;
        }

        auto& cmd = commands[i];
        
        // Skip empty commands
        if (cmd.empty()) {
            continue;
        }

        // Use string_view to avoid copying command name
        const std::string_view commandName = cmd[0];

        // Handle control flow commands
        if (commandName == "try:") {
            if (inTrySection && commandSuccess.load(std::memory_order_acquire)) {
                commands = {};
                #if USING_LOGGING_DIRECTIVE
                disableLogging = true;
                logFilePath = defaultLogFilePath;
                #endif
                return true;
            }
            commandSuccess.store(true, std::memory_order_release);
            inTrySection = true;
            // Clear and continue
            cmd = {};
            continue;
        }
        
        if (commandName == "erista:") {
            inEristaSection = true;
            inMarikoSection = false;
            // Clear and continue
            cmd = {};
            continue;
        }
        
        if (commandName == "mariko:") {
            inEristaSection = false;
            inMarikoSection = true;
            // Clear and continue
            cmd = {};
            continue;
        }

        // Skip commands in try section if previous command failed
        if (!commandSuccess.load(std::memory_order_acquire) && inTrySection) {
            cmd = {};
            continue;
        }

        // Determine if command should execute based on platform sections
        const bool shouldExecute = 
            (inEristaSection && !inMarikoSection && usingErista) ||
            (!inEristaSection && inMarikoSection && usingMariko) ||
            (!inEristaSection && !inMarikoSection);

        if (!shouldExecute) {
            cmd = {};
            continue;
        }

        // Only execute if not in try section or if we're succeeding in try section
        if (inTrySection && !commandSuccess.load(std::memory_order_acquire)) {
            cmd = {};
            continue;
        }

        // Apply placeholder replacements only if needed
        bool hasPlaceholders = false;
        for (const auto& arg : cmd) {
            if (arg.find('{') != std::string::npos) {
                hasPlaceholders = true;
                break;
            }
        }
        
        if (hasPlaceholders) {
            applyPlaceholderReplacements(cmd, hexPath, iniPath, listString, listPath, jsonString, jsonPath, packagePath);
        }

        #if USING_LOGGING_DIRECTIVE
        if (interpreterLogging.load(std::memory_order_acquire)) {
            disableLogging = false;
        }

        if (!disableLogging) {
            // Build log message efficiently
            messageBuffer.clear();
            messageBuffer += "Executing command: ";
            for (const auto& token : cmd) {
                messageBuffer += token;
                messageBuffer += ' ';
            }
            
            logMessage(messageBuffer);
        }
        #endif

        const size_t cmdSize = cmd.size();

        // Process different command types with direct assignment to reuse string buffers
        if (commandName == LIST_STR) {
            if (cmdSize >= 2) {
                listString = cmd[1]; // Reuses existing string buffer
                removeQuotes(listString);
            }
        } 
        else if (commandName == LIST_FILE_STR) {
            if (cmdSize >= 2) {
                listPath = cmd[1];
                preprocessPath(listPath, packagePath);
            }
        } 
        else if (commandName == JSON_STR) {
            if (cmdSize >= 2) {
                jsonString = cmd[1];
                removeQuotes(jsonString);  // ADD THIS LINE
            }
        } 
        else if (commandName == JSON_FILE_STR) {
            if (cmdSize >= 2) {
                jsonPath = cmd[1];
                preprocessPath(jsonPath, packagePath);
            }
        } 
        else if (commandName == INI_FILE_STR) {
            if (cmdSize >= 2) {
                iniPath = cmd[1];
                preprocessPath(iniPath, packagePath);
            }
        } 
        else if (commandName == HEX_FILE_STR) {
            if (cmdSize >= 2) {
                hexPath = cmd[1];
                preprocessPath(hexPath, packagePath);
            }
        } 
        else {
            // Process all other commands
            processCommand(cmd, packagePath, selectedCommand);
        }
        
        // Clear the processed command immediately to free its memory
        cmd = {};
    }

    // Final cleanup
    commands = {};

    #if USING_LOGGING_DIRECTIVE
    disableLogging = true;
    logFilePath = defaultLogFilePath;
    #endif

    return commandSuccess.load(std::memory_order_acquire);
}


// Helper function to parse command arguments
void parseCommandArguments(const std::vector<std::string>& cmd, const std::string& packagePath, std::string& sourceListPath, std::string& destinationListPath, std::string& logSource, std::string& logDestination, std::string& sourcePath, std::string& destinationPath, std::string& copyFilterListPath, std::string& filterListPath) {
    for (size_t i = 1; i < cmd.size(); ++i) {
        if (cmd[i] == "-src" && i + 1 < cmd.size()) {
            sourceListPath = cmd[++i];
            preprocessPath(sourceListPath, packagePath);
        } else if (cmd[i] == "-dest" && i + 1 < cmd.size()) {
            destinationListPath = cmd[++i];
            preprocessPath(destinationListPath, packagePath);
        } else if (cmd[i] == "-log_src" && i + 1 < cmd.size()) {
            logSource = cmd[++i];
            preprocessPath(logSource, packagePath);
        } else if (cmd[i] == "-log_dest" && i + 1 < cmd.size()) {
            logDestination = cmd[++i];
            preprocessPath(logDestination, packagePath);
        } else if ((cmd[i] == "-copy_filter" || cmd[i] == "-cp_filter") && i + 1 < cmd.size()) {
            copyFilterListPath = cmd[++i];
            preprocessPath(copyFilterListPath, packagePath);
        } else if (cmd[i] == "-filter" && i + 1 < cmd.size()) {
            filterListPath = cmd[++i];
            preprocessPath(filterListPath, packagePath);
        } else if (sourcePath.empty()) {
            sourcePath = cmd[i];
            preprocessPath(sourcePath, packagePath);
        } else if (destinationPath.empty()) {
            destinationPath = cmd[i];
            preprocessPath(destinationPath, packagePath);
        }
    }
}


void handleMakeDirCommand(const std::vector<std::string>& cmd, const std::string& packagePath) {
    if (cmd.size() >= 2) {
        std::string sourcePath = cmd[1];
        preprocessPath(sourcePath, packagePath);
        createDirectory(sourcePath);
    }
}

// If destPath exactly matches a PROTECTED_FILES entry, redirects it to destPath + ".ultra"
// BEFORE the operation so the live file is never touched.
// For directory destinations (unzip, dir copy), renames any protected file that landed
// at its exact path after the operation.
static inline void redirectIfProtected(std::string& destPath) {
    for (const std::string& file : PROTECTED_FILES) {
        if (destPath == file) {
            destPath += ".ultra";
            return;
        }
    }
}

static inline void stageProtectedFromDir(const std::string& destPath) {
    if (destPath.empty() || destPath.back() != '/') return;
    for (const std::string& file : PROTECTED_FILES) {
        if (file.compare(0, destPath.size(), destPath) == 0 && isFile(file)) {
            rename(file.c_str(), (file + ".ultra").c_str());
        }
    }
}

void handleCopyCommand(const std::vector<std::string>& cmd, const std::string& packagePath) {
    // Declare only the strings we always need
    std::string sourceListPath, destinationListPath, logSource, logDestination, sourcePath, destinationPath, copyFilterListPath, filterListPath;
    parseCommandArguments(cmd, packagePath, sourceListPath, destinationListPath, logSource, logDestination, sourcePath, destinationPath, copyFilterListPath, filterListPath);
    
    if (!sourceListPath.empty() && !destinationListPath.empty()) {
        // Process list-based copying
        auto sourceFilesList = readListFromFile(sourceListPath);
        auto destinationFilesList = readListFromFile(destinationListPath);
        
        // Only create filterSet if filter file exists
        std::unique_ptr<std::unordered_set<std::string>> filterSet;
        if (!filterListPath.empty()) {
            filterSet = std::make_unique<std::unordered_set<std::string>>(readSetFromFile(filterListPath, packagePath));
        }
        
        const size_t listSize = std::min(sourceFilesList.size(), destinationFilesList.size());
        for (size_t i = 0; i < listSize; ++i) {
            // Reuse existing sourcePath and destinationPath strings
            sourcePath = std::move(sourceFilesList[i]);
            sourceFilesList[i].shrink_to_fit();     // Free the capacity
            preprocessPath(sourcePath, packagePath);
            
            destinationPath = std::move(destinationFilesList[i]);
            destinationFilesList[i].shrink_to_fit(); // Free the capacity
            preprocessPath(destinationPath, packagePath);
            
            // Only check filter if it exists
            const bool shouldCopy = !filterSet || filterSet->find(sourcePath) == filterSet->end();
            
            if (shouldCopy) {
                redirectIfProtected(destinationPath);
                const long long totalSize = getTotalSize(sourcePath);
                long long totalBytesCopied = 0;
                copyFileOrDirectory(sourcePath, destinationPath, &totalBytesCopied, totalSize);
            }
        }
        
    } else {
        // Single file/directory copying - early returns to avoid unnecessary work
        if (sourcePath.empty() || destinationPath.empty()) {
            #if USING_LOGGING_DIRECTIVE
            if (!disableLogging)
                logMessage("Source and destination paths must be specified.");
            #endif
            return;
        }
        
        if (!isFileOrDirectory(sourcePath)) {
            #if USING_LOGGING_DIRECTIVE
            if (!disableLogging)
                logMessage("Source file or directory doesn't exist: " + sourcePath);
            #endif
            return;
        }
        
        if (sourcePath.find('*') != std::string::npos) {
            std::unique_ptr<std::unordered_set<std::string>> filterSet;
            if (!filterListPath.empty()) {
                filterSet = std::make_unique<std::unordered_set<std::string>>(readSetFromFile(filterListPath, packagePath));
            }
            copyFileOrDirectoryByPattern(sourcePath, destinationPath, logSource, logDestination, filterSet.get());
            stageProtectedFromDir(destinationPath);
        } else {
            redirectIfProtected(destinationPath);
            const long long totalSize = getTotalSize(sourcePath);
            long long totalBytesCopied = 0;
            copyFileOrDirectory(sourcePath, destinationPath, &totalBytesCopied, totalSize, logSource, logDestination);
        }
    }
}

void handleDeleteCommand(const std::vector<std::string>& cmd, const std::string& packagePath) {
    // Declare only the strings we need
    std::string sourceListPath, destinationListPath, logSource, logDestination, sourcePath, destinationPath, copyFilterListPath, filterListPath;
    parseCommandArguments(cmd, packagePath, sourceListPath, destinationListPath, logSource, logDestination, sourcePath, destinationPath, copyFilterListPath, filterListPath);
    
    if (!sourceListPath.empty()) {
        // Process list-based deletion
        auto sourceFilesList = readListFromFile(sourceListPath);
        
        // Only create filterSet if filter file exists
        std::unique_ptr<std::unordered_set<std::string>> filterSet;
        if (!filterListPath.empty()) {
            filterSet = std::make_unique<std::unordered_set<std::string>>(readSetFromFile(filterListPath, packagePath));
        }
        
        for (size_t i = 0; i < sourceFilesList.size(); ++i) {
            // Move string to avoid copy
            sourcePath = std::move(sourceFilesList[i]);
            sourceFilesList[i].shrink_to_fit();     // Free the capacity
            preprocessPath(sourcePath, packagePath);
            
            // Only check filter if it exists
            const bool shouldDelete = !filterSet || filterSet->find(sourcePath) == filterSet->end();
            
            if (shouldDelete) {
                deleteFileOrDirectory(sourcePath, logSource);
            }
        }
        
    } else {
        // Single file/directory deletion - early returns for error conditions
        if (sourcePath.empty()) {
            #if USING_LOGGING_DIRECTIVE
            if (!disableLogging)
                logMessage("Source path must be specified.");
            #endif
            return;
        }
        
        if (isDangerousCombination(sourcePath)) {
            #if USING_LOGGING_DIRECTIVE
            if (!disableLogging)
                logMessage("Dangerous combination detected.");
            #endif
            return;
        }
        
        // Perform the delete operation
        if (sourcePath.find('*') != std::string::npos) {
            // Load filterSet for single path operations
            std::unique_ptr<std::unordered_set<std::string>> filterSet;
            if (!filterListPath.empty()) {
                filterSet = std::make_unique<std::unordered_set<std::string>>(readSetFromFile(filterListPath, packagePath));
            }
            deleteFileOrDirectoryByPattern(sourcePath, logSource, filterSet.get());
        } else {
            deleteFileOrDirectory(sourcePath, logSource);
        }
    }
}


void handleMirrorCommand(const std::vector<std::string>& cmd, const std::string& packagePath) {
    // Early validation
    if (cmd.size() < 2) {
        #if USING_LOGGING_DIRECTIVE
        if (!disableLogging)
            logMessage("Mirror command requires at least a source path.");
        #endif
        return;
    }
    
    // Extract and preprocess source path
    std::string sourcePath = cmd[1];
    preprocessPath(sourcePath, packagePath);
    
    // Extract destination path or use default
    std::string destinationPath;
    if (cmd.size() >= 3) {
        destinationPath = cmd[2];
        preprocessPath(destinationPath, packagePath);
    } else {
        destinationPath = ROOT_PATH;
    }
    
    // Determine operation type using string_view to avoid string creation
    const std::string_view commandName = cmd[0];
    const std::string operation =
        (commandName == "mirror-copy" || commandName == "mirror-cp" ||
         commandName == "mirror_copy" || commandName == "mirror_cp") ? "copy" : "delete";
    
    if (sourcePath.find('*') == std::string::npos) {
        // Single directory mirror
        mirrorFiles(sourcePath, destinationPath, operation);
    } else {
        // Wildcard mirror - get file list and process with immediate cleanup
        auto fileList = getFilesListByWildcards(sourcePath);
        
        // Process files one by one, freeing memory as we go
        for (size_t i = 0; i < fileList.size(); ++i) {
            // Move the string to avoid copy
            const auto sourceDirectory = std::move(fileList[i]);
            fileList[i].shrink_to_fit();     // Free the capacity
            mirrorFiles(sourceDirectory, destinationPath, operation);
        }
    }
}

void handleMoveCommand(const std::vector<std::string>& cmd, const std::string& packagePath) {
    // Declare only the strings we need
    std::string sourceListPath, destinationListPath, logSource, logDestination, sourcePath, destinationPath, copyFilterListPath, filterListPath;
    parseCommandArguments(cmd, packagePath, sourceListPath, destinationListPath, logSource, logDestination, sourcePath, destinationPath, copyFilterListPath, filterListPath);
    
    if (!sourceListPath.empty() && !destinationListPath.empty()) {
        // Load filter sets (these are typically small)
        std::unique_ptr<std::unordered_set<std::string>> copyFilterSet;
        if (!copyFilterListPath.empty()) {
            copyFilterSet = std::make_unique<std::unordered_set<std::string>>(readSetFromFile(copyFilterListPath, packagePath));
        }
        
        std::unique_ptr<std::unordered_set<std::string>> filterSet;
        if (!filterListPath.empty()) {
            filterSet = std::make_unique<std::unordered_set<std::string>>(readSetFromFile(filterListPath, packagePath));
        }

        // Stream process both files line by line
        FILE* sourceFile = fopen(sourceListPath.c_str(), "r");
        FILE* destFile = fopen(destinationListPath.c_str(), "r");
        
        if (!sourceFile || !destFile) {
            if (sourceFile) fclose(sourceFile);
            if (destFile) fclose(destFile);
            #if USING_LOGGING_DIRECTIVE
            if (!disableLogging)
                logMessage("Failed to open source or destination list files");
            #endif
            return;
        }
        
        // Set larger buffers for better I/O performance
        static constexpr size_t FILE_BUFFER_SIZE = 8192;
        setvbuf(sourceFile, nullptr, _IOFBF, FILE_BUFFER_SIZE);
        setvbuf(destFile, nullptr, _IOFBF, FILE_BUFFER_SIZE);
        
        static constexpr size_t BUFFER_SIZE = 8192;
        char sourceBuffer[BUFFER_SIZE];
        char destBuffer[BUFFER_SIZE];
        
        // Static string for repeated log messages to reduce allocations
        #if USING_LOGGING_DIRECTIVE
        static const std::string skipDirMsg = "Skipping non-empty directory: ";
        #endif
        
        // Process files line by line simultaneously
        while (fgets(sourceBuffer, BUFFER_SIZE, sourceFile) && 
               fgets(destBuffer, BUFFER_SIZE, destFile)) {
            
            // Optimized newline removal - scan once instead of using strlen
            char* srcEnd = sourceBuffer;
            while (*srcEnd && *srcEnd != '\n') ++srcEnd;
            const size_t sourceLen = srcEnd - sourceBuffer;
            *srcEnd = '\0';
            
            char* destEnd = destBuffer;
            while (*destEnd && *destEnd != '\n') ++destEnd;
            const size_t destLen = destEnd - destBuffer;
            *destEnd = '\0';
            
            // Clear and append to reuse existing string capacity
            sourcePath.clear();
            sourcePath.append(sourceBuffer, sourceLen);
            
            destinationPath.clear();
            destinationPath.append(destBuffer, destLen);
            
            preprocessPath(sourcePath, packagePath);
            preprocessPath(destinationPath, packagePath);
            
            // Cache filter lookup result
            const bool shouldProcess = !filterSet || filterSet->find(sourcePath) == filterSet->end();
            
            if (shouldProcess) {
                // Check if it's a directory using the buffer directly (avoid string access)
                const bool isDirectory = sourceLen > 0 && sourcePath[sourceLen - 1] == '/';
                
                if (!isDirectory) {
                    // Check copy filter once and cache result
                    const bool shouldCopy = copyFilterSet && copyFilterSet->find(sourcePath) != copyFilterSet->end();
                    
                    if (shouldCopy) {
                        redirectIfProtected(destinationPath);
                        const long long totalSize = getTotalSize(sourcePath);
                        long long totalBytesCopied = 0;
                        copyFileOrDirectory(sourcePath, destinationPath, &totalBytesCopied, totalSize);
                    } else {
                        redirectIfProtected(destinationPath);
                        moveFileOrDirectory(sourcePath, destinationPath, logSource, logDestination);
                    }
                } else {
                    if (isDirectoryEmpty(sourcePath)) {
                        moveFileOrDirectory(sourcePath, destinationPath, logSource, logDestination);
                    }
                    #if USING_LOGGING_DIRECTIVE
                    else if (!disableLogging) {
                        logMessage(skipDirMsg + sourcePath);
                    }
                    #endif
                }
            }
        }
        
        fclose(sourceFile);
        fclose(destFile);
        
    } else {
        // Single file/directory moving - early returns for error conditions
        if (sourcePath.empty() || destinationPath.empty()) {
            #if USING_LOGGING_DIRECTIVE
            if (!disableLogging)
                logMessage("Source and destination paths must be specified.");
            #endif
            return;
        }
        
        if (isDangerousCombination(sourcePath)) {
            #if USING_LOGGING_DIRECTIVE
            if (!disableLogging)
                logMessage("Dangerous combination detected.");
            #endif
            return;
        }
        
        // Perform the move operation
        if (sourcePath.find('*') != std::string::npos) {
            std::unique_ptr<std::unordered_set<std::string>> filterSet;
            if (!filterListPath.empty()) {
                filterSet = std::make_unique<std::unordered_set<std::string>>(readSetFromFile(filterListPath, packagePath));
            }
            moveFilesOrDirectoriesByPattern(sourcePath, destinationPath, logSource, logDestination, filterSet.get());
            stageProtectedFromDir(destinationPath);
        } else {
            redirectIfProtected(destinationPath);
            moveFileOrDirectory(sourcePath, destinationPath, logSource, logDestination);
        }
    }
}

void handleIniCommands(const std::vector<std::string>& cmd, const std::string& packagePath) {
    const std::string& command = cmd[0];
    const size_t cmdSize = cmd.size();
    
    // All commands need at least sourcePath and section
    if (cmdSize < 3)
        return;
    
    std::string sourcePath = cmd[1];
    preprocessPath(sourcePath, packagePath);
    std::string desiredSection = cmd[2];
    removeQuotes(desiredSection);
    
    if (command == "add-ini-section") {
        addIniSection(sourcePath, desiredSection);
        
    } else if (command == "rename-ini-section" && cmdSize >= 4) {
        std::string desiredNewSection = cmd[3];
        removeQuotes(desiredNewSection);
        renameIniSection(sourcePath, desiredSection, desiredNewSection);
        
    } else if (command == "remove-ini-section") {
        removeIniSection(sourcePath, desiredSection);
        
    } else if (command == "remove-ini-key" && cmdSize >= 4) {
        std::string desiredKey = cmd[3];
        removeQuotes(desiredKey);
        removeIniKey(sourcePath, desiredSection, desiredKey);
        
    } else if ((command == "set-ini-val" || command == "set-ini-value") && cmdSize >= 5) {
        std::string desiredKey = cmd[3];
        removeQuotes(desiredKey);
        
        std::string desiredValue = cmd[4];
        removeQuotes(desiredValue);
        
        setIniFileValue(sourcePath, desiredSection, desiredKey, desiredValue);
        
    } else if (command == "set-ini-key" && cmdSize >= 5) {
        std::string desiredKey = cmd[3];
        removeQuotes(desiredKey);
        
        std::string desiredNewKey = cmd[4];
        removeQuotes(desiredNewKey);
        
        setIniFileKey(sourcePath, desiredSection, desiredKey, desiredNewKey);
    } else if (command == "set-ini-val-matching-key" && cmdSize >= 5) {
        // set-ini-val-matching-key <file> <patternKey> <newKey> <newValue>
        // desiredSection is used as patternKey here (empty string = all sections)
        std::string desiredKey = cmd[3];
        removeQuotes(desiredKey);
        
        std::string desiredValue = cmd[4];
        removeQuotes(desiredValue);
        
        if (!ult::isFile(sourcePath))
            setCommandFailed();

        // desiredSection here is the pattern key
        addKeyToMatchingSections(sourcePath, desiredSection, desiredKey, desiredValue);
        
    } else if (command == "remove-ini-key-matching-key" && cmdSize >= 4) {
        // remove-ini-key-matching-key <file> <patternKey> <keyToRemove>
        // desiredSection is used as patternKey here (empty string = all sections)
        std::string desiredKey = cmd[3];
        removeQuotes(desiredKey);
        
        if (!ult::isFile(sourcePath))
            setCommandFailed();

        // desiredSection here is the pattern key
        removeKeyFromMatchingSections(sourcePath, desiredSection, desiredKey);
    }
}

void handleJsonCommands(const std::vector<std::string>& cmd, const std::string& packagePath) {
    const std::string& command = cmd[0];
    const size_t cmdSize = cmd.size();
    
    if (cmdSize < 4)
        return;
    
    std::string sourcePath = cmd[1];
    preprocessPath(sourcePath, packagePath);
    
    std::string key = cmd[2];
    removeQuotes(key);
    
    std::string value;
    for (size_t i = 3; i < cmdSize; ++i) {
        if (i > 3)
            value += ' ';
        value += cmd[i];
    }
    removeQuotes(value);
    
    if (command == "set-json-key") {
        ult::renameJsonKey(sourcePath, key, value);
    } else if (command == "set-json-val" || command == "set-json-value") {
        ult::setJsonValue(sourcePath, key, value, true);
    }
}

void handleHexEdit(const std::string& sourcePath, const std::string& secondArg, const std::string& thirdArg, const std::string& fourthArg, const std::string& fifthArg, const std::string& commandName, const std::vector<std::string>& cmd) {
    
    if (commandName == "hex-by-offset") {
        hexEditByOffset(sourcePath, secondArg, thirdArg);
        return;
    }
    
    std::string hexDataToReplace;
    std::string hexDataReplacement;
    size_t occurrenceArgIndex = 0;
    
    if (commandName == "hex-by-swap") {
        hexDataToReplace = secondArg;
        hexDataReplacement = thirdArg;
        occurrenceArgIndex = 4;
        
    } else if (commandName == "hex-by-string") {
        hexDataToReplace = asciiToHex(secondArg);
        hexDataReplacement = asciiToHex(thirdArg);
        
        // Pad to same length
        const size_t lenDiff = (hexDataToReplace.length() > hexDataReplacement.length()) 
            ? hexDataToReplace.length() - hexDataReplacement.length()
            : hexDataReplacement.length() - hexDataToReplace.length();
        
        if (hexDataReplacement.length() < hexDataToReplace.length()) {
            hexDataReplacement.append(lenDiff, '\0');
        } else if (hexDataToReplace.length() < hexDataReplacement.length()) {
            hexDataToReplace.append(lenDiff, '\0');
        }
        occurrenceArgIndex = 4;
        
    } else if (commandName == "hex-by-decimal" || commandName == "hex-by-rdecimal") {
        const bool isReversed = (commandName == "hex-by-rdecimal");
        if (fourthArg.empty()) {
            hexDataToReplace   = isReversed ? decimalToReversedHex(secondArg) : decimalToHex(secondArg);
            hexDataReplacement = isReversed ? decimalToReversedHex(thirdArg)  : decimalToHex(thirdArg);
        } else {
            if (!isValidNumber(fourthArg))
                return;
            const size_t byteSize = ult::stoi(fourthArg);
            hexDataToReplace   = isReversed ? decimalToReversedHex(secondArg, byteSize) : decimalToHex(secondArg, byteSize);
            hexDataReplacement = isReversed ? decimalToReversedHex(thirdArg,  byteSize) : decimalToHex(thirdArg,  byteSize);
        }
        occurrenceArgIndex = 5;
        
    } else {
        return;
    }
    
    // Handle occurrence parameter if present
    if (cmd.size() >= occurrenceArgIndex + 1) {
        std::string occurrenceStr = cmd[occurrenceArgIndex];
        removeQuotes(occurrenceStr);
        if (!isValidNumber(occurrenceStr))
            return;
        const size_t occurrence = ult::stoi(occurrenceStr);
        hexEditFindReplace(sourcePath, hexDataToReplace, hexDataReplacement, occurrence);
    } else {
        hexEditFindReplace(sourcePath, hexDataToReplace, hexDataReplacement);
    }
}

void handleHexByCustom(const std::string& sourcePath, const std::string& customPattern, const std::string& offset, std::string hexDataReplacement, const std::string& commandName, std::string byteGroupSize) {
    if (hexDataReplacement != NULL_STR) {
        if (commandName == "hex-by-custom-decimal-offset" || commandName == "hex-by-custom-rdecimal-offset") {
            const bool isReversed = (commandName == "hex-by-custom-rdecimal-offset");
            if (!byteGroupSize.empty()) {
                if (!isValidNumber(byteGroupSize))
                    return;
                hexDataReplacement = isReversed
                    ? decimalToReversedHex(hexDataReplacement, ult::stoi(byteGroupSize))
                    : decimalToHex(hexDataReplacement, ult::stoi(byteGroupSize));
            } else {
                hexDataReplacement = isReversed
                    ? decimalToReversedHex(hexDataReplacement)
                    : decimalToHex(hexDataReplacement);
            }
        }
        hexEditByCustomOffset(sourcePath, customPattern, offset, hexDataReplacement);
    }
}


void rebootToHekateConfig(Payload::HekateConfigList& configList, const std::string& option, bool isIni) {
    int rebootIndex = -1;  // Initialize rebootIndex to -1, indicating no match found
    auto configIterator = configList.begin();
    if (std::all_of(option.begin(), option.end(), ::isdigit)) {
        if (!isValidNumber(option)) {
            return;
        }
        rebootIndex = ult::stoi(option);
        std::advance(configIterator, rebootIndex);
    } else {
        for (auto it = configList.begin(); it != configList.end(); ++it) {
            if (it->name == option) {
                rebootIndex = std::distance(configList.begin(), it);
                configIterator = it;  // Update the iterator to the matching element
                break;
            }
        }
    }
    if (rebootIndex != -1) {
        Payload::RebootToHekateConfig(*configIterator, isIni);
    }
}

inline std::string getUnquoted(const std::vector<std::string>& cmd, size_t index) {
    std::string value = cmd[index];
    removeQuotes(value);
    return value;
}

inline void setCommandFailed() {
    commandSuccess.store(false, std::memory_order_release);
}
inline void setCommandResult(bool result) {
    commandSuccess.store(result, std::memory_order_release);
}

void executeCommands(std::vector<std::vector<std::string>> commands) {
    interpretAndExecuteCommands(std::move(commands), "", "");
    resetPercentages();
}

void executeIniCommands(const std::string &iniPath, const std::string &section, const std::string &packagePath = PACKAGE_PATH) {
    if (isFile(iniPath)) {
        auto commands = loadSpecificSectionFromIni(iniPath, section);
        if (!commands.empty()) {
            interpretAndExecuteCommands(std::move(commands), packagePath, section);
            resetPercentages();
        }
    }
}

// ---------------------------------------------------------------------------
// ipcDispatch — open a service via smGetServiceOriginal (non-blocking), send
// a void->void command, close the session, and return the dispatch Result.
// Returns a failed Result immediately if the service is not registered.
// ---------------------------------------------------------------------------
static inline Result ipcDispatch(const char* svcName, u32 enumCmd) {
    Handle handle = INVALID_HANDLE;
    Result rc = smGetServiceOriginal(&handle, smEncodeName(svcName));
    if (R_FAILED(rc)) return rc;
    Service srv = {};
    serviceCreate(&srv, handle);
    rc = serviceDispatch(&srv, enumCmd);
    serviceClose(&srv);
    return rc;
}

// ---------------------------------------------------------------------------
// Parse a TID string (bare 16-hex-char or 0x-prefixed) to u64.
// Returns 0 on empty / invalid input.
static inline u64 parseTid(const std::string& s) {
    if (s.empty()) return 0;
    const char* p = s.c_str();
    if (s.size() > 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) p += 2;
    return static_cast<u64>(std::strtoull(p, nullptr, 16));
}

// Returns true when the module's content folder exists under /atmosphere/contents/.
// This is true whether the module is currently running or not.
inline bool moduleExists(u64 programId) {
    if (programId == 0) return false;
    char path[FS_MAX_PATH];
    std::snprintf(path, sizeof(path), "/atmosphere/contents/%016lX", programId);
    return isDirectory(path);
}

// Returns true when the module is currently running (has an active process ID).
inline bool moduleIsActive(u64 programId) {
    if (programId == 0) return false;
    u64 pid = 0;
    return R_SUCCEEDED(pmdmntGetProcessId(&pid, programId)) && pid != 0;
}

// ipcPollExit — poll pmdmnt every 50 ms for up to 200 ms waiting for a
// process to disappear. Returns true as soon as the process is gone.
// ---------------------------------------------------------------------------
static inline bool ipcPollExit(u64 programId) {
    constexpr u64 kInterval = 50'000'000ULL;
    constexpr u64 kTimeout  = 200'000'000ULL;
    u64 elapsed = 0;
    while (elapsed < kTimeout) {
        u64 pid = 0;
        if (R_FAILED(pmdmntGetProcessId(&pid, programId)) || pid == 0)
            return true;
        svcSleepThread(kInterval);
        elapsed += kInterval;
    }
    return false;
}

// Main processCommand function
void processCommand(const std::vector<std::string>& cmd, const std::string& packagePath = "", const std::string& selectedCommand = "") {
    const std::string& commandName = cmd[0];
    const size_t cmdSize = cmd.size();
    
    // Fast path dispatch by first character
    switch (commandName[0]) {
        case 'a':
            if (commandName == "add-ini-section") {
                handleIniCommands(cmd, packagePath);
                return;
            }
            break;
            
        case 'b':
            if (commandName == "back") {
                goBackAfter.store(true, std::memory_order_release);
                return;
            }
            if (cmdSize >= 2 && commandName == "backlight") {
                std::string togglePattern = getUnquoted(cmd, 1);
                lblInitialize();
                if (togglePattern == "auto") {
                    if (cmdSize >= 3) {
                        togglePattern = cmd[2];
                        if (togglePattern == ON_STR)
                            lblEnableAutoBrightnessControl();
                        else if (togglePattern == OFF_STR)
                            lblDisableAutoBrightnessControl();
                    }
                } else if (togglePattern == ON_STR) {
                    lblSwitchBacklightOn(0);
                } else if (togglePattern == OFF_STR) {
                    lblSwitchBacklightOff(0);
                } else if (isValidNumber(togglePattern)) {
                    lblSetCurrentBrightnessSetting(ult::stof(togglePattern) / 100.0f);
                }
                lblExit();
                return;
            }
            break;
            
        case 'c':
            if (commandName == "cp" || commandName == "copy") {
                handleCopyCommand(cmd, packagePath);
                return;
            }
            if (commandName == "compare") {
                if (cmdSize >= 4) {
                    std::string path1 = cmd[1];
                    preprocessPath(path1, packagePath);
                    std::string path2 = cmd[2];
                    preprocessPath(path2, packagePath);
                    std::string outputPath = cmd[3];
                    preprocessPath(outputPath, packagePath);
                    if (path1.find('*') != std::string::npos)
                        compareWildcardFilesLists(path1, path2, outputPath);
                    else
                        compareFilesLists(path1, path2, outputPath);
                }
                return;
            }
            if (cmdSize >= 2 && commandName == "clear") {
                const std::string clearOption = getUnquoted(cmd, 1);
                if (clearOption == "log") {
                    #if USING_LOGGING_DIRECTIVE
                    deleteFileOrDirectory(defaultLogFilePath);
                    #endif
                } else if (clearOption == "hex_sum_cache") {
                    hexSumCache.clear();
                }
                return;
            }
            break;
            
        case 'd':
            if (commandName == "del" || commandName == "delete") {
                handleDeleteCommand(cmd, packagePath);
                return;
            }
            if (commandName == "download" || commandName == "download-no-retry") {
                if (cmdSize >= 3) {
                    std::string fileUrl = cmd[1];
                    preprocessUrl(fileUrl);
                    std::string destinationPath = cmd[2];
                    preprocessPath(destinationPath, packagePath);
                    bool downloadSuccess = false;
                    
                    const bool shouldRetry = (commandName == "download");
                    const size_t maxAttempts = shouldRetry ? 3 : 1;
                    
                    for (size_t i = 0; i < maxAttempts; ++i) {
                        downloadSuccess = downloadFile(fileUrl, destinationPath);
                        if (abortDownload.load(std::memory_order_acquire)) {
                            downloadSuccess = false;
                            break;
                        }
                        if (downloadSuccess) break;
                        
                        if (shouldRetry && i < maxAttempts - 1) {
                            svcSleepThread(200'000'000);
                        }
                    }
                    
                    commandSuccess.store(
                        downloadSuccess && commandSuccess.load(std::memory_order_acquire),
                        std::memory_order_release
                    );
                }
                return;
            }
            if (cmdSize >= 2 && commandName == "dot-clean") {
                std::string path = cmd[1];
                preprocessPath(path, packagePath);
                dotCleanDirectory(path);
                return;
            }
            break;
            
        case 'e':
            if (cmdSize >= 2 && commandName == "exec") {
                const std::string sectionName = getUnquoted(cmd, 1);
                
                if (cmdSize >= 3) {
                    const std::string secondArg = getUnquoted(cmd, 2);
                    
                    if (secondArg.length() >= 4 && secondArg.substr(secondArg.length() - 4) == ".ini") {
                        std::string iniPath = secondArg;
                        preprocessPath(iniPath, packagePath);
                        
                        const size_t lastSlash = iniPath.find_last_of('/');
                        const std::string iniPackagePath = (lastSlash != std::string::npos)
                            ? iniPath.substr(0, lastSlash + 1) : packagePath;
                        
                        bool resetCommandSuccess = false;
                        if (!commandSuccess.load(std::memory_order_acquire))
                            resetCommandSuccess = true;
                        
                        executeIniCommands(iniPath, sectionName, iniPackagePath);
                        
                        if (resetCommandSuccess)
                            setCommandFailed();
                    } else {
                        setCommandFailed();
                    }
                } else {
                    bool resetCommandSuccess = false;
                    if (!commandSuccess.load(std::memory_order_acquire))
                        resetCommandSuccess = true;
                    
                    executeIniCommands(packagePath + BOOT_PACKAGE_FILENAME, sectionName, packagePath);
                    
                    if (resetCommandSuccess)
                        setCommandFailed();
                }
                return;
            }
            if (commandName == "exit") {
                if (cmdSize >= 2) {
                    // Explicit destination: close overlay and relaunch into the requested tab.
                    const std::string selection = getUnquoted(cmd, 1);
                    if (selection == "to_overlays") {
                        setIniFileValue(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME, IN_OVERLAY_STR, TRUE_STR);
                        exitingUltrahand.store(true, std::memory_order_release);
                        ult::launchingOverlay.store(true, std::memory_order_release);
                        tsl::setNextOverlay(OVERLAY_PATH+"ovlmenu.ovl");
                        tsl::Overlay::get()->close(true);
                    } else if (selection == "to_packages") {
                        setIniFileValue(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME, "to_packages", TRUE_STR);
                        setIniFileValue(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME, IN_OVERLAY_STR, TRUE_STR);
                        exitingUltrahand.store(true, std::memory_order_release);
                        ult::launchingOverlay.store(true, std::memory_order_release);
                        tsl::setNextOverlay(OVERLAY_PATH+"ovlmenu.ovl");
                        tsl::Overlay::get()->close(true);
                    } else if (selection == "package") {
                        // Bare `exit package`: signal the UI thread to return to the packages menu instantly.
                        // PackageMenu::handleInput() will drain returnContextStack, identify the
                        // originating package folder, and swapTo<MainMenu> on the packages tab with
                        // the cursor positioned on the correct package entry.
                        triggerReturnToPackages.store(true, std::memory_order_release);
                    } if (selection == "ovlloader") {
                        if (requestOverlayExit()) {
                            exitingUltrahand.store(true, std::memory_order_release);
                            ult::launchingOverlay.store(true, std::memory_order_release);
                            tsl::setNextOverlay(OVERLAY_PATH+"ovlmenu.ovl");
                            tsl::Overlay::get()->close(true);
                        }
                    }
                    // Unknown arg: no-op (fall through to return)
                } else {
                    exitingUltrahand.store(true, std::memory_order_release);
                    ult::launchingOverlay.store(true, std::memory_order_release);
                    tsl::setNextOverlay(OVERLAY_PATH+"ovlmenu.ovl");
                    tsl::Overlay::get()->close(true);
                }
                return;
            }

            break;
            
        case 'f':
            if (commandName == "force_failure") {
                setCommandFailed();
                return;
            } if (commandName == "flag") {
                if (cmdSize >= 3) {
                    std::string wildcardPattern = cmd[1];
                    preprocessPath(wildcardPattern, packagePath);
                    std::string outputDir = cmd[2];
                    preprocessPath(outputDir, packagePath);
                    createFlagFiles(wildcardPattern, outputDir);
                } else {
                    #if USING_LOGGING_DIRECTIVE
                    if (!disableLogging)
                        logMessage("Usage: flag <wildcardPattern> <outputDir>");
                    #endif
                }
                return;
            }
            break;
            
        case 'h':
            if (commandName.compare(0, 7, "hex-by-") == 0) {
                if (cmdSize >= 4) {
                    std::string sourcePath = cmd[1];
                    preprocessPath(sourcePath, packagePath);
                    
                    const std::string secondArg = getUnquoted(cmd, 2);
                    const std::string thirdArg = getUnquoted(cmd, 3);
                    
                    std::string fourthArg;
                    if (cmdSize >= 5)
                        fourthArg = getUnquoted(cmd, 4);
                    
                    std::string fifthArg;
                    if (cmdSize >= 6)
                        fifthArg = getUnquoted(cmd, 5);
                    
                    if (commandName == "hex-by-custom-offset" || 
                        commandName == "hex-by-custom-decimal-offset" || 
                        commandName == "hex-by-custom-rdecimal-offset") {
                        if (cmdSize >= 5) {
                            const std::string customPattern = getUnquoted(cmd, 2);
                            const std::string offset = getUnquoted(cmd, 3);
                            const std::string hexDataReplacement = getUnquoted(cmd, 4);
                            
                            std::string byteGroupSize;
                            if (cmdSize >= 6)
                                byteGroupSize = getUnquoted(cmd, 5);
                            
                            handleHexByCustom(sourcePath, customPattern, offset, hexDataReplacement, commandName, byteGroupSize);
                        }
                    } else {
                        handleHexEdit(sourcePath, secondArg, thirdArg, fourthArg, fifthArg, commandName, cmd);
                    }
                }
                return;
            }
            break;
            
        case 'i':
            if (commandName == "ipc_exists") {
                // ipc_exists <SERVICE_NAME>  — success when service is registered.
                // smGetServiceOriginal never blocks on an unregistered name.
                if (cmdSize >= 2) {
                    Handle handle = INVALID_HANDLE;
                    const bool registered = R_SUCCEEDED(
                        smGetServiceOriginal(&handle, smEncodeName(cmd[1].c_str())));
                    if (registered) svcCloseHandle(handle);
                    setCommandResult(registered);
                }
                return;
            }
            if (commandName == "ipc-exec") {
                // ipc-exec <SERVICE_NAME> <ENUM_CMD>
                // Sends a void->void IPC command. Success = dispatch accepted.
                if (cmdSize >= 3) {
                    const u32 enumCmd = static_cast<u32>(ult::stoi(cmd[2]));
                    commandSuccess.store(
                        R_SUCCEEDED(ipcDispatch(cmd[1].c_str(), enumCmd)),
                        std::memory_order_release);
                } else {
                    setCommandFailed();
                }
                return;
            }
            break;

        case 'l':
            if (commandName == "logging") {
                interpreterLogging.store(true, std::memory_order_release);
                return;
            }
            break;
            
        case 'm':
            if (commandName == "mkdir" || commandName == "make") {
                handleMakeDirCommand(cmd, packagePath);
                return;
            }
            if (commandName == "mv" || commandName == "move") {
                handleMoveCommand(cmd, packagePath);
                return;
            }
            // legacy support for `mirror_`
            if ((commandName.compare(0, 7, "mirror_") == 0) || (commandName.compare(0, 7, "mirror-") == 0)) {
                handleMirrorCommand(cmd, packagePath);
                return;
            }
            if (commandName == "matching_txt_line") {
                // [!]matching_txt_line <path> <line...>
                if (cmdSize >= 3) {
                    std::string path = cmd[1];
                    preprocessPath(path, packagePath);
                    std::string line = getUnquoted(cmd, 2);
                    for (size_t i = 3; i < cmdSize; ++i) { line += ' '; line += getUnquoted(cmd, i); }
                    const bool match = txtLineExists(path, line);
                    setCommandResult(match);
                } else { setCommandFailed(); }
                return;
            }
            if (commandName == "matching_hex_val") {
                // [!]matching_hex_val <path> <offset> <hex>
                if (cmdSize >= 4) {
                    std::string path = cmd[1];
                    preprocessPath(path, packagePath);
                    const uint32_t offset = static_cast<uint32_t>(
                        isValidNumber(cmd[2]) ? std::strtoul(cmd[2].c_str(), nullptr, 0) : 0u);
                    const bool match = hexValMatches(path, offset, getUnquoted(cmd, 3));
                    setCommandResult(match);
                } else { setCommandFailed(); }
                return;
            }
            if (commandName == "matching_hex_val_custom") {
                // [!]matching_hex_val_custom <path> <ascii_pattern> <offset> <hex>
                if (cmdSize >= 5) {
                    std::string path = cmd[1];
                    preprocessPath(path, packagePath);
                    const bool match = hexValMatchesCustom(path, getUnquoted(cmd, 2), getUnquoted(cmd, 3), getUnquoted(cmd, 4));
                    setCommandResult(match);
                } else { setCommandFailed(); }
                return;
            }
            if (commandName == "matching_ini_val") {
                // [!]matching_ini_val <path> <section> <key> <value...>
                if (cmdSize >= 5) {
                    std::string path = cmd[1];
                    preprocessPath(path, packagePath);
                    std::string expected = getUnquoted(cmd, 4);
                    for (size_t i = 5; i < cmdSize; ++i) { expected += ' '; expected += getUnquoted(cmd, i); }
                    std::string actual = parseValueFromIniSection(path, getUnquoted(cmd, 2), getUnquoted(cmd, 3));
                    removeQuotes(actual);
                    setCommandResult(actual == expected);
                } else { setCommandFailed(); }
                return;
            }
            if (commandName == "module_exists") {
                const bool exists = cmdSize >= 2 && moduleExists(parseTid(cmd[1]));
                setCommandResult(exists);
                return;
            }
            if (commandName == "module_is_active") {
                const bool active = cmdSize >= 2 && moduleIsActive(parseTid(cmd[1]));
                setCommandResult(active);
                return;
            }
            if (commandName == "module") {
                // module start <TID> — launch a sysmodule by program ID.
                // module stop  <TID> — stop a sysmodule, graceful-first with
                //                      force-kill fallback for dynamic modules.
                // Refuses start/stop of static modules with no graceful contract.
                // <TID>: bare 16-hex-char folder name or 0x-prefixed program ID.
                if (cmdSize >= 3) {
                    const std::string& subcmd = cmd[1];
                    const char* tidCStr = cmd[2].c_str();
                    if (cmd[2].size() > 2 && tidCStr[0] == '0' &&
                        (tidCStr[1] == 'x' || tidCStr[1] == 'X'))
                        tidCStr += 2;
                    const u64 programId = static_cast<u64>(
                        std::strtoull(tidCStr, nullptr, 16));
                    if (programId == 0) {
                        setCommandFailed();
                        return;
                    }

                    // Read toolbox.json — safe defaults if absent.
                    bool needReboot  = true;
                    bool hasGraceful = false;
                    char gracefulSvc[16] = {};
                    u32  gracefulCmd = 0;
                    {
                        char tbPath[FS_MAX_PATH];
                        std::snprintf(tbPath, sizeof(tbPath),
                            "/atmosphere/contents/%016lX/toolbox.json", programId);
                        FILE* fp = std::fopen(tbPath, "rb");
                        if (fp) {
                            std::fseek(fp, 0, SEEK_END);
                            const long sz = std::ftell(fp);
                            if (sz > 0 && sz <= 4096) {
                                std::fseek(fp, 0, SEEK_SET);
                                char tbBuf[4096];
                                if (std::fread(tbBuf, 1, sz, fp) == static_cast<size_t>(sz)) {
                                    tbBuf[sz] = '\0';
                                    cJSON* tb = cJSON_ParseWithLength(tbBuf, sz);
                                    if (tb) {
                                        cJSON* ri = cJSON_GetObjectItem(tb, "requires_reboot");
                                        if (ri && cJSON_IsBool(ri))
                                            needReboot = static_cast<bool>(cJSON_IsTrue(ri));
                                        cJSON* si = cJSON_GetObjectItem(tb, "shutdown_service");
                                        cJSON* ci = cJSON_GetObjectItem(tb, "shutdown_cmd");
                                        if (si && cJSON_IsString(si) && ci && cJSON_IsNumber(ci)) {
                                            const size_t svcLen = std::strlen(si->valuestring);
                                            if (svcLen > 0 && svcLen <= 8 && ci->valueint >= 0) {
                                                std::memcpy(gracefulSvc, si->valuestring, svcLen);
                                                gracefulSvc[svcLen] = '\0';
                                                gracefulCmd = static_cast<u32>(ci->valueint);
                                                hasGraceful = true;
                                            }
                                        }
                                        cJSON_Delete(tb);
                                    }
                                }
                            }
                            std::fclose(fp);
                        }
                    }

                    // Static modules with no graceful contract cannot be toggled.
                    if (needReboot && !hasGraceful) {
                        setCommandFailed();
                        return;
                    }

                    if (subcmd == "start") {
                        pmshellInitialize();
                        const NcmProgramLocation loc{
                            .program_id = programId,
                            .storageID  = NcmStorageId_None,
                        };
                        u64 pid = 0;
                        const bool ok = R_SUCCEEDED(pmshellLaunchProgram(0, &loc, &pid));
                        pmshellExit();
                        setCommandResult(ok);

                    } else if (subcmd == "stop") {
                        bool stopped = false;
                        if (hasGraceful &&
                            R_SUCCEEDED(ipcDispatch(gracefulSvc, gracefulCmd)))
                            stopped = ipcPollExit(programId);
                        if (!stopped && !needReboot) {
                            pmshellInitialize();
                            stopped = R_SUCCEEDED(pmshellTerminateProgram(programId));
                            pmshellExit();
                        }
                        setCommandResult(stopped);

                    } else {
                        setCommandFailed();
                    }
                } else {
                    setCommandFailed();
                }
                return;
            }
            break;
            
        case 'n':
            if (commandName == "notify" || commandName == "notify-now" ||
                commandName == "notification" || commandName == "notification-now") {
                if (cmdSize > 1 && tsl::notification) {
                    const std::string text = getUnquoted(cmd, 1);
                    size_t argIdx = 2;
            
                    // Helper: peek next arg, consume and advance only if predicate is true
                    auto tryConsume = [&](auto pred) -> std::string {
                        if (argIdx < cmdSize) {
                            const std::string val = getUnquoted(cmd, argIdx);
                            if (pred(val)) { ++argIdx; return val; }
                        }
                        return {};
                    };
            
                    const std::string fontStr = tryConsume([](const std::string& s){ 
                        return isValidNumber(s) && ult::stoi(s) >= 1 && ult::stoi(s) <= 34; 
                    });
                    const std::string alignStr = tryConsume([](const std::string& s){ return s == LEFT_STR || s == CENTER_STR || s == RIGHT_STR; });
                    const std::string splitStr = tryConsume([](const std::string& s){ return s == WORD_STR || s == CHAR_STR; });
                    const std::string durStr = tryConsume([](const std::string& s){ return isValidNumber(s); });
                    const std::string title    = tryConsume([](const std::string& s){ return s != TRUE_STR && s != FALSE_STR; });
                    const std::string showTimeStr = tryConsume([](const std::string& s){ return s == TRUE_STR || s == FALSE_STR; });
                    const std::string appId    = (argIdx < cmdSize) ? getUnquoted(cmd, argIdx) : "";
            
                    const bool hasTitle = !title.empty();
                    const int  fontSize = !fontStr.empty()  ? std::clamp(ult::stoi(fontStr), 1, 34)
                                                             : (hasTitle ? 24 : 26);
                    const std::string_view alignment = !alignStr.empty() ? std::string_view(alignStr)
                                                                          : (hasTitle ? LEFT_STR : CENTER_STR);
                    const bool showTime = showTimeStr.empty() || showTimeStr == TRUE_STR;
            
                    const bool now = commandName.size() > 4 &&
                                     commandName.compare(commandName.size() - 4, 4, "-now") == 0;
                    
                    const u32 duration = durStr.empty()    ? (!now ? 4000u : 3000u)
                                       : durStr == "0"     ? 0u
                                       : static_cast<u32>(std::clamp(ult::stoi(durStr), 500, 30000));
                    tsl::notification->show(text, fontSize, 20,
                                            appId, title, duration,
                                            now, false, showTime,
                                            alignment,
                                            splitStr);
                }
                return;
            }
            if (commandName == "ntp-sync") {
                // ntp-sync <url>  — on-demand clock sync; checks internet access internally
                if (cmdSize >= 2) {
                    std::string url = getUnquoted(cmd, 1);
                    setCommandResult(ult::syncNtp(url));
                } else { setCommandFailed(); }
                return;
            }
            break;
            
        case 'o':
            if (commandName == "open") {
                if (cmdSize >= 2) {
                    std::string overlayPath = getUnquoted(cmd, 1);
                    preprocessPath(overlayPath, packagePath);
                    
                    if (!isFile(overlayPath)) {
                        #if USING_LOGGING_DIRECTIVE
                        if (!disableLogging)
                            logMessage("Overlay file not found: " + overlayPath);
                        #endif
                        setCommandFailed();
                        return;
                    }
                    
                    std::string launchArgs;
                    if (cmdSize > 2) {
                        for (size_t i = 2; i < cmdSize; ++i) {
                            if (i > 2) launchArgs += " ";
                            launchArgs += getUnquoted(cmd, i);
                        }
                    }
                    
                    // Snapshot the current package-menu position (if any) so returning from
                    // this overlay with a plain back-out can restore it exactly. No-op if we
                    // aren't inside an interactive package menu (e.g. boot_package/exit_package).
                    if (ult::openCommandInvokedCallback)
                        ult::openCommandInvokedCallback();

                    // Past the isFile() failure return above, so this only ever fires for an
                    // "open" that's actually about to launch something.
                    lastCommandLaunchesOverlay.store(true, std::memory_order_release);

                    {
                        std::lock_guard<std::mutex> lock(ult::overlayLaunchMutex);
                        ult::requestedOverlayPath = overlayPath;
                        ult::requestedOverlayArgs = launchArgs;
                        ult::overlayLaunchRequested.store(true, std::memory_order_release);
                    }
                    
                    #if USING_LOGGING_DIRECTIVE
                    if (!disableLogging)
                        logMessage("Requesting overlay launch: " + overlayPath + " with args: " + launchArgs);
                    #endif
                    
                    return;
                } else {
                    #if USING_LOGGING_DIRECTIVE
                    if (!disableLogging)
                        logMessage("Usage: open <overlay_path> [launch_arguments...]");
                    #endif
                    setCommandFailed();
                    return;
                }
            }
            break;
            
        case 'p':
            if (commandName == "path_exists") {
                if (cmdSize >= 2) {
                    std::string sourcePath = cmd[1];
                    preprocessPath(sourcePath, packagePath);
                    setCommandResult(ult::isFileOrDirectory(sourcePath));
                }
            }
            if (commandName == "pchtxt2ips") {
                if (cmdSize >= 3) {
                    std::string sourcePath = cmd[1];
                    preprocessPath(sourcePath, packagePath);
                    std::string destinationPath = cmd[2];
                    preprocessPath(destinationPath, packagePath);
                    commandSuccess.store(
                        pchtxt2ips(sourcePath, destinationPath) && commandSuccess.load(std::memory_order_acquire),
                        std::memory_order_release
                    );
                }
                return;
            }
            if (commandName == "pchtxt2cheat") {
                if (cmdSize >= 2) {
                    std::string sourcePath = cmd[1];
                    preprocessPath(sourcePath, packagePath);
                    if (cmdSize >= 3) {
                        const std::string cheatName = cmd[2];
                        commandSuccess.store(
                            pchtxt2cheat(sourcePath, cheatName) && commandSuccess.load(std::memory_order_acquire),
                            std::memory_order_release
                        );
                    } else {
                        commandSuccess.store(
                            pchtxt2cheat(sourcePath) && commandSuccess.load(std::memory_order_acquire),
                            std::memory_order_release
                        );
                    }
                }
                return;
            }
            break;
            
        case 'r':
            if (commandName == "rename") {
                handleMoveCommand(cmd, packagePath);
                return;
            }
            if (commandName == "rename-ini-section" || commandName == "remove-ini-section" || 
                commandName == "remove-ini-key" || commandName == "remove-ini-key-matching-key") {
                handleIniCommands(cmd, packagePath);
                return;
            }
            if (commandName == "refresh") {
                if (cmdSize == 1) {
                    refreshPage.store(true, std::memory_order_release);
                } else {
                    const std::string refreshPattern = getUnquoted(cmd, 1);
                    if (refreshPattern == "theme")
                        tsl::initializeThemeVars();
                    else if (refreshPattern == "package")
                        refreshPackage.store(true, std::memory_order_release);
                    else if (refreshPattern == "wallpaper")
                        refreshWallpaperNow.store(true, std::memory_order_release);
                    else if (refreshPattern == "combos")
                        ult::refreshCombos.store(true, std::memory_order_release);
                }
                return;
            }
            if (commandName == "refresh-return") {
                refreshReturnAfter.store(true, std::memory_order_release);
                return;
            }
            if (cmdSize > 1 && commandName == "refresh-to") {
                const std::string refreshPattern = getUnquoted(cmd, 1);
                std::string refreshPattern2 = "";
                std::string refreshPattern3 = "";
                
                if (cmdSize > 2)
                    refreshPattern2 = getUnquoted(cmd, 2);
                
                if (cmdSize > 3)
                    refreshPattern3 = getUnquoted(cmd, 3);
                
                jumpItemName = refreshPattern;
                jumpItemValue = refreshPattern2;
                jumpItemExactMatch = !(refreshPattern3 == FALSE_STR);
                skipJumpReset.store(true, std::memory_order_release);
                refreshPage.store(true, std::memory_order_release);
                return;
            }
            if (commandName == "reboot") {
                if (cmdSize == 2) {
                    const std::string rebootPattern = getUnquoted(cmd, 1);
                    if (rebootPattern == "ovlloader") {
                        if (requestOverlayReload()) {
                            exitingUltrahand.store(true, std::memory_order_release);
                            ult::launchingOverlay.store(true, std::memory_order_release);
                            tsl::setNextOverlay(OVERLAY_PATH+"ovlmenu.ovl");
                            tsl::Overlay::get()->close(true);
                        }
                        return;
                    }
                }

                bool launchUpdaterPayload = false;
                for (const std::string& file : PROTECTED_FILES) {
                    if (isFile(file + ".ultra")) {
                        launchUpdaterPayload = true;
                        break;
                    }
                }
                
                if (launchUpdaterPayload) {
                    const std::string rebootOption = PAYLOADS_PATH + "ultrahand_updater.bin";
                    if (!isFile(rebootOption)) {
                        downloadFile(UPDATER_PAYLOAD_URL, PAYLOADS_PATH, false, true);
                        downloadPercentage.store(-1, std::memory_order_release);
                    }
                    if (isFile(rebootOption)) {
                        const std::string fileName = getNameFromPath(rebootOption);
                        
                        if (util::IsErista()) {
                            Payload::PayloadConfig reboot_payload = { fileName, rebootOption };
                            Payload::RebootToPayload(reboot_payload);
                        } else {
                            std::string strippedRebootOption = rebootOption;
                            if (strippedRebootOption.compare(0, ROOT_PATH.length(), ROOT_PATH) == 0) {
                                strippedRebootOption.erase(0, ROOT_PATH.length());
                            }
                            
                            const std::string iniPath = "/bootloader/ini/" + fileName + ".ini";
                            deleteFileOrDirectory(iniPath);
                            setIniFileValue(iniPath, fileName, "payload", strippedRebootOption);
                            setIniFileValue(iniPath, fileName, "bootwait", "0");
                            Payload::HekateConfigList iniConfigList = Payload::LoadIniConfigList();
                            rebootToHekateConfig(iniConfigList, fileName, true);
                        }
                    } else {
                        launchUpdaterPayload = false;
                    }
                }
                if (!launchUpdaterPayload && (util::IsErista() || util::SupportsMarikoRebootToConfig())) {
                    std::string rebootOption;
                    if (cmdSize >= 2) {
                        rebootOption = getUnquoted(cmd, 1);
                        if (cmdSize >= 3) {
                            const std::string option = getUnquoted(cmd, 2);
                            if (rebootOption == "boot") {
                                Payload::HekateConfigList bootConfigList = Payload::LoadHekateConfigList();
                                rebootToHekateConfig(bootConfigList, option, false);
                            } else if (rebootOption == "ini") {
                                Payload::HekateConfigList iniConfigList = Payload::LoadIniConfigList();
                                rebootToHekateConfig(iniConfigList, option, true);
                            }
                        }
                        if (rebootOption == "UMS") {
                            Payload::RebootToHekateUMS(Payload::UmsTarget_Sd);
                        } else if (rebootOption == "HEKATE" || rebootOption == "hekate") {
                            Payload::RebootToHekateMenu();
                        } else if (isFile(rebootOption)) {
                            const std::string fileName = getNameFromPath(rebootOption);
                            if (util::IsErista()) {
                                Payload::PayloadConfig reboot_payload = {fileName, rebootOption};
                                Payload::RebootToPayload(reboot_payload);
                            } else {
                                std::string strippedRebootOption = rebootOption;
                                if (strippedRebootOption.compare(0, ROOT_PATH.length(), ROOT_PATH) == 0) {
                                    strippedRebootOption.erase(0, ROOT_PATH.length());
                                }
                                
                                const std::string iniPath = "/bootloader/ini/" + fileName + ".ini";
                                setIniFileValue(iniPath, fileName, "payload", strippedRebootOption);
                                setIniFileValue(iniPath, fileName, "bootwait", "0");
                                Payload::HekateConfigList iniConfigList = Payload::LoadIniConfigList();
                                rebootToHekateConfig(iniConfigList, fileName, true);
                            }
                        }
                    }
                    if (rebootOption.empty())
                        Payload::RebootToHekate();
                }
                
                i2cExit();
                splExit();
                fsdevUnmountAll();
                Payload::StageUsbPdTeardown();
                spsmShutdown(SpsmShutdownMode_Reboot);
                spsmExit();
                return;
            }

            break;
            
        case 's':
            if (commandName == "set-ini-val" || commandName == "set-ini-value" || 
                commandName == "set-ini-key" || commandName == "set-ini-val-matching-key") {
                handleIniCommands(cmd, packagePath);
                return;
            }
            if (commandName == "set-json-val" || commandName == "set-json-value" || 
                commandName == "set-json-key") {
                handleJsonCommands(cmd, packagePath);
                return;
            }
            if (cmdSize >= 2 && commandName == "set-footer") {
                const std::string desiredValue = getUnquoted(cmd, 1);
                if (desiredValue.find(NULL_STR) != std::string::npos)
                    setCommandFailed();
                else
                    setIniFileValue((packagePath + CONFIG_FILENAME), selectedCommand, FOOTER_STR, desiredValue);
                return;
            }
            if (cmdSize > 1 && commandName == "set-region") {
                const std::string regionStr = stringToUppercase(getUnquoted(cmd, 1));
                
                SetRegion region;
                bool validRegion = true;
                
                if (regionStr == "JPN") {
                    region = SetRegion_JPN;
                } else if (regionStr == "USA") {
                    region = SetRegion_USA;
                } else if (regionStr == "EUR") {
                    region = SetRegion_EUR;
                } else if (regionStr == "AUS") {
                    region = SetRegion_AUS;
                } else if (regionStr == "HTK") {
                    region = SetRegion_HTK;
                } else if (regionStr == "CHN") {
                    region = SetRegion_CHN;
                } else {
                    validRegion = false;
                }
                
                if (validRegion) {
                    if (R_FAILED(setsysSetRegionCode(region)))
                        setCommandFailed();
                } else {
                    setCommandFailed();
                }
                return;
            }
            if (commandName == "shutdown") {
                if (cmdSize >= 2) {
                    const std::string selection = getUnquoted(cmd, 1);
                    if (selection == "controllers") {
                        powerOffAllControllers();
                    }
                } else {
                    fsdevUnmountAll();
                    Payload::StageUsbPdTeardown();
                    (void)spsmShutdown(SpsmShutdownMode_Normal);
                }
                return;
            }
            break;
            
        case 't':
            if (commandName == "touch") {
                if (cmdSize >= 2) {
                    std::string filePath = cmd[1];
                    preprocessPath(filePath, packagePath);
                    const std::string parentDir = filePath.substr(0, filePath.rfind('/'));
                    if (!parentDir.empty())
                        createDirectory(parentDir);
                    FILE* f = fopen(filePath.c_str(), "a");
                    if (f) fclose(f);
                }
                return;
            }
            break;

        case 'u':
            if (cmdSize >= 3 && commandName == "unzip") {
                std::string sourcePath = cmd[1];
                preprocessPath(sourcePath, packagePath);
                std::string destinationPath = cmd[2];
                preprocessPath(destinationPath, packagePath);
                commandSuccess.store(
                    unzipFile(sourcePath, destinationPath) && commandSuccess.load(std::memory_order_acquire),
                    std::memory_order_release
                );
                return;
            }
            break;
            
        case 'v':
            if (commandName == "volume") {
                if (cmdSize >= 2) {
                    const std::string volumeInput = getUnquoted(cmd, 1);
                    
                    if (isValidNumber(volumeInput)) {
                        const float volumePercentage = ult::stof(volumeInput);
                        
                        if (volumePercentage < 0.0f || volumePercentage > 150.0f) {
                            return;
                        }
                        
                        const float masterVolume = volumePercentage / 100.0f;
                        
                        audctlInitialize();
                        audctlSetSystemOutputMasterVolume(masterVolume);
                        audctlExit();
                    }
                } else {
                    #if USING_LOGGING_DIRECTIVE
                    if (!disableLogging)
                        logMessage("Volume command missing required argument.");
                    #endif
                }
                return;
            }
            break;

        case '!':
            if (commandName == "!ipc_exists") {
                // !ipc_exists <SERVICE_NAME> — success when NOT registered.
                if (cmdSize >= 2) {
                    Handle handle = INVALID_HANDLE;
                    const bool registered = R_SUCCEEDED(
                        smGetServiceOriginal(&handle, smEncodeName(cmd[1].c_str())));
                    if (registered) svcCloseHandle(handle);
                    setCommandResult(!registered);
                }
                return;
            }
            if (commandName == "!matching_txt_line") {
                // !matching_txt_line <path> <line...>
                if (cmdSize >= 3) {
                    std::string path = cmd[1];
                    preprocessPath(path, packagePath);
                    std::string line = getUnquoted(cmd, 2);
                    for (size_t i = 3; i < cmdSize; ++i) { line += ' '; line += getUnquoted(cmd, i); }
                    setCommandResult(!txtLineExists(path, line));
                } else { setCommandFailed(); }
                return;
            }
            if (commandName == "!matching_hex_val") {
                // !matching_hex_val <path> <offset> <hex>
                if (cmdSize >= 4) {
                    std::string path = cmd[1];
                    preprocessPath(path, packagePath);
                    const uint32_t offset = static_cast<uint32_t>(
                        isValidNumber(cmd[2]) ? std::strtoul(cmd[2].c_str(), nullptr, 0) : 0u);
                    setCommandResult(!hexValMatches(path, offset, getUnquoted(cmd, 3)));
                } else { setCommandFailed(); }
                return;
            }
            if (commandName == "!matching_hex_val_custom") {
                // !matching_hex_val_custom <path> <ascii_pattern> <offset> <hex>
                if (cmdSize >= 5) {
                    std::string path = cmd[1];
                    preprocessPath(path, packagePath);
                    setCommandResult(!hexValMatchesCustom(path, getUnquoted(cmd, 2), getUnquoted(cmd, 3), getUnquoted(cmd, 4)));
                } else { setCommandFailed(); }
                return;
            }
            if (commandName == "!matching_ini_val") {
                // !matching_ini_val <path> <section> <key> <value...>
                if (cmdSize >= 5) {
                    std::string path = cmd[1];
                    preprocessPath(path, packagePath);
                    std::string expected = getUnquoted(cmd, 4);
                    for (size_t i = 5; i < cmdSize; ++i) { expected += ' '; expected += getUnquoted(cmd, i); }
                    std::string actual = parseValueFromIniSection(path, getUnquoted(cmd, 2), getUnquoted(cmd, 3));
                    removeQuotes(actual);
                    setCommandResult(actual != expected);
                } else { setCommandFailed(); }
                return;
            }
            if (commandName == "!module_exists") {
                setCommandResult(cmdSize >= 2 && !moduleExists(parseTid(cmd[1])));
                return;
            }
            if (commandName == "!module_is_active") {
                setCommandResult(cmdSize >= 2 && !moduleIsActive(parseTid(cmd[1])));
                return;
            }
            if (commandName == "!path_exists") {
                if (cmdSize >= 2) {
                    std::string sourcePath = cmd[1];
                    preprocessPath(sourcePath, packagePath);
                    setCommandResult(!ult::isFileOrDirectory(sourcePath));
                }
                return;
            }
    }
}



// ---------------------------------------------------------------------------
// Menu condition helpers
//
// Backing checks and the evaluator for the ;visibility_condition= and
// ;toggle_state_condition= option directives. A condition is a single string
// of the form "<mode> <args...>" with these modes:
//
//   path_exists       <path>
//   ipc_exists        <service_name>
//   module_exists     <TID>
//   module_is_active  <TID>
//   matching_txt_line     <path> <line>
//   matching_hex_val      <path> <offset> <hex>
//   matching_hex_val_custom <path> <ascii_pattern> <offset> <hex>
//   matching_ini_val      <path> <section> <key> <value>
//
// matching_hex_val_custom resolves <offset> relative to the file offset of
// <ascii_pattern> (e.g. "CUST"), via the same magic-scan + hexSumCache that
// hex-by-custom-offset uses for writes -- see hexValMatchesCustom() below.
//
// Prefix any mode with ! to negate: !path_exists, !module_exists, etc.
//
// The final free-text field (line / value / hex) keeps any embedded spaces.
// ---------------------------------------------------------------------------

// Returns true when an exact line (trailing CR/LF stripped) exists in a file.
// Full-line comparison: a target that is only a substring of a longer line does
// not match. Lines longer than the read buffer are skipped, not partially
// compared.
inline bool txtLineExists(const std::string& filePath, const std::string& line) {
    if (!isFile(filePath)) return false;
    FILE* file = fopen(filePath.c_str(), "r");
    if (!file) return false;

    char buffer[1024];
    bool found = false;
    bool skipUntilNewline = false;
    while (fgets(buffer, sizeof(buffer), file)) {
        size_t len = std::strlen(buffer);
        const bool hasNewline = (len > 0 && buffer[len - 1] == '\n');

        if (skipUntilNewline) {
            // Previous chunk was a partial read of an over-long line; skip the rest.
            skipUntilNewline = !hasNewline;
            continue;
        }
        if (!hasNewline && !feof(file)) {
            // Line is longer than the buffer; skip the remainder and do not compare.
            skipUntilNewline = true;
            continue;
        }

        // Strip trailing CR/LF.
        while (len > 0 && (buffer[len - 1] == '\n' || buffer[len - 1] == '\r')) {
            buffer[--len] = '\0';
        }

        if (line.size() == len && std::memcmp(buffer, line.c_str(), len) == 0) {
            found = true;
            break;
        }
    }
    fclose(file);
    return found;
}

// Returns true when the bytes at offset match expectedHex. The hex string is
// validated (even length, hex digits only, spaces ignored) before the file is
// opened, so malformed input simply returns false.
inline bool hexValMatches(const std::string& filePath, uint32_t offset, std::string expectedHex) {
    if (!isFile(filePath)) return false;

    // Strip spaces in-place, then validate length and characters.
    size_t w = 0;
    for (size_t r = 0; r < expectedHex.size(); ++r) {
        if (expectedHex[r] != ' ') expectedHex[w++] = expectedHex[r];
    }
    expectedHex.resize(w);
    if (expectedHex.empty() || (expectedHex.size() % 2) != 0) return false;
    for (size_t i = 0; i < expectedHex.size(); ++i) {
        if (!std::isxdigit(static_cast<unsigned char>(expectedHex[i]))) return false;
    }

    FILE* file = fopen(filePath.c_str(), "rb");
    if (!file) return false;

    if (fseek(file, static_cast<long>(offset), SEEK_SET) != 0) {
        fclose(file);
        return false;
    }

    const size_t len = expectedHex.size() / 2;
    std::vector<unsigned char> buffer(len);
    const size_t readLen = fread(buffer.data(), 1, len, file);
    fclose(file);

    if (readLen < len) return false;

    for (size_t i = 0; i < len; ++i) {
        unsigned int byte = 0;
        if (std::sscanf(expectedHex.c_str() + i * 2, "%2x", &byte) != 1) return false;
        if (buffer[i] != static_cast<unsigned char>(byte)) return false;
    }
    return true;
}

// Returns true when the bytes at (patternOffset + offset) match expectedHex, where
// patternOffset is the file offset of customAsciiPattern (e.g. "CUST"), resolved via
// the same findHexDataOffsets() magic-scan and hexSumCache that hex-by-custom-offset
// uses for writes -- so this read-side check can never drift out of sync with where
// hex-by-custom-offset actually patches. The hex string is validated (even length,
// hex digits only, spaces ignored) before any file I/O, exactly like hexValMatches().
inline bool hexValMatchesCustom(const std::string& filePath, const std::string& customAsciiPattern,
                                 const std::string& offsetStr, std::string expectedHex) {
    if (!isFile(filePath)) return false;

    // Strip spaces in-place, then validate length and characters.
    size_t w = 0;
    for (size_t r = 0; r < expectedHex.size(); ++r) {
        if (expectedHex[r] != ' ') expectedHex[w++] = expectedHex[r];
    }
    expectedHex.resize(w);
    if (expectedHex.empty() || (expectedHex.size() % 2) != 0) return false;
    for (size_t i = 0; i < expectedHex.size(); ++i) {
        if (!std::isxdigit(static_cast<unsigned char>(expectedHex[i]))) return false;
    }

    const size_t len = expectedHex.size() / 2;
    const std::string actual = parseHexDataAtCustomOffset(filePath, customAsciiPattern, offsetStr, len);
    if (actual.size() != expectedHex.size()) return false;

    for (size_t i = 0; i < len; ++i) {
        unsigned int a = 0, b = 0;
        if (std::sscanf(actual.c_str() + i * 2, "%2x", &a) != 1) return false;
        if (std::sscanf(expectedHex.c_str() + i * 2, "%2x", &b) != 1) return false;
        if (a != b) return false;
    }
    return true;
}

// Evaluates a "<mode> <args...>" condition string. Returns true when satisfied.
// Used by both ;visibility_condition= (true -> shown) and
// ;toggle_state_condition= (true -> ON). An empty condition returns true; an
// unknown mode or missing argument returns false.
inline bool evaluateMenuCondition(std::string condition, const std::string& packagePath) {
    removeQuotes(condition);
    replacePlaceholdersInArg(condition, generalPlaceholders);

    size_t pos = 0;
    const size_t end = condition.size();

    // Pull the next space-delimited token starting at pos.
    auto nextToken = [&]() -> std::string {
        while (pos < end && condition[pos] == ' ') ++pos;
        const size_t start = pos;
        while (pos < end && condition[pos] != ' ') ++pos;
        return condition.substr(start, pos - start);
    };
    // The remaining text after pos, leading spaces trimmed (free-text field).
    auto remainder = [&]() -> std::string {
        while (pos < end && condition[pos] == ' ') ++pos;
        std::string rest = condition.substr(pos);
        removeQuotes(rest);
        return rest;
    };

    std::string mode = nextToken();
    if (mode.empty()) return true;

    const bool negate = !mode.empty() && mode[0] == '!';
    if (negate) mode.erase(0, 1);
    if (mode.empty()) return true;

    if (mode == "path_exists") {
        std::string path = nextToken();
        if (path.empty()) return false;
        preprocessPath(path, packagePath);
        return negate ^ isFileOrDirectory(path);
    }
    if (mode == "ipc_exists") {
        const std::string service = nextToken();
        if (service.empty()) return false;
        Handle handle = INVALID_HANDLE;
        const bool registered = R_SUCCEEDED(
            smGetServiceOriginal(&handle, smEncodeName(service.c_str())));
        if (registered) svcCloseHandle(handle);
        return negate ^ registered;
    }
    if (mode == "module_exists") {
        const std::string tid = nextToken();
        if (tid.empty()) return false;
        return negate ^ moduleExists(parseTid(tid));
    }
    if (mode == "module_is_active") {
        const std::string tid = nextToken();
        if (tid.empty()) return false;
        return negate ^ moduleIsActive(parseTid(tid));
    }
    if (mode == "matching_txt_line") {
        std::string path = nextToken();
        const std::string line = remainder();
        if (path.empty() || line.empty()) return false;
        preprocessPath(path, packagePath);
        return negate ^ txtLineExists(path, line);
    }
    if (mode == "matching_hex_val") {
        std::string path = nextToken();
        const std::string offsetStr = nextToken();
        const std::string hex = remainder();
        if (path.empty() || offsetStr.empty() || hex.empty()) return false;
        preprocessPath(path, packagePath);
        uint32_t offset = 0;
        if (isValidNumber(offsetStr))
            offset = static_cast<uint32_t>(std::strtoul(offsetStr.c_str(), nullptr, 0));
        return negate ^ hexValMatches(path, offset, hex);
    }
    if (mode == "matching_hex_val_custom") {
        std::string path = nextToken();
        std::string pattern = nextToken();
        std::string offsetStr = nextToken();
        const std::string hex = remainder();
        if (path.empty() || pattern.empty() || offsetStr.empty() || hex.empty()) return false;
        preprocessPath(path, packagePath);
        return negate ^ hexValMatchesCustom(path, pattern, offsetStr, hex);
    }
    if (mode == "matching_ini_val") {
        std::string path = nextToken();
        const std::string section = nextToken();
        const std::string key = nextToken();
        const std::string expected = remainder();
        if (path.empty() || section.empty() || key.empty()) return false;
        preprocessPath(path, packagePath);
        std::string currentVal = parseValueFromIniSection(path, section, key);
        removeQuotes(currentVal);
        return negate ^ (currentVal == expected);
    }
    return negate ^ false;
}


// Thread information structure
Thread interpreterThread;
std::atomic<bool> interpreterThreadExit{false};

// Cache for stack size to avoid repeated INI parsing
static int cachedStackSize = 0;

// Work data structure to pass to thread
struct InterpreterWorkData {
    std::vector<std::vector<std::string>> commands;
    std::string packagePath;
    std::string selectedCommand;
    
    InterpreterWorkData(std::vector<std::vector<std::string>>&& cmds, 
                       const std::string& path, 
                       const std::string& selected)
        : commands(std::move(cmds)), packagePath(path), selectedCommand(selected) {}
};

inline void clearInterpreterFlags(bool state = false) {
    // Use relaxed ordering for simple flag clearing - these are just state flags
    // and don't need acquire-release synchronization
    abortDownload.store(state, std::memory_order_relaxed);
    abortUnzip.store(state, std::memory_order_relaxed);
    abortFileOp.store(state, std::memory_order_relaxed);
    abortCommand.store(state, std::memory_order_relaxed);
}

void backgroundInterpreter(void* workPtr) {
    // Get work data directly - no queue needed
    auto workData = static_cast<InterpreterWorkData*>(workPtr);
    
    // Check for exit signal before starting work
    if (interpreterThreadExit.load(std::memory_order_acquire)) {
        delete workData;
        return;
    }
    

    // Process the work if we have commands
    if (!workData->commands.empty()) {
        // Clear flags and setup for execution
        clearInterpreterFlags();
        resetPercentages();
        threadFailure.store(false, std::memory_order_release);
        
        runningInterpreter.store(true, std::memory_order_release);
        
        // Execute the commands
        interpretAndExecuteCommands(std::move(workData->commands), 
                                   std::move(workData->packagePath), 
                                   std::move(workData->selectedCommand));

        // Brief sleep before cleanup
        svcSleepThread(200'000'000);

        // Cleanup
        runningInterpreter.store(false, std::memory_order_release);
        clearInterpreterFlags();
        resetPercentages();
    }
    
    // Clean up work data
    delete workData;
    
    if (ult::isHidden.load(std::memory_order_acquire)) {
        if (tsl::notification && ult::useNotifications) {
            if (commandSuccess.load(std::memory_order_acquire))
                tsl::notification->showNow(NOTIFY_HEADER + TASK_IS_COMPLETE);
            else
                tsl::notification->showNow(NOTIFY_HEADER + TASK_HAS_FAILED);
        }
    }

    // Thread naturally exits here
}

void closeInterpreterThread() {
    // Signal any running thread to exit
    interpreterThreadExit.store(true, std::memory_order_release);
    
    // Wait for thread to finish and clean up
    threadWaitForExit(&interpreterThread);
    threadClose(&interpreterThread);
    
    // Reset state
    clearInterpreterFlags();
    interpreterThreadExit.store(false, std::memory_order_release);
}

int getInterpreterStackSize(const std::string& packagePath = "") {
    #if USING_LOGGING_DIRECTIVE
    if (!packagePath.empty()) {
        disableLogging = !(parseValueFromIniSection(PACKAGES_INI_FILEPATH, getNameFromPath(packagePath), USE_LOGGING_STR) == TRUE_STR);
        logFilePath = packagePath + "log.txt";
    }
    #endif
    // Cache stack size parsing to avoid repeated INI file access
    if (cachedStackSize == 0) {
        std::string interpreterHeap = parseValueFromIniSection(ULTRAHAND_CONFIG_INI_PATH, MEMORY_STR, "interpreter_heap");

        if (!interpreterHeap.empty()) {
            // Strip optional "0x" or "0X" prefix
            if (interpreterHeap.size() > 2 && interpreterHeap[0] == '0' &&
                (interpreterHeap[1] == 'x' || interpreterHeap[1] == 'X')) {
                interpreterHeap = interpreterHeap.substr(2);
            }

            // Validate remaining string is hex
            bool validHex = !interpreterHeap.empty();
            for (char c : interpreterHeap) {
                if (!std::isxdigit(static_cast<unsigned char>(c))) {
                    validHex = false;
                    break;
                }
            }

            if (validHex) {
                cachedStackSize = ult::stoi(interpreterHeap, nullptr, 16);  // Convert from base 16
            } else {
                cachedStackSize = 0x8000;  // Default if invalid
            }
        } else {
            cachedStackSize = 0x8000;  // Default if empty
        }
    }

    return cachedStackSize;
}



// Combined function - creates thread with work data directly
void executeInterpreterCommands(std::vector<std::vector<std::string>>&& commands, 
                               const std::string& packagePath = "", 
                               const std::string& selectedCommand = "") {
    
    // Wait for the existing thread to finish
    threadWaitForExit(&interpreterThread);
    threadClose(&interpreterThread);

    // Early exit if no commands
    if (commands.empty()) {
        return;
    }

    if (ult::useSoundEffects) {
        if (triggerEnterSound.exchange(false)) {
            ult::Audio::playEnterSound();
        } else if (triggerOnSound.exchange(false)) {
            ult::Audio::playOnSound();
        } else if (triggerOffSound.exchange(false)) {
            ult::Audio::playOffSound();
        }

        ult::Audio::unloadAllSounds({ult::Audio::SoundType::Wall, ult::Audio::SoundType::Notification});
    }
    
    // Get stack size and setup logging
    const int stackSize = getInterpreterStackSize(packagePath);
    
    // Ensure exit flag is clear before starting
    interpreterThreadExit.store(false, std::memory_order_release);
    
    // Create work data (will be cleaned up by the thread)
    auto workData = new InterpreterWorkData(std::move(commands), packagePath, selectedCommand);
    
    // Create and start thread directly with work data
    const int result = threadCreate(&interpreterThread, backgroundInterpreter, workData, nullptr, stackSize, 0x2B, -2);
    if (result != 0) {
        // Handle thread creation failure
        setCommandFailed();
        clearInterpreterFlags();
        runningInterpreter.store(false, std::memory_order_release);
        
        // Clean up work data since thread won't
        delete workData;
        
        #if USING_LOGGING_DIRECTIVE
        if (!disableLogging)
            logMessage("Failed to create interpreter thread.");
        logFilePath = defaultLogFilePath;
        disableLogging = true;
        #endif
        return;
    }
    threadStart(&interpreterThread);
}
