Русский текст при загрузке названии файла в меню IMGUI

ReoGenT

Участник
Автор темы
90
6
Столкнулся с проблемой: Пытаюсь загрузить в меню IMGUI русское название файла, появляются знаки вопроса: (Занялся переводом SA Wiki)

sa-mp-030.png


Скриншот 03-05-2022 133311.png

Lua:
local encoding = require 'encoding'
local inicfg = require 'inicfg'
local imgui = require 'imgui'
local key = require 'vkeys'

local config_path = 'moonloader/sa wiki/settings.cfg'
local config = inicfg.load(nil, config_path)
local settings = config.general
local info = [[
Автор/Создатель: LUCHARE
Перевод сделал: Fox_Yotanhaim
]]

imgui.RenderInMenu = settings.render_in_menu
imgui.LockPlayer = settings.lock_player

encoding.default = 'CP1251'
local u8 = encoding.UTF8
local ImVec2 = imgui.ImVec2
local ImBool = imgui.ImBool
local ImBuffer = imgui.ImBuffer

function StyleColorsDark()
    imgui.SwitchContext()
    local style = imgui.GetStyle()
    local colors = style.Colors
    local clr = imgui.Col
    local ImVec4 = imgui.ImVec4

    style.WindowRounding = 2.0
    style.WindowTitleAlign = ImVec2(0.5, 0.5)
    style.ChildWindowRounding = 2.0
    style.FrameRounding = 0
    style.ItemSpacing = ImVec2(5.0, 4.0)
    style.ScrollbarSize = 11
    style.ScrollbarRounding = 16
    style.GrabMinSize = 8.0
    style.GrabRounding = 1.0
    -- style.Alpha =
    -- style.WindowPadding =
    -- style.WindowMinSize =
    -- style.FramePadding =
    -- style.ItemInnerSpacing =
    -- style.TouchExtraPadding =
    -- style.IndentSpacing =
    -- style.ColumnsMinSpacing = ?
    -- style.ButtonTextAlign =
    -- style.DisplayWindowPadding =
    -- style.DisplaySafeAreaPadding =
    -- style.AntiAliasedLines =
    -- style.AntiAliasedShapes =
    -- style.CurveTessellationTol =

    colors[clr.Text]                   = ImVec4(1.00, 1.00, 1.00, 1.00)
    colors[clr.TextDisabled]           = ImVec4(0.50, 0.50, 0.50, 1.00)
    colors[clr.WindowBg]               = ImVec4(0.06, 0.06, 0.06, 0.94)
    colors[clr.ChildWindowBg]          = ImVec4(1.00, 1.00, 1.00, 0.00)
    colors[clr.PopupBg]                = ImVec4(0.08, 0.08, 0.08, 0.94)
    colors[clr.ComboBg]                = colors[clr.PopupBg]
    colors[clr.Border]                 = ImVec4(0.43, 0.43, 0.50, 0.50)
    colors[clr.BorderShadow]           = ImVec4(0.00, 0.00, 0.00, 0.00)
    colors[clr.FrameBg]                = ImVec4(0.16, 0.29, 0.48, 0.54)
    colors[clr.FrameBgHovered]         = ImVec4(0.26, 0.59, 0.98, 0.40)
    colors[clr.FrameBgActive]          = ImVec4(0.26, 0.59, 0.98, 0.67)
    colors[clr.TitleBg]                = ImVec4(0.04, 0.04, 0.04, 1.00)
    colors[clr.TitleBgActive]          = ImVec4(0.16, 0.29, 0.48, 1.00)
    colors[clr.TitleBgCollapsed]       = ImVec4(0.00, 0.00, 0.00, 0.51)
    colors[clr.MenuBarBg]              = ImVec4(0.14, 0.14, 0.14, 1.00)
    colors[clr.ScrollbarBg]            = ImVec4(0.02, 0.02, 0.02, 0.53)
    colors[clr.ScrollbarGrab]          = ImVec4(0.31, 0.31, 0.31, 1.00)
    colors[clr.ScrollbarGrabHovered]   = ImVec4(0.41, 0.41, 0.41, 1.00)
    colors[clr.ScrollbarGrabActive]    = ImVec4(0.51, 0.51, 0.51, 1.00)
    colors[clr.CheckMark]              = ImVec4(0.26, 0.59, 0.98, 1.00)
    colors[clr.SliderGrab]             = ImVec4(0.24, 0.52, 0.88, 1.00)
    colors[clr.SliderGrabActive]       = ImVec4(0.26, 0.59, 0.98, 1.00)
    colors[clr.Button]                 = ImVec4(0.26, 0.59, 0.98, 0.40)
    colors[clr.ButtonHovered]          = ImVec4(0.26, 0.59, 0.98, 1.00)
    colors[clr.ButtonActive]           = ImVec4(0.06, 0.53, 0.98, 1.00)
    colors[clr.Header]                 = ImVec4(0.26, 0.59, 0.98, 0.31)
    colors[clr.HeaderHovered]          = ImVec4(0.26, 0.59, 0.98, 0.80)
    colors[clr.HeaderActive]           = ImVec4(0.26, 0.59, 0.98, 1.00)
    colors[clr.Separator]              = colors[clr.Border]
    colors[clr.SeparatorHovered]       = ImVec4(0.26, 0.59, 0.98, 0.78)
    colors[clr.SeparatorActive]        = ImVec4(0.26, 0.59, 0.98, 1.00)
    colors[clr.ResizeGrip]             = ImVec4(0.26, 0.59, 0.98, 0.00)
    colors[clr.ResizeGripHovered]      = ImVec4(0.26, 0.59, 0.98, 0.00)
    colors[clr.ResizeGripActive]       = ImVec4(0.26, 0.59, 0.98, 0.00)
    colors[clr.CloseButton]            = ImVec4(0.41, 0.41, 0.41, 0.50)
    colors[clr.CloseButtonHovered]     = ImVec4(0.98, 0.39, 0.36, 1.00)
    colors[clr.CloseButtonActive]      = ImVec4(0.98, 0.39, 0.36, 1.00)
    colors[clr.PlotLines]              = ImVec4(0.61, 0.61, 0.61, 1.00)
    colors[clr.PlotLinesHovered]       = ImVec4(1.00, 0.43, 0.35, 1.00)
    colors[clr.PlotHistogram]          = ImVec4(0.90, 0.70, 0.00, 1.00)
    colors[clr.PlotHistogramHovered]   = ImVec4(1.00, 0.60, 0.00, 1.00)
    colors[clr.TextSelectedBg]         = ImVec4(0.26, 0.59, 0.98, 0.35)
    colors[clr.ModalWindowDarkening]   = ImVec4(0.80, 0.80, 0.80, 0.35)
