CApp.cpp 2.9 KB

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