This commit is contained in:
2024-01-15 12:14:55 +01:00
commit 215030f626
294 changed files with 39223 additions and 0 deletions

View File

@ -0,0 +1,182 @@
#include <std_include.hpp>
#include "bit_buffer.hpp"
namespace demonware
{
bool bit_buffer::read_bytes(const unsigned int bytes, unsigned char* output)
{
return this->read(bytes * 8, output);
}
bool bit_buffer::read_bool(bool* output)
{
if (!this->read_data_type(1))
{
return false;
}
return this->read(1, output);
}
bool bit_buffer::read_uint32(unsigned int* output)
{
if (!this->read_data_type(8))
{
return false;
}
return this->read(32, output);
}
bool bit_buffer::read_data_type(const char expected)
{
char data_type = 0;
if (!this->use_data_types_) return true;
if (this->read(5, &data_type))
{
return (data_type == expected);
}
return false;
}
bool bit_buffer::write_bytes(const unsigned int bytes, const char* data)
{
return this->write_bytes(bytes, reinterpret_cast<const unsigned char*>(data));
}
bool bit_buffer::write_bytes(const unsigned int bytes, const unsigned char* data)
{
return this->write(bytes * 8, data);
}
bool bit_buffer::write_bool(bool data)
{
if (this->write_data_type(1))
{
return this->write(1, &data);
}
return false;
}
bool bit_buffer::write_int32(int data)
{
if (this->write_data_type(7))
{
return this->write(32, &data);
}
return false;
}
bool bit_buffer::write_uint32(unsigned int data)
{
if (this->write_data_type(8))
{
return this->write(32, &data);
}
return false;
}
bool bit_buffer::write_data_type(char data)
{
if (!this->use_data_types_)
{
return true;
}
return this->write(5, &data);
}
bool bit_buffer::read(unsigned int bits, void* output)
{
if (bits == 0) return false;
if ((this->current_bit_ + bits) > (this->buffer_.size() * 8)) return false;
int cur_byte = this->current_bit_ >> 3;
auto cur_out = 0;
const char* bytes = this->buffer_.data();
const auto output_bytes = static_cast<unsigned char*>(output);
while (bits > 0)
{
const int min_bit = (bits < 8) ? bits : 8;
const auto this_byte = bytes[cur_byte++] & 0xFF;
const int remain = this->current_bit_ & 7;
if ((min_bit + remain) <= 8)
{
output_bytes[cur_out] = BYTE((0xFF >> (8 - min_bit)) & (this_byte >> remain));
}
else
{
output_bytes[cur_out] = BYTE(
(0xFF >> (8 - min_bit)) & (bytes[cur_byte] << (8 - remain)) | (this_byte >> remain));
}
cur_out++;
this->current_bit_ += min_bit;
bits -= min_bit;
}
return true;
}
bool bit_buffer::write(const unsigned int bits, const void* data)
{
if (bits == 0) return false;
this->buffer_.resize(this->buffer_.size() + (bits >> 3) + 1);
int bit = bits;
const auto bytes = const_cast<char*>(this->buffer_.data());
const auto* input_bytes = static_cast<const unsigned char*>(data);
while (bit > 0)
{
const int bit_pos = this->current_bit_ & 7;
auto rem_bit = 8 - bit_pos;
const auto this_write = (bit < rem_bit) ? bit : rem_bit;
const BYTE mask = ((0xFF >> rem_bit) | (0xFF << (bit_pos + this_write)));
const int byte_pos = this->current_bit_ >> 3;
const BYTE temp_byte = (mask & bytes[byte_pos]);
const BYTE this_bit = ((bits - bit) & 7);
const auto this_byte = (bits - bit) >> 3;
auto this_data = input_bytes[this_byte];
const auto next_byte = (((bits - 1) >> 3) > this_byte) ? input_bytes[this_byte + 1] : 0;
this_data = BYTE((next_byte << (8 - this_bit)) | (this_data >> this_bit));
const BYTE out_byte = (~mask & (this_data << bit_pos) | temp_byte);
bytes[byte_pos] = out_byte;
this->current_bit_ += this_write;
bit -= this_write;
}
return true;
}
void bit_buffer::set_use_data_types(const bool use_data_types)
{
this->use_data_types_ = use_data_types;
}
unsigned int bit_buffer::size() const
{
return this->current_bit_ / 8 + (this->current_bit_ % 8 ? 1 : 0);
}
std::string& bit_buffer::get_buffer()
{
this->buffer_.resize(this->size());
return this->buffer_;
}
}

View File

@ -0,0 +1,40 @@
#pragma once
namespace demonware
{
class bit_buffer final
{
public:
bit_buffer() = default;
explicit bit_buffer(std::string buffer) : buffer_(std::move(buffer))
{
}
bool read_bytes(unsigned int bytes, unsigned char* output);
bool read_bool(bool* output);
bool read_uint32(unsigned int* output);
bool read_data_type(char expected);
bool write_bytes(unsigned int bytes, const char* data);
bool write_bytes(unsigned int bytes, const unsigned char* data);
bool write_bool(bool data);
bool write_int32(int data);
bool write_uint32(unsigned int data);
bool write_data_type(char data);
bool read(unsigned int bits, void* output);
bool write(unsigned int bits, const void* data);
void set_use_data_types(bool use_data_types);
unsigned int size() const;
std::string& get_buffer();
private:
std::string buffer_{};
unsigned int current_bit_ = 0;
bool use_data_types_ = true;
};
}

View File

@ -0,0 +1,308 @@
#include <std_include.hpp>
#include "byte_buffer.hpp"
namespace demonware
{
bool byte_buffer::read_byte(unsigned char* output)
{
if (!this->read_data_type(3)) return false;
return this->read(1, output);
}
bool byte_buffer::read_bool(bool* output)
{
if (!this->read_data_type(1)) return false;
return this->read(1, output);
}
bool byte_buffer::read_int16(short* output)
{
if (!this->read_data_type(5)) return false;
return this->read(2, output);
}
bool byte_buffer::read_uint16(unsigned short* output)
{
if (!this->read_data_type(6)) return false;
return this->read(2, output);
}
bool byte_buffer::read_int32(int* output)
{
if (!this->read_data_type(7)) return false;
return this->read(4, output);
}
bool byte_buffer::read_uint32(unsigned int* output)
{
if (!this->read_data_type(8)) return false;
return this->read(4, output);
}
bool byte_buffer::read_int64(__int64* output)
{
if (!this->read_data_type(9)) return false;
return this->read(8, output);
}
bool byte_buffer::read_uint64(unsigned __int64* output)
{
if (!this->read_data_type(10)) return false;
return this->read(8, output);
}
bool byte_buffer::read_float(float* output)
{
if (!this->read_data_type(13)) return false;
return this->read(4, output);
}
bool byte_buffer::read_string(std::string* output)
{
char* out_data;
if (this->read_string(&out_data))
{
output->clear();
output->append(out_data);
return true;
}
return false;
}
bool byte_buffer::read_string(char** output)
{
if (!this->read_data_type(16)) return false;
*output = const_cast<char*>(this->buffer_.data()) + this->current_byte_;
this->current_byte_ += strlen(*output) + 1;
return true;
}
bool byte_buffer::read_string(char* output, const int length)
{
if (!this->read_data_type(16)) return false;
strcpy_s(output, length, const_cast<char*>(this->buffer_.data()) + this->current_byte_);
this->current_byte_ += strlen(output) + 1;
return true;
}
bool byte_buffer::read_blob(std::string* output)
{
char* out_data;
int length;
if (this->read_blob(&out_data, &length))
{
output->clear();
output->append(out_data, length);
return true;
}
return false;
}
bool byte_buffer::read_blob(char** output, int* length)
{
if (!this->read_data_type(0x13))
{
return false;
}
unsigned int size;
this->read_uint32(&size);
*output = const_cast<char*>(this->buffer_.data()) + this->current_byte_;
*length = static_cast<int>(size);
this->current_byte_ += size;
return true;
}
bool byte_buffer::read_data_type(const char expected)
{
if (!this->use_data_types_) return true;
char type;
this->read(1, &type);
return type == expected;
}
bool byte_buffer::read_array_header(const unsigned char expected, unsigned int* element_count,
unsigned int* element_size)
{
if (element_count) *element_count = 0;
if (element_size) *element_size = 0;
if (!this->read_data_type(expected + 100)) return false;
uint32_t array_size, el_count;
if (!this->read_uint32(&array_size)) return false;
this->set_use_data_types(false);
this->read_uint32(&el_count);
this->set_use_data_types(true);
if (element_count) *element_count = el_count;
if (element_size) *element_size = array_size / el_count;
return true;
}
bool byte_buffer::write_byte(char data)
{
this->write_data_type(3);
return this->write(1, &data);
}
bool byte_buffer::write_bool(bool data)
{
this->write_data_type(1);
return this->write(1, &data);
}
bool byte_buffer::write_int16(short data)
{
this->write_data_type(5);
return this->write(2, &data);
}
bool byte_buffer::write_uint16(unsigned short data)
{
this->write_data_type(6);
return this->write(2, &data);
}
bool byte_buffer::write_int32(int data)
{
this->write_data_type(7);
return this->write(4, &data);
}
bool byte_buffer::write_uint32(unsigned int data)
{
this->write_data_type(8);
return this->write(4, &data);
}
bool byte_buffer::write_int64(__int64 data)
{
this->write_data_type(9);
return this->write(8, &data);
}
bool byte_buffer::write_uint64(unsigned __int64 data)
{
this->write_data_type(10);
return this->write(8, &data);
}
bool byte_buffer::write_data_type(char data)
{
if (!this->use_data_types_) return true;
return this->write(1, &data);
}
bool byte_buffer::write_float(float data)
{
this->write_data_type(13);
return this->write(4, &data);
}
bool byte_buffer::write_string(const std::string& data)
{
return this->write_string(data.data());
}
bool byte_buffer::write_string(const char* data)
{
this->write_data_type(16);
return this->write(static_cast<int>(strlen(data)) + 1, data);
}
bool byte_buffer::write_blob(const std::string& data)
{
return this->write_blob(data.data(), INT(data.size()));
}
bool byte_buffer::write_blob(const char* data, const int length)
{
this->write_data_type(0x13);
this->write_uint32(length);
return this->write(length, data);
}
bool byte_buffer::write_array_header(const unsigned char type, const unsigned int element_count,
const unsigned int element_size)
{
const auto using_types = this->is_using_data_types();
this->set_use_data_types(false);
auto result = this->write_byte(type + 100);
this->set_use_data_types(true);
result &= this->write_uint32(element_count * element_size);
this->set_use_data_types(false);
result &= this->write_uint32(element_count);
this->set_use_data_types(using_types);
return result;
}
bool byte_buffer::read(const int bytes, void* output)
{
if (bytes + this->current_byte_ > this->buffer_.size()) return false;
std::memmove(output, this->buffer_.data() + this->current_byte_, bytes);
this->current_byte_ += bytes;
return true;
}
bool byte_buffer::write(const int bytes, const void* data)
{
this->buffer_.append(static_cast<const char*>(data), bytes);
this->current_byte_ += bytes;
return true;
}
bool byte_buffer::write(const std::string& data)
{
return this->write(static_cast<int>(data.size()), data.data());
}
void byte_buffer::set_use_data_types(const bool use_data_types)
{
this->use_data_types_ = use_data_types;
}
size_t byte_buffer::size() const
{
return this->buffer_.size();
}
bool byte_buffer::is_using_data_types() const
{
return use_data_types_;
}
std::string& byte_buffer::get_buffer()
{
return this->buffer_;
}
std::string byte_buffer::get_remaining()
{
return std::string(this->buffer_.begin() + this->current_byte_, this->buffer_.end());
}
bool byte_buffer::has_more_data() const
{
return this->buffer_.size() > this->current_byte_;
}
}

View File

@ -0,0 +1,69 @@
#pragma once
namespace demonware
{
class byte_buffer final
{
public:
byte_buffer() = default;
explicit byte_buffer(std::string buffer) : buffer_(std::move(buffer))
{
}
bool read_byte(unsigned char* output);
bool read_bool(bool* output);
bool read_int16(short* output);
bool read_uint16(unsigned short* output);
bool read_int32(int* output);
bool read_uint32(unsigned int* output);
bool read_int64(__int64* output);
bool read_uint64(unsigned __int64* output);
bool read_float(float* output);
bool read_string(char** output);
bool read_string(char* output, int length);
bool read_string(std::string* output);
bool read_blob(char** output, int* length);
bool read_blob(std::string* output);
bool read_data_type(char expected);
bool read_array_header(unsigned char expected, unsigned int* element_count,
unsigned int* element_size = nullptr);
bool write_byte(char data);
bool write_bool(bool data);
bool write_int16(short data);
bool write_uint16(unsigned short data);
bool write_int32(int data);
bool write_uint32(unsigned int data);
bool write_int64(__int64 data);
bool write_uint64(unsigned __int64 data);
bool write_data_type(char data);
bool write_float(float data);
bool write_string(const char* data);
bool write_string(const std::string& data);
bool write_blob(const char* data, int length);
bool write_blob(const std::string& data);
bool write_array_header(unsigned char type, unsigned int element_count, unsigned int element_size);
bool read(int bytes, void* output);
bool write(int bytes, const void* data);
bool write(const std::string& data);
void set_use_data_types(bool use_data_types);
size_t size() const;
bool is_using_data_types() const;
std::string& get_buffer();
std::string get_remaining();
bool has_more_data() const;
private:
std::string buffer_;
size_t current_byte_ = 0;
bool use_data_types_ = true;
};
}

View File

@ -0,0 +1,144 @@
#pragma once
#include "byte_buffer.hpp"
namespace demonware
{
class bdTaskResult
{
public:
virtual ~bdTaskResult() = default;
virtual void serialize(byte_buffer*)
{
}
virtual void deserialize(byte_buffer*)
{
}
};
class bdFileData final : public bdTaskResult
{
public:
std::string file_data;
explicit bdFileData(std::string buffer) : file_data(std::move(buffer))
{
}
void serialize(byte_buffer* buffer) override
{
buffer->write_blob(this->file_data);
}
void deserialize(byte_buffer* buffer) override
{
buffer->read_blob(&this->file_data);
}
};
class bdFileInfo final : public bdTaskResult
{
public:
uint64_t file_id;
uint32_t create_time;
uint32_t modified_time;
bool priv;
uint64_t owner_id;
std::string filename;
uint32_t file_size;
void serialize(byte_buffer* buffer) override
{
buffer->write_uint32(this->file_size);
buffer->write_uint64(this->file_id);
buffer->write_uint32(this->create_time);
buffer->write_uint32(this->modified_time);
buffer->write_bool(this->priv);
buffer->write_uint64(this->owner_id);
buffer->write_string(this->filename);
}
void deserialize(byte_buffer* buffer) override
{
buffer->read_uint32(&this->file_size);
buffer->read_uint64(&this->file_id);
buffer->read_uint32(&this->create_time);
buffer->read_uint32(&this->modified_time);
buffer->read_bool(&this->priv);
buffer->read_uint64(&this->owner_id);
buffer->read_string(&this->filename);
}
};
class bdTimeStamp final : public bdTaskResult
{
public:
uint32_t unix_time;
void serialize(byte_buffer* buffer) override
{
buffer->write_uint32(this->unix_time);
}
void deserialize(byte_buffer* buffer) override
{
buffer->read_uint32(&this->unix_time);
}
};
class bdDMLInfo : public bdTaskResult
{
public:
std::string country_code; // Char [3]
std::string country; // Char [65]
std::string region; // Char [65]
std::string city; // Char [129]
float latitude;
float longitude;
void serialize(byte_buffer* buffer) override
{
buffer->write_string(this->country_code);
buffer->write_string(this->country);
buffer->write_string(this->region);
buffer->write_string(this->city);
buffer->write_float(this->latitude);
buffer->write_float(this->longitude);
}
void deserialize(byte_buffer* buffer) override
{
buffer->read_string(&this->country_code);
buffer->read_string(&this->country);
buffer->read_string(&this->region);
buffer->read_string(&this->city);
buffer->read_float(&this->latitude);
buffer->read_float(&this->longitude);
}
};
class bdDMLRawData final : public bdDMLInfo
{
public:
uint32_t asn; // Autonomous System Number.
std::string timezone;
void serialize(byte_buffer* buffer) override
{
bdDMLInfo::serialize(buffer);
buffer->write_uint32(this->asn);
buffer->write_string(this->timezone);
}
void deserialize(byte_buffer* buffer) override
{
bdDMLInfo::deserialize(buffer);
buffer->read_uint32(&this->asn);
buffer->read_string(&this->timezone);
}
};
}