end

function range(min, max)
    return {min = min, max = max}
end

function _load_text(file)
    if doesFileExist(file) then
        local out = {}
        local i = 1
        for line in io.lines(file) do
            out[i] = line:gsub('\\n', '\n')
            i = i + 1
        end
        return out
    else
        error(('File "%s" does not exist'):format(file))
    end
end

function load_text(file)
    file = 'moonloader/sa wiki/text/' .. file
    return _load_text(file)
end

function load_text_dir(dir)
    local out = {}
    local dir = 'moonloader/sa wiki/text/' .. dir
    local search, file = findFirstFile(dir .. '/*.txt')
    while file do
        out[file:gsub('.txt', '')] = _load_text(dir .. '/' .. file)
        file = findNextFile(search)
    end
    findClose(search)
    return out
end

function load_pics(folder, r)
    local out = {}
    for i = r.min, r.max do
        local file = ('moonloader/sa wiki/pic/%s/%d.png'):format(folder, i)
        if doesFileExist(file) then
            out[i] = imgui.CreateTextureFromFile(file)
        else
            print(('Attention! File "%s" does not exist'):format(file))
        end
    end
    return out
end

function _create_window(caption, text, img, imgsz)
    local window = {
        render = ImBool(false);
        input = ImBuffer('', 512);
        selected = -1;
        caption = caption;
        text = text;
        img = img;
        imgsz = imgsz;
    }

    function window:toggle()
        self.render.v = not self.render.v
    end

    function window:draw_text(t, imgs)
        for k, v in pairs(t) do
            local _type = type(v)
            if _type == 'table' then
                if imgui.TreeNode(k) then
                    self:draw_text(v, self.img[k])
                    imgui.TreePop()
                end
            elseif _type == 'string' then
                if v:lower():find(self.input.v:lower()) then
                    imgui.BulletText(u8(v))
                    if imgui.IsItemClicked(1) then self.selected = k end
                    if k == self.selected then
                        if imgui.BeginPopupContextItem('##context', 1) then
                            if imgui.Selectable('Copy') then
                                imgui.SetClipboardText(v)
                                self.selected = -1
                                imgui.CloseCurrentPopup()
                            end
                            local id = v:match('ID:%s*(%d+)')
                            if id ~= nil then
                                if imgui.Selectable('Copy ID') then
                                    imgui.SetClipboardText(id)
                                    self.selected = -1
                                    imgui.CloseCurrentPopup()
                                end
                            end
                            imgui.EndPopup()
                        end
                    end
                    if imgui.IsItemHovered()  then
                        if self.img ~= nil then
                            local id = tonumber(v:match('ID:%s*(%d+)')) or k
                            local img = (imgs or self.img)[id]
                            imgui.BeginTooltip()
                            if img ~= nil then
                                imgui.Image(img, self.imgsz)
                            else
                                imgui.TextDisabled('Нет картинки')
                            end
                            imgui.EndTooltip()
                        end
                    end
                    imgui.Separator()
                end
            end
        end
    end

    function window:draw()
        if self.render.v then
            imgui.SetNextWindowSize(ImVec2(460, 500), imgui.Cond.FirstUseEver)
            imgui.Begin(self.caption, self.render)
            imgui.InputText('Поиск', self.input)
            self:draw_text(self.text, self.img)
            imgui.End()
        end
    end

    return window
