PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/PhyreFly/src/PFUtils.cs

https://bitbucket.org/drummertom999/phyreballs
C# | 82 lines | 80 code | 2 blank | 0 comment | 1 complexity | de2bf8d29883d2911964eee501dc206b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Windows.Forms;
  7. namespace PhyreFly
  8. {
  9. class PFUtils
  10. {
  11. public static bool PathIsAbsolute(string path)
  12. {
  13. return path[1] == ':';
  14. }
  15. public static string TidyPath(string path)
  16. {
  17. return path.Replace("/", "\\");
  18. }
  19. public static bool EnsureAssetExists(string source)
  20. {
  21. // Ensure that the file exists (even if we are using a relative path)
  22. // by attempting to resolve and check the assets absolute path
  23. string absolutePath = source;
  24. if (!PFUtils.PathIsAbsolute(source))
  25. {
  26. string workingDir = string.Empty;
  27. if (!PFAssetProcessor.GetWorkingDir(ref workingDir))
  28. return false;
  29. absolutePath = Path.Combine(workingDir, source);
  30. }
  31. absolutePath = PFUtils.TidyPath(absolutePath);
  32. if (!File.Exists(absolutePath))
  33. {
  34. MessageBox.Show(string.Format("The following file could not be found: {0}. Please check paths and working directory", absolutePath));
  35. if (!PFUtils.PathIsAbsolute(source))
  36. PFAssetProcessor.WorkingDir = string.Empty; // Reset working folder (as its probably wrong)
  37. return false;
  38. }
  39. return true;
  40. }
  41. public static void RemoveReadOnlyFlag(string filename)
  42. {
  43. FileInfo file = new FileInfo(filename);
  44. if ((file.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
  45. file.Attributes = (FileAttributes)(Convert.ToInt32(file.Attributes) - Convert.ToInt32(FileAttributes.ReadOnly));
  46. }
  47. public static void RecursiveDeleteFolders(string root, string[] matchList)
  48. {
  49. DirectoryInfo info = new DirectoryInfo(root);
  50. foreach (DirectoryInfo dir in info.GetDirectories())
  51. {
  52. if (matchList.Contains(dir.Name))
  53. {
  54. ++PFAssetProcessor.nCleanCount;
  55. PFUtils.RemoveDirAndContents(dir.FullName);
  56. }
  57. else
  58. {
  59. RecursiveDeleteFolders(dir.FullName, matchList);
  60. }
  61. }
  62. }
  63. public static bool RemoveDirAndContents(string dir)
  64. {
  65. try
  66. {
  67. PFConsole.WriteLine(PFConsole.Severity.WARNING, "Deleting folder {0}", dir);
  68. Directory.Delete(dir, true);
  69. return true;
  70. }
  71. catch (Exception) { return false; }
  72. }
  73. }
  74. }