mirror of
https://github.com/IgorTimofeev/MineOS.git
synced 2025-12-20 02:59:20 +01:00
Added filesystem.name(path[, removeEndSlash) option
This commit is contained in:
parent
272924ab4f
commit
9eab5d6d65
@ -3,40 +3,14 @@ local filesystem = require("Filesystem")
|
|||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
local compressor = {}
|
local compressor = {
|
||||||
local OCAFSignature = "OCAF"
|
readBufferSize = 1024,
|
||||||
local readBufferSize = 1024
|
ignoredFiles = {
|
||||||
local ignoredFiles = {
|
[".DS_Store"] = true
|
||||||
[".DS_Store"] = true
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
local OCAFSignature = "OCAF"
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
@ -49,22 +23,49 @@ function compressor.pack(archivePath, fileList)
|
|||||||
if handle then
|
if handle then
|
||||||
-- Writing signature
|
-- Writing signature
|
||||||
handle:write(OCAFSignature)
|
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
|
-- Recursive packing
|
||||||
local function doPack(fileList, localPath)
|
local function doPack(fileList, localPath)
|
||||||
for i = 1, #fileList do
|
for i = 1, #fileList do
|
||||||
local filename = filesystem.name(fileList[i])
|
local filename = filesystem.name(fileList[i])
|
||||||
local currentLocalPath = localPath .. "/" .. filename
|
local currentLocalPath = localPath .. filename
|
||||||
-- print("Writing path:", currentLocalPath)
|
-- print("Writing path:", currentLocalPath)
|
||||||
|
|
||||||
if not ignoredFiles[filename] then
|
if not compressor.ignoredFiles[filename] then
|
||||||
if filesystem.isDirectory(fileList[i]) then
|
local isDirectory = filesystem.isDirectory(fileList[i])
|
||||||
handle:writeBytes(1)
|
|
||||||
writePath(handle, currentLocalPath)
|
|
||||||
|
|
||||||
|
-- Writing byte of data type (0 is a file, 1 is a directory)
|
||||||
|
handle:writeBytes(isDirectory and 1 or 0)
|
||||||
|
-- Writing string path as
|
||||||
|
-- <N bytes for reading a number that represents J bytes of path length>
|
||||||
|
-- <J bytes for reading a number that represents path length>
|
||||||
|
-- <PathLength bytes of path>
|
||||||
|
local bytesForCountPathBytes = numberToByteArray(#currentLocalPath)
|
||||||
|
handle:writeBytes(#bytesForCountPathBytes)
|
||||||
|
handle:writeBytes(table.unpack(bytesForCountPathBytes))
|
||||||
|
handle:write(currentLocalPath)
|
||||||
|
|
||||||
|
if isDirectory then
|
||||||
-- Obtaining new file list
|
-- Obtaining new file list
|
||||||
local newList = filesystem.list(fileList[i])
|
local newList = filesystem.list(fileList[i])
|
||||||
-- Forming absolute paths for list
|
-- Creating absolute paths for list elements
|
||||||
for j = 1, #newList do
|
for j = 1, #newList do
|
||||||
newList[j] = fileList[i] .. newList[j]
|
newList[j] = fileList[i] .. newList[j]
|
||||||
end
|
end
|
||||||
@ -75,21 +76,17 @@ function compressor.pack(archivePath, fileList)
|
|||||||
return success, reason
|
return success, reason
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
handle:writeBytes(0)
|
|
||||||
writePath(handle, currentLocalPath)
|
|
||||||
|
|
||||||
local otherHandle, reason = filesystem.open(fileList[i], "rb")
|
local otherHandle, reason = filesystem.open(fileList[i], "rb")
|
||||||
if otherHandle then
|
if otherHandle then
|
||||||
-- Пишем размер файла
|
-- Writing file size
|
||||||
local fileSizeBytes = numberToByteArray(filesystem.size(fileList[i]))
|
local fileSizeBytes = numberToByteArray(filesystem.size(fileList[i]))
|
||||||
|
|
||||||
handle:writeBytes(#fileSizeBytes)
|
handle:writeBytes(#fileSizeBytes)
|
||||||
handle:writeBytes(table.unpack(fileSizeBytes))
|
handle:writeBytes(table.unpack(fileSizeBytes))
|
||||||
|
|
||||||
-- Пишем содержимое
|
-- Writing file contents
|
||||||
local data
|
local data
|
||||||
while true do
|
while true do
|
||||||
data = otherHandle:readString(readBufferSize)
|
data = otherHandle:readString(compressor.readBufferSize)
|
||||||
|
|
||||||
if data then
|
if data then
|
||||||
handle:write(data)
|
handle:write(data)
|
||||||
@ -124,10 +121,12 @@ function compressor.unpack(archivePath, unpackPath)
|
|||||||
if handle then
|
if handle then
|
||||||
local readedSignature = handle:readString(#OCAFSignature)
|
local readedSignature = handle:readString(#OCAFSignature)
|
||||||
if readedSignature == OCAFSignature then
|
if readedSignature == OCAFSignature then
|
||||||
|
-- Reading encoding method *just in case*
|
||||||
|
handle:readBytes(1)
|
||||||
|
-- Reading contents
|
||||||
while true do
|
while true do
|
||||||
local typeData = handle:readString(1)
|
local type = handle:readBytes(1)
|
||||||
if typeData then
|
if type then
|
||||||
local type = string.byte(typeData)
|
|
||||||
-- Reading path
|
-- Reading path
|
||||||
local localPath = unpackPath .. handle:readString(handle:readBytes(handle:readBytes(1)))
|
local localPath = unpackPath .. handle:readString(handle:readBytes(handle:readBytes(1)))
|
||||||
-- print("Readed path:", localPath)
|
-- print("Readed path:", localPath)
|
||||||
@ -140,7 +139,7 @@ function compressor.unpack(archivePath, unpackPath)
|
|||||||
if otherHandle then
|
if otherHandle then
|
||||||
local readedCount, needToRead, data = 0
|
local readedCount, needToRead, data = 0
|
||||||
while readedCount < fileSize do
|
while readedCount < fileSize do
|
||||||
needToRead = math.min(readBufferSize, fileSize - readedCount)
|
needToRead = math.min(compressor.readBufferSize, fileSize - readedCount)
|
||||||
otherHandle:write(handle:readString(needToRead))
|
otherHandle:write(handle:readString(needToRead))
|
||||||
readedCount = readedCount + needToRead
|
readedCount = readedCount + needToRead
|
||||||
end
|
end
|
||||||
|
|||||||
@ -21,8 +21,8 @@ function filesystem.path(path)
|
|||||||
return path:match("^(.+%/).") or ""
|
return path:match("^(.+%/).") or ""
|
||||||
end
|
end
|
||||||
|
|
||||||
function filesystem.name(path)
|
function filesystem.name(path, removeSlashes)
|
||||||
return path:match("%/?([^%/]+)%/?$")
|
return path:match(removeSlashes and "%/?([^%/]+)%/?$" or "%/?([^%/]+%/?)$")
|
||||||
end
|
end
|
||||||
|
|
||||||
function filesystem.extension(path, lower)
|
function filesystem.extension(path, lower)
|
||||||
|
|||||||
@ -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
|
if tree.showMode == GUI.IO_MODE_BOTH or tree.showMode == GUI.IO_MODE_DIRECTORY then
|
||||||
for i = 1, #expandables do
|
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
|
if tree.expandedItems[path .. expandables[i]] then
|
||||||
filesystemTreeUpdateFileListRecursively(tree, path .. expandables[i], offset + 2)
|
filesystemTreeUpdateFileListRecursively(tree, path .. expandables[i], offset + 2)
|
||||||
|
|||||||
@ -482,9 +482,9 @@ function system.icon(x, y, path, textColor, selectionColor)
|
|||||||
|
|
||||||
icon.path = path
|
icon.path = path
|
||||||
icon.extension = filesystem.extension(icon.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.isDirectory = filesystem.isDirectory(icon.path)
|
||||||
|
icon.name = filesystem.name(path, true)
|
||||||
|
icon.nameWithoutExtension = filesystem.hideExtension(icon.name)
|
||||||
icon.isShortcut = false
|
icon.isShortcut = false
|
||||||
icon.selected = false
|
icon.selected = false
|
||||||
|
|
||||||
@ -830,7 +830,7 @@ local function iconOnRightClick(icon, e1, e2, e3, e4)
|
|||||||
for i = 1, #selectedIcons do
|
for i = 1, #selectedIcons do
|
||||||
if not selectedIcons[i].isShortcut then
|
if not selectedIcons[i].isShortcut then
|
||||||
system.createShortcut(
|
system.createShortcut(
|
||||||
paths.user.desktop .. "/" .. selectedIcons[i].nameWithoutExtension .. ".lnk",
|
paths.user.desktop .. selectedIcons[i].nameWithoutExtension .. ".lnk",
|
||||||
selectedIcons[i].path
|
selectedIcons[i].path
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user