public static HashDir CreateDir(HashDir parent, string name) { var hashDir = new HashDir(); hashDir.DirId = MathUtil.GetStringHash(name); hashDir.Name = name; hashDir.Childs = SList.Create <HashDir>(1); hashDir.ChildsDirId = SList.Create <int>(1); hashDir.Files = SList.Create <HashFile>(1); hashDir.FilesId = SList.Create <int>(1); hashDir.ParentDir = parent; if (parent != null) { hashDir.ParentDirId = parent.DirId; SList.Add(parent.Childs, hashDir); SList.Add(parent.ChildsDirId, hashDir.DirId); } else { hashDir.ParentDirId = -1; } CacheDirFullPath(hashDir); return(hashDir); }
/// <summary> /// Returns true if the given dir is avaiable for use. /// </summary> public static bool IsDirAvaibale(HashDir dir) { var files = GetAvailableFilesFromDir(dir); var childs = GetAllAvailableChild(dir); return(files.Count > 0 || childs.Count > 0); }
/// <summary> /// Changes the current dir to the given dir. /// </summary> public static void ChangeDir(HashDir dir) { var data = DataHolder.DeviceData.CurrentDevice.FileSystem; data.CurrentDir = dir; TerminalUtil.UpdateCurrentPathLabel(); TerminalUtil.UpdateCommandBuffer(); }
public static void AddFileToDir(HashDir dir, HashFile file) { file.ParentDir = dir; file.ParentDirId = dir.DirId; SList.Add(dir.Files, file); SList.Add(dir.FilesId, file.FileId); CacheFile(file); }
public static HashFile CreateImageFile(HashDir parent, string name, Texture2D image) { var hashFile = CreateBaseFile(parent, name); var imageFile = new ImageFile(); imageFile.ImageContent = image; hashFile.Content = imageFile; hashFile.FileType = HashFileType.Image; CacheFile(hashFile); return(hashFile); }
public static HashFile CreateTextFile(HashDir parent, string name, string text) { var hashFile = CreateBaseFile(parent, name); var textFile = new TextFile(); textFile.EncryptedTextContent = TextUtil.EncryptString(text); textFile.Story = new Ink.Runtime.Story(text); hashFile.Content = textFile; hashFile.FileType = HashFileType.Text; CacheFile(hashFile); return(hashFile); }
/// <summary> /// Finds a child of the parent folder by name. /// If no child is found, null is returned. /// </summary> public static HashDir FindChildByName(HashDir parent, string childName) { var childs = GetAllAvailableChild(parent); for (int i = 0; i < childs.Count; i++) { var child = childs[i]; if (string.Equals(child.Name, childName, StringComparison.InvariantCultureIgnoreCase)) { return(child); } } return(null); }
/// <summary> /// Creates and returns a dir from the given serialized data. /// </summary> public static HashDir GetDirFromSerializedData(SerializedHashDir serializedDir) { var dir = new HashDir(); dir.DirId = serializedDir.DirId; dir.ParentDirId = serializedDir.ParentDirId; dir.Name = serializedDir.Name; dir.ChildsDirId = SList.FromArray(serializedDir.ChildsDirId); dir.FilesId = SList.FromArray(serializedDir.FilesId); dir.Childs = SList.Create <HashDir>(dir.ChildsDirId.Count); dir.Files = SList.Create <HashFile>(dir.FilesId.Count); return(dir); }
/// <summary> /// Returns a list containing all of the dir files. /// </summary> public static void CacheDirFiles(HashDir dir) { SList.Clear(dir.Files); for (int i = 0; i < dir.FilesId.Count; i++) { var fileId = dir.FilesId[i]; var file = FindFileById(fileId); DebugUtil.Assert(file == null, string.Format("THE DIR {0} HAS A INVALID FILE {1}", dir.Name, fileId)); file.ParentDirId = dir.DirId; file.ParentDir = dir; SList.Add(dir.Files, file); } }
/// <summary> /// Returns a list containing all the files of the given dir that are available for use. /// See IsFileAvailable. /// </summary> public static SimpleList <HashFile> GetAvailableFilesFromDir(HashDir dir) { var result = SList.Create <HashFile>(1); for (int i = 0; i < dir.Files.Count; i++) { var file = dir.Files[i]; if (IsFileAvaibale(file)) { SList.Add(result, file); } } return(result); }
public static SimpleList <HashDir> GetAllAvailableChild(HashDir dir) { var result = SList.Create <HashDir>(dir.Childs.Count); for (int i = 0; i < dir.Childs.Count; i++) { var child = dir.Childs[i]; if (IsDirAvaibale(child)) { SList.Add(result, child); } } return(result); }
private static HashFile CreateBaseFile(HashDir parent, string name) { var hashFile = new HashFile(); hashFile.Name = GetValidFileName(parent, name); hashFile.FileId = MathUtil.GetStringHash(name); hashFile.UserPermission = SList.Create <ClassPair <string, AccessPermission> >(1); hashFile.Condition = new HashStory.Condition(); hashFile.Condition.MinimalDays = HashStory.MainState.CurrentDay; AddFileToDir(parent, hashFile); return(hashFile); }
/// <summary> /// Cache the dirs children based on the ChildsDirid values. /// </summary> public static void CacheDirChildren(HashDir dir) { SList.Clear(dir.Childs); for (int i = 0; i < dir.ChildsDirId.Count; i++) { var childId = dir.ChildsDirId[i]; var child = FindDirById(childId); DebugUtil.Assert(child == null, string.Format("THE DIR {0} HAS A INVALID CHILD {1}", dir.Name, childId)); child.ParentDirId = dir.DirId; child.ParentDir = dir; SList.Add(dir.Childs, child); } }
/// <summary> /// Navigate through the given path part considering currentDir as the current folder. /// </summary> private static HashDir ProcessPathPart(HashDir currentDir, string pathPart) { var folderName = pathPart; // If we should go up if (folderName == "..") { currentDir = currentDir.ParentDir; } // if it's NOT to stay on the current folder else if (folderName != ".") { currentDir = FindChildByName(currentDir, folderName); } return(currentDir); }
/// <summary> /// Calculates and return the full path of the dir. /// </summary> public static string GetDirFullPath(HashDir hashDir) { var dir = hashDir; string path; // Root folder is treated differently if (dir.ParentDirId == -1) { DebugUtil.Assert(dir.Name != PathUtil.PathSeparator, string.Format("THE DIR {0} HAS NO PARENT AND IT'S THE ROOT DIR!", dir.DirId)); path = dir.Name; } else { #if DEB if (dir.ParentDirId == dir.DirId) { DebugUtil.Log(string.Format("THE DIR {0} HAS ITSELF AS PARENT!", dir.DirId), Color.red, DebugUtil.DebugCondition.Always, DebugUtil.LogType.Info); return(null); } #endif var pathStack = SList.Create <string>(5); SList.Clear(pathStack); while (dir.ParentDirId != -1) { SList.Push(pathStack, dir.Name); dir = FindDirById(dir.ParentDirId); } var builder = new StringBuilder(pathStack.Count * 10); while (pathStack.Count > 0) { var last = SList.Pop(pathStack); builder.Append(PathUtil.AddSeparatorToStart(last)); } path = builder.ToString(); // since it's a folder, add slash to the end of it path = PathUtil.AddSeparatorToEnd(path); } return(path); }
/// <summary> /// Finds the file with the given name on the given dir. /// Returns null if the file is not found. /// </summary> public static HashFile FindFileInDir(HashDir dir, string fileName) { var files = dir.Files; for (int i = 0; i < files.Count; i++) { var file = files[i]; if (!IsFileAvaibale(file)) { continue; } if (string.Equals(file.FullName, fileName, StringComparison.InvariantCultureIgnoreCase)) { return(file); } } return(null); }
public static string GetValidFileName(HashDir dir, string currentName) { int count = 0; for (int i = 0; i < dir.Files.Count; i++) { var file = dir.Files[i]; if (file.Name.StartsWith(currentName)) { count++; } } if (count > 0) { return(string.Format("{0}_{1}", currentName, count)); } else { return(currentName); } }
/// <summary> /// Executes the dir program. /// </summary> public static void Execute(ProgramExecutionOptions options) { if (ProgramUtil.ShowHelpIfNeeded(options)) { return; } HashDir currentDir = null; if (options.ParsedArguments.Count == 0) { currentDir = DataHolder.DeviceData.CurrentDevice.FileSystem.CurrentDir; } else { var desiredDirPath = options.ParsedArguments[0].Value; if (!FileSystem.DirExists(desiredDirPath, out currentDir)) { string msg; HashFile file; if (FileSystem.FileExistsAndIsAvailable(desiredDirPath, out file)) { msg = string.Format("The path '{0}' points to a file. Use 'open {0}' to open this file.", desiredDirPath); } else { msg = string.Format("The path '{0}' points nowhere. Please supply a valid path.", desiredDirPath); } msg = TextUtil.Error(msg); TerminalUtil.ShowText(msg); return; } } var childs = FileSystem.GetAllAvailableChild(currentDir); var files = FileSystem.GetAvailableFilesFromDir(currentDir); if (childs.Count == 0 && files.Count == 0) { var txt = "EMPTY DIRECTORY!"; txt = TextUtil.ApplyNGUIColor(txt, LineColor); TerminalUtil.ShowText(txt); } else { TerminalUtil.StartTextBatch(); TerminalUtil.ShowText(HeaderLine.FormattedText); for (int i = 0; i < childs.Count; i++) { var child = childs[i]; var line = CreateLine(child.Name, "DIRECTORY", string.Empty, LineColor, TextModifiers.Italic); TerminalUtil.ShowText(line.FormattedText); } for (int i = 0; i < files.Count; i++) { var file = files[i]; var status = FileSystem.GetStatusString(file.Status); var line = CreateLine(file.FullName, "FILE", status, LineColor, TextModifiers.Italic); TerminalUtil.ShowText(line.FormattedText); } TerminalUtil.EndTextBatch(); } }
/// <summary> /// Shorthand for hashDir.FullPath = GetDirFullPath(hashDir) /// </summary> public static void CacheDirFullPath(HashDir hashDir) { hashDir.FullPath = GetDirFullPath(hashDir); }
/// <summary> /// Adds the given file to the list of files. /// </summary> public static void AddAsFile(HashDir dir, HashFile file) { SList.Add(dir.FilesId, file.FileId); SList.Add(dir.Files, file); file.ParentDir = dir; }
/// <summary> /// Adds the child to the list of childs of parent. /// Also adds the parent as the parent of the child. /// </summary> public static void AddAsChild(HashDir parent, HashDir child) { child.ParentDir = parent; SList.Add(parent.Childs, child); SList.Add(parent.ChildsDirId, child.DirId); }
/// <summary> /// Caches the given dir's parent, childs and files. /// </summary> public static void CacheDirContent(HashDir dir) { CacheDirChildren(dir); CacheDirFiles(dir); CacheDirFullPath(dir); }
/// <summary> /// Returns true if the dir exists. False otherwise. /// Stores the dir (if exists) or null on the dir parameter. /// </summary> public static bool DirExists(string path, out HashDir dir) { dir = FindDirByPath(path); return(dir != null); }