LibraryUtility.cs 7.0 KB

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