ResourceOps.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import EditorEvents = require("../editor/EditorEvents");
  23. class ResourceOps extends Atomic.ScriptObject {
  24. constructor() {
  25. super();
  26. this.subscribeToEvent("AssetImportError", (ev: ToolCore.AssetImportErrorEvent) => {
  27. resourceOps.sendEvent(EditorEvents.ModalError, { title: "Asset Import Error", message: ev.error });
  28. });
  29. }
  30. }
  31. var resourceOps = new ResourceOps();
  32. export function CreateNewFolder(resourcePath: string, reportError: boolean = true): boolean {
  33. var title = "New Folder Error";
  34. var fs = Atomic.getFileSystem();
  35. if (fs.dirExists(resourcePath) || fs.fileExists(resourcePath)) {
  36. if (reportError)
  37. resourceOps.sendEvent(EditorEvents.ModalError, { title: title, message: "Already exists: " + resourcePath });
  38. return false;
  39. }
  40. if (!fs.createDir(resourcePath)) {
  41. if (reportError)
  42. resourceOps.sendEvent(EditorEvents.ModalError, { title: title, message: "Could not create " + resourcePath });
  43. return false;
  44. }
  45. var db = ToolCore.getAssetDatabase();
  46. db.scan();
  47. return true;
  48. }
  49. export function CreateNewComponent(resourcePath: string, componentName: string, template: Editor.Templates.FileTemplateDefinition, reportError: boolean = true): boolean {
  50. var title = "New Component Error";
  51. var fs = Atomic.fileSystem;
  52. if (fs.dirExists(resourcePath) || fs.fileExists(resourcePath)) {
  53. if (reportError)
  54. resourceOps.sendEvent(EditorEvents.ModalError, { title: title, message: "Already exists: " + resourcePath });
  55. return false;
  56. }
  57. var templateFilename = template.filename;
  58. var file = Atomic.cache.getFile(templateFilename);
  59. if (!file) {
  60. if (reportError)
  61. resourceOps.sendEvent(EditorEvents.ModalError, { title: title, message: "Failed to open template: " + templateFilename });
  62. return false;
  63. }
  64. var out = new Atomic.File(resourcePath, Atomic.FILE_WRITE);
  65. var success = out.copy(file);
  66. out.close();
  67. if (!success) {
  68. if (reportError)
  69. resourceOps.sendEvent(EditorEvents.ModalError, { title: title, message: "Failed template copy: " + templateFilename + " -> " + resourcePath });
  70. return false;
  71. }
  72. ToolCore.assetDatabase.scan();
  73. return true;
  74. }
  75. export function CreateNewScript(resourcePath: string, scriptName: string, template: Editor.Templates.FileTemplateDefinition, reportError: boolean = true): boolean {
  76. var title = "New Script Error";
  77. var fs = Atomic.fileSystem;
  78. if (fs.dirExists(resourcePath) || fs.fileExists(resourcePath)) {
  79. if (reportError)
  80. resourceOps.sendEvent(EditorEvents.ModalError, { title: title, message: "Already exists: " + resourcePath });
  81. return false;
  82. }
  83. var templateFilename = template.filename;
  84. var file = Atomic.cache.getFile(templateFilename);
  85. if (!file) {
  86. if (reportError)
  87. resourceOps.sendEvent(EditorEvents.ModalError, { title: title, message: "Failed to open template: " + templateFilename });
  88. return false;
  89. }
  90. var out = new Atomic.File(resourcePath, Atomic.FILE_WRITE);
  91. var success = out.copy(file);
  92. out.close();
  93. if (!success) {
  94. if (reportError)
  95. resourceOps.sendEvent(EditorEvents.ModalError, { title: title, message: "Failed template copy: " + templateFilename + " -> " + resourcePath });
  96. return false;
  97. }
  98. ToolCore.assetDatabase.scan();
  99. return true;
  100. }
  101. export function CreateNewScene(resourcePath: string, sceneName: string, reportError: boolean = true): boolean {
  102. var title = "New Scene Error";
  103. var fs = Atomic.fileSystem;
  104. if (fs.dirExists(resourcePath) || fs.fileExists(resourcePath)) {
  105. if (reportError)
  106. resourceOps.sendEvent(EditorEvents.ModalError, { title: title, message: "Already exists: " + resourcePath });
  107. return false;
  108. }
  109. var templateFilename = "AtomicEditor/templates/template_scene.scene";
  110. var file = Atomic.cache.getFile(templateFilename);
  111. if (!file) {
  112. if (reportError)
  113. resourceOps.sendEvent(EditorEvents.ModalError, { title: title, message: "Failed to open template: " + templateFilename });
  114. return false;
  115. }
  116. var out = new Atomic.File(resourcePath, Atomic.FILE_WRITE);
  117. var success = out.copy(file);
  118. out.close();
  119. if (!success) {
  120. if (reportError)
  121. resourceOps.sendEvent(EditorEvents.ModalError, { title: title, message: "Failed template copy: " + templateFilename + " -> " + resourcePath });
  122. return false;
  123. }
  124. ToolCore.assetDatabase.scan();
  125. return true;
  126. }
  127. export function CreateNewMaterial(resourcePath: string, materialName: string, reportError: boolean = true): boolean {
  128. var title = "New Material Error";
  129. var fs = Atomic.fileSystem;
  130. if (fs.dirExists(resourcePath) || fs.fileExists(resourcePath)) {
  131. if (reportError)
  132. resourceOps.sendEvent(EditorEvents.ModalError, { title: title, message: "Already exists: " + resourcePath });
  133. return false;
  134. }
  135. var templateFilename = "AtomicEditor/templates/template_material.material";
  136. var file = Atomic.cache.getFile(templateFilename);
  137. if (!file) {
  138. if (reportError)
  139. resourceOps.sendEvent(EditorEvents.ModalError, { title: title, message: "Failed to open template: " + templateFilename });
  140. return false;
  141. }
  142. var out = new Atomic.File(resourcePath, Atomic.FILE_WRITE);
  143. var success = out.copy(file);
  144. out.close();
  145. if (!success) {
  146. if (reportError)
  147. resourceOps.sendEvent(EditorEvents.ModalError, { title: title, message: "Failed template copy: " + templateFilename + " -> " + resourcePath });
  148. return false;
  149. }
  150. ToolCore.assetDatabase.scan();
  151. return true;
  152. }