Autoupdate.cpp
Upload User: dzyhzl
Upload Date: 2019-04-29
Package Size: 56270k
Code Size: 4k
Development Platform:

C/C++

  1. // Autoupdate.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "Autoupdate.h"
  5. #include "AutoupdateDlg.h"
  6. #include "../Engine/Src/KWin32.h"
  7. #include "../Engine/Src/KPakList.h"
  8. #include "../Engine/Src/KFilePath.h"
  9. #include "../Engine/Src/KIniFile.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. KPakList g_PakList;
  16. const int nCanUpdateVersionNumber = 20;
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CAutoupdateApp
  19. BEGIN_MESSAGE_MAP(CAutoupdateApp, CWinApp)
  20. //{{AFX_MSG_MAP(CAutoupdateApp)
  21. // NOTE - the ClassWizard will add and remove mapping macros here.
  22. //    DO NOT EDIT what you see in these blocks of generated code!
  23. //}}AFX_MSG
  24. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CAutoupdateApp construction
  28. CAutoupdateApp::CAutoupdateApp()
  29. {
  30. m_hMutex = NULL;
  31. }
  32. CAutoupdateApp::~CAutoupdateApp()
  33. {
  34. if (m_hMutex)
  35.     {
  36. CloseHandle(m_hMutex);
  37.         m_hMutex = NULL;
  38.     }
  39. }
  40. BOOL CAutoupdateApp::IsTheSelfRun()
  41. {
  42. int max_retry = 8;
  43. while(1)
  44. {
  45. m_hMutex = CreateMutex(NULL,FALSE, "Autoupdate");
  46. if(m_hMutex && GetLastError() == ERROR_ALREADY_EXISTS)
  47. {
  48. CloseHandle(m_hMutex);
  49.             m_hMutex = NULL;
  50. if(--max_retry == 0) {
  51. return TRUE;
  52. }
  53. Sleep(1000);
  54. }
  55. else 
  56.         {
  57. break;
  58. }
  59. }
  60. return FALSE;
  61. }
  62. BOOL CAutoupdateApp::IsTheGameRun()
  63. {
  64. HANDLE hGMutex = CreateMutex(NULL, FALSE, m_strMutex);
  65. if (hGMutex)
  66. {
  67. if(GetLastError() == ERROR_ALREADY_EXISTS)
  68. {
  69. CloseHandle(hGMutex);
  70. return TRUE;
  71. }
  72. }
  73. CloseHandle(hGMutex);
  74. return FALSE;
  75. }
  76. /////////////////////////////////////////////////////////////////////////////
  77. // The one and only CAutoupdateApp object
  78. CAutoupdateApp theApp;
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CAutoupdateApp initialization
  81. BOOL CAutoupdateApp::InitInstance()
  82. {
  83. AfxEnableControlContainer();
  84. // Standard initialization
  85. // If you are not using these features and wish to reduce the size
  86. //  of your final executable, you should remove from the following
  87. //  the specific initialization routines you do not need.
  88. #ifdef _AFXDLL
  89. Enable3dControls(); // Call this when using MFC in a shared DLL
  90. #else
  91. Enable3dControlsStatic(); // Call this when linking to MFC statically
  92. #endif
  93. char szModulePath[MAX_PATH + 1];
  94. ::GetModuleFileName(NULL, szModulePath, (MAX_PATH + 1));
  95. char *pszOffset = NULL;
  96. pszOffset = strrchr(szModulePath, '\');
  97.     ASSERT(pszOffset);
  98.     pszOffset[1] = '';
  99. g_SetRootPath(szModulePath);
  100. g_PakList.Open("\config.ini");
  101. KIniFile IniFile;
  102. if (!IniFile.Load("\settings\autoupdate.ini"))
  103. return FALSE;
  104. char szMutex[MAX_PATH];
  105. IniFile.GetString("main", "mutex", "Worldeditor", szMutex, MAX_PATH);
  106. m_strMutex = szMutex;
  107. if (m_strMutex.IsEmpty())
  108. return FALSE;
  109. if (IsTheSelfRun())
  110. {
  111. AfxMessageBox("自动升级程序正在运行中,请稍候 !");
  112. return FALSE;
  113. }
  114. if (IsTheGameRun())
  115. {
  116. AfxMessageBox("游戏程序正在运行中,不能进行自动升级,请关闭游戏后再试 !");
  117. return FALSE;
  118. }
  119. CAutoupdateDlg dlg;
  120. BOOL b = dlg.InitAutoUpdate();
  121. g_PakList.Close();
  122. if (!b)
  123. {
  124. // AfxMessageBox("自动升级程序配置错误,不能进行自动升级 !");
  125. return FALSE;
  126. }
  127. m_pMainWnd = &dlg;
  128. // AfxMessageBox("开始显示界面");
  129. int nResponse = dlg.DoModal();
  130. if (nResponse == IDOK)
  131. {
  132. // TODO: Place code here to handle when the dialog is
  133. //  dismissed with OK
  134. }
  135. else if (nResponse == IDCANCEL)
  136. {
  137. // TODO: Place code here to handle when the dialog is
  138. //  dismissed with Cancel
  139. }
  140. // Since the dialog has been closed, return FALSE so that we exit the
  141. //  application, rather than start the application's message pump.
  142. return FALSE;
  143. }
  144. int CAutoupdateApp::ExitInstance()
  145. {
  146. return CWinApp::ExitInstance();
  147. }