Переделка inicfg на json

boy next door

Участник
Автор темы
262
24
Версия MoonLoader
.026-beta
Всем привет. Я вот более менее разобрался с inicfg, но с json у меня траблы.
Я не понимаю что до чего там, уже смотрел туторы, но ничего толком не понял.
Можете, пожалуйста, показать на наглядном примере, как мне перенести значение с моего inicfg на json?
Буду очень благодарен!

Lua:
local inicfg = require 'inicfg'
local directIni = 'ohelpNew'
local ini = inicfg.load(inicfg.load({
    main = {
        theme = 0
    },
}, directIni))
inicfg.save(ini, directIni)
local colorListNumber = imgui.new.int()
colorListNumber[0] = ini.main.theme


function onScriptTerminate(s) -- функция сохранения, при выгрузке скрипта
    if s == thisScript() then
    ini.main.theme = colorListNumber[0] -- запихиваем комбо в ини
    inicfg.save(ini, directIni) -- сохраняем
    end
end


--onFrame

if imgui.Combo('',colorListNumber,colorListBuffer, #colorList) then
    theme[colorListNumber[0]+1].change()
    ini.main.theme = colorListNumber[0]
    inicfg.save(ini, directIni)
end

--imgui.End()
 

boy next door

Участник
Автор темы
262
24

Fellespe.

Участник
76
16
Lua:
local inicfg = require 'inicfg'
local directIni = 'ohelpNew'
local ini = inicfg.load(inicfg.load({
    main = {
        theme = 0
    },
}, directIni))
inicfg.save(ini, directIni)
local colorListNumber = imgui.new.int()
colorListNumber[0] = ini.main.theme


function onScriptTerminate(s) -- функция сохранения, при выгрузке скрипта
    if s == thisScript() then
    ini.main.theme = colorListNumber[0] -- запихиваем комбо в ини
    inicfg.save(ini, directIni) -- сохраняем
    end
end


--onFrame

if imgui.Combo('',colorListNumber,colorListBuffer, #colorList) then
    theme[colorListNumber[0]+1].change()
    ini.main.theme = colorListNumber[0]
    inicfg.save(ini, directIni)
end

--imgui.End()

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 settings = json('config.json'):Load({
    ['theme'] = ini.main.theme
}) -- Подгрузка

ini.main.theme = 123123 -- Пример изменения
settings['theme'] = ini.main.theme
local status, code = json('config.json'):Save(settings)
sampAddChatMessage(status and 'Настройки сохранены!' or 'Настройки не были сохранены: '..code, -1)
 

boy next door

Участник
Автор темы
262
24
Lua:
local inicfg = require 'inicfg'
local directIni = 'ohelpNew'
local ini = inicfg.load(inicfg.load({
    main = {
        theme = 0
    },
}, directIni))
inicfg.save(ini, directIni)
local colorListNumber = imgui.new.int()
colorListNumber[0] = ini.main.theme


function onScriptTerminate(s) -- функция сохранения, при выгрузке скрипта
    if s == thisScript() then
    ini.main.theme = colorListNumber[0] -- запихиваем комбо в ини
    inicfg.save(ini, directIni) -- сохраняем
    end
end


--onFrame

if imgui.Combo('',colorListNumber,colorListBuffer, #colorList) then
    theme[colorListNumber[0]+1].change()
    ini.main.theme = colorListNumber[0]
    inicfg.save(ini, directIni)
end

--imgui.End()

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 settings = json('config.json'):Load({
    ['theme'] = ini.main.theme
}) -- Подгрузка

ini.main.theme = 123123 -- Пример изменения
settings['theme'] = ini.main.theme
local status, code = json('config.json'):Save(settings)
sampAddChatMessage(status and 'Настройки сохранены!' or 'Настройки не были сохранены: '..code, -1)
(error) ohelpNew.lua: F:\white gta ot subs x smoke\moonloader\ohelpNew.lua:234: attempt to index global 'ini' (a nil value)
stack traceback:
F:\white gta ot subs x smoke\moonloader\ohelpNew.lua:234: in main chunk
1721203563926.png


(error) ohelpNew.lua: F:\white gta ot subs x smoke\moonloader\ohelpNew.lua:234: attempt to index global 'ini' (a nil value)
stack traceback:
F:\white gta ot subs x smoke\moonloader\ohelpNew.lua:234: in main chunk
1721203563926.png
Решил.

Вопрос. Можно ли убрать сохранение в ini? Я не могу сделать так, чтобы файл .ini не сохранялся.
 
Последнее редактирование:

g305noobo

Известный
Модератор
323
559
(error) ohelpNew.lua: F:\white gta ot subs x smoke\moonloader\ohelpNew.lua:234: attempt to index global 'ini' (a nil value)
stack traceback:
F:\white gta ot subs x smoke\moonloader\ohelpNew.lua:234: in main chunk
Посмотреть вложение 246735


Решил.

Вопрос. Можно ли убрать сохранение в ini? Я не могу сделать так, чтобы файл .ini не сохранялся.
Lua:
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 direct_json = getWorkingDirectory() .. "\\config\\ohelpNew.json"
local settings = json(direct_json):Load({
    ['theme'] = 0,
})

local colorListNumber = imgui.new.int()
colorListNumber[0] = settings['theme']


addEventHandler('onScriptTerminate', function(s) -- функция сохранения, при выгрузке скрипта
    if s == script.this then
        settings['theme'] = colorListNumber[0] -- запихиваем комбо в ини
        json(direct_json):Save(settings)
    end
end)

if imgui.Combo('',colorListNumber,colorListBuffer, #colorList) then
    theme[colorListNumber[0]+1].change()
    settings['theme'] = colorListNumber[0]
    json(direct_json):Save(settings)
end
 

boy next door

Участник
Автор темы
262
24
Lua:
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 direct_json = getWorkingDirectory() .. "\\config\\ohelpNew.json"
local settings = json(direct_json):Load({
    ['theme'] = 0,
})

local colorListNumber = imgui.new.int()
colorListNumber[0] = settings['theme']


addEventHandler('onScriptTerminate', function(s) -- функция сохранения, при выгрузке скрипта
    if s == script.this then
        settings['theme'] = colorListNumber[0] -- запихиваем комбо в ини
        json(direct_json):Save(settings)
    end
end)

if imgui.Combo('',colorListNumber,colorListBuffer, #colorList) then
    theme[colorListNumber[0]+1].change()
    settings['theme'] = colorListNumber[0]
    json(direct_json):Save(settings)
end
ohelpNew.lua: F:\white gta ot subs x smoke\moonloader\ohelpNew.lua:881: attempt to index local 'F' (a nil value)
stack traceback:
F:\white gta ot subs x smoke\moonloader\ohelpNew.lua:881: in function 'Save'
F:\white gta ot subs x smoke\moonloader\ohelpNew.lua:890: in function 'Load'
F:\white gta ot subs x smoke\moonloader\ohelpNew.lua:906: in main chunk
1721206297406.png
 

g305noobo

Известный
Модератор
323
559
ohelpNew.lua: F:\white gta ot subs x smoke\moonloader\ohelpNew.lua:881: attempt to index local 'F' (a nil value)
stack traceback:
F:\white gta ot subs x smoke\moonloader\ohelpNew.lua:881: in function 'Save'
F:\white gta ot subs x smoke\moonloader\ohelpNew.lua:890: in function 'Load'
F:\white gta ot subs x smoke\moonloader\ohelpNew.lua:906: in main chunk
Посмотреть вложение 246736
а, там уже путь до конфига прописан, local direct_json = getWorkingDirectory() .. "\\config\\ohelpNew.json" измени на local direct_json = getWorkingDirectory() .. "\\ohelpNew.json"