2
0

LibraryUtility.cs 6.2 KB

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