LibraryUtility.cs 5.0 KB

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