|
| 1 | +#include "pch.h" |
| 2 | +#include "Games\Dashboard\AutoUpdater.h" |
| 3 | + |
| 4 | +#define HAYZEN_VERSION "6.0.3" |
| 5 | + |
| 6 | +Socket AutoUpdater::s_Socket; |
| 7 | +std::string AutoUpdater::s_PluginPath; |
| 8 | + |
| 9 | +HRESULT AutoUpdater::Init() |
| 10 | +{ |
| 11 | + HRESULT hr = S_OK; |
| 12 | + |
| 13 | + // Initialize the DashLaunch functions |
| 14 | + hr = DashLaunch::Init(); |
| 15 | + if (FAILED(hr)) |
| 16 | + return E_FAIL; |
| 17 | + |
| 18 | + // Get the full path to the Hayzen plugin |
| 19 | + s_PluginPath = GetPluginPath(); |
| 20 | + if (s_PluginPath == "") |
| 21 | + return E_FAIL; |
| 22 | + |
| 23 | + // Create the socket that will be used to communicate with the server |
| 24 | + s_Socket = Socket("3.219.96.23", 80); |
| 25 | + |
| 26 | + return hr; |
| 27 | +} |
| 28 | + |
| 29 | +HRESULT AutoUpdater::CheckForNewVersion(bool &newVersionAvailable) |
| 30 | +{ |
| 31 | + // Connect to the server |
| 32 | + HRESULT hr = s_Socket.Connect(); |
| 33 | + if (FAILED(hr)) |
| 34 | + return E_FAIL; |
| 35 | + |
| 36 | + std::string request = "HEAD /" HAYZEN_VERSION " HTTP/1.1\r\nHost: hayzen-updater.herokuapp.com\r\n\r\n"; |
| 37 | + |
| 38 | + // Send the request to the server |
| 39 | + int bytesSent = s_Socket.Send(request.c_str(), request.size()); |
| 40 | + if (bytesSent != static_cast<int>(request.size())) |
| 41 | + { |
| 42 | + s_Socket.Disconnect(); |
| 43 | + return E_FAIL; |
| 44 | + } |
| 45 | + |
| 46 | + // Wait a little bit for the server to process the request and send the response |
| 47 | + Sleep(700); |
| 48 | + |
| 49 | + // Get the response from the server |
| 50 | + char buffer[4096] = { 0 }; |
| 51 | + std::string response = ""; |
| 52 | + int bytesReceived = 0; |
| 53 | + while ((bytesReceived = s_Socket.Receive(buffer, sizeof(buffer))) > 0) |
| 54 | + { |
| 55 | + response.append(buffer, bytesReceived); |
| 56 | + |
| 57 | + Sleep(100); |
| 58 | + } |
| 59 | + |
| 60 | + // Disconnect from the server |
| 61 | + s_Socket.Disconnect(); |
| 62 | + |
| 63 | + // Get the status code |
| 64 | + size_t pos = response.find("\r\n"); |
| 65 | + std::string firstResponseLine = response.substr(0, pos); |
| 66 | + |
| 67 | + int statusCode = 0; |
| 68 | + sscanf_s(firstResponseLine.c_str(), "HTTP/1.1 %d", &statusCode); |
| 69 | + |
| 70 | + // 304 Not Modified means the version in the HAYZEN_VERSION macro is the latest one |
| 71 | + if (statusCode == 304) |
| 72 | + { |
| 73 | + newVersionAvailable = false; |
| 74 | + return S_OK; |
| 75 | + } |
| 76 | + |
| 77 | + // 200 OK means the version in the HAYZEN_VERSION macro is not the latest one |
| 78 | + if (statusCode == 200) |
| 79 | + { |
| 80 | + newVersionAvailable = true; |
| 81 | + return S_OK; |
| 82 | + } |
| 83 | + |
| 84 | + // Something went wrong if the status code is not 200 nor 304 |
| 85 | + return E_FAIL; |
| 86 | +} |
| 87 | + |
| 88 | +HRESULT AutoUpdater::DownloadLatestVersion() |
| 89 | +{ |
| 90 | + // Connect to the server |
| 91 | + HRESULT hr = s_Socket.Connect(); |
| 92 | + if (FAILED(hr)) |
| 93 | + return E_FAIL; |
| 94 | + |
| 95 | + std::string request = "GET /latest HTTP/1.1\r\nHost: hayzen-updater.herokuapp.com\r\n\r\n"; |
| 96 | + |
| 97 | + // Send the request to the server |
| 98 | + int bytesSent = s_Socket.Send(request.c_str(), request.size()); |
| 99 | + if (bytesSent != static_cast<int>(request.size())) |
| 100 | + { |
| 101 | + s_Socket.Disconnect(); |
| 102 | + return E_FAIL; |
| 103 | + } |
| 104 | + |
| 105 | + // Wait a little bit for the server to process the request and send the response |
| 106 | + Sleep(700); |
| 107 | + |
| 108 | + // Get the response from the server |
| 109 | + char buffer[4096] = { 0 }; |
| 110 | + std::string response = ""; |
| 111 | + int bytesReceived = 0; |
| 112 | + while ((bytesReceived = s_Socket.Receive(buffer, sizeof(buffer))) > 0) |
| 113 | + { |
| 114 | + response.append(buffer, bytesReceived); |
| 115 | + |
| 116 | + Sleep(100); |
| 117 | + } |
| 118 | + |
| 119 | + // Disconnect from the server |
| 120 | + s_Socket.Disconnect(); |
| 121 | + |
| 122 | + // Make sure we received something |
| 123 | + if (response.size() == 0) |
| 124 | + return E_FAIL; |
| 125 | + |
| 126 | + // Isolate the Content-Length header |
| 127 | + size_t startOfContentLengthLine = response.find("Content-Length"); |
| 128 | + if (startOfContentLengthLine == std::string::npos) |
| 129 | + return E_FAIL; |
| 130 | + |
| 131 | + size_t endOfContentLengthLine = response.find("\r\n", startOfContentLengthLine); |
| 132 | + std::string contentLengthLine = response.substr(startOfContentLengthLine, endOfContentLengthLine - startOfContentLengthLine); |
| 133 | + |
| 134 | + // Get the content length |
| 135 | + int contentLength = 0; |
| 136 | + sscanf_s(contentLengthLine.c_str(), "Content-Length: %d", &contentLength); |
| 137 | + |
| 138 | + // Get the content |
| 139 | + std::string delimiter = "\r\n\r\n"; |
| 140 | + size_t startOfContent = response.find(delimiter) + delimiter.size(); |
| 141 | + std::string content = response.substr(startOfContent, contentLength); |
| 142 | + |
| 143 | + // Open the file |
| 144 | + std::ofstream file; |
| 145 | + file.open(s_PluginPath, std::ios::out | std::ios::binary); |
| 146 | + if (!file.is_open()) |
| 147 | + return E_FAIL; |
| 148 | + |
| 149 | + // Write the content to the file |
| 150 | + file << content; |
| 151 | + file.close(); |
| 152 | + |
| 153 | + return hr; |
| 154 | +} |
| 155 | + |
| 156 | +std::string AutoUpdater::GetPluginPath() |
| 157 | +{ |
| 158 | + // Dashlaunch allows a maximum of 5 plugins |
| 159 | + const size_t maxNumberOfPlugins = 5; |
| 160 | + |
| 161 | + for (size_t i = 0; i <= maxNumberOfPlugins; i++) |
| 162 | + { |
| 163 | + char *pluginFullPath = nullptr; |
| 164 | + char pluginKey[8] = { 0 }; |
| 165 | + |
| 166 | + // Create the plugin key, plugin1, plugin2 and so on |
| 167 | + _snprintf_s(pluginKey, _TRUNCATE, "plugin%d", i); |
| 168 | + |
| 169 | + // Get the path set for the current plugin key |
| 170 | + BOOL res = DashLaunch::GetOptionValueByName(pluginKey, reinterpret_cast<uint32_t *>(&pluginFullPath)); |
| 171 | + |
| 172 | + // Some plugin keys might be missing in launch.ini so just continue to the next plugin if the key is not found |
| 173 | + if (res == FALSE) |
| 174 | + continue; |
| 175 | + |
| 176 | + // Extract the file name from the full plugin path |
| 177 | + char pluginFileName[MAX_PATH] = { 0 }; |
| 178 | + _splitpath_s( |
| 179 | + pluginFullPath, |
| 180 | + nullptr, 0, |
| 181 | + nullptr, 0, |
| 182 | + pluginFileName, sizeof(pluginFileName), |
| 183 | + nullptr, 0 |
| 184 | + ); |
| 185 | + |
| 186 | + // Check if the current plugin file name is Hayzen, return the current plugin full path |
| 187 | + if (!strcmp(pluginFileName, "Hayzen")) |
| 188 | + { |
| 189 | + // The full path returned from DashLaunch does not contain the drive name so we need to add it |
| 190 | + std::string result = "hdd:"; |
| 191 | + result += pluginFullPath; |
| 192 | + |
| 193 | + return result; |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + // If nothing is found, just return an empty string |
| 198 | + return std::string(""); |
| 199 | +} |
0 commit comments