[Lua] Table File packer

Upload *YOUR* gamehacking tools/helpers here
Post Reply
UltimatePoto42
Expert Cheater
Expert Cheater
Posts: 125
Joined: Tue May 02, 2017 6:00 am
Reputation: 15

[Lua] Table File packer

Post by UltimatePoto42 »

Table File packer

This kind of goes with my Lua Table File Package Loader. It will pack files from a local folder, but more so it will pack Lua modules based on a entry point and what is required (imported) form there. Basically it hijacks the require function and uses the package searchers to find all needed modules. It creates a menu item to launch a form but you can also just call the functions. It's an extension of Cheat Engine Table Dev Tools, but it's not required for this tool. Just put the file in the "autorun" folder.

Image


TableDevToolsTableFiles.lua:

Code: Select all

local DevToolsMenuItemName = 'miDevTools'
local DevToolsMenuItemCaption = '&Dev Tools'
local miDevTools

local _require = require


function findLuaModules(entryPointName)
	local foundModules = { }
	local loader
	local modPath
	require = function(name)
		package.loaded[name] = false
		for i, searcher in ipairs(package.searchers) do
			loader, modPath = searcher(name)
			if modPath then
				if modPath:match('init.lua$') then
					foundModules[name..'.init'] = modPath
				else
					foundModules[name] = modPath
				end
				break
			end
		end
		return _require(name)
	end
	require(entryPointName)
	require = _require
	return foundModules
end
registerLuaFunctionHighlight('findLuaModules')

function packLuaModules(entryPointName, removeIfFound)
	local packedLuaModules = { }
	local foundModules = findLuaModules(entryPointName)
	for name, modPath in pairs(foundModules) do
		local tfName = name..'.lua'
		local tableFile = findTableFile(tfName)
		if tableFile and removeIfFound then
			tableFile.delete()
			tableFile = nil
		end
		if not tableFile then
			tableFile = createTableFile(tfName, modPath)
			packedLuaModules[tfName] = modPath
		end
	end
	return packedLuaModules
end
registerLuaFunctionHighlight('packLuaModules')

function repackLuaModules(entryPointName)
	return packLuaModules(entryPointName, true)
end
registerLuaFunctionHighlight('repackLuaModules')

function getTableFilesList()
	local tableMenuItemName = 'miTable'
	local tableAddFileMenuItemName = 'miAddFile'
	if not MainForm.Menu then return end
	local menuItems = MainForm.Menu.Items
	local miTable
	local tableFiles = { }
	for i = 0, menuItems.Count - 1 do
		if menuItems[i].Name == tableMenuItemName then
			miTable = menuItems[i]
			---- If table menu is never opened, 
			---- 	then the table files are never added as menu items.
			miTable.doClick()
		end
	end
	local startCapture = false
	for i = 0, miTable.Count - 1 do
		if startCapture then
			table.insert(tableFiles, miTable[i].Caption)
		end
		if not startCapture and miTable[i].Name == tableAddFileMenuItemName then
			startCapture = true
		end
	end
	return tableFiles
end
registerLuaFunctionHighlight('getTableFilesList')

function removeAllTableFiles()
	local deletedList = { }
	local fileList = getTableFilesList()
	for i = 1, #fileList do
		local name = fileList[i]
		local tableFile = findTableFile(name)
		if tableFile then
			tableFile.delete()
			table.insert(deletedList, name)
		end
	end
	return deletedList
end
registerLuaFunctionHighlight('removeAllTableFiles')

