ScriptCompiler.cpp 4.5 KB

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