Get motd from xlabs.dev

This fixes #131
This commit is contained in:
momo5502 2021-04-26 19:23:58 +02:00
parent aa2848ac31
commit 4db7fb5bb5
9 changed files with 141 additions and 44 deletions

View File

@ -248,14 +248,14 @@ end
flags {"NoIncrementalLink", "NoMinimalRebuild", "MultiProcessorCompile", "No64BitChecks"}
buildoptions {"/GL"}
linkoptions { "/IGNORE:4702", "/LTCG" }
configuration "windows"
defines {"_WINDOWS", "WIN32"}
configuration "Release"
optimize "Size"
buildoptions {"/GL"}
linkoptions { "/IGNORE:4702", "/LTCG" }
defines {"NDEBUG"}

View File

@ -0,0 +1,42 @@
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "motd.hpp"
#include <utils/hook.hpp>
#include <utils/http.hpp>
#include <utils/io.hpp>
#include <resource.hpp>
namespace motd
{
namespace
{
std::string motd_resource = utils::nt::load_resource(DW_MOTD);
std::future<std::optional<std::string>> motd_future;
}
std::string get_text()
{
try
{
return motd_future.get().value_or(motd_resource);
}
catch (std::exception&)
{
}
return motd_resource;
}
class component final : public component_interface
{
public:
void post_load() override
{
motd_future = utils::http::get_data_async("https://xlabs.dev/s1/motd.txt");
}
};
}
REGISTER_COMPONENT(motd::component)

View File

@ -0,0 +1,6 @@
#pragma once
namespace motd
{
std::string get_text();
}

View File

@ -3,6 +3,7 @@
#include <utils/io.hpp>
#include <utils/nt.hpp>
#include <utils/http.hpp>
#include <utils/toast.hpp>
#include <utils/binary_resource.hpp>
@ -29,50 +30,16 @@ namespace updater
{
utils::binary_resource s1x_icon(ICON_IMAGE, "s1x-icon.png");
std::string download_file_sync(const std::string& url)
{
CComPtr<IStream> stream;
if (FAILED(URLOpenBlockingStreamA(nullptr, url.data(), &stream, 0, nullptr)))
{
return {};
}
char buffer[0x1000];
std::string result;
HRESULT status{};
do
{
DWORD bytes_read = 0;
status = stream->Read(buffer, sizeof(buffer), &bytes_read);
if (bytes_read > 0)
{
result.append(buffer, bytes_read);
}
}
while (SUCCEEDED(status) && status != S_FALSE);
if (FAILED(status))
{
return {};
}
return result;
}
bool is_update_available()
{
const auto version = download_file_sync(APPVEYOR_VERSION_TXT);
return !version.empty() && version != GIT_HASH;
const auto version = utils::http::get_data(APPVEYOR_VERSION_TXT);
return version && !version->empty() && version != GIT_HASH;
}
void perform_update(const std::string& target)
{
const auto binary = download_file_sync(APPVEYOR_S1X_EXE);
utils::io::write_file(target, binary);
const auto binary = utils::http::get_data(APPVEYOR_S1X_EXE);
utils::io::write_file(target, *binary);
}
void delete_old_file(const std::string& file)

View File

@ -6,6 +6,7 @@
#include <utils/cryptography.hpp>
#include "game/game.hpp"
#include "component/motd.hpp"
namespace demonware
{
@ -17,7 +18,7 @@ namespace demonware
this->register_task(12, &bdStorage::get_user_file);
this->register_task(13, &bdStorage::unk13);
this->map_publisher_resource("motd-.*\\.txt", DW_MOTD);
this->map_publisher_resource_variant("motd-.*\\.txt", motd::get_text);
this->map_publisher_resource("ffotd-.*\\.ff", DW_FASTFILE);
this->map_publisher_resource("playlists(_.+)?\\.aggr", DW_PLAYLISTS);
this->map_publisher_resource("social_[Tt][Uu][0-9]+\\.cfg", DW_SOCIAL_CONFIG);
@ -30,7 +31,17 @@ namespace demonware
void bdStorage::map_publisher_resource(const std::string& expression, const INT id)
{
auto data = utils::nt::load_resource(id);
this->publisher_resources_.emplace_back(std::regex{expression}, data);
this->map_publisher_resource_variant(expression, std::move(data));
}
void bdStorage::map_publisher_resource_variant(const std::string& expression, resource_variant resource)
{
if (resource.valueless_by_exception())
{
throw std::runtime_error("Publisher resource variant is empty!");
}
this->publisher_resources_.emplace_back(std::regex{expression}, std::move(resource));
}
bool bdStorage::load_publisher_resource(const std::string& name, std::string& buffer)
@ -39,7 +50,15 @@ namespace demonware
{
if (std::regex_match(name, resource.first))
{
buffer = resource.second;
if (std::holds_alternative<std::string>(resource.second))
{
buffer = std::get<std::string>(resource.second);
}
else
{
buffer = std::get<callback>(resource.second)();
}
return true;
}
}

View File

@ -8,9 +8,12 @@ namespace demonware
bdStorage();
private:
std::vector<std::pair<std::regex, std::string>> publisher_resources_;
using callback = std::function<std::string()>;
using resource_variant = std::variant<std::string, callback>;
std::vector<std::pair<std::regex, resource_variant>> publisher_resources_;
void map_publisher_resource(const std::string& expression, INT id);
void map_publisher_resource_variant(const std::string& expression, resource_variant resource);
bool load_publisher_resource(const std::string& name, std::string& buffer);
void list_publisher_files(service_server* server, byte_buffer* buffer);

View File

@ -70,6 +70,7 @@
#include <sstream>
#include <optional>
#include <unordered_set>
#include <variant>
#include <gsl/gsl>
#include <udis86.h>

48
src/common/utils/http.cpp Normal file
View File

@ -0,0 +1,48 @@
#include "http.hpp"
#include "nt.hpp"
#include <atlcomcli.h>
namespace utils::http
{
std::optional<std::string> get_data(const std::string& url)
{
CComPtr<IStream> stream;
if (FAILED(URLOpenBlockingStreamA(nullptr, url.data(), &stream, 0, nullptr)))
{
return {};
}
char buffer[0x1000];
std::string result;
HRESULT status{};
do
{
DWORD bytes_read = 0;
status = stream->Read(buffer, sizeof(buffer), &bytes_read);
if (bytes_read > 0)
{
result.append(buffer, bytes_read);
}
}
while (SUCCEEDED(status) && status != S_FALSE);
if (FAILED(status))
{
return {};
}
return {result};
}
std::future<std::optional<std::string>> get_data_async(const std::string& url)
{
return std::async(std::launch::async, [url]()
{
return get_data(url);
});
}
}

11
src/common/utils/http.hpp Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include <string>
#include <optional>
#include <future>
namespace utils::http
{
std::optional<std::string> get_data(const std::string& url);
std::future<std::optional<std::string>> get_data_async(const std::string& url);
}