CSBModuleWriter.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/IO/FileSystem.h>
  8. #include "../JSBind.h"
  9. #include "../JSBPackage.h"
  10. #include "../JSBModule.h"
  11. #include "../JSBEnum.h"
  12. #include "../JSBClass.h"
  13. #include "../JSBFunction.h"
  14. #include "CSBClassWriter.h"
  15. #include "CSBModuleWriter.h"
  16. namespace ToolCore
  17. {
  18. CSBModuleWriter::CSBModuleWriter(JSBModule *module) : module_(module)
  19. {
  20. }
  21. void CSBModuleWriter::WriteIncludes(String& source)
  22. {
  23. Vector<String>& includes = module_->includes_;
  24. for (unsigned i = 0; i < includes.Size(); i++)
  25. {
  26. if (includes[i].StartsWith("<"))
  27. source.AppendWithFormat("#include %s\n", includes[i].CString());
  28. else
  29. source.AppendWithFormat("#include \"%s\"\n", includes[i].CString());
  30. }
  31. Vector<JSBHeader*> allheaders;
  32. HashMap<StringHash, SharedPtr<JSBEnum> >::Iterator eitr = module_->enums_.Begin();
  33. while (eitr != module_->enums_.End())
  34. {
  35. allheaders.Push(eitr->second_->GetHeader());
  36. eitr++;
  37. }
  38. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator citr = module_->classes_.Begin();
  39. while (citr != module_->classes_.End())
  40. {
  41. allheaders.Push(citr->second_->GetHeader());
  42. citr++;
  43. }
  44. Vector<JSBHeader*> included;
  45. for (unsigned i = 0; i < allheaders.Size(); i++)
  46. {
  47. JSBHeader* header = allheaders.At(i);
  48. if (included.Contains(header))
  49. continue;
  50. String headerPath = GetPath(header->GetFilePath());
  51. String headerfile = GetFileNameAndExtension(header->GetFilePath());
  52. JSBind* jsbind = header->GetSubsystem<JSBind>();
  53. headerPath.Replace(jsbind->GetSourceRootFolder() + "Source/", "");
  54. source.AppendWithFormat("#include <%s%s>\n", headerPath.CString(), headerfile.CString());
  55. included.Push(header);
  56. }
  57. }
  58. void CSBModuleWriter::GenerateSource(String& sourceOut)
  59. {
  60. source_ = "// This file was autogenerated by JSBind, changes will be lost\n";
  61. source_ += "#ifdef ATOMIC_PLATFORM_WINDOWS\n";
  62. source_ += "#pragma warning(disable: 4244) // possible loss of data\n";
  63. source_ += "#endif\n";
  64. if (module_->Requires("3D"))
  65. {
  66. source_ += "#ifdef ATOMIC_3D\n";
  67. }
  68. source_ += "#include <Duktape/duktape.h>\n";
  69. source_ += "#include <AtomicJS/Javascript/JSVM.h>\n";
  70. source_ += "#include <AtomicJS/Javascript/JSAPI.h>\n";
  71. WriteIncludes(source_);
  72. String ns = module_->GetPackage()->GetNamespace();
  73. if (ns != "Atomic")
  74. {
  75. source_ += "\n\nusing namespace " + ns + ";\n\n";
  76. }
  77. source_ += "\n\nnamespace Atomic\n{\n \n";
  78. source_ += "// Begin Class Declarations\n";
  79. source_ += "// End Class Declarations\n\n";
  80. source_ += "// Begin Classes\n";
  81. Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
  82. for (unsigned i = 0; i < classes.Size(); i++)
  83. {
  84. CSBClassWriter clsWriter(classes[i]);
  85. clsWriter.GenerateSource(source_);
  86. }
  87. source_ += "// End Classes\n\n";
  88. // end Atomic namespace
  89. source_ += "\n}\n";
  90. if (module_->Requires("3D"))
  91. {
  92. source_ += "#endif //ATOMIC_3D\n";
  93. }
  94. sourceOut = source_;
  95. }
  96. }