local imgui = require 'mimgui'
local ffi = require 'ffi'
local encoding = require 'encoding'
encoding.default = 'CP1251'
local u8 = encoding.UTF8
function json(filePath)
local filePath = getWorkingDirectory()..'\\config\\'..(filePath:find('(.+).json') and filePath or filePath..'.json')
local class = {}
if not doesDirectoryExist(getWorkingDirectory()..'\\config') then
createDirectory(getWorkingDirectory()..'\\config')
end
function class:Save(tbl)
if tbl then
local F = io.open(filePath, 'w')
F:write(encodeJson(tbl) or {})
F:close()
return true, 'ok'
end
return false, 'table = nil'
end
function class:Load(defaultTable)
if not doesFileExist(filePath) then
class:Save(defaultTable or {})
end
local F = io.open(filePath, 'r+')
local TABLE = decodeJson(F:read() or {})
F:close()
for def_k, def_v in next, defaultTable do
if TABLE[def_k] == nil then
TABLE[def_k] = def_v
end
end
return TABLE
end
return class
end
local testJSON = json('testJSON.json'):Load({})
function save()
json('testJSON.json'):Save(testJSON)
end
local WinState = imgui.new.bool()
local whatSave = {
name = imgui.new.char[256](),
}
imgui.OnFrame(function() return WinState[0] end, function(player)
imgui.SetNextWindowPos(imgui.ImVec2(500,500), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
imgui.SetNextWindowSize(imgui.ImVec2(500, 500), imgui.Cond.Always)
imgui.Begin('##Window', WinState, imgui.WindowFlags.NoResize)
imgui.InputText('Name',whatSave.name,256)
if imgui.Button(u8'Добавить') then
table.insert(testJSON, {
name = u8:decode(ffi.string(whatSave.name)),
}) save()
whatSave = {
name = imgui.new.char[256](),
}
end
imgui.SameLine()
if imgui.Button(u8'Очистить') then
whatSave = {
name = imgui.new.char[256](),
}
end
imgui.Separator()
for index, data in pairs(testJSON) do
if imgui.Button(u8(data.name)..'##'..index) then
sampAddChatMessage('Ты нажал кнопку '..data.name,-1)
end
imgui.SameLine()
if imgui.Button(u8'Удалить##'..index) then
table.remove(testJSON,index)
save()
end
end
imgui.End()
end)
function main()
sampRegisterChatCommand('cmd', function() WinState[0] = not WinState[0] end)
wait(-1)
end