ResourceOps.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 EditorUI = require("../ui/EditorUI");
  23. import ProjectTemplates = require("ProjectTemplates");
  24. class ResourceOps extends Atomic.ScriptObject {
  25. constructor() {
  26. super();
  27. this.subscribeToEvent(ToolCore.AssetImportErrorEvent((ev: ToolCore.AssetImportErrorEvent) => {
  28. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: "Asset Import Error", message: ev.error }));
  29. }));
  30. this.subscribeToEvent(Editor.RequestProjectLoadEvent((ev: Editor.RequestProjectLoadEvent) => { this.handleRequestProjectLoad(ev); }));
  31. }
  32. handleRequestProjectLoad(ev:Editor.RequestProjectLoadEvent) {
  33. var fs = Atomic.fileSystem;
  34. var projectPath = Atomic.addTrailingSlash(Atomic.getPath(ev.path));
  35. var openProject = () => this.sendEvent(Editor.EditorLoadProjectEventData({ path: ev.path }));
  36. // Check whether there is a cache folder, if so, this project has been loaded before
  37. if (Atomic.fileSystem.dirExists(projectPath + "Cache")) {
  38. openProject();
  39. return;
  40. } else {
  41. // this may be an example
  42. var parentPath = Atomic.getParentPath(projectPath);
  43. var exampleInfoPath = parentPath + "AtomicExample.json";
  44. if (!fs.fileExists(exampleInfoPath)) {
  45. openProject();
  46. return;
  47. }
  48. var jsonFile = new Atomic.File(exampleInfoPath, Atomic.FileMode.FILE_READ);
  49. if (!jsonFile.isOpen()) {
  50. return;
  51. }
  52. var exampleJson = JSON.parse(jsonFile.readText());
  53. var allLanguages = ["CSharp", "JavaScript", "TypeScript"];
  54. var language = null;
  55. for (var i = 0; i < allLanguages.length; i++) {
  56. if (projectPath.indexOf(allLanguages[i]) != -1) {
  57. language = allLanguages[i];
  58. break;
  59. }
  60. }
  61. if (!language) {
  62. return;
  63. }
  64. var projectDef = {
  65. name : exampleJson.name ? exampleJson.name : "Anonymous Example",
  66. desc : exampleJson.desc ? exampleJson.desc : "",
  67. screenshot : parentPath + "Screenshot.png",
  68. folder : parentPath,
  69. languages : [language],
  70. appDelegateClass : exampleJson.appDelegateClass,
  71. namespace : exampleJson.namespace
  72. };
  73. var ops = EditorUI.getModelOps();
  74. ops.showCreateProject(projectDef, projectPath);
  75. }
  76. }
  77. }
  78. var resourceOps = new ResourceOps();
  79. export function CreateNewFolder(resourcePath: string, reportError: boolean = true): boolean {
  80. var title = "New Folder Error";
  81. var fs = Atomic.getFileSystem();
  82. if (fs.dirExists(resourcePath) || fs.fileExists(resourcePath)) {
  83. if (reportError)
  84. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Already exists: " + resourcePath }));
  85. return false;
  86. }
  87. if (!fs.createDir(resourcePath)) {
  88. if (reportError)
  89. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Could not create " + resourcePath }));
  90. return false;
  91. }
  92. var db = ToolCore.getAssetDatabase();
  93. db.scan();
  94. return true;
  95. }
  96. export function CreateNewComponent(resourcePath: string, componentName: string, template: Editor.Templates.FileTemplateDefinition, reportError: boolean = true): boolean {
  97. var title = "New Component Error";
  98. var fs = Atomic.fileSystem;
  99. if (fs.dirExists(resourcePath) || fs.fileExists(resourcePath)) {
  100. if (reportError)
  101. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Already exists: " + resourcePath }));
  102. return false;
  103. }
  104. var templateFilename = template.filename;
  105. var file = Atomic.cache.getFile(templateFilename);
  106. if (!file) {
  107. if (reportError)
  108. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Failed to open template: " + templateFilename }));
  109. return false;
  110. }
  111. var out = new Atomic.File(resourcePath, Atomic.FileMode.FILE_WRITE);
  112. var success = out.copy(file);
  113. out.close();
  114. if (!success) {
  115. if (reportError)
  116. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Failed template copy: " + templateFilename + " -> " + resourcePath }));
  117. return false;
  118. }
  119. ToolCore.assetDatabase.scan();
  120. return true;
  121. }
  122. export function CreateNewScript(resourcePath: string, scriptName: string, template: Editor.Templates.FileTemplateDefinition, reportError: boolean = true): boolean {
  123. var title = "New Script Error";
  124. var fs = Atomic.fileSystem;
  125. if (fs.dirExists(resourcePath) || fs.fileExists(resourcePath)) {
  126. if (reportError)
  127. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Already exists: " + resourcePath }));
  128. return false;
  129. }
  130. var templateFilename = template.filename;
  131. var file = Atomic.cache.getFile(templateFilename);
  132. if (!file) {
  133. if (reportError)
  134. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Failed to open template: " + templateFilename }));
  135. return false;
  136. }
  137. var out = new Atomic.File(resourcePath, Atomic.FileMode.FILE_WRITE);
  138. var success = out.copy(file);
  139. out.close();
  140. if (!success) {
  141. if (reportError)
  142. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Failed template copy: " + templateFilename + " -> " + resourcePath }));
  143. return false;
  144. }
  145. ToolCore.assetDatabase.scan();
  146. return true;
  147. }
  148. export function CreateNewScene(resourcePath: string, sceneName: string, reportError: boolean = true): boolean {
  149. var title = "New Scene Error";
  150. var fs = Atomic.fileSystem;
  151. if (fs.dirExists(resourcePath) || fs.fileExists(resourcePath)) {
  152. if (reportError)
  153. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Already exists: " + resourcePath }));
  154. return false;
  155. }
  156. var templateFilename = "AtomicEditor/templates/template_scene.scene";
  157. var file = Atomic.cache.getFile(templateFilename);
  158. if (!file) {
  159. if (reportError)
  160. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Failed to open template: " + templateFilename }));
  161. return false;
  162. }
  163. var out = new Atomic.File(resourcePath, Atomic.FileMode.FILE_WRITE);
  164. var success = out.copy(file);
  165. out.close();
  166. if (!success) {
  167. if (reportError)
  168. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Failed template copy: " + templateFilename + " -> " + resourcePath }));
  169. return false;
  170. }
  171. ToolCore.assetDatabase.scan();
  172. return true;
  173. }
  174. export function CreateNewMaterial(resourcePath: string, materialName: string, reportError: boolean = true): boolean {
  175. var title = "New Material Error";
  176. var fs = Atomic.fileSystem;
  177. if (fs.dirExists(resourcePath) || fs.fileExists(resourcePath)) {
  178. if (reportError)
  179. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Already exists: " + resourcePath }));
  180. return false;
  181. }
  182. var templateFilename = "AtomicEditor/templates/template_material.material";
  183. var file = Atomic.cache.getFile(templateFilename);
  184. if (!file) {
  185. if (reportError)
  186. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Failed to open template: " + templateFilename }));
  187. return false;
  188. }
  189. var out = new Atomic.File(resourcePath, Atomic.FileMode.FILE_WRITE);
  190. var success = out.copy(file);
  191. out.close();
  192. if (!success) {
  193. if (reportError)
  194. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Failed template copy: " + templateFilename + " -> " + resourcePath }));
  195. return false;
  196. }
  197. ToolCore.assetDatabase.scan();
  198. return true;
  199. }
  200. //TODO - Replace this by creating a temporary scene that cannot be saved
  201. export function CreateNewAnimationPreviewScene(reportError: boolean = true): boolean {
  202. var title = "Animation Viewer Error";
  203. var templateFilename = "AtomicEditor/templates/template_scene.scene";
  204. var templateFile = Atomic.cache.getFile(templateFilename);
  205. if (!templateFile) {
  206. if (reportError)
  207. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Failed to open template scene: " + templateFile }));
  208. return false;
  209. }
  210. var animFilename = "AtomicEditor/templates/animation_viewer.scene";
  211. var animFile = Atomic.cache.getFile(animFilename);
  212. if (!animFile) {
  213. if (reportError)
  214. resourceOps.sendEvent(Editor.EditorModalEventData({ type: Editor.EDITOR_MODALERROR, title: title, message: "Failed to open animation viewer: " + animFilename }));
  215. return false;
  216. }
  217. //Reset the animation viewer scene to a blank scene
  218. animFile = templateFile;
  219. resourceOps.sendEvent(Editor.EditorEditResourceEventData({ path: animFilename, lineNumber: 0 }));
  220. return true;
  221. }