JSon.

Aizen Sosuke

Участник
Автор темы
120
20
Версия MoonLoader
.026-beta
Всем привет. Пытаюсь сохранить скриптом команды в json (в таблицу). Оно сохраняет, но не так, как нужно.
Если заносить данные вот-так:
Lua:
local json_file = getWorkingDirectory()..'\\config\\cmdtools.json'
local list = {
    ['commands'] = {
        cmd = 'bug',
        type_n = 'jail',
        time_n = 5,
        prich = 'Баг инвентаря'
    }
}
То все отлично сохраняет:
1642330168335.png

Но как только я пытаюсь через имгуи окно сделать, то вот что получается:
1642331207662.png

Как я добавляю:
Lua:
if imgui.Button(u8'Создать команду.') then
            local file = io.open('moonloader\\config\\cmdtools.json', "r+")
            local jsonInString = file:read("*a")
            file:close()
            local jsonTable = decodeJson(jsonInString)   
        
            table.insert(list["commands"], {cmd = cmd.v})
            table.insert(list["commands"], {type_n = type_n.v})
            table.insert(list["commands"], {prich = prich.v})
            table.insert(list["commands"], {time_n = time_n.v})
            jsonSave(json_file, list)
            
            sampRegisterChatCommand(cmd.v, function(num)
                sampSendChat(u8:decode(string.format("/%s "..num.." %s %s", type_n.v, time_n.v, prich.v)))
            end)
        end

Что я не так делаю?

Вот весь код:
Lua:
script_name('[ARZ] MiniToolsAdm')
script_author('Haunted_BabyTape')

local imgui = require 'imgui'
local sampev = require('samp.events')
local encoding = require 'encoding'

local main_window_state = imgui.ImBool(false)

local json_file = getWorkingDirectory()..'\\config\\cmdtools.json'
local list = {
    ['commands'] = {
        cmd = 'bug',
        type_n = 'jail',
        time_n = 5,
        prich = 'Баг инвентаря'
    }
}

local cmd = imgui.ImBuffer(256)
local type_n = imgui.ImBuffer(256)
local prich = imgui.ImBuffer(256)
local time_n = imgui.ImInt(0)

encoding.default = 'CP1251'
u8 = encoding.UTF8

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

    colors[clr.Text]   = ImVec4(0.00, 0.00, 0.00, 0.51)
    colors[clr.TextDisabled]   = ImVec4(0.24, 0.24, 0.24, 1.00)
    colors[clr.WindowBg]              = ImVec4(1.00, 1.00, 1.00, 1.00)
    colors[clr.ChildWindowBg]         = ImVec4(0.96, 0.96, 0.96, 1.00)
    colors[clr.PopupBg]               = ImVec4(0.92, 0.92, 0.92, 1.00)
    colors[clr.Border]                = ImVec4(0.86, 0.86, 0.86, 1.00)
    colors[clr.BorderShadow]          = ImVec4(0.00, 0.00, 0.00, 0.00)
    colors[clr.FrameBg]               = ImVec4(0.88, 0.88, 0.88, 1.00)
    colors[clr.FrameBgHovered]        = ImVec4(0.82, 0.82, 0.82, 1.00)
    colors[clr.FrameBgActive]         = ImVec4(0.76, 0.76, 0.76, 1.00)
    colors[clr.TitleBg]               = ImVec4(0.00, 0.45, 1.00, 0.82)
    colors[clr.TitleBgCollapsed]      = ImVec4(0.00, 0.45, 1.00, 0.82)
    colors[clr.TitleBgActive]         = ImVec4(0.00, 0.45, 1.00, 0.82)
    colors[clr.MenuBarBg]             = ImVec4(0.00, 0.37, 0.78, 1.00)
    colors[clr.ScrollbarBg]           = ImVec4(0.00, 0.00, 0.00, 0.00)
    colors[clr.ScrollbarGrab]         = ImVec4(0.00, 0.35, 1.00, 0.78)
    colors[clr.ScrollbarGrabHovered]  = ImVec4(0.00, 0.33, 1.00, 0.84)
    colors[clr.ScrollbarGrabActive]   = ImVec4(0.00, 0.31, 1.00, 0.88)
    colors[clr.ComboBg]               = ImVec4(0.92, 0.92, 0.92, 1.00)
    colors[clr.CheckMark]             = ImVec4(0.00, 0.49, 1.00, 0.59)
    colors[clr.SliderGrab]            = ImVec4(0.00, 0.49, 1.00, 0.59)
    colors[clr.SliderGrabActive]      = ImVec4(0.00, 0.39, 1.00, 0.71)
    colors[clr.Button]                = ImVec4(0.00, 0.49, 1.00, 0.59)
    colors[clr.ButtonHovered]         = ImVec4(0.00, 0.49, 1.00, 0.71)
    colors[clr.ButtonActive]          = ImVec4(0.00, 0.49, 1.00, 0.78)
    colors[clr.Header]                = ImVec4(0.00, 0.49, 1.00, 0.78)
    colors[clr.HeaderHovered]         = ImVec4(0.00, 0.49, 1.00, 0.71)
    colors[clr.HeaderActive]          = ImVec4(0.00, 0.49, 1.00, 0.78)
    colors[clr.ResizeGrip]            = ImVec4(0.00, 0.39, 1.00, 0.59)
    colors[clr.ResizeGripHovered]     = ImVec4(0.00, 0.27, 1.00, 0.59)
    colors[clr.ResizeGripActive]      = ImVec4(0.00, 0.25, 1.00, 0.63)
    colors[clr.CloseButton]           = ImVec4(0.00, 0.35, 0.96, 0.71)
    colors[clr.CloseButtonHovered]    = ImVec4(0.00, 0.31, 0.88, 0.69)
    colors[clr.CloseButtonActive]     = ImVec4(0.00, 0.25, 0.88, 0.67)
    colors[clr.PlotLines]             = ImVec4(0.00, 0.39, 1.00, 0.75)
    colors[clr.PlotLinesHovered]      = ImVec4(0.00, 0.39, 1.00, 0.75)
    colors[clr.PlotHistogram]         = ImVec4(0.00, 0.39, 1.00, 0.75)
    colors[clr.PlotHistogramHovered]  = ImVec4(0.00, 0.35, 0.92, 0.78)
    colors[clr.TextSelectedBg]        = ImVec4(0.00, 0.47, 1.00, 0.59)
    colors[clr.ModalWindowDarkening]  = ImVec4(0.20, 0.20, 0.20, 0.35)
