ScriptCompiler.cpp 5.8 KB

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