end

function create_window(name, imgrange, imgsz)
    local _name = name:lower()
    return _create_window(name, load_text(_name .. '.txt'), load_pics(_name, imgrange), imgsz)
end

local menu = {
    ['Скины'] = _create_window('Скины', load_text('peds.txt') ,load_pics('peds', range(0, 311)), ImVec2(55, 100));
    ['Оружие'] = _create_window('Оружие', load_text('weapons.txt') ,load_pics('weapons', range(0, 46)), ImVec2(200, 200));
    ['Транспорт'] = _create_window('Транспорт', load_text_dir('vehicles'), load_pics('vehicles', range(400, 611)), ImVec2(204, 125));
    ['Локации'] = _create_window(
        'Локации',
        load_text_dir('locations'),
        {
            ['Oysters'] = load_pics('oysters', range(1, 50));
            ['Snapshots'] = load_pics('snapshots', range(1, 50));
            ['Horseshoes'] = load_pics('horseshoes', range(1, 50));
        },
        ImVec2(225, 169)
    );
    ['Чит-Коды'] = _create_window('Чит-Коды', load_text('cheat-codes.txt'));
}

StyleColorsDark()

local draw = ImBool(false)
local draw_info = ImBool(false)
local draw_settings = ImBool(false)
local settings_input_key = ImBuffer(settings.key, 64)
local settings_cb_lock_player = ImBool(settings.lock_player)
local settings_cb_render_in_menu = ImBool(settings.render_in_menu)

