/tools/PhyreFly/src/PFUtils.cs
C# | 82 lines | 80 code | 2 blank | 0 comment | 1 complexity | de2bf8d29883d2911964eee501dc206b MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Windows.Forms;
-
- namespace PhyreFly
- {
- class PFUtils
- {
- public static bool PathIsAbsolute(string path)
- {
- return path[1] == ':';
- }
-
- public static string TidyPath(string path)
- {
- return path.Replace("/", "\\");
- }
-
- public static bool EnsureAssetExists(string source)
- {
- // Ensure that the file exists (even if we are using a relative path)
- // by attempting to resolve and check the assets absolute path
- string absolutePath = source;
- if (!PFUtils.PathIsAbsolute(source))
- {
- string workingDir = string.Empty;
- if (!PFAssetProcessor.GetWorkingDir(ref workingDir))
- return false;
- absolutePath = Path.Combine(workingDir, source);
- }
-
- absolutePath = PFUtils.TidyPath(absolutePath);
- if (!File.Exists(absolutePath))
- {
- MessageBox.Show(string.Format("The following file could not be found: {0}. Please check paths and working directory", absolutePath));
- if (!PFUtils.PathIsAbsolute(source))
- PFAssetProcessor.WorkingDir = string.Empty; // Reset working folder (as its probably wrong)
- return false;
- }
-
- return true;
- }
-
- public static void RemoveReadOnlyFlag(string filename)
- {
- FileInfo file = new FileInfo(filename);
- if ((file.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
- file.Attributes = (FileAttributes)(Convert.ToInt32(file.Attributes) - Convert.ToInt32(FileAttributes.ReadOnly));
- }
-
- public static void RecursiveDeleteFolders(string root, string[] matchList)
- {
- DirectoryInfo info = new DirectoryInfo(root);
- foreach (DirectoryInfo dir in info.GetDirectories())
- {
- if (matchList.Contains(dir.Name))
- {
- ++PFAssetProcessor.nCleanCount;
- PFUtils.RemoveDirAndContents(dir.FullName);
- }
- else
- {
- RecursiveDeleteFolders(dir.FullName, matchList);
- }
- }
- }
-
- public static bool RemoveDirAndContents(string dir)
- {
- try
- {
- PFConsole.WriteLine(PFConsole.Severity.WARNING, "Deleting folder {0}", dir);
- Directory.Delete(dir, true);
- return true;
- }
- catch (Exception) { return false; }
- }
- }
- }