Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "skse64/PluginAPI.h"
- #include "skse64_common/skse_version.h"
- #include "skse64_common/SafeWrite.h"
- #include "skse64_common/BranchTrampoline.h"
- #include "xbyak/xbyak.h"
- #include "skse64_common/Relocation.h"
- #include <xmmintrin.h>
- #include <shlobj.h>
- #include <string>
- #include <chrono>
- RelocAddr <uintptr_t> RescaleInjectionAddr(0x0074319c);
- RelocAddr <uintptr_t> RescaleInjectionAddr2(0x00624dea);
- uintptr_t RescaleReturnAddr = RescaleInjectionAddr + 0x09;
- uintptr_t RescaleReturnAddr2 = RescaleInjectionAddr2 + 0x09;
- float foo = 100.0f;
- //RelocAddr <float> oneHundredConstant(0x001539f18);
- IDebugLog gLog;
- PluginHandle g_pluginHandle = kPluginHandle_Invalid;
- const UInt32 kSerializationDataVersion = 1;
- float __vectorcall RescaleArmor(float hiddenResist, float vanillaResist) {
- //_DMESSAGE("%f,%f", hiddenResist, vanillaResist);
- if (vanillaResist < 0.0f) {
- return vanillaResist;
- }
- return 1.0f / (1.0f + vanillaResist);
- }
- void ArmorScalingFix() {
- _DMESSAGE("ArmorScalingFix");
- //xmm0 - hiddenResist
- //xmm1 - vanillaResist
- //xmm2 - 1.0f float constant
- struct RescaleInjection1 : Xbyak::CodeGenerator
- {
- RescaleInjection1(void * buf) : Xbyak::CodeGenerator(4096, buf)
- {
- Xbyak::Label rescaleReturn;
- Xbyak::Label dataLabel;
- push(rbp);
- mov(rbp, rsp);
- movss(ptr[rbp - 4],xmm0);
- //if vanillaResist < 0
- pxor(xmm0, xmm0);
- ucomiss(xmm0, xmm1);
- //then return
- jae(rescaleReturn);
- //tmp = vanillaresist
- movss(xmm0, xmm1);
- // vanillaresist = 1+vanillaresist
- addss(xmm1, ptr[rip+dataLabel]);
- //tmp = tmp/vanillaresist
- divss(xmm0, xmm1);
- //vanillaresist = tmp
- movss(xmm1, xmm0);
- L(rescaleReturn);
- movss(xmm0, ptr[rbp - 4]);
- pop(rbp);
- //replaced instruction
- movss(ptr[rbp + 0x77], xmm1);
- jmp(ptr[rip]);
- dq(RescaleReturnAddr);
- L(dataLabel);
- dd(0x3f800000); //1.0f
- }
- };
- _DMESSAGE("Generating code");
- void * codeBuf = g_localTrampoline.StartAlloc();
- _DMESSAGE("foo");
- RescaleInjection1 code(codeBuf);
- _DMESSAGE("bar");
- g_localTrampoline.EndAlloc(code.getCurr());
- _DMESSAGE("Write5Branch 1");
- g_branchTrampoline.Write5Branch(RescaleInjectionAddr.GetUIntPtr(), uintptr_t(code.getCode()));
- _DMESSAGE("Generating code 2");
- //xmm0 - hiddenResist
- //xmm8 - vanillaResist
- struct RescaleInjection2 : Xbyak::CodeGenerator
- {
- RescaleInjection2(void * buf) : Xbyak::CodeGenerator(4096, buf)
- {
- Xbyak::Label rescaleReturn2;
- Xbyak::Label dataLabel2;
- push(rbp);
- mov(rbp, rsp);
- movss(ptr[rbp - 4], xmm0);
- //if vanillaResist < 0
- pxor(xmm0, xmm0);
- ucomiss(xmm0, xmm8);
- //then return
- jae(rescaleReturn2);
- //tmp = vanillaresist
- movss(xmm0, xmm8);
- // vanillaresist = 1+vanillaresist
- addss(xmm8, ptr[rip + dataLabel2]);
- //tmp = tmp/vanillaresist
- divss(xmm0, xmm8);
- //vanillaresist = tmp
- movss(xmm8, xmm0);
- L(rescaleReturn2);
- movss(xmm0, ptr[rbp - 4]);
- pop(rbp);
- //replaced instruction
- comiss(xmm8, xmm1);
- jmp(ptr[rip]);
- dq(RescaleReturnAddr2);
- L(dataLabel2);
- dd(0x3f800000); //1.0f
- }
- };
- void * codeBuf2 = g_localTrampoline.StartAlloc();
- RescaleInjection2 code2(codeBuf2);
- g_localTrampoline.EndAlloc(code2.getCurr());
- _DMESSAGE("Write5Branch 2");
- g_branchTrampoline.Write5Branch(RescaleInjectionAddr2.GetUIntPtr(), uintptr_t(code2.getCode()));
- }
- extern "C"
- {
- bool SKSEPlugin_Query(const SKSEInterface * skse, PluginInfo * info)
- {
- SInt32 logLevel = IDebugLog::kLevel_DebugMessage;
- if (logLevel >= 0)
- gLog.OpenRelative(CSIDL_MYDOCUMENTS, "\\My Games\\Skyrim Special Edition\\SKSE\\TestPlugin.log");
- _DMESSAGE("TestPlugin");
- // populate info structure
- info->infoVersion = PluginInfo::kInfoVersion;
- info->name = "TestPlugin";
- info->version = 1;
- // store plugin handle so we can identify ourselves later
- g_pluginHandle = skse->GetPluginHandle();
- if(skse->isEditor)
- {
- _MESSAGE("loaded in editor, marking as incompatible");
- return false;
- }
- else if (skse->runtimeVersion != RUNTIME_VERSION_1_5_23)
- {
- UInt32 runtimeVersion = RUNTIME_VERSION_1_5_23;
- _FATALERROR("unsupported runtime version %08X", skse->runtimeVersion);
- return false;
- }
- // supported runtime version
- return true;
- }
- bool SKSEPlugin_Load(const SKSEInterface * skse)
- {
- if (!g_branchTrampoline.Create(1024 * 64))
- {
- _ERROR("couldn't create branch trampoline. this is fatal. skipping remainder of init process.");
- return false;
- }
- if (!g_localTrampoline.Create(1024 * 64, nullptr))
- {
- _ERROR("couldn't create codegen buffer. this is fatal. skipping remainder of init process.");
- return false;
- }
- ArmorScalingFix();
- return true;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement