--[====================================================================================================================[ * Remove All Unnamed Structures (prompts first) * Remove All Structures (prompts first) * Disable All Memory Records (prompts first) * Disable No Run All Memory Records (prompts first) * Save All Table Files To Disk * Remove All Table Files (prompts first) * Save All Cea Table Files To Disk * Remove All Cea Table Files (prompts first) * Save All Lua Table Files To Disk * Remove All Lua Table Files (prompts first) * Save All But Cea & Lua Table Files * Remove All But Cea & Lua Table Files (prompts first) * Clean Table (prompts first) See: CleanTableFunctionList 'removeAllStructures' 'removeAllButCeaLuaTableFiles' 'removeAllCeaTableFiles' 'removeAllLuaTableFiles' ]====================================================================================================================]-- local CreateMainFormMenuItems = true local MainMenuItemName = 'miTools' local MainMenuItemCaption = 'Tools' local UnnamedStructureString = 'unnamed structure' local TableMenuItemName = 'miTable' local TableAddFileMenuItemName = 'miAddFile' local CeaFolder = 'ceaFiles' local LuaFolder = 'luaFiles' local TableFilesFolder = 'tableFiles' local CleanTableFunctionList = { 'removeAllStructures', 'removeAllButCeaLuaTableFiles', 'removeAllCeaTableFiles', 'removeAllLuaTableFiles', } local NAME = 'pluginAddMenu_Tools_TableHelpers' local CLASS_NAME = 'I2TableHelpFunctions' local VERSION = '1.0.5' local AUTHOR = 'Matt Irwin; Matt.Irwin.US@Gmail.com' local LICENSE = [=[MIT License Copyright (c) 2018 Matt Irwin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sub-license, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]=] local format = string.format local strE = string.empty or STRING_EMPTY or '' local t = translate ---- ---- ---- Class / Base table ---- I2TableHelpFunctions = { Name = NAME, ClassName = CLASS_NAME, Version = VERSION, Author = AUTHOR, License = LICENSE, CreateMainFormMenuItems = CreateMainFormMenuItems, MainMenuItemName = MainMenuItemName, MainMenuItemCaption = MainMenuItemCaption, UnnamedStructureString = UnnamedStructureString, TableMenuItemName = TableMenuItemName, TableAddFileMenuItemName = TableAddFileMenuItemName, CeaFolder = CeaFolder, LuaFolder = LuaFolder, TableFilesFolder = TableFilesFolder, CleanTableFunctionList = CleanTableFunctionList, } -- ---- ---- Helpers local function getPath( ... ) local pathseparator = package.config:sub(1,1) local elements = { ... } return table.concat(elements, pathseparator) end local function exists(path) if type(path) ~= 'string' then return end return os.rename(path, path) and true or false end local function isFile(path) if type(path) ~= 'string' then return end if not exists(path) then return false end local f = io.open(path) if f then f:close() return true end return false end local function isDirectory(path) return (exists(path) and not isFile(path)) end local function createDirectory(directoryName, path) shellExecute('cmd','/c mkdir "' .. directoryName .. '"', path, false) sleep(100) ---- sleep 0.1 seconds for shellexecute to complete end local function getTableFilesList() local mainForm = getMainForm() if mainForm.Menu == nil then return end local menuItems = mainForm.Menu.Items local miTable = nil local tableFiles = {} for i = 0, menuItems.Count - 1 do if menuItems[i].Name == I2TableHelpFunctions.TableMenuItemName then miTable = menuItems[i] ---- If table menu is never opened, then the table files are never added as menu items. miTable.doClick() mainForm.bringToFront() end end local startCapture = false for i = 0, miTable.Count - 1 do if startCapture then tableFiles[#tableFiles + 1] = miTable[i].Caption end if miTable[i].Name == I2TableHelpFunctions.TableAddFileMenuItemName then startCapture = true end end return tableFiles end -- ---- ---- Functions function I2TableHelpFunctions.removeAllUnnamedStructures() local msg = t('Remove all unnamed structures from the table') .. '?' local mr = messageDialog(msg, mtConfirmation, mbYes, mbNo, mbCancel) if mr == mrYes then for i = getStructureCount() - 1, 0, -1 do local struct = getStructure(i) if struct.Name == I2TableHelpFunctions.UnnamedStructureString then struct.removeFromGlobalStructureList() end end return true elseif mr == mrNo then return true end return false end function I2TableHelpFunctions.removeAllStructures() local msg = t('Remove all structures from the table?') local mr = messageDialog(msg, mtConfirmation, mbYes, mbNo, mbCancel) if mr == mrYes then for i = getStructureCount() - 1, 0, -1 do local struct = getStructure(i) struct.removeFromGlobalStructureList() end return true elseif mr == mrNo then return true end return false end function I2TableHelpFunctions.disableAllMemeoryRecords() local msg = t('Disable all memory records?') local mr = messageDialog(msg, mtConfirmation, mbYes, mbNo, mbCancel) if mr == mrYes then local addressesList = getAddressList() for i = 0, addressesList.Count - 1 do local memoryRecord = addressesList.getMemoryRecord(i) if memoryRecord ~= nil and memoryRecord.Active then memoryRecord.Active = false end end return true elseif mr == mrNo then return true end return false end function I2TableHelpFunctions.disableAllWithoutExecute() local msg = t('Disable all memory records without executing the disable section?') local mr = messageDialog(msg, mtConfirmation, mbYes, mbNo, mbCancel) if mr == mrYes then local addressesList = getAddressList() addressesList.disableAllWithoutExecute() -- for i = 0, addressesList.Count - 1 do -- local memoryRecord = addressesList.getMemoryRecord(i) -- if memoryRecord ~= nil then -- memoryRecord.disableWithoutExecute() -- end -- end -- return true elseif mr == mrNo then return true end return false end function I2TableHelpFunctions.saveAllTableFilesToDisk() local tableFiles = getTableFilesList() for i, v in ipairs(tableFiles) do local tf = findTableFile(v) if tf then if not isDirectory(I2TableHelpFunctions.TableFilesFolder) then createDirectory(I2TableHelpFunctions.TableFilesFolder) end tf.saveToFile(getPath(I2TableHelpFunctions.TableFilesFolder, v)) end end end function I2TableHelpFunctions.removeAllTableFiles() local msg = t('Remove all table files from the table?') local mr = messageDialog(msg, mtConfirmation, mbYes, mbNo, mbCancel) if mr == mrYes then local tableFiles = getTableFilesList() for i, v in ipairs(tableFiles) do local tf = findTableFile(v) if tf then tf.delete() end end return true elseif mr == mrNo then return true end return false end function I2TableHelpFunctions.saveAllCeaTableFilesToDisk() local tableFiles = getTableFilesList() for i, v in ipairs(tableFiles) do if v:lower():sub(#v - 3) == '.cea' then local tf = findTableFile(v) if tf then if not isDirectory(I2TableHelpFunctions.CeaFolder) then createDirectory(I2TableHelpFunctions.CeaFolder) end tf.saveToFile(getPath(I2TableHelpFunctions.CeaFolder, v)) end end end end function I2TableHelpFunctions.removeAllCeaTableFiles() local msg = t('Remove all CEA table files from the table?') local mr = messageDialog(msg, mtConfirmation, mbYes, mbNo, mbCancel) if mr == mrYes then local tableFiles = getTableFilesList() for i, v in ipairs(tableFiles) do if v:lower():sub(#v - 3) == '.cea' then local tf = findTableFile(v) if tf then tf.delete() end end end return true elseif mr == mrNo then return true end return false end function I2TableHelpFunctions.saveAllLuaTableFilesToDisk() local tableFiles = getTableFilesList() for i, v in ipairs(tableFiles) do if v:lower():sub(#v - 3) == '.lua' then local tf = findTableFile(v) if tf then if not isDirectory(I2TableHelpFunctions.LuaFolder) then createDirectory(I2TableHelpFunctions.LuaFolder) end tf.saveToFile(getPath(I2TableHelpFunctions.LuaFolder, v)) end end end end function I2TableHelpFunctions.removeAllLuaTableFiles() local msg = t('Remove all Lua table files from the table?') local mr = messageDialog(msg, mtConfirmation, mbYes, mbNo, mbCancel) if mr == mrYes then local tableFiles = getTableFilesList() for i, v in ipairs(tableFiles) do if v:lower():sub(#v - 3) == '.lua' then local tf = findTableFile(v) if tf then tf.delete() end end end return true elseif mr == mrNo then return true end return false end function I2TableHelpFunctions.saveAllButCeaLuaTableFiles() local tableFiles = getTableFilesList() for i, v in ipairs(tableFiles) do local fe = v:lower():sub(#v - 3) if fe ~= '.cea' and fe ~= '.lua' then local tf = findTableFile(v) if tf then if not isDirectory(I2TableHelpFunctions.TableFilesFolder) then createDirectory(I2TableHelpFunctions.TableFilesFolder) end tf.saveToFile(getPath(I2TableHelpFunctions.TableFilesFolder, v)) end end end end function I2TableHelpFunctions.removeAllButCeaLuaTableFiles() local msg = t('Remove all table files except CEA and Lua files from the table?') local mr = messageDialog(msg, mtConfirmation, mbYes, mbNo, mbCancel) if mr == mrYes then local tableFiles = getTableFilesList() for i, v in ipairs(tableFiles) do local fe = v:lower():sub(#v - 3) if fe ~= '.cea' and fe ~= '.lua' then local tf = findTableFile(v) if tf then tf.delete() end end end return true elseif mr == mrNo then return true end return false end function I2TableHelpFunctions.cleanTable() local tbl = I2TableHelpFunctions.CleanTableFunctionList local run = true for i, v in ipairs(tbl) do if type(I2TableHelpFunctions[v]) == 'function' and run then run = I2TableHelpFunctions[v]() end end end -- ---- ---- Setup and load function addMenuItem(parent, caption) if parent == nil then return nil end local newItem = createMenuItem(parent) parent.add(newItem) newItem.Caption = caption return newItem end local function createMainFormMenu() local mainForm = getMainForm() if mainForm.Menu == nil then return end local menuItems = mainForm.Menu.Items local miTools = nil for i = 0, menuItems.Count - 1 do if menuItems[i].Name == I2TableHelpFunctions.MainMenuItemName then miTools = menuItems[i] end end if miTools == nil then miTools = createMenuItem(mainForm) miTools.Name = I2TableHelpFunctions.MainMenuItemName miTools.Caption = I2TableHelpFunctions.MainMenuItemCaption menuItems.insert(menuItems.Count - 2, miTools) end return miTools end local function loadMenuAddAddressListHelpers() local function loadloadMenuAddAddressListHelpersTimer_tick(timer) timer.destroy() if I2TableHelpFunctions.CreateMainFormMenuItems then local miTools = createMainFormMenu() addMenuItem(miTools, t('Save All Table Files To Disk')).setOnClick(I2TableHelpFunctions.saveAllTableFilesToDisk) addMenuItem(miTools, t('Save All Cea Table Files To Disk')).setOnClick(I2TableHelpFunctions.saveAllCeaTableFilesToDisk) addMenuItem(miTools, t('Save All Lua Table Files To Disk')).setOnClick(I2TableHelpFunctions.saveAllLuaTableFilesToDisk) addMenuItem(miTools, t('Save All But Cea Lua Table Files')).setOnClick(I2TableHelpFunctions.removeAllButCeaLuaTableFiles) addMenuItem(miTools, '-') addMenuItem(miTools, t('Remove All Table Files')).setOnClick(I2TableHelpFunctions.removeAllTableFiles) addMenuItem(miTools, t('Remove All Cea Table Files')).setOnClick(I2TableHelpFunctions.removeAllCeaTableFiles) addMenuItem(miTools, t('Remove All Lua Table Files')).setOnClick(I2TableHelpFunctions.removeAllLuaTableFiles) addMenuItem(miTools, t('Remove All But Cea Lua Table Files')).setOnClick(I2TableHelpFunctions.removeAllButCeaLuaTableFiles) addMenuItem(miTools, '-') addMenuItem(miTools, t('Disable All Memory Records')).setOnClick(I2TableHelpFunctions.disableAllMemeoryRecords) addMenuItem(miTools, t('Disable Without Executing All Memory Records')).setOnClick(I2TableHelpFunctions.disableAllWithoutExecute) addMenuItem(miTools, '-') addMenuItem(miTools, t('Remove All Unnamed Structures')).setOnClick(I2TableHelpFunctions.removeAllUnnamedStructures) addMenuItem(miTools, t('Remove All Structures')).setOnClick(I2TableHelpFunctions.removeAllStructures) addMenuItem(miTools, '-') addMenuItem(miTools, t('Clean Table')).setOnClick(I2TableHelpFunctions.cleanTable) end end ---- Use timer to allow other files to load menu items. local intervals = 200 local timer = createTimer(getMainForm()) timer.Interval = intervals timer.OnTimer = loadloadMenuAddAddressListHelpersTimer_tick end loadMenuAddAddressListHelpers() return I2TableHelpFunctions