1
0

y_writemem_old.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*----------------------------------------------------------------------------*\
  2. ===========================
  3. Y Sever Includes - Writemem
  4. ===========================
  5. Description:
  6. Write to any absolute address in the SA:MP server in pure PAWN with embedded
  7. assembly (i.e. a new native). Calls "VirtualProtect" to make writes safe.
  8. Legal:
  9. Version: MPL 1.1
  10. The contents of this file are subject to the Mozilla Public License Version
  11. 1.1 (the "License"); you may not use this file except in compliance with
  12. the License. You may obtain a copy of the License at
  13. http://www.mozilla.org/MPL/
  14. Software distributed under the License is distributed on an "AS IS" basis,
  15. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. for the specific language governing rights and limitations under the
  17. License.
  18. The Original Code is the YSI ini include.
  19. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  20. Portions created by the Initial Developer are Copyright (C) 2011
  21. the Initial Developer. All Rights Reserved.
  22. Contributors:
  23. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  24. Thanks:
  25. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  26. ZeeX - Very productive conversations.
  27. koolk - IsPlayerinAreaEx code.
  28. TheAlpha - Danish translation.
  29. breadfish - German translation.
  30. Fireburn - Dutch translation.
  31. yom - French translation.
  32. 50p - Polish translation.
  33. Zamaroht - Spanish translation.
  34. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  35. for me to strive to better.
  36. Pixels^ - Running XScripters where the idea was born.
  37. Matite - Pestering me to release it and using it.
  38. Very special thanks to:
  39. Thiadmer - PAWN, whose limits continue to amaze me!
  40. Kye/Kalcor - SA:MP.
  41. SA:MP Team past, present and future - SA:MP.
  42. Version:
  43. 1.0
  44. Changelog:
  45. 01/02/12:
  46. Added "VirtualProtect" calls to the code.
  47. First version.
  48. Functions:
  49. Public:
  50. -
  51. Core:
  52. -
  53. Stock:
  54. -
  55. Static:
  56. -
  57. Inline:
  58. -
  59. API:
  60. WriteMem - Write data to an address.
  61. Callbacks:
  62. -
  63. Definitions:
  64. asm - Convert a stream of assembly to a cell.
  65. Enums:
  66. -
  67. Macros:
  68. -
  69. Tags:
  70. -
  71. Variables:
  72. Global:
  73. -
  74. Static:
  75. YSI_g_sWriteMem - The assembly.
  76. Commands:
  77. -
  78. Compile options:
  79. -
  80. Operators:
  81. -
  82. \*----------------------------------------------------------------------------*/
  83. #include <a_samp>
  84. #include "y_amx"
  85. #include "y_hooks"
  86. #include "y_utils"
  87. native WriteMem(addr, value) = SetDeathDropAmount;
  88. #define asm(%0,%1,%2,%3) ((0x%0<<0)|(0x%1<<8)|(0x%2<<16)|(0x%3<<24))
  89. /*
  90. cell AMX_NATIVE_CALL
  91. n_WriteMem(AMX * amx, cell * params)
  92. {
  93. if (params[0] == 8)
  94. {
  95. DWORD
  96. oldp;
  97. VirtualProtect((cell *)params[1], 4, PAGE_EXECUTE_READWRITE, &oldp);
  98. *((cell *)params[1]) = params[2];
  99. }
  100. return 0;
  101. }
  102. BECOMES:
  103. align 16
  104. push ebp
  105. mov ebp, esp
  106. push esi
  107. mov esi, [ebp+12]
  108. cmp dword ptr [esi], 8
  109. jnz short loc_ret
  110. mov ecx, [esi+4]
  111. lea eax, [ebp+12]
  112. push eax
  113. push 40h
  114. push 4
  115. push ecx
  116. call ds:__imp__VirtualProtect@16
  117. mov edx, [esi+4]
  118. mov eax, [esi+8]
  119. mov [edx], eax
  120. loc_ret:
  121. xor eax, eax
  122. pop esi
  123. pop ebp
  124. retn
  125. BECOMES:
  126. */
  127. static
  128. YSI_g_sWriteMem[] =
  129. {
  130. //asm(CC,CC,CC,CC), asm(CC,CC,CC,CC), asm(CC,CC,CC,CC), asm(CC,CC,CC,CC),
  131. //asm(55,8B,EC,56), asm(8B,75,0C,83), asm(3E,08,75,1A), asm(8B,4E,04,8D),
  132. //asm(45,0C,50,6A), asm(40,6A,04,51), asm(FF,15,3C,70), asm(00,10,8B,56),
  133. //asm(04,8B,46,08), asm(89,02,33,C0), asm(5E,5D,C3,CC)
  134. asm(CC,CC,CC,CC), asm(CC,CC,CC,CC), asm(CC,CC,CC,CC), asm(CC,CC,CC,CC),
  135. asm(55,8B,EC,56), asm(8B,75,0C,83), asm(3E,08,75,1A), asm(8B,4E,04,8D),
  136. asm(45,0C,50,6A), asm(40,6A,04,51), asm(FF,15,8C,11), asm(4A,00,8B,56),
  137. asm(04,8B,46,08), asm(89,02,33,C0), asm(5E,5D,C3,CC)
  138. };
  139. forward _@_y_writemem_inc_@_();
  140. public _@_y_writemem_inc_@_()
  141. {
  142. if (FALSE)
  143. {
  144. WriteMem(0, 0);
  145. }
  146. }
  147. static
  148. WM_Shift(from, to, data[], len = sizeof (data))
  149. {
  150. if (FALSE)
  151. {
  152. WriteMem(0, 0);
  153. }
  154. while (from < len)
  155. {
  156. data[to++] = data[from++];
  157. }
  158. }
  159. hook OnScriptInit()
  160. {
  161. if (FALSE)
  162. {
  163. WriteMem(0, 0);
  164. }
  165. // Have, but don't call "WriteMem" a few times - makes the code happier.
  166. _@_y_writemem_inc_@_();
  167. new
  168. addr = AMX_GetGlobalAddress(YSI_g_sWriteMem),
  169. nat;
  170. // Align the code to a 16-byte boundary.
  171. switch (addr & 15)
  172. {
  173. case 0:
  174. {
  175. WM_Shift(4, 0, YSI_g_sWriteMem);
  176. }
  177. case 4:
  178. {
  179. WM_Shift(4, 3, YSI_g_sWriteMem);
  180. addr += 12;
  181. }
  182. case 8:
  183. {
  184. WM_Shift(4, 2, YSI_g_sWriteMem);
  185. addr += 8;
  186. }
  187. case 12:
  188. {
  189. WM_Shift(4, 1, YSI_g_sWriteMem);
  190. addr += 4;
  191. }
  192. default:
  193. {
  194. P:E("Cannot relocate YSI_g_sWriteMem");
  195. }
  196. }
  197. // If these specific functions aren't called, it doesn't seem to work...
  198. AMX_GetNativePointer(0, nat, "SetDeathDropAmount");
  199. AMX_GetNativeEntry(0, nat, "SetDeathDropAmount");
  200. #emit LOAD.S.pri addr
  201. #emit SREF.S.pri nat
  202. return 1;
  203. }