BuildWeb.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include <Atomic/Core/StringUtils.h>
  23. #include <Atomic/IO/FileSystem.h>
  24. #include <Atomic/IO/File.h>
  25. #include <Atomic/Resource/ResourceCache.h>
  26. #include "../ToolSystem.h"
  27. #include "../ToolEnvironment.h"
  28. #include "../Project/Project.h"
  29. #include "../Assets/AssetDatabase.h"
  30. #include "BuildEvents.h"
  31. #include "BuildSystem.h"
  32. #include "BuildWeb.h"
  33. namespace ToolCore
  34. {
  35. BuildWeb::BuildWeb(Context* context, Project* project) : BuildBase(context, project, PLATFORMID_WEB)
  36. {
  37. }
  38. BuildWeb::~BuildWeb()
  39. {
  40. }
  41. void BuildWeb::Initialize()
  42. {
  43. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  44. Project* project = tsystem->GetProject();
  45. Vector<String> defaultResourcePaths;
  46. GetDefaultResourcePaths(defaultResourcePaths);
  47. for (unsigned i = 0; i < defaultResourcePaths.Size(); i++)
  48. {
  49. AddResourceDir(defaultResourcePaths[i]);
  50. }
  51. BuildDefaultResourceEntries();
  52. // TODO: smart filtering of cache
  53. String projectResources = project->GetResourcePath();
  54. AddProjectResourceDir(projectResources);
  55. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  56. String cachePath = db->GetCachePath();
  57. AddProjectResourceDir(cachePath);
  58. BuildProjectResourceEntries();
  59. }
  60. void BuildWeb::Build(const String& buildPath)
  61. {
  62. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  63. buildPath_ = AddTrailingSlash(buildPath) + GetBuildSubfolder();
  64. VariantMap buildOutput;
  65. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Starting Web Deployment</color>\n\n";
  66. SendEvent(E_BUILDOUTPUT, buildOutput);
  67. Initialize();
  68. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  69. if (fileSystem->DirExists(buildPath_))
  70. fileSystem->RemoveDir(buildPath_, true);
  71. String dataPath = tenv->GetToolDataDir();
  72. String buildSourceDir = dataPath + "Deployment/Web";
  73. fileSystem->CreateDir(buildPath_);
  74. String resourcePackagePath = buildPath_ + "/AtomicResources.data";
  75. GenerateResourcePackage(resourcePackagePath);
  76. fileSystem->Copy(buildSourceDir + "/AtomicPlayer.html", buildPath_ + "/AtomicPlayer.html");
  77. fileSystem->Copy(buildSourceDir + "/AtomicPlayer.html.mem", buildPath_ + "/AtomicPlayer.html.mem");
  78. fileSystem->Copy(buildSourceDir + "/AtomicPlayer.js", buildPath_ + "/AtomicPlayer.js");
  79. fileSystem->Copy(buildSourceDir + "/AtomicLoader.js", buildPath_ + "/AtomicLoader.js");
  80. fileSystem->Copy(buildSourceDir + "/index.html", buildPath_ + "/index.html");
  81. fileSystem->Copy(buildSourceDir + "/Atomic_Logo_Header.png", buildPath_ + "/Atomic_Logo_Header.png");
  82. File file(context_, buildSourceDir + "/AtomicResources_js.template", FILE_READ);
  83. unsigned size = file.GetSize();
  84. SharedArrayPtr<char> buffer(new char[size + 1]);
  85. file.Read(buffer.Get(), size);
  86. buffer[size] = '\0';
  87. String resourcejs = (const char*) buffer.Get();
  88. file.Close();
  89. file.Open(buildPath_ + "/AtomicResources.data", FILE_READ);
  90. int rsize = (int) file.GetSize();
  91. file.Close();
  92. String request;
  93. request.AppendWithFormat("new DataRequest(0, %i, 0, 0).open('GET', '/AtomicResources%s');", rsize, PAK_EXTENSION);
  94. resourcejs.Replace("$$REMOTE_PACKAGE_SIZE$$", ToString("%i", rsize));
  95. resourcejs.Replace("$$ATOMIC_RESOURCES_DATA_REQUEST$$", request);
  96. file.Open(buildPath_ + "/AtomicResources.js", FILE_WRITE);
  97. file.Write(resourcejs.CString(), resourcejs.Length());
  98. file.Close();
  99. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Web Deployment Complete</color>\n\n";
  100. SendEvent(E_BUILDOUTPUT, buildOutput);
  101. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  102. buildSystem->BuildComplete(PLATFORMID_WEB, buildPath_);
  103. //fileSystem->SystemCommandAsync("/Applications/Firefox.app/Contents/MacOS/firefox");
  104. }
  105. }