[Lua Snippet] Load saved skin from RedMenu

This is a conversion of LoadDefaultPed function from RedMenu (source), written in C#, into Lua.

Command usage: /loadskin [idx]

StorageManager = {}

function StorageManager.TryGet(key)
    return GetExternalKvpInt('RedMenu', 'int_' .. key)
end

function StorageManager.TryGetFloat(key)
    return GetExternalKvpFloat('RedMenu', 'float_' .. key)
end

function tableContains(tbl, value)
    for k,v in ipairs(tbl) do
        if v == value then
            return true
        end
    end
    return false
end

currentMpClothes = {}
for i = 1, 39 do
    currentMpClothes[i] = 0
end

currentSpClothes = {}
for i = 1, 40 do
    currentSpClothes[i] = 0
end

FacialFeatureData = {}
FacialFeatureData.FacialFeatures = {
    { Name = "Head Width",       Hash = 0x84D6, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Eyebrow Height",   Hash = 0x3303, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Eyebrow Width",    Hash = 0x2FF9, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Eyebrow Depth",    Hash = 0x4AD1, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Ear Width",        Hash = 0xC04F, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Ear Angle",        Hash = 0xB6CE, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Ear Height",       Hash = 0x2844, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Earlobe Size",     Hash = 0xED30, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Cheekbone Height", Hash = 0x6A0B, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Cheekbone Width",  Hash = 0xABCF, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Cheekbone Depth",  Hash = 0x358D, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Jaw Height",       Hash = 0x8D0A, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Jaw Width",        Hash = 0xEBAE, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Jaw Depth",        Hash = 0x1DF6, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Chin Height",      Hash = 0x3C0F, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Chin Width",       Hash = 0xC3B2, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Chin Depth",       Hash = 0xE323, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Eyelid Height",    Hash = 0x8B2B, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Eyelid Width",     Hash = 0x1B6B, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Eye Depth",        Hash = 0xEE44, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Eye Angle",        Hash = 0xD266, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Eye Distance",     Hash = 0xA54E, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Eye Height",       Hash = 0xDDFB, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Nose Width",       Hash = 0x6E7F, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Nose Size",        Hash = 0x3471, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Nose Height",      Hash = 0x03F5, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Nose Angle",       Hash = 0x34B1, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Nose Curvature",   Hash = 0xF156, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Nostril Distance", Hash = 0x561E, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Mouth Width",      Hash = 0xF065, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Mouth Depth",      Hash = 0xAA69, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Mouth X Pos",      Hash = 0x7AC3, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Mouth Y Pos",      Hash = 0x410D, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Upper Lip Height", Hash = 0x1A00, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Upper Lip Width",  Hash = 0x91C1, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Upper Lip Depth",  Hash = 0xC375, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Lower Lip Height", Hash = 0xBB4D, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Lower Lip Width",  Hash = 0xB0B0, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 },
    { Name = "Lower Lip Depth",  Hash = 0x5D16, Min = -10, Max = 10, Step = 1, Default = 0, Increment = 0.1 }
}


currentFacialFeatures = {}
for k,v in ipairs(FacialFeatureData.FacialFeatures) do
    currentFacialFeatures[k] = 0;
end

currentBodySettings = {}
for i = 1, 2 do
    currentBodySettings[i] = 0;
end

function LoadDefaultPed(pedIndex)
    local outfit = 0
    local model = 0

    local outfitKey = "SavedPeds_" .. pedIndex .. "_outfit"
    local modelKey = "SavedPeds_" .. pedIndex .. "_model"

    outfit = StorageManager.TryGet(outfitKey)
    if not outfit then
        outfit = 0
    end

    model = StorageManager.TryGet(modelKey)

    if IsModelInCdimage(model) then
        RequestModel(model, false)
        while not HasModelLoaded(model) do
            Citizen.Wait(0)
        end
        SetPlayerModel(PlayerId(), model, 0)
        Citizen.InvokeNative(0x77FF8D35EEC6BBC4, PlayerPedId(), outfit, 0)
        SetModelAsNoLongerNeeded(model)

        if model == GetHashKey("mp_male") then
            local ammoComponents = {0xDA0E2C55, 0x3F1F01E5}
            for _, component in pairs(ammoComponents) do
                Citizen.InvokeNative(0xD710A5007C2AC539, PlayerPedId(), component, 0)
            end
            Citizen.InvokeNative(0xCC8CA3E88256E58F, PlayerPedId(), false, true, true, true, false)
        end
    else
        print("^1[ERROR] This ped model is not present in the game files " .. model .. ".^7")
    end

    Citizen.Wait(500)

    Citizen.Wait(0)

    local mpClothesKeys = {}
    for key in pairs(currentMpClothes) do
        table.insert(mpClothesKeys, key)
    end

    for i = 1, #mpClothesKeys do
        local key = mpClothesKeys[i]
        local hash = 0
        local mpKey = "SavedPeds_" .. pedIndex .. "_mp_" .. (key - 1) -- C# Index (from 0)

        local hash = StorageManager.TryGet(mpKey)
        if hash then
            local clothingHashes = {
                0x18729F39, 0x1D4C528A, 0x2026C46D, 0x3107499B, 0x378AD10C,
                0x3C1A74CD, 0x3F7F3587, 0x485EE834, 0x514ADCEA, 0x05E47CA6,
                0x5FC29285, 0x0662AC34, 0x72E6EF74, 0x7505EF42, 0x777EC6EF,
                0x79D7DF96, 0x7A6BBD0B, 0x7A96FACA, 0x7BC10759, 0x823687F5,
                0x83887E88, 0x864B03AE, 0x877A2CF7, 0x91CE9B20, 0x94504D26,
                0x96EDAE5C, 0x9925C067, 0x9B2C8B89, 0xA0E3AB7F, 0xA6D134C6,
                0xAF14310B, 0x0B3966C9, 0xB6B6122D, 0xE06D30CE, 0xEA24B45E,
                0xEABE0032, 0xF1542D11, 0xF16A1D23, 0xF8016BCA, 0xFAE9107F
            }

            if tableContains(clothingHashes, hash) then
                Citizen.InvokeNative(0xD710A5007C2AC539, PlayerPedId(), hash, 0)
                Citizen.InvokeNative(0xCC8CA3E88256E58F, PlayerPedId(), false, true, true, true, false)
            elseif hash ~= 0 then
                Citizen.InvokeNative(0xD3A7B003ED343FD9, PlayerPedId(), hash, true, true, false)
            end
            currentMpClothes[key] = hash
        else
            currentMpClothes[key] = 0
        end
    end

    Citizen.Wait(0)

    local spClothesKeys = {}
    for key in pairs(currentSpClothes) do
        table.insert(spClothesKeys, key)
    end

    for i = 1, #spClothesKeys do
        local key = spClothesKeys[i]
        local hash = 0
        local spKey = "SavedPeds_" .. pedIndex .. "_sp_" .. (key - 1) -- C# Index (from 0)

        hash = StorageManager.TryGet(spKey)
        if hash then
            local clothingHashes = {
                0x15D3C7F2, 0x18729F39, 0x1AECF7DC, 0x1D4C528A, 0x2026C46D,
                0x3107499B, 0x378AD10C, 0x3C1A74CD, 0x3F1F01E5, 0x3F7F3587,
                0x485EE834, 0x49C89D9B, 0x4A73515C, 0x514ADCEA, 0x5FC29285,
                0x76F0E272, 0x777EC6EF, 0x79D7DF96, 0x7A96FACA, 0x7BE77792,
                0x7C00A8F0, 0x823687F5, 0x83887E88, 0x864B03AE, 0x877A2CF7,
                0x8EFB276A, 0x94504D26, 0x96EDAE5C, 0x9925C067, 0x9B2C8B89,
                0xA6D134C6, 0xB3966C9, 0xB6B6122D, 0xB6B63737, 0xB9E2FA01,
                0xDA0E2C55, 0xDB64A390, 0xE06D30CE, 0xEA24B45E, 0xEABE0032,
                0xECC8B25A
            }

            if tableContains(clothingHashes, hash) then
                Citizen.InvokeNative(0xD710A5007C2AC539, PlayerPedId(), hash, 0)
                Citizen.InvokeNative(0xCC8CA3E88256E58F, PlayerPedId(), false, true, true, true, false)
            elseif hash ~= 0 then
                Citizen.InvokeNative(0xD3A7B003ED343FD9, PlayerPedId(), hash, true, false, false)
            end
            currentSpClothes[key] = hash
        else
            currentSpClothes[key] = 0
        end
    end

    Citizen.Wait(0)

    local ffKeys = {}
    for key in pairs(currentFacialFeatures) do
        table.insert(ffKeys, key)
    end

    for i = 1, #ffKeys do
        local key = ffKeys[i]
        local value = 0
        local ffKey = "SavedPeds_" .. pedIndex .. "_ff_" .. (key - 1) -- C# Index (from 0)

        value = StorageManager.TryGetFloat(ffKey)
        if value then
            if value ~= 0 then
                Citizen.InvokeNative(0x5653AB26C82938CF, PlayerPedId(), key, value)
            end
            currentFacialFeatures[key] = value
        else
            currentFacialFeatures[key] = 0
        end
    end

    Citizen.Wait(0)

    local bcKeys = {}
    for key in pairs(currentBodySettings) do
        table.insert(bcKeys, key)
    end

    for i = 1, #bcKeys do
        local key = bcKeys[i]
        local hash = 0
        local bcKey = "SavedPeds_" .. pedIndex .. "_bc_" .. (key - 1) -- C# Index (from 0)

        hash = StorageManager.TryGet(bcKey)
        if hash then
            if hash ~= 0 then
                Citizen.InvokeNative(0x1902C4CFCC5BE57C, PlayerPedId(), hash)
            end
            currentBodySettings[key] = hash
        else
            currentBodySettings[key] = 0
        end
    end

    Citizen.InvokeNative(0xCC8CA3E88256E58F, PlayerPedId(), false, true, true, true, false)
end

RegisterCommand('loadskin', function(source, args)
    LoadDefaultPed(tonumber(args[1]) or 1)
end)