end
style()

function main()
    if not isSampLoaded() or not isSampfuncsLoaded() then
        return
    end

    while not isSampAvailable() do
        wait(0)
    end
    
    if not doesFileExist(json_file) then jsonSave(json_file, list) end
    list = jsonRead(json_file)
    
    sampRegisterChatCommand('cmdtools', cmd_tool)

    sampAddChatMessage('{00ffcc}[CmdTools] {ffffff}- For Payson!', -1)
    sampAddChatMessage('{00ffcc}[CmdTools] {ffffff}- Version: 1.0 beta', -1)
    sampAddChatMessage('{00ffcc}[CmdTools] {ffffff}- Author: Haunted_BabyTape', -1)
    
    local file = io.open('moonloader\\config\\cmdtools.json', "r+")
    local jsonInString = file:read("*a")
    file:close()
    local jsonTable = decodeJson(jsonInString)
    
    sampRegisterChatCommand(cmd.v, function(num)
        sampSendChat(u8:decode(string.format("/%s "..num.." %s %s", type_n.v, time_n.v, prich.v)))
    end)

    sampRegisterChatCommand(jsonTable["commands"]["cmd"], function(num)
        sampAddChatMessage('succes')
    end)
end

function cmd_tool(arg)
    main_window_state.v = not main_window_state.v
    imgui.Process = main_window_state.v
end

function imgui.OnDrawFrame()
    if not main_window_state.v then
        imgui.Process = false
    end   

    if not window_pos then
        ScreenX, ScreenY = getScreenResolution()ScreenX, ScreenY = getScreenResolution()
        imgui.SetNextWindowPos(imgui.ImVec2(ScreenX / 2 , ScreenY / 2), imgui.Cond.FirsUseEver, imgui.ImVec2(0.5, 0.5))
    end   

    if main_window_state.v then
        imgui.SetNextWindowSize(imgui.ImVec2(290, 225), imgui.Cond.FirstUseEver)

        imgui.Begin(u8'[CmdTools] Команды', main_window_state, imgui.WindowFlags.MenuBar)
        
        imgui.InputText(u8'Команда.', cmd)
        imgui.InputText(u8'Тип наказания.', type_n)
        imgui.InputText(u8'Причина.', prich)
        imgui.InputInt(u8'Время.', time_n)
            
        if imgui.Button(u8'Создать команду.') then
            local file = io.open('moonloader\\config\\cmdtools.json', "r+")
            local jsonInString = file:read("*a")
            file:close()
            local jsonTable = decodeJson(jsonInString)   
        
            table.insert(list["commands"], {cmd = cmd.v})
            table.insert(list["commands"], {type_n = type_n.v})
            table.insert(list["commands"], {prich = prich.v})
            table.insert(list["commands"], {time_n = time_n.v})
            jsonSave(json_file, list)
            
            sampRegisterChatCommand(cmd.v, function(num)
                sampSendChat(u8:decode(string.format("/%s "..num.." %s %s", type_n.v, time_n.v, prich.v)))
            end)
        end       

        imgui.End()
    end
end

function jsonSave(jsonFilePath, t)
    file = io.open(jsonFilePath, "w")
    file:write(encodeJson(t))
    file:flush()
    file:close()
end

function jsonRead(jsonFilePath)
    local file = io.open(jsonFilePath, "r+")
    local jsonInString = file:read("*a")
    file:close()
    local jsonTable = decodeJson(jsonInString)
    return jsonTable
end