importers.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /******************************************************************************
  2. Copyright (C) 2019-2020 by Dillon Pentz <dillon@vodbox.io>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include "obs.hpp"
  16. #include "json11.hpp"
  17. #include <util/platform.h>
  18. #include <util/util.hpp>
  19. #include <string>
  20. #include <vector>
  21. #include <QDir>
  22. enum obs_importer_responses {
  23. IMPORTER_SUCCESS,
  24. IMPORTER_FILE_NOT_FOUND,
  25. IMPORTER_FILE_NOT_RECOGNISED,
  26. IMPORTER_FILE_WONT_OPEN,
  27. IMPORTER_ERROR_DURING_CONVERSION,
  28. IMPORTER_UNKNOWN_ERROR,
  29. IMPORTER_NOT_FOUND
  30. };
  31. typedef std::vector<std::string> OBSImporterFiles;
  32. class Importer {
  33. public:
  34. virtual ~Importer() {}
  35. virtual std::string Prog() { return "Null"; };
  36. virtual int ImportScenes(const std::string &path, std::string &name, json11::Json &res) = 0;
  37. virtual bool Check(const std::string &path) = 0;
  38. virtual std::string Name(const std::string &path) = 0;
  39. virtual OBSImporterFiles FindFiles()
  40. {
  41. OBSImporterFiles f;
  42. return f;
  43. };
  44. };
  45. class ClassicImporter : public Importer {
  46. public:
  47. std::string Prog() { return "OBSClassic"; };
  48. int ImportScenes(const std::string &path, std::string &name, json11::Json &res);
  49. bool Check(const std::string &path);
  50. std::string Name(const std::string &path);
  51. OBSImporterFiles FindFiles();
  52. };
  53. class StudioImporter : public Importer {
  54. public:
  55. std::string Prog() { return "OBSStudio"; };
  56. int ImportScenes(const std::string &path, std::string &name, json11::Json &res);
  57. bool Check(const std::string &path);
  58. std::string Name(const std::string &path);
  59. };
  60. class SLImporter : public Importer {
  61. public:
  62. std::string Prog() { return "Streamlabs"; };
  63. int ImportScenes(const std::string &path, std::string &name, json11::Json &res);
  64. bool Check(const std::string &path);
  65. std::string Name(const std::string &path);
  66. OBSImporterFiles FindFiles();
  67. };
  68. class XSplitImporter : public Importer {
  69. public:
  70. std::string Prog() { return "XSplitBroadcaster"; };
  71. int ImportScenes(const std::string &path, std::string &name, json11::Json &res);
  72. bool Check(const std::string &path);
  73. std::string Name(const std::string &) { return "XSplit Import"; };
  74. OBSImporterFiles FindFiles();
  75. };
  76. void ImportersInit();
  77. std::string DetectProgram(const std::string &path);
  78. std::string GetSCName(const std::string &path, const std::string &prog);
  79. int ImportSCFromProg(const std::string &path, std::string &name, const std::string &program, json11::Json &res);
  80. int ImportSC(const std::string &path, std::string &name, json11::Json &res);
  81. OBSImporterFiles ImportersFindFiles();
  82. void TranslateOSStudio(json11::Json &data);
  83. void TranslatePaths(json11::Json &data, const std::string &rootDir);
  84. static inline std::string GetFilenameFromPath(const std::string &path)
  85. {
  86. #ifdef _WIN32
  87. size_t pos = path.find_last_of('\\');
  88. if (pos == -1 || pos < path.find_last_of('/'))
  89. pos = path.find_last_of('/');
  90. #else
  91. size_t pos = path.find_last_of('/');
  92. #endif
  93. size_t ext = path.find_last_of('.');
  94. if (ext < pos) {
  95. return path.substr(pos + 1);
  96. } else {
  97. return path.substr(pos + 1, ext - pos - 1);
  98. }
  99. }
  100. static inline std::string GetFolderFromPath(const std::string &path)
  101. {
  102. #ifdef _WIN32
  103. size_t pos = path.find_last_of('\\');
  104. if (pos == -1 || pos < path.find_last_of('/'))
  105. pos = path.find_last_of('/');
  106. #else
  107. size_t pos = path.find_last_of('/');
  108. #endif
  109. return path.substr(0, pos + 1);
  110. }
  111. static inline std::string StringReplace(const std::string &in, const std::string &search, const std::string &rep)
  112. {
  113. std::string res = in;
  114. size_t pos;
  115. while ((pos = res.find(search)) != std::string::npos) {
  116. res.replace(pos, search.length(), rep);
  117. }
  118. return res;
  119. }
  120. static inline std::string ReadLine(std::string &str)
  121. {
  122. str = StringReplace(str, "\r\n", "\n");
  123. size_t pos = str.find('\n');
  124. if (pos == std::string::npos)
  125. pos = str.find(EOF);
  126. if (pos == std::string::npos)
  127. pos = str.find('\0');
  128. if (pos == std::string::npos)
  129. return "";
  130. std::string res = str.substr(0, pos);
  131. str = str.substr(pos + 1);
  132. return res;
  133. }