This repository has been archived by the owner on Nov 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
luaTar_gg.lua
230 lines (219 loc) · 5.36 KB
/
luaTar_gg.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
-- This is the GameGuardian UI wrapper for luaTar
-- make sure you have the luaTar.lua script as well :D
local tar,err = loadfile("luaTar.lua")
if type(tar) ~= "function" or err then print("luaTar.lua is not exist or unable to be loaded! this GameGuardian wrapper depends on it. Details: ",err) return end
tar = tar()
-- Helper function
function table.tostring(t,dp)
dp = dp or 0
local r,tab,tv = '{\n',('\t'):rep(dp)
local tabb = tab..'\t'
for k,v in pairs(t) do
r = r..tabb
tv = type(v)
if type(k) == 'string' then
r = r..k..' = '
end
if tv == 'table' then
r = r..table.tostring(v,dp+1)
elseif tv == 'boolean' or tv == 'number' then
r = r..tostring(v)
elseif tv == 'function' then
r = r.."funcion()end"
else
r = r..'"'..v..'"'
end
r = r..',\n'
end
return r..tab..'}'
end
-- Tests (also acts as an example as well :/)
function runTests()
local fileName = "Test.tar"
os.remove(fileName)
print("\nTar — Add Files")
print("- a.txt")
tar.add(fileName,"a.txt","This is text Alpha.")
print("- b.txt")
tar.add(fileName,"b.txt","This is text Bravo.")
print("- folder/")
tar.add(fileName,"folder/")
print("- folder/c.txt")
tar.add(fileName,"folder/c.txt","This is text Charlie.")
print("\nTar — Parse header")
print(table.tostring(tar.parseHeader(fileName)))
print("\nTar — Remove Bravo")
tar.remove(fileName,"b.txt")
print("\nTar — Query File")
local fileList = tar.queryFile(fileName)
for i=1,#fileList do print(i,fileList[i]) end
print("\nTar — Query Folder")
local fileList = tar.queryFolder(fileName)
for i=1,#fileList do print(i,fileList[i]) end
print("\nTar — Extract")
print(1,tar.extract(fileName,"a.txt"))
print(2,tar.extract(fileName,"b.txt") or "<removed>")
print(3,tar.extract(fileName,"folder/c.txt"))
print("\nTar — End of Test.")
os.remove(fileName)
end
--runTests()
-- GG Wrapper
io.readFile = function(path,opMode,readMode)
local file = io.open(path,opMode)
local content = file:read(readMode)
file:close()
return content
end
local opts = {
tarPath = gg.getFile():match("(.+)/"),
tarName = "packedStuff.tar",
filePath = gg.getFile(),
}
while true do
local CH = gg.choice({
"1. Add",
"2. Add Folder",
"3. Extract",
"4. Remove",
"5. List files",
"6. Parse Tar header",
"Run tests & Exit",
"Exit",
},nil,"ABJ4403's Lua Tape Archive")
if not CH then
break
elseif CH == 1 then
CH = gg.prompt({
"Output of the Tar archive",
"Name of Tar archive",
"File to be added",
},{
opts.tarPath,
opts.tarName,
opts.filePath
},{
"path",
"text",
"file",
})
if CH and CH[1] and CH[2] and CH[3] then
opts.tarPath = CH[1]
opts.tarName = CH[2]:gsub("%.tar$","")..".tar"
opts.tarFile = opts.tarPath..'/'..opts.tarName
opts.filePath = CH[3]
opts.fileName = CH[3]:gsub('(.+)/','')
local fileContent = io.readFile(CH[3],'r','*a')
if not fileContent then gg.toast("File cant be opened")
elseif tar.add(opts.tarFile,opts.fileName,fileContent) then gg.toast("Archive created!")
else gg.toast("Cant create archive path, is the path correct?") end
end
elseif CH == 2 then
CH = gg.prompt({
"Output of the Tar archive",
"Name of Tar archive",
"Folder name",
},{
opts.tarPath,
opts.tarName,
"New Folder"
},{
"path",
"text",
"text",
})
if CH and CH[1] and CH[2] and CH[3] then
opts.tarPath = CH[1]
opts.tarName = CH[2]:gsub("%.tar$","")..".tar"
opts.tarFile = opts.tarPath..'/'..opts.tarName
opts.folderName = CH[3].."/"
if tar.add(opts.tarFile,opts.folderName) then gg.toast("Archive created!")
else gg.toast("Cant create archive path, is the path correct?") end
end
elseif CH == 3 then
CH = gg.prompt({
"Name of Tar archive",
},{
opts.tarPath..'/'..opts.tarName,
},{
"file",
})
if CH and CH[1] then
opts.tarFile = CH[1]
local fileList = tar.queryFile(CH[1])
if fileList[1] then
local CH = gg.multiChoice(fileList,nil,"Select files to be extracted")
if CH then
for i=1,#fileList do
if CH[i] then
io.open(opts.tarPath..'/'..fileList[i]:gsub("/","_"),"wb"):write(
tar.extract(opts.tarFile,fileList[i])
):close()
end
end
end
else
gg.toast("Unknown error")
end
end
elseif CH == 4 then
CH = gg.prompt({
"Name of Tar archive",
},{
opts.tarPath..'/'..opts.tarName,
},{
"file",
})
if CH and CH[1] then
opts.tarFile = CH[1]
local fileList = {
table.unpack(tar.queryFolder(CH[1])),
table.unpack(tar.queryFile(CH[1]))
}
if fileList[1] then
local CH = gg.multiChoice(fileList,nil,"Select files to be removed")
if CH then
for i=1,#fileList do
if CH[i] then
tar.remove(opts.tarFile,fileList[i])
end
end
end
else
gg.toast("Unknown error")
end
end
elseif CH == 5 then
CH = gg.prompt({
"Name of Tar archive",
},{
opts.tarPath..'/'..opts.tarName,
},{
"file",
})
if CH and CH[1] then
local fileList = tar.queryFolder(CH[1])
for i=1,#fileList do print(i,fileList[i]) end
local fileList = tar.queryFile(CH[1])
for i=1,#fileList do print(i,fileList[i]) end
break
end
elseif CH == 6 then
CH = gg.prompt({
"Name of Tar archive",
},{
opts.tarPath..'/'..opts.tarName,
},{
"file",
})
if CH and CH[1] then
print(table.tostring(tar.parseHeader(CH[1])))
break
end
elseif CH == 7 then
runTests()
break
elseif CH == 8 then
break
end
end