AEResourceOps.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "AtomicEditor.h"
  5. #include <Atomic/Core/Context.h>
  6. #include <Atomic/IO/File.h>
  7. #include <Atomic/IO/FileSystem.h>
  8. #include <Atomic/Resource/ResourceCache.h>
  9. #include "../AEEvents.h"
  10. #include "../AEEditor.h"
  11. #include "../UI/UIMainFrame.h"
  12. #include "../UI/UIResourceFrame.h"
  13. #include "../UI/UIProjectFrame.h"
  14. #include "../Project/AEProject.h"
  15. #include "AEResourceOps.h"
  16. namespace AtomicEditor
  17. {
  18. /// Construct.
  19. ResourceOps::ResourceOps(Context* context) :
  20. Object(context)
  21. {
  22. context->RegisterSubsystem(this);
  23. }
  24. /// Destruct.
  25. ResourceOps::~ResourceOps()
  26. {
  27. }
  28. bool ResourceOps::CheckCreateComponent(const String& resourcePath, const String& resourceName, bool reportError)
  29. {
  30. Editor* editor = GetSubsystem<Editor>();
  31. Project* project = editor->GetProject();
  32. String fullpath = resourcePath + resourceName;
  33. if (!resourceName.EndsWith(".js"))
  34. fullpath += ".js";
  35. FileSystem* fs = GetSubsystem<FileSystem>();
  36. if (fs->FileExists(fullpath))
  37. {
  38. if (reportError)
  39. {
  40. String errorMsg;
  41. errorMsg.AppendWithFormat("The component:\n\n%s\n\nalready exists", fullpath.CString());
  42. editor->PostModalError("Create Component Error", errorMsg);
  43. }
  44. return false;
  45. }
  46. if (!project->IsComponentsDirOrFile(resourcePath))
  47. {
  48. if (reportError)
  49. {
  50. String errorMsg;
  51. errorMsg.AppendWithFormat("Components must reside in or in a subfolder of the Components folder");
  52. editor->PostModalError("Create Component Error", errorMsg);
  53. }
  54. return false;
  55. }
  56. return true;
  57. }
  58. bool ResourceOps::CheckCreateScript(const String& resourcePath, const String& resourceName, bool reportError)
  59. {
  60. Editor* editor = GetSubsystem<Editor>();
  61. Project* project = editor->GetProject();
  62. String fullpath = resourcePath + resourceName;
  63. if (!resourceName.EndsWith(".js"))
  64. fullpath += ".js";
  65. FileSystem* fs = GetSubsystem<FileSystem>();
  66. if (fs->FileExists(fullpath))
  67. {
  68. if (reportError)
  69. {
  70. String errorMsg;
  71. errorMsg.AppendWithFormat("The script:\n\n%s\n\nalready exists", fullpath.CString());
  72. editor->PostModalError("Create Script Error", errorMsg);
  73. }
  74. return false;
  75. }
  76. if (!project->IsScriptsDirOrFile(resourcePath))
  77. {
  78. if (reportError)
  79. {
  80. String errorMsg;
  81. errorMsg.AppendWithFormat("Scripts must reside in or in a subfolder of the Scripts folder");
  82. editor->PostModalError("Create Script Error", errorMsg);
  83. }
  84. return false;
  85. }
  86. return true;
  87. }
  88. bool ResourceOps::CopyFile(File* srcFile, const String& destFileName)
  89. {
  90. SharedPtr<File> destFile(new File(context_, destFileName, FILE_WRITE));
  91. if (!destFile->IsOpen())
  92. return false;
  93. unsigned fileSize = srcFile->GetSize();
  94. SharedArrayPtr<unsigned char> buffer(new unsigned char[fileSize]);
  95. unsigned bytesRead = srcFile->Read(buffer.Get(), fileSize);
  96. unsigned bytesWritten = destFile->Write(buffer.Get(), fileSize);
  97. return bytesRead == fileSize && bytesWritten == fileSize;
  98. }
  99. void ResourceOps::HandleResourceDelete(const String& resourcePath, bool reportError)
  100. {
  101. Editor* editor = GetSubsystem<Editor>();
  102. FileSystem* fs = GetSubsystem<FileSystem>();
  103. if (fs->DirExists(resourcePath))
  104. {
  105. if (reportError)
  106. editor->PostModalError("Delete Resource Error", "Deleting Folders not currently implemented");
  107. return;
  108. }
  109. else if (fs->FileExists(resourcePath))
  110. {
  111. if (!fs->Delete(resourcePath))
  112. {
  113. if (reportError)
  114. {
  115. String errorMsg;
  116. errorMsg.AppendWithFormat("Unable to delete:\n\n %s", resourcePath.CString());
  117. editor->PostModalError("Delete Resource Error", errorMsg);
  118. }
  119. return;
  120. }
  121. GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();
  122. return;
  123. }
  124. else
  125. {
  126. if (reportError)
  127. {
  128. String errorMsg;
  129. errorMsg.AppendWithFormat("Unable to find:\n\n %s", resourcePath.CString());
  130. editor->PostModalError("Delete Resource Error", errorMsg);
  131. }
  132. return;
  133. }
  134. }
  135. void ResourceOps::HandleNewFolder(const String& resourcePath, bool reportError)
  136. {
  137. Editor* editor = GetSubsystem<Editor>();
  138. FileSystem* fs = GetSubsystem<FileSystem>();
  139. if (fs->DirExists(resourcePath) || fs->FileExists(resourcePath))
  140. {
  141. if (reportError)
  142. {
  143. String errorMsg;
  144. errorMsg.AppendWithFormat("Already exists:\n\n %s", resourcePath.CString());
  145. editor->PostModalError("New Folder Error", errorMsg);
  146. }
  147. return;
  148. }
  149. if (!fs->CreateDir(resourcePath))
  150. {
  151. if (reportError)
  152. {
  153. String errorMsg;
  154. errorMsg.AppendWithFormat("Could not create:\n\n %s", resourcePath.CString());
  155. editor->PostModalError("New Folder Error", errorMsg);
  156. }
  157. return;
  158. }
  159. // file watcher doesn't currently handle subdir
  160. GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();
  161. }
  162. void ResourceOps::HandleCreateComponent(const String& resourcePath, const String& resourceName,
  163. bool navigateToResource, bool reportError)
  164. {
  165. Editor* editor = GetSubsystem<Editor>();
  166. if (!CheckCreateComponent(resourcePath, resourceName, reportError))
  167. return;
  168. ResourceCache* cache = GetSubsystem<ResourceCache>();
  169. SharedPtr<File> srcFile = cache->GetFile("AtomicEditor/templates/template_component.js");
  170. if (srcFile.Null() || !srcFile->IsOpen())
  171. {
  172. editor->PostModalError("Create Component Error", "Could not open component template");
  173. return;
  174. }
  175. String fullpath = resourcePath + resourceName;
  176. if (!resourceName.EndsWith(".js"))
  177. fullpath += ".js";
  178. if (!CopyFile(srcFile, fullpath))
  179. {
  180. String errorMsg;
  181. errorMsg.AppendWithFormat("Error copying template:\n\n%s\n\nto:\n\n%s",
  182. "AtomicEditor/template_component.js", fullpath.CString());
  183. editor->PostModalError("Create Component Error", errorMsg);
  184. return;
  185. }
  186. if (navigateToResource)
  187. {
  188. ResourceFrame* rframe = GetSubsystem<MainFrame>()->GetResourceFrame();
  189. rframe->EditResource(fullpath);
  190. }
  191. GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();
  192. }
  193. void ResourceOps::HandleCreateScript(const String& resourcePath, const String& resourceName,
  194. bool navigateToResource, bool reportError)
  195. {
  196. Editor* editor = GetSubsystem<Editor>();
  197. if (!CheckCreateScript(resourcePath, resourceName, reportError))
  198. return;
  199. ResourceCache* cache = GetSubsystem<ResourceCache>();
  200. SharedPtr<File> srcFile = cache->GetFile("AtomicEditor/templates/template_script.js");
  201. if (srcFile.Null() || !srcFile->IsOpen())
  202. {
  203. editor->PostModalError("Create Script Error", "Could not open script template");
  204. return;
  205. }
  206. String fullpath = resourcePath + resourceName;
  207. if (!resourceName.EndsWith(".js"))
  208. fullpath += ".js";
  209. if (!CopyFile(srcFile, fullpath))
  210. {
  211. String errorMsg;
  212. errorMsg.AppendWithFormat("Error copying template:\n\n%s\n\nto:\n\n%s",
  213. "AtomicEditor/template_script.js", fullpath.CString());
  214. editor->PostModalError("Create Script Error", errorMsg);
  215. return;
  216. }
  217. if (navigateToResource)
  218. {
  219. ResourceFrame* rframe = GetSubsystem<MainFrame>()->GetResourceFrame();
  220. rframe->EditResource(fullpath);
  221. }
  222. GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();
  223. }
  224. }