View File

@ -0,0 +1,130 @@
#include <std_include.hpp>
#include "keys.hpp"
#include <utils/cryptography.hpp>
#include <utils/string.hpp>
namespace demonware
{
struct data_t
{
char m_session_key[24];
char m_response[8];
char m_hmac_key[20];
char m_enc_key[16];
char m_dec_key[16];
} data{};
std::string packet_buffer;
void calculate_hmacs_s1(const char* data_, const unsigned int data_size, const char* key,
const unsigned int key_size,
char* dst, const unsigned int dst_size)
{
char buffer[64];
unsigned int pos = 0;
unsigned int out_offset = 0;
char count = 1;
std::string result;
// buffer add key
std::memcpy(&buffer[pos], key, key_size);
pos += key_size;
// buffer add count
buffer[pos] = count;
pos++;
// calculate hmac
result = utils::cryptography::hmac_sha1::compute(std::string(buffer, pos), std::string(data_, data_size));
// save output
std::memcpy(dst, result.data(), std::min(20u, (dst_size - out_offset)));
out_offset = 20;
// second loop
while (true)
{
// if we filled the output buffer, exit
if (out_offset >= dst_size)
break;
// buffer add last result
pos = 0;
std::memcpy(&buffer[pos], result.data(), 20);
pos += 20;
// buffer add key
std::memcpy(&buffer[pos], key, key_size);
pos += key_size;
// buffer add count
count++;
buffer[pos] = count;
pos++;
// calculate hmac
result = utils::cryptography::hmac_sha1::compute(std::string(buffer, pos), std::string(data_, data_size));
// save output
std::memcpy(dst + out_offset, result.data(), std::min(20u, (dst_size - out_offset)));
out_offset += 20;
}
}
void derive_keys_s1()
{
const auto out_1 = utils::cryptography::sha1::compute(packet_buffer); // out_1 size 20
auto data_3 = utils::cryptography::hmac_sha1::compute(data.m_session_key, out_1);
char out_2[16];
calculate_hmacs_s1(data_3.data(), 20, "CLIENTCHAL", 10, out_2, 16);
char out_3[72];
calculate_hmacs_s1(data_3.data(), 20, "BDDATA", 6, out_3, 72);
std::memcpy(data.m_response, &out_2[8], 8);
std::memcpy(data.m_hmac_key, &out_3[20], 20);
std::memcpy(data.m_dec_key, &out_3[40], 16);
std::memcpy(data.m_enc_key, &out_3[56], 16);
#ifdef DEBUG
printf("[DW] Response id: %s\n", utils::string::dump_hex(std::string(&out_2[8], 8)).data());
printf("[DW] Hash verify: %s\n", utils::string::dump_hex(std::string(&out_3[20], 20)).data());
printf("[DW] AES dec key: %s\n", utils::string::dump_hex(std::string(&out_3[40], 16)).data());
printf("[DW] AES enc key: %s\n", utils::string::dump_hex(std::string(&out_3[56], 16)).data());
printf("[DW] Bravo 6, going dark.\n");
#endif
}
void queue_packet_to_hash(const std::string& packet)
{
packet_buffer.append(packet);
}
void set_session_key(const std::string& key)
{
std::memcpy(data.m_session_key, key.data(), 24);
}
std::string get_decrypt_key()
{
return std::string(data.m_dec_key, 16);
}
std::string get_encrypt_key()
{
return std::string(data.m_enc_key, 16);
}
std::string get_hmac_key()
{
return std::string(data.m_hmac_key, 20);
}
std::string get_response_id()
{
return std::string(data.m_response, 8);
}
}

View File

@ -0,0 +1,12 @@
#pragma once
namespace demonware
{
void derive_keys_s1();
void queue_packet_to_hash(const std::string& packet);
void set_session_key(const std::string& key);
std::string get_decrypt_key();
std::string get_encrypt_key();
std::string get_hmac_key();
std::string get_response_id();
}

View File

@ -0,0 +1,87 @@
#include <std_include.hpp>
#include "keys.hpp"
#include "reply.hpp"
#include "servers/service_server.hpp"
#include <utils/cryptography.hpp>
namespace demonware
{
std::string unencrypted_reply::data()
{
byte_buffer result;
result.set_use_data_types(false);
result.write_int32(static_cast<int>(this->buffer_.size()) + 2);
result.write_bool(false);
result.write_byte(this->type());
result.write(this->buffer_);
return result.get_buffer();
}
std::string encrypted_reply::data()
{
byte_buffer result;
result.set_use_data_types(false);
byte_buffer enc_buffer;
enc_buffer.set_use_data_types(false);
enc_buffer.write_uint32(static_cast<unsigned int>(this->buffer_.size())); // service data size CHECKTHIS!!
enc_buffer.write_byte(this->type()); // TASK_REPLY type
enc_buffer.write(this->buffer_); // service data
auto aligned_data = enc_buffer.get_buffer();
auto size = aligned_data.size();
size = ~15 & (size + 15); // 16 byte align
aligned_data.resize(size);
// seed
std::string seed("\x5E\xED\x5E\xED\x5E\xED\x5E\xED\x5E\xED\x5E\xED\x5E\xED\x5E\xED", 16);
// encrypt
const auto enc_data = utils::cryptography::aes::encrypt(aligned_data, seed, demonware::get_encrypt_key());
// header : encrypted service data : hash
static auto msg_count = 0;
msg_count++;
byte_buffer response;
response.set_use_data_types(false);
response.write_int32(30 + static_cast<int>(enc_data.size()));
response.write_byte(static_cast<char>(0xAB));
response.write_byte(static_cast<char>(0x85));
response.write_int32(msg_count);
response.write(16, seed.data());
response.write(enc_data);
// hash entire packet and append end
auto hash_data = utils::cryptography::hmac_sha1::compute(response.get_buffer(), demonware::get_hmac_key());
hash_data.resize(8);
response.write(8, hash_data.data());
return response.get_buffer();
}
void remote_reply::send(bit_buffer* buffer, const bool encrypted)
{
std::unique_ptr<typed_reply> reply;
if (encrypted) reply = std::make_unique<encrypted_reply>(this->type_, buffer);
else reply = std::make_unique<unencrypted_reply>(this->type_, buffer);
this->server_->send_reply(reply.get());
}
void remote_reply::send(byte_buffer* buffer, const bool encrypted)
{
std::unique_ptr<typed_reply> reply;
if (encrypted) reply = std::make_unique<encrypted_reply>(this->type_, buffer);
else reply = std::make_unique<unencrypted_reply>(this->type_, buffer);
this->server_->send_reply(reply.get());
}
}

View File

@ -0,0 +1,164 @@
#pragma once
#include "bit_buffer.hpp"
#include "byte_buffer.hpp"
#include "data_types.hpp"
namespace demonware
{
class reply
{
public:
reply() = default;
reply(reply&&) = delete;
reply(const reply&) = delete;
reply& operator=(reply&&) = delete;
reply& operator=(const reply&) = delete;
virtual ~reply() = default;
virtual std::string data() = 0;
};
class raw_reply : public reply
{
protected:
std::string buffer_;
public:
raw_reply() = default;
explicit raw_reply(std::string data) : buffer_(std::move(data))
{
}
std::string data() override
{
return this->buffer_;
}
};
class typed_reply : public raw_reply
{
public:
typed_reply(const uint8_t _type) : type_(_type)
{
}
protected:
uint8_t type() const { return this->type_; }
private:
uint8_t type_;
};
class encrypted_reply final : public typed_reply
{
public:
encrypted_reply(const uint8_t type, bit_buffer* bbuffer) : typed_reply(type)
{
this->buffer_.append(bbuffer->get_buffer());
}
encrypted_reply(const uint8_t type, byte_buffer* bbuffer) : typed_reply(type)
{
this->buffer_.append(bbuffer->get_buffer());
}
std::string data() override;
};
class unencrypted_reply final : public typed_reply
{
public:
unencrypted_reply(const uint8_t _type, bit_buffer* bbuffer) : typed_reply(_type)
{
this->buffer_.append(bbuffer->get_buffer());
}
unencrypted_reply(const uint8_t _type, byte_buffer* bbuffer) : typed_reply(_type)
{
this->buffer_.append(bbuffer->get_buffer());
}
std::string data() override;
};
class service_server;
class remote_reply final
{
public:
remote_reply(service_server* server, uint8_t _type) : type_(_type), server_(server)
{
}
void send(bit_buffer* buffer, bool encrypted);
void send(byte_buffer* buffer, bool encrypted);
uint8_t type() const { return this->type_; }
private:
uint8_t type_;
service_server* server_;
};
class service_reply final
{
public:
service_reply(service_server* _server, const uint8_t _type, const uint32_t _error)
: type_(_type), error_(_error), reply_(_server, 1)
{
}
uint64_t send()
{
static uint64_t id = 0x0000000000000000;
const auto transaction_id = ++id;
byte_buffer buffer;
buffer.write_uint64(transaction_id);
buffer.write_uint32(this->error_);
buffer.write_byte(this->type_);
if (!this->error_)
{
buffer.write_uint32(uint32_t(this->objects_.size()));
if (!this->objects_.empty())
{
buffer.write_uint32(uint32_t(this->objects_.size()));
for (auto& object : this->objects_)
{
object->serialize(&buffer);
}
this->objects_.clear();
}
}
else
{
buffer.write_uint64(transaction_id);
}
this->reply_.send(&buffer, true);
return transaction_id;
}
void add(const std::shared_ptr<bdTaskResult>& object)
{
this->objects_.push_back(object);
}
void add(bdTaskResult* object)
{
this->add(std::shared_ptr<bdTaskResult>(object));
}
private:
uint8_t type_;
uint32_t error_;
remote_reply reply_;
std::vector<std::shared_ptr<bdTaskResult>> objects_;
};
}

View File

@ -0,0 +1,60 @@
#pragma once
#include "servers/base_server.hpp"
#include <utils/cryptography.hpp>
namespace demonware
{
template <typename T>
class server_registry
{
static_assert(std::is_base_of<base_server, T>::value, "Invalid server registry type");
public:
template <typename S, typename ...Args>
void create(Args&&... args)
{
static_assert(std::is_base_of<T, S>::value, "Invalid server type");
auto server = std::make_unique<S>(std::forward<Args>(args)...);
const auto address = server->get_address();
servers_[address] = std::move(server);
}
void for_each(const std::function<void(T&)>& callback) const
{
for (auto& server : servers_)
{
callback(*server.second);
}
}
T* find(const std::string& name)
{
const auto address = utils::cryptography::jenkins_one_at_a_time::compute(name);
return find(address);
}
T* find(const uint32_t address)
{
const auto it = servers_.find(address);
if (it == servers_.end())
{
return nullptr;
}
return it->second.get();
}
void frame()
{
for (auto& server : servers_)
{
server.second->frame();
}
}
private:
std::unordered_map<uint32_t, std::unique_ptr<T>> servers_;
};
}

View File

@ -0,0 +1,167 @@
#include <std_include.hpp>
#include "auth3_server.hpp"
#include "../keys.hpp"
#include <utils/cryptography.hpp>
#include <utils/string.hpp>
namespace demonware
{
namespace
{
#pragma pack(push, 1)
struct auth_ticket
{
unsigned int m_magicNumber;
char m_type;
unsigned int m_titleID;
unsigned int m_timeIssued;
unsigned int m_timeExpires;
unsigned __int64 m_licenseID;
unsigned __int64 m_userID;
char m_username[64];
char m_sessionKey[24];
char m_usingHashMagicNumber[3];
char m_hash[4];
};
#pragma pack(pop)
}
void auth3_server::send_reply(reply* data)
{
if (!data) return;
this->send(data->data());
}
void auth3_server::handle(const std::string& packet)
{
if (packet.starts_with("POST /auth/"))
{
#ifdef DEBUG
printf("[DW]: [auth]: user requested authentication.\n");
#endif
return;
}
unsigned int title_id = 0;
unsigned int iv_seed = 0;
std::string identity{};
std::string token{};
rapidjson::Document j;
const rapidjson::ParseResult parse_result = j.Parse(packet);
if (!parse_result || !j.IsObject())
{
return;
}
if (j.HasMember("title_id") && j["title_id"].IsString())
{
title_id = std::stoul(j["title_id"].GetString());
}
if (j.HasMember("iv_seed") && j["iv_seed"].IsString())
{
iv_seed = std::stoul(j["iv_seed"].GetString());
}
if (j.HasMember("extra_data") && j["extra_data"].IsString())
{
rapidjson::Document extra_data;
auto& ed = j["extra_data"];
extra_data.Parse(ed.GetString(), ed.GetStringLength());
if (extra_data.HasMember("token") && extra_data["token"].IsString())
{
auto& token_field = extra_data["token"];
std::string token_b64(token_field.GetString(), token_field.GetStringLength());
token = utils::cryptography::base64::decode(token_b64);
}
}
#ifdef DEBUG
printf("[DW]: [auth]: authenticating user %s\n", token.data() + 64);
#endif
std::string auth_key(reinterpret_cast<char*>(token.data() + 32), 24);
std::string session_key(
"\x13\x37\x13\x37\x13\x37\x13\x37\x13\x37\x13\x37\x13\x37\x13\x37\x13\x37\x13\x37\x13\x37\x13\x37", 24);
// client_ticket
auth_ticket ticket{};
std::memset(&ticket, 0x0, sizeof ticket);
ticket.m_magicNumber = 0x0EFBDADDE;
ticket.m_type = 0;
ticket.m_titleID = title_id;
ticket.m_timeIssued = static_cast<uint32_t>(time(nullptr));
ticket.m_timeExpires = ticket.m_timeIssued + 30000;
ticket.m_licenseID = 0;
ticket.m_userID = reinterpret_cast<uint64_t>(token.data() + 56);
strncpy_s(ticket.m_username, sizeof(ticket.m_username), reinterpret_cast<char*>(token.data() + 64), 64);
std::memcpy(ticket.m_sessionKey, session_key.data(), 24);
const auto iv = utils::cryptography::tiger::compute(std::string(reinterpret_cast<char*>(&iv_seed), 4));
const auto ticket_enc = utils::cryptography::des3::encrypt(
std::string(reinterpret_cast<char*>(&ticket), sizeof(ticket)), iv, auth_key);
const auto ticket_b64 = utils::cryptography::base64::encode(
reinterpret_cast<const unsigned char*>(ticket_enc.data()), 128);
// server_ticket
uint8_t auth_data[128];
std::memset(&auth_data, 0, sizeof auth_data);
std::memcpy(auth_data, session_key.data(), 24);
const auto auth_data_b64 = utils::cryptography::base64::encode(auth_data, 128);
demonware::set_session_key(session_key);
// header time
char date[64];
const auto now = time(nullptr);
tm gmtm{};
gmtime_s(&gmtm, &now);
strftime(date, 64, "%a, %d %b %G %T", &gmtm);
// json content
rapidjson::Document doc;
doc.SetObject();
doc.AddMember("auth_task", "29", doc.GetAllocator());
doc.AddMember("code", "700", doc.GetAllocator());
auto seed = std::to_string(iv_seed);
doc.AddMember("iv_seed", rapidjson::StringRef(seed.data(), seed.size()), doc.GetAllocator());
doc.AddMember("client_ticket", rapidjson::StringRef(ticket_b64.data(), ticket_b64.size()), doc.GetAllocator());
doc.AddMember("server_ticket", rapidjson::StringRef(auth_data_b64.data(), auth_data_b64.size()),
doc.GetAllocator());
doc.AddMember("client_id", "", doc.GetAllocator());
doc.AddMember("account_type", "steam", doc.GetAllocator());
doc.AddMember("crossplay_enabled", false, doc.GetAllocator());
doc.AddMember("loginqueue_eanbled", false, doc.GetAllocator());
rapidjson::Value value{};
doc.AddMember("lsg_endpoint", value, doc.GetAllocator());
rapidjson::StringBuffer buffer{};
rapidjson::Writer<rapidjson::StringBuffer, rapidjson::Document::EncodingType, rapidjson::ASCII<>>
writer(buffer);
doc.Accept(writer);
// http stuff
std::string result;
result.append("HTTP/1.1 200 OK\r\n");
result.append("Server: TornadoServer/4.5.3\r\n");
result.append("Content-Type: application/json\r\n");
result.append(utils::string::va("Date: %s GMT\r\n", date));
result.append(utils::string::va("Content-Length: %d\r\n\r\n", buffer.GetLength()));
result.append(buffer.GetString(), buffer.GetLength());
raw_reply reply(result);
this->send_reply(&reply);
#ifdef DEBUG
printf("[DW]: [auth]: user successfully authenticated.\n");
#endif
}
}

