JSBind.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/ProcessUtils.h>
  23. #include <Atomic/IO/FileSystem.h>
  24. #include "JSBPackage.h"
  25. #include "JSBind.h"
  26. #include "JavaScript/JSPackageWriter.h"
  27. #include "CSharp/CSPackageWriter.h"
  28. namespace ToolCore
  29. {
  30. JSBind::JSBind(Context* context) : Object(context),
  31. package_(new JSBPackage(context))
  32. {
  33. }
  34. JSBind::~JSBind()
  35. {
  36. }
  37. bool JSBind::LoadPackage(const String& sourceRootFolder, const String& packageFolder, const String& platform)
  38. {
  39. sourceRootFolder_ = sourceRootFolder;
  40. packageFolder_ = packageFolder;
  41. platform_ = platform;
  42. package_->Load(sourceRootFolder_ + packageFolder_);
  43. return true;
  44. }
  45. bool JSBind::GenerateCSharpBindings()
  46. {
  47. String modulesFolder = "Artifacts/Build/Source/Generated/" + platform_ + "/CSharp/Packages/";
  48. modulesFolder += package_->GetName() + "/";
  49. String nativeOutputFolder = sourceRootFolder_ + "/" + modulesFolder + "Native/";
  50. String managedOutputFolder = sourceRootFolder_ + "/" + modulesFolder + "Managed/";
  51. FileSystem* fs = GetSubsystem<FileSystem>();
  52. if (!fs->CreateDirs(sourceRootFolder_, modulesFolder + "Native/") || !fs->DirExists(nativeOutputFolder))
  53. {
  54. String error = "Unable to create bindings native output folder: " + nativeOutputFolder;
  55. ErrorExit(error.CString());
  56. }
  57. if (!fs->CreateDirs(sourceRootFolder_, modulesFolder + "Managed/") || !fs->DirExists(managedOutputFolder))
  58. {
  59. String error = "Unable to create bindings managed output folder: " + managedOutputFolder;
  60. ErrorExit(error.CString());
  61. }
  62. destScriptFolder_ = managedOutputFolder;
  63. destNativeFolder_ = nativeOutputFolder;
  64. CSPackageWriter writer(package_);
  65. package_->GenerateSource(writer);
  66. return true;
  67. }
  68. bool JSBind::GenerateJavaScriptBindings()
  69. {
  70. String modulesFolder = "Artifacts/Build/Source/Generated/" + platform_ + "/Javascript/Packages/";
  71. modulesFolder += package_->GetName() + "/";
  72. String outputFolder = sourceRootFolder_ + "/" + modulesFolder;
  73. FileSystem* fs = GetSubsystem<FileSystem>();
  74. if (!fs->CreateDirs(sourceRootFolder_, modulesFolder) || !fs->DirExists(outputFolder))
  75. {
  76. String error = "Unable to create bindings output folder: " + outputFolder;
  77. ErrorExit(error.CString());
  78. }
  79. destScriptFolder_ = String::EMPTY;
  80. destNativeFolder_ = outputFolder;
  81. JSPackageWriter writer(package_);
  82. package_->GenerateSource(writer);
  83. return true;
  84. }
  85. }