-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiles.xojo_code
More file actions
370 lines (304 loc) · 10.3 KB
/
Files.xojo_code
File metadata and controls
370 lines (304 loc) · 10.3 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#tag Module
Protected Module Files
#tag Method, Flags = &h0
Sub AddUndoAction()
// The undo is disabled because the realtime engine does not support undo actions yet
'dim undoFolder as folderitem
'dim filename as string
'
'//if the undo folder does not exist, we create it
'undoFolder = GetProjectFolder.child("undo")
'if not undoFolder.exists then
'undoFolder.createAsFolder
'filename = "0"
'else
'//We choose the new filename
'filename = cstr(undoFolder.Count)
'end if
'
'SaveProject GetFolderItem(GetProjectFolder.ShellPath).child("undo").child(filename)
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Function ChooseProject() As FolderItem
dim dialog as FolderItemDialog
dim file as FolderItem
dialog = new OpenDialog
dialog.promptText = "Select a project to open"
#if targetCarbon
dialog.filter = "Dungeon/project"
#endif
#if TargetWin32
dialog.filter = "*.spz"
#endif
file = dialog.showModal()
return file
End Function
#tag EndMethod
#tag Method, Flags = &h0
Sub cleanDataFolders()
dim tempString as string
dim file as new folderitem
dim i as integer
file = file.Child("Engines").Child("Phoenix")
if file <> nil then
for i = file.count downto 1
if file.item(i).directory then
tempString = left(file.item(i).name, 4)
if tempString = "data" then
deleteFolder(file.item(i))
end if
end if
next
end if
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub CopyFileOrFolder(source as FolderItem, destination as FolderItem, optional destinationName as String)
Dim newFolder as FolderItem
//We check for the existence of the file first
if source = nil then
Notify ("A needed file could not be located", "Your demo editor installation may be corrupt. Please reinstall it.")
exit
end if
if not source.exists then
Notify ("The file '" + source.Name + "' could not be located", "Your project may be corrupt")
exit
end if
If source.directory then //it's a folder
if left(source.name, 1) <> "." then // Avoid directories starting with "." like .svn or MacOS X folders
if destinationName = "" then
newFolder = destination.child(source.name)
else
newFolder = destination.Child(destinationName)
end if
newFolder.createAsFolder
For i as integer = 1 to source.count //go through each item
If source.item(i).directory then
//it's a folder
CopyFileOrFolder source.item(i), newFolder
//recursively call this routine passing it the folder
else
source.item(i).CopyFileTo newFolder
//it's a file so copy it
end if
next
end if
else //it's not a folder, so copy the item
if destinationName = "" then
destination.Delete
source.CopyFileTo destination
else
source.CopyFileTo destination.Child(destinationName)
end if
end if
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub deleteFile(target as folderitem)
// Function to delete a single file
if target = nil then
Trace("Files: deleteFile: Passed null folderitem. Nothing to do.", cstTraceLevelWarning)
return
end if
if not target.Directory then
target.Delete
else
Trace("Files: deleteFile: Passed folderitem (" + target.ShellPath + ") is not a file", cstTraceLevelWarning)
end if
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub deleteFilesOfExtension(theFolder as folderitem, extension as string)
dim result as string
dim command as string
if TargetMacOS then
command = "rm " + theFolder.ShellPath + "/" + "*." + extension
result = executeShell(command)
elseif TargetWindows then
command = "del """ + theFolder.ShellPath + "\" + "*.""" + extension
result = executeShell(command)
end if
Trace("Command: " + command + " Result: " + result, cstTraceLevelDebug)
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub deleteFolder(target as folderitem)
//Recursive function to erase a directory
if target = nil then return
for i as integer = target.count downto 1
if target.trueitem(i) <> nil and target.trueitem(i).Exists then deleteFolder(target.trueitem(i))
next
if target.exists then target.delete
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Function ExecuteShell(command as string) As string
//This method execute a shell command
Dim s As new Shell
s.execute (command)
return s.Result
End Function
#tag EndMethod
#tag Method, Flags = &h0
Function fileExists(theFilePath as String) As boolean
dim f as folderitem
f = GetFolderItem(theFilePath)
if f = nil then return false
return f.Exists
End Function
#tag EndMethod
#tag Method, Flags = &h1
Protected Sub GetFileList(extension as String, byRef fileList() as FolderItem, startPath as FolderItem)
dim i, j as integer
dim fileExtension as String
// We repeat for each item inside the current directory:
for i=1 to startPath.count
if startPath.item(i).Directory then
// If the file is a directory we scan it (using recursiveness)
GetFileList(extension, fileList(), startPath.Child(startPath.item(i).name))
else
// The file is not a directory so we check if it is of the searched type
for j=1 to CountFields(extension, ",")
fileExtension = lowercase(NthField(startPath.item(i).name, ".", CountFields(startPath.item(i).name, ".")))
if NthField(extension, ",", j) = fileExtension then
//We add the item to the array
ReDim fileList(UBound(fileList) + 1)
fileList(UBound(fileList)) = startPath.item(i)
exit
end if
next
end if
next
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Function GetForcedFolderItem(theShellPath as String) As FolderItem
// This function returns a folderitem for the given shellpath.
// If the route to the sellpath does not exist, it creates it
// The Shell Path must be a file path, but the return path will be a folder path
dim i as integer
dim myFolderItem as FolderItem
dim folderDepth as integer
if TargetMacOS then
folderDepth = CountFields(theShellPath, "/") - 1
theShellPath = ReplaceAll(theShellPath, "\ ", " ")
myFolderItem = GetFolderItem("/" + NthField(theShellPath, "/", 1), FolderItem.PathTypeShell)
else
theShellPath = ReplaceAll(theShellPath, "/", "\")
folderDepth = CountFields(theShellPath, "\") - 1
myFolderItem = Volume(0)
end if
for i=2 to folderDepth
#if TargetMacOS then
if not myFolderItem.Child(NthField(theShellPath, "/", i)).Exists then
#else
if not myFolderItem.Child(NthField(theShellPath, "\", i)).Exists then
#endif
if TargetMacOS then
myFolderItem.Child(NthField(theShellPath, "/", i)).CreateAsFolder
else
myFolderItem.Child(NthField(theShellPath, "\", i)).CreateAsFolder
end if
end if
if TargetMacOS then
myFolderItem = myFolderItem.Child(NthField(theShellPath, "/", i))
else
myFolderItem = myFolderItem.Child(NthField(theShellPath, "\", i).DefineEncoding(Encodings.UTF8))
end if
next
return myFolderItem
End Function
#tag EndMethod
#tag Method, Flags = &h1
Protected Function GetRelativePathName(file as FolderItem) As String
dim theRelativePath as String
dim elementName as String
// We get the root file name
elementName = file.name
// And get the relative path
if InStr(0, file.ShellPath, "pool") = 0 then return ""
while elementName <> "pool"
theRelativePath = elementName + "/" + theRelativePath
file = file.parent
elementName = file.name
wend
// And return the result, trimming the last character (a "/")
return left(theRelativePath, len(theRelativePath) - 1)
End Function
#tag EndMethod
#tag Method, Flags = &h0
Sub getUsedFileSystem(ByRef fileList() as String, startFolder as FolderItem, prefix as String)
dim i as integer
dim filePath as String
for i=1 to startFolder.Count
// If the item we are looking for is a directory, recursively add it
if startFolder.child(startfolder.item(i).name).Directory then
getUsedFileSystem (fileList(), startFolder.child(startfolder.item(i).name), prefix + startFolder.child(startfolder.item(i).Name).name + "/")
else
filePath = prefix + startFolder.child(startFolder.item(i).Name).name
fileList.Append filePath
end if
next
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub rewriteFile(source as folderitem, destination as FolderItem)
Dim inputStream as BinaryStream = BinaryStream.Open(source, false)
If destination <> Nil Then
Try
dim outputStream as BinaryStream = BinaryStream.Create(destination, true)
outputStream.LittleEndian = not inputStream.LittleEndian
Do Until inputStream.EOF
outputStream.WriteUInt8 inputStream.ReadUInt8
Loop
Catch exc as IOException
MsgBox "Oops - failed to create the output file."
End Try
End If
End Sub
#tag EndMethod
#tag ViewBehavior
#tag ViewProperty
Name="Index"
Visible=true
Group="ID"
InitialValue="-2147483648"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Left"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Name"
Visible=true
Group="ID"
InitialValue=""
Type="String"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Super"
Visible=true
Group="ID"
InitialValue=""
Type="String"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Top"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag EndViewBehavior
End Module
#tag EndModule