SubprocessSystem.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  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 <Atomic/Core/CoreEvents.h>
  23. #include <Atomic/Core/Context.h>
  24. #include <Atomic/IO/FileSystem.h>
  25. #include "SubprocessSystem.h"
  26. namespace ToolCore
  27. {
  28. SubprocessSystem::SubprocessSystem(Context* context) :
  29. Object(context),
  30. updateTimer_(0.0f)
  31. {
  32. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(SubprocessSystem, HandleUpdate));
  33. }
  34. SubprocessSystem::~SubprocessSystem()
  35. {
  36. for (unsigned i = 0; i < processes_.Size(); i++)
  37. processes_[i]->Stop();
  38. processes_.Clear();
  39. }
  40. Subprocess* SubprocessSystem::Launch(const String& command, const Vector<String>& args, const String& initialDirectory)
  41. {
  42. Poco::Process::Env env;
  43. return Launch(command, args, initialDirectory, env);
  44. }
  45. Subprocess* SubprocessSystem::Launch(const String& command, const Vector<String>& args, const String& initialDirectory, const Poco::Process::Env& env)
  46. {
  47. SharedPtr<Subprocess> process(new Subprocess(context_));
  48. if (process->Launch(GetNativePath(command), args, GetNativePath(initialDirectory), env))
  49. {
  50. processes_.Push(process);
  51. return process;
  52. }
  53. return 0;
  54. }
  55. void SubprocessSystem::HandleUpdate(StringHash eventType, VariantMap& eventData)
  56. {
  57. Vector<Subprocess*> remove;
  58. for (unsigned i = 0; i < processes_.Size(); i++)
  59. {
  60. Subprocess* process = processes_[i];
  61. if (!process->Update(this))
  62. {
  63. remove.Push(process);
  64. }
  65. }
  66. for (unsigned i = 0; i < remove.Size(); i++)
  67. {
  68. processes_.Remove(SharedPtr<Subprocess>(remove[i]));
  69. }
  70. }
  71. }