function packTableFiles(folderPath, removeIfFound)
	---- Names starting with "_" are hidden when packing files
	folderPath = folderPath or ''
	local fileList
	local tableFiles = { }
	if type(folderPath) == 'table' then
		fileList = folderPath
	else
		fileList = getFileList(folderPath)
	end
	if fileList then
		for i, fileName in ipairs(fileList) do
			if fileName:sub(1, 1) ~= '_' and not fileName:lower():match('.lua$') 
			and not fileName:lower():match('.ct$') then
				local tableFileName = fileName:match('^[(%w:\\)].*\\(.-%..-)$') or fileName
				if tableFileName then
					local tableFile = findTableFile(tableFileName)
					if tableFile and removeIfFound then
						tableFile.delete()
						tableFile = nil
					end
					if not tableFile then
						createTableFile(tableFileName, fileName)
						table.insert(tableFiles, tableFileName)
					end
				end
			end
		end
	end
	if type(folderPath) == 'string' then
		folderList = getDirectoryList(folderPath)
		if folderList then
			for i, path in ipairs(folderList) do
				local folderName = path
				if folderPath ~= '' then
					folderName = path:sub(#folderPath + 2, #path)
				end
				if folderName:sub(1, 1) ~= '_' then
					local tbl = packTableFiles(path, removeIfFound)
					for _, v in ipairs(tbl) do
						table.insert(tableFiles, v)
					end
				end
			end
		end
	end
	return tableFiles
end
registerLuaFunctionHighlight('packTableFiles')

function repackTableFiles(folderPath, removeAll)
	if removeAll then
		removeAllTableFiles()
	end
	return packTableFiles(folderPath, true)
end
registerLuaFunctionHighlight('repackTableFiles')

local function setupForm()
	local settings = getSettings('TableDevToolsTableFilesForm')
	local padding = 5
	local btnHeight = 40
	local form = createForm(false)
	form.Caption = 'Table File Packer'
	form.Width = tonumber(settings['FormWidth']) or 550
	form.Height = tonumber(settings['FormHeight']) or 500
	form.Top = tonumber(settings['FormTop']) or 10
	form.Left = tonumber(settings['FormLeft']) or 10
	form.PopupMode = 'pmNone'
	-- form.BorderStyle = 'bsSizeable'
	local list = createListBox(form)
	list.Width = 300
	list.Height = form.Height - padding * 2
	list.Top = padding
	list.Left = padding
	list.Anchors = '[akTop,akBottom,akLeft]'
	----
	local btnWidth = form.Width - (list.Width + padding * 3)
	local pos = padding
	----
	local btnListFiles = createButton(form)
	btnListFiles.Caption = 'List Files'
	btnListFiles.Width = btnWidth
	btnListFiles.Height = btnHeight
	btnListFiles.Top = pos
	btnListFiles.Left = list.Width + padding * 2
	btnListFiles.OnClick = function(sender)
		list.clear()
		local fileList = getTableFilesList()
		local stringsList = list.getItems()
		for i, name in ipairs(fileList) do
			stringsList.add(name)
		end
	end
	pos = pos + btnHeight
	----
	local btnRemoveFiles = createButton(form)
	btnRemoveFiles.Caption = 'Remove All Table Files'
	btnRemoveFiles.Width = btnWidth
	btnRemoveFiles.Height = btnHeight
	btnRemoveFiles.Top = pos
	btnRemoveFiles.Left = list.Width + padding * 2
	btnRemoveFiles.OnClick = function(sender)
		list.clear()
		removeAllTableFiles()
		local fileList = getTableFilesList()
		local stringsList = list.getItems()
		for i, name in ipairs(fileList) do
			stringsList.add(name)
		end
	end
	pos = pos + btnHeight
	----
	local lblLocalFolder = createLabel(form)
	lblLocalFolder.Caption = 'Table File\'s Local Folder'
	lblLocalFolder.Width = btnWidth
	lblLocalFolder.Top = pos
	lblLocalFolder.Left = list.Width + padding * 2
	pos = pos + lblLocalFolder.Height
	----
	local edtLocalFolder = createEdit(form)
	edtLocalFolder.Width = btnWidth
	edtLocalFolder.Top = pos
	edtLocalFolder.Left = list.Width + padding * 2
	pos = pos + edtLocalFolder.Height
	----
	local btnRepack = createButton(form)
	btnRepack.Caption = 'Repack Table Files'
	btnRepack.Width = btnWidth
	btnRepack.Height = btnHeight
	btnRepack.Top = pos
	btnRepack.Left = list.Width + padding * 2
	btnRepack.OnClick = function(sender)
		list.clear()
		repackTableFiles(edtLocalFolder.Text)
		local fileList = getTableFilesList()
		local stringsList = list.getItems()
		for i, name in ipairs(fileList) do
			stringsList.add(name)
		end
	end
	pos = pos + btnHeight
	----
	local lblEntryPoint = createLabel(form)
	lblEntryPoint.Caption = 'Lua Module\'s Entry Point'
	lblEntryPoint.Width = btnWidth
	lblEntryPoint.Top = pos
	lblEntryPoint.Left = list.Width + padding * 2
	pos = pos + lblEntryPoint.Height
	----
	local edtEntryPoint = createEdit(form)
	edtEntryPoint.Width = btnWidth
	edtEntryPoint.Top = pos
	edtEntryPoint.Left = list.Width + padding * 2
	pos = pos + edtEntryPoint.Height
	----
	local btnRepackMosules = createButton(form)
	btnRepackMosules.Caption = 'Repack Lua Modules'
	btnRepackMosules.Width = btnWidth
	btnRepackMosules.Height = btnHeight
	btnRepackMosules.Top = pos
	btnRepackMosules.Left = list.Width + padding * 2
	btnRepackMosules.OnClick = function(sender)
		list.clear()
		repackLuaModules(edtEntryPoint.Text)
		local fileList = getTableFilesList()
		local stringsList = list.getItems()
		for i, name in ipairs(fileList) do
			stringsList.add(name)
		end
	end
	pos = pos + btnHeight
	----
	form.registerCloseCallback(function(form)
		settings['FormWidth'] = form.Width
		settings['FormHeight'] = form.Height
		settings['FormTop'] = form.Top
		settings['FormLeft'] = form.Left
		return caFree
	end)
	return form
end
local form = setupForm()

local function getMainFormMenuItem()
	if MainForm.Menu == nil then return end
	local menuItems = MainForm.Menu.Items
	local mi = nil
	for i = 0, menuItems.Count - 1 do
		if menuItems[i].Name == DevToolsMenuItemName then
			mi = menuItems[i]
			mi.visible = true
		end
	end
	if mi == nil then
		mi = createMenuItem(MainForm)
		mi.Name = DevToolsMenuItemName
		mi.Caption = DevToolsMenuItemCaption
		MainForm.Menu.Items.insert(5, mi)
	end
	return mi
end

local function addMenuItem(caption, onClick)
	local newItem = createMenuItem(miDevTools)
	miDevTools.add(newItem)
	newItem.Caption = caption
	newItem.OnClick = onClick
	return newItem
end

local function loadMenuDevTools()
	miDevTools = getMainFormMenuItem()
	if miDevTools.Count > 0 then
		addMenuItem('-')
	end
	addMenuItem(translate('Open Table File Packer'), form.show)
end

loadMenuDevTools()

Post Reply

Who is online

Users browsing this forum: tyberuswilde