/// <summary> /// Creates a new instance of the class ScanInfo. /// </summary> /// <param name="name">File or directory name.</param> /// <param name="path">Full path of the file/directory.</param> /// <param name="isFile">Flag that indicates whether this scan info is for a file or not.</param> /// <param name="parent">Parent of this instance.</param> public ScanInfo(string name, string path, bool isFile, ScanInfo parent = null) { Name = name; Path = path; IsFile = isFile; Parent = parent; if (Parent != null) Parent.Childs.Add(this); }
/// <summary> /// Creates a new instance of the class ScanInfo. /// </summary> /// <param name="name">File or directory name.</param> /// <param name="path">Full path of the file/directory.</param> /// <param name="isFile">Flag that indicates whether this scan info is for a file or not.</param> /// <param name="parent">Parent of this instance.</param> public ScanInfo(string name, string path, bool isFile, ScanInfo parent = null) { Name = name; Path = path; IsFile = isFile; Parent = parent; if (Parent != null) { Parent.Childs.Add(this); } }
/// <summary> /// Creates a TreeNodeMod from the passed ScanInfo. /// </summary> /// <param name="unknown">The ScanInfo of the unknown node.</param> /// <returns>The new created TreeNodeMod.</returns> private static ModNode ScanInfoToKSPMA_TreeNode(ScanInfo unknown) { ModNode node = new ModNode(); node.Key = unknown.Path; node.Name = unknown.Name; node.AddDate = DateTime.Now.ToString(); node.Destination = KSPPathHelper.GetRelativePath(unknown.Path); node.NodeType = (unknown.IsFile) ? NodeType.UnknownFileInstalled : NodeType.UnknownFolderInstalled; node._Checked = true; Messenger.AddDebug(string.Format(Messages.MSG_MODNODE_0_CREATED, node.Key)); foreach (ScanInfo si in unknown.Childs) { ModNode child = ScanInfoToKSPMA_TreeNode(si); node.Nodes.Add(child); } return node; }
/// <summary> /// Compares the ScanInfo to all known nodes (from parent). /// </summary> /// <param name="scanInfo">The ScanInfo to compare.</param> /// <param name="parent">The start node of the comparison.</param> /// <returns>True if a match was found, otherwise false.</returns> private static bool CompareNodes(ScanInfo scanInfo, ModNode parent) { if (scanInfo.Name.Equals(parent.Text, StringComparison.CurrentCultureIgnoreCase)) return true; foreach (ModNode child in parent.Nodes) { if (child.Text.Equals(scanInfo.Name, StringComparison.CurrentCultureIgnoreCase)) return true; if (CompareNodes(scanInfo, child)) return true; } return false; }
/// <summary> /// Scans the passed dir for files and directories and creates a tree of ScanInfos from it. /// </summary> /// <param name="scanDir">The ScanInfo of the start directory.</param> private static void ScanDir(ScanInfo scanDir) { List<ScanInfo> entries = new List<ScanInfo>(); foreach (string file in Directory.GetFiles(scanDir.Path)) { Messenger.AddDebug(string.Format(Messages.MSG_FILE_0_FOUND, file)); string filename = Path.GetFileName(file); ScanInfo scanInfo = new ScanInfo(filename, file, true, scanDir); scanInfo.Parent = scanDir; } string[] dirs = Directory.GetDirectories(scanDir.Path); foreach (string dir in dirs) { Messenger.AddDebug(string.Format(Messages.MSG_DIRECTORY_0_FOUND, dir)); string dirname = dir.Substring(dir.LastIndexOf(Path.DirectorySeparatorChar) + 1); ScanInfo scanInfo = new ScanInfo(dirname, dir, false, scanDir); ScanDir(scanInfo); } }
/// <summary> /// Scans the KSP GameData directory for installed mods and adds them to the ModSelection. /// </summary> internal static void ScanGameData() { Messenger.AddDebug(Constants.SEPARATOR); Messenger.AddDebug(Messages.MSG_SCAN_GAMDATA_FOLDER_STARTED); Messenger.AddDebug(Constants.SEPARATOR); EventDistributor.InvokeAsyncTaskStarted(Instance); View.SetEnabledOfAllControls(false); View.ShowBusy = true; AsyncTask<bool> asyncJob = new AsyncTask<bool>(); asyncJob.SetCallbackFunctions(() => { string[] ignoreDirs = new string[] { "squad", "myflags", "nasamission" }; List<ScanInfo> entries = new List<ScanInfo>(); try { string scanDir = KSPPathHelper.GetPath(KSPPaths.GameData); string[] dirs = Directory.GetDirectories(scanDir); foreach (string dir in dirs) { string dirname = dir.Substring(dir.LastIndexOf(Path.DirectorySeparatorChar) + 1); if (!ignoreDirs.Contains(dirname.ToLower())) { Messenger.AddDebug(string.Format(Messages.MSG_DIRECTORY_0_FOUND, dirname)); ScanInfo scanInfo = new ScanInfo(dirname, dir, false); entries.Add(scanInfo); ScanDir(scanInfo); } } List<ScanInfo> unknowns = GetUnknowenNodes(entries); if (unknowns.Count > 0) { foreach (ScanInfo unknown in unknowns) { ModNode node = ScanInfoToKSPMA_TreeNode(unknown); RefreshCheckedStateOfMods(new[] { node }); Model.Nodes.Add(node); Messenger.AddInfo(string.Format(Messages.MSG_MOD_ADDED_0, node.Text)); } } else Messenger.AddInfo(Messages.MSG_SCAN_NO_NEW_MODS_FOUND); } catch (Exception ex) { Messenger.AddError(Messages.MSG_SCAN_ERROR_DURING_SCAN, ex); } return true; }, (result, ex) => { Messenger.AddDebug(Constants.SEPARATOR); EventDistributor.InvokeAsyncTaskDone(Instance); View.SetEnabledOfAllControls(true); View.ShowBusy = false; }); asyncJob.Run(); }