ScriptCompiler.cpp 5.2 KB

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