ScriptCompiler.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. SharedPtr<Engine> engine(new Engine(context));
  62. context->RegisterSubsystem(new Script(context));
  63. context->RegisterSubsystem(new FileSystem(context));
  64. context->RegisterSubsystem(new ResourceCache(context));
  65. context->RegisterSubsystem(new Log(context));
  66. Log* log = context->GetSubsystem<Log>();
  67. log->SetLevel(LOG_WARNING);
  68. log->SetTimeStamp(false);
  69. if (!dumpApiMode)
  70. {
  71. String path, file, extension;
  72. SplitPath(outputFile, path, file, extension);
  73. ResourceCache* cache = context->GetSubsystem<ResourceCache>();
  74. // Add resource path to be able to resolve includes
  75. if (arguments.Size() > 1)
  76. cache->AddResourceDir(arguments[1]);
  77. else
  78. cache->AddResourceDir(cache->GetPreferredResourceDir(path));
  79. if (!file.StartsWith("*"))
  80. CompileScript(context, outputFile);
  81. else
  82. {
  83. Vector<String> scriptFiles;
  84. context->GetSubsystem<FileSystem>()->ScanDir(scriptFiles, path, file + extension, SCAN_FILES, false);
  85. for (unsigned i = 0; i < scriptFiles.Size(); ++i)
  86. CompileScript(context, path + scriptFiles[i]);
  87. }
  88. }
  89. else
  90. {
  91. if (!outputFile.Empty())
  92. {
  93. log->SetQuiet(true);
  94. log->Open(outputFile);
  95. }
  96. // If without output file, dump to stdout instead
  97. context->GetSubsystem<Script>()->DumpAPI();
  98. }
  99. return EXIT_SUCCESS;
  100. }
  101. void CompileScript(Context* context, const String& fileName)
  102. {
  103. PrintLine("Compiling script file " + fileName);
  104. File inFile(context, fileName, FILE_READ);
  105. if (!inFile.IsOpen())
  106. ErrorExit("Failed to open script file " + fileName);
  107. ScriptFile script(context);
  108. if (!script.Load(inFile))
  109. ErrorExit();
  110. String outFileName = ReplaceExtension(fileName, ".asc");
  111. File outFile(context, outFileName, FILE_WRITE);
  112. if (!outFile.IsOpen())
  113. ErrorExit("Failed to open output file " + fileName);
  114. script.SaveByteCode(outFile);
  115. }