ScriptCompiler.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  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 <Urho3D/AngelScript/Script.h>
  23. #include <Urho3D/AngelScript/ScriptFile.h>
  24. #include <Urho3D/Core/Context.h>
  25. #include <Urho3D/Core/ProcessUtils.h>
  26. #include <Urho3D/Engine/Engine.h>
  27. #include <Urho3D/Engine/EngineDefs.h>
  28. #include <Urho3D/IO/File.h>
  29. #include <Urho3D/IO/FileSystem.h>
  30. #include <Urho3D/IO/Log.h>
  31. #include <Urho3D/Resource/ResourceCache.h>
  32. #ifdef URHO3D_LUA
  33. #include <Urho3D/LuaScript/LuaScript.h>
  34. #endif
  35. #ifdef WIN32
  36. #include <windows.h>
  37. #endif
  38. #include <Urho3D/DebugNew.h>
  39. using namespace Urho3D;
  40. void CompileScript(Context* context, const String& fileName);
  41. int main(int argc, char** argv)
  42. {
  43. #ifdef WIN32
  44. const Vector<String>& arguments = ParseArguments(GetCommandLineW());
  45. #else
  46. const Vector<String>& arguments = ParseArguments(argc, argv);
  47. #endif
  48. bool dumpApiMode = false;
  49. String sourceTree;
  50. String outputFile;
  51. if (arguments.Size() < 1)
  52. ErrorExit("Usage: ScriptCompiler <input file> [resource path for includes]\n"
  53. " ScriptCompiler -dumpapi <source tree> <Doxygen output file> [C header output file]");
  54. else
  55. {
  56. if (arguments[0] != "-dumpapi")
  57. outputFile = arguments[0];
  58. else
  59. {
  60. dumpApiMode = true;
  61. if (arguments.Size() > 2)
  62. {
  63. sourceTree = arguments[1];
  64. outputFile = arguments[2];
  65. }
  66. else
  67. ErrorExit("Usage: ScriptCompiler -dumpapi <source tree> <Doxygen output file> [C header output file]");
  68. }
  69. }
  70. SharedPtr<Context> context(new Context());
  71. SharedPtr<Engine> engine(new Engine(context));
  72. context->RegisterSubsystem(new Script(context));
  73. // In API dumping mode initialize the engine and instantiate LuaScript system if available so that we
  74. // can dump attributes from as many classes as possible
  75. if (dumpApiMode)
  76. {
  77. VariantMap engineParameters;
  78. engineParameters[EP_HEADLESS] = true;
  79. engineParameters[EP_WORKER_THREADS] = false;
  80. engineParameters[EP_LOG_NAME] = String::EMPTY;
  81. engineParameters[EP_RESOURCE_PATHS] = String::EMPTY;
  82. engineParameters[EP_AUTOLOAD_PATHS] = String::EMPTY;
  83. engine->Initialize(engineParameters);
  84. #ifdef URHO3D_LUA
  85. context->RegisterSubsystem(new LuaScript(context));
  86. #endif
  87. }
  88. auto* log = context->GetSubsystem<Log>();
  89. // Register Log subsystem manually if compiled without logging support
  90. if (!log)
  91. {
  92. context->RegisterSubsystem(new Log(context));
  93. log = context->GetSubsystem<Log>();
  94. }
  95. log->SetLevel(LOG_WARNING);
  96. log->SetTimeStamp(false);
  97. if (!dumpApiMode)
  98. {
  99. String path, file, extension;
  100. SplitPath(outputFile, path, file, extension);
  101. auto* cache = context->GetSubsystem<ResourceCache>();
  102. // Add resource path to be able to resolve includes
  103. if (arguments.Size() > 1)
  104. cache->AddResourceDir(arguments[1]);
  105. else
  106. cache->AddResourceDir(cache->GetPreferredResourceDir(path));
  107. if (!file.StartsWith("*"))
  108. CompileScript(context, outputFile);
  109. else
  110. {
  111. Vector<String> scriptFiles;
  112. context->GetSubsystem<FileSystem>()->ScanDir(scriptFiles, path, file + extension, SCAN_FILES, false);
  113. for (unsigned i = 0; i < scriptFiles.Size(); ++i)
  114. CompileScript(context, path + scriptFiles[i]);
  115. }
  116. }
  117. else
  118. {
  119. if (!outputFile.Empty())
  120. {
  121. log->SetQuiet(true);
  122. log->Open(outputFile);
  123. }
  124. // If without output file, dump to stdout instead
  125. context->GetSubsystem<Script>()->DumpAPI(DOXYGEN, sourceTree);
  126. // Only dump API as C Header when an output file name is explicitly given
  127. if (arguments.Size() > 3)
  128. {
  129. outputFile = arguments[3];
  130. log->Open(outputFile);
  131. context->GetSubsystem<Script>()->DumpAPI(C_HEADER, sourceTree);
  132. }
  133. }
  134. return EXIT_SUCCESS;
  135. }
  136. void CompileScript(Context* context, const String& fileName)
  137. {
  138. PrintLine("Compiling script file " + fileName);
  139. File inFile(context, fileName, FILE_READ);
  140. if (!inFile.IsOpen())
  141. ErrorExit("Failed to open script file " + fileName);
  142. ScriptFile script(context);
  143. if (!script.Load(inFile))
  144. ErrorExit();
  145. String outFileName = ReplaceExtension(fileName, ".asc");
  146. File outFile(context, outFileName, FILE_WRITE);
  147. if (!outFile.IsOpen())
  148. ErrorExit("Failed to open output file " + fileName);
  149. script.SaveByteCode(outFile);
  150. }