function imgui.OnDrawFrame()
    if draw.v then
        imgui.SetNextWindowSize(ImVec2(250, 205))
        imgui.Begin('Админ-Вики', draw, imgui.WindowFlags.NoResize)
        for k, v in pairs(menu) do
            if imgui.Button(k, ImVec2(-0.1, 20)) then
                v:toggle()
            end
            v:draw()
        end
        imgui.Separator()
        if imgui.Button('Настройки', ImVec2(-0.1, 0)) then
            draw_settings.v = not draw_settings.v
        end
        if draw_settings.v then
            imgui.Begin('Настройки', draw_settings)
            if imgui.InputText('Ключ', settings_input_key, imgui.InputTextFlags.EnterReturnsTrue) then
                local inpt = settings_input_key.v
                if key.name_to_id(inpt, false) ~= nil then
                    settings.key = inpt
                else
                    settings_input_key.v = '<неправильный ключ>'
                end
            end
            if imgui.Checkbox('Рендер в меню', settings_cb_render_in_menu) then
                local state = settings_cb_render_in_menu.v
                settings.render_in_menu = state
                imgui.RenderInMenu = state
            end
            if imgui.Checkbox('Заблокировать игрока', settings_cb_lock_player) then
                local state = settings_cb_lock_player.v
                settings.lock_player = state
                imgui.LockPlayer = state
            end
            imgui.End()
        end
        if imgui.Button('Инфо', ImVec2(-0.1, 0)) then
            draw_info.v = not draw_info.v
        end
        if draw_info.v then
            imgui.Begin('Инфо', draw_info)
            imgui.Text(info)
            imgui.End()
        end
        imgui.End()
    end
end

function onWindowMessage(msg, wparam, lparam)
    if msg == 0x101 then
        if wparam == key.name_to_id(settings.key) then
            draw.v = not draw.v
        end
    end
end

function main()
    while true do
        imgui.Process = draw.v; wait(0)
    end
end

function onScriptTerminate(script, quitGame)
    if script == thisScript() then
        inicfg.save(config, config_path)
    end
end
 

ReoGenT

Участник
Автор темы
90
6
блять, я тебе выше пример написал
Lua:
function create_window(name, imgrange, imgsz)
    local _name = name:lower()
    return _create_window(name, load_text(_name .. '.txt'), load_pics(_name, imgrange), imgsz)
end

local menu = {
    ['Скины'] = _create_window('Скины', load_text('peds.txt') ,load_pics('peds', range(0, 311)), ImVec2(55, 100));
    ['Оружие'] = _create_window('Оружие', load_text('weapons.txt') ,load_pics('weapons', range(0, 46)), ImVec2(200, 200));
    ['Транспорт'] = _create_window('Транспорт', load_text_dir('vehicles'), load_pics('vehicles', range(400, 611)), ImVec2(204, 125));
    ['Локации'] = _create_window(
        'Локации',
        load_text_dir('locations'),
        {
            ['Oysters'] = load_pics('oysters', range(1, 50));
            ['Snapshots'] = load_pics('snapshots', range(1, 50));
            ['Horseshoes'] = load_pics('horseshoes', range(1, 50));
        },
        ImVec2(225, 169)
    );
    ['Чит-Коды'] = _create_window('Чит-Коды', load_text('cheat-codes.txt'));
}

ГДЕ блять я тебе тут сделаю твой стринг, если сука файлы находятся в папке Vehicle и эти файлы не указаны в коде, указана только папка нахуй.
 

Rice.

https://t.me/riceoff
Модератор
1,691
1,436
Lua:
function create_window(name, imgrange, imgsz)
    local _name = name:lower()
    return _create_window(name, load_text(_name .. '.txt'), load_pics(_name, imgrange), imgsz)
end

