importers.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 &path)
  79. {
  80. return "XSplit Import";
  81. UNUSED_PARAMETER(path);
  82. };
  83. OBSImporterFiles FindFiles();
  84. };
  85. void ImportersInit();
  86. std::string DetectProgram(const std::string &path);
  87. std::string GetSCName(const std::string &path, const std::string &prog);
  88. int ImportSCFromProg(const std::string &path, std::string &name,
  89. const std::string &program, json11::Json &res);
  90. int ImportSC(const std::string &path, std::string &name, json11::Json &res);
  91. OBSImporterFiles ImportersFindFiles();
  92. void TranslateOSStudio(json11::Json &data);
  93. void TranslatePaths(json11::Json &data, const std::string &rootDir);
  94. static inline std::string GetFilenameFromPath(const std::string &path)
  95. {
  96. #ifdef _WIN32
  97. size_t pos = path.find_last_of('\\');
  98. if (pos == -1 || pos < path.find_last_of('/'))
  99. pos = path.find_last_of('/');
  100. #else
  101. size_t pos = path.find_last_of('/');
  102. #endif
  103. size_t ext = path.find_last_of('.');
  104. if (ext < pos) {
  105. return path.substr(pos + 1);
  106. } else {
  107. return path.substr(pos + 1, ext - pos - 1);
  108. }
  109. }
  110. static inline std::string GetFolderFromPath(const std::string &path)
  111. {
  112. #ifdef _WIN32
  113. size_t pos = path.find_last_of('\\');
  114. if (pos == -1 || pos < path.find_last_of('/'))
  115. pos = path.find_last_of('/');
  116. #else
  117. size_t pos = path.find_last_of('/');
  118. #endif
  119. return path.substr(0, pos + 1);
  120. }
  121. static inline std::string StringReplace(const std::string &in,
  122. const std::string &search,
  123. const std::string &rep)
  124. {
  125. std::string res = in;
  126. size_t pos;
  127. while ((pos = res.find(search)) != std::string::npos) {
  128. res.replace(pos, search.length(), rep);
  129. }
  130. return res;
  131. }
  132. static inline std::string ReadLine(std::string &str)
  133. {
  134. str = StringReplace(str, "\r\n", "\n");
  135. size_t pos = str.find('\n');
  136. if (pos == std::string::npos)
  137. pos = str.find(EOF);
  138. if (pos == std::string::npos)
  139. pos = str.find('\0');
  140. if (pos == std::string::npos)
  141. return "";
  142. std::string res = str.substr(0, pos);
  143. str = str.substr(pos + 1);
  144. return res;
  145. }