Hello Guest it is March 29, 2024, 11:47:40 AM

Author Topic: Immediately Save gReg/iReg Settings to Machine.ini  (Read 1012 times)

0 Members and 1 Guest are viewing this topic.

Immediately Save gReg/iReg Settings to Machine.ini
« on: October 17, 2019, 02:28:31 PM »
I'm trying to save the persistent gRegs/iRegs to Machine.ini immediately after changing the value.

I tried using mcProfileFlush, mcProfileSave and mcProfileReload in a button but the Machine.ini file does not immediately change after the script runs.

Instead I have to go through and rewrite every value of the gRegs/iRegs at the end of the Machine.ini file using file IO functions of LUA.

The issue with this method is that we have over 100 gRegs/iRegs in our screen and that value keeps increasing as we develop more of the screen.

The reason I have to do this in the first place is that the Machine.ini randomly gets corrupted if Mach4 crashes or if we power the machine down by just switching it off instead of exiting Mach4 first and shutting down the computer. Most of the customers will just press the power button which is what we think is causing the issue. The only way to get their settings back is to overwrite the corrupted Machine.ini with the Machine.ini.01 in the backups folder, but that file usually doesn't contain any of the recent changes. It looks like that backup file gets created when Mach4 is launched. 

The corrupted Machine.ini file just contains all zeros with terminating \n character at end of line. About 50 characters on every line.

Is there an easier solution for saving the gRegs/iRegs to Machine.ini? Could the Machine.ini corruption be from problems with code in our screen or is this a widespread issue?
Re: Immediately Save gReg/iReg Settings to Machine.ini
« Reply #1 on: October 17, 2019, 07:18:51 PM »
This is the solution I came up with. We do not expect customer to ever edit any other plugins or the Mach4 config so it works for us

Button Clicked Script:

Code: [Select]
function file_exists(file)
    local f = io.open(file, "rb")
    if f then f:close() end
    return f ~= nil
end

function Save()
    mc.mcProfileFlush(inst) --doesn't actually do anything ??
    wx.wxMilliSleep(100)
    mc.mcProfileSave(inst) -- doesn't actually do anything ??
    wx.wxMilliSleep(100)

    local profile = mc.mcProfileGetName(inst)
    local path = mc.mcCntlGetMachDir(inst)
    local machineINIPath = path .. "\\Profiles\\" .. profile .. "\\Machine.ini"
    local outputINIPath = path .. "\\UnCorrupted\\Machine.ini"

    if not file_exists(machineINIPath) then
        mc.mcCntlSetLastError(inst, "Machine.ini file does not exist in Mach4 Profiles directory")
        return
    end

    local outputINIFile = io.open(outputINIPath, "w")

    local regValue = 0
    local persistent = false
    local isRegister = false

    for line in io.lines(machineINIPath) do
        if string.match(line, "gReg") and not isRegister then
            isRegister = true
        elseif isRegister then
            if string.match(line, "Name=") then
                local regName = string.match(line, "=(.*)")
                local regHandle = mc.mcRegGetHandle(inst, "gRegs0/" .. regName)
                regValue = mc.mcRegGetValue(regHandle)
            elseif string.match(line, "Persistent=1") then
                persistent = true
            elseif persistent then
                line = "Value=" .. tostring(regValue)
                persistent = false
                isRegister = false
                regValue = 0
            end
        end

        outputINIFile:write(line, "\n")
    end

    outputINIFile:close()

    local rs = wx.wxMessageBox("Saving Complete ", "Confirm", wx.wxOK + wx.wxICON_EXCLAMATION)
end

Save()


.bat File linked to profile start (overwrites any existing machine.ini) .bat file is located in
C:\Mach4\UnCorrupted\

Code: [Select]
set workingDir=%cd%
set machDir=C:\Mach4

@echo F|xcopy /y "%workingDir%\Machine.ini" "%machDir%\Profiles\ProfileName\Machine.ini" 
start %machDir%\Mach4GUI.exe /p ProfileName