ScriptCompiler.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Copyright (c) 2008-2013 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 WIN32
  32. #include <windows.h>
  33. #endif
  34. #include "DebugNew.h"
  35. using namespace Urho3D;
  36. void CompileScript(Context* context, const String& fileName);
  37. int main(int argc, char** argv)
  38. {
  39. #ifdef WIN32
  40. const Vector<String>& arguments = ParseArguments(GetCommandLineW());
  41. #else
  42. const Vector<String>& arguments = ParseArguments(argc, argv);
  43. #endif
  44. bool dumpApiMode = false;
  45. String outputFile;
  46. if (arguments.Size() < 1)
  47. ErrorExit("Usage: ScriptCompiler <input file> [resource path for includes]\n"
  48. " ScriptCompiler -dumpapi <output file>");
  49. else
  50. {
  51. if (arguments[0] != "-dumpapi")
  52. outputFile = arguments[0];
  53. else
  54. {
  55. dumpApiMode = true;
  56. if (arguments.Size() > 1)
  57. outputFile = arguments[1];
  58. }
  59. }
  60. SharedPtr<Context> context(new Context());
  61. // Note: creating the Engine registers most subsystems which don't require engine initialization
  62. SharedPtr<Engine> engine(new Engine(context));
  63. context->RegisterSubsystem(new Script(context));
  64. Log* log = context->GetSubsystem<Log>();
  65. // Register Log subsystem manually if compiled without logging support
  66. if (!log)
  67. {
  68. context->RegisterSubsystem(new Log(context));
  69. log = context->GetSubsystem<Log>();
  70. }
  71. log->SetLevel(LOG_WARNING);
  72. log->SetTimeStamp(false);
  73. if (!dumpApiMode)
  74. {
  75. String path, file, extension;
  76. SplitPath(outputFile, path, file, extension);
  77. ResourceCache* cache = context->GetSubsystem<ResourceCache>();
  78. // Add resource path to be able to resolve includes
  79. if (arguments.Size() > 1)
  80. cache->AddResourceDir(arguments[1]);
  81. else
  82. cache->AddResourceDir(cache->GetPreferredResourceDir(path));
  83. if (!file.StartsWith("*"))
  84. CompileScript(context, outputFile);
  85. else
  86. {
  87. Vector<String> scriptFiles;
  88. context->GetSubsystem<FileSystem>()->ScanDir(scriptFiles, path, file + extension, SCAN_FILES, false);
  89. for (unsigned i = 0; i < scriptFiles.Size(); ++i)
  90. CompileScript(context, path + scriptFiles[i]);
  91. }
  92. }
  93. else
  94. {
  95. if (!outputFile.Empty())
  96. {
  97. log->SetQuiet(true);
  98. log->Open(outputFile);
  99. }
  100. // If without output file, dump to stdout instead
  101. context->GetSubsystem<Script>()->DumpAPI();
  102. }
  103. return EXIT_SUCCESS;
  104. }
  105. void CompileScript(Context* context, const String& fileName)
  106. {
  107. PrintLine("Compiling script file " + fileName);
  108. File inFile(context, fileName, FILE_READ);
  109. if (!inFile.IsOpen())
  110. ErrorExit("Failed to open script file " + fileName);
  111. ScriptFile script(context);
  112. if (!script.Load(inFile))
  113. ErrorExit();
  114. String outFileName = ReplaceExtension(fileName, ".asc");
  115. File outFile(context, outFileName, FILE_WRITE);
  116. if (!outFile.IsOpen())
  117. ErrorExit("Failed to open output file " + fileName);
  118. script.SaveByteCode(outFile);
  119. }