importers.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /******************************************************************************
  2. Copyright (C) 2019-2020 by Dillon Pentz <[email protected]>
  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,
  37. json11::Json &res) = 0;
  38. virtual bool Check(const std::string &path) = 0;
  39. virtual std::string Name(const std::string &path) = 0;
  40. virtual OBSImporterFiles FindFiles()
  41. {
  42. OBSImporterFiles f;
  43. return f;
  44. };
  45. };
  46. class ClassicImporter : public Importer {
  47. public:
  48. std::string Prog() { return "OBSClassic"; };
  49. int ImportScenes(const std::string &path, std::string &name,
  50. json11::Json &res);
  51. bool Check(const std::string &path);
  52. std::string Name(const std::string &path);
  53. OBSImporterFiles FindFiles();
  54. };
  55. class StudioImporter : public Importer {
  56. public:
  57. std::string Prog() { return "OBSStudio"; };
  58. int ImportScenes(const std::string &path, std::string &name,
  59. json11::Json &res);
  60. bool Check(const std::string &path);
  61. std::string Name(const std::string &path);
  62. };
  63. class SLImporter : public Importer {
  64. public:
  65. std::string Prog() { return "Streamlabs"; };
  66. int ImportScenes(const std::string &path, std::string &name,
  67. json11::Json &res);
  68. bool Check(const std::string &path);
  69. std::string Name(const std::string &path);
  70. OBSImporterFiles FindFiles();
  71. };
  72. class XSplitImporter : public Importer {
  73. public:
  74. std::string Prog() { return "XSplitBroadcaster"; };
  75. int ImportScenes(const std::string &path, std::string &name,
  76. json11::Json &res);
  77. bool Check(const std::string &path);
  78. std::string Name(const std::string &) { return "XSplit Import"; };
  79. OBSImporterFiles FindFiles();
  80. };
  81. void ImportersInit();
  82. std::string DetectProgram(const std::string &path);
  83. std::string GetSCName(const std::string &path, const std::string &prog);
  84. int ImportSCFromProg(const std::string &path, std::string &name,
  85. const std::string &program, json11::Json &res);
  86. int ImportSC(const std::string &path, std::string &name, json11::Json &res);
  87. OBSImporterFiles ImportersFindFiles();
  88. void TranslateOSStudio(json11::Json &data);
  89. void TranslatePaths(json11::Json &data, const std::string &rootDir);
  90. static inline std::string GetFilenameFromPath(const std::string &path)
  91. {
  92. #ifdef _WIN32
  93. size_t pos = path.find_last_of('\\');
  94. if (pos == -1 || pos < path.find_last_of('/'))
  95. pos = path.find_last_of('/');
  96. #else
  97. size_t pos = path.find_last_of('/');
  98. #endif
  99. size_t ext = path.find_last_of('.');
  100. if (ext < pos) {
  101. return path.substr(pos + 1);
  102. } else {
  103. return path.substr(pos + 1, ext - pos - 1);
  104. }
  105. }
  106. static inline std::string GetFolderFromPath(const std::string &path)
  107. {
  108. #ifdef _WIN32
  109. size_t pos = path.find_last_of('\\');
  110. if (pos == -1 || pos < path.find_last_of('/'))
  111. pos = path.find_last_of('/');
  112. #else
  113. size_t pos = path.find_last_of('/');
  114. #endif
  115. return path.substr(0, pos + 1);
  116. }
  117. static inline std::string StringReplace(const std::string &in,
  118. const std::string &search,
  119. const std::string &rep)
  120. {
  121. std::string res = in;
  122. size_t pos;
  123. while ((pos = res.find(search)) != std::string::npos) {
  124. res.replace(pos, search.length(), rep);
  125. }
  126. return res;
  127. }
  128. static inline std::string ReadLine(std::string &str)
  129. {
  130. str = StringReplace(str, "\r\n", "\n");
  131. size_t pos = str.find('\n');
  132. if (pos == std::string::npos)
  133. pos = str.find(EOF);
  134. if (pos == std::string::npos)
  135. pos = str.find('\0');
  136. if (pos == std::string::npos)
  137. return "";
  138. std::string res = str.substr(0, pos);
  139. str = str.substr(pos + 1);
  140. return res;
  141. }