CApp.cpp 2.2 KB

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