ScriptCompiler.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "ScriptFile.h"
  30. #ifdef WIN32
  31. #include <windows.h>
  32. #endif
  33. #include "DebugNew.h"
  34. using namespace Urho3D;
  35. void CompileScript(Context* context, const String& fileName);
  36. int main(int argc, char** argv)
  37. {
  38. #ifdef WIN32
  39. const Vector<String>& arguments = ParseArguments(GetCommandLineW());
  40. #else
  41. const Vector<String>& arguments = ParseArguments(argc, argv);
  42. #endif
  43. String outputFile;
  44. if (arguments.Size() < 1)
  45. ErrorExit("Usage: ScriptCompiler <input file> [resource path for includes]");
  46. else
  47. outputFile = arguments[0];
  48. SharedPtr<Context> context(new Context());
  49. SharedPtr<Engine> engine(new Engine(context));
  50. context->RegisterSubsystem(new FileSystem(context));
  51. context->RegisterSubsystem(new ResourceCache(context));
  52. context->RegisterSubsystem(new Log(context));
  53. Log* log = context->GetSubsystem<Log>();
  54. log->SetLevel(LOG_WARNING);
  55. log->SetTimeStamp(false);
  56. if (!engine->InitializeScripting())
  57. ErrorExit("Unable to initialize script engine. The application will now exit.");
  58. String path, file, extension;
  59. SplitPath(outputFile, path, file, extension);
  60. ResourceCache* cache = context->GetSubsystem<ResourceCache>();
  61. // Add resource path to be able to resolve includes
  62. if (arguments.Size() > 1)
  63. cache->AddResourceDir(arguments[1]);
  64. else
  65. cache->AddResourceDir(cache->GetPreferredResourceDir(path));
  66. if (!file.StartsWith("*"))
  67. CompileScript(context, outputFile);
  68. else
  69. {
  70. Vector<String> scriptFiles;
  71. context->GetSubsystem<FileSystem>()->ScanDir(scriptFiles, path, file + extension, SCAN_FILES, false);
  72. for (unsigned i = 0; i < scriptFiles.Size(); ++i)
  73. CompileScript(context, path + scriptFiles[i]);
  74. }
  75. return EXIT_SUCCESS;
  76. }
  77. void CompileScript(Context* context, const String& fileName)
  78. {
  79. PrintLine("Compiling script file " + fileName);
  80. File inFile(context, fileName, FILE_READ);
  81. if (!inFile.IsOpen())
  82. ErrorExit("Failed to open script file " + fileName);
  83. ScriptFile script(context);
  84. if (!script.Load(inFile))
  85. ErrorExit("Failed to compile script file " + fileName + ": " + context->GetSubsystem<Log>()->GetLastMessage());
  86. String outFileName = ReplaceExtension(fileName, ".asc");
  87. File outFile(context, outFileName, FILE_WRITE);
  88. if (!outFile.IsOpen())
  89. ErrorExit("Failed to open output file " + fileName);
  90. script.SaveByteCode(outFile);
  91. }