111
This commit is contained in:
63
include/SqrReg_Dio.hpp
Normal file
63
include/SqrReg_Dio.hpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
#include "squirrel.h"
|
||||
#include "sqstdaux.h"
|
||||
#include "sqstdblob.h"
|
||||
#include "sqstdio.h"
|
||||
#include "sqstdmath.h"
|
||||
#include "sqstdstring.h"
|
||||
#include "sqstdsystem.h"
|
||||
#include <asio.hpp>
|
||||
#include <asio/ssl.hpp>
|
||||
#include <asio/basic_socket_streambuf.hpp>
|
||||
#include <iostream>
|
||||
|
||||
static SQInteger CreateHttp(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *Host;
|
||||
sq_getstring(v, 2, &Host);
|
||||
const SQChar *Sevice;
|
||||
sq_getstring(v, 3, &Sevice);
|
||||
const SQChar *Content;
|
||||
sq_getstring(v, 4, &Content);
|
||||
|
||||
asio::io_context ioContext;
|
||||
asio::ip::tcp::resolver resolver(ioContext);
|
||||
asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(Host, Sevice);
|
||||
|
||||
asio::ip::tcp::socket socket(ioContext);
|
||||
asio::connect(socket, endpoints);
|
||||
|
||||
std::string request = std::string(Content);
|
||||
asio::write(socket, asio::buffer(request));
|
||||
|
||||
asio::streambuf response;
|
||||
asio::error_code error;
|
||||
asio::read(socket, response, error);
|
||||
|
||||
if (error != asio::error::eof)
|
||||
{
|
||||
throw asio::system_error(error);
|
||||
sq_pushnull(v);
|
||||
}
|
||||
|
||||
std::istream responseStream(&response);
|
||||
std::ostringstream oss;
|
||||
oss << responseStream.rdbuf();
|
||||
sq_pushstring(v, oss.str().c_str(), -1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger register_Dio_func(HSQUIRRELVM v, SQFUNCTION f, const char *fname)
|
||||
{
|
||||
sq_pushroottable(v);
|
||||
sq_pushstring(v, fname, -1);
|
||||
sq_newclosure(v, f, 0); // create a new function
|
||||
sq_newslot(v, -3, SQFalse);
|
||||
sq_pop(v, 1); // pops the root table
|
||||
}
|
||||
|
||||
static void RegisterDio(HSQUIRRELVM v)
|
||||
{
|
||||
// 创建Http
|
||||
register_Dio_func(v, CreateHttp, _SC("Sq_CreateHttp"));
|
||||
}
|
||||
@@ -10,10 +10,12 @@
|
||||
#include "croncpp.h"
|
||||
#include "l_socket.h"
|
||||
#include <openssl/md5.h>
|
||||
#include <opencc/opencc.h>
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <ffi.h>
|
||||
#include <iconv.h>
|
||||
#include "nlohmann/json.hpp"
|
||||
|
||||
#define CONTAINS_STRING(str, substr) (str == substr)
|
||||
@@ -883,12 +885,108 @@ static SQInteger L_Sq_Cron_Next(HSQUIRRELVM v)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _stream_myreadstring(HSQUIRRELVM v)
|
||||
{
|
||||
SQStream *self = NULL;
|
||||
if (SQ_FAILED(sq_getinstanceup(v, 1, (SQUserPointer *)&self, (SQUserPointer)((SQUnsignedInteger)SQSTD_STREAM_TYPE_TAG), SQFalse)))
|
||||
return sq_throwerror(v, _SC("invalid type tag"));
|
||||
if (!self || !self->IsValid())
|
||||
return sq_throwerror(v, _SC("the stream is invalid"));
|
||||
|
||||
SQInteger Count;
|
||||
sq_getinteger(v, 2, &Count);
|
||||
char *Str = new char[Count + 1];
|
||||
self->Read(Str, Count);
|
||||
std::string Sstr(Str, Count);
|
||||
delete[] Str;
|
||||
sq_pushstring(v, Sstr.c_str(), -1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger _CrcDecode(HSQUIRRELVM v)
|
||||
{
|
||||
SQInteger Buffer[4];
|
||||
|
||||
sq_pushnull(v); // null iterator
|
||||
int Idx = 0;
|
||||
while (SQ_SUCCEEDED(sq_next(v, -2)))
|
||||
{
|
||||
sq_getinteger(v, -1, &Buffer[Idx]);
|
||||
Idx++;
|
||||
sq_pop(v, 2);
|
||||
}
|
||||
sq_pop(v, 1);
|
||||
|
||||
SQInteger crc32;
|
||||
sq_getinteger(v, 3, &crc32);
|
||||
|
||||
int64 num = 2175242257;
|
||||
int64 anInt = ((Buffer[0]) << 0) | ((Buffer[1]) << 8) | ((Buffer[2]) << 16) | ((Buffer[3]) << 24);
|
||||
int64 val = (anInt ^ num ^ crc32);
|
||||
int64 jiemi = (val >> 6) | ((val << (32 - 6)) & 0xFFFFFFFF);
|
||||
|
||||
sq_newarray(v, 0);
|
||||
for (size_t i = 0; i < 4; i++)
|
||||
{
|
||||
sq_pushinteger(v, (jiemi >> (i * 8)) & 0xFF);
|
||||
sq_arrayappend(v, -2);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger L_Conversion(HSQUIRRELVM v)
|
||||
{
|
||||
const SQChar *Str;
|
||||
sq_getstring(v, 2, &Str);
|
||||
SQInteger Type;
|
||||
sq_getinteger(v, 3, &Type);
|
||||
|
||||
std::string traditionalStr = std::string(Str);
|
||||
|
||||
opencc_t ot;
|
||||
if (Type == 0)
|
||||
ot = opencc_open(OPENCC_DEFAULT_CONFIG_TRAD_TO_SIMP);
|
||||
else if (Type == 1)
|
||||
ot = opencc_open(OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD);
|
||||
|
||||
char *NewStr = opencc_convert_utf8(ot, traditionalStr.c_str(), traditionalStr.length());
|
||||
std::string RetStr(NewStr);
|
||||
sq_pushstring(v, RetStr.c_str(), -1);
|
||||
opencc_convert_utf8_free(NewStr);
|
||||
opencc_close(ot);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static SQInteger L_GetTimestampString(HSQUIRRELVM v)
|
||||
{
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
|
||||
auto value = now_ms.time_since_epoch();
|
||||
long long milliseconds = value.count();
|
||||
|
||||
std::ostringstream oss;
|
||||
oss << milliseconds;
|
||||
|
||||
sq_pushstring(v, oss.str().c_str(), -1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void RegisterGame(HSQUIRRELVM v)
|
||||
{
|
||||
getConfigPath(szGamePath, sizeof(szGamePath));
|
||||
|
||||
// 获取字符串时间戳
|
||||
register_World_func(v, L_GetTimestampString, _SC("Sq_GetTimestampString"));
|
||||
|
||||
// 简繁转换
|
||||
register_World_func(v, L_Conversion, _SC("Sq_Conversion"));
|
||||
|
||||
// 获取下一个cron
|
||||
register_World_func(v, L_Sq_Cron_Next, _SC("Sq_Cron_Next"));
|
||||
// 自身封装读取流
|
||||
register_World_func(v, _stream_myreadstring, _SC("stream_myreadstring"));
|
||||
register_World_func(v, _CrcDecode, _SC("Sq_CrcDecode"));
|
||||
|
||||
// int 和指针相互转换
|
||||
register_World_func(v, RunScript, _SC("sq_RunScript"));
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
#include "sqstdmath.h"
|
||||
#include "sqstdstring.h"
|
||||
#include "sqstdsystem.h"
|
||||
#include "sqstddio.h"
|
||||
#include <curl/curl.h>
|
||||
#include "Tool.h"
|
||||
#include <map>
|
||||
#include <fstream>
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "SqrReg_Dungeon.hpp" //副本类
|
||||
#include "SqrReg_Memory.hpp" //内存类
|
||||
#include "SqrReg_ActiveHook.hpp" //动态HOOK
|
||||
#include "SqrReg_Dio.hpp" //网络类
|
||||
#include <iostream>
|
||||
#include <stdarg.h>
|
||||
|
||||
@@ -32,5 +33,6 @@ static void GlobaRegisterSquirrel(HSQUIRRELVM v)
|
||||
RegisterPacket(v); // 发包类
|
||||
RegisterUser(v); // 用户类
|
||||
RegisterMemory(v); // 内存类
|
||||
RegisterActiveHook(v); // 内存类
|
||||
RegisterActiveHook(v); // 动态HOOK
|
||||
RegisterDio(v); // 网络类
|
||||
}
|
||||
Reference in New Issue
Block a user