-
Notifications
You must be signed in to change notification settings - Fork 0
/
smf.lua
152 lines (134 loc) · 4.46 KB
/
smf.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
-- Program Data
require("extra.programMetadata") -- Constants used throughout the program.
-- Compression Tools
local compressors = {
-- Common but strong compressors
["7z"] = require("compressors.7z"),
["xz"] = require("compressors.xz"),
-- Less common, but advantaged when used right.
-- ["upx"] = require("compressors.upx"),
["dolphin-tool"] = require("compressors.dolphin-tool")
-- ["nsz"] = require("compressors.nsz")
}
local bannedExtensions = require("extra.bannedExtensions")
-- External Modules
local lfs = require("lfs")
-- Custom Modules
local fsUtils = require("modules.fsUtils")
local logSystem = require("modules.logSystem")
-- Now we can start listening to the user.
ARGUMENTS = require("modules.argumentManager")
-- Variables
local filesSkipped = 0
local checkedCompressors = {}
-- Cache folder
if (not fsUtils.exists(CACHE_FOLDER)) then
logSystem.log("debug", "Creating stomp-my-files folder in cache...")
lfs.mkdir(CACHE_FOLDER)
end
-- Checks
local function checkForCompressor(compressorType)
-- If the compressor wasn't already checked
if (not checkedCompressors[compressorType]) then
-- We check it through its provided function, and if we succeed, it's added to the list.
checkedCompressors[compressorType] = compressors[compressorType].check()
end
-- We return its status.
return checkedCompressors[compressorType]
end
local function attemptOperation(name, type, input)
local availability = checkForCompressor(name)
if (availability == "system" or availability == "local" or availability == "notDefined") then
if type == "compress" then
return compressors[name].compress(input)
elseif type == "decompress" then
return compressors[name].decompress(input)
else
error("Unknown operation type "..type..".")
end
else
logSystem.log("warning", "Could not provide "..name.." for \""..input.."\". Skipping...")
filesSkipped = filesSkipped + 1
return "failed"
end
end
--[[
MAIN PROGRAM
]]
logSystem.log("debug", "Beginning main function...")
for _,input in ipairs(ARGUMENTS.toBeCompressed) do
-- We get rid of the possibly present unnecessary / character on folders.
if (input:sub(-1) == "/") then
input = input:sub(1,-2)
end
if (fsUtils.isDirectory(input)) then
logSystem.log("debug", "File "..input.." is a directory. Using 7z...")
-- For now, we only use 7z for folders.
if (not attemptOperation("7z", "compress", input)) then
filesSkipped = filesSkipped + 1
end
else
-- The extension is our current way to determine which tool to use.
local extension = fsUtils.getExtension(input)
if extension == nil then
logSystem.log("info", "File "..input.." has no extension. Assuming it's a compiled executable...")
else
logSystem.log("debug", "File "..input.." uses extension "..extension.." .")
end
-- We check the file isn't banned.
if (bannedExtensions[extension]) then
logSystem.log("warning", "File "..input.." will not compress well. Skipping...")
filesSkipped = filesSkipped + 1
break
end
-- We use a switch to determine which compressor to use.
local switch = {
["7z"] = function()
attemptOperation("7z", "decompress", input)
end,
["xz"] = function()
attemptOperation("xz", "decompress", input)
end,
["iso"] = function()
if ARGUMENTS.settings.isoMode == "wii_gc" then
attemptOperation("dolphin-tool", "compress", input)
elseif ARGUMENTS.settings.isoMode == "iso" then
attemptOperation("xz", "compress", input)
else
logSystem.log("warning", "No behavior specified for ISOs. Skipping...")
filesSkipped = filesSkipped + 1
end
end,
["rvz"] = function()
attemptOperation("dolphin-tool", "decompress", input)
end
}
local f = switch[extension]
if (f) then
f()
else
-- We rely on xz as a fallback to unsupported extensions.
if extension ~= nil then
logSystem.log(
"debug",
"No particular behavior defined for "..extension..". Relying on default compression behavior..."
)
else
logSystem.log(
"debug",
"No particular behavior defined for files without extension (yet). Relying on default compression behavior..."
)
end
attemptOperation("xz", "compress", input)
end
end
end
-- End of program routine
if (filesSkipped ~= 0) then
if (filesSkipped == #ARGUMENTS.toBeCompressed) then
logSystem.log("warning", "ATTENTION ! All files/folders were skipped.")
else
logSystem.log("warning", "ATTENTION ! One or more files/folders were skipped.")
end
end
logSystem.log("info", "Exiting StompMyFiles...")