Move libcockatrice_server to libcockatrice_network/server/remote

Took 1 hour 41 minutes

Took 39 seconds

Took 4 minutes
This commit is contained in:
Lukas Brübach 2025-10-08 15:29:44 +02:00
parent eb7c4cae6c
commit 133c02f973
118 changed files with 218 additions and 338 deletions

View file

@ -5,10 +5,16 @@ add_subdirectory(libcockatrice/protocol/pb)
add_library(libcockatrice_protocol STATIC)
set(SOURCES
libcockatrice/protocol/debug_pb_message.cpp
libcockatrice/protocol/featureset.cpp
libcockatrice/protocol/get_pb_extension.cpp
libcockatrice/protocol/pending_command.cpp
)
set(HEADERS
libcockatrice/protocol/debug_pb_message.h
libcockatrice/protocol/featureset.h
libcockatrice/protocol/get_pb_extension.h
libcockatrice/protocol/pending_command.h
)
@ -21,7 +27,7 @@ target_sources(libcockatrice_protocol
add_dependencies(libcockatrice_protocol libcockatrice_protocol_pb)
# Link the actual generated protobuf library
target_link_libraries(libcockatrice_protocol PUBLIC ${COCKATRICE_QT_MODULES} libcockatrice_protocol_pb)
target_link_libraries(libcockatrice_protocol PUBLIC ${COCKATRICE_QT_MODULES} libcockatrice_protocol_pb libcockatrice_utility)
# Expose include paths
target_include_directories(

View file

@ -1,32 +0,0 @@
# Top-level wrapper for the protobuf library
add_subdirectory(pb)
add_library(libcockatrice_protocol STATIC)
set(SOURCES
pending_command.cpp
)
set(HEADERS
pending_command.h
)
target_sources(libcockatrice_protocol
PRIVATE
${SOURCES}
${HEADERS}
)
add_dependencies(libcockatrice_protocol libcockatrice_protocol_pb)
# Link the actual generated protobuf library
target_link_libraries(libcockatrice_protocol PUBLIC ${COCKATRICE_QT_MODULES} libcockatrice_protocol_pb)
# Expose include paths
target_include_directories(
libcockatrice_protocol
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}/pb # generated pb headers
)

View file

@ -0,0 +1,106 @@
#include "debug_pb_message.h"
#include <QList>
#include <QString>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>
#include <google/protobuf/text_format.h>
#include <libcockatrice/utility/trice_limits.h>
// FastFieldValuePrinter is added in protobuf 3.4, going out of our way to add the old FieldValuePrinter is not worth it
#if GOOGLE_PROTOBUF_VERSION > 3004000
// value printer to use for all values, will snip too long contents
class LimitedPrinter : public ::google::protobuf::TextFormat::FastFieldValuePrinter
{
public:
void PrintString(const std::string &val,
::google::protobuf::TextFormat::BaseTextGenerator *generator) const override;
};
// value printer to use for specifc values, will expunge sensitive info
class SafePrinter : public ::google::protobuf::TextFormat::FastFieldValuePrinter
{
public:
void PrintString(const std::string &val,
::google::protobuf::TextFormat::BaseTextGenerator *generator) const override;
static void applySafePrinter(const ::google::protobuf::Message &message,
::google::protobuf::TextFormat::Printer &printer);
};
void LimitedPrinter::PrintString(const std::string &val,
::google::protobuf::TextFormat::BaseTextGenerator *generator) const
{
auto length = val.length();
if (length > MAX_TEXT_LENGTH) {
::google::protobuf::TextFormat::FastFieldValuePrinter::PrintString(
val.substr(0, MAX_NAME_LENGTH) + "... ---snip--- (" + std::to_string(length) + " bytes total", generator);
} else {
::google::protobuf::TextFormat::FastFieldValuePrinter::PrintString(val, generator);
}
}
void SafePrinter::PrintString(const std::string & /*val*/,
::google::protobuf::TextFormat::BaseTextGenerator *generator) const
{
generator->PrintLiteral("\" ---value expunged--- \"");
}
void SafePrinter::applySafePrinter(const ::google::protobuf::Message &message,
::google::protobuf::TextFormat::Printer &printer)
{
const auto *reflection = message.GetReflection();
std::vector<const google::protobuf::FieldDescriptor *> fields;
reflection->ListFields(message, &fields);
for (const auto *field : fields) {
switch (field->cpp_type()) {
case ::google::protobuf::FieldDescriptor::CPPTYPE_STRING:
if (field->name().find("password") != std::string::npos) { // name contains password
auto *safePrinter = new SafePrinter();
if (!printer.RegisterFieldValuePrinter(field, safePrinter))
delete safePrinter; // in case safePrinter has not been taken ownership of
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
if (field->is_repeated()) {
for (int i = 0; i < reflection->FieldSize(message, field); ++i) {
applySafePrinter(reflection->GetRepeatedMessage(message, field, i), printer);
}
} else {
applySafePrinter(reflection->GetMessage(message, field), printer);
}
break;
default:
break;
}
}
}
#endif // GOOGLE_PROTOBUF_VERSION > 3004000
QString getSafeDebugString(const ::google::protobuf::Message &message)
{
#if GOOGLE_PROTOBUF_VERSION > 3001000
auto size = message.ByteSizeLong();
#else
auto size = message.ByteSize();
#endif
::google::protobuf::TextFormat::Printer printer;
printer.SetSingleLineMode(true); // compact mode
printer.SetExpandAny(true); // prints all fields
#if GOOGLE_PROTOBUF_VERSION > 3004000
// printer takes ownership of the LimitedPrinter and will delete it
printer.SetDefaultFieldValuePrinter(new LimitedPrinter());
// check field names an create SafePrinters for necessary fields
SafePrinter::applySafePrinter(message, printer);
#else
// removing passwords from debug output will only be supported on newer protobuf versions
printer.SetTruncateStringFieldLongerThan(MAX_TEXT_LENGTH);
#endif // GOOGLE_PROTOBUF_VERSION > 3004000
std::string debug_string;
printer.PrintToString(message, &debug_string);
return QString::number(size) + " bytes " + QString::fromStdString(debug_string);
}

View file

@ -0,0 +1,15 @@
#ifndef DEBUG_PB_MESSAGE_H
#define DEBUG_PB_MESSAGE_H
class QString;
namespace google
{
namespace protobuf
{
class Message;
}
} // namespace google
QString getSafeDebugString(const ::google::protobuf::Message &message);
#endif // DEBUG_PB_MESSAGE_H

View file

@ -0,0 +1,77 @@
#include "featureset.h"
#include <QDebug>
#include <QMap>
FeatureSet::FeatureSet()
{
}
QMap<QString, bool> FeatureSet::getDefaultFeatureList()
{
initalizeFeatureList(featureList);
return featureList;
}
void FeatureSet::initalizeFeatureList(QMap<QString, bool> &_featureList)
{
// default features [name], [is required to connect]
_featureList.insert("client_id", false);
_featureList.insert("client_ver", false);
_featureList.insert("feature_set", false);
_featureList.insert("user_ban_history", false);
_featureList.insert("room_chat_history", false);
_featureList.insert("client_warnings", false);
_featureList.insert("mod_log_lookup", false);
_featureList.insert("idle_client", false);
_featureList.insert("forgot_password", false);
_featureList.insert("websocket", false);
// featureList.insert("hashed_password_login", false);
// These are temp to force users onto a newer client
_featureList.insert("2.7.0_min_version", false);
_featureList.insert("2.8.0_min_version", false);
}
void FeatureSet::enableRequiredFeature(QMap<QString, bool> &_featureList, const QString &featureName)
{
if (_featureList.contains(featureName))
_featureList.insert(featureName, true);
}
void FeatureSet::disableRequiredFeature(QMap<QString, bool> &_featureList, const QString &featureName)
{
if (_featureList.contains(featureName))
_featureList.insert(featureName, false);
}
QMap<QString, bool>
FeatureSet::addFeature(QMap<QString, bool> &_featureList, const QString &featureName, bool isFeatureRequired)
{
_featureList.insert(featureName, isFeatureRequired);
return _featureList;
}
QMap<QString, bool> FeatureSet::identifyMissingFeatures(const QMap<QString, bool> &suppliedFeatures,
QMap<QString, bool> requiredFeatures)
{
QMap<QString, bool> missingList;
QMap<QString, bool>::iterator i;
for (i = requiredFeatures.begin(); i != requiredFeatures.end(); ++i) {
if (!suppliedFeatures.contains(i.key())) {
missingList.insert(i.key(), i.value());
}
}
return missingList;
}
bool FeatureSet::isRequiredFeaturesMissing(const QMap<QString, bool> &suppliedFeatures,
QMap<QString, bool> requiredFeatures)
{
QMap<QString, bool>::iterator i;
for (i = requiredFeatures.begin(); i != requiredFeatures.end(); ++i) {
if (i.value() && suppliedFeatures.contains(i.key())) {
return true;
}
}
return false;
}

View file

@ -0,0 +1,28 @@
#ifndef FEATURESET_H
#define FEATURESET_H
#include <QMap>
#include <QObject>
#include <QSet>
#include <QString>
class FeatureSet : public QObject
{
public:
FeatureSet();
QMap<QString, bool> getDefaultFeatureList();
void initalizeFeatureList(QMap<QString, bool> &_featureList);
void enableRequiredFeature(QMap<QString, bool> &_featureList, const QString &featureName);
void disableRequiredFeature(QMap<QString, bool> &_featureList, const QString &featureName);
QMap<QString, bool>
addFeature(QMap<QString, bool> &_featureList, const QString &featureName, bool isFeatureRequired);
QMap<QString, bool> identifyMissingFeatures(const QMap<QString, bool> &featureListToCheck,
QMap<QString, bool> featureListToCompareTo);
bool isRequiredFeaturesMissing(const QMap<QString, bool> &featureListToCheck,
QMap<QString, bool> featureListToCompareTo);
private:
QMap<QString, bool> featureList;
};
#endif // FEEATURESET_H

View file

@ -0,0 +1,14 @@
#include "get_pb_extension.h"
#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>
int getPbExtension(const ::google::protobuf::Message &message)
{
std::vector<const ::google::protobuf::FieldDescriptor *> fieldList;
message.GetReflection()->ListFields(message, &fieldList);
for (unsigned int j = 0; j < fieldList.size(); ++j)
if (fieldList[j]->is_extension())
return fieldList[j]->number();
return -1;
}

View file

@ -0,0 +1,14 @@
#ifndef GET_PB_EXTENSION_H
#define GET_PB_EXTENSION_H
namespace google
{
namespace protobuf
{
class Message;
}
} // namespace google
int getPbExtension(const ::google::protobuf::Message &message);
#endif