BadScript 2
Loading...
Searching...
No Matches
BadVirtualFileSystem.cs
Go to the documentation of this file.
2
4
9{
13 private readonly BadVirtualRoot m_Root = new BadVirtualRoot();
14
18 private string m_CurrentDirectory = "/";
19
20 public string GetStartupDirectory()
21 {
22 return "/";
23 }
24
25 public bool Exists(string path)
26 {
27 return IsFile(path) || IsDirectory(path);
28 }
29
30 public bool IsFile(string path)
31 {
34
35 for (int i = 0; i < parts.Length - 1; i++)
36 {
37 if (!current.DirectoryExists(parts[i]))
38 {
39 return false;
40 }
41
42 current = current.GetDirectory(parts[i]);
43 }
44
45 return current.FileExists(parts[parts.Length - 1]);
46 }
47
48 public bool IsDirectory(string path)
49 {
51
52 if (parts.Length == 1 && string.IsNullOrEmpty(parts[0]))
53 {
54 return true;
55 }
56
58
59 foreach (string t in parts)
60 {
61 if (!current.DirectoryExists(t))
62 {
63 return false;
64 }
65
66 current = current.GetDirectory(t);
67 }
68
69 return true;
70 }
71
72 public IEnumerable<string> GetFiles(string path, string extension, bool recursive)
73 {
74 if (recursive)
75 {
76 return GetFilesRecursive(
78 extension
79 );
80 }
81
83 }
84
85 public IEnumerable<string> GetDirectories(string path, bool recursive)
86 {
87 return recursive
90 }
91
92 public void CreateDirectory(string path, bool recursive = false)
93 {
96
97 for (int i = 0; i < parts.Length - 1; i++)
98 {
99 if (!current.DirectoryExists(parts[i]))
100 {
101 if (!recursive)
102 {
103 throw new IOException("Directory does not exist");
104 }
105
106 current = current.CreateDirectory(parts[i]);
107 }
108 else
109 {
110 current = current.GetDirectory(parts[i]);
111 }
112 }
113
114 current.CreateDirectory(parts[parts.Length - 1]);
115 }
116
117 public void DeleteDirectory(string path, bool recursive)
118 {
120 parent.DeleteDirectory(Path.GetFileName(path), recursive);
121 }
122
123 public void DeleteFile(string path)
124 {
126 parent.DeleteFile(Path.GetFileName(path));
127 }
128
129 public string GetFullPath(string path)
130 {
132 }
133
134 public Stream OpenRead(string path)
135 {
136 string fullPath = BadVirtualPathReader.ResolvePath(path, m_CurrentDirectory);
137
138 if (!Exists(fullPath) || !IsFile(fullPath))
139 {
140 throw new FileNotFoundException(fullPath);
141 }
142
144
145 string[] paths = BadVirtualPathReader.SplitPath(fullPath);
146
147 return dir.GetFile(paths[paths.Length - 1]).OpenRead();
148 }
149
150 public Stream OpenWrite(string path, BadWriteMode mode)
151 {
152 string fullPath = BadVirtualPathReader.ResolvePath(path, m_CurrentDirectory);
153
154 if (mode == BadWriteMode.Append && (!Exists(fullPath) || !IsFile(fullPath)))
155 {
156 throw new FileNotFoundException(path);
157 }
158
160 string[] paths = BadVirtualPathReader.SplitPath(fullPath);
161
162 return dir.GetOrCreateFile(paths[paths.Length - 1]).OpenWrite(mode);
163 }
164
165 public string GetCurrentDirectory()
166 {
167 return m_CurrentDirectory;
168 }
169
170 public void SetCurrentDirectory(string path)
171 {
172 if (!Exists(path) || !IsDirectory(path))
173 {
174 throw new DirectoryNotFoundException(path);
175 }
176
178 }
179
180 public void Copy(string src, string dst, bool overwrite = true)
181 {
182 if (IsDirectory(src))
183 {
184 if (IsSubfolderOf(src, dst))
185 {
186 throw new IOException("Cannot copy a directory to a subfolder of itself.");
187 }
188
189 if (!overwrite && IsDirectory(src))
190 {
191 throw new IOException("Directory already exists.");
192 }
193
194 CopyDirectoryToDirectory(src, dst);
195 }
196 else if (IsFile(src))
197 {
198 if (!overwrite && IsFile(src))
199 {
200 throw new IOException("File already exists.");
201 }
202
203 CopyFileToFile(src, dst);
204 }
205 else
206 {
207 throw new IOException("Source path is not a file or directory");
208 }
209 }
210
211 public void Move(string src, string dst, bool overwrite = true)
212 {
213 Copy(src, dst, overwrite);
214
215 if (IsDirectory(src))
216 {
217 DeleteDirectory(src, true);
218 }
219 else
220 {
221 DeleteFile(src);
222 }
223 }
224
230 {
231 return m_Root;
232 }
233
240 {
241 return GetDirectory(
245 .SkipLast(1)
246 )
247 );
248 }
249
256 {
257 if (string.IsNullOrEmpty(path) || BadVirtualPathReader.IsRootPath(path))
258 {
259 return m_Root;
260 }
261
262 string fullPath = BadVirtualPathReader.ResolvePath(path, m_CurrentDirectory);
263 string[] parts = BadVirtualPathReader.SplitPath(fullPath);
264
265 return parts.Aggregate<string, BadVirtualDirectory>(m_Root, (current1, t) => current1.GetDirectory(t));
266 }
267
273 private static IEnumerable<string> GetDirectories(BadVirtualDirectory directory)
274 {
275 return directory.Directories.Select(sub => sub.AbsolutePath);
276 }
277
283 private IEnumerable<string> GetDirectoriesRecursive(BadVirtualDirectory directory)
284 {
285 foreach (string s in GetDirectories(directory))
286 {
287 yield return s;
288 }
289
290 foreach (BadVirtualDirectory sub in directory.Directories)
291 {
292 foreach (string s in GetDirectoriesRecursive(sub))
293 {
294 yield return s;
295 }
296 }
297 }
298
305 private static IEnumerable<string> GetFiles(BadVirtualDirectory directory, string extension)
306 {
307 return from file in directory.Files where extension == "" || Path.GetExtension(file.Name) == extension select file.AbsolutePath;
308 }
309
316 private IEnumerable<string> GetFilesRecursive(BadVirtualDirectory directory, string extension)
317 {
318 foreach (string file in GetFiles(directory, extension))
319 {
320 yield return file;
321 }
322
323 foreach (BadVirtualDirectory subDirectory in directory.Directories)
324 {
325 foreach (string file in GetFilesRecursive(subDirectory, extension))
326 {
327 yield return file;
328 }
329 }
330 }
331
338 private bool IsSubfolderOf(string root, string sub)
339 {
340 return GetFullPath(sub).StartsWith(GetFullPath(root));
341 }
342
348 private void CopyFileToFile(string src, string dst)
349 {
350 using Stream s = OpenRead(src);
351 using Stream d = OpenWrite(dst, BadWriteMode.CreateNew);
352 s.CopyTo(d);
353 }
354
360 private void CopyDirectoryToDirectory(string src, string dst)
361 {
362 foreach (string directory in GetDirectories(src, true))
363 {
364 CreateDirectory(directory);
365 }
366
367 foreach (string file in GetFiles(src, "*", true))
368 {
369 CopyFileToFile(file, file.Replace(src, dst));
370 }
371 }
372}
Represents a Virtual File System Directory Entry.
BadVirtualDirectory GetDirectory(string name)
Returns an existing directory.
BadVirtualFile GetFile(string name)
Returns an existing File.
void DeleteDirectory(string name, bool recursive=true)
Deletes a subdirectory with the specified name.
bool FileExists(string name)
Returns true if the file with the specified name exist in this directory.
BadVirtualDirectory CreateDirectory(string name)
Returns the existing directory or Creates a new directory with the specified name.
IEnumerable< BadVirtualDirectory > Directories
Subdirectories.
void DeleteFile(string name)
Deletes a file from the current directory.
bool DirectoryExists(string name)
Returns true if the directory with the specified name exist in this directory.
BadVirtualFile GetOrCreateFile(string name)
Returns an existing file or creates a new file with the specified name.
Stream OpenWrite(BadWriteMode mode)
Returns a writable stream for the file.
Stream OpenRead()
Returns a readable stream for the file.
Virtual File System Implementation for the BadScript Engine.
Stream OpenWrite(string path, BadWriteMode mode)
Opens a file for writing.
void Move(string src, string dst, bool overwrite=true)
Moves a file or directory to a new location.
static IEnumerable< string > GetFiles(BadVirtualDirectory directory, string extension)
Returns the files in the specified path that match the specified extension.
bool IsFile(string path)
Returns true if the given path is a file.
IEnumerable< string > GetDirectories(string path, bool recursive)
Returns all directories in the given directory.
IEnumerable< string > GetFiles(string path, string extension, bool recursive)
Returns all files in the given directory that match the specified extension.
string GetCurrentDirectory()
Returns the Current Directory.
void CopyFileToFile(string src, string dst)
Copies a file to a file.
BadVirtualDirectory GetDirectory(string path)
Returns the directory at the specified path.
BadVirtualRoot GetRoot()
Returns the root directory of the filesystem.
IEnumerable< string > GetDirectoriesRecursive(BadVirtualDirectory directory)
Returns the directories in the specified path recursively.
string GetStartupDirectory()
The Startup Directory of the Application.
bool IsSubfolderOf(string root, string sub)
Returns true if sub is a subfolder of root.
void DeleteDirectory(string path, bool recursive)
Deletes a directory.
BadVirtualDirectory GetParentDirectory(string path)
Returns the Parent directory of the specified path.
Stream OpenRead(string path)
Opens a file for reading.
void SetCurrentDirectory(string path)
Sets the current Directory.
bool Exists(string path)
Returns true if the given path is a file or directory.
void CopyDirectoryToDirectory(string src, string dst)
Copies a directory to a directory.
static IEnumerable< string > GetDirectories(BadVirtualDirectory directory)
Returns the directories in the specified path.
IEnumerable< string > GetFilesRecursive(BadVirtualDirectory directory, string extension)
Returns the files in the specified path that match the specified extension recursively.
string GetFullPath(string path)
Returns the full path of the given path.
string m_CurrentDirectory
The Current Directory.
void DeleteFile(string path)
Deletes a file.
readonly BadVirtualRoot m_Root
The Root Directory.
void Copy(string src, string dst, bool overwrite=true)
Copies a file or directory to a new location.
bool IsDirectory(string path)
Returns true if the given path is a directory.
void CreateDirectory(string path, bool recursive=false)
Creates a new directory.
Implements Operations to read and manipulate File System Paths.
static string JoinPath(IEnumerable< string > parts)
static string ResolvePath(string path, string currentDir)
Implements a Virtual File System Root Directory.
Defines the interface for a file system.
Definition IFileSystem.cs:7
Contains the Implementation of the BadScript Virtual File System.
BadWriteMode
The Write Modes of the File System Abstraction.
Contains Utility Functions and Classes.
Definition BadEnum.cs:5