BadScript 2
Loading...
Searching...
No Matches
BadVirtualDirectory.cs
Go to the documentation of this file.
2
7
12{
16 private readonly List<BadVirtualDirectory> m_Directories = new List<BadVirtualDirectory>();
17
21 private readonly List<BadVirtualFile> m_Files = new List<BadVirtualFile>();
22
28 protected BadVirtualDirectory(string name, BadVirtualDirectory? parent) : base(name, parent) { }
29
33 public bool IsEmpty => m_Files.Count == 0 && m_Directories.All(x => x.IsEmpty);
34
35
39 public IEnumerable<BadVirtualDirectory> Directories => m_Directories;
40
44 public IEnumerable<BadVirtualFile> Files => m_Files;
45
49 public string AbsolutePath => Parent?.AbsolutePath + Name + "/";
50
54 public override bool HasChildren => m_Directories.Count != 0 || m_Files.Count != 0;
55
59 public override IEnumerable<BadVirtualNode> Children => m_Directories.Concat(m_Files.Cast<BadVirtualNode>());
60
66 public bool FileExists(string name)
67 {
68 return m_Files.Any(x => x.Name == name);
69 }
70
76 public bool DirectoryExists(string name)
77 {
78 return m_Directories.Any(x => x.Name == name);
79 }
80
87 public void DeleteDirectory(string name, bool recursive = true)
88 {
89 BadLogger.Log($"Deleting Directory {AbsolutePath}{name}", "BFS");
90 BadVirtualDirectory? dir = m_Directories.FirstOrDefault(x => x.Name == name);
91
92 if (dir == null)
93 {
94 return;
95 }
96
97 if (!dir.IsEmpty && !recursive)
98 {
99 throw new IOException("Directory is not empty");
100 }
101
102 m_Directories.Remove(dir);
103 }
104
109 public void DeleteFile(string name)
110 {
111 BadLogger.Log($"Deleting File {AbsolutePath}{name}", "BFS");
112 m_Files.RemoveAll(x => x.Name == name);
113 }
114
121 {
122 BadLogger.Log($"Create Directory {AbsolutePath}{name}", "BFS");
124
125 if (!DirectoryExists(name))
126 {
127 dir = new BadVirtualDirectory(name, this);
128 m_Directories.Add(dir);
129 }
130 else
131 {
132 dir = GetDirectory(name);
133 }
134
135 return dir;
136 }
137
145 {
146 return m_Directories.FirstOrDefault(x => x.Name == name) ??
147 throw new Exception($"Directory '{name}' not found in {this}");
148 }
149
155 public BadVirtualFile CreateFile(string name)
156 {
157 BadLogger.Log($"Creating File {AbsolutePath}{name}", "BFS");
158
159 if (FileExists(name))
160 {
161 throw new Exception($"File {name} already exists in {this}");
162 }
163
164 BadVirtualFile file = new BadVirtualFile(name, this);
165 m_Files.Add(file);
166
167 return file;
168 }
169
175 public BadVirtualFile GetOrCreateFile(string name)
176 {
177 return FileExists(name) ? GetFile(name) : CreateFile(name);
178 }
179
186 public BadVirtualFile GetFile(string name)
187 {
188 return m_Files.FirstOrDefault(x => x.Name == name) ?? throw new Exception($"File '{name}' not found in {this}");
189 }
190}
Public facing interface for a logger.
Definition BadLogger.cs:7
static void Log(string message)
Writes a Log to the Message Handler.
Definition BadLogger.cs:26
Represents a Virtual File System Directory Entry.
readonly List< BadVirtualDirectory > m_Directories
Subdirectories.
override IEnumerable< BadVirtualNode > Children
Returns a list of all children of this Directory.
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.
override bool HasChildren
Returns true if the Directory contains a at least one subdirectory or file.
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.
bool IsEmpty
Returns true if the Directory contains no files and no files in the subdirectories.
IEnumerable< BadVirtualDirectory > Directories
Subdirectories.
IEnumerable< BadVirtualFile > Files
Files inside this directory.
BadVirtualFile CreateFile(string name)
Returns the existing file or Creates a new file with the specified name.
BadVirtualDirectory(string name, BadVirtualDirectory? parent)
Creates a new Virtual Directory.
readonly List< BadVirtualFile > m_Files
Files inside this directory.
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.
string AbsolutePath
The Absolute Path of this Directory.
BadVirtualFile GetOrCreateFile(string name)
Returns an existing file or creates a new file with the specified name.
Implements a Virtual File System File.
Base class for all Virtual Filesystem Nodes.
BadVirtualDirectory? Parent
Parent Directory.
Contains Logging system for the BadScript Runtime.
Definition BadLog.cs:6
Contains the Implementation of the BadScript Virtual File System.