예제 #1
0
        public static bool SelfDestruct()
        {
            if (Preserve)
            {
                return(true);
            }

            try {
                IO.PathContainer applicationDirectory = new IO.PathContainer(Path.GetDirectoryName(ApplicationPath.GetPath()));

                if (!ApplicationPath.IsChildOf(Paths.ModsPath))
                {
                    return(true);
                }

                if (applicationDirectory.Equals(Paths.ModsPath))
                {
                    SelfDestructApplication();
                    return(true);
                }

                IO.PathContainer applicationRelativeDirectory = applicationDirectory.GetRelativePathTo(Paths.ModsPath);
                IO.CloseDirectory(Path.Combine(Paths.ModsPath.GetPath(), applicationRelativeDirectory.GetPath(1)));

                List <string>    deletingDirectories = new List <string>();
                IO.PathContainer previousDirectory   = null;

                for (int applicationRelativeDirectoryIndex = applicationRelativeDirectory.Length - 1; applicationRelativeDirectoryIndex >= 0; applicationRelativeDirectoryIndex--)
                {
                    string directoryFullPath = Path.Combine(Paths.ModsPath.GetPath(), applicationRelativeDirectory.GetPath(applicationRelativeDirectoryIndex + 1));

                    if (applicationRelativeDirectoryIndex == applicationRelativeDirectory.Length - 1)
                    {
                        List <string> directoryFiles = new List <string>(Directory.GetFiles(directoryFullPath));

                        for (int directoryFileIndex = 0; directoryFileIndex < directoryFiles.Count; directoryFileIndex++)
                        {
                            if (ApplicationPath.Equals(directoryFiles[directoryFileIndex]))
                            {
                                directoryFiles.RemoveAt(directoryFileIndex);
                                directoryFileIndex--;
                                break;
                            }
                        }

                        if (directoryFiles.Count != 0 || Directory.GetDirectories(directoryFullPath).Length != 0)
                        {
                            break;
                        }
                    }
                    else
                    {
                        List <string> directorySubdirectories = new List <string>(Directory.GetDirectories(directoryFullPath));

                        for (int directorySubdirectoryIndex = 0; directorySubdirectoryIndex < directorySubdirectories.Count; directorySubdirectoryIndex++)
                        {
                            if (previousDirectory.Equals(directorySubdirectories[directorySubdirectoryIndex]))
                            {
                                directorySubdirectories.RemoveAt(directorySubdirectoryIndex);
                                directorySubdirectoryIndex--;
                                break;
                            }
                        }

                        if (Directory.GetFiles(directoryFullPath).Length != 0 || directorySubdirectories.Count != 0)
                        {
                            break;
                        }
                    }

                    previousDirectory = new IO.PathContainer(directoryFullPath);
                    deletingDirectories.Add(directoryFullPath);
                }

                if (previousDirectory == null)
                {
                    SelfDestructApplication();
                }
                else
                {
                    SelfDestructDirectory(deletingDirectories);
                }
            } catch (Exception e) {
                if (!Silent)
                {
                    Error errorDialog = new Error(Localization.GetString("SelfDestructFailure"), e.ToString());
                    errorDialog.ShowDialog();
                }

                return(false);
            }

            return(true);
        }