import TABLE from require "ZF.main.util.table" import UTIL from require "ZF.main.util.util" class CONFIG version: "1.0.2" -- checks if a file or folder exists -- @param file string -- @param isDir boolean -- @return boolean, string fileExist: (dir, isDir) => a = dir\sub 1, 1 b = dir\sub -1, -1 c = "\"" if a == c and b == c dir = dir\sub 2, -2 dir ..= "/" if isDir ok, err, code = os.rename dir, dir unless ok return true if code == 13 return ok, err -- creates one or more new directories -- @param dir string -- @return nil mkdir: (dir) => assert dir, "expected dir" unless @fileExist dir, true os.execute "mkdir #{dir}" -- deletes one or more directories -- @param dir string -- @return nil rmdir: (dir) => assert dir, "expected dir" if @fileExist dir, true os.execute "rd /s /q #{dir}" -- convert a path beginning with a path specifier to an absolute path -- @param code string -- @return nil aegiPath: (code = "?user") => @path = aegisub.decode_path code -- converts the gui to string and writes it to a text file -- @param dir string -- @param gui table -- @return string writeGui: (dir, gui) => write = (content) -> written = "" for name, value in pairs content written ..= "#{name}:#{value}|" written = written\sub 1, -2 return written written = type(gui) != "table" and "" or write gui file = io.open dir, "w" file\write written file\close! return written -- reads the string generated by the writeGui function -- @param dir string -- @return table readGui: (dir) => split = (content) -> result, values = {}, UTIL\headsTails content, "|" for _, value in ipairs values set = UTIL\headsTails value, ":" conc = table.concat set, "", 2 conc = conc == "true" and true or (tonumber(conc) and tonumber(conc) or conc) result[set[1]] = conc return result if arq = io.open dir, "r" read = arq\read "*a" arq\close! return split read -- loads the lines contained in the saved file -- @param gui table -- @param macro_name string -- @return table, table loadGui: (gui, macro_name) => @aegiPath! macro_name = macro_name\lower! dir, new, read = "#{@path}\\zeref-cfg\\#{macro_name\gsub("%s", "_")}.config", TABLE(gui)\copy! if read = @readGui dir for k, v in ipairs new v.value = read[v.name] if v.name return new, read -- saves the contents of an interface to a file -- @param gui table -- @param macro_name string -- @return string saveGui: (gui, macro_name) => @aegiPath! dir = "#{@path}\\zeref-cfg" @mkdir "\"#{dir}\"" return @writeGui "#{dir}\\#{macro_name\lower!\gsub("%s", "_")}.config", gui {:CONFIG}