importers.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. enum obs_importer_responses {
  22. IMPORTER_SUCCESS,
  23. IMPORTER_FILE_NOT_FOUND,
  24. IMPORTER_FILE_NOT_RECOGNISED,
  25. IMPORTER_FILE_WONT_OPEN,
  26. IMPORTER_ERROR_DURING_CONVERSION,
  27. IMPORTER_UNKNOWN_ERROR,
  28. IMPORTER_NOT_FOUND
  29. };
  30. typedef std::vector<std::string> OBSImporterFiles;
  31. class Importer {
  32. public:
  33. virtual ~Importer() {}
  34. virtual std::string Prog() { return "Null"; };
  35. virtual int ImportScenes(const std::string &path, std::string &name,
  36. 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,
  49. json11::Json &res);
  50. bool Check(const std::string &path);
  51. std::string Name(const std::string &path);
  52. OBSImporterFiles FindFiles();
  53. };
  54. class StudioImporter : public Importer {
  55. public:
  56. std::string Prog() { return "OBSStudio"; };
  57. int ImportScenes(const std::string &path, std::string &name,
  58. json11::Json &res);
  59. bool Check(const std::string &path);
  60. std::string Name(const std::string &path);
  61. };
  62. class SLImporter : public Importer {
  63. public:
  64. std::string Prog() { return "Streamlabs"; };
  65. int ImportScenes(const std::string &path, std::string &name,
  66. json11::Json &res);
  67. bool Check(const std::string &path);
  68. std::string Name(const std::string &path);
  69. OBSImporterFiles FindFiles();
  70. };
  71. class XSplitImporter : public Importer {
  72. public:
  73. std::string Prog() { return "XSplitBroadcaster"; };
  74. int ImportScenes(const std::string &path, std::string &name,
  75. json11::Json &res);
  76. bool Check(const std::string &path);
  77. std::string Name(const std::string &path)
  78. {
  79. return "XSplit Import";
  80. UNUSED_PARAMETER(path);
  81. };
  82. OBSImporterFiles FindFiles();
  83. };
  84. void ImportersInit();
  85. std::string DetectProgram(const std::string &path);
  86. std::string GetSCName(const std::string &path, const std::string &prog);
  87. int ImportSCFromProg(const std::string &path, std::string &name,
  88. const std::string &program, json11::Json &res);
  89. int ImportSC(const std::string &path, std::string &name, json11::Json &res);
  90. OBSImporterFiles ImportersFindFiles();
  91. void TranslateOSStudio(json11::Json &data);
  92. static inline std::string GetFilenameFromPath(const std::string &path)
  93. {
  94. #ifdef _WIN32
  95. size_t pos = path.find_last_of('\\');
  96. if (pos == -1 || pos < path.find_last_of('/'))
  97. pos = path.find_last_of('/');
  98. #else
  99. size_t pos = path.find_last_of('/');
  100. #endif
  101. size_t ext = path.find_last_of('.');
  102. if (ext < pos) {
  103. return path.substr(pos + 1);
  104. } else {
  105. return path.substr(pos + 1, ext - pos - 1);
  106. }
  107. }
  108. static inline std::string GetFolderFromPath(const std::string &path)
  109. {
  110. #ifdef _WIN32
  111. size_t pos = path.find_last_of('\\');
  112. if (pos == -1 || pos < path.find_last_of('/'))
  113. pos = path.find_last_of('/');
  114. #else
  115. size_t pos = path.find_last_of('/');
  116. #endif
  117. return path.substr(0, pos + 1);
  118. }
  119. static inline std::string StringReplace(const std::string &in,
  120. const std::string &search,
  121. const std::string &rep)
  122. {
  123. std::string res = in;
  124. size_t pos;
  125. while ((pos = res.find(search)) != std::string::npos) {
  126. res.replace(pos, search.length(), rep);
  127. }
  128. return res;
  129. }
  130. static inline std::string ReadLine(std::string &str)
  131. {
  132. str = StringReplace(str, "\r\n", "\n");
  133. size_t pos = str.find('\n');
  134. if (pos == std::string::npos)
  135. pos = str.find(EOF);
  136. if (pos == std::string::npos)
  137. pos = str.find('\0');
  138. if (pos == std::string::npos)
  139. return "";
  140. std::string res = str.substr(0, pos);
  141. str = str.substr(pos + 1);
  142. return res;
  143. }