CApp.cpp 2.7 KB

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