diff --git a/test/dllmain.cpp b/test/dllmain.cpp index 4ef3d52..a810154 100644 --- a/test/dllmain.cpp +++ b/test/dllmain.cpp @@ -78,10 +78,13 @@ int HOOK() } + + void testcall() { + //SQdofile(GetSqVm(), L"1.cpp", false, false); + //SQloadfile(GetSqVm(), L"1.cpp", false); - //SQDofile(GetSqVm(),L"1.cpp",0,0); //std::cout << gm(0x186FB828) << std::endl; diff --git a/test/pch.h b/test/pch.h index a73e1f9..950eb87 100644 --- a/test/pch.h +++ b/test/pch.h @@ -56,8 +56,11 @@ static SqGetlocal* SQGetlocal = (SqGetlocal*)0x135AFD0; typedef int (Sq_Wakeupvm)(uint32_t v, int n, int retval, int raiseerror); static Sq_Wakeupvm* SQ_Wakeupvm = (Sq_Wakeupvm*)0x135B0D0; //SQ_Call -typedef int (Sq_Call)(uint32_t v, int params, int retval, int raiseerror); -static Sq_Call* SQ_Call = (Sq_Call*)0x1359280; +typedef int (Sq_CallFunc)(uint32_t v, int params, int retval, int raiseerror); +static Sq_CallFunc* SQ_Call = (Sq_CallFunc*)0x1359280; +//SQ_Remove +typedef int (SQ_RemoveFunc)(uint32_t v, int idx); +static SQ_RemoveFunc* SQ_Remove = (SQ_RemoveFunc*)0x1359000; //GetInt typedef int(SqGetIntFunc)(uint32_t v, uint32_t stackIndex, int* sint); static SqGetIntFunc* SQGetInt = (SqGetIntFunc*)0x1358D70; @@ -82,6 +85,9 @@ static SqGetUserpointerFunc* SQGetUserpointer = (SqGetUserpointerFunc*)0x1358F80 //GetTop typedef int(SqGetTopFunc)(uint32_t v); static SqGetTopFunc* SQGetTop = (SqGetTopFunc*)0x1358FC0; +//Push +typedef int(SqPushFunc)(uint32_t v, int idx); +static SqPushFunc* SQPush = (SqPushFunc*)0x1358C90; //PushString typedef int(SqPushStringFunc)(uint32_t v, const wchar_t* s, int len); static SqPushStringFunc* SQPushString = (SqPushStringFunc*)0x1358A60; @@ -95,6 +101,48 @@ static SqPushBoolFunc* SQPushBool = (SqPushBoolFunc*)0x1358B10; typedef int(SqPushFloatFunc)(uint32_t v, float sfloat); static SqPushFloatFunc* SQPushFloat = (SqPushFloatFunc*)0x1358B60; + + +//SQLOADFILE +typedef void* (SQLOADFILEFunc)(uint32_t v, const wchar_t* filename, bool printerror); +static SQLOADFILEFunc* SQLOADFILEF = (SQLOADFILEFunc*)0x13560E0; +//SQDOFILE +typedef void* (SQDOFILEFunc)(uint32_t v, const wchar_t* filename, bool retval, bool printerror); +static SQDOFILEFunc* SQDOFILE = (SQDOFILEFunc*)0x1356240; + + +//SQ_io_file_lexfeed_编码 +//typedef int(LSQLEXREADFUNC)(int n); +typedef int(*LSQLEXREADFUNC)(int n); +static LSQLEXREADFUNC SQ_io_file_lexfeed_ASCII = (LSQLEXREADFUNC)0x1355F60; +static LSQLEXREADFUNC SQ_io_file_lexfeed_UCS2_BE = (LSQLEXREADFUNC)0x1356050; +static LSQLEXREADFUNC SQ_io_file_lexfeed_UCS2_LE = (LSQLEXREADFUNC)0x1356020; +static LSQLEXREADFUNC SQ_io_file_lexfeed_UTF8 = (LSQLEXREADFUNC)0x1355F90; +//SQfopen +typedef void* (SQfopenFunc)(const wchar_t* filename, const wchar_t* type); +static SQfopenFunc* SQfopen = (SQfopenFunc*)0x1355DC0; +//SQfread +typedef int(SQfreadFunc)(void* buffer, int a, int b, void* file); +static SQfreadFunc* SQfread = (SQfreadFunc*)0x1355DD0; +//SQfseek +typedef int(SQfseekFunc)(void* buffer, int a, int b); +static SQfseekFunc* SQfseek = (SQfseekFunc*)0x1355DF0; +//SQ_File_read +typedef int(SQ_File_readFunc)(void* file, void* afile, int a); +static SQ_File_readFunc* SQ_File_read = (SQ_File_readFunc*)0x1356090; +//SQ_Readclosure +typedef int(SQ_ReadclosureFunc)(uint32_t v, SQ_File_readFunc a, void* file); +static SQ_ReadclosureFunc* SQ_Readclosure = (SQ_ReadclosureFunc*)0x1359460; +//SQ_Fclose +typedef int(SQ__FcloseFunc)(void* file); +static SQ__FcloseFunc* SQ__Fclose = (SQ__FcloseFunc*)0x1355E70; +//SQ_Compile +typedef int(SQ_CompileFunc)(uint32_t v, LSQLEXREADFUNC FUNC, void* file, const wchar_t* filename, bool printerror); +static SQ_CompileFunc* SQ_Compile = (SQ_CompileFunc*)0x135A390; +//SQ_Throwerror +typedef int(SQ_ThrowerrorFunc)(uint32_t v, const wchar_t* error); +static SQ_ThrowerrorFunc* SQ_Throwerror = (SQ_ThrowerrorFunc*)0x13591A0; + //获取Squirrel v 基址 inline uint32_t GetSqVm(); @@ -168,8 +216,6 @@ static int GoDungeon(uint32_t v) - - //获取EXE使用头 号位数据 int GetExeNutWrtNum(int Pos); diff --git a/test/test.vcxproj b/test/test.vcxproj index b537bf2..b8941ac 100644 --- a/test/test.vcxproj +++ b/test/test.vcxproj @@ -97,7 +97,7 @@ Console - false + true false @@ -112,12 +112,13 @@ Use pch.h MultiThreaded + EditAndContinue Console true true - false + true false squirrel.lib;libMinHook.x86.lib;%(AdditionalDependencies) diff --git a/test/函数块.cpp b/test/函数块.cpp index 4bf29c3..55780a9 100644 --- a/test/函数块.cpp +++ b/test/函数块.cpp @@ -237,6 +237,96 @@ int GetEquAddr(int addr) } return -1; } +// +static int SQloadfile(uint32_t v, const wchar_t* filename, bool printerror) +{ + void* file = SQfopen(filename, L"rb"); + //void* file = (void*)"IIIAAA <- 66487"; + int ret; + unsigned short us; + unsigned char uc; + LSQLEXREADFUNC func = SQ_io_file_lexfeed_ASCII; + if (file) + { + ret = SQfread(&us, 1, 2, file); + if (ret != 2) + { + us = 0; + } + if (us == 0xFAFA) + { + SQfseek(file, 0, 2); + if (SQ_Readclosure(v, SQ_File_read, file) > 0) + { + SQ__Fclose(file); + return SQ_OK; + } + } + else + { + switch (us) + { + case 0xFFFE: func = SQ_io_file_lexfeed_UCS2_BE; break;//UTF-16 little endian; + case 0xFEFF: func = SQ_io_file_lexfeed_UCS2_LE; break;//UTF-16 big endian; + case 0xBBEF: + if (SQfread(&uc, 1, sizeof(uc), file) == 0) + { + SQ__Fclose(file); + return SQ_Throwerror(v, _SC("io error")); + } + if (uc != 0xBF) + { + SQ__Fclose(file); + return SQ_Throwerror(v, _SC("Unrecognozed ecoding")); + } + func = SQ_io_file_lexfeed_UTF8; + break;//UTF-8 ; + default: SQfseek(file, 0, 2); break; // ascii + } + + if (SQ_Compile(v, func, file, filename, printerror) >= 0) + { + SQ__Fclose(file); + return SQ_OK; + } + } + SQ__Fclose(file); + return -1; + } + return SQ_Throwerror(v, _SC("cannot open the file")); +} + +static int SQdofile(uint32_t v, const wchar_t* filename, bool retval, bool printerror) +{ + if (SQloadfile(v, filename, printerror) >= 0) + { + SQPush(v, -2); + if ((int)SQ_Call(v, 1, retval, 1) >= 0) + { + SQ_Remove(v, -(retval != 0) - 1); + return 1; + } + SQPop(v, 1); + } + return -1; +} + + + + + + + + + + + + + + + + + //-------------------------------------------------------------------------------------------Squirrel @@ -256,209 +346,210 @@ void RegisterNutApi(const wchar_t* funcName, void* funcAddr, uint32_t v) SQNewSlot(v, -3, false); SQPopTop(v); } - //Test -static SQInteger sq_Test(uint32_t v) +static int sq_Test(uint32_t v) { - //sq_pushinteger(v, n1); + SQdofile(v, L"1.cpp", false, false); return 0; } // װ -static SQInteger GetCharacterAttribute(HSQUIRRELVM v) +static int GetCharacterAttribute(uint32_t v) { - SQInteger n1 ,n2 ; - sq_getinteger(v, 2, &n1); - sq_getinteger(v, 3, &n2); + int n1 ,n2 ; + int num = SQGetTop(v); + int CharAddr = *(int*)(0x1AB7CDC); - if (n1 > 0 && n2 > 0 && n2 <= 27) + if (num == 3) { + SQGetInt(v, 2, &n1); + SQGetInt(v, 3, &n2); int TValue = *(int*)(CharAddr + GetEquAddr(n2)); int SValue = (TValue + n1); if (n1 != 0x8 && n1 != 0x1C && n1 != 0xF4) - sq_pushinteger(v, (SQInteger)(DNFDeCode(SValue))); + SQPushInt(v, (DNFDeCode(SValue))); else - sq_pushinteger(v, (SQInteger)(*(int*)(SValue))); + SQPushInt(v, (*(int*)(SValue))); } - else if (n1 > 0) + else if (num == 2) { + SQGetInt(v, 2, &n1); int Value = (CharAddr + n1); - sq_pushinteger(v, (SQInteger)(DNFDeCode(Value))); + SQPushInt(v, (DNFDeCode(Value))); } else { - sq_pushinteger(v, -1); + SQPushString(v, L"parameter error",-1); } return 1; } //д װ -static SQInteger SetCharacterAttribute(HSQUIRRELVM v) +static int SetCharacterAttribute(uint32_t v) { - SQInteger n1, n2,n3; - sq_getinteger(v, 2, &n1); - sq_getinteger(v, 3, &n2); - sq_getinteger(v, 4, &n3); + int n1, n2, n3; + + int num = SQGetTop(v); - std::cout << n1 << std::endl; - std::cout << n2 << std::endl; - std::cout << n3 << std::endl; - int CharAddr = *(int*)(0x1AB7CDC); - if (n1 > 0 && n2 > 0 && n3 > 0) + if (num == 4) { + SQGetInt(v, 2, &n1); + SQGetInt(v, 3, &n2); + SQGetInt(v, 4, &n3); + int TValue = *(int*)(CharAddr + GetEquAddr(n2)); int SValue = (TValue + n1); if (n1 != 0x8 && n1 != 0x1C && n1 != 0xF4) - //sq_pushinteger(v, (SQInteger)(DNFDeCode(SValue))); DNFEnCode(SValue, n3); else - //sq_pushinteger(v, (SQInteger)(*(int*)(SValue))); *(int*)SValue = n3; - sq_pushinteger(v, (SQInteger)1); + SQPushBool(v, true); } - else if (n1 > 0 && n2 >0) + else if (num == 3) { + SQGetInt(v, 2, &n1); + SQGetInt(v, 3, &n2); + int Value = (CharAddr + n1); DNFEnCode(Value, n2); - sq_pushinteger(v, (SQInteger)1); + SQPushBool(v, true); } else { - sq_pushinteger(v, -1); + SQPushBool(v, false); } - - sq_pushinteger(v, -1); + return 1; } //ȡ -static SQInteger GetTownIndex(HSQUIRRELVM v) +static int GetTownIndex(uint32_t v) { - sq_pushinteger(v, GetHook(0x1A5E258, "0xAC+0xD4+")); + SQPushInt(v, GetHook(0x1A5E258, "0xAC+0xD4+")); return 1; } //ȡ -static SQInteger GetRegionIndex(HSQUIRRELVM v) +static int GetRegionIndex(uint32_t v) { - sq_pushinteger(v, *(int*)(GetHook(0x1A5E258, "0xAC+0xD8+"))); + SQPushInt(v, *(int*)(GetHook(0x1A5E258, "0xAC+0xD8+"))); return 1; } //ȡX -static SQInteger GetTownXpos(HSQUIRRELVM v) +static int GetTownXpos(uint32_t v) { - sq_pushinteger(v, GetHook(0x1AB7CE0, "0x2BC+")); + SQPushInt(v, GetHook(0x1AB7CE0, "0x2BC+")); return 1; } //ȡY -static SQInteger GetTownYpos(HSQUIRRELVM v) +static int GetTownYpos(uint32_t v) { - sq_pushinteger(v, GetHook(0x1AB7CE0, "0x2C0+")); + SQPushInt(v, GetHook(0x1AB7CE0, "0x2C0+")); return 1; } // -static SQInteger SendPackType(HSQUIRRELVM v) +static int SendPackType(uint32_t v) { - SQInteger n1; - sq_getinteger(v, 2, &n1); + int n1; + SQGetInt(v, 2, &n1); _SendpacksType(*_SendClass, 0, n1); - sq_pushinteger(v, 1); + SQPushInt(v, 1); return 1; } //Byte -static SQInteger SendPackByte(HSQUIRRELVM v) +static int SendPackByte(uint32_t v) { - SQInteger n1; - sq_getinteger(v, 2, &n1); + int n1; + SQGetInt(v, 2, &n1); _SendPacksByte(*_SendClass, 0, n1); - sq_pushinteger(v, 1); + SQPushInt(v, 1); return 1; } //Word -static SQInteger SendPackWord(HSQUIRRELVM v) +static int SendPackWord(uint32_t v) { - SQInteger n1; - sq_getinteger(v, 2, &n1); + int n1; + SQGetInt(v, 2, &n1); _SendPacksWord(*_SendClass, 0, n1); - sq_pushinteger(v, 1); + SQPushInt(v, 1); return 1; } //DWord -static SQInteger SendPackDWord(HSQUIRRELVM v) +static int SendPackDWord(uint32_t v) { - SQInteger n1; - sq_getinteger(v, 2, &n1); + int n1; + SQGetInt(v, 2, &n1); _SendPacksDWord(*_SendClass, 0, n1); - sq_pushinteger(v, 1); + SQPushInt(v, 1); return 1; } // -static SQInteger SendPack(HSQUIRRELVM v) +static int SendPack(uint32_t v) { _SendPacks(); - sq_pushinteger(v, 1); + SQPushInt(v, 1); return 1; } //Ʒ -static SQInteger GivePlayerItem(HSQUIRRELVM v) +static int GivePlayerItem(uint32_t v) { - SQInteger n1,n2; + int n1,n2; + int num = SQGetTop(v); - sq_getinteger(v, 2, &n1); - sq_getinteger(v, 3, &n2); - - if (n1>0 && n2>0) + if (num == 3) { + SQGetInt(v, 2, &n1); + SQGetInt(v, 3, &n2); + _SendpacksType(*_SendClass, 0, 65); _SendPacksDWord(*_SendClass, 0, 1); _SendPacksDWord(*_SendClass, 0, n1); _SendPacksDWord(*_SendClass, 0, n2); _SendPacks(); - sq_pushinteger(v, 1); + SQPushBool(v, true); } - - if (!n1 || !n2) + else { - sq_pushinteger(v, 0); + SQPushBool(v, false); } return 1; } //װ -static SQInteger GivePlayerEqu(HSQUIRRELVM v) +static int GivePlayerEqu(uint32_t v) { - SQInteger n1, n2; + int n1, n2; + int num = SQGetTop(v); - sq_getinteger(v, 2, &n1); - sq_getinteger(v, 3, &n2); - - if (n1 > 0 && n2 > 0) + if (num == 3) { + SQGetInt(v, 2, &n1); + SQGetInt(v, 3, &n2); + _SendpacksType(*_SendClass, 0, 65); _SendPacksDWord(*_SendClass, 0, 2); _SendPacksDWord(*_SendClass, 0, n1); _SendPacksDWord(*_SendClass, 0, n2); _SendPacks(); - sq_pushinteger(v, 1); + SQPushBool(v, true); } - - if (!n1 || !n2) + else { - sq_pushinteger(v, 0); + SQPushBool(v, false); } return 1; @@ -472,8 +563,7 @@ static int GoDungeon(uint32_t v) int n3 = 0; int n4 = 0; - int num; - num = SQGetTop(v); + int num = SQGetTop(v); if (num == 2) { @@ -506,6 +596,28 @@ static int GoDungeon(uint32_t v) return 1; } +//Ldofile +static int LDofile(uint32_t v) +{ + wchar_t* n1; + + int num = SQGetTop(v); + + if (num == 2) + { + SQGetString(v, 2, &n1); + SQPopTop(v); + SQdofile(v, n1, false, false); + SQPushBool(v, true); + } + else + { + SQPushBool(v, false); + } + + return 1; +} + void RegisterNut() { RegisterNutApi(L"L_sq_Test", sq_Test); @@ -523,6 +635,7 @@ void RegisterNut() RegisterNutApi(L"L_sq_GivePlayerItem", GivePlayerItem); RegisterNutApi(L"L_sq_GivePlayerEqu", GivePlayerEqu); RegisterNutApi(L"L_sq_GoDungeon", GoDungeon); + RegisterNutApi(L"L_sq_Dofile", LDofile); }