AllSrvUI.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /////////////////////////////////////////////////////////////////////////////
  2. // AllSrvUI.cpp : Defines the class behaviors for the application.
  3. //
  4. #include "stdafx.h"
  5. #include "AllSrvUI.h"
  6. #include "AllSrvUISheet.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // The one and only CAllSrvUIApp object
  14. CAllSrvUIApp theApp;
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CAllSrvUIApp
  17. /////////////////////////////////////////////////////////////////////////////
  18. // Message Map
  19. BEGIN_MESSAGE_MAP(CAllSrvUIApp, CWinApp)
  20. //{{AFX_MSG_MAP(CAllSrvUIApp)
  21. //}}AFX_MSG
  22. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  23. END_MESSAGE_MAP()
  24. /////////////////////////////////////////////////////////////////////////////
  25. // Construction
  26. CAllSrvUIApp::CAllSrvUIApp() :
  27. m_hrCoInit(E_FAIL)
  28. {
  29. }
  30. /////////////////////////////////////////////////////////////////////////////
  31. // Attributes
  32. void CAllSrvUIApp::GetArtPath(char * szArtPath)
  33. {
  34. strcpy(szArtPath, ".\\Artwork\\");
  35. CRegKey key;
  36. if (ERROR_SUCCESS == key.Open(HKEY_LOCAL_MACHINE, HKLM_FedSrv, KEY_READ))
  37. {
  38. ZString strArtPath;
  39. if (SUCCEEDED(LoadRegString(key, "Artpath", strArtPath)))
  40. {
  41. // if reg value exists copy over default
  42. strncpy(szArtPath, PCC(strArtPath), MAX_PATH);
  43. }
  44. // ensure last character is a backslash
  45. int nLast = max(0, strlen(szArtPath)-1);
  46. if (szArtPath[nLast] != '\\' || szArtPath[nLast] != '/')
  47. szArtPath[nLast+1] = '\\';
  48. szArtPath[nLast+2] = 0;
  49. }
  50. }
  51. /////////////////////////////////////////////////////////////////////////////
  52. // Overrides
  53. BOOL CAllSrvUIApp::InitInstance()
  54. {
  55. // Determine if another instance is already running
  56. HANDLE hEvent = GetAllSrvUIEvent();
  57. if (hEvent)
  58. {
  59. // Signal the other instance to activate itself
  60. ::SetEvent(hEvent);
  61. // Close the event handle and exit
  62. ::CloseHandle(hEvent);
  63. return false;
  64. }
  65. // Ensure that the EULA has been agreed to
  66. HRESULT hr = FirstRunEula();
  67. if (E_FAIL == hr)
  68. {
  69. ::MessageBox(0, "Error while loading loading ebueula.dll", "Allegiance Server", 0);
  70. return false;
  71. }
  72. else if (S_FALSE == hr)
  73. {
  74. ::MessageBox(0, "You must accept the End User License Agreement before running the Allegiance Server", "Allegiance Server", 0);
  75. return false;
  76. }
  77. else
  78. {
  79. assert(S_OK == hr);
  80. }
  81. // Create the instance event
  82. CreateAllSrvUIEvent();
  83. // Initialize COM
  84. // m_hrCoInit = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  85. m_hrCoInit = CoInitialize(NULL);
  86. // Create the property sheet window
  87. CAllSrvUISheet* pps = new CAllSrvUISheet;
  88. // Create the property sheet as a modeless dialog box
  89. if (pps->Create())
  90. m_pMainWnd = pps;
  91. // Continue processing the application
  92. return true;
  93. }
  94. int CAllSrvUIApp::ExitInstance()
  95. {
  96. // Uninitialize COM
  97. if (SUCCEEDED(m_hrCoInit))
  98. CoUninitialize();
  99. // Perform default processing
  100. return CWinApp::ExitInstance();
  101. }
  102. BOOL CAllSrvUIApp::OnIdle(LONG lCount)
  103. {
  104. HANDLE hEvents[] = {m_shEventSync};
  105. DWORD dwWait = ::MsgWaitForMultipleObjects(sizeofArray(hEvents), hEvents,
  106. false, INFINITE, QS_ALLINPUT);
  107. if (WAIT_OBJECT_0 == dwWait)
  108. {
  109. ::ResetEvent(m_shEventSync);
  110. m_pMainWnd->SetForegroundWindow();
  111. m_pMainWnd->ShowWindow(SW_SHOWNORMAL);
  112. }
  113. return true;
  114. }
  115. /////////////////////////////////////////////////////////////////////////////
  116. // Implementation
  117. HANDLE CAllSrvUIApp::GetAllSrvUIEvent()
  118. {
  119. // Open the mutex using the global name first
  120. DWORD dwAccess = EVENT_MODIFY_STATE | SYNCHRONIZE;
  121. HANDLE hEvent = ::OpenEvent(dwAccess, false, szAllSrvUIRunningGlobal);
  122. if (!hEvent)
  123. hEvent = ::OpenEvent(dwAccess, false, szAllSrvUIRunning);
  124. return hEvent;
  125. }
  126. void CAllSrvUIApp::CreateAllSrvUIEvent()
  127. {
  128. // Create a NULL dacl to give "everyone" access
  129. SECURITY_ATTRIBUTES* psa = NULL;
  130. SECURITY_DESCRIPTOR sd;
  131. SECURITY_ATTRIBUTES sa = {sizeof(sa), &sd, false};
  132. if (IsWinNT())
  133. {
  134. InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
  135. SetSecurityDescriptorDacl(&sd, true, NULL, FALSE);
  136. psa = &sa;
  137. }
  138. // Create the event using the global name first
  139. HANDLE hEvent = ::CreateEvent(psa, true, false, szAllSrvUIRunningGlobal);
  140. if (!hEvent)
  141. hEvent = ::CreateEvent(psa, true, false, szAllSrvUIRunning);
  142. m_shEventSync = hEvent;
  143. }
  144. //
  145. // EULA related files should be in the artwork folder so that they may be autoupdated
  146. //
  147. HRESULT CAllSrvUIApp::FirstRunEula()
  148. {
  149. TCHAR szArtpath[MAX_PATH];
  150. GetArtPath(szArtpath);
  151. PathString strArtPath = szArtpath;
  152. PathString strEulaRTF = strArtPath + "eula.rtf";
  153. {
  154. ZFile file(strEulaRTF);
  155. int n = file.GetLength(); // -1 means error
  156. if (n == -1)
  157. return E_FAIL;
  158. }
  159. // don't use += operator cause it's buggy with PathString
  160. strArtPath = strArtPath + "EBUEula.dll";
  161. HINSTANCE hMod = LoadLibrary(PCC(strArtPath));
  162. if (NULL == hMod) // can't attach to DLL
  163. {
  164. // this time, search path
  165. hMod = LoadLibrary("EBUEula.dll");
  166. if (NULL == hMod) // can't attach to DLL
  167. return E_FAIL;
  168. }
  169. EBUPROC pfnEBUEula = (EBUPROC) GetProcAddress(hMod, "EBUEula");
  170. if (NULL == pfnEBUEula) // can't find entry point
  171. {
  172. FreeLibrary(hMod);
  173. return E_FAIL;
  174. }
  175. /*
  176. TCHAR szWarranty[MAX_PATH];
  177. LoadString(GetModuleHandle(), STR_EULAFILENAME, szEULA, sizeof(szEULA));
  178. LoadString(GetModuleHandle(), STR_WARRANTYNAME, szWarranty, sizeof(szWarranty));
  179. //
  180. //This call enables both EULA and warranty accepting/viewing/printing. If your
  181. //game doesn't ship with a WARRANTY file, specifiy NULL instead of szWarranty…
  182. //The code below, for instance, works with both OEM and retail builds…
  183. //
  184. TCHAR *pszWarrantyParam = 0xFFFFFFFF != GetFileAttributes(szWarranty) ? szWarranty : NULL;
  185. */
  186. bool bAllowGameToRun = pfnEBUEula(HKLM_AllSrvUI, PCC(strEulaRTF), NULL, TRUE) != 0;
  187. FreeLibrary(hMod);
  188. return (bAllowGameToRun ? S_OK : S_FALSE);
  189. }