ResourceOps.ts 10 KB

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