BsEditorApplication.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. #include "BsEditorApplication.h"
  2. #include "BsEditorWindowManager.h"
  3. #include "BsEditorWidgetManager.h"
  4. #include "BsMainEditorWindow.h"
  5. #include "BsRenderWindow.h"
  6. #include "BsBuiltinEditorResources.h"
  7. #include "BsUndoRedo.h"
  8. #include "BsFileSerializer.h"
  9. #include "BsFileSystem.h"
  10. #include "BsResourceImporter.h"
  11. #include "BsEditorWidgetLayout.h"
  12. #include "BsScenePicking.h"
  13. #include "BsSelection.h"
  14. #include "BsGizmoManager.h"
  15. #include "BsCodeEditor.h"
  16. #include "BsBuildManager.h"
  17. #include "BsScriptCodeImporter.h"
  18. #include "BsShaderIncludeHandler.h"
  19. #include "BsDropDownWindowManager.h"
  20. #include "BsPrefabImporter.h"
  21. #include "BsProjectLibrary.h"
  22. #include "BsProjectSettings.h"
  23. #include "BsEditorSettings.h"
  24. #include "BsScriptManager.h"
  25. #include "BsFileSystem.h"
  26. // DEBUG ONLY
  27. #include "BsResources.h"
  28. #include "BsSceneObject.h"
  29. #include "BsImporter.h"
  30. #include "BsGpuProgram.h"
  31. #include "BsShader.h"
  32. #include "BsTexture.h"
  33. #include "BsMaterial.h"
  34. #include "BsTechnique.h"
  35. #include "BsPass.h"
  36. #include "BsCRenderable.h"
  37. #include "BsVirtualInput.h"
  38. #include "BsFolderMonitor.h"
  39. #include "BsCCamera.h"
  40. #include "BsCGUIWidget.h"
  41. #include "BsGUIButton.h"
  42. #include "BsGUILayout.h"
  43. #include "BsEvent.h"
  44. #include "BsCoreRenderer.h"
  45. #include "BsMesh.h"
  46. #include "BsMath.h"
  47. #include "BsDebug.h"
  48. #include "../../BansheeFreeImgImporter/Dependencies/Include/FreeImage.h"
  49. namespace BansheeEngine
  50. {
  51. const Path EditorApplication::WIDGET_LAYOUT_PATH = PROJECT_INTERNAL_DIR + L"Layout.asset";
  52. const Path EditorApplication::BUILD_DATA_PATH = PROJECT_INTERNAL_DIR + L"BuildData.asset";
  53. const Path EditorApplication::EDITOR_SETTINGS_PATH = RUNTIME_DATA_PATH + L"Settings.asset";
  54. const Path EditorApplication::PROJECT_SETTINGS_PATH = PROJECT_INTERNAL_DIR + L"Settings.asset";
  55. RENDER_WINDOW_DESC createRenderWindowDesc()
  56. {
  57. RENDER_WINDOW_DESC renderWindowDesc;
  58. renderWindowDesc.videoMode = VideoMode(1280, 720);
  59. renderWindowDesc.title = "BansheeEditor";
  60. renderWindowDesc.fullscreen = false;
  61. renderWindowDesc.border = WindowBorder::None;
  62. renderWindowDesc.hideUntilSwap = true;
  63. return renderWindowDesc;
  64. }
  65. EditorApplication::EditorApplication(RenderAPIPlugin renderAPIPlugin)
  66. :Application(createRenderWindowDesc(), renderAPIPlugin, RendererPlugin::Default),
  67. mActiveRAPIPlugin(renderAPIPlugin), mSBansheeEditorPlugin(nullptr), mIsProjectLoaded(false)
  68. {
  69. }
  70. EditorApplication::~EditorApplication()
  71. {
  72. /************************************************************************/
  73. /* DEBUG CODE */
  74. /************************************************************************/
  75. gResources().unload(mTestTexRef);
  76. gResources().unload(mDbgMeshRef);
  77. gResources().unload(mTestShader);
  78. gResources().unload(mTestMaterial);
  79. mTestMaterial = nullptr;
  80. mTestTexRef = nullptr;
  81. mDbgMeshRef = nullptr;
  82. mTestShader = nullptr;
  83. /************************************************************************/
  84. /* END DEBUG CODE */
  85. /************************************************************************/
  86. ProjectLibrary::shutDown();
  87. BuiltinEditorResources::shutDown();
  88. }
  89. void EditorApplication::onStartUp()
  90. {
  91. Application::onStartUp();
  92. loadEditorSettings();
  93. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  94. BuiltinEditorResources::startUp();
  95. {
  96. auto inputConfig = VirtualInput::instance().getConfiguration();
  97. inputConfig->registerButton("Rename", BC_F2);
  98. inputConfig->registerButton("Undo", BC_Z, ButtonModifier::Ctrl);
  99. inputConfig->registerButton("Redo", BC_Y, ButtonModifier::Ctrl);
  100. inputConfig->registerButton("Copy", BC_C, ButtonModifier::Ctrl);
  101. inputConfig->registerButton("Cut", BC_X, ButtonModifier::Ctrl);
  102. inputConfig->registerButton("Paste", BC_V, ButtonModifier::Ctrl);
  103. inputConfig->registerButton("Duplicate", BC_D, ButtonModifier::Ctrl);
  104. inputConfig->registerButton("Delete", BC_DELETE);
  105. }
  106. ScriptCodeImporter* scriptCodeImporter = bs_new<ScriptCodeImporter>();
  107. Importer::instance()._registerAssetImporter(scriptCodeImporter);
  108. ResourceImporter* resourceImporter = bs_new<ResourceImporter>();
  109. Importer::instance()._registerAssetImporter(resourceImporter);
  110. PrefabImporter* prefabImporter = bs_new<PrefabImporter>();
  111. Importer::instance()._registerAssetImporter(prefabImporter);
  112. ProjectLibrary::startUp();
  113. UndoRedo::startUp();
  114. EditorWindowManager::startUp();
  115. EditorWidgetManager::startUp();
  116. DropDownWindowManager::startUp();
  117. ScenePicking::startUp();
  118. Selection::startUp();
  119. GizmoManager::startUp();
  120. BuildManager::startUp();
  121. CodeEditorManager::startUp();
  122. MainEditorWindow* mainWindow = MainEditorWindow::create(getPrimaryWindow());
  123. ScriptManager::instance().initialize();
  124. /************************************************************************/
  125. /* DEBUG CODE */
  126. /************************************************************************/
  127. HShader dummyParsedShader = Importer::instance().import<Shader>(RUNTIME_DATA_PATH + "Raw\\Engine\\Shaders\\TestFX.bsl");
  128. assert(dummyParsedShader != nullptr); // Ad hoc unit test
  129. RenderAPICore* renderAPI = RenderAPICore::instancePtr();
  130. HSceneObject testModelGO = SceneObject::create("TestMesh");
  131. HRenderable testRenderable = testModelGO->addComponent<CRenderable>();
  132. Path testShaderLoc = RUNTIME_DATA_PATH + L"Test.bsl";;
  133. mTestShader = Importer::instance().import<Shader>(testShaderLoc);
  134. gResources().save(mTestShader, L"E:\\testShader.asset", true);
  135. gResources().unload(mTestShader);
  136. mTestShader = gResources().load<Shader>(L"E:\\testShader.asset");
  137. mTestMaterial = Material::create(mTestShader);
  138. mTestTexRef = static_resource_cast<Texture>(Importer::instance().import(RUNTIME_DATA_PATH + L"Examples\\Dragon.tga"));
  139. mDbgMeshRef = static_resource_cast<Mesh>(Importer::instance().import(RUNTIME_DATA_PATH + L"Examples\\Dragon.fbx"));
  140. gResources().save(mTestTexRef, L"E:\\ExportTest.tex", true);
  141. gResources().save(mDbgMeshRef, L"E:\\ExportMesh.mesh", true);
  142. gResources().unload(mTestTexRef);
  143. gResources().unload(mDbgMeshRef);
  144. mTestTexRef = static_resource_cast<Texture>(gResources().loadAsync(L"E:\\ExportTest.tex"));
  145. mDbgMeshRef = static_resource_cast<Mesh>(gResources().loadAsync(L"E:\\ExportMesh.mesh"));
  146. mDbgMeshRef.blockUntilLoaded();
  147. mDbgMeshRef->blockUntilCoreInitialized();
  148. mTestTexRef.blockUntilLoaded();
  149. mTestTexRef->blockUntilCoreInitialized();
  150. mTestMaterial->setTexture("tex", mTestTexRef);
  151. gResources().save(mTestShader, L"E:\\ExportShader.asset", true);
  152. gResources().save(mTestMaterial, L"E:\\ExportMaterial.mat", true);
  153. gResources().unload(mTestMaterial);
  154. gResources().unload(mTestShader);
  155. mTestShader = gResources().load<Shader>(L"E:\\ExportShader.asset");
  156. mTestMaterial = gResources().load<Material>(L"E:\\ExportMaterial.mat");
  157. testRenderable->setMesh(mDbgMeshRef);
  158. testRenderable->setMaterial(0, mTestMaterial);
  159. HSceneObject clone = testModelGO->clone();
  160. testModelGO->destroy();
  161. /************************************************************************/
  162. /* END DEBUG CODE */
  163. /************************************************************************/
  164. }
  165. void EditorApplication::onShutDown()
  166. {
  167. unloadProject();
  168. CodeEditorManager::shutDown();
  169. BuildManager::shutDown();
  170. GizmoManager::shutDown();
  171. Selection::shutDown();
  172. ScenePicking::shutDown();
  173. saveEditorSettings();
  174. DropDownWindowManager::shutDown();
  175. EditorWidgetManager::shutDown();
  176. EditorWindowManager::shutDown();
  177. UndoRedo::shutDown();
  178. Application::onShutDown();
  179. }
  180. void EditorApplication::loadScriptSystem()
  181. {
  182. loadPlugin("BansheeMono", &mMonoPlugin);
  183. loadPlugin("SBansheeEngine", &mSBansheeEnginePlugin);
  184. loadPlugin("SBansheeEditor", &mSBansheeEditorPlugin);
  185. }
  186. // DEBUG ONLY
  187. struct MSDOSHeader
  188. {
  189. UINT16 signature;
  190. UINT16 lastSize;
  191. UINT16 numBlocks;
  192. UINT16 numReloc;
  193. UINT16 hdrSize;
  194. UINT16 minAlloc;
  195. UINT16 maxAlloc;
  196. UINT16 ss;
  197. UINT16 sp;
  198. UINT16 checksum;
  199. UINT16 ip;
  200. UINT16 cs;
  201. UINT16 relocPos;
  202. UINT16 numOverlay;
  203. UINT16 reserved1[4];
  204. UINT16 oemId;
  205. UINT16 oemInfo;
  206. UINT16 reserved2[10];
  207. UINT32 lfanew;
  208. };
  209. struct COFFHeader
  210. {
  211. UINT16 machine;
  212. UINT16 numSections;
  213. UINT32 timeDateStamp;
  214. UINT32 ptrSymbolTable;
  215. UINT32 numSymbols;
  216. UINT16 sizeOptHeader;
  217. UINT16 characteristics;
  218. };
  219. struct PEDataDirectory
  220. {
  221. UINT32 virtualAddress;
  222. UINT32 size;
  223. };
  224. struct PEOptionalHeader32
  225. {
  226. UINT16 signature;
  227. UINT8 majorLinkerVersion;
  228. UINT8 minorLinkerVersion;
  229. UINT32 sizeCode;
  230. UINT32 sizeInitializedData;
  231. UINT32 sizeUninitializedData;
  232. UINT32 addressEntryPoint;
  233. UINT32 baseCode;
  234. UINT32 baseData;
  235. UINT32 baseImage;
  236. UINT32 alignmentSection;
  237. UINT32 alignmentFile;
  238. UINT16 majorOSVersion;
  239. UINT16 minorOSVersion;
  240. UINT16 majorImageVersion;
  241. UINT16 minorImageVersion;
  242. UINT16 majorSubsystemVersion;
  243. UINT16 minorSubsystemVersion;
  244. UINT32 reserved;
  245. UINT32 sizeImage;
  246. UINT32 sizeHeaders;
  247. UINT32 checksum;
  248. UINT16 subsystem;
  249. UINT16 characteristics;
  250. UINT32 sizeStackReserve;
  251. UINT32 sizeStackCommit;
  252. UINT32 sizeHeapReserve;
  253. UINT32 sizeHeapCommit;
  254. UINT32 loaderFlags;
  255. UINT32 NumRvaAndSizes;
  256. PEDataDirectory dataDirectory[16];
  257. };
  258. struct PEOptionalHeader64
  259. {
  260. UINT16 signature;
  261. UINT8 majorLinkerVersion;
  262. UINT8 minorLinkerVersion;
  263. UINT32 sizeCode;
  264. UINT32 sizeInitializedData;
  265. UINT32 sizeUninitializedData;
  266. UINT32 addressEntryPoint;
  267. UINT32 baseCode;
  268. UINT64 baseImage;
  269. UINT32 alignmentSection;
  270. UINT32 alignmentFile;
  271. UINT16 majorOSVersion;
  272. UINT16 minorOSVersion;
  273. UINT16 majorImageVersion;
  274. UINT16 minorImageVersion;
  275. UINT16 majorSubsystemVersion;
  276. UINT16 minorSubsystemVersion;
  277. UINT32 reserved;
  278. UINT32 sizeImage;
  279. UINT32 sizeHeaders;
  280. UINT32 checksum;
  281. UINT16 subsystem;
  282. UINT16 characteristics;
  283. UINT64 sizeStackReserve;
  284. UINT64 sizeStackCommit;
  285. UINT64 sizeHeapReserve;
  286. UINT64 sizeHeapCommit;
  287. UINT32 loaderFlags;
  288. UINT32 NumRvaAndSizes;
  289. PEDataDirectory dataDirectory[16];
  290. };
  291. struct PESectionHeader
  292. {
  293. char name[8];
  294. UINT32 virtualSize;
  295. UINT32 relativeVirtualAddress;
  296. UINT32 physicalSize;
  297. UINT32 physicalAddress;
  298. UINT8 deprecated[12];
  299. UINT32 flags;
  300. };
  301. struct PEImageResourceDirectory
  302. {
  303. UINT32 flags;
  304. UINT32 timeDateStamp;
  305. UINT16 majorVersion;
  306. UINT16 minorVersion;
  307. UINT16 numNamedEntries;
  308. UINT16 numIdEntries;
  309. };
  310. struct PEImageResourceEntryData
  311. {
  312. UINT32 offsetData;
  313. UINT32 size;
  314. UINT32 codePage;
  315. UINT32 resourceHandle;
  316. };
  317. struct PEImageResourceEntry
  318. {
  319. UINT32 offsetName;
  320. UINT32 type;
  321. UINT32 offsetDirectory : 31;
  322. UINT32 isDirectory : 1;
  323. };
  324. #define MSDOS_SIGNATURE 0x5A4D
  325. #define PE_SIGNATURE 0x0000455
  326. #define PE_32BIT_SIGNATURE 0x10B
  327. #define PE_64BIT_SIGNATURE 0x20B
  328. #define PE_NUM_DIRECTORY_ENTRIES 16
  329. #define PE_SECTION_UNINITIALIZED_DATA 0x00000080
  330. #define PE_IMAGE_DIRECTORY_ENTRY_RESOURCE 2
  331. #define PE_IMAGE_RT_ICON 3
  332. struct IconHeader
  333. {
  334. UINT32 size;
  335. INT32 width;
  336. INT32 height;
  337. UINT16 planes;
  338. UINT16 bitCount;
  339. UINT32 compression;
  340. UINT32 sizeImage;
  341. INT32 xPelsPerMeter;
  342. INT32 yPelsPerMeter;
  343. UINT32 clrUsed;
  344. UINT32 clrImportant;
  345. };
  346. void copyPixelsToIcon(const Map<UINT32, PixelDataPtr>& pixelsPerSize, UINT8* iconData)
  347. {
  348. IconHeader* iconHeader = (IconHeader*)iconData;
  349. if (iconHeader->size != sizeof(IconHeader) || iconHeader->compression != 0
  350. || iconHeader->planes != 1 || iconHeader->bitCount != 32)
  351. {
  352. // Unsupported format
  353. return;
  354. }
  355. UINT8* iconPixels = iconData + sizeof(IconHeader);
  356. UINT32 width = iconHeader->width;
  357. UINT32 height = iconHeader->height / 2;
  358. auto iterFind = pixelsPerSize.find(width);
  359. if (iterFind == pixelsPerSize.end() || iterFind->second->getWidth() != width
  360. || iterFind->second->getHeight() != height)
  361. {
  362. // No icon of this size provided
  363. return;
  364. }
  365. // Write colors
  366. PixelDataPtr srcPixels = iterFind->second;
  367. UINT32* colorData = (UINT32*)iconPixels;
  368. UINT32 idx = 0;
  369. for (UINT32 y = 0; y < height; y++)
  370. {
  371. for (UINT32 x = 0; x < width; x++)
  372. colorData[idx] = srcPixels->getColorAt(x, y).getAsBGRA();
  373. }
  374. // Write AND mask
  375. UINT32 colorDataSize = width * height * sizeof(UINT32);
  376. UINT8* maskData = iconPixels + colorDataSize;
  377. UINT32 numPackedPixels = width / 8; // One per bit in byte
  378. for (UINT32 y = 0; y < height; y++)
  379. {
  380. UINT8 mask = 0;
  381. for (UINT32 packedX = 0; packedX < numPackedPixels; packedX++)
  382. {
  383. for (UINT32 pixelIdx = 0; pixelIdx < 8; pixelIdx++)
  384. {
  385. UINT32 x = packedX * 8 + pixelIdx;
  386. Color color = srcPixels->getColorAt(x, y);
  387. if (color.a < 0.25f)
  388. mask |= 1 << 7 - pixelIdx;
  389. }
  390. *maskData = mask;
  391. maskData++;
  392. }
  393. }
  394. }
  395. void setIconInEXE(const Path& path, const Map<UINT32, PixelDataPtr>& pixelsPerSize)
  396. {
  397. // A PE file is structured as such:
  398. // - MSDOS Header
  399. // - PE Signature
  400. // - COFF Header
  401. // - PE Optional Header
  402. // - One or multiple sections
  403. // - .code
  404. // - .data
  405. // - ...
  406. // - .rsrc
  407. // - icon/cursor/etc data
  408. std::fstream stream;
  409. stream.open(path.toString().c_str(), std::ios::in | std::ios::out | std::ios::binary);
  410. // First check magic number to ensure file is even an executable
  411. UINT16 magicNum;
  412. stream.read((char*)&magicNum, sizeof(magicNum));
  413. if (magicNum != MSDOS_SIGNATURE)
  414. BS_EXCEPT(InvalidStateException, "Provided file is not a valid executable.");
  415. // Read the MSDOS header and skip over it
  416. stream.seekg(0);
  417. MSDOSHeader msdosHeader;
  418. stream.read((char*)&msdosHeader, sizeof(MSDOSHeader));
  419. // Read PE signature
  420. stream.seekg(msdosHeader.lfanew);
  421. UINT32 peSignature;
  422. stream.read((char*)&peSignature, sizeof(peSignature));
  423. if (peSignature != PE_SIGNATURE)
  424. BS_EXCEPT(InvalidStateException, "Provided file is not in PE format.");
  425. // Read COFF header
  426. COFFHeader coffHeader;
  427. stream.read((char*)&coffHeader, sizeof(COFFHeader));
  428. if (coffHeader.sizeOptHeader == 0) // .exe files always have an optional header
  429. BS_EXCEPT(InvalidStateException, "Provided file is not a valid executable.");
  430. UINT32 numSectionHeaders = coffHeader.numSections;
  431. // Read optional header
  432. auto optionalHeaderPos = stream.tellg();
  433. UINT16 optionalHeaderSignature;
  434. stream.read((char*)&optionalHeaderSignature, sizeof(optionalHeaderSignature));
  435. PEDataDirectory* dataDirectory = nullptr;
  436. stream.seekg(optionalHeaderPos);
  437. if (optionalHeaderSignature == PE_32BIT_SIGNATURE)
  438. {
  439. PEOptionalHeader32 optionalHeader;
  440. stream.read((char*)&optionalHeader, sizeof(optionalHeader));
  441. dataDirectory = optionalHeader.dataDirectory;
  442. }
  443. else if (optionalHeaderSignature == PE_64BIT_SIGNATURE)
  444. {
  445. PEOptionalHeader64 optionalHeader;
  446. stream.read((char*)&optionalHeader, sizeof(optionalHeader));
  447. dataDirectory = optionalHeader.dataDirectory;
  448. }
  449. else
  450. BS_EXCEPT(InvalidStateException, "Unrecognized PE format.");
  451. // Read section headers
  452. auto sectionHeaderPos = optionalHeaderPos + (std::ifstream::pos_type)coffHeader.sizeOptHeader;
  453. stream.seekg(sectionHeaderPos);
  454. PESectionHeader* sectionHeaders = bs_stack_alloc<PESectionHeader>(numSectionHeaders);
  455. stream.read((char*)sectionHeaders, sizeof(PESectionHeader) * numSectionHeaders);
  456. // Look for .rsrc section header
  457. std::function<void(PEImageResourceDirectory*, PEImageResourceDirectory*, UINT8*, UINT32)> setIconData =
  458. [&](PEImageResourceDirectory* base, PEImageResourceDirectory* current, UINT8* imageData, UINT32 sectionAddress)
  459. {
  460. UINT32 numEntries = current->numIdEntries + current->numNamedEntries;
  461. PEImageResourceEntry* entries = (PEImageResourceEntry*)(current + 1);
  462. for (UINT32 i = 0; i < numEntries; i++)
  463. {
  464. if (entries[i].type != PE_IMAGE_RT_ICON)
  465. continue;
  466. if(entries[i].isDirectory)
  467. {
  468. PEImageResourceDirectory* child = (PEImageResourceDirectory*)(((UINT8*)base) + entries[i].offsetDirectory);
  469. setIconData(base, child, imageData, sectionAddress);
  470. }
  471. else
  472. {
  473. PEImageResourceEntryData* data = (PEImageResourceEntryData*)(((UINT8*)base) + entries[i].offsetDirectory);
  474. UINT8* iconData = imageData + (data->offsetData - sectionAddress);
  475. copyPixelsToIcon(pixelsPerSize, iconData);
  476. }
  477. }
  478. };
  479. for (UINT32 i = 0; i < numSectionHeaders; i++)
  480. {
  481. if (sectionHeaders[i].flags & PE_SECTION_UNINITIALIZED_DATA)
  482. continue;
  483. if (strcmp(sectionHeaders[i].name, ".rsrc") == 0)
  484. {
  485. UINT32 imageSize = sectionHeaders[i].physicalSize;
  486. UINT8* imageData = (UINT8*)bs_stack_alloc(imageSize);
  487. stream.seekg(sectionHeaders[i].physicalAddress);
  488. stream.read((char*)imageData, imageSize);
  489. UINT32 resourceDirOffset = dataDirectory->virtualAddress - sectionHeaders[i].relativeVirtualAddress;
  490. PEImageResourceDirectory* resourceDirectory = (PEImageResourceDirectory*)&imageData[resourceDirOffset];
  491. setIconData(resourceDirectory, resourceDirectory, imageData, sectionHeaders[i].relativeVirtualAddress);
  492. stream.seekp(sectionHeaders[i].physicalAddress);
  493. stream.write((char*)imageData, imageSize);
  494. bs_stack_free(imageData);
  495. }
  496. }
  497. bs_stack_free(sectionHeaders);
  498. stream.close();
  499. }
  500. void EditorApplication::startUp(RenderAPIPlugin renderAPI)
  501. {
  502. CoreApplication::startUp<EditorApplication>(renderAPI);
  503. }
  504. void EditorApplication::preUpdate()
  505. {
  506. Application::preUpdate();
  507. EditorWidgetManager::instance().update();
  508. DropDownWindowManager::instance().update();
  509. }
  510. void EditorApplication::postUpdate()
  511. {
  512. Application::postUpdate();
  513. ProjectLibrary::instance().update();
  514. EditorWindowManager::instance().update();
  515. }
  516. Path EditorApplication::getEditorAssemblyPath() const
  517. {
  518. Path assemblyPath = getBuiltinAssemblyFolder();
  519. assemblyPath.append(toWString(EDITOR_ASSEMBLY) + L".dll");
  520. return assemblyPath;
  521. }
  522. Path EditorApplication::getEditorScriptAssemblyPath() const
  523. {
  524. Path assemblyPath = getScriptAssemblyFolder();
  525. assemblyPath.append(toWString(SCRIPT_EDITOR_ASSEMBLY) + L".dll");
  526. return assemblyPath;
  527. }
  528. Path EditorApplication::getScriptAssemblyFolder() const
  529. {
  530. if (!isProjectLoaded())
  531. return Path::BLANK;
  532. Path assemblyFolder = getProjectPath();
  533. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  534. return assemblyFolder;
  535. }
  536. void EditorApplication::saveProject()
  537. {
  538. if (!isProjectLoaded())
  539. return;
  540. Path buildDataPath = getProjectPath();
  541. buildDataPath.append(BUILD_DATA_PATH);
  542. BuildManager::instance().save(buildDataPath);
  543. saveWidgetLayout(EditorWidgetManager::instance().getLayout());
  544. saveEditorSettings();
  545. saveProjectSettings();
  546. ProjectLibrary::instance().saveLibrary();
  547. }
  548. void EditorApplication::unloadProject()
  549. {
  550. if (!isProjectLoaded())
  551. return;
  552. saveProject();
  553. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  554. BuildManager::instance().clear();
  555. UndoRedo::instance().clear();
  556. EditorWidgetManager::instance().closeAll();
  557. ProjectLibrary::instance().unloadLibrary();
  558. Resources::instance().unloadAllUnused();
  559. gCoreSceneManager().clearScene();
  560. mProjectPath = Path::BLANK;
  561. mProjectName = StringUtil::WBLANK;
  562. mIsProjectLoaded = false;
  563. }
  564. void EditorApplication::loadProject(const Path& projectPath)
  565. {
  566. unloadProject();
  567. mProjectPath = projectPath;
  568. mProjectName = projectPath.getWTail();
  569. mIsProjectLoaded = true;
  570. loadProjectSettings();
  571. Path buildDataPath = getProjectPath();
  572. buildDataPath.append(BUILD_DATA_PATH);
  573. BuildManager::instance().load(buildDataPath);
  574. // Do this before restoring windows and loading library to ensure types are loaded
  575. ScriptManager::instance().reload();
  576. ProjectLibrary::instance().loadLibrary();
  577. EditorWidgetLayoutPtr layout = loadWidgetLayout();
  578. if (layout != nullptr)
  579. EditorWidgetManager::instance().setLayout(layout);
  580. }
  581. void EditorApplication::createProject(const Path& path)
  582. {
  583. Path resourceDir = Path::combine(path, ProjectLibrary::RESOURCES_DIR);
  584. Path internalResourcesDir = Path::combine(path, ProjectLibrary::INTERNAL_RESOURCES_DIR);
  585. if (!FileSystem::exists(resourceDir))
  586. FileSystem::createDir(resourceDir);
  587. if (!FileSystem::exists(internalResourcesDir))
  588. FileSystem::createDir(internalResourcesDir);
  589. Path defaultLayoutPath = FileSystem::getWorkingDirectoryPath();
  590. defaultLayoutPath.append(BuiltinEditorResources::DefaultWidgetLayoutPath);
  591. if (FileSystem::exists(defaultLayoutPath))
  592. {
  593. Path projectLayoutPath = Path::combine(path, WIDGET_LAYOUT_PATH);
  594. FileSystem::copy(defaultLayoutPath, projectLayoutPath, false);
  595. }
  596. }
  597. bool EditorApplication::isValidProjectPath(const Path& path)
  598. {
  599. if (!path.isAbsolute())
  600. return false;
  601. if (!FileSystem::isDirectory(path))
  602. return false;
  603. return true;
  604. }
  605. EditorWidgetLayoutPtr EditorApplication::loadWidgetLayout()
  606. {
  607. Path layoutPath = getProjectPath();
  608. layoutPath.append(WIDGET_LAYOUT_PATH);
  609. if(FileSystem::exists(layoutPath))
  610. {
  611. FileDecoder fs(layoutPath);
  612. return std::static_pointer_cast<EditorWidgetLayout>(fs.decode());
  613. }
  614. return nullptr;
  615. }
  616. void EditorApplication::saveWidgetLayout(const EditorWidgetLayoutPtr& layout)
  617. {
  618. Path layoutPath = getProjectPath();
  619. layoutPath.append(WIDGET_LAYOUT_PATH);
  620. FileEncoder fs(layoutPath);
  621. fs.encode(layout.get());
  622. }
  623. void EditorApplication::loadEditorSettings()
  624. {
  625. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  626. absoluteDataPath.append(EDITOR_SETTINGS_PATH);
  627. if (FileSystem::exists(absoluteDataPath))
  628. {
  629. FileDecoder fs(absoluteDataPath);
  630. mEditorSettings = std::static_pointer_cast<EditorSettings>(fs.decode());
  631. }
  632. if (mEditorSettings == nullptr)
  633. mEditorSettings = bs_shared_ptr_new<EditorSettings>();
  634. }
  635. void EditorApplication::saveEditorSettings()
  636. {
  637. if (mEditorSettings == nullptr)
  638. return;
  639. Path absoluteDataPath = FileSystem::getWorkingDirectoryPath();
  640. absoluteDataPath.append(EDITOR_SETTINGS_PATH);
  641. FileEncoder fs(absoluteDataPath);
  642. fs.encode(mEditorSettings.get());
  643. }
  644. void EditorApplication::loadProjectSettings()
  645. {
  646. if (isProjectLoaded())
  647. {
  648. Path absoluteDataPath = getProjectPath();
  649. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  650. if (FileSystem::exists(absoluteDataPath))
  651. {
  652. FileDecoder fs(absoluteDataPath);
  653. mProjectSettings = std::static_pointer_cast<ProjectSettings>(fs.decode());
  654. }
  655. }
  656. if (mProjectSettings == nullptr)
  657. mProjectSettings = bs_shared_ptr_new<ProjectSettings>();
  658. }
  659. void EditorApplication::saveProjectSettings()
  660. {
  661. if (mProjectSettings == nullptr || !isProjectLoaded())
  662. return;
  663. Path absoluteDataPath = getProjectPath();
  664. absoluteDataPath.append(PROJECT_SETTINGS_PATH);
  665. FileEncoder fs(absoluteDataPath);
  666. fs.encode(mProjectSettings.get());
  667. }
  668. ShaderIncludeHandlerPtr EditorApplication::getShaderIncludeHandler() const
  669. {
  670. return bs_shared_ptr_new<EditorShaderIncludeHandler>();
  671. }
  672. EditorApplication& gEditorApplication()
  673. {
  674. return static_cast<EditorApplication&>(EditorApplication::instance());
  675. }
  676. }