patch.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <vector>
  6. #include "../../common/MiscFunctions.h"
  7. #include "../../common/Log.h"
  8. #include "buffer.h"
  9. #include "../WorldDatabase.h"
  10. #include "../../common/version.h"
  11. #include "patch.h"
  12. using namespace std;
  13. extern WorldDatabase database;
  14. extern PatchServer patch;
  15. static void TCPClientDisconnect(TCPClient *tcp) {
  16. }
  17. static void TCPClientData(TCPClient *tcp, const char *data, unsigned int len) {
  18. static FILE *f = NULL;
  19. if (f == NULL) {
  20. if (len == 1) {
  21. LogWrite(PATCHER__INFO, 0, "Patcher", "Server is up to date");
  22. patch.SetReading(false);
  23. return;
  24. }
  25. if (strncmp(data, "ERROR", 5) == 0) {
  26. LogWrite(PATCHER__ERROR, 0, "Patcher", "There was an error downloading updates");
  27. tcp->Disconnect();
  28. patch.SetReading(false);
  29. patch.SetQuitAfter(true);
  30. return;
  31. }
  32. if (strncmp(data, "SPEED", 5) == 0) {
  33. LogWrite(PATCHER__ERROR, 0, "Patcher", "You have tried to download updates too quickly and are now locked out, try again later");
  34. tcp->Disconnect();
  35. patch.SetReading(false);
  36. patch.SetQuitAfter(true);
  37. return;
  38. }
  39. if (strncmp(data, "LOCKOUT", 7) == 0) {
  40. LogWrite(PATCHER__ERROR, 0, "Patcher", "You are locked out, try again later");
  41. tcp->Disconnect();
  42. patch.SetReading(false);
  43. patch.SetQuitAfter(true);
  44. return;
  45. }
  46. if (strncmp(data, "UNKNOWN", 7) == 0) {
  47. LogWrite(PATCHER__ERROR, 0, "Patcher", "An unknown error occured");
  48. tcp->Disconnect();
  49. patch.SetReading(false);
  50. patch.SetQuitAfter(true);
  51. return;
  52. }
  53. f = fopen("eq2-updates.sql", "wb");
  54. if (f == NULL) {
  55. LogWrite(PATCHER__ERROR, 0, "Patcher", "Failed to open 'eve-updates.sql' for writing: %s", strerror(errno));
  56. tcp->Disconnect();
  57. patch.SetReading(false);
  58. patch.SetQuitAfter(true);
  59. return;
  60. }
  61. fprintf(f, "SET unique_checks=0;\n");
  62. fprintf(f, "SET foreign_key_checks=0;\n");
  63. LogWrite(PATCHER__INFO, 0, "Patcher", "Downloading updates");
  64. }
  65. if (memchr(data, '\0', len) == NULL)
  66. fwrite(data, sizeof(*data), len, f);
  67. else {
  68. fwrite(data, sizeof(*data), len - 1, f);
  69. fprintf(f, "SET foreign_key_checks=1;\n");
  70. fprintf(f, "SET unique_checks=1;\n");
  71. fclose(f);
  72. f = NULL;
  73. LogWrite(PATCHER__INFO, 0, "Patcher", "Updates finished downloading");
  74. LogWrite(PATCHER__INFO, 0, "Patcher", "Executing updates");
  75. //database.BeginTransaction();
  76. if (!database.QueriesFromFile("eq2-updates.sql")) {
  77. //database.RollbackTransaction();
  78. LogWrite(PATCHER__INFO, 0, "Patcher", "Failed to update database");
  79. }
  80. else {
  81. //database.CommitTransaction();
  82. LogWrite(PATCHER__INFO, 0, "Patcher", "Database updated");
  83. }
  84. patch.SetReading(false);
  85. }
  86. }
  87. PatchServer::PatchServer() {
  88. enabled = true;
  89. reading = false;
  90. quit_after = false;
  91. }
  92. PatchServer::~PatchServer() {
  93. Stop();
  94. }
  95. void PatchServer::SetHost(const char *host) {
  96. tcp.SetHost(host);
  97. }
  98. void PatchServer::SetPort(const char *port) {
  99. tcp.SetPort(port);
  100. }
  101. void PatchServer::SetEnabled(bool enabled) {
  102. this->enabled = enabled;
  103. }
  104. void PatchServer::SetReading(bool reading) {
  105. this->reading = reading;
  106. }
  107. void PatchServer::SetQuitAfter(bool quit_after) {
  108. this->quit_after = quit_after;
  109. }
  110. bool PatchServer::IsEnabled() {
  111. return enabled;
  112. }
  113. bool PatchServer::QuitAfter() {
  114. return quit_after;
  115. }
  116. bool PatchServer::Start() {
  117. TCPClientCallbacks callbacks;
  118. vector<TableVersion *> table_versions;
  119. vector<TableVersion *>::iterator itr;
  120. Buffer buffer;
  121. TCP::Start();
  122. if (!enabled) {
  123. LogWrite(PATCHER__INFO, 0, "Patcher", "Skipping check for database updates from the patch server");
  124. return true;
  125. }
  126. callbacks.data = TCPClientData;
  127. callbacks.disconnect = TCPClientDisconnect;
  128. tcp.SetCallbacks(&callbacks);
  129. if (!tcp.Start()) {
  130. LogWrite(PATCHER__ERROR, 0, "Patcher", "Failed to connect to patch server at %s:%s: %s", tcp.GetHost()[0] == '\0' ? "localhost" : tcp.GetHost(), tcp.GetPort(), tcp.GetError());
  131. return false;
  132. }
  133. LogWrite(PATCHER__INFO, 0, "Patcher", "Connected to patch server at %s:%s", tcp.GetHost()[0] == '\0' ? "localhost" : tcp.GetHost(), tcp.GetPort());
  134. if (!database.GetTableVersions(&table_versions))
  135. return false;
  136. int32 version = (CURRENT_DATABASE_MAJORVERSION * 100) + CURRENT_DATABASE_MINORVERSION;
  137. buffer.WriteUInt32(version);
  138. for (itr = table_versions.begin(); itr != table_versions.end(); itr++) {
  139. buffer.WriteUInt8((*itr)->name_len);
  140. buffer.Write((*itr)->name, (*itr)->name_len);
  141. buffer.WriteUInt32((*itr)->version);
  142. buffer.WriteUInt32((*itr)->data_version);
  143. free(*itr);
  144. }
  145. buffer.WriteAtUInt32(0, buffer.GetLength());
  146. reading = true;
  147. return tcp.Queue(buffer.Peek(), buffer.GetLength());
  148. }
  149. void PatchServer::Stop() {
  150. tcp.Stop();
  151. TCP::Stop();
  152. }
  153. bool PatchServer::Process() {
  154. bool success = true;
  155. if (!enabled)
  156. return true;
  157. while (success && reading) {
  158. success = tcp.Process(0);
  159. SleepMS(10);
  160. }
  161. return success;
  162. }