View File

@ -0,0 +1,16 @@
#pragma once
#include "tcp_server.hpp"
#include "../reply.hpp"
namespace demonware
{
class auth3_server : public tcp_server
{
public:
using tcp_server::tcp_server;
private:
void send_reply(reply* data);
void handle(const std::string& packet) override;
};
}

View File

@ -0,0 +1,22 @@
#include <std_include.hpp>
#include "base_server.hpp"
#include <utils/cryptography.hpp>
namespace demonware
{
base_server::base_server(std::string name): name_(std::move(name))
{
this->address_ = utils::cryptography::jenkins_one_at_a_time::compute(this->name_);
}
const std::string& base_server::get_name() const
{
return this->name_;
}
uint32_t base_server::get_address() const
{
return this->address_;
}
}

View File

@ -0,0 +1,30 @@
#pragma once
namespace demonware
{
class base_server
{
public:
using stream_queue = std::queue<char>;
using data_queue = std::queue<std::string>;
base_server(std::string name);
base_server(base_server&&) = delete;
base_server(const base_server&) = delete;
base_server& operator=(base_server&&) = delete;
base_server& operator=(const base_server&) = delete;
virtual ~base_server() = default;
const std::string& get_name() const;
uint32_t get_address() const;
virtual void frame() = 0;
private:
std::string name_;
std::uint32_t address_ = 0;
};
}

View File

@ -0,0 +1,176 @@
#include <std_include.hpp>
#include "lobby_server.hpp"
#include "../services.hpp"
#include "../keys.hpp"
#include <utils/cryptography.hpp>
namespace demonware
{
lobby_server::lobby_server(std::string name) : tcp_server(std::move(name))
{
this->register_service<bdAnticheat>();
this->register_service<bdContentStreaming>();
this->register_service<bdDML>();
this->register_service<bdEventLog>();
this->register_service<bdGroups>();
this->register_service<bdStats>();
this->register_service<bdStorage>();
this->register_service<bdTitleUtilities>();
this->register_service<bdProfiles>();
this->register_service<bdRichPresence>();
this->register_service<bdFacebook>();
this->register_service<bdUNK63>();
this->register_service<bdUNK80>();
this->register_service<bdPresence>();
this->register_service<bdMarketingComms>();
this->register_service<bdMatchMaking2>();
this->register_service<bdMarketing>();
};
void lobby_server::send_reply(reply* data)
{
if (!data) return;
this->send(data->data());
}
void lobby_server::handle(const std::string& packet)
{
byte_buffer buffer(packet);
buffer.set_use_data_types(false);
try
{
while (buffer.has_more_data())
{
int size;
buffer.read_int32(&size);
if (size <= 0)
{
const std::string zero("\x00\x00\x00\x00", 4);
raw_reply reply(zero);
this->send_reply(&reply);
return;
}
else if (size == 0xC8)
{
#ifdef DEBUG
printf("[DW]: [lobby]: received client_header_ack.\n");
#endif
int c8;
buffer.read_int32(&c8);
std::string packet_1 = buffer.get_remaining();
demonware::queue_packet_to_hash(packet_1);
const std::string packet_2(
"\x16\x00\x00\x00\xab\x81\xd2\x00\x00\x00\x13\x37\x13\x37\x13\x37\x13\x37\x13\x37\x13\x37\x13\x37\x13\x37",
26);
demonware::queue_packet_to_hash(packet_2);
raw_reply reply(packet_2);
this->send_reply(&reply);
#ifdef DEBUG
printf("[DW]: [lobby]: sending server_header_ack.\n");
#endif
return;
}
if (buffer.size() < size_t(size)) return;
uint8_t check_ab;
buffer.read_byte(&check_ab);
if (check_ab == 0xAB)
{
uint8_t type;
buffer.read_byte(&type);
if (type == 0x82)
{
#ifdef DEBUG
printf("[DW]: [lobby]: received client_auth.\n");
#endif
std::string packet_3(packet.data(), packet.size() - 8); // this 8 are client hash check?
demonware::queue_packet_to_hash(packet_3);
demonware::derive_keys_s1();
char buff[14] = "\x0A\x00\x00\x00\xAB\x83";
std::memcpy(&buff[6], demonware::get_response_id().data(), 8);
std::string response(buff, 14);
raw_reply reply(response);
this->send_reply(&reply);
#ifdef DEBUG
printf("[DW]: [lobby]: sending server_auth_done.\n");
#endif
return;
}
else if (type == 0x85)
{
uint32_t msg_count;
buffer.read_uint32(&msg_count);
char seed[16];
buffer.read(16, &seed);
std::string enc = buffer.get_remaining();
char hash[8];
std::memcpy(hash, &(enc.data()[enc.size() - 8]), 8);
std::string dec = utils::cryptography::aes::decrypt(
std::string(enc.data(), enc.size() - 8), std::string(seed, 16),
demonware::get_decrypt_key());
byte_buffer serv(dec);
serv.set_use_data_types(false);
uint32_t serv_size;
serv.read_uint32(&serv_size);
uint8_t magic; // 0x86
serv.read_byte(&magic);
uint8_t service_id;
serv.read_byte(&service_id);
this->call_service(service_id, serv.get_remaining());
return;
}
}
printf("[DW]: [lobby]: ERROR! received unk message.\n");
return;
}
}
catch (...)
{
}
}
void lobby_server::call_service(const uint8_t id, const std::string& data)
{
const auto& it = this->services_.find(id);
if (it != this->services_.end())
{
it->second->exec_task(this, data);
}
else
{
printf("[DW]: [lobby]: missing service '%s'\n", utils::string::va("%d", id));
// return no error
byte_buffer buffer(data);
uint8_t task_id;
buffer.read_byte(&task_id);
this->create_reply(task_id)->send();
}
}
}

View File

@ -0,0 +1,33 @@
#pragma once
#include "tcp_server.hpp"
#include "service_server.hpp"
#include "../service.hpp"
namespace demonware
{
class lobby_server : public tcp_server, service_server
{
public:
lobby_server(std::string name);
template <typename T>
void register_service()
{
static_assert(std::is_base_of<service, T>::value, "service must inherit from service");
auto service = std::make_unique<T>();
const uint8_t id = service->id();
this->services_[id] = std::move(service);
}
void send_reply(reply* data) override;
private:
std::unordered_map<uint8_t, std::unique_ptr<service>> services_;
void handle(const std::string& packet) override;
void call_service(uint8_t id, const std::string& data);
};
}

View File

@ -0,0 +1,27 @@
#pragma once
#include "../reply.hpp"
namespace demonware
{
class service_server
{
public:
virtual ~service_server() = default;
virtual std::shared_ptr<remote_reply> create_message(uint8_t type)
{
auto reply = std::make_shared<remote_reply>(this, type);
return reply;
}
virtual std::shared_ptr<service_reply> create_reply(uint8_t type, uint32_t error = 0)
{
auto reply = std::make_shared<service_reply>(this, type, error);
return reply;
}
virtual void send_reply(reply* data) = 0;
};
}

View File

@ -0,0 +1,62 @@
#include <std_include.hpp>
#include "stun_server.hpp"
#include "../byte_buffer.hpp"
namespace demonware
{
void stun_server::handle(const endpoint_data& endpoint, const std::string& packet)
{
uint8_t type, version, padding;
byte_buffer buffer(packet);
buffer.set_use_data_types(false);
buffer.read_byte(&type);
buffer.read_byte(&version);
buffer.read_byte(&padding);
switch (type)
{
case 30:
this->ip_discovery(endpoint);
break;
case 20:
this->nat_discovery(endpoint);
break;
default:
break;
}
}
void stun_server::ip_discovery(const endpoint_data& endpoint)
{
const uint32_t ip = 0x0100007f;
byte_buffer buffer;
buffer.set_use_data_types(false);
buffer.write_byte(31); // type
buffer.write_byte(2); // version
buffer.write_byte(0); // version
buffer.write_uint32(ip); // external ip
buffer.write_uint16(3074); // port
this->send(endpoint, buffer.get_buffer());
}
void stun_server::nat_discovery(const endpoint_data& endpoint)
{
const uint32_t ip = 0x0100007f;
byte_buffer buffer;
buffer.set_use_data_types(false);
buffer.write_byte(21); // type
buffer.write_byte(2); // version
buffer.write_byte(0); // version
buffer.write_uint32(ip); // external ip
buffer.write_uint16(3074); // port
buffer.write_uint32(this->get_address()); // server ip
buffer.write_uint16(3074); // server port
this->send(endpoint, buffer.get_buffer());
}
}

View File

@ -0,0 +1,18 @@
#pragma once
#include "udp_server.hpp"
namespace demonware
{
class stun_server : public udp_server
{
public:
using udp_server::udp_server;
private:
void handle(const endpoint_data& endpoint, const std::string& packet) override;
void ip_discovery(const endpoint_data& endpoint);
void nat_discovery(const endpoint_data& endpoint);
};
}

View File

@ -0,0 +1,84 @@
#include <std_include.hpp>
#include "tcp_server.hpp"
namespace demonware
{
void tcp_server::handle_input(const char* buf, size_t size)
{
in_queue_.access([&](data_queue& queue)
{
queue.emplace(buf, size);
});
}
size_t tcp_server::handle_output(char* buf, size_t size)
{
if (out_queue_.get_raw().empty())
{
return 0;
}
return out_queue_.access<size_t>([&](stream_queue& queue)
{
for (size_t i = 0; i < size; ++i)
{
if (queue.empty())
{
return i;
}
buf[i] = queue.front();
queue.pop();
}
return size;
});
}
bool tcp_server::pending_data()
{
return !this->out_queue_.get_raw().empty();
}
void tcp_server::frame()
{
if (this->in_queue_.get_raw().empty())
{
return;
}
while (true)
{
std::string packet{};
const auto result = this->in_queue_.access<bool>([&](data_queue& queue)
{
if (queue.empty())
{
return false;
}
packet = std::move(queue.front());
queue.pop();
return true;
});
if (!result)
{
break;
}
this->handle(packet);
}
}
void tcp_server::send(const std::string& data)
{
out_queue_.access([&](stream_queue& queue)
{
for (const auto& val : data)
{
queue.push(val);
}
});
}
}

View File

@ -0,0 +1,27 @@
#pragma once
#include "base_server.hpp"
#include <utils/concurrency.hpp>
namespace demonware
{
class tcp_server : public base_server
{
public:
using base_server::base_server;
void handle_input(const char* buf, size_t size);
size_t handle_output(char* buf, size_t size);
bool pending_data();
void frame() override;
protected:
virtual void handle(const std::string& data) = 0;
void send(const std::string& data);
private:
utils::concurrency::container<data_queue> in_queue_;
utils::concurrency::container<stream_queue> out_queue_;
};
}

View File

@ -0,0 +1,103 @@
#include <std_include.hpp>
#include "udp_server.hpp"
namespace demonware
{
void udp_server::handle_input(const char* buf, size_t size, endpoint_data endpoint)
{
in_queue_.access([&](in_queue& queue)
{
in_packet p;
p.data = std::string{buf, size};
p.endpoint = std::move(endpoint);
queue.emplace(std::move(p));
});
}
size_t udp_server::handle_output(SOCKET socket, char* buf, size_t size, sockaddr* address, int* addrlen)
{
return out_queue_.access<size_t>([&](socket_queue_map& map) -> size_t
{
const auto entry = map.find(socket);
if (entry == map.end())
{
return 0;
}
auto& queue = entry->second;
if (queue.empty())
{
return 0;
}
auto data = std::move(queue.front());
queue.pop();
const auto copy_size = std::min(size, data.data.size());
std::memcpy(buf, data.data.data(), copy_size);
std::memcpy(address, &data.address, sizeof(data.address));
*addrlen = sizeof(data.address);
return copy_size;
});
}
bool udp_server::pending_data(SOCKET socket)
{
return this->out_queue_.access<bool>([&](const socket_queue_map& map)
{
const auto entry = map.find(socket);
if (entry == map.end())
{
return false;
}
return !entry->second.empty();
});
}
void udp_server::send(const endpoint_data& endpoint, std::string data)
{
out_queue_.access([&](socket_queue_map& map)
{
out_packet p;
p.data = std::move(data);
p.address = endpoint.address;
map[endpoint.socket].emplace(std::move(p));
});
}
void udp_server::frame()
{
if (this->in_queue_.get_raw().empty())
{
return;
}
while (true)
{
in_packet packet{};
const auto result = this->in_queue_.access<bool>([&](in_queue& queue)
{
if (queue.empty())
{
return false;
}
packet = std::move(queue.front());
queue.pop();
return true;
});
if (!result)
{
break;
}
this->handle(packet.endpoint, std::move(packet.data));
}
}
}

View File

@ -0,0 +1,62 @@
#pragma once
#include "base_server.hpp"
#include <utils/concurrency.hpp>
namespace demonware
{
class udp_server : public base_server
{
public:
struct endpoint_data
{
SOCKET socket{};
sockaddr_in address{};
endpoint_data() = default;
endpoint_data(const SOCKET sock, const sockaddr* addr, const int size)
{
if (size != sizeof(this->address))
{
throw std::runtime_error("Invalid size");
}
this->socket = sock;
std::memcpy(&this->address, addr, sizeof(this->address));
}
};
using base_server::base_server;
void handle_input(const char* buf, size_t size, endpoint_data endpoint);
size_t handle_output(SOCKET socket, char* buf, size_t size, sockaddr* address, int* addrlen);
bool pending_data(SOCKET socket);
void frame() override;
protected:
virtual void handle(const endpoint_data& endpoint, const std::string& data) = 0;
void send(const endpoint_data& endpoint, std::string data);
private:
struct in_packet
{
std::string data;
endpoint_data endpoint;
};
struct out_packet
{
std::string data;
sockaddr_in address;
};
using in_queue = std::queue<in_packet>;
using out_queue = std::queue<out_packet>;
using socket_queue_map = std::unordered_map<SOCKET, out_queue>;
utils::concurrency::container<in_queue> in_queue_;
utils::concurrency::container<socket_queue_map> out_queue_;
};
}

View File

@ -0,0 +1,11 @@
#include <std_include.hpp>
#include "umbrella_server.hpp"
namespace demonware
{
void umbrella_server::handle(const std::string& packet)
{
// TODO:
}
}

View File

@ -0,0 +1,14 @@
#pragma once
#include "tcp_server.hpp"
namespace demonware
{
class umbrella_server : public tcp_server
{
public:
using tcp_server::tcp_server;
private:
void handle(const std::string& packet) override;
};
}

View File

@ -0,0 +1,89 @@
#pragma once
#include <utils/string.hpp>
#include "servers/service_server.hpp"
namespace demonware
{
class service
{
using callback_t = std::function<void(service_server*, byte_buffer*)>;
uint8_t id_;
std::string name_;
std::mutex mutex_;
uint8_t task_id_;
std::map<uint8_t, callback_t> tasks_;
public:
virtual ~service() = default;
service(service&&) = delete;
service(const service&) = delete;
service& operator=(const service&) = delete;
service(const uint8_t id, std::string name) : id_(id), name_(std::move(name)), task_id_(0)
{
}
uint8_t id() const
{
return this->id_;
}
const std::string& name() const
{
return this->name_;
}
uint8_t task_id() const
{
return this->task_id_;
}
virtual void exec_task(service_server* server, const std::string& data)
{
std::lock_guard<std::mutex> _(this->mutex_);
byte_buffer buffer(data);
buffer.read_byte(&this->task_id_);
const auto& it = this->tasks_.find(this->task_id_);
if (it != this->tasks_.end())
{
#ifdef DEBUG
printf("[DW] %s: executing task '%d'\n", name_.data(), this->task_id_);
#endif
it->second(server, &buffer);
}
else
{
printf("[DW] %s: missing task '%d'\n", name_.data(), this->task_id_);
// return no error
server->create_reply(this->task_id_)->send();
}
}
protected:
template <typename Class, typename T, typename... Args>
void register_task(const uint8_t id, T (Class::* callback)(Args ...) const)
{
this->tasks_[id] = [this, callback](Args ... args) -> T
{
return (reinterpret_cast<Class*>(this)->*callback)(args...);
};
}
template <typename Class, typename T, typename... Args>
void register_task(const uint8_t id, T (Class::* callback)(Args ...))
{
this->tasks_[id] = [this, callback](Args ... args) -> T
{
return (reinterpret_cast<Class*>(this)->*callback)(args...);
};
}
};
}

