This commit is contained in:
aozhiwei 2022-12-25 15:06:03 +08:00
parent be01cfc54f
commit a6a7c3f751
2 changed files with 8 additions and 472 deletions

View File

@ -18,362 +18,16 @@
namespace f8
{
bool ReadCsvMetaFile(const std::string& filename,
google::protobuf::Message* prototype,
std::function<void (google::protobuf::Message*)> push_back_func)
{
const google::protobuf::Descriptor* descriptor = prototype->GetDescriptor();
const google::protobuf::Reflection* reflection = prototype->GetReflection();
a8::CsvReader reader;
reader.Load(filename);
while (reader.NextLine()) {
google::protobuf::Message* msg = prototype->New();
for (int i = 0; i < descriptor->field_count(); ++i) {
const google::protobuf::FieldDescriptor* field_desc = descriptor->field(i);
const std::string& field_name = field_desc->name();
if (field_name.empty() || field_name[0] == '_') {
continue;
}
if (!reader.KeyExists(field_name) && field_desc->is_optional()) {
continue;
}
switch (field_desc->cpp_type()) {
case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
{
reflection->SetString(msg, field_desc, reader.GetValue(field_name));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
{
reflection->SetInt32(msg, field_desc, reader.GetValue(field_name));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
{
reflection->SetUInt32(msg, field_desc, reader.GetValue(field_name));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
{
reflection->SetInt64(msg, field_desc, reader.GetValue(field_name));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
{
reflection->SetUInt64(msg, field_desc, reader.GetValue(field_name));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
{
reflection->SetFloat(msg, field_desc, (double)reader.GetValue(field_name));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
{
reflection->SetDouble(msg, field_desc, reader.GetValue(field_name));
}
break;
default:
{
abort();
}
break;
}//end switch
}//end for
push_back_func(msg);
delete msg;
}
return true;
}
static bool JsonToMessage(a8::XObject& jsonobj, google::protobuf::Message* msg)
{
const google::protobuf::Descriptor* descriptor = msg->GetDescriptor();
const google::protobuf::Reflection* reflection = msg->GetReflection();
for (int i = 0; i < descriptor->field_count(); ++i) {
const google::protobuf::FieldDescriptor* field_desc = descriptor->field(i);
const std::string& field_name = field_desc->name();
if (field_name.empty() || field_name[0] == '_') {
continue;
}
if (!jsonobj.HasKey(field_name) && field_desc->is_optional()) {
continue;
}
if (field_desc->is_repeated()) {
std::shared_ptr<a8::XObject> repeated_field = jsonobj.At(field_name);
if (!repeated_field) {
continue;
}
for (int i = 0; i < repeated_field->Size(); ++i) {
auto field_value = repeated_field->At(i);
switch (field_desc->cpp_type()) {
case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
{
reflection->AddString(msg, field_desc, field_value->AsXValue());
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
{
reflection->AddInt32(msg, field_desc, field_value->AsXValue());
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
{
reflection->AddUInt32(msg, field_desc, field_value->AsXValue());
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
{
reflection->AddInt64(msg, field_desc, field_value->AsXValue());
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
{
reflection->AddUInt64(msg, field_desc, field_value->AsXValue());
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
{
reflection->AddFloat(msg, field_desc, (double)field_value->AsXValue());
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
{
reflection->AddDouble(msg, field_desc, field_value->AsXValue());
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
{
google::protobuf::Message* p = reflection->AddMessage(msg, field_desc);
JsonToMessage(*field_value, p);
}
break;
default:
{
abort();
}
break;
}//end switch
}
} else {
switch (field_desc->cpp_type()) {
case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
{
reflection->SetString(msg, field_desc, jsonobj.At(field_name)->AsXValue());
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
{
reflection->SetInt32(msg, field_desc, jsonobj.At(field_name)->AsXValue()); }
break;
case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
{
reflection->SetUInt32(msg, field_desc, jsonobj.At(field_name)->AsXValue());
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
{
reflection->SetInt64(msg, field_desc, jsonobj.At(field_name)->AsXValue());
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
{
reflection->SetUInt64(msg, field_desc, jsonobj.At(field_name)->AsXValue());
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
{
reflection->SetFloat(msg, field_desc, (double)jsonobj.At(field_name)->AsXValue());
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
{
reflection->SetDouble(msg, field_desc, jsonobj.At(field_name)->AsXValue());
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
{
google::protobuf::Message* p = reflection->MutableMessage(msg, field_desc);
JsonToMessage(*jsonobj.At(field_name), p);
}
break;
default:
{
abort();
}
break;
}//end switch
}
}
return true;
}
static bool MessageToJson(const google::protobuf::Message* msg, a8::MutableXObject& jsonobj)
{
const google::protobuf::Descriptor* descriptor = msg->GetDescriptor();
const google::protobuf::Reflection* reflection = msg->GetReflection();
for (int i = 0; i < descriptor->field_count(); ++i) {
const google::protobuf::FieldDescriptor* field_desc = descriptor->field(i);
const std::string& field_name = field_desc->name();
if (field_desc->is_repeated()) {
a8::MutableXObject* array_obj = a8::MutableXObject::NewArray();
std::shared_ptr<a8::XObject> repeated_field = jsonobj.At(field_name);
continue;
for (int i = 0; i < reflection->FieldSize(*msg, field_desc); ++i) {
switch (field_desc->cpp_type()) {
case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
{
array_obj->Push(reflection->GetRepeatedBool(*msg, field_desc, i));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
{
array_obj->Push(reflection->GetRepeatedString(*msg, field_desc, i));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
{
array_obj->Push(reflection->GetRepeatedInt32(*msg, field_desc, i));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
{
array_obj->Push(reflection->GetRepeatedUInt32(*msg, field_desc, i));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
{
array_obj->Push(reflection->GetRepeatedInt64(*msg, field_desc, i));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
{
array_obj->Push(reflection->GetRepeatedUInt64(*msg, field_desc, i));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
{
array_obj->Push(reflection->GetRepeatedFloat(*msg, field_desc, i));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
{
array_obj->Push(reflection->GetRepeatedDouble(*msg, field_desc, i));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
{
a8::MutableXObject* p = a8::MutableXObject::NewObject();
MessageToJson(&reflection->GetRepeatedMessage(*msg, field_desc, i),
*p
);
array_obj->Push(*p);
delete p;
}
break;
default:
{
abort();
}
break;
}//end switch
}
jsonobj.SetVal(field_name, *array_obj);
delete array_obj;
} else {
switch (field_desc->cpp_type()) {
case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
{
jsonobj.SetVal(field_name, reflection->GetBool(*msg, field_desc));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
{
jsonobj.SetVal(field_name, reflection->GetString(*msg, field_desc));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
{
jsonobj.SetVal(field_name, reflection->GetInt32(*msg, field_desc));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
{
jsonobj.SetVal(field_name, reflection->GetUInt32(*msg, field_desc));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
{
jsonobj.SetVal(field_name, reflection->GetInt64(*msg, field_desc));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
{
jsonobj.SetVal(field_name, reflection->GetUInt64(*msg, field_desc));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
{
jsonobj.SetVal(field_name, reflection->GetFloat(*msg, field_desc));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
{
jsonobj.SetVal(field_name, reflection->GetDouble(*msg, field_desc));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
{
a8::MutableXObject* p = a8::MutableXObject::NewObject();
MessageToJson(&reflection->GetMessage(*msg, field_desc),
*p
);
jsonobj.SetVal(field_name, *p);
}
break;
default:
{
abort();
}
break;
}//end switch
}
}
return true;
}
bool ReadJsonMetaFile(const std::string& filename,
google::protobuf::Message* prototype,
std::function<void (google::protobuf::Message*)> push_back_func)
{
a8::XObject json_reader;
json_reader.ReadFromJsonFile(filename);
for (int i = 0; i < json_reader.Size(); ++i) {
std::shared_ptr<a8::XObject> p = json_reader.At(i);
google::protobuf::Message* msg = prototype->New();
JsonToMessage(*p, msg);
push_back_func(msg);
delete msg;
}
return true;
}
std::string PbToJson(const google::protobuf::Message* msg)
{
std::string data;
a8::MutableXObject* p = a8::MutableXObject::NewObject();
MessageToJson(msg, *p);
p->ToJsonStr(data);
delete p;
return data;
std::string json;
google::protobuf::util::JsonPrintOptions options;
options.add_whitespace = true;
options.always_print_primitive_fields = true;
options.preserve_proto_field_names = true;
MessageToJsonString(*msg, &json, options).ok();
return json;
}
void InitMysqlConnection(a8::mysql::Query* query)
@ -464,22 +118,6 @@ namespace f8
return true;
}
bool LoginCheck(const std::string& accountid, const std::string& sessionid)
{
std::vector<std::string> strings;
a8::Split(sessionid, strings, '_');
if (strings.size() < 4) {
return false;
}
if (IsOnlineEnv()) {
//session_createtime account_registertime md51 md52
std::string md5_str = accountid + "f3a6a9a5-217a-4079-ab99-b5d69b8212be" +
strings[1] + strings[0];
return a8::openssl::md5(md5_str) == strings[2];
}
return true;
}
int ExtractGameIdFromAccountId(const std::string& accountid)
{
std::vector<std::string> strings;
@ -535,15 +173,4 @@ namespace f8
return true;
}
std::string PbToJson(google::protobuf::Message* pbmsg)
{
std::string json;
google::protobuf::util::JsonPrintOptions options;
options.add_whitespace = true;
options.always_print_primitive_fields = true;
options.preserve_proto_field_names = true;
MessageToJsonString(*pbmsg, &json, options).ok();
return json;
}
}

View File

@ -4,15 +4,6 @@
#include <a8/stringlist.h>
#include <a8/csvreader.h>
namespace google
{
namespace protobuf
{
class Descriptor;
class Message;
}
}
namespace a8
{
namespace mysql
@ -24,29 +15,6 @@ namespace a8
namespace f8
{
typedef void (*ReadCsvMetaFileCallback)(const std::string&);
bool ReadCsvMetaFile(const std::string& filename,
google::protobuf::Message* prototype,
std::function<void (google::protobuf::Message*)> push_back_func);
template <typename T>
bool ReadCsvMetaFile(const std::string& filename, std::list<T>& meta_list,
ReadCsvMetaFileCallback callback_func = nullptr)
{
if (callback_func) {
callback_func(filename);
}
T dummy;
return ReadCsvMetaFile(filename,
&dummy,
[&meta_list] (google::protobuf::Message* msg)
{
T t;
t.CopyFrom(*msg);
meta_list.emplace_back(t);
});
}
template <typename T>
bool ReadCsvMetaFile(const std::string& filename, std::vector<T*>& meta_list)
@ -105,68 +73,11 @@ namespace f8
}
meta_list.push_back(obj);
}
}
bool ReadJsonMetaFile(const std::string& filename,
google::protobuf::Message* prototype,
std::function<void (google::protobuf::Message*)> push_back_func);
template <typename T>
bool ReadJsonMetaFile(const std::string& filename, std::list<T>& meta_list)
{
T dummy;
return ReadJsonMetaFile(filename,
&dummy,
[&meta_list] (google::protobuf::Message* msg)
{
T t;
t.CopyFrom(*msg);
meta_list.emplace_back(t);
});
return true;
}
std::string PbToJson(const google::protobuf::Message* msg);
template <typename T1,
typename T2>
static void RepeatedFieldToVector(const T1& t1, T2& t2)
{
t2.clear();
for (auto& val : t1) {
t2.push_back(val);
}
}
template <typename T1,
typename T2>
static void RepeatedFieldToSet(const T1& t1, T2& t2)
{
t2.clear();
for (auto& val : t1) {
t2.insert(val);
}
}
template <typename T1,
typename T2>
static void VectorToRepeatedField(const T1& t1, T2& t2)
{
t2.Clear();
for (auto& val : t1) {
*t2.Add() = val;
}
}
template <typename T1,
typename T2>
static void SetToRepeatedField(const T1& t1, T2& t2)
{
t2.Clear();
for (auto& val : t1) {
*t2.Add() = val;
}
}
void InitMysqlConnection(a8::mysql::Query* query);
void CheckMysqlConnection(a8::mysql::Connection* conn, a8::mysql::Query* query,
std::string dbhost, int port, std::string dbuser, std::string dbpasswd, std::string gamedb);
@ -174,7 +85,6 @@ namespace f8
bool CheckRegisterTimeInSessionId(const std::string& accountid, const std::string& sessionid);
time_t ExtractRegisterTimeFromSessionId(const std::string& sessionid);
bool IsValidSessionId(const std::string& accountid, const std::string& sessionid);
bool LoginCheck(const std::string& accountid, const std::string& sessionid);
int ExtractGameIdFromAccountId(const std::string& accountid);
int ExtractChannelIdFromAccountId(const std::string& accountid);
@ -182,5 +92,4 @@ namespace f8
bool IsTestEnv();
bool IsValidNormalConfig(a8::XObject& conf, std::vector<std::string> fields);
std::string PbToJson(google::protobuf::Message* pbmsg);
}