local menu = {
    ['Скины'] = _create_window('Скины', load_text('peds.txt') ,load_pics('peds', range(0, 311)), ImVec2(55, 100));
    ['Оружие'] = _create_window('Оружие', load_text('weapons.txt') ,load_pics('weapons', range(0, 46)), ImVec2(200, 200));
    ['Транспорт'] = _create_window('Транспорт', load_text_dir('vehicles'), load_pics('vehicles', range(400, 611)), ImVec2(204, 125));
    ['Локации'] = _create_window(
        'Локации',
        load_text_dir('locations'),
        {
            ['Oysters'] = load_pics('oysters', range(1, 50));
            ['Snapshots'] = load_pics('snapshots', range(1, 50));
            ['Horseshoes'] = load_pics('horseshoes', range(1, 50));
        },
        ImVec2(225, 169)
    );
    ['Чит-Коды'] = _create_window('Чит-Коды', load_text('cheat-codes.txt'));
}

ГДЕ блять я тебе тут сделаю твой стринг, если сука файлы находятся в папке Vehicle и эти файлы не указаны в коде, указана только папка нахуй.
Lua:
        for k, v in pairs(menu) do
            if imgui.Button(u8(k), ImVec2(-0.1, 20)) then
                v:toggle()
            end
            v:draw()
        end
 

ReoGenT

Участник
Автор темы
90
6
Lua:
        for k, v in pairs(menu) do
            if imgui.Button(u8(k), ImVec2(-0.1, 20)) then
                v:toggle()
            end
            v:draw()
        end

Код:
[ML] (error) sa wiki.lua: ...rogram Files\GTA San Andreas\moonloader\lib\encoding.lua:60: assertion failed!
stack traceback:
    [C]: in function 'assert'
    ...rogram Files\GTA San Andreas\moonloader\lib\encoding.lua:60: in function 'u8'
    ...OLD\Program Files\GTA San Andreas\moonloader\sa wiki.lua:260: in function 'OnDrawFrame'
    ...D\Program Files\GTA San Andreas\moonloader\lib\imgui.lua:1378: in function <...D\Program Files\GTA San Andreas\moonloader\lib\imgui.lua:1367>
[ML] (error) sa wiki.lua: Script died due to an error. (01CFC14C)
 

why ega

РП игрок
Модератор
2,547
2,238
Lua:
function create_window(name, imgrange, imgsz)
    local _name = name:lower()
    return _create_window(name, load_text(_name .. '.txt'), load_pics(_name, imgrange), imgsz)
end

local menu = {
    ['Скины'] = _create_window('Скины', load_text('peds.txt') ,load_pics('peds', range(0, 311)), ImVec2(55, 100));
    ['Оружие'] = _create_window('Оружие', load_text('weapons.txt') ,load_pics('weapons', range(0, 46)), ImVec2(200, 200));
    ['Транспорт'] = _create_window('Транспорт', load_text_dir('vehicles'), load_pics('vehicles', range(400, 611)), ImVec2(204, 125));
    ['Локации'] = _create_window(
        'Локации',
        load_text_dir('locations'),
        {
            ['Oysters'] = load_pics('oysters', range(1, 50));
            ['Snapshots'] = load_pics('snapshots', range(1, 50));
            ['Horseshoes'] = load_pics('horseshoes', range(1, 50));
        },
        ImVec2(225, 169)
    );
    ['Чит-Коды'] = _create_window('Чит-Коды', load_text('cheat-codes.txt'));
}

ГДЕ блять я тебе тут сделаю твой стринг, если сука файлы находятся в папке Vehicle и эти файлы не указаны в коде, указана только папка нахуй.
перед кириллицой поставь "u8"
 

ReoGenT

Участник
Автор темы
90
6
перед кириллицой поставь "u8"
Код:
[ML] (error) sa wiki.lua: ...rogram Files\GTA San Andreas\moonloader\lib\encoding.lua:60: assertion failed!
stack traceback:
    [C]: in function 'assert'
    ...rogram Files\GTA San Andreas\moonloader\lib\encoding.lua:60: in function 'u8'
    ...OLD\Program Files\GTA San Andreas\moonloader\sa wiki.lua:230: in main chunk
[ML] (error) sa wiki.lua: Script died due to an error. (01C3A60C)