LibraryUtility.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.IO;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Contains various helper methods for dealing with the project library.
  8. /// </summary>
  9. public static class LibraryUtility
  10. {
  11. /// <summary>
  12. /// Creates a new folder with in the specified folder.
  13. /// </summary>
  14. /// <param name="folder">Folder relative to project library to create the new foldeer in.</param>
  15. public static void CreateFolder(string folder)
  16. {
  17. string path = Path.Combine(folder, "New Folder");
  18. path = GetUniquePath(path);
  19. ProjectLibrary.CreateFolder(path);
  20. }
  21. /// <summary>
  22. /// Creates a new material with the default shader in the specified folder.
  23. /// </summary>
  24. /// <param name="folder">Folder relative to project library to create the material in.</param>
  25. public static void CreateEmptyMaterial(string folder)
  26. {
  27. string path = Path.Combine(folder, "New Material.asset");
  28. path = GetUniquePath(path);
  29. Material material = new Material(Builtin.DiffuseShader);
  30. ProjectLibrary.Create(material, path);
  31. }
  32. /// <summary>
  33. /// Creates a new empty sprite texture in the specified folder.
  34. /// </summary>
  35. /// <param name="folder">Folder relative to project library to create the sprite texture in.</param>
  36. public static void CreateEmptySpriteTexture(string folder)
  37. {
  38. string path = Path.Combine(folder, "New Sprite Texture.asset");
  39. path = GetUniquePath(path);
  40. SpriteTexture spriteTexture = new SpriteTexture(null);
  41. ProjectLibrary.Create(spriteTexture, path);
  42. }
  43. /// <summary>
  44. /// Creates a new empty string table in the specified folder.
  45. /// </summary>
  46. /// <param name="folder">Folder relative to project library to create the string table in.</param>
  47. public static void CreateEmptyStringTable(string folder)
  48. {
  49. string path = Path.Combine(folder, "New String Table.asset");
  50. path = GetUniquePath(path);
  51. StringTable stringTable = new StringTable();
  52. ProjectLibrary.Create(stringTable, path);
  53. }
  54. /// <summary>
  55. /// Creates a new empty GUI skin in the specified folder.
  56. /// </summary>
  57. /// <param name="folder">Folder relative to project library to create the GUI skin in.</param>
  58. public static void CreateEmptyGUISkin(string folder)
  59. {
  60. string path = Path.Combine(folder, "New GUI Skin.asset");
  61. path = GetUniquePath(path);
  62. GUISkin guiSkin = new GUISkin();
  63. ProjectLibrary.Create(guiSkin, path);
  64. }
  65. /// <summary>
  66. /// Creates a new shader containing a rough code outline in the specified folder.
  67. /// </summary>
  68. /// <param name="folder">Folder relative to project library to create the shader in.</param>
  69. public static void CreateEmptyShader(string folder)
  70. {
  71. string path = Path.Combine(folder, "New Shader.bsl");
  72. path = Path.Combine(ProjectLibrary.ResourceFolder, path);
  73. path = GetUniquePath(path);
  74. File.WriteAllText(path, EditorBuiltin.EmptyShaderCode);
  75. ProjectLibrary.Refresh(path);
  76. }
  77. /// <summary>
  78. /// Creates a new C# script containing a rough code outline in the specified folder.
  79. /// </summary>
  80. /// <param name="folder">Folder relative to project library to create the C# script in.</param>
  81. public static void CreateEmptyCSScript(string folder)
  82. {
  83. string path = Path.Combine(folder, "New Script.cs");
  84. path = Path.Combine(ProjectLibrary.ResourceFolder, path);
  85. path = GetUniquePath(path);
  86. File.WriteAllText(path, EditorBuiltin.EmptyCSScriptCode);
  87. ProjectLibrary.Refresh(path);
  88. }
  89. /// <summary>
  90. /// Checks if a file or folder at the specified path exists in the library, and if it does generates a new unique
  91. /// name for the file or folder.
  92. /// </summary>
  93. /// <param name="path">Path to the file or folder to generate a unique name.</param>
  94. /// <returns>New path with the unique name. This will be unchanged from input path if the input path doesn't
  95. /// already exist.</returns>
  96. public static string GetUniquePath(string path)
  97. {
  98. string extension = Path.GetExtension(path);
  99. string pathClean = path;
  100. if (!String.IsNullOrEmpty(extension))
  101. pathClean = path.Remove(path.Length - extension.Length);
  102. int idx = 0;
  103. int separatorIdx = pathClean.LastIndexOf('_');
  104. if (separatorIdx != -1)
  105. {
  106. string numberString = pathClean.Substring(separatorIdx + 1, pathClean.Length - (separatorIdx + 1));
  107. if (int.TryParse(numberString, out idx))
  108. {
  109. pathClean = pathClean.Substring(0, separatorIdx);
  110. idx++;
  111. }
  112. }
  113. string destination = path;
  114. while (ProjectLibrary.Exists(destination))
  115. {
  116. destination = pathClean + "_" + idx + extension;
  117. idx++;
  118. }
  119. return destination;
  120. }
  121. }
  122. }