111
This commit is contained in:
@@ -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"));
|
||||
|
||||
Reference in New Issue
Block a user