AutopatcherClientGFx3Impl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. // Common includes
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "Kbhit.h"
  14. #include "GetTime.h"
  15. #include "RakPeerInterface.h"
  16. #include "MessageIdentifiers.h"
  17. #include "BitStream.h"
  18. #include "StringCompressor.h"
  19. #include "PacketizedTCP.h"
  20. // Client only includes
  21. #include "FileListTransferCBInterface.h"
  22. #include "FileListTransfer.h"
  23. #include "AutopatcherClient.h"
  24. #include "AutopatcherPatchContext.h"
  25. #include "RakSleep.h"
  26. #include "AutopatcherClientGFx3Impl.h"
  27. using namespace RakNet;
  28. static const char *AUTOPATCHER_LAST_UPDATE_FILE="autopatcherLastUpdate.txt";
  29. static const char *AUTOPATCHER_RESTART_FILE="autopatcherRestart.txt";
  30. class TestCB : public RakNet::FileListTransferCBInterface
  31. {
  32. public:
  33. virtual bool OnFile(OnFileStruct *onFileStruct)
  34. {
  35. char buff[1024];
  36. if (onFileStruct->context.op==PC_HASH_1_WITH_PATCH || onFileStruct->context.op==PC_HASH_2_WITH_PATCH)
  37. strcpy(buff,"Patched: ");
  38. else if (onFileStruct->context.op==PC_WRITE_FILE)
  39. strcpy(buff,"Written: ");
  40. else if (onFileStruct->context.op==PC_ERROR_FILE_WRITE_FAILURE)
  41. strcpy(buff,"Write Failure: ");
  42. else if (onFileStruct->context.op==PC_ERROR_PATCH_TARGET_MISSING)
  43. strcpy(buff,"Patch target missing: ");
  44. else if (onFileStruct->context.op==PC_ERROR_PATCH_APPLICATION_FAILURE)
  45. strcpy(buff,"Patch process failure: ");
  46. else if (onFileStruct->context.op==PC_ERROR_PATCH_RESULT_CHECKSUM_FAILURE)
  47. strcpy(buff,"Patch checksum failure: ");
  48. else if (onFileStruct->context.op==PC_NOTICE_WILL_COPY_ON_RESTART)
  49. strcpy(buff,"Copy pending restart: ");
  50. else if (onFileStruct->context.op==PC_NOTICE_FILE_DOWNLOADED)
  51. strcpy(buff,"Downloaded: ");
  52. else if (onFileStruct->context.op==PC_NOTICE_FILE_DOWNLOADED_PATCH)
  53. strcpy(buff,"Downloaded Patch: ");
  54. else
  55. RakAssert(0);
  56. sprintf(buff+strlen(buff), "%i. (100%%) %i/%i %s %ib / %ib\n", onFileStruct->setID, onFileStruct->fileIndex+1, onFileStruct->numberOfFilesInThisSet,
  57. onFileStruct->fileName, onFileStruct->byteLengthOfThisFile,
  58. onFileStruct->byteLengthOfThisSet);
  59. FxResponseArgs<1> args;
  60. args.Add(GFxValue(buff));
  61. FxDelegate::Invoke2(autopatcherClient->movie, "addToPatchNotesText", args);
  62. FxResponseArgs<5> args2;
  63. args2.Add(GFxValue(buff));
  64. args2.Add(GFxValue(1.0));
  65. args2.Add(GFxValue(1.0));
  66. args2.Add(GFxValue((double)onFileStruct->bytesDownloadedForThisSet));
  67. args2.Add(GFxValue((double)onFileStruct->byteLengthOfThisSet));
  68. FxDelegate::Invoke2(autopatcherClient->movie, "updateProgressBars", args2);
  69. // Return false for the file data to be deallocated automatically
  70. return false;
  71. }
  72. virtual void OnFileProgress(FileProgressStruct *fps)
  73. {
  74. char buff[1024];
  75. sprintf(buff, "%s %ib / %ib\n", fps->onFileStruct->fileName,
  76. fps->onFileStruct->bytesDownloadedForThisFile, fps->onFileStruct->byteLengthOfThisFile);
  77. FxResponseArgs<5> args2;
  78. float thisFileProgress,totalProgress;
  79. thisFileProgress=(float)fps->partCount/(float)fps->partTotal;
  80. totalProgress=(float)(fps->onFileStruct->fileIndex+1)/(float)fps->onFileStruct->numberOfFilesInThisSet;
  81. args2.Add(GFxValue(buff));
  82. args2.Add(GFxValue((double)fps->onFileStruct->bytesDownloadedForThisFile));
  83. args2.Add(GFxValue((double)fps->onFileStruct->byteLengthOfThisFile));
  84. args2.Add(GFxValue((double)fps->onFileStruct->bytesDownloadedForThisSet));
  85. args2.Add(GFxValue((double)fps->onFileStruct->byteLengthOfThisSet));
  86. FxDelegate::Invoke2(autopatcherClient->movie, "updateProgressBars", args2);
  87. }
  88. AutopatcherClientGFx3Impl *autopatcherClient;
  89. } transferCallback;
  90. AutopatcherClientGFx3Impl::AutopatcherClientGFx3Impl()
  91. {
  92. autopatcherClient=0;
  93. fileListTransfer=0;
  94. packetizedTCP=0;
  95. }
  96. AutopatcherClientGFx3Impl::~AutopatcherClientGFx3Impl()
  97. {
  98. Shutdown();
  99. }
  100. void AutopatcherClientGFx3Impl::Init(const char *_pathToThisExe, GPtr<FxDelegate> pDelegate, GPtr<GFxMovieView> pMovie)
  101. {
  102. pDelegate->RegisterHandler(this);
  103. delegate=pDelegate;
  104. movie=pMovie;
  105. strcpy(pathToThisExe,_pathToThisExe);
  106. autopatcherClient=RakNet::OP_NEW<AutopatcherClient>(_FILE_AND_LINE_);
  107. fileListTransfer=RakNet::OP_NEW<FileListTransfer>(_FILE_AND_LINE_);
  108. packetizedTCP=RakNet::OP_NEW<PacketizedTCP>(_FILE_AND_LINE_);
  109. autopatcherClient->SetFileListTransferPlugin(fileListTransfer);
  110. packetizedTCP->AttachPlugin(autopatcherClient);
  111. packetizedTCP->AttachPlugin(fileListTransfer);
  112. }
  113. void AutopatcherClientGFx3Impl::Update(void)
  114. {
  115. RakNet::Packet *p;
  116. SystemAddress notificationAddress;
  117. notificationAddress=packetizedTCP->HasCompletedConnectionAttempt();
  118. if (notificationAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  119. {
  120. UpdateConnectResult(true);
  121. serverAddress=notificationAddress;
  122. }
  123. notificationAddress=packetizedTCP->HasFailedConnectionAttempt();
  124. if (notificationAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  125. {
  126. UpdateConnectResult(false);
  127. }
  128. notificationAddress=packetizedTCP->HasNewIncomingConnection();
  129. notificationAddress=packetizedTCP->HasLostConnection();
  130. if (notificationAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  131. {
  132. UpdateConnectResult(false);
  133. }
  134. p=packetizedTCP->Receive();
  135. while (p)
  136. {
  137. if (p->data[0]==ID_AUTOPATCHER_REPOSITORY_FATAL_ERROR)
  138. {
  139. char buff[256];
  140. RakNet::BitStream temp(p->data, p->length, false);
  141. temp.IgnoreBits(8);
  142. StringCompressor::Instance()->DecodeString(buff, 256, &temp);
  143. // Error.
  144. FxDelegate::Invoke2(movie, "gotoCompletionMenu", FxResponseArgs<0>());
  145. FxResponseArgs<1> args2;
  146. args2.Add(GFxValue(buff));
  147. FxDelegate::Invoke2(movie, "setCompletionMessage", args2);
  148. }
  149. else if (p->data[0]==ID_AUTOPATCHER_FINISHED)
  150. {
  151. FxDelegate::Invoke2(movie, "gotoCompletionMenu", FxResponseArgs<0>());
  152. SaveLastUpdateDate();
  153. }
  154. else if (p->data[0]==ID_AUTOPATCHER_RESTART_APPLICATION)
  155. {
  156. FxDelegate::Invoke2(movie, "gotoCompletionMenu", FxResponseArgs<0>());
  157. FxResponseArgs<1> args2;
  158. RakNet::RakString completionMsg("Launch \"AutopatcherClientRestarter.exe %s\"\nQuit this application immediately after to unlock files.\n", AUTOPATCHER_RESTART_FILE);
  159. args2.Add(GFxValue(completionMsg.C_String()));
  160. FxDelegate::Invoke2(movie, "setCompletionMessage", args2);
  161. SaveLastUpdateDate();
  162. }
  163. packetizedTCP->DeallocatePacket(p);
  164. p=packetizedTCP->Receive();
  165. }
  166. }
  167. void AutopatcherClientGFx3Impl::Shutdown(void)
  168. {
  169. if (delegate.GetPtr()!=0)
  170. {
  171. delegate->UnregisterHandler(this);
  172. delegate.Clear();
  173. }
  174. movie.Clear();
  175. if (packetizedTCP)
  176. packetizedTCP->Stop();
  177. RakNet::OP_DELETE(autopatcherClient,_FILE_AND_LINE_);
  178. RakNet::OP_DELETE(fileListTransfer,_FILE_AND_LINE_);
  179. RakNet::OP_DELETE(packetizedTCP,_FILE_AND_LINE_);
  180. autopatcherClient=0;
  181. fileListTransfer=0;
  182. packetizedTCP=0;
  183. }
  184. const char* AutopatcherClientGFx3Impl::Connect(const char *ip, unsigned short port)
  185. {
  186. if (packetizedTCP->Start(0,1)==true)
  187. {
  188. packetizedTCP->Connect(ip,port,false);
  189. return "Connecting";
  190. }
  191. else
  192. return "Start call failed.";
  193. }
  194. void AutopatcherClientGFx3Impl::PressedPatch(const FxDelegateArgs& pparams)
  195. {
  196. AutopatcherClientGFx3Impl* prt = (AutopatcherClientGFx3Impl*)pparams.GetHandler();
  197. //appNameText.text, appDirectoryText.text, fullRescanBtn.selected
  198. const char *appName = pparams[0].GetString();
  199. const char *appDir = pparams[1].GetString();
  200. bool fullRescan = pparams[2].GetBool();
  201. strcpy(prt->appDirectory, appDir);
  202. char restartFile[512];
  203. strcpy(restartFile, appDir);
  204. strcat(restartFile, "/");
  205. strcat(restartFile, AUTOPATCHER_RESTART_FILE);
  206. double lastUpdateDate;
  207. if (fullRescan==false)
  208. prt->LoadLastUpdateDate(&lastUpdateDate,appDir);
  209. else
  210. lastUpdateDate=0;
  211. transferCallback.autopatcherClient=prt;
  212. if (prt->autopatcherClient->PatchApplication(appName, appDir, lastUpdateDate, prt->serverAddress, &transferCallback, restartFile, prt->pathToThisExe))
  213. {
  214. FxDelegate::Invoke2(prt->movie, "gotoPatchMenu", FxResponseArgs<0>());
  215. }
  216. else
  217. {
  218. prt->packetizedTCP->Stop();
  219. //prt->UpdateConnectResult("Failed to start patching");
  220. FxDelegate::Invoke2(prt->movie, "gotoPatchStartMenu", FxResponseArgs<0>());
  221. }
  222. }
  223. void AutopatcherClientGFx3Impl::OpenSite(const FxDelegateArgs& pparams)
  224. {
  225. AutopatcherClientGFx3Impl* prt = (AutopatcherClientGFx3Impl*)pparams.GetHandler();
  226. const char *siteType = pparams[0].GetString();
  227. if (stricmp(siteType, "help")==0)
  228. {
  229. ShellExecute(NULL, "open", "http://www.jenkinssoftware.com/raknet/manual/autopatcher.html", NULL, NULL, SW_SHOWNORMAL);
  230. }
  231. else if (stricmp(siteType, "raknet")==0)
  232. {
  233. ShellExecute(NULL, "open", "http://www.jenkinssoftware.com/", NULL, NULL, SW_SHOWNORMAL);
  234. }
  235. else if (stricmp(siteType, "scaleform")==0)
  236. {
  237. ShellExecute(NULL, "open", "https://www.scaleform.com/", NULL, NULL, SW_SHOWNORMAL);
  238. }
  239. }
  240. void AutopatcherClientGFx3Impl::PressedConnect(const FxDelegateArgs& pparams)
  241. {
  242. AutopatcherClientGFx3Impl* prt = (AutopatcherClientGFx3Impl*)pparams.GetHandler();
  243. const char *result = prt->Connect(pparams[0].GetString(), atoi(pparams[1].GetString()));
  244. }
  245. void AutopatcherClientGFx3Impl::PressedOKBtn(const FxDelegateArgs& pparams)
  246. {
  247. AutopatcherClientGFx3Impl* prt = (AutopatcherClientGFx3Impl*)pparams.GetHandler();
  248. prt->autopatcherClient->Clear();
  249. prt->packetizedTCP->Stop();
  250. prt->GotoMainMenu();
  251. }
  252. void AutopatcherClientGFx3Impl::UpdateConnectResult( bool isConnected )
  253. {
  254. FxResponseArgs<1> args;
  255. args.Add(GFxValue(isConnected));
  256. FxDelegate::Invoke2(movie, "ConnectResult", args);
  257. }
  258. void AutopatcherClientGFx3Impl::Accept(CallbackProcessor* cbreg)
  259. {
  260. cbreg->Process( "PressedOKBtn", &AutopatcherClientGFx3Impl::PressedOKBtn );
  261. cbreg->Process( "PressedConnect", &AutopatcherClientGFx3Impl::PressedConnect );
  262. cbreg->Process( "PressedPatch", &AutopatcherClientGFx3Impl::PressedPatch );
  263. cbreg->Process( "openSite", &AutopatcherClientGFx3Impl::OpenSite );
  264. }
  265. void AutopatcherClientGFx3Impl::SaveLastUpdateDate(void)
  266. {
  267. char inPath[512];
  268. double serverDate=autopatcherClient->GetServerDate();
  269. strcpy(inPath, appDirectory);
  270. strcat(inPath, "/");
  271. strcat(inPath, AUTOPATCHER_LAST_UPDATE_FILE);
  272. FILE *fp = fopen(inPath,"wb");
  273. if (fp!=0)
  274. {
  275. fwrite(&serverDate,sizeof(double),1,fp);
  276. fclose(fp);
  277. }
  278. }
  279. void AutopatcherClientGFx3Impl::LoadLastUpdateDate(double *out, const char *appDir)
  280. {
  281. char inPath[512];
  282. strcpy(appDirectory,appDir);
  283. strcpy(inPath, appDirectory);
  284. strcat(inPath, "/");
  285. strcat(inPath, AUTOPATCHER_LAST_UPDATE_FILE);
  286. FILE *fp = fopen(inPath,"rb");
  287. if (fp!=0)
  288. {
  289. fread(out,sizeof(double),1,fp);
  290. fclose(fp);
  291. }
  292. else
  293. out[0]=0;
  294. }
  295. void AutopatcherClientGFx3Impl::GotoMainMenu(void)
  296. {
  297. FxDelegate::Invoke2(movie, "gotoMainMenu", FxResponseArgs<0>());
  298. autopatcherClient->Clear();
  299. packetizedTCP->Stop();
  300. }
粤ICP备19079148号