LibraryUtility.cs 6.3 KB

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