AEResourceOps.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. SubscribeToEvent(E_EDITORSHUTDOWN, HANDLER(ResourceOps, HandleEditorShutdown));
  24. }
  25. /// Destruct.
  26. ResourceOps::~ResourceOps()
  27. {
  28. }
  29. bool ResourceOps::CheckCreateComponent(const String& resourcePath, const String& resourceName, bool reportError)
  30. {
  31. Editor* editor = GetSubsystem<Editor>();
  32. Project* project = editor->GetProject();
  33. String fullpath = resourcePath + resourceName;
  34. if (!resourceName.EndsWith(".js"))
  35. fullpath += ".js";
  36. FileSystem* fs = GetSubsystem<FileSystem>();
  37. if (fs->FileExists(fullpath))
  38. {
  39. if (reportError)
  40. {
  41. String errorMsg;
  42. errorMsg.AppendWithFormat("The component:\n\n%s\n\nalready exists", fullpath.CString());
  43. editor->PostModalError("Create Component Error", errorMsg);
  44. }
  45. return false;
  46. }
  47. if (!project->IsComponentsDirOrFile(resourcePath))
  48. {
  49. if (reportError)
  50. {
  51. String errorMsg;
  52. errorMsg.AppendWithFormat("Components must reside in or in a subfolder of the Components folder");
  53. editor->PostModalError("Create Component Error", errorMsg);
  54. }
  55. return false;
  56. }
  57. return true;
  58. }
  59. bool ResourceOps::CheckCreateScript(const String& resourcePath, const String& resourceName, bool reportError)
  60. {
  61. Editor* editor = GetSubsystem<Editor>();
  62. Project* project = editor->GetProject();
  63. String fullpath = resourcePath + resourceName;
  64. if (!resourceName.EndsWith(".js"))
  65. fullpath += ".js";
  66. FileSystem* fs = GetSubsystem<FileSystem>();
  67. if (fs->FileExists(fullpath))
  68. {
  69. if (reportError)
  70. {
  71. String errorMsg;
  72. errorMsg.AppendWithFormat("The script:\n\n%s\n\nalready exists", fullpath.CString());
  73. editor->PostModalError("Create Script Error", errorMsg);
  74. }
  75. return false;
  76. }
  77. if (!project->IsScriptsDirOrFile(resourcePath))
  78. {
  79. if (reportError)
  80. {
  81. String errorMsg;
  82. errorMsg.AppendWithFormat("Scripts must reside in or in a subfolder of the Scripts folder");
  83. editor->PostModalError("Create Script Error", errorMsg);
  84. }
  85. return false;
  86. }
  87. return true;
  88. }
  89. bool ResourceOps::CheckCreateModule(const String& resourcePath, const String& resourceName, bool reportError)
  90. {
  91. Editor* editor = GetSubsystem<Editor>();
  92. Project* project = editor->GetProject();
  93. String fullpath = resourcePath + resourceName;
  94. if (!resourceName.EndsWith(".js"))
  95. fullpath += ".js";
  96. FileSystem* fs = GetSubsystem<FileSystem>();
  97. if (fs->FileExists(fullpath))
  98. {
  99. if (reportError)
  100. {
  101. String errorMsg;
  102. errorMsg.AppendWithFormat("The module:\n\n%s\n\nalready exists", fullpath.CString());
  103. editor->PostModalError("Create Module Error", errorMsg);
  104. }
  105. return false;
  106. }
  107. if (!project->IsModulesDirOrFile(resourcePath))
  108. {
  109. if (reportError)
  110. {
  111. String errorMsg;
  112. errorMsg.AppendWithFormat("Modules must reside in or in a subfolder of the Modules folder");
  113. editor->PostModalError("Create Module Error", errorMsg);
  114. }
  115. return false;
  116. }
  117. return true;
  118. }
  119. bool ResourceOps::CheckCreate2DLevel(const String& resourcePath, const String& resourceName, bool reportError)
  120. {
  121. Editor* editor = GetSubsystem<Editor>();
  122. Project* project = editor->GetProject();
  123. String fullpath = resourcePath + resourceName;
  124. if (!resourceName.EndsWith(".tmx"))
  125. fullpath += ".tmx";
  126. FileSystem* fs = GetSubsystem<FileSystem>();
  127. if (fs->FileExists(fullpath))
  128. {
  129. if (reportError)
  130. {
  131. String errorMsg;
  132. errorMsg.AppendWithFormat("The level:\n\n%s\n\nalready exists", fullpath.CString());
  133. editor->PostModalError("Create 2D Level Error", errorMsg);
  134. }
  135. return false;
  136. }
  137. return true;
  138. }
  139. bool ResourceOps::CopyFile(File* srcFile, const String& destFileName)
  140. {
  141. SharedPtr<File> destFile(new File(context_, destFileName, FILE_WRITE));
  142. if (!destFile->IsOpen())
  143. return false;
  144. unsigned fileSize = srcFile->GetSize();
  145. SharedArrayPtr<unsigned char> buffer(new unsigned char[fileSize]);
  146. unsigned bytesRead = srcFile->Read(buffer.Get(), fileSize);
  147. unsigned bytesWritten = destFile->Write(buffer.Get(), fileSize);
  148. return bytesRead == fileSize && bytesWritten == fileSize;
  149. }
  150. void ResourceOps::HandleResourceDelete(const String& resourcePath, bool reportError)
  151. {
  152. Editor* editor = GetSubsystem<Editor>();
  153. FileSystem* fs = GetSubsystem<FileSystem>();
  154. if (fs->DirExists(resourcePath))
  155. {
  156. fs->RemoveDir(resourcePath, true);
  157. GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();
  158. return;
  159. }
  160. else if (fs->FileExists(resourcePath))
  161. {
  162. if (!fs->Delete(resourcePath))
  163. {
  164. if (reportError)
  165. {
  166. String errorMsg;
  167. errorMsg.AppendWithFormat("Unable to delete:\n\n %s", resourcePath.CString());
  168. editor->PostModalError("Delete Resource Error", errorMsg);
  169. }
  170. return;
  171. }
  172. GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();
  173. return;
  174. }
  175. else
  176. {
  177. if (reportError)
  178. {
  179. String errorMsg;
  180. errorMsg.AppendWithFormat("Unable to find:\n\n %s", resourcePath.CString());
  181. editor->PostModalError("Delete Resource Error", errorMsg);
  182. }
  183. return;
  184. }
  185. }
  186. void ResourceOps::HandleNewFolder(const String& resourcePath, bool reportError)
  187. {
  188. Editor* editor = GetSubsystem<Editor>();
  189. FileSystem* fs = GetSubsystem<FileSystem>();
  190. if (fs->DirExists(resourcePath) || fs->FileExists(resourcePath))
  191. {
  192. if (reportError)
  193. {
  194. String errorMsg;
  195. errorMsg.AppendWithFormat("Already exists:\n\n %s", resourcePath.CString());
  196. editor->PostModalError("New Folder Error", errorMsg);
  197. }
  198. return;
  199. }
  200. if (!fs->CreateDir(resourcePath))
  201. {
  202. if (reportError)
  203. {
  204. String errorMsg;
  205. errorMsg.AppendWithFormat("Could not create:\n\n %s", resourcePath.CString());
  206. editor->PostModalError("New Folder Error", errorMsg);
  207. }
  208. return;
  209. }
  210. // file watcher doesn't currently handle subdir
  211. GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();
  212. }
  213. void ResourceOps::HandleCreateComponent(const String& resourcePath, const String& resourceName,
  214. bool navigateToResource, bool reportError)
  215. {
  216. Editor* editor = GetSubsystem<Editor>();
  217. if (!CheckCreateComponent(resourcePath, resourceName, reportError))
  218. return;
  219. ResourceCache* cache = GetSubsystem<ResourceCache>();
  220. SharedPtr<File> srcFile = cache->GetFile("AtomicEditor/templates/template_component.js");
  221. if (srcFile.Null() || !srcFile->IsOpen())
  222. {
  223. editor->PostModalError("Create Component Error", "Could not open component template");
  224. return;
  225. }
  226. String fullpath = resourcePath + resourceName;
  227. if (!resourceName.EndsWith(".js"))
  228. fullpath += ".js";
  229. if (!CopyFile(srcFile, fullpath))
  230. {
  231. String errorMsg;
  232. errorMsg.AppendWithFormat("Error copying template:\n\n%s\n\nto:\n\n%s",
  233. "AtomicEditor/template_component.js", fullpath.CString());
  234. editor->PostModalError("Create Component Error", errorMsg);
  235. return;
  236. }
  237. if (navigateToResource)
  238. {
  239. ResourceFrame* rframe = GetSubsystem<MainFrame>()->GetResourceFrame();
  240. rframe->EditResource(fullpath);
  241. }
  242. GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();
  243. }
  244. void ResourceOps::HandleCreateScript(const String& resourcePath, const String& resourceName,
  245. bool navigateToResource, bool reportError)
  246. {
  247. Editor* editor = GetSubsystem<Editor>();
  248. if (!CheckCreateScript(resourcePath, resourceName, reportError))
  249. return;
  250. ResourceCache* cache = GetSubsystem<ResourceCache>();
  251. SharedPtr<File> srcFile = cache->GetFile("AtomicEditor/templates/template_script.js");
  252. if (srcFile.Null() || !srcFile->IsOpen())
  253. {
  254. editor->PostModalError("Create Script Error", "Could not open script template");
  255. return;
  256. }
  257. String fullpath = resourcePath + resourceName;
  258. if (!resourceName.EndsWith(".js"))
  259. fullpath += ".js";
  260. if (!CopyFile(srcFile, fullpath))
  261. {
  262. String errorMsg;
  263. errorMsg.AppendWithFormat("Error copying template:\n\n%s\n\nto:\n\n%s",
  264. "AtomicEditor/template_script.js", fullpath.CString());
  265. editor->PostModalError("Create Script Error", errorMsg);
  266. return;
  267. }
  268. if (navigateToResource)
  269. {
  270. ResourceFrame* rframe = GetSubsystem<MainFrame>()->GetResourceFrame();
  271. rframe->EditResource(fullpath);
  272. }
  273. GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();
  274. }
  275. void ResourceOps::HandleCreateModule(const String& resourcePath, const String& resourceName,
  276. bool navigateToResource, bool reportError)
  277. {
  278. Editor* editor = GetSubsystem<Editor>();
  279. if (!CheckCreateModule(resourcePath, resourceName, reportError))
  280. return;
  281. ResourceCache* cache = GetSubsystem<ResourceCache>();
  282. SharedPtr<File> srcFile = cache->GetFile("AtomicEditor/templates/template_module.js");
  283. if (srcFile.Null() || !srcFile->IsOpen())
  284. {
  285. editor->PostModalError("Create Script Error", "Could not open module template");
  286. return;
  287. }
  288. String fullpath = resourcePath + resourceName;
  289. if (!resourceName.EndsWith(".js"))
  290. fullpath += ".js";
  291. if (!CopyFile(srcFile, fullpath))
  292. {
  293. String errorMsg;
  294. errorMsg.AppendWithFormat("Error copying template:\n\n%s\n\nto:\n\n%s",
  295. "AtomicEditor/template_module.js", fullpath.CString());
  296. editor->PostModalError("Create Module Error", errorMsg);
  297. return;
  298. }
  299. if (navigateToResource)
  300. {
  301. ResourceFrame* rframe = GetSubsystem<MainFrame>()->GetResourceFrame();
  302. rframe->EditResource(fullpath);
  303. }
  304. GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();
  305. }
  306. void ResourceOps::HandleCreate2DLevel(const String& resourcePath, const String& resourceName,
  307. bool navigateToResource, bool reportError)
  308. {
  309. Editor* editor = GetSubsystem<Editor>();
  310. if (!CheckCreate2DLevel(resourcePath, resourceName, reportError))
  311. return;
  312. ResourceCache* cache = GetSubsystem<ResourceCache>();
  313. SharedPtr<File> srcFile = cache->GetFile("AtomicEditor/templates/template_empty.tmx");
  314. if (srcFile.Null() || !srcFile->IsOpen())
  315. {
  316. editor->PostModalError("Create Script Error", "Could not open module template");
  317. return;
  318. }
  319. String fullpath = resourcePath + resourceName;
  320. if (!resourceName.EndsWith(".tmx"))
  321. fullpath += ".tmx";
  322. if (!CopyFile(srcFile, fullpath))
  323. {
  324. String errorMsg;
  325. errorMsg.AppendWithFormat("Error copying template:\n\n%s\n\nto:\n\n%s",
  326. "AtomicEditor/template_empty.tmx", fullpath.CString());
  327. editor->PostModalError("Create 2D Level Error", errorMsg);
  328. return;
  329. }
  330. if (navigateToResource)
  331. {
  332. //ResourceFrame* rframe = GetSubsystem<MainFrame>()->GetResourceFrame();
  333. //rframe->EditResource(fullpath);
  334. }
  335. GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();
  336. }
  337. void ResourceOps::HandleEditorShutdown(StringHash eventType, VariantMap& eventData)
  338. {
  339. context_->RemoveSubsystem(GetType());
  340. }
  341. }