From 9eab5d6d65ca234acb42e3b15419a61c1ba9b71f Mon Sep 17 00:00:00 2001 From: Igor Timofeev Date: Mon, 21 Jan 2019 09:25:36 +0300 Subject: [PATCH] Added filesystem.name(path[, removeEndSlash) option --- Extensions/Lua/Context menu.lua | 64 ++++++++++----------- Extensions/Lua/Launcher.lua | 4 +- Extensions/Pic/Context menu.lua | 36 ++++++------ Extensions/Pkg/Launcher.lua | 14 ++--- Libraries/Compressor.lua | 99 ++++++++++++++++----------------- Libraries/Filesystem.lua | 4 +- Libraries/GUI.lua | 2 +- Libraries/System.lua | 6 +- 8 files changed, 114 insertions(+), 115 deletions(-) diff --git a/Extensions/Lua/Context menu.lua b/Extensions/Lua/Context menu.lua index 3107d2d6..1b46e995 100755 --- a/Extensions/Lua/Context menu.lua +++ b/Extensions/Lua/Context menu.lua @@ -1,32 +1,32 @@ - -local filesystem = require("Filesystem") -local GUI = require("GUI") -local paths = require("Paths") -local system = require("System") - -local workspace, icon, menu = select(1, ...), select(2, ...), select(3, ...) - -menu:addItem(system.localization.edit).onTouch = function() - system.execute(paths.system.applicationMineCodeIDE, icon.path) -end - -menu:addItem(system.localization.uploadToPastebin, not component.isAvailable("internet")).onTouch = function() - system.uploadToPastebin(icon.path) -end - -menu:addSeparator() - -menu:addItem(system.localization.launchWithArguments).onTouch = function() - system.launchWithArguments(workspace, icon.path) -end - -menu:addItem(system.localization.flashEEPROM, not component.isAvailable("eeprom") or filesystem.size(icon.path) > 4096).onTouch = function() - local container = GUI.addBackgroundContainer(workspace, true, true, system.localization.flashEEPROM) - container.layout:addChild(GUI.label(1, 1, container.width, 1, 0x969696, system.localization.flashingEEPROM .. "...")):setAlignment(GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_TOP) - workspace:draw() - - component.get("eeprom").set(filesystem.read(icon.path)) - - container:remove() - workspace:draw() -end + +local filesystem = require("Filesystem") +local GUI = require("GUI") +local paths = require("Paths") +local system = require("System") + +local workspace, icon, menu = select(1, ...), select(2, ...), select(3, ...) + +menu:addItem(system.localization.edit).onTouch = function() + system.execute(paths.system.applicationMineCodeIDE, icon.path) +end + +menu:addItem(system.localization.uploadToPastebin, not component.isAvailable("internet")).onTouch = function() + system.uploadToPastebin(icon.path) +end + +menu:addSeparator() + +menu:addItem(system.localization.launchWithArguments).onTouch = function() + system.launchWithArguments(workspace, icon.path) +end + +menu:addItem(system.localization.flashEEPROM, not component.isAvailable("eeprom") or filesystem.size(icon.path) > 4096).onTouch = function() + local container = GUI.addBackgroundContainer(workspace, true, true, system.localization.flashEEPROM) + container.layout:addChild(GUI.label(1, 1, container.width, 1, 0x969696, system.localization.flashingEEPROM .. "...")):setAlignment(GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_TOP) + workspace:draw() + + component.get("eeprom").set(filesystem.read(icon.path)) + + container:remove() + workspace:draw() +end diff --git a/Extensions/Lua/Launcher.lua b/Extensions/Lua/Launcher.lua index d42379fc..08459732 100755 --- a/Extensions/Lua/Launcher.lua +++ b/Extensions/Lua/Launcher.lua @@ -1,2 +1,2 @@ - -require("System").execute(select(1, ...)) + +require("System").execute(select(1, ...)) diff --git a/Extensions/Pic/Context menu.lua b/Extensions/Pic/Context menu.lua index b7410383..3f6c23a5 100755 --- a/Extensions/Pic/Context menu.lua +++ b/Extensions/Pic/Context menu.lua @@ -1,18 +1,18 @@ - -local system = require("System") - -local workspace, icon, menu = select(1, ...), select(2, ...), select(3, ...) - -menu:addItem(system.localization.setAsWallpaper).onTouch = function() - system.properties.interfaceWallpaperEnabled = true - system.properties.interfaceWallpaperPath = icon.path - - system.updateWallpaper() - workspace:draw() - - system.saveProperties() -end - -menu:addItem(system.localization.uploadToPastebin, not component.isAvailable("internet")).onTouch = function() - system.uploadToPastebin(icon.path) -end + +local system = require("System") + +local workspace, icon, menu = select(1, ...), select(2, ...), select(3, ...) + +menu:addItem(system.localization.setAsWallpaper).onTouch = function() + system.properties.interfaceWallpaperEnabled = true + system.properties.interfaceWallpaperPath = icon.path + + system.updateWallpaper() + workspace:draw() + + system.saveProperties() +end + +menu:addItem(system.localization.uploadToPastebin, not component.isAvailable("internet")).onTouch = function() + system.uploadToPastebin(icon.path) +end diff --git a/Extensions/Pkg/Launcher.lua b/Extensions/Pkg/Launcher.lua index 71ac9d90..1f12050e 100755 --- a/Extensions/Pkg/Launcher.lua +++ b/Extensions/Pkg/Launcher.lua @@ -1,8 +1,8 @@ -local path = select(1, ...) - -local success, reason = require("Compressor").unpack(path, require("Filesystem").path(path)) -if not success then - require("GUI").alert(reason) -end - +local path = select(1, ...) + +local success, reason = require("Compressor").unpack(path, require("Filesystem").path(path)) +if not success then + require("GUI").alert(reason) +end + computer.pushSignal("system", "updateFileList") \ No newline at end of file diff --git a/Libraries/Compressor.lua b/Libraries/Compressor.lua index c8a71c73..96715c49 100755 --- a/Libraries/Compressor.lua +++ b/Libraries/Compressor.lua @@ -3,40 +3,14 @@ local filesystem = require("Filesystem") ----------------------------------------------------------------------------------------------- -local compressor = {} -local OCAFSignature = "OCAF" -local readBufferSize = 1024 -local ignoredFiles = { - [".DS_Store"] = true +local compressor = { + readBufferSize = 1024, + ignoredFiles = { + [".DS_Store"] = true + } } ------------------------------------------------------------------------------------------------ - -local function numberToByteArray(number) - local byteArray = {} - - repeat - table.insert(byteArray, 1, bit32.band(number, 0xFF)) - number = bit32.rshift(number, 8) - until number <= 0 - - return byteArray -end - --- Записываем путь в виде <кол-во байт для размера пути> <размер пути> <путь> -local function writePath(handle, path) - -- Получаем юникод-байтики названия файла или папки - local pathBytes = {string.byte(path, 1, #path)} - - -- Записываем количество всякой хуйни - local bytesForCountPathBytes = numberToByteArray(#pathBytes) - - handle:writeBytes(#bytesForCountPathBytes) - handle:writeBytes(table.unpack(bytesForCountPathBytes)) - - -- Записываем путь - handle:write(path) -end +local OCAFSignature = "OCAF" ----------------------------------------------------------------------------------------------- @@ -49,22 +23,49 @@ function compressor.pack(archivePath, fileList) if handle then -- Writing signature handle:write(OCAFSignature) - + -- Writing encoding method (maybe will be used in future) + handle:writeBytes(0) + + local function numberToByteArray(number) + local byteArray, a, b = {} + + repeat + a = number / 256 + b = a - a % 1 + + table.insert(byteArray, 1, number - b * 256) + + number = b + until number <= 0 + + return byteArray + end + -- Recursive packing local function doPack(fileList, localPath) for i = 1, #fileList do local filename = filesystem.name(fileList[i]) - local currentLocalPath = localPath .. "/" .. filename + local currentLocalPath = localPath .. filename -- print("Writing path:", currentLocalPath) - if not ignoredFiles[filename] then - if filesystem.isDirectory(fileList[i]) then - handle:writeBytes(1) - writePath(handle, currentLocalPath) + if not compressor.ignoredFiles[filename] then + local isDirectory = filesystem.isDirectory(fileList[i]) + -- Writing byte of data type (0 is a file, 1 is a directory) + handle:writeBytes(isDirectory and 1 or 0) + -- Writing string path as + -- + -- + -- + local bytesForCountPathBytes = numberToByteArray(#currentLocalPath) + handle:writeBytes(#bytesForCountPathBytes) + handle:writeBytes(table.unpack(bytesForCountPathBytes)) + handle:write(currentLocalPath) + + if isDirectory then -- Obtaining new file list local newList = filesystem.list(fileList[i]) - -- Forming absolute paths for list + -- Creating absolute paths for list elements for j = 1, #newList do newList[j] = fileList[i] .. newList[j] end @@ -75,21 +76,17 @@ function compressor.pack(archivePath, fileList) return success, reason end else - handle:writeBytes(0) - writePath(handle, currentLocalPath) - local otherHandle, reason = filesystem.open(fileList[i], "rb") if otherHandle then - -- Пишем размер файла + -- Writing file size local fileSizeBytes = numberToByteArray(filesystem.size(fileList[i])) - handle:writeBytes(#fileSizeBytes) handle:writeBytes(table.unpack(fileSizeBytes)) - -- Пишем содержимое + -- Writing file contents local data while true do - data = otherHandle:readString(readBufferSize) + data = otherHandle:readString(compressor.readBufferSize) if data then handle:write(data) @@ -124,10 +121,12 @@ function compressor.unpack(archivePath, unpackPath) if handle then local readedSignature = handle:readString(#OCAFSignature) if readedSignature == OCAFSignature then + -- Reading encoding method *just in case* + handle:readBytes(1) + -- Reading contents while true do - local typeData = handle:readString(1) - if typeData then - local type = string.byte(typeData) + local type = handle:readBytes(1) + if type then -- Reading path local localPath = unpackPath .. handle:readString(handle:readBytes(handle:readBytes(1))) -- print("Readed path:", localPath) @@ -140,7 +139,7 @@ function compressor.unpack(archivePath, unpackPath) if otherHandle then local readedCount, needToRead, data = 0 while readedCount < fileSize do - needToRead = math.min(readBufferSize, fileSize - readedCount) + needToRead = math.min(compressor.readBufferSize, fileSize - readedCount) otherHandle:write(handle:readString(needToRead)) readedCount = readedCount + needToRead end diff --git a/Libraries/Filesystem.lua b/Libraries/Filesystem.lua index fa84935e..879dde17 100644 --- a/Libraries/Filesystem.lua +++ b/Libraries/Filesystem.lua @@ -21,8 +21,8 @@ function filesystem.path(path) return path:match("^(.+%/).") or "" end -function filesystem.name(path) - return path:match("%/?([^%/]+)%/?$") +function filesystem.name(path, removeSlashes) + return path:match(removeSlashes and "%/?([^%/]+)%/?$" or "%/?([^%/]+%/?)$") end function filesystem.extension(path, lower) diff --git a/Libraries/GUI.lua b/Libraries/GUI.lua index 9cadc713..280d2334 100755 --- a/Libraries/GUI.lua +++ b/Libraries/GUI.lua @@ -2482,7 +2482,7 @@ local function filesystemTreeUpdateFileListRecursively(tree, path, offset) if tree.showMode == GUI.IO_MODE_BOTH or tree.showMode == GUI.IO_MODE_DIRECTORY then for i = 1, #expandables do - tree:addItem(filesystem.name(expandables[i]), path .. expandables[i], offset, true) + tree:addItem(filesystem.name(expandables[i], true), path .. expandables[i], offset, true) if tree.expandedItems[path .. expandables[i]] then filesystemTreeUpdateFileListRecursively(tree, path .. expandables[i], offset + 2) diff --git a/Libraries/System.lua b/Libraries/System.lua index 31796112..27e313b3 100755 --- a/Libraries/System.lua +++ b/Libraries/System.lua @@ -482,9 +482,9 @@ function system.icon(x, y, path, textColor, selectionColor) icon.path = path icon.extension = filesystem.extension(icon.path) - icon.name = filesystem.name(path) - icon.nameWithoutExtension = filesystem.hideExtension(icon.name) icon.isDirectory = filesystem.isDirectory(icon.path) + icon.name = filesystem.name(path, true) + icon.nameWithoutExtension = filesystem.hideExtension(icon.name) icon.isShortcut = false icon.selected = false @@ -830,7 +830,7 @@ local function iconOnRightClick(icon, e1, e2, e3, e4) for i = 1, #selectedIcons do if not selectedIcons[i].isShortcut then system.createShortcut( - paths.user.desktop .. "/" .. selectedIcons[i].nameWithoutExtension .. ".lnk", + paths.user.desktop .. selectedIcons[i].nameWithoutExtension .. ".lnk", selectedIcons[i].path ) end