View File

@ -0,0 +1,34 @@
#pragma once
#include "bit_buffer.hpp"
#include "byte_buffer.hpp"
#include "data_types.hpp"
#include "reply.hpp"
#include "service.hpp"
#include "servers/service_server.hpp"
//#include "services/bdTeams.hpp" // 3
#include "services/bdStats.hpp" // 4
//#include "services/bdMessaging.hpp" // 6
#include "services/bdProfiles.hpp" // 8
#include "services/bdStorage.hpp" // 10
#include "services/bdTitleUtilities.hpp" // 12
//#include "services/bdMatchMaking.hpp" // 21
#include "services/bdCounters.hpp" // 23
#include "services/bdDML.hpp" // 27
#include "services/bdGroups.hpp" // 28
//#include "services/bdCMail.hpp" // 29
#include "services/bdFacebook.hpp" // 36
#include "services/bdAnticheat.hpp" // 38
#include "services/bdContentStreaming.hpp" // 50
//#include "services/bdTags.hpp" // 52
#include "services/bdUNK63.hpp" // 63
#include "services/bdEventLog.hpp" // 67
#include "services/bdRichPresence.hpp" // 68
//#include "services/bdTitleUtilities2.hpp" // 72
#include "services/bdUNK80.hpp"
// AccountLinking // 86
#include "services/bdPresence.hpp" //103
#include "services/bdMarketingComms.hpp" //104
#include "services/bdMatchMaking2.hpp" //138
#include "services/bdMarketing.hpp" //139

View File

