CApp.cpp 2.3 KB

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