Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. #include "skse64/PluginAPI.h"
  2. #include "skse64_common/skse_version.h"
  3. #include "skse64_common/SafeWrite.h"
  4. #include "skse64_common/BranchTrampoline.h"
  5. #include "xbyak/xbyak.h"
  6. #include "skse64_common/Relocation.h"
  7.  
  8. #include <xmmintrin.h>
  9. #include <shlobj.h>
  10. #include <string>
  11. #include <chrono>
  12.  
  13. RelocAddr <uintptr_t> RescaleInjectionAddr(0x0074319c);
  14. RelocAddr <uintptr_t> RescaleInjectionAddr2(0x00624dea);
  15. uintptr_t RescaleReturnAddr = RescaleInjectionAddr + 0x09;
  16. uintptr_t RescaleReturnAddr2 = RescaleInjectionAddr2 + 0x09;
  17. float foo = 100.0f;
  18. //RelocAddr <float> oneHundredConstant(0x001539f18);
  19.  
  20. IDebugLog gLog;
  21.  
  22. PluginHandle g_pluginHandle = kPluginHandle_Invalid;
  23. const UInt32 kSerializationDataVersion = 1;
  24.  
  25. float __vectorcall RescaleArmor(float hiddenResist, float vanillaResist) {
  26. //_DMESSAGE("%f,%f", hiddenResist, vanillaResist);
  27. if (vanillaResist < 0.0f) {
  28. return vanillaResist;
  29. }
  30. return 1.0f / (1.0f + vanillaResist);
  31. }
  32.  
  33. void ArmorScalingFix() {
  34. _DMESSAGE("ArmorScalingFix");
  35. //xmm0 - hiddenResist
  36. //xmm1 - vanillaResist
  37. //xmm2 - 1.0f float constant
  38. struct RescaleInjection1 : Xbyak::CodeGenerator
  39. {
  40. RescaleInjection1(void * buf) : Xbyak::CodeGenerator(4096, buf)
  41. {
  42. Xbyak::Label rescaleReturn;
  43. Xbyak::Label dataLabel;
  44. push(rbp);
  45. mov(rbp, rsp);
  46. movss(ptr[rbp - 4],xmm0);
  47. //if vanillaResist < 0
  48. pxor(xmm0, xmm0);
  49. ucomiss(xmm0, xmm1);
  50. //then return
  51. jae(rescaleReturn);
  52. //tmp = vanillaresist
  53. movss(xmm0, xmm1);
  54. // vanillaresist = 1+vanillaresist
  55. addss(xmm1, ptr[rip+dataLabel]);
  56. //tmp = tmp/vanillaresist
  57. divss(xmm0, xmm1);
  58. //vanillaresist = tmp
  59. movss(xmm1, xmm0);
  60. L(rescaleReturn);
  61. movss(xmm0, ptr[rbp - 4]);
  62. pop(rbp);
  63. //replaced instruction
  64. movss(ptr[rbp + 0x77], xmm1);
  65. jmp(ptr[rip]);
  66. dq(RescaleReturnAddr);
  67. L(dataLabel);
  68. dd(0x3f800000); //1.0f
  69. }
  70. };
  71. _DMESSAGE("Generating code");
  72. void * codeBuf = g_localTrampoline.StartAlloc();
  73. _DMESSAGE("foo");
  74. RescaleInjection1 code(codeBuf);
  75. _DMESSAGE("bar");
  76. g_localTrampoline.EndAlloc(code.getCurr());
  77. _DMESSAGE("Write5Branch 1");
  78. g_branchTrampoline.Write5Branch(RescaleInjectionAddr.GetUIntPtr(), uintptr_t(code.getCode()));
  79. _DMESSAGE("Generating code 2");
  80.  
  81. //xmm0 - hiddenResist
  82. //xmm8 - vanillaResist
  83. struct RescaleInjection2 : Xbyak::CodeGenerator
  84. {
  85. RescaleInjection2(void * buf) : Xbyak::CodeGenerator(4096, buf)
  86. {
  87. Xbyak::Label rescaleReturn2;
  88. Xbyak::Label dataLabel2;
  89. push(rbp);
  90. mov(rbp, rsp);
  91. movss(ptr[rbp - 4], xmm0);
  92. //if vanillaResist < 0
  93. pxor(xmm0, xmm0);
  94. ucomiss(xmm0, xmm8);
  95. //then return
  96. jae(rescaleReturn2);
  97. //tmp = vanillaresist
  98. movss(xmm0, xmm8);
  99. // vanillaresist = 1+vanillaresist
  100. addss(xmm8, ptr[rip + dataLabel2]);
  101. //tmp = tmp/vanillaresist
  102. divss(xmm0, xmm8);
  103. //vanillaresist = tmp
  104. movss(xmm8, xmm0);
  105. L(rescaleReturn2);
  106. movss(xmm0, ptr[rbp - 4]);
  107. pop(rbp);
  108. //replaced instruction
  109. comiss(xmm8, xmm1);
  110. jmp(ptr[rip]);
  111. dq(RescaleReturnAddr2);
  112. L(dataLabel2);
  113. dd(0x3f800000); //1.0f
  114. }
  115. };
  116. void * codeBuf2 = g_localTrampoline.StartAlloc();
  117. RescaleInjection2 code2(codeBuf2);
  118. g_localTrampoline.EndAlloc(code2.getCurr());
  119. _DMESSAGE("Write5Branch 2");
  120. g_branchTrampoline.Write5Branch(RescaleInjectionAddr2.GetUIntPtr(), uintptr_t(code2.getCode()));
  121.  
  122. }
  123.  
  124. extern "C"
  125. {
  126.  
  127. bool SKSEPlugin_Query(const SKSEInterface * skse, PluginInfo * info)
  128. {
  129. SInt32 logLevel = IDebugLog::kLevel_DebugMessage;
  130.  
  131. if (logLevel >= 0)
  132. gLog.OpenRelative(CSIDL_MYDOCUMENTS, "\\My Games\\Skyrim Special Edition\\SKSE\\TestPlugin.log");
  133.  
  134. _DMESSAGE("TestPlugin");
  135.  
  136. // populate info structure
  137. info->infoVersion = PluginInfo::kInfoVersion;
  138. info->name = "TestPlugin";
  139. info->version = 1;
  140.  
  141. // store plugin handle so we can identify ourselves later
  142. g_pluginHandle = skse->GetPluginHandle();
  143.  
  144. if(skse->isEditor)
  145. {
  146. _MESSAGE("loaded in editor, marking as incompatible");
  147. return false;
  148. }
  149. else if (skse->runtimeVersion != RUNTIME_VERSION_1_5_23)
  150. {
  151. UInt32 runtimeVersion = RUNTIME_VERSION_1_5_23;
  152. _FATALERROR("unsupported runtime version %08X", skse->runtimeVersion);
  153. return false;
  154. }
  155.  
  156. // supported runtime version
  157. return true;
  158. }
  159.  
  160. bool SKSEPlugin_Load(const SKSEInterface * skse)
  161. {
  162. if (!g_branchTrampoline.Create(1024 * 64))
  163. {
  164. _ERROR("couldn't create branch trampoline. this is fatal. skipping remainder of init process.");
  165. return false;
  166. }
  167.  
  168. if (!g_localTrampoline.Create(1024 * 64, nullptr))
  169. {
  170. _ERROR("couldn't create codegen buffer. this is fatal. skipping remainder of init process.");
  171. return false;
  172. }
  173. ArmorScalingFix();
  174. return true;
  175. }
  176.  
  177. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement