CApp.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "stdafx.h"
  2. #include "CApp.h"
  3. CApp* SSingleton<CApp>::ms_Singleton = NULL;
  4. CApp::CApp() : m_is_out(0), m_versioninfo(nullptr),m_userinfo(nullptr), m_server_list(nullptr), m_sys_config(nullptr), m_proxy_mode(PROXY_MODE::sys_mode), m_route_mode(ROUT_MODE::cn_mode), m_login_info(nullptr),node_select_id(0)
  5. {
  6. m_hInst = nullptr;
  7. }
  8. CApp::~CApp()
  9. {
  10. UnInit();
  11. }
  12. void CApp::Init()
  13. {
  14. if (!m_login_info) {
  15. m_login_info = new CLoginInfo();
  16. }
  17. if (!m_userinfo)
  18. {
  19. m_userinfo = new CUserInfo();
  20. }
  21. if (!m_server_list)
  22. {
  23. m_server_list = new CServerList();
  24. }
  25. if (!m_versioninfo)
  26. {
  27. m_versioninfo = new CVersion();
  28. }
  29. if (!m_sys_config)
  30. {
  31. m_sys_config = new CSysConfig();
  32. }
  33. }
  34. void CApp::UnInit() {
  35. if (m_login_info) {
  36. delete m_login_info;
  37. m_login_info = nullptr;
  38. }
  39. if (m_userinfo)
  40. {
  41. delete m_userinfo;
  42. m_userinfo = nullptr;
  43. }
  44. if (m_server_list)
  45. {
  46. delete m_server_list;
  47. m_server_list = nullptr;
  48. }
  49. if (m_versioninfo)
  50. {
  51. delete m_versioninfo;
  52. m_versioninfo = nullptr;
  53. }
  54. if (m_sys_config)
  55. {
  56. delete m_sys_config;
  57. m_sys_config = nullptr;
  58. }
  59. }
  60. CLoginInfo* CApp::GetLoginInfo() {
  61. return m_login_info;
  62. }
  63. CUserInfo* CApp::GetUserinfo()
  64. {
  65. return m_userinfo;
  66. }
  67. CServerList* CApp::GetServerList()
  68. {
  69. return m_server_list;
  70. }
  71. CVersion* CApp::GetVerinfo()
  72. {
  73. return m_versioninfo;
  74. }
  75. CSysConfig* CApp::GetSysconfig()
  76. {
  77. return m_sys_config;
  78. }
  79. void CApp::SetOut(int out)
  80. {
  81. m_is_out = out;
  82. }
  83. int CApp::GetOut()
  84. {
  85. return m_is_out;
  86. }
  87. void CApp::SetSysMode(PROXY_MODE mode)
  88. {
  89. m_proxy_mode = mode;
  90. }
  91. PROXY_MODE CApp::GetSysMode()
  92. {
  93. return m_proxy_mode;
  94. }
  95. void CApp::SetRouteMode(ROUT_MODE mode)
  96. {
  97. m_route_mode = mode;
  98. }
  99. ROUT_MODE CApp::GetRouteMode()
  100. {
  101. return m_route_mode;
  102. }
  103. void CApp::SetMethod(HINSTANCE hinst)
  104. {
  105. m_hInst = hinst;
  106. }
  107. HINSTANCE CApp::GetMethod()
  108. {
  109. return m_hInst;
  110. }
  111. void CApp::ChkeAndSetDtaPath()
  112. {
  113. std::wstring portableDataPath = std::filesystem::current_path().wstring() + L"\\" + DSPROXY_DATA_DIR_PORTABLE;
  114. if (std::filesystem::is_directory(portableDataPath)) {
  115. m_dataPath = std::move(portableDataPath);
  116. }
  117. else {
  118. m_dataPath = GetKnownFolderFsPath(FOLDERID_RoamingAppData) / DSPROXY_DATA_DIR;
  119. }
  120. m_configPath = m_dataPath / DSPROXY_CONFIG_DIR_NAME;
  121. m_config_Gui_Path = m_dataPath;
  122. }
  123. bool CApp::CheckOnlyOneInstance() noexcept
  124. {
  125. auto hFile = CreateFileW(m_dataPath.c_str(), GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
  126. if (hFile == INVALID_HANDLE_VALUE && GetLastError() != ERROR_FILE_NOT_FOUND)
  127. return false;
  128. // Hold this handle
  129. return true;
  130. }
  131. std::filesystem::path CApp::GetDataPath()
  132. {
  133. return m_dataPath;
  134. }
  135. std::filesystem::path CApp::GetConfigPath()
  136. {
  137. return m_configPath;
  138. }
  139. std::filesystem::path CApp::GetConfigGuiPath()
  140. {
  141. return m_config_Gui_Path;
  142. }
  143. void CApp::SetupDataDirectory()
  144. {
  145. try
  146. {
  147. CreateDirectoryIgnoreExist(m_configPath.c_str());
  148. }
  149. CATCH_LOG();
  150. }
  151. void CApp::SetCLashRuning(bool m)
  152. {
  153. m_clashRuning = m;
  154. }
  155. bool CApp::GetClashRuning()
  156. {
  157. return m_clashRuning;
  158. }
  159. std::string CApp::GetSelect_node() const { return select_node; }
  160. void CApp::SetSelect_node(std::string val) {
  161. select_node = val;
  162. }
  163. void CApp::SetSelectNodeById(int val) {
  164. node_select_id = val;
  165. }
  166. int CApp::GetSelectNodeById() {
  167. return node_select_id;
  168. }