Rindro-Sqr/Base/_Tool/BaseTool_Class.nut

398 lines
13 KiB
Plaintext

/*
文件名:BaseTool_Class.nut
路径:Base/_Tool/BaseTool_Class.nut
创建日期:2024-08-06 23:49
文件用途:基础工具类
*/
function printT(T) {
Sq_OutPutTable(Json.Encode(T));
}
//Json类
class Json {
function Encode(Table) {
local JsonObj = JSONEncoder();
return JsonObj.encode(Table);
local Size = Table.len();
local Pos = 0;
local Str = "{";
foreach(Key, Value in Table) {
++Pos;
Str += "\"";
Str += Key.tostring();
Str += "\"";
Str += ":";
if (typeof(Value) == "string") {
Str += "\"";
Str += Value;
Str += "\"";
} else if (typeof(Value) == "table") {
Str += Json.Encode(Value);
} else if (typeof(Value) == "array") {
Str += "[";
foreach(ArrObj in Value) {
if (typeof(ArrObj) == "table") Str += Json.Encode(Value);
else {
Str += ArrObj;
Str += ",";
}
}
Str = Str.slice(0, Str.len() - 1);
Str += "]";
} else Str += Value;
if (Pos != Size) Str += ",";
}
Str += "}";
return Str;
}
function Decode(Str) {
// Str = L_sq_DecondeJson(Str);
// local Str = "local _M = " + Str + ";\n return _M;\n";
// local Func = compilestring(Str);
// return Func();
local JsonObj = JSONParser();
return JsonObj.parse(Str);
}
}
//Key 键盘按键类
class KeyPress {
function KeyDown(obj, KeyValue) {
local KeyEvent = obj.getVar(KeyValue.tostring()); //声明储存器
KeyEvent.clear_vector(); //清空储存器的向量位
if (sq_IsKeyDown(KeyValue, ENUM_SUBKEY_TYPE_ALL) && KeyEvent.getInt(0) == 0) {
KeyEvent.setInt(0, 1);
return true;
}
}
function KeyUp(obj, KeyValue) {
local KeyEvent = obj.getVar(KeyValue.tostring()); //声明储存器
if (sq_IsKeyUp(KeyValue, ENUM_SUBKEY_TYPE_ALL)) {
KeyEvent.setInt(0, 0);
}
}
function isKeyPress(KeyValue) {
local obj = sq_getMyCharacter();
if (KeyPress.KeyDown(obj, KeyValue)) {
return true;
}
KeyPress.KeyUp(obj, KeyValue);
}
}
//Key 键盘按键类
class KeyPressNB {
function KeyDown(obj, KeyValue, VarKey) {
local KeyEvent = obj.getVar(VarKey); //声明储存器
KeyEvent.clear_vector(); //清空储存器的向量位
if (sq_IsKeyDown(KeyValue, ENUM_SUBKEY_TYPE_ALL) && KeyEvent.getInt(0) == 0) {
KeyEvent.setInt(0, 1);
return true;
}
}
function KeyUp(obj, KeyValue, VarKey) {
local KeyEvent = obj.getVar(VarKey); //声明储存器
if (sq_IsKeyUp(KeyValue, ENUM_SUBKEY_TYPE_ALL)) {
KeyEvent.setInt(0, 0);
}
}
function isKeyPress(KeyValue, VarKey) {
local obj = sq_getMyCharacter();
obj = sq_GetCNRDObjectToActiveObject(obj);
if (KeyPressNB.KeyDown(obj, KeyValue, VarKey)) {
return true;
}
KeyPressNB.KeyUp(obj, KeyValue, VarKey);
}
}
//基础工具类
class Rindro_BaseToolClass {
function GetItemNameById(ItemId) {
local ItemObject = L_sq_GetItem(ItemId);
local NamePointer = L_sq_RA(ItemObject + 0x20);
local ItemName = NativePointer(L_sq_I2P(NamePointer)).readUnicodeString();
return ItemName;
}
function SendPack(T) {
local str = Json.Encode(T);
L_sq_SendPackType(130);
L_sq_SendPackWChar(str);
L_sq_SendPack();
}
function SendPackEx(T) {
local str = L_sq_EncondeJson(T);
L_sq_SendPackType(130);
L_sq_SendPackWChar(str);
L_sq_SendPack();
}
function RegisterPack(Id, CallBack) {
Pack_Control.rawset(Id, CallBack);
}
//绘制简易静态Ani // obj -- ani路径 -- X -- Y -- 第几帧 -- ani名字
function T_DrawStayAni(obj, aniFileName, x, y, index, aniname) {
local SelectAni = obj.getVar().GetAnimationMap(aniname, aniFileName);
//sq_AnimationProc(SelectAni);
sq_DrawSpecificFrame(SelectAni, x, y, false, index, false, 1.0);
return SelectAni;
}
//绘制简易静态Ani // obj -- ani路径 -- X -- Y -- 第几帧 -- ani名字
function T_DrawStayAniRate(obj, aniFileName, x, y, index, aniname, rate) {
local SelectAni = obj.getVar().GetAnimationMap(aniname, aniFileName);
SelectAni.setImageRateFromOriginal(rate, rate);
//sq_AnimationProc(SelectAni);
sq_DrawSpecificFrame(SelectAni, x, y, false, index, false, 1.0);
return SelectAni;
}
//绘制简易动态Ani // obj -- ani路径 -- X -- Y -- ani名字
function T_DrawDynamicAni(obj, aniFileName, x, y, aniname) {
local ani = obj.getVar().GetAnimationMap(aniname, aniFileName);
sq_AnimationProc(ani);
sq_drawCurrentFrame(ani, x, y, true);
return ani;
}
//超简易绘制动态Ani
function DrawAniEx(X, Y, AniPath, ...) {
local AniName = "简易播放Ani:" + AniPath;
if (vargc > 0) AniName = vargv[0];
local obj = sq_getMyCharacter();
local ani = obj.getVar().GetAnimationMap(AniName, AniPath);
sq_AnimationProc(ani);
sq_drawCurrentFrame(ani, X, Y, true);
return ani;
}
//初始化根表成员
function InitClass(Name) {
local RootTab = getroottable();
if (RootTab.rawin(Name) == true) {
RootTab.rawdelete(Name);
}
}
//获取交互Npc名称
function GetEachNpcId() {
local OneAddress = L_sq_RA(0x1ADE0E0);
if (OneAddress) {
local TowAddress = L_sq_RA(OneAddress + 8);
if (TowAddress) {
local Id = L_sq_RA(TowAddress + 0x210);
return Id;
}
}
return null;
}
function DrawNineBox(X, Y, Width, Height, Img, StartIndex) {
//如果没有载入img就载入
if (!Rindro_Image_GlobalMap.rawin(Img)) {
Rindro_Image_GlobalMap[Img] <- Rindro_Image(Img);
}
//绘制左上角
Rindro_Image_GlobalMap[Img].DrawPng(StartIndex, X, Y);
//获取左上角的图片宽高
local LeftTopW = Rindro_Image_GlobalMap[Img].GetPng(StartIndex).GetWidth();
local LeftTopH = Rindro_Image_GlobalMap[Img].GetPng(StartIndex).GetHeight();
//绘制上边
//获取上边的宽
local TopW = Rindro_Image_GlobalMap[Img].GetPng(StartIndex + 1).GetWidth();
Rindro_Image_GlobalMap[Img].DrawExPng(StartIndex + 1, X + LeftTopW, Y, 0, sq_RGBA(255, 255, 255, 250), (Width - LeftTopW * 2).tofloat() / TopW, 1.0);
//绘制右上角
Rindro_Image_GlobalMap[Img].DrawPng(StartIndex + 2, X + Width - LeftTopW, Y);
//绘制左边
//获取左边的高
local LeftH = Rindro_Image_GlobalMap[Img].GetPng(StartIndex + 3).GetHeight();
Rindro_Image_GlobalMap[Img].DrawExPng(StartIndex + 3, X, Y + LeftTopH, 0, sq_RGBA(255, 255, 255, 250), 1.0, (Height - LeftTopH * 2).tofloat() / LeftH);
//绘制中间
//获取中间的宽高
local MiddleW = Rindro_Image_GlobalMap[Img].GetPng(StartIndex + 4).GetWidth();
local MiddleH = Rindro_Image_GlobalMap[Img].GetPng(StartIndex + 4).GetHeight();
Rindro_Image_GlobalMap[Img].DrawExPng(StartIndex + 4, X + LeftTopW, Y + LeftTopH, 0, sq_RGBA(255, 255, 255, 250), (Width - LeftTopW * 2).tofloat() / MiddleW, (Height - LeftTopH * 2).tofloat() / MiddleH);
//绘制右边
Rindro_Image_GlobalMap[Img].DrawExPng(StartIndex + 5, X + Width - LeftTopW, Y + LeftTopH, 0, sq_RGBA(255, 255, 255, 250), 1.0, (Height - LeftTopH * 2).tofloat() / LeftH);
//绘制左下角
Rindro_Image_GlobalMap[Img].DrawPng(StartIndex + 6, X, Y + Height - LeftTopH);
//绘制下边
Rindro_Image_GlobalMap[Img].DrawExPng(StartIndex + 7, X + LeftTopW, Y + Height - LeftTopH, 0, sq_RGBA(255, 255, 255, 250), (Width - LeftTopW * 2).tofloat() / TopW, 1.0);
//绘制右下角
Rindro_Image_GlobalMap[Img].DrawPng(StartIndex + 8, X + Width - LeftTopW, Y + Height - LeftTopH);
}
}
//获取文字绘制长度
class LenheartTextClass {
function GetStringLength(str) {
if (typeof str != "string") str = "无字符";
return L_sq_GetStringDrawLength(str);
}
}
class MemoryTool {
//给指定地址写入字节数组
function WirteByteArr(Address, ByteArr) {
for (local i = 0; i< ByteArr.len(); i++) {
L_sq_WAB(Address + i, ByteArr[i]);
}
}
//解密读取内存地址的数据
function DecodeMemoryData(Address) {
local nEax, nEcx8, nEsi, nEdx, nTmp;
nEax = NativePointer(L_sq_I2P(Address)).readInt();
if (nEax == -1) return nEax;
nEcx8 = NativePointer(L_sq_I2P(Address + 8)).readInt();
if (nEcx8 == -1) return nEcx8;
nEsi = NativePointer(L_sq_I2P(0x1AF8D78)).readInt();
if (nEsi == -1) return nEdx;
nEdx = nEax >> 16;
nTmp = (nEdx << 2) + nEsi + 36;
nEdx = NativePointer(L_sq_I2P(nTmp)).readInt();
if (nEdx == -1) return nEdx;
nEax = nEax & 65535;
nTmp = (nEax << 2) + nEdx + 8468;
nEax = NativePointer(L_sq_I2P(nTmp)).readInt();
if (nEax == -1) return nEax;
nEdx = nEax & 0xFFFF;
nEsi = nEdx << 16;
nEsi = nEsi | nEdx;
nEax = nEsi ^ nEcx8;
return nEax;
}
//加密写入内存地址数据
function EncodeMemoryData(AddreSs, Data) {
local JEdi, JEcx, JEax, JEsi, JEdx, JSs;
JEcx = AddreSs;
JEax = L_sq_RA(0x1AF8DB8);
JEax = JEax + 1;
L_sq_WA(0x1AF8DB8, JEax);
JEdx = JEax;
JEdx = JEdx >> 8;
JEdx = JEdx << 24;
JEdx = JEdx >> 24;
JEdx = L_sq_RA(JEdx * 2 + 0x1843F58);
JEdx = JEdx & 0xFFFF;
JEax = JEax << 24;
JEax = JEax >> 24;
JSs = L_sq_RA(JEax * 2 + 0x1844158);
JSs = JSs & 0xFFFF;
JEdx = JEdx ^ JSs;
JEax = JEdx;
JEax = JEax & 0xFFFF;
JEsi = Data;
JEdx = JEsi >> 16;
JSs = JEsi & 0xFFFF;
JEdx = JEdx + JSs;
JEdx = JEdx ^ JEax;
JEdi = JEdx;
JEdx = JEax;
JEax = JEax << 16;
JEax = JEax + JEdx;
JEsi = Data;
JEax = JEax ^ JEsi;
JEsi = AddreSs + 8;
L_sq_WA(JEsi, JEax);
JEax = L_sq_RA(AddreSs);
JEsi = L_sq_RA(0x1AF8D78);
JEcx = JEdi;
JEcx = JEcx << 16;
JEcx = JEcx + JEdx;
JEdx = JEax;
JEdx = JEdx >> 16;
JEdx = L_sq_RA(JEsi + JEdx * 4 + 36);
JEax = JEax & 0xFFFF;
L_sq_WA(JEdx + JEax * 4 + 8468, JEcx);
}
function DecodeMemoryDataF(Address) {
local Value = MemoryTool.DecodeMemoryData(Address);
local Bl = blob();
Bl.writen(Value, 'i');
Bl.seek(0);
return Bl.readn('f');
}
function ReadFloat(Address) {
local Arr = [];
for (local i = 0; i< 4; i++) {
Arr.append(L_sq_RAB(Address + i));
}
// local Value = L_sq_RAB(Address);
// print(Value);
local Bl = blob();
foreach(intvalue in Arr) {
Bl.writen(intvalue, 'c');
}
// Bl.writen(Value, 'i');
Bl.seek(0);
return Bl.readn('f');
}
}
//大数字
class longlong {
Value = null;
//构造函数 不管是不是string类型都要转成string类型
constructor(StrValue) {
Value = StrValue.tostring();
}
function _add(other) {
return longlong(L_sq_LongLongOperation(this.Value, other.Value, "+"));
}
function _sub(other) {
return longlong(L_sq_LongLongOperation(this.Value, other.Value, "-"));
}
function _mul(other) {
return longlong(L_sq_LongLongOperation(this.Value, other.Value, "*"));
}
function _div(other) {
return L_sq_LongLongOperation(this.Value, other.Value, "/");
}
function _unm() {
return longlong(L_sq_LongLongOperation(longlong("0"), this.Value, "-"));
}
function _modulo(other) {
return longlong(L_sq_LongLongOperation(this.Value, other.Value, "%"));
}
function GetFormat(FType) {
local Buf = L_sq_LongLongOperation(this.Value, FType, "format");
if (Buf.len()< 2) return Buf + ".0";
local Value = Buf.slice(0, -1);
local Unit = Buf.slice(-1);
local RetStr = format(FType + Unit, Value.tofloat());
return RetStr;
}
}