本帖最后由 muscipular 于 2021-8-11 18:50 编辑
单机发现cgmsv占cpu挺多的。写了个脚本降低cpu使用率, 基本约等于0, 只支持cgmsv_21.2a windows版
- local ffi = require "ffi";
- local hook = { hooks = {} }
- ffi.cdef [[
- void Sleep(int ms);
- int VirtualProtect(void* lpAddress, unsigned long dwSize, unsigned long flNewProtect, unsigned long* lpflOldProtect);
- ]]
- function hook.inlineHook(cast, callback, hookAddr, size, prefixCode, postCode)
- local callbackAddr = type(callback) == 'function' and tonumber(ffi.cast('intptr_t', ffi.cast('void*', ffi.cast(cast, callback)))) or 0;
- if type(callback) ~= 'function' and callback then
- callbackAddr = callback;
- end
- local hookFnPtr = ffi.cast('void*', hookAddr)
- local oldProtectFlag = ffi.new('unsigned long[1]')
- local tmpProtectFlag = ffi.new('unsigned long[1]')
- local detourBytes = ffi.new('uint8_t[?]', 2048)
- local backup = ffi.new('uint8_t[?]', size)
- -- make backup
- ffi.copy(backup, hookFnPtr, size);
- -- prefixCode
- for i, v in ipairs(prefixCode) do
- detourBytes[i - 1] = v;
- end
- --call callback
- if callback then
- detourBytes[#prefixCode] = 0xE8;
- ffi.cast('uint32_t*', detourBytes + #prefixCode + 1)[0] = callbackAddr - (ffi.cast('uint32_t', detourBytes) + #prefixCode + 5);
- else
- detourBytes[#prefixCode] = 0x90;
- detourBytes[#prefixCode + 1] = 0x90;
- detourBytes[#prefixCode + 2] = 0x90;
- detourBytes[#prefixCode + 3] = 0x90;
- detourBytes[#prefixCode + 4] = 0x90;
- end
- -- prefixCode
- for i, v in ipairs(postCode) do
- detourBytes[i - 1 + 5 + #prefixCode] = v;
- end
- --origin code
- ffi.copy(detourBytes + #prefixCode + 5 + #postCode, hookFnPtr, size);
- --jmp to origin code
- detourBytes[#prefixCode + 5 + size + #postCode] = 0xE9;
- ffi.cast('int32_t*', detourBytes + #prefixCode + 5 + size + #postCode + 1)[0] = ffi.cast('int32_t', (hookAddr + size) - (ffi.cast('int32_t', detourBytes) + size + #postCode + #prefixCode + 10));
- --mark memory executable
- ffi.C.VirtualProtect(detourBytes, 2048, 0x40, tmpProtectFlag);
- --mark memory writable
- ffi.C.VirtualProtect(hookFnPtr, size, 0x40, oldProtectFlag)
- --jmp to hook code
- ffi.cast('uint8_t*', hookAddr)[0] = 0xE9;
- ffi.cast('uint32_t*', hookAddr + 1)[0] = ffi.cast('uint32_t', detourBytes) - (hookAddr + 5);
- for i = 5, size - 1 do
- ffi.cast('uint8_t*', hookAddr + i)[0] = 0x90;
- end
- --restore memory protect
- ffi.C.VirtualProtect(hookFnPtr, size, oldProtectFlag[0], tmpProtectFlag)
- local new_hook = {}
- new_hook.uninstall = function()
- ffi.C.VirtualProtect(hookFnPtr, size, 0x40, oldProtectFlag)
- ffi.copy(hookFnPtr, backup, size)
- ffi.C.VirtualProtect(hookFnPtr, size, oldProtectFlag[0], tmpProtectFlag)
- hook.hooks[tostring(hookAddr)] = nil;
- end
- new_hook.detourBytes = detourBytes;
- new_hook.backup = backup;
- new_hook.callback = callback;
- hook.hooks[tostring(hookAddr)] = new_hook;
- return new_hook;
- end
- ffi.hook = hook;
- local val = 100; --延迟时间,单位毫秒,最大127,不能少于0
- ffi.hook.inlineHook('nil', ffi.cast('uint32_t', ffi.cast('void*', ffi.C.Sleep)), 0x004013DD, 5, { 0x6A, val }, { })
- ffi.hook.inlineHook('nil', ffi.cast('uint32_t', ffi.cast('void*', ffi.C.Sleep)), 0x00420C5F, 7, { 0x6A, val }, { })
- ffi.hook.inlineHook('nil', ffi.cast('uint32_t', ffi.cast('void*', ffi.C.Sleep)), 0x00425337, 7, { 0x6A, val }, { })
- ffi.hook.inlineHook('nil', ffi.cast('uint32_t', ffi.cast('void*', ffi.C.Sleep)), 0x004EB3EA, 5, { 0x6A, val }, { })
复制代码
补充内容 (2021-8-11 18:56):
PS:不要重复加载,不然可能崩端
PS2:不建议用在多人服务器上,否则可能延迟比较大,或者手动修改延迟降低一下
补充内容 (2021-9-27 11:49):
已整合至 http://bbs.cgmsv.com/thread-1036-1-1.html |