@ -0,0 +1,25 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdAnticheat::bdAnticheat() : service(38, "bdAnticheat")
{
this->register_task(2, &bdAnticheat::unk2);
this->register_task(4, &bdAnticheat::report_console_details);
}
void bdAnticheat::unk2(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO: Read data as soon as needed
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdAnticheat::report_console_details(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO: Read data as soon as needed
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,14 @@
#pragma once
namespace demonware
{
class bdAnticheat final : public service
{
public:
bdAnticheat();
private:
void unk2(service_server* server, byte_buffer* buffer) const;
void report_console_details(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,25 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdContentStreaming::bdContentStreaming() : service(50, "bdContentStreaming")
{
this->register_task(2, &bdContentStreaming::unk2);
this->register_task(3, &bdContentStreaming::unk3);
}
void bdContentStreaming::unk2(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdContentStreaming::unk3(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,14 @@
#pragma once
namespace demonware
{
class bdContentStreaming final : public service
{
public:
bdContentStreaming();
private:
void unk2(service_server* server, byte_buffer* buffer) const;
void unk3(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,25 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdCounters::bdCounters() : service(23, "bdCounters")
{
this->register_task(1, &bdCounters::unk1);
this->register_task(2, &bdCounters::unk2);
}
void bdCounters::unk1(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdCounters::unk2(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,14 @@
#pragma once
namespace demonware
{
class bdCounters final : public service
{
public:
bdCounters();
private:
void unk1(service_server* server, byte_buffer* buffer) const;
void unk2(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,28 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdDML::bdDML() : service(27, "bdDML")
{
this->register_task(2, &bdDML::get_user_raw_data);
}
void bdDML::get_user_raw_data(service_server* server, byte_buffer* /*buffer*/) const
{
auto result = new bdDMLRawData;
result->country_code = "US";
result->country_code = "'Murica";
result->region = "New York";
result->city = "New York";
result->latitude = 0;
result->longitude = 0;
result->asn = 0x2119;
result->timezone = "+01:00";
auto reply = server->create_reply(this->task_id());
reply->add(result);
reply->send();
}
}

View File

@ -0,0 +1,13 @@
#pragma once
namespace demonware
{
class bdDML final : public service
{
public:
bdDML();
private:
void get_user_raw_data(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,17 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdEventLog::bdEventLog() : service(67, "bdEventLog")
{
this->register_task(6, &bdEventLog::unk6);
}
void bdEventLog::unk6(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,13 @@
#pragma once
namespace demonware
{
class bdEventLog final : public service
{
public:
bdEventLog();
private:
void unk6(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,41 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdFacebook::bdFacebook() : service(36, "bdFacebook")
{
this->register_task(1, &bdFacebook::unk1);
this->register_task(3, &bdFacebook::unk3);
this->register_task(7, &bdFacebook::unk7);
this->register_task(8, &bdFacebook::unk8);
}
void bdFacebook::unk1(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdFacebook::unk3(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdFacebook::unk7(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdFacebook::unk8(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,16 @@
#pragma once
namespace demonware
{
class bdFacebook final : public service
{
public:
bdFacebook();
private:
void unk1(service_server* server, byte_buffer* buffer) const;
void unk3(service_server* server, byte_buffer* buffer) const;
void unk7(service_server* server, byte_buffer* buffer) const;
void unk8(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,25 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdGroups::bdGroups() : service(28, "bdGroup")
{
this->register_task(1, &bdGroups::set_groups);
this->register_task(4, &bdGroups::unk4);
}
void bdGroups::set_groups(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdGroups::unk4(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,14 @@
#pragma once
namespace demonware
{
class bdGroups final : public service
{
public:
bdGroups();
private:
void set_groups(service_server* server, byte_buffer* buffer) const;
void unk4(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,25 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdMarketing::bdMarketing() : service(139, "bdMarketing")
{
this->register_task(2, &bdMarketing::unk2);
this->register_task(3, &bdMarketing::unk3);
}
void bdMarketing::unk2(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdMarketing::unk3(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,14 @@
#pragma once
namespace demonware
{
class bdMarketing final : public service
{
public:
bdMarketing();
private:
void unk2(service_server* server, byte_buffer* buffer) const;
void unk3(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,17 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdMarketingComms::bdMarketingComms() : service(104, "bdMarketingComms")
{
this->register_task(1, &bdMarketingComms::get_messages);
}
void bdMarketingComms::get_messages(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,13 @@
#pragma once
namespace demonware
{
class bdMarketingComms final : public service
{
public:
bdMarketingComms();
private:
void get_messages(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,49 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdMatchMaking2::bdMatchMaking2() : service(138, "bdMatchMaking2")
{
this->register_task(1, &bdMatchMaking2::unk1);
this->register_task(2, &bdMatchMaking2::unk2);
this->register_task(3, &bdMatchMaking2::unk3);
this->register_task(5, &bdMatchMaking2::unk5);
this->register_task(16, &bdMatchMaking2::unk16);
}
void bdMatchMaking2::unk1(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdMatchMaking2::unk2(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdMatchMaking2::unk3(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdMatchMaking2::unk5(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdMatchMaking2::unk16(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,17 @@
#pragma once
namespace demonware
{
class bdMatchMaking2 final : public service
{
public:
bdMatchMaking2();
private:
void unk1(service_server* server, byte_buffer* buffer) const;
void unk2(service_server* server, byte_buffer* buffer) const;
void unk3(service_server* server, byte_buffer* buffer) const;
void unk5(service_server* server, byte_buffer* buffer) const;
void unk16(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,25 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdPresence::bdPresence() : service(103, "bdPresence")
{
this->register_task(1, &bdPresence::unk1);
this->register_task(3, &bdPresence::unk3);
}
void bdPresence::unk1(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdPresence::unk3(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,14 @@
#pragma once
namespace demonware
{
class bdPresence final : public service
{
public:
bdPresence();
private:
void unk1(service_server* server, byte_buffer* buffer) const;
void unk3(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,17 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdProfiles::bdProfiles() : service(8, "bdProfiles")
{
this->register_task(3, &bdProfiles::unk3);
}
void bdProfiles::unk3(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,13 @@
#pragma once
namespace demonware
{
class bdProfiles final : public service
{
public:
bdProfiles();
private:
void unk3(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,25 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdRichPresence::bdRichPresence() : service(68, "bdRichPresence")
{
this->register_task(1, &bdRichPresence::unk1);
this->register_task(2, &bdRichPresence::unk2);
}
void bdRichPresence::unk1(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdRichPresence::unk2(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,14 @@
#pragma once
namespace demonware
{
class bdRichPresence final : public service
{
public:
bdRichPresence();
private:
void unk1(service_server* server, byte_buffer* buffer) const;
void unk2(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,49 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdStats::bdStats() : service(4, "bdStats")
{
this->register_task(1, &bdStats::unk1);
this->register_task(3, &bdStats::unk3); // leaderboards
this->register_task(4, &bdStats::unk4);
this->register_task(8, &bdStats::unk8);
this->register_task(11, &bdStats::unk11);
}
void bdStats::unk1(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdStats::unk3(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdStats::unk4(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdStats::unk8(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdStats::unk11(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,17 @@
#pragma once
namespace demonware
{
class bdStats final : public service
{
public:
bdStats();
private:
void unk1(service_server* server, byte_buffer* buffer) const;
void unk3(service_server* server, byte_buffer* buffer) const;
void unk4(service_server* server, byte_buffer* buffer) const;
void unk8(service_server* server, byte_buffer* buffer) const;
void unk11(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,211 @@
#include <std_include.hpp>
#include "game/game.hpp"
#include "../services.hpp"
#include <utils/nt.hpp>
#include <utils/io.hpp>
#include <utils/cryptography.hpp>
#include <component/filesystem.hpp>
namespace demonware
{
namespace
{
std::string get_motd_text()
{
return "This is not a copy & pasted client";
}
}
bdStorage::bdStorage() : service(10, "bdStorage")
{
this->register_task(6, &bdStorage::list_publisher_files);
this->register_task(7, &bdStorage::get_publisher_file);
this->register_task(10, &bdStorage::set_user_file);
this->register_task(12, &bdStorage::get_user_file);
this->register_task(13, &bdStorage::unk13);
this->map_publisher_resource_variant(".*\\motd-.*\\.txt", get_motd_text);
this->map_publisher_resource("ffotd-.*\\.ff", "dw/ffotd-1.22.1.ff", DW_FASTFILE);
this->map_publisher_resource("playlists(_.+)?\\.aggr", "dw/playlists_tu22.aggr", DW_PLAYLISTS);
this->map_publisher_resource("social_[Tt][Uu][0-9]+\\.cfg", "dw/social_tu22.cfg", DW_SOCIAL_CONFIG);
this->map_publisher_resource("mm\\.cfg", "dw/mm.cfg", DW_MM_CONFIG);
this->map_publisher_resource("entitlement_config\\.info", "dw/entitlement_config.info", DW_ENTITLEMENT_CONFIG);
this->map_publisher_resource("lootConfig_[Tt][Uu][0-9]+\\.csv", "dw/lootConfig_tu22.csv", DW_LOOT_CONFIG);
this->map_publisher_resource("winStoreConfig_[Tt][Uu][0-9]+\\.csv", "dw/winStoreConfig_tu22.csv", DW_STORE_CONFIG);
}
void bdStorage::map_publisher_resource(const std::string& expression, const std::string& path, const int id)
{
auto data = filesystem::exists(path)
? filesystem::read_file(path)
: utils::nt::load_resource(id);
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) const
{
for (const auto& resource : this->publisher_resources_)
{
if (std::regex_match(name, resource.first))
{
if (std::holds_alternative<std::string>(resource.second))
{
buffer = std::get<std::string>(resource.second);
}
else
{
buffer = std::get<callback>(resource.second)();
}
return true;
}
}
#ifdef DEBUG
printf("[DW]: [bdStorage]: missing publisher file: %s\n", name.data());
#endif
return false;
}
void bdStorage::list_publisher_files(service_server* server, byte_buffer* buffer)
{
uint32_t date;
uint16_t num_results, offset;
std::string filename, data;
buffer->read_uint32(&date);
buffer->read_uint16(&num_results);
buffer->read_uint16(&offset);
buffer->read_string(&filename);
auto reply = server->create_reply(this->task_id());
if (this->load_publisher_resource(filename, data))
{
auto* info = new bdFileInfo;
info->file_id = *reinterpret_cast<const uint64_t*>(utils::cryptography::sha1::compute(filename).data());
info->filename = filename;
info->create_time = 0;
info->modified_time = info->create_time;
info->file_size = uint32_t(data.size());
info->owner_id = 0;
info->priv = false;
reply->add(info);
}
reply->send();
}
void bdStorage::get_publisher_file(service_server* server, byte_buffer* buffer)
{
std::string filename;
buffer->read_string(&filename);
#ifdef DEBUG
printf("[DW]: [bdStorage]: loading publisher file: %s\n", filename.data());
#endif
std::string data;
if (this->load_publisher_resource(filename, data))
{
#ifdef DEBUG
printf("[DW]: [bdStorage]: sending publisher file: %s, size: %lld\n", filename.data(), data.size());
#endif
auto reply = server->create_reply(this->task_id());
reply->add(new bdFileData(data));
reply->send();
}
else
{
server->create_reply(this->task_id(), game::BD_NO_FILE)->send();
}
}
std::string bdStorage::get_user_file_path(const std::string& name)
{
return "players2/user/" + name;
}
void bdStorage::set_user_file(service_server* server, byte_buffer* buffer) const
{
bool priv;
uint64_t owner;
std::string game, filename, data;
buffer->read_string(&game);
buffer->read_string(&filename);
buffer->read_bool(&priv);
buffer->read_blob(&data);
buffer->read_uint64(&owner);
const auto path = get_user_file_path(filename);
utils::io::write_file(path, data);
auto* info = new bdFileInfo;
info->file_id = *reinterpret_cast<const uint64_t*>(utils::cryptography::sha1::compute(filename).data());
info->filename = filename;
info->create_time = uint32_t(time(nullptr));
info->modified_time = info->create_time;
info->file_size = uint32_t(data.size());
info->owner_id = owner;
info->priv = priv;
auto reply = server->create_reply(this->task_id());
reply->add(info);
reply->send();
}
void bdStorage::get_user_file(service_server* server, byte_buffer* buffer) const
{
uint64_t owner{};
std::string game, filename, platform, data;
buffer->read_string(&game);
buffer->read_string(&filename);
buffer->read_uint64(&owner);
buffer->read_string(&platform);
#ifdef DEBUG
printf("[DW]: [bdStorage]: user file: %s, %s, %s\n", game.data(), filename.data(), platform.data());
#endif
const auto path = get_user_file_path(filename);
if (utils::io::read_file(path, &data))
{
auto reply = server->create_reply(this->task_id());
reply->add(new bdFileData(data));
reply->send();
}
else
{
server->create_reply(this->task_id(), game::BD_NO_FILE)->send();
}
}
void bdStorage::unk13(service_server* server, byte_buffer* buffer) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,27 @@
#pragma once
namespace demonware
{
class bdStorage final : public service
{
public:
bdStorage();
private:
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, const std::string& path, 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) const;
void list_publisher_files(service_server* server, byte_buffer* buffer);
void get_publisher_file(service_server* server, byte_buffer* buffer);
void set_user_file(service_server* server, byte_buffer* buffer) const;
void get_user_file(service_server* server, byte_buffer* buffer) const;
void unk13(service_server* server, byte_buffer* buffer) const;
static std::string get_user_file_path(const std::string& name);
};
}

View File

@ -0,0 +1,20 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdTitleUtilities::bdTitleUtilities() : service(12, "bdTitleUtilities")
{
this->register_task(6, &bdTitleUtilities::get_server_time);
}
void bdTitleUtilities::get_server_time(service_server* server, byte_buffer* /*buffer*/) const
{
auto* const time_result = new bdTimeStamp;
time_result->unix_time = uint32_t(time(nullptr));
auto reply = server->create_reply(this->task_id());
reply->add(time_result);
reply->send();
}
}

View File

@ -0,0 +1,13 @@
#pragma once
namespace demonware
{
class bdTitleUtilities final : public service
{
public:
bdTitleUtilities();
private:
void get_server_time(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,17 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdUNK63::bdUNK63() : service(63, "bdUNK63")
{
//this->register_task(6, "unk6", &bdUNK63::unk6);
}
void bdUNK63::unk(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,13 @@
#pragma once
namespace demonware
{
class bdUNK63 final : public service
{
public:
bdUNK63();
private:
void unk(service_server* server, byte_buffer* buffer) const;
};
}

View File

@ -0,0 +1,57 @@
#include <std_include.hpp>
#include "../services.hpp"
namespace demonware
{
bdUNK80::bdUNK80() : service(80, "bdUNK80")
{
this->register_task(42, &bdUNK80::unk42);
this->register_task(49, &bdUNK80::unk49);
this->register_task(60, &bdUNK80::unk60);
this->register_task(130, &bdUNK80::unk130);
this->register_task(165, &bdUNK80::unk165);
this->register_task(193, &bdUNK80::unk193);
}
void bdUNK80::unk42(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdUNK80::unk49(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdUNK80::unk60(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdUNK80::unk130(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdUNK80::unk165(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
void bdUNK80::unk193(service_server* server, byte_buffer* /*buffer*/) const
{
// TODO:
auto reply = server->create_reply(this->task_id());
reply->send();
}
}

View File

@ -0,0 +1,18 @@
#pragma once
namespace demonware
{
class bdUNK80 final : public service
{
public:
bdUNK80();
private:
void unk42(service_server* server, byte_buffer* buffer) const;
void unk49(service_server* server, byte_buffer* buffer) const;
void unk60(service_server* server, byte_buffer* buffer) const;
void unk130(service_server* server, byte_buffer* buffer) const;
void unk165(service_server* server, byte_buffer* buffer) const;
void unk193(service_server* server, byte_buffer* buffer) const;
};
}

150
src/client/game/dvars.cpp Normal file
View File

@ -0,0 +1,150 @@
#include <std_include.hpp>
#include <utils/string.hpp>
#include "game.hpp"
namespace dvars
{
game::dvar_t* aimassist_enabled = nullptr;
game::dvar_t* con_inputBoxColor = nullptr;
game::dvar_t* con_inputHintBoxColor = nullptr;
game::dvar_t* con_outputBarColor = nullptr;
game::dvar_t* con_outputSliderColor = nullptr;
game::dvar_t* con_outputWindowColor = nullptr;
game::dvar_t* con_inputDvarMatchColor = nullptr;
game::dvar_t* con_inputDvarValueColor = nullptr;
game::dvar_t* con_inputDvarInactiveValueColor = nullptr;
game::dvar_t* con_inputCmdMatchColor = nullptr;
game::dvar_t* g_playerEjection = nullptr;
game::dvar_t* g_playerCollision = nullptr;
game::dvar_t* bg_surfacePenetration = nullptr;
game::dvar_t* pm_bouncing = nullptr;
game::dvar_t* g_dump_scripts = nullptr;
game::dvar_t* g_gravity = nullptr;
game::dvar_t* g_speed = nullptr;
game::dvar_t* g_elevators = nullptr;
game::dvar_t* g_log = nullptr;
game::dvar_t* jump_height = nullptr;
game::dvar_t* jump_ladderPushVel = nullptr;
game::dvar_t* player_sustainAmmo = nullptr;
game::dvar_t* r_fullbright = nullptr;
game::dvar_t* cg_legacyCrashHandling = nullptr;
game::dvar_t* sv_cheats = nullptr;
std::string dvar_get_vector_domain(const int components, const game::dvar_limits& domain)
{
if (domain.vector.min == -FLT_MAX)
{
if (domain.vector.max == FLT_MAX)
{
return utils::string::va("Domain is any %iD vector", components);
}
else
{
return utils::string::va("Domain is any %iD vector with components %g or smaller", components,
domain.vector.max);
}
}
else if (domain.vector.max == FLT_MAX)
{
return utils::string::va("Domain is any %iD vector with components %g or bigger", components,
domain.vector.min);
}
else
{
return utils::string::va("Domain is any %iD vector with components from %g to %g", components,
domain.vector.min, domain.vector.max);
}
}
std::string dvar_get_domain(const game::dvar_type type, const game::dvar_limits& domain)
{
std::string str;
switch (type)
{
case game::dvar_type::boolean:
return "Domain is 0 or 1"s;
case game::dvar_type::value:
if (domain.value.min == -FLT_MAX)
{
if (domain.value.max == FLT_MAX)
{
return "Domain is any number"s;
}
else
{
return utils::string::va("Domain is any number %g or smaller", domain.value.max);
}
}
else if (domain.value.max == FLT_MAX)
{
return utils::string::va("Domain is any number %g or bigger", domain.value.min);
}
else
{
return utils::string::va("Domain is any number from %g to %g", domain.value.min, domain.value.max);
}
case game::dvar_type::vec2:
return dvar_get_vector_domain(2, domain);
case game::dvar_type::rgb:
case game::dvar_type::vec3:
return dvar_get_vector_domain(3, domain);
case game::dvar_type::vec4:
return dvar_get_vector_domain(4, domain);
case game::dvar_type::integer:
if (domain.enumeration.stringCount == INT_MIN)
{
if (domain.integer.max == INT_MAX)
{
return "Domain is any integer"s;
}
else
{
return utils::string::va("Domain is any integer %i or smaller", domain.integer.max);
}
}
else if (domain.integer.max == INT_MAX)
{
return utils::string::va("Domain is any integer %i or bigger", domain.integer.min);
}
else
{
return utils::string::va("Domain is any integer from %i to %i", domain.integer.min, domain.integer.max);
}
case game::dvar_type::color:
return "Domain is any 4-component color, in RGBA format"s;
case game::dvar_type::enumeration:
str = "Domain is one of the following:"s;
for (auto string_index = 0; string_index < domain.enumeration.stringCount; ++string_index)
{
str += utils::string::va("\n %2i: %s", string_index, domain.enumeration.strings[string_index]);
}
return str;
case game::dvar_type::string:
return "Domain is any text"s;
default:
return utils::string::va("unhandled dvar type '%i'", type);
}
}
}

44
src/client/game/dvars.hpp Normal file
View File

@ -0,0 +1,44 @@
#pragma once
#include "structs.hpp"
namespace dvars
{
extern game::dvar_t* aimassist_enabled;
extern game::dvar_t* con_inputBoxColor;
extern game::dvar_t* con_inputHintBoxColor;
extern game::dvar_t* con_outputBarColor;
extern game::dvar_t* con_outputSliderColor;
extern game::dvar_t* con_outputWindowColor;
extern game::dvar_t* con_inputDvarMatchColor;
extern game::dvar_t* con_inputDvarValueColor;
extern game::dvar_t* con_inputDvarInactiveValueColor;
extern game::dvar_t* con_inputCmdMatchColor;
extern game::dvar_t* g_playerCollision;
extern game::dvar_t* g_playerEjection;
extern game::dvar_t* bg_surfacePenetration;
extern game::dvar_t* pm_bouncing;
extern game::dvar_t* g_dump_scripts;
extern game::dvar_t* g_gravity;
extern game::dvar_t* g_speed;
extern game::dvar_t* g_elevators;
extern game::dvar_t* g_log;
extern game::dvar_t* jump_height;
extern game::dvar_t* jump_ladderPushVel;
extern game::dvar_t* player_sustainAmmo;
extern game::dvar_t* r_fullbright;
extern game::dvar_t* cg_legacyCrashHandling;
extern game::dvar_t* sv_cheats;
std::string dvar_get_vector_domain(const int components, const game::dvar_limits& domain);
std::string dvar_get_domain(const game::dvar_type type, const game::dvar_limits& domain);
}

130
src/client/game/game.cpp Normal file
View File

@ -0,0 +1,130 @@
#include <std_include.hpp>
#include "game.hpp"
#include <utils/flags.hpp>
namespace game
{
int Cmd_Argc()
{
return cmd_args->argc[cmd_args->nesting];
}
const char* Cmd_Argv(const int index)
{
return cmd_args->argv[cmd_args->nesting][index];
}
int SV_Cmd_Argc()
{
return sv_cmd_args->argc[sv_cmd_args->nesting];
}
const char* SV_Cmd_Argv(const int index)
{
return sv_cmd_args->argv[sv_cmd_args->nesting][index];
}
bool VirtualLobby_Loaded()
{
return !game::environment::is_sp() && *mp::virtualLobby_loaded == 1;
}
namespace environment
{
launcher::mode mode = launcher::mode::none;
launcher::mode translate_surrogate(const launcher::mode _mode)
{
switch (_mode)
{
case launcher::mode::survival:
case launcher::mode::zombies:
return launcher::mode::multiplayer;
default:
return _mode;
}
}
launcher::mode get_real_mode()
{
if (mode == launcher::mode::none)
{
throw std::runtime_error("Launcher mode not valid. Something must be wrong.");
}
return mode;
}
launcher::mode get_mode()
{
return translate_surrogate(get_real_mode());
}
bool is_sp()
{
return get_mode() == launcher::mode::singleplayer;
}
bool is_mp()
{
return get_mode() == launcher::mode::multiplayer;
}
bool is_dedi()
{
return get_mode() == launcher::mode::server;
}
void set_mode(const launcher::mode _mode)
{
mode = _mode;
}
std::string get_string()
{
const auto current_mode = get_real_mode();
switch (current_mode)
{
case launcher::mode::server:
return "dedicated server";
case launcher::mode::zombies:
return "zombies";
case launcher::mode::survival:
return "survival";
case launcher::mode::multiplayer:
return "multiplayer";
case launcher::mode::singleplayer:
return "singleplayer";
case launcher::mode::none:
return "none";
default:
return "unknown (" + std::to_string(static_cast<int>(mode)) + ")";
}
}
}
bool is_headless()
{
static const auto headless = utils::flags::has_flag("headless");
return headless;
}
void show_error(const std::string& text, const std::string& title)
{
if (is_headless())
{
puts(text.data());
}
else
{
MessageBoxA(nullptr, text.data(), title.data(), MB_ICONERROR | MB_SETFOREGROUND | MB_TOPMOST);
}
}
}

73
src/client/game/game.hpp Normal file
View File

@ -0,0 +1,73 @@
#pragma once
#include "structs.hpp"
#include "launcher/launcher.hpp"
#define SELECT_VALUE(sp, mp) (game::environment::is_sp() ? (sp) : (mp))
#define SERVER_CD_KEY "s1-mod-CD-Key"
namespace game
{
namespace environment
{
launcher::mode get_mode();
launcher::mode get_real_mode();
bool is_sp();
bool is_mp();
bool is_dedi();
void set_mode(launcher::mode mode);
std::string get_string();
}
template <typename T>
class symbol
{
public:
symbol(const size_t sp_address, const size_t mp_address)
: sp_object_(reinterpret_cast<T*>(sp_address))
, mp_object_(reinterpret_cast<T*>(mp_address))
{
}
T* get() const
{
if (environment::is_sp())
{
return sp_object_;
}
return mp_object_;
}
operator T*() const
{
return this->get();
}
T* operator->() const
{
return this->get();
}
private:
T* sp_object_;
T* mp_object_;
};
[[nodiscard]] int Cmd_Argc();
[[nodiscard]] const char* Cmd_Argv(int index);
[[nodiscard]] int SV_Cmd_Argc();
[[nodiscard]] const char* SV_Cmd_Argv(int index);
[[nodiscard]] bool VirtualLobby_Loaded();
[[nodiscard]] bool is_headless();
void show_error(const std::string& text, const std::string& title = "Error");
}
#include "symbols.hpp"

View File

@ -0,0 +1,90 @@
#include <std_include.hpp>
#include "functions.hpp"
#include <utils/string.hpp>
#include "component/gsc/script_extension.hpp"
#include "component/gsc/script_loading.hpp"
namespace scripting
{
namespace
{
int find_function_index(const std::string& name, [[maybe_unused]] const bool prefer_global)
{
const auto target = utils::string::to_lower(name);
auto const& first = gsc::gsc_ctx->func_map();
auto const& second = gsc::gsc_ctx->meth_map();
if (const auto itr = first.find(name); itr != first.end())
{
return static_cast<int>(itr->second);
}
if (const auto itr = second.find(name); itr != second.end())
{
return static_cast<int>(itr->second);
}
return -1;
}
}
std::uint32_t parse_token_id(const std::string& name)
{
if (name.starts_with("_ID"))
{
return static_cast<std::uint32_t>(std::strtol(name.substr(3).data(), nullptr, 10));
}
return 0;
}
std::string find_token(std::uint32_t id)
{
return gsc::gsc_ctx->token_name(id);
}
std::string find_token_single(std::uint32_t id)
{
return gsc::gsc_ctx->token_name(id);
}
unsigned int find_token_id(const std::string& name)
{
const auto id = gsc::gsc_ctx->token_id(name);
if (id)
{
return id;
}
const auto parsed_id = parse_token_id(name);
if (parsed_id)
{
return parsed_id;
}
return game::SL_GetCanonicalString(name.data());
}
script_function get_function_by_index(const std::uint32_t index)
{
static const auto function_table = &gsc::func_table;
static const auto method_table = SELECT_VALUE(0x14966A670, 0x147DD2F50);
if (index < 0x1000)
{
return reinterpret_cast<script_function*>(function_table)[index - 1];
}
return reinterpret_cast<script_function*>(method_table)[index - 0x8000];
}
script_function find_function(const std::string& name, const bool prefer_global)
{
const auto index = find_function_index(name, prefer_global);
if (index < 0) return nullptr;
return get_function_by_index(index);
}
}

View File

@ -0,0 +1,14 @@
#pragma once
#include "game/game.hpp"
namespace scripting
{
using script_function = void(*)(game::scr_entref_t);
std::string find_token(std::uint32_t id);
std::string find_token_single(std::uint32_t id);
unsigned int find_token_id(const std::string& name);
script_function get_function_by_index(std::uint32_t index);
script_function find_function(const std::string& name, bool prefer_global);
}

1942
src/client/game/structs.hpp Normal file

File diff suppressed because it is too large Load Diff

310
src/client/game/symbols.hpp Normal file
View File

@ -0,0 +1,310 @@
#pragma once
#define WEAK __declspec(selectany)
namespace game
{
/***************************************************************
* Functions
**************************************************************/
WEAK symbol<void(int type, VariableUnion u)> AddRefToValue{0x140315830, 0x1403F1F20};
WEAK symbol<void(unsigned int id)> AddRefToObject{0, 0x1403F1F10};
WEAK symbol<unsigned int(unsigned int id)> AllocThread{0, 0x1403F2270};
WEAK symbol<void(int type, VariableUnion u)> RemoveRefToValue{0x140317340, 0x1403F3A50};
WEAK symbol<void(unsigned int id)> RemoveRefToObject{0, 0x1403F3940};
WEAK symbol<void(void*, void*)> AimAssist_AddToTargetList{0, 0x140001730};
WEAK symbol<void(unsigned int weapon, bool isAlternate, char* output, unsigned int maxStringLen)> BG_GetWeaponNameComplete{0x0, 0x140165580};
WEAK symbol<void(errorParm code, const char* message, ...)> Com_Error{0x1402F7570, 0x1403CE480};
WEAK symbol<void()> Com_Frame_Try_Block_Function{0x1402F7E10, 0x1403CEF30};
WEAK symbol<CodPlayMode()> Com_GetCurrentCoDPlayMode{0, 0x1404C9690};
WEAK symbol<void(float, float, int)> Com_SetSlowMotion{0, 0x1403D19B0};
WEAK symbol<void()> Com_Quit_f{0x1402F9390, 0x1403D08C0};
WEAK symbol<void(const char* cmdName, void (), cmd_function_s* allocedCmd)> Cmd_AddCommandInternal{0x1402EDDB0, 0x1403AF2C0};
WEAK symbol<void(int localClientNum, int controllerIndex, const char* text)> Cmd_ExecuteSingleCommand{0x1402EE350, 0x1403AF900};
WEAK symbol<void(const char*)> Cmd_RemoveCommand{0x1402EE910, 0x1403AFEF0};
WEAK symbol<void(const char* text_in)> Cmd_TokenizeString{0x1402EEA30, 0x1403B0020};
WEAK symbol<void()> Cmd_EndTokenizeString{0x1402EE000, 0x1403AF5B0};
WEAK symbol<void(const char* message)> Conbuf_AppendText{0x14038F220, 0x1404D9040};
WEAK symbol<char*(int start)> ConcatArgs{0x14021A7E0, 0x1402E9670};
WEAK symbol<void(int localClientNum, void (*)(int localClientNum))> Cbuf_AddCall{0x1402ED820, 0x1403AECF0};
WEAK symbol<void(int localClientNum, const char* text)> Cbuf_AddText{0x1402ED890, 0x1403AED70};
WEAK symbol<void(int localClientNum, int controllerIndex, const char* buffer, void (int, int, const char*))> Cbuf_ExecuteBufferInternal{0x1402ED9A0, 0x1403AEE80};
WEAK symbol<bool()> CL_IsCgameInitialized{0x140136560, 0x1401FD510};
WEAK symbol<void(int localClientNum, const char* string)> CL_ForwardCommandToServer{0x0, 0x14020B310};
WEAK symbol<void(int localClientNum)> CL_WritePacket{0x0, 0x1402058F0};
WEAK symbol<void(int localClientNum)> CL_Disconnect{0x0, 0x140209EC0};
WEAK symbol<void(int localClientNum, const char* message)> CG_GameMessage{0x1400EE500, 0x1401A3050};
WEAK symbol<void(int localClientNum, /*mp::cg_s**/void* cg, const char* dvar, const char* value)> CG_SetClientDvarFromServer{0, 0x1401BF0A0};
WEAK symbol<void(XAssetType type, void (*func)(XAssetHeader, void*), void* inData, bool includeOverride)> DB_EnumXAssets_FastFile{0x14017D7C0, 0x14026EC10};
WEAK symbol<void(XAssetType type, void(*func)(XAssetHeader, void*), const void* inData, bool includeOverride)> DB_EnumXAssets_Internal{0x14017D830, 0x14026EC80};
WEAK symbol<XAssetEntry(XAssetType type, const char* name)> DB_FindXAssetEntry{0x14017D830, 0x14026F020};
WEAK symbol<XAssetHeader(XAssetType type, const char *name, int allowCreateDefault)> DB_FindXAssetHeader{0x14017DCA0, 0x14026F0F0};
WEAK symbol<const char*(const XAsset* asset)> DB_GetXAssetName{0x140151C00, 0x140240DD0};
WEAK symbol<int(XAssetType type)> DB_GetXAssetTypeSize{0x140151C20, 0x140240DF0};
WEAK symbol<void(XZoneInfo* zoneInfo, unsigned int zoneCount, DBSyncMode syncMode)> DB_LoadXAssets{0x14017FB20, 0x140270F30};
WEAK symbol<int(XAssetType type, const char* name)> DB_XAssetExists{0x140182190, 0x1402750F0};
WEAK symbol<int(XAssetType type, const char* name)> DB_IsXAssetDefault{0x14017EEF0, 0x140270320};
WEAK symbol<int(const RawFile* rawfile)> DB_GetRawFileLen{0x14017E890, 0x14026FCC0};
WEAK symbol<void(const RawFile* rawfile, char* buf, int size)> DB_GetRawBuffer{0x14017E750, 0x14026FB90};
WEAK symbol<char*(const char* filename, char* buf, int size)> DB_ReadRawFile{0x140180E30, 0x140273080};
WEAK symbol<dvar_t*(const char* name)> Dvar_FindVar{0x140370860, 0x1404BF8B0};
WEAK symbol<void(const dvar_t* dvar)> Dvar_ClearModified{0x140370700, 0x1404BF690};
WEAK symbol<void(char* buffer, int index)> Dvar_GetCombinedString{0x1402FB590, 0x1403D3290};
WEAK symbol<bool(const char* name)> Dvar_IsValidName{0x140370CB0, 0x1404BFF70};
WEAK symbol<void(dvar_t* dvar, DvarSetSource source)> Dvar_Reset{0x140372950, 0x1404C1DB0};
WEAK symbol<void(const char* dvar, const char* buffer)> Dvar_SetCommand{0x1403730D0, 0x1404C2520};
WEAK symbol<void(const dvar_t* dvar, const char* string)> Dvar_SetString{0x140373DE0, 0x1404C3610};
WEAK symbol<void(const dvar_t* dvar, bool value)> Dvar_SetBool{0x0, 0x1404C1F30};
WEAK symbol<void(const char*, const char*, DvarSetSource)> Dvar_SetFromStringByNameFromSource{0x1403737D0, 0x1404C2E40};
WEAK symbol<const char*(dvar_t* dvar, dvar_value value)> Dvar_ValueToString{0x140374E10, 0x1404C47B0};
WEAK symbol<dvar_t*(const char* dvarName, bool value, unsigned int flags, const char* description)>
Dvar_RegisterBool{0x140371850, 0x1404C0BE0};
WEAK symbol<dvar_t*(const char* dvarName, const char** valueList, int defaultIndex, unsigned int flags,
const char* description)> Dvar_RegisterEnum{0x140371B30, 0x1404C0EC0};
WEAK symbol<dvar_t*(const char* dvarName, float value, float min, float max, unsigned int flags,
const char* description)> Dvar_RegisterFloat{0x140371C20, 0x1404C0FB0};
WEAK symbol<dvar_t*(const char* dvarName, int value, int min, int max, unsigned int flags, const char* desc)>
Dvar_RegisterInt{0x140371CF0, 0x1404C1080};
WEAK symbol<dvar_t*(const char* dvarName, const char* value, unsigned int flags, const char* description)>
Dvar_RegisterString{0x140372050, 0x1404C1450};
WEAK symbol<dvar_t* (const char* dvarName, float x, float y, float min, float max,
unsigned int flags, const char* description)> Dvar_RegisterVec2{0x140372120, 0x1404C1520};
WEAK symbol<dvar_t* (const char* dvarName, float x, float y, float z, float min, float max,
unsigned int flags, const char* description)> Dvar_RegisterVec3{0x140372230, 0x1404C1600};
WEAK symbol<dvar_t*(const char* dvarName, float x, float y, float z, float w, float min, float max,
unsigned int flags, const char* description)> Dvar_RegisterVec4{0x140372430, 0x1404C1800};
WEAK symbol<DWOnlineStatus()> dwGetLogOnStatus{0, 0x14053CCB0};
WEAK symbol<long long(const char* qpath, char** buffer)> FS_ReadFile{0x140362390, 0x1404AF380};
WEAK symbol<void(void* buffer)> FS_FreeFile{0x140362380, 0x1404AF370};
WEAK symbol<void (const char *gameName)> FS_Startup{0x140361940, 0x1404AE930};
WEAK symbol<void (const char *path, const char *dir, int bLanguageDirectory, int iLanguage)> FS_AddGameDirectory{0x14035F3F0, 0x1404ACF80};
WEAK symbol<void (const char *path, const char *dir)> FS_AddLocalizedGameDirectory{0x14035F5C0, 0x1404AD170};
WEAK symbol<void()> GScr_LoadConsts{0x140283970, 0x1403479C0};
WEAK symbol<unsigned int(unsigned int parentId, unsigned int name)> FindVariable{0x1403165D0, 0x1403F2DC0};
WEAK symbol<unsigned int(int entnum, unsigned int classnum)> FindEntityId{0x1403166D0, 0x1403F2CC0};
WEAK symbol<scr_string_t(unsigned int parentId, unsigned int id)> GetVariableName{0x1403170E0, 0x1403F37F0};
WEAK symbol<void(VariableValue* result, unsigned int classnum, int entnum, int offset)> GetEntityFieldValue{0x14031AAD0, 0x1403F72A0};
WEAK symbol<unsigned int(unsigned int)> GetObjectType{0x140316F70, 0x1403F3670};
WEAK symbol<unsigned int(unsigned int, unsigned int)> GetVariable{0x0, 0x1403F3730};
WEAK symbol<void()> G_Glass_Update{0x14021D540, 0x1402EDEE0};
WEAK symbol<int(int clientNum)> G_GetClientScore{0, 0x1402F6AB0};
WEAK symbol<unsigned int(const char* name)> G_GetWeaponForName{0x140274590, 0x14033FF60};
WEAK symbol<int(playerState_s* ps, unsigned int weapon, int dualWield, int startInAltMode, int, int, int, char)> G_GivePlayerWeapon{0x1402749B0, 0x140340470};
WEAK symbol<void(playerState_s* ps, unsigned int weapon, int hadWeapon)> G_InitializeAmmo{0x1402217F0, 0x1402F22B0};
WEAK symbol<void(int clientNum, unsigned int weapon)> G_SelectWeapon{0x140275380, 0x140340D50};
WEAK symbol<int(playerState_s* ps, unsigned int weapon)> G_TakePlayerWeapon{0x1402754E0, 0x1403411D0};
WEAK symbol<char*(char* string)> I_CleanStr{0x140379010, 0x1404C99A0};
WEAK symbol<char*(GfxImage *image, uint32_t width, uint32_t height, uint32_t depth, uint32_t mipCount, uint32_t imageFlags, DXGI_FORMAT imageFormat, const char *name, const D3D11_SUBRESOURCE_DATA *initData)> Image_Setup{0x1404858D0, 0x1405A3150};
WEAK symbol<const char*(int, int, int)> Key_KeynumToString{0x14013F380, 0x140207C50};
WEAK symbol<unsigned int(int)> Live_SyncOnlineDataFlags{0x1404459A0, 0x140562830};
WEAK symbol<void(int localClientNum, const char* menuName, int isPopup, int isModal, unsigned int isExclusive)> LUI_OpenMenu{0, 0x14048E450};
WEAK symbol<void()> LUI_EnterCriticalSection{0, 0x1400D2B10};
WEAK symbol<void()> LUI_LeaveCriticalSection{0, 0x1400D7620};
WEAK symbol<bool(int localClientNum, const char* menuName)> Menu_IsMenuOpenAndVisible{0, 0x140488570};
WEAK symbol<Material*(const char* material)> Material_RegisterHandle{0x1404919D0, 0x1405AFBE0};
WEAK symbol<void(netadr_s*, sockaddr*)> NetadrToSockadr{0, 0x1404B6F10};
WEAK symbol<void(netsrc_t, netadr_s*, const char*)> NET_OutOfBandPrint{0, 0x1403DADC0};
WEAK symbol<void(netsrc_t sock, int length, const void* data, const netadr_s* to)> NET_SendLoopPacket{0, 0x1403DAF80};
WEAK symbol<bool(const char* s, netadr_s* a)> NET_StringToAdr{0, 0x1403DB070};
WEAK symbol<void(float x, float y, float width, float height, float s0, float t0, float s1, float t1,
float* color, Material* material)> R_AddCmdDrawStretchPic{0x1404A2580, 0x1405C0CB0};
WEAK symbol<void(const char*, int, Font_s*, float, float, float, float, float, float*, int)> R_AddCmdDrawText{0x1404A2BF0, 0x1405C1320};
WEAK symbol<void(const char*, int, Font_s*, float, float, float, float, float, const float*, int, int, char)> R_AddCmdDrawTextWithCursor{0x1404A35E0, 0x1405C1D10};
WEAK symbol<Font_s*(const char* font)> R_RegisterFont{0x140481F90, 0x14059F3C0};
WEAK symbol<void()> R_SyncRenderThread{0x1404A4D60, 0x1405C34F0};
WEAK symbol<int(const char* text, int maxChars, Font_s* font)> R_TextWidth{0x140482270, 0x14059F6B0};
WEAK symbol<ScreenPlacement*()> ScrPlace_GetViewPlacement{0x14014FA70, 0x14023CB50};
WEAK symbol<unsigned int()> Scr_AllocArray{0x140317C50, 0x1403F4280};
WEAK symbol<const float*(const float* v)> Scr_AllocVector{0x140317D10, 0x1403F4370};
WEAK symbol<const char*(unsigned int index)> Scr_GetString{0x14031C570, 0x1403F8C50};
WEAK symbol<void(const char* value)> Scr_AddString{0x0, 0x1403F7C10};
WEAK symbol<void(int value)> Scr_AddInt{0x0, 0x1403F7B30};
WEAK symbol<int(unsigned int index)> Scr_GetInt{0x14031C1F0, 0x1403F88D0};
WEAK symbol<float(unsigned int index)> Scr_GetFloat{0x14031C090, 0x1403F8820};
WEAK symbol<unsigned int()> Scr_GetNumParam{0x14031C2A0, 0x1403F8980};
WEAK symbol<void()> Scr_ClearOutParams{0x14031B7C0, 0x1403F8040};
WEAK symbol<scr_entref_t(unsigned int entId)> Scr_GetEntityIdRef{0x14031A0D0, 0x1403F68A0};
WEAK symbol<int(unsigned int classnum, int entnum, int offset)> Scr_SetObjectField{0x14026B620, 0x140339450};
WEAK symbol<void(void* ent, unsigned int stringValue, unsigned int paramcount)> Scr_Notify{0x0, 0x140339120};
WEAK symbol<void(unsigned int id, unsigned int stringValue, unsigned int paramcount)> Scr_NotifyId{0x14031CB80, 0x1403F92D0};
WEAK symbol<bool(VariableValue* value)> Scr_CastString{0x0, 0x1403F4500};
WEAK symbol<unsigned __int16(int handle, unsigned int paramcount)> Scr_ExecThread{0x0, 0x1403F8120};
WEAK symbol<unsigned int(const char* name)> Scr_LoadScript{0x0, 0x1403EE250};
WEAK symbol<unsigned int(const char* script, unsigned int name)> Scr_GetFunctionHandle{0x0, 0x1403EE0D0};
WEAK symbol<unsigned int(void* func, int type, unsigned int name)> Scr_RegisterFunction{0x1403115B0, 0x1403EDAE0};
WEAK symbol<unsigned int(unsigned int localId, const char* pos, unsigned int paramcount)> VM_Execute{0x0, 0x1403F9E40};
WEAK symbol<void()> Scr_ErrorInternal{0x0, 0x1403F80A0};
WEAK symbol<const char*(scr_string_t stringValue)> SL_ConvertToString{0x140314850, 0x1403F0F10};
WEAK symbol<scr_string_t(const char* str)> SL_FindString{0x140314AF0, 0x1403F11C0};
WEAK symbol<scr_string_t(const char* str, unsigned int user)> SL_GetString{0x140314D90, 0x1403F1440};
WEAK symbol<unsigned int(char const* str)> SL_GetCanonicalString{0x140311770, 0x1403EDCA0};
WEAK symbol<void(int arg, char* buffer, int bufferLength)> SV_Cmd_ArgvBuffer{0x1402EEFD0, 0x1403B05C0};
WEAK symbol<void(const char* text_in)> SV_Cmd_TokenizeString{0, 0x1403B0640};
WEAK symbol<void()> SV_Cmd_EndTokenizedString{0, 0x1403B0600};
WEAK symbol<mp::gentity_s*(const char* name)> SV_AddBot{0, 0x140438EC0};
WEAK symbol<bool(int clientNum)> SV_BotIsBot{0, 0x140427300};
WEAK symbol<const char*()> SV_BotGetRandomName{0, 0x1404267E0};
WEAK symbol<mp::gentity_s*(int)> SV_AddTestClient{0, 0x140439190};
WEAK symbol<bool(mp::gentity_s*)> SV_CanSpawnTestClient{0, 0x140439460};
WEAK symbol<int(mp::gentity_s* ent)> SV_SpawnTestClient{0, 0x14043C750};
WEAK symbol<void(mp::gentity_s*)> SV_AddEntity{0, 0x1403388B0};
WEAK symbol<void(netadr_s* from)> SV_DirectConnect{0, 0x1404397A0};
WEAK symbol<void(mp::client_t* client)> SV_DropClient{0, 0x140438A30};
WEAK symbol<void(mp::client_t*, const char*, int)> SV_ExecuteClientCommand{0, 0x15121D8E6};
WEAK symbol<void(int localClientNum)> SV_FastRestart{0, 0x1404374E0};
WEAK symbol<void(int clientNum, svscmd_type type, const char* text)> SV_GameSendServerCommand{0x1403F3A70, 0x14043E120};
WEAK symbol<const char*(int clientNum)> SV_GetGuid{0, 0x14043E1E0};
WEAK symbol<int(int clientNum)> SV_GetClientPing{0, 0x14043E1C0};
WEAK symbol<playerState_s*(int num)> SV_GetPlayerstateForClientNum{0x1403F3AB0, 0x14043E260};
WEAK symbol<void(int clientNum, const char* reason)> SV_KickClientNum{0, 0x1404377A0};
WEAK symbol<bool()> SV_Loaded{0x1403F42C0, 0x14043FA50};
WEAK symbol<bool(const char* map)> SV_MapExists{0, 0x140437800};
WEAK symbol<void(int localClientNum, const char* map, bool mapIsPreloaded)> SV_StartMap{0, 0x140438320};
WEAK symbol<void(int localClientNum, const char* map, bool mapIsPreloaded, bool migrate)> SV_StartMapForParty{0, 0x140438490};
WEAK symbol<void(int index, const char* string)> SV_SetConfigstring{0, 0x14043FCA0};
WEAK symbol<void(char* path, int pathSize, Sys_Folder folder, const char* filename, const char* ext)>
Sys_BuildAbsPath{0x14037BBE0, 0x1404CC7E0};
WEAK symbol<HANDLE(int folder, const char* baseFileName)> Sys_CreateFile{0x14037BCA0, 0x1404CC8A0};
WEAK symbol<void(const char* error, ...)> Sys_Error{0x14038C770, 0x1404D6260};
WEAK symbol<bool(const char* path)> Sys_FileExists{0x14038C810, 0x1404D6310};
WEAK symbol<bool()> Sys_IsDatabaseReady2{0x1402FF980, 0x1403E1840};
WEAK symbol<int()> Sys_Milliseconds{0x14038E9F0, 0x1404D8730};
WEAK symbol<bool(int, void const*, const netadr_s*)> Sys_SendPacket{0x14038E720, 0x1404D8460};
WEAK symbol<void(Sys_Folder, const char* path)> Sys_SetFolder{0x14037BDD0, 0x1404CCA10};
WEAK symbol<void()> Sys_ShowConsole{0x14038FA90, 0x1404D98B0};
WEAK symbol<bool()> Sys_IsMainThread{0x1402FF9C0, 0x1403E1880};
WEAK symbol<const char*(const char*)> UI_GetMapDisplayName{0, 0x1403B1CD0};
WEAK symbol<const char*(const char*)> UI_GetGameTypeDisplayName{0, 0x1403B1670};
WEAK symbol<void(unsigned int localClientNum, const char** args)> UI_RunMenuScript{0, 0x140490060};
WEAK symbol<int(const char* text, int maxChars, Font_s* font, float scale)> UI_TextWidth{0, 0x140492380};
WEAK symbol<const char*()> SEH_GetCurrentLanguageName{0x140339300, 0x1404745C0};
WEAK symbol<void*(unsigned int size, unsigned int alignment, unsigned int type, int source)> PMem_AllocFromSource_NoDebug{0x1403775F0, 0x1404C7BA0};
WEAK symbol<void*(unsigned int size)> Hunk_AllocateTempMemoryHighInternal{0x140369D60, 0x1404B68B0};
WEAK symbol<void*(jmp_buf* Buf, int Value)> longjmp{0x14059C5C0, 0x1406FD930};
WEAK symbol<int(jmp_buf* Buf)> _setjmp{0x14059CD00, 0x1406FE070};
/***************************************************************
* Variables
**************************************************************/
WEAK symbol<int> keyCatchers{0x1413D5B00, 0x1417E168C};
WEAK symbol<PlayerKeyState> playerKeys{0x1413BC5DC, 0x1417DA46C};
WEAK symbol<CmdArgs> cmd_args{0x1492EC6F0, 0x1479ECB00};
WEAK symbol<CmdArgs> sv_cmd_args{0x1492EC7A0, 0x1479ECBB0};
WEAK symbol<cmd_function_s*> cmd_functions{0x1492EC848, 0x1479ECC58};
WEAK symbol<int> dvarCount{0x14A7BFF34, 0x14B32AA30};
WEAK symbol<dvar_t*> sortedDvars{0x14A7BFF50, 0x14B32AA50};
WEAK symbol<unsigned int> levelEntityId{0x149AF55B0, 0x14815DEB0};
WEAK symbol<int> g_script_error_level{0x14A1917A8, 0x1487F9FA4};
WEAK symbol<jmp_buf> g_script_error{0x14A1917B0, 0x1487FA0C0};
WEAK symbol<scr_classStruct_t> g_classMap{0x14080A840, 0x1409BE1B0};
WEAK symbol<scrVarGlob_t> scr_VarGlob{0x149B1D680, 0x148185F80};
WEAK symbol<scrVmPub_t> scr_VmPub{0x14A1938C0, 0x1487FC1C0};
WEAK symbol<function_stack_t> scr_function_stack{0x14A19DE40, 0x148806740};
WEAK symbol<const char*> command_whitelist{0x140808EF0, 0x1409B8DC0};
WEAK symbol<SOCKET> query_socket{0, 0x14B5B9180};
WEAK symbol<int> level_time{0x0, 0x144959C2C};
WEAK symbol<void*> DB_XAssetPool{0x140804690, 0x1409B40D0};
WEAK symbol<unsigned int> db_hashTable{0x142C3E050, 0x143716B10};
WEAK symbol<XAssetEntry> g_assetEntryPool{0x142CC2400, 0x14379F100};
WEAK symbol<int> g_poolSize{0x140804140, 0x1409B4B90};
WEAK symbol<const char*> g_assetNames{0x140803C90, 0x1409B3180};
WEAK symbol<DWORD> threadIds{0x149632EC0, 0x147DCEA30};
WEAK symbol<GfxDrawMethod_s> gfxDrawMethod{0x14CDFAFE8, 0x14D80FD98};
WEAK symbol<unsigned int> tls_index{0x14F65DAF0, 0x150085C44};
namespace mp
{
WEAK symbol<gentity_s> g_entities{0, 0x144758C70};
WEAK symbol<client_t> svs_clients{0, 0x1496C4B10};
WEAK symbol<int> svs_numclients{0, 0x1496C4B0C};
WEAK symbol<int> gameTime{0, 0x144959C2C};
WEAK symbol<int> serverTime{0, 0x1496C4B00};
WEAK symbol<int> ping{0, 0x1417E6A84};
WEAK symbol<int> sv_serverId_value{0, 0x1488A9A60};
WEAK symbol<char> virtualLobby_loaded{0, 0x1417E161D};
}
namespace sp
{
WEAK symbol<gentity_s> g_entities{0x143C26DC0, 0};
}
namespace hks
{
WEAK symbol<lua_State*> lua_state{0, 0x1412E2B50};
WEAK symbol<void(lua_State* s, const char* str, unsigned int l)> hksi_lua_pushlstring{0, 0x1400290B0};
WEAK symbol<HksObject*(HksObject* result, lua_State* s, const HksObject* table, const HksObject* key)> hks_obj_getfield{0, 0x14009D3C0};
WEAK symbol<void(lua_State* s, const HksObject* tbl, const HksObject* key, const HksObject* val)> hks_obj_settable{0, 0x14009E480};
WEAK symbol<HksObject* (HksObject* result, lua_State* s, const HksObject* table, const HksObject* key)> hks_obj_gettable{0, 0x14009D800};
WEAK symbol<void(lua_State* s, int nargs, int nresults, const unsigned int* pc)> vm_call_internal{0, 0x1400C9EC0};
WEAK symbol<HashTable*(lua_State* s, unsigned int arraySize, unsigned int hashSize)> Hashtable_Create{0, 0x14008AAE0};
WEAK symbol<cclosure*(lua_State* s, lua_function function, int num_upvalues, int internal_, int profilerTreatClosureAsFunc)> cclosure_Create{0, 0x14008AD00};
WEAK symbol<int(lua_State* s, int t)> hksi_luaL_ref{0, 0x1400A7D60};
WEAK symbol<void(lua_State* s, int t, int ref)> hksi_luaL_unref{0, 0x1400A0660};
WEAK symbol<int(lua_State* s, const HksCompilerSettings* options, const char* buff, unsigned __int64 sz, const char* name)> hksi_hksL_loadbuffer{0, 0x14009ECA0};
WEAK symbol<int(lua_State* s, const char* what, lua_Debug* ar)> hksi_lua_getinfo{0, 0x1400A0C00};
WEAK symbol<int(lua_State* s, int level, lua_Debug* ar)> hksi_lua_getstack{0, 0x1400A0EC0};
WEAK symbol<void(lua_State* s, const char* fmt, ...)> hksi_luaL_error{0, 0x1400A03D0};
WEAK symbol<const char*> s_compilerTypeName{0, 0x1409AB270};
}
}

View File

@ -0,0 +1,172 @@
#include <std_include.hpp>
#include "execution.hpp"
#include "component/console.hpp"
namespace ui_scripting
{
namespace
{
script_value get_field(void* ptr, game::hks::HksObjectType type, const script_value& key)
{
const auto state = *game::hks::lua_state;
const auto top = state->m_apistack.top;
push_value(key);
game::hks::HksObject value{};
game::hks::HksObject obj{};
obj.t = type;
obj.v.ptr = ptr;
game::hks::hks_obj_gettable(&value, state, &obj, &state->m_apistack.top[-1]);
state->m_apistack.top = top;
return value;
}
void set_field(void* ptr, game::hks::HksObjectType type, const script_value& key, const script_value& value)
{
const auto state = *game::hks::lua_state;
game::hks::HksObject obj{};
obj.t = type;
obj.v.ptr = ptr;
game::hks::hks_obj_settable(state, &obj, &key.get_raw(), &value.get_raw());
}
}
void push_value(const script_value& value)
{
const auto state = *game::hks::lua_state;
*state->m_apistack.top = value.get_raw();
state->m_apistack.top++;
}
void push_value(const game::hks::HksObject& value)
{
const auto state = *game::hks::lua_state;
*state->m_apistack.top = value;
state->m_apistack.top++;
}
script_value get_return_value(int offset)
{
const auto state = *game::hks::lua_state;
return state->m_apistack.top[-1 - offset];
}
arguments get_return_values()
{
const auto state = *game::hks::lua_state;
const auto count = static_cast<int>(state->m_apistack.top - state->m_apistack.base);
arguments values;
for (auto i = count - 1; i >= 0; i--)
{
const auto v = get_return_value(i);
values.push_back(v);
}
if (values.empty())
{
values.push_back({});
}
return values;
}
arguments get_return_values(game::hks::HksObject* base)
{
const auto state = *game::hks::lua_state;
const auto count = static_cast<int>(state->m_apistack.top - base);
arguments values;
for (auto i = count - 1; i >= 0; i--)
{
const auto v = get_return_value(i);
values.push_back(v);
}
if (values.empty())
{
values.push_back({});
}
return values;
}
bool notify(const std::string& name, const event_arguments& arguments)
{
const auto state = *game::hks::lua_state;
if (state == nullptr)
{
return false;
}
const auto _1 = gsl::finally(game::LUI_LeaveCriticalSection);
game::LUI_EnterCriticalSection();
try
{
const auto globals = table((*::game::hks::lua_state)->globals.v.table);
const auto engine = globals.get("Engine").as<table>();
const auto root = engine.get("GetLuiRoot")()[0].as<userdata>();
const auto process_event = root.get("processEvent");
table event{};
event.set("name", name);
event.set("dispatchChildren", true);
for (const auto& arg : arguments)
{
event.set(arg.first, arg.second);
}
process_event(root, event);
return true;
}
catch (const std::exception& e)
{
console::error("Error processing event '%s' %s\n", name.data(), e.what());
}
return false;
}
arguments call_script_function(const function& function, const arguments& arguments)
{
const auto state = *game::hks::lua_state;
const auto top = state->m_apistack.top;
push_value(function);
for (auto i = arguments.begin(); i != arguments.end(); ++i)
{
push_value(*i);
}
game::hks::vm_call_internal(state, static_cast<int>(arguments.size()), -1, 0);
const auto args = get_return_values(top);
state->m_apistack.top = top;
return args;
}
script_value get_field(const userdata& self, const script_value& key)
{
return get_field(self.ptr, game::hks::TUSERDATA, key);
}
script_value get_field(const table& self, const script_value& key)
{
return get_field(self.ptr, game::hks::TTABLE, key);
}
void set_field(const userdata& self, const script_value& key, const script_value& value)
{
set_field(self.ptr, game::hks::TUSERDATA, key, value);
}
void set_field(const table& self, const script_value& key, const script_value& value)
{
set_field(self.ptr, game::hks::TTABLE, key, value);
}
}

View File

@ -0,0 +1,23 @@
#pragma once
#include "game/game.hpp"
#include "types.hpp"
#include "script_value.hpp"
namespace ui_scripting
{
void push_value(const script_value& value);
void push_value(const game::hks::HksObject& value);
script_value get_return_value(int offset);
arguments get_return_values();
arguments get_return_values(game::hks::HksObject* base);
bool notify(const std::string& name, const event_arguments& arguments);
arguments call_script_function(const function& function, const arguments& arguments);
script_value get_field(const userdata& self, const script_value& key);
script_value get_field(const table& self, const script_value& key);
void set_field(const userdata& self, const script_value& key, const script_value& value);
void set_field(const table& self, const script_value& key, const script_value& value);
}

View File

@ -0,0 +1,448 @@
#include <std_include.hpp>
#include "execution.hpp"
#include "types.hpp"
#include "script_value.hpp"
namespace ui_scripting
{
hks_object::hks_object(const game::hks::HksObject& value)
{
this->assign(value);
}
hks_object::hks_object(const hks_object& other) noexcept
{
this->operator=(other);
}
hks_object::hks_object(hks_object&& other) noexcept
{
this->operator=(std::move(other));
}
hks_object& hks_object::operator=(const hks_object& other) noexcept
{
if (this != &other)
{
this->release();
this->assign(other.value_);
}
return *this;
}
hks_object& hks_object::operator=(hks_object&& other) noexcept
{
if (this != &other)
{
this->release();
this->value_ = other.value_;
other.value_.t = game::hks::TNONE;
}
return *this;
}
hks_object::~hks_object()
{
this->release();
}
const game::hks::HksObject& hks_object::get() const
{
return this->value_;
}
void hks_object::assign(const game::hks::HksObject& value)
{
this->value_ = value;
const auto state = *game::hks::lua_state;
const auto top = state->m_apistack.top;
push_value(this->value_);
this->ref_ = game::hks::hksi_luaL_ref(*game::hks::lua_state, -10000);
state->m_apistack.top = top;
}
void hks_object::release()
{
if (this->ref_)
{
game::hks::hksi_luaL_unref(*game::hks::lua_state, -10000, this->ref_);
this->value_.t = game::hks::TNONE;
}
}
/***************************************************************
* Constructors
**************************************************************/
script_value::script_value(const game::hks::HksObject& value)
: value_(value)
{
}
script_value::script_value(const int value)
{
game::hks::HksObject obj{};
obj.t = game::hks::TNUMBER;
obj.v.number = static_cast<float>(value);
this->value_ = obj;
}
script_value::script_value(const unsigned int value)
{
game::hks::HksObject obj{};
obj.t = game::hks::TNUMBER;
obj.v.number = static_cast<float>(value);
this->value_ = obj;
}
script_value::script_value(const long long value)
{
game::hks::HksObject obj{};
obj.t = game::hks::TUI64;
obj.v.i64 = value;
this->value_ = obj;
}
script_value::script_value(const unsigned long long value)
{
game::hks::HksObject obj{};
obj.t = game::hks::TUI64;
obj.v.ui64 = value;
this->value_ = obj;
}
script_value::script_value(const bool value)
{
game::hks::HksObject obj{};
obj.t = game::hks::TBOOLEAN;
obj.v.boolean = value;
this->value_ = obj;
}
script_value::script_value(const float value)
{
game::hks::HksObject obj{};
obj.t = game::hks::TNUMBER;
obj.v.number = static_cast<float>(value);
this->value_ = obj;
}
script_value::script_value(const double value)
: script_value(static_cast<float>(value))
{
}
script_value::script_value(const char* value, const size_t len)
{
game::hks::HksObject obj{};
const auto state = *game::hks::lua_state;
if (state == nullptr)
{
return;
}
const auto top = state->m_apistack.top;
game::hks::hksi_lua_pushlstring(state, value, static_cast<unsigned int>(len));
obj = state->m_apistack.top[-1];
state->m_apistack.top = top;
this->value_ = obj;
}
script_value::script_value(const char* value)
: script_value(value, strlen(value))
{
}
script_value::script_value(const std::string& value)
: script_value(value.data(), value.size())
{
}
script_value::script_value(const lightuserdata& value)
{
game::hks::HksObject obj{};
obj.t = game::hks::TLIGHTUSERDATA;
obj.v.ptr = value.ptr;
this->value_ = obj;
}
script_value::script_value(const userdata& value)
{
game::hks::HksObject obj{};
obj.t = game::hks::TUSERDATA;
obj.v.ptr = value.ptr;
this->value_ = obj;
}
script_value::script_value(const table& value)
{
game::hks::HksObject obj{};
obj.t = game::hks::TTABLE;
obj.v.ptr = value.ptr;
this->value_ = obj;
}
script_value::script_value(const function& value)
{
game::hks::HksObject obj{};
obj.t = value.type;
obj.v.ptr = value.ptr;
this->value_ = obj;
}
/***************************************************************
* Integer
**************************************************************/
template <>
bool script_value::is<int>() const
{
const auto number = this->get_raw().v.number;
return this->get_raw().t == game::hks::TNUMBER && static_cast<int>(number) == number;
}
template <>
bool script_value::is<unsigned int>() const
{
return this->is<int>();
}
template <>
int script_value::get() const
{
return static_cast<int>(this->get_raw().v.number);
}
template <>
unsigned int script_value::get() const
{
return static_cast<unsigned int>(this->get_raw().v.number);
}
/***************************************************************
* Integer 64
**************************************************************/
template <>
bool script_value::is<long long>() const
{
return this->get_raw().t == game::hks::TUI64;
}
template <>
bool script_value::is<unsigned long long>() const
{
return this->is<long long>();
}
template <>
long long script_value::get() const
{
return static_cast<long long>(this->get_raw().v.ui64);
}
template <>
unsigned long long script_value::get() const
{
return static_cast<unsigned long long>(this->get_raw().v.ui64);
}
/***************************************************************
* Boolean
**************************************************************/
template <>
bool script_value::is<bool>() const
{
return this->get_raw().t == game::hks::TBOOLEAN;
}
template <>
bool script_value::get() const
{
return this->get_raw().v.boolean;
}
/***************************************************************
* Float
**************************************************************/
template <>
bool script_value::is<float>() const
{
return this->get_raw().t == game::hks::TNUMBER;
}
template <>
bool script_value::is<double>() const
{
return this->is<float>();
}
template <>
float script_value::get() const
{
return this->get_raw().v.number;
}
template <>
double script_value::get() const
{
return static_cast<double>(this->get_raw().v.number);
}
/***************************************************************
* String
**************************************************************/
template <>
bool script_value::is<const char*>() const
{
return this->get_raw().t == game::hks::TSTRING;
}
template <>
bool script_value::is<std::string>() const
{
return this->is<const char*>();
}
template <>
const char* script_value::get() const
{
return this->get_raw().v.str->m_data;
}
template <>
std::string script_value::get() const
{
return this->get<const char*>();
}
/***************************************************************
* Lightuserdata
**************************************************************/
template <>
bool script_value::is<lightuserdata>() const
{
return this->get_raw().t == game::hks::TLIGHTUSERDATA;
}
template <>
lightuserdata script_value::get() const
{
return this->get_raw().v.ptr;
}
/***************************************************************
* Userdata
**************************************************************/
template <>
bool script_value::is<userdata>() const
{
return this->get_raw().t == game::hks::TUSERDATA;
}
template <>
userdata script_value::get() const
{
return this->get_raw().v.ptr;
}
/***************************************************************
* Table
**************************************************************/
template <>
bool script_value::is<table>() const
{
return this->get_raw().t == game::hks::TTABLE;
}
template <>
table script_value::get() const
{
return this->get_raw().v.table;
}
/***************************************************************
* Function
**************************************************************/
template <>
bool script_value::is<function>() const
{
return this->get_raw().t == game::hks::TIFUNCTION
|| this->get_raw().t == game::hks::TCFUNCTION;
}
template <>
function script_value::get() const
{
return {this->get_raw().v.cClosure, this->get_raw().t};
}
/***************************************************************
*
**************************************************************/
const game::hks::HksObject& script_value::get_raw() const
{
return this->value_.get();
}
bool script_value::operator==(const script_value& other) const
{
if (this->get_raw().t != other.get_raw().t)
{
return false;
}
if (this->get_raw().t == game::hks::TSTRING)
{
return this->get<std::string>() == other.get<std::string>();
}
return this->get_raw().v.native == other.get_raw().v.native;
}
arguments script_value::operator()() const
{
return this->as<function>()();
}
arguments script_value::operator()(const arguments& arguments) const
{
return this->as<function>()(arguments);
}
function_argument::function_argument(const arguments& args, const script_value& value, const int index)
: values_(args)
, value_(value)
, index_(index)
{
}
function_arguments::function_arguments(const arguments& values)
: values_(values)
{
}
}

View File

@ -0,0 +1,256 @@
#pragma once
#include "game/game.hpp"
#include <utils/string.hpp>
namespace ui_scripting
{
class lightuserdata;
class userdata_value;
class userdata;
class table_value;
class table;
class function;
class script_value;
template <typename T>
std::string get_typename()
{
auto& info = typeid(T);
if (info == typeid(std::string) ||
info == typeid(const char*))
{
return "string";
}
if (info == typeid(lightuserdata))
{
return "lightuserdata";
}
if (info == typeid(userdata))
{
return "userdata";
}
if (info == typeid(table))
{
return "table";
}
if (info == typeid(function))
{
return "function";
}
if (info == typeid(int) ||
info == typeid(float) ||
info == typeid(unsigned int))
{
return "number";
}
if (info == typeid(bool))
{
return "boolean";
}
return info.name();
}
class hks_object
{
public:
hks_object() = default;
hks_object(const game::hks::HksObject& value);
hks_object(const hks_object& other) noexcept;
hks_object(hks_object&& other) noexcept;
hks_object& operator=(const hks_object& other) noexcept;
hks_object& operator=(hks_object&& other) noexcept;
~hks_object();
const game::hks::HksObject& get() const;
private:
void assign(const game::hks::HksObject& value);
void release();
game::hks::HksObject value_{game::hks::TNONE, {}};
int ref_{};
};
using arguments = std::vector<script_value>;
using event_arguments = std::unordered_map<std::string, script_value>;
class script_value
{
public:
script_value() = default;
script_value(const game::hks::HksObject& value);
script_value(int value);
script_value(unsigned int value);
script_value(long long value);
script_value(unsigned long long value);
script_value(bool value);
script_value(float value);
script_value(double value);
script_value(const char* value);
script_value(const char* value, const size_t len);
script_value(const std::string& value);
script_value(const lightuserdata& value);
script_value(const userdata& value);
script_value(const table& value);
script_value(const function& value);
template <template<class, class> class C, class T, typename TableType = table>
script_value(const C<T, std::allocator<T>>& container)
{
TableType table_{};
int index = 1;
for (const auto& value : container)
{
table_.set(index++, value);
}
game::hks::HksObject obj{};
obj.t = game::hks::TTABLE;
obj.v.ptr = table_.ptr;
this->value_ = obj;
}
template <typename F>
script_value(F f)
: script_value(function(f))
{
}
bool operator==(const script_value& other) const;
arguments operator()() const;
arguments operator()(const arguments& arguments) const;
template<class ...T>
arguments operator()(T... arguments) const
{
return this->as<function>().call({arguments...});
}
template <size_t Size>
table_value operator[](const char(&key)[Size]) const
{
return {this->as<table>(), key};
}
template <typename T = script_value>
table_value operator[](const T& key) const
{
return {this->as<table>(), key};
}
template <typename T>
bool is() const;
template <typename T>
T as() const
{
if (!this->is<T>())
{
const auto hks_typename = game::hks::s_compilerTypeName[this->get_raw().t + 2];
const auto typename_ = get_typename<T>();
throw std::runtime_error(utils::string::va("%s expected, got %s",
typename_.data(), hks_typename));
}
return get<T>();
}
template <typename T>
operator T() const
{
return this->as<T>();
}
const game::hks::HksObject& get_raw() const;
hks_object value_{};
private:
template <typename T>
T get() const;
};
class variadic_args : public arguments
{
};
class function_argument
{
public:
function_argument(const arguments& args, const script_value& value, const int index);
template <typename T>
T as() const
{
try
{
return this->value_.as<T>();
}
catch (const std::exception& e)
{
throw std::runtime_error(utils::string::va("bad argument #%d (%s)",
this->index_ + 1, e.what()));
}
}
template <>
variadic_args as() const
{
variadic_args args{};
for (auto i = this->index_; i < this->values_.size(); i++)
{
args.push_back(this->values_[i]);
}
return args;
}
template <typename T>
operator T() const
{
return this->as<T>();
}
private:
arguments values_{};
script_value value_{};
int index_{};
};
class function_arguments
{
public:
function_arguments(const arguments& values);
function_argument operator[](const int index) const
{
if (index >= values_.size())
{
return {values_, {}, index};
}
return {values_, values_[index], index};
}
private:
arguments values_{};
};
}

View File

@ -0,0 +1,354 @@
#include <std_include.hpp>
#include "types.hpp"
#include "execution.hpp"
namespace ui_scripting
{
/***************************************************************
* Lightuserdata
**************************************************************/
lightuserdata::lightuserdata(void* ptr_)
: ptr(ptr_)
{
}
/***************************************************************
* Userdata
**************************************************************/
userdata::userdata(void* ptr_)
: ptr(ptr_)
{
this->add();
}
userdata::userdata(const userdata& other)
{
this->operator=(other);
}
userdata::userdata(userdata&& other) noexcept
{
this->ptr = other.ptr;
this->ref = other.ref;
other.ref = 0;
}
userdata::~userdata()
{
this->release();
}
userdata& userdata::operator=(const userdata& other)
{
if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->ref = other.ref;
this->add();
}
return *this;
}
userdata& userdata::operator=(userdata&& other) noexcept
{
if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->ref = other.ref;
other.ref = 0;
}
return *this;
}
void userdata::add()
{
game::hks::HksObject value{};
value.v.ptr = this->ptr;
value.t = game::hks::TUSERDATA;
const auto state = *game::hks::lua_state;
const auto top = state->m_apistack.top;
push_value(value);
this->ref = game::hks::hksi_luaL_ref(*game::hks::lua_state, -10000);
state->m_apistack.top = top;
}
void userdata::release()
{
if (this->ref)
{
game::hks::hksi_luaL_unref(*game::hks::lua_state, -10000, this->ref);
}
}
void userdata::set(const script_value& key, const script_value& value) const
{
set_field(*this, key, value);
}
script_value userdata::get(const script_value& key) const
{
return get_field(*this, key);
}
userdata_value userdata::operator[](const script_value& key) const
{
return {*this, key};
}
userdata_value::userdata_value(const userdata& table, const script_value& key)
: userdata_(table)
, key_(key)
{
this->value_ = this->userdata_.get(key).get_raw();
}
void userdata_value::operator=(const script_value& value)
{
this->userdata_.set(this->key_, value);
this->value_ = value.get_raw();
}
bool userdata_value::operator==(const script_value& value)
{
return this->userdata_.get(this->key_) == value;
}
/***************************************************************
* Table
**************************************************************/
table::table()
{
const auto state = *game::hks::lua_state;
this->ptr = game::hks::Hashtable_Create(state, 0, 0);
this->add();
}
table::table(game::hks::HashTable* ptr_)
: ptr(ptr_)
{
this->add();
}
table::table(const table& other)
{
this->operator=(other);
}
table::table(table&& other) noexcept
{
this->ptr = other.ptr;
this->ref = other.ref;
other.ref = 0;
}
table::~table()
{
this->release();
}
table& table::operator=(const table& other)
{
if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->ref = other.ref;
this->add();
}
return *this;
}
table& table::operator=(table&& other) noexcept
{
if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->ref = other.ref;
other.ref = 0;
}
return *this;
}
void table::add()
{
game::hks::HksObject value{};
value.v.table = this->ptr;
value.t = game::hks::TTABLE;
const auto state = *game::hks::lua_state;
const auto top = state->m_apistack.top;
push_value(value);
this->ref = game::hks::hksi_luaL_ref(*game::hks::lua_state, -10000);
state->m_apistack.top = top;
}
void table::release()
{
if (this->ref)
{
game::hks::hksi_luaL_unref(*game::hks::lua_state, -10000, this->ref);
}
}
void table::set(const script_value& key, const script_value& value) const
{
set_field(*this, key, value);
}
table_value table::operator[](const script_value& key) const
{
return {*this, key};
}
script_value table::get(const script_value& key) const
{
return get_field(*this, key);
}
table_value::table_value(const table& table, const script_value& key)
: table_(table)
, key_(key)
{
this->value_ = this->table_.get(key).get_raw();
}
void table_value::operator=(const script_value& value)
{
this->table_.set(this->key_, value);
this->value_ = value.get_raw();
}
void table_value::operator=(const table_value& value)
{
this->table_.set(this->key_, value);
this->value_ = value.get_raw();
}
bool table_value::operator==(const script_value& value)
{
return this->table_.get(this->key_) == value;
}
bool table_value::operator==(const table_value& value)
{
return this->table_.get(this->key_) == value;
}
/***************************************************************
* Function
**************************************************************/
function::function(game::hks::lua_function func)
{
const auto state = *game::hks::lua_state;
this->ptr = game::hks::cclosure_Create(state, func, 0, 0, 0);
this->type = game::hks::HksObjectType::TCFUNCTION;
this->add();
}
function::function(game::hks::cclosure* ptr_, game::hks::HksObjectType type_)
: ptr(ptr_)
, type(type_)
{
this->add();
}
function::function(const function& other)
{
this->operator=(other);
}
function::function(function&& other) noexcept
{
this->ptr = other.ptr;
this->type = other.type;
this->ref = other.ref;
other.ref = 0;
}
function::~function()
{
this->release();
}
function& function::operator=(const function& other)
{
if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->type = other.type;
this->ref = other.ref;
this->add();
}
return *this;
}
function& function::operator=(function&& other) noexcept
{
if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->type = other.type;
this->ref = other.ref;
other.ref = 0;
}
return *this;
}
void function::add()
{
game::hks::HksObject value{};
value.v.cClosure = this->ptr;
value.t = this->type;
const auto state = *game::hks::lua_state;
const auto top = state->m_apistack.top;
push_value(value);
this->ref = game::hks::hksi_luaL_ref(*game::hks::lua_state, -10000);
state->m_apistack.top = top;
}
void function::release()
{
if (this->ref)
{
game::hks::hksi_luaL_unref(*game::hks::lua_state, -10000, this->ref);
}
}
arguments function::call(const arguments& arguments) const
{
return call_script_function(*this, arguments);
}
arguments function::operator()(const arguments& arguments) const
{
return this->call(arguments);
}
arguments function::operator()() const
{
return this->call({});
}
}

View File

@ -0,0 +1,140 @@
#pragma once
#include "game/game.hpp"
#include "script_value.hpp"
#include "component/ui_scripting.hpp"
namespace ui_scripting
{
class lightuserdata
{
public:
lightuserdata(void*);
void* ptr;
};
class userdata_value;
class userdata
{
public:
userdata(void*);
userdata(const userdata& other);
userdata(userdata&& other) noexcept;
~userdata();
userdata& operator=(const userdata& other);
userdata& operator=(userdata&& other) noexcept;
script_value get(const script_value& key) const;
void set(const script_value& key, const script_value& value) const;
userdata_value operator[](const script_value& key) const;
void* ptr;
private:
void add();
void release();
int ref{};
};
class userdata_value : public script_value
{
public:
userdata_value(const userdata& table, const script_value& key);
void operator=(const script_value& value);
bool operator==(const script_value& value);
private:
userdata userdata_;
script_value key_;
};
class table_value;
class table
{
public:
table();
table(game::hks::HashTable* ptr_);
table(const table& other);
table(table&& other) noexcept;
~table();
table& operator=(const table& other);
table& operator=(table&& other) noexcept;
script_value get(const script_value& key) const;
void set(const script_value& key, const script_value& value) const;
table_value operator[](const script_value& key) const;
game::hks::HashTable* ptr;
private:
void add();
void release();
int ref{};
};
class table_value : public script_value
{
public:
table_value(const table& table, const script_value& key);
void operator=(const script_value& value);
void operator=(const table_value& value);
bool operator==(const script_value& value);
bool operator==(const table_value& value);
private:
table table_;
script_value key_;
};
class function
{
public:
function(game::hks::lua_function);
function(game::hks::cclosure*, game::hks::HksObjectType);
template <typename F>
function(F f)
{
this->ptr = ui_scripting::convert_function(f);
this->type = game::hks::TCFUNCTION;
}
function(const function& other);
function(function&& other) noexcept;
~function();
function& operator=(const function& other);
function& operator=(function&& other) noexcept;
arguments call(const arguments& arguments) const;
arguments operator()(const arguments& arguments) const;
template<class ...T>
arguments operator()(T... arguments) const
{
return this->call({arguments...});
}
arguments operator()() const;
game::hks::cclosure* ptr;
game::hks::HksObjectType type;
private:
void add();
void release();
int ref{};
};
}