1
0

y_cgen.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #if defined _INC_y_cgen
  2. #endinput
  3. #endif
  4. #define _INC_y_cgen
  5. /*+
  6. * <library name="y_cgen">
  7. * <section>
  8. * Description
  9. * </section>
  10. * Large chunks of code that isn't used, so that the space can be reclaimed and
  11. * reused by other code generating functions.
  12. * <section>
  13. * Version
  14. * </section>
  15. * 1.0
  16. * </library>
  17. *//** *//*
  18. Legal:
  19. Version: MPL 1.1
  20. The contents of this file are subject to the Mozilla Public License Version
  21. 1.1 the "License"; you may not use this file except in compliance with
  22. the License. You may obtain a copy of the License at
  23. http://www.mozilla.org/MPL/
  24. Software distributed under the License is distributed on an "AS IS" basis,
  25. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  26. for the specific language governing rights and limitations under the
  27. License.
  28. The Original Code is the YSI framework.
  29. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  30. Portions created by the Initial Developer are Copyright C 2011
  31. the Initial Developer. All Rights Reserved.
  32. Contributors:
  33. Y_Less
  34. koolk
  35. JoeBullet/Google63
  36. g_aSlice/Slice
  37. Misiur
  38. samphunter
  39. tianmeta
  40. maddinat0r
  41. spacemud
  42. Crayder
  43. Dayvison
  44. Ahmad45123
  45. Zeex
  46. irinel1996
  47. Yiin-
  48. Chaprnks
  49. Konstantinos
  50. Masterchen09
  51. Southclaws
  52. PatchwerkQWER
  53. m0k1
  54. paulommu
  55. udan111
  56. Thanks:
  57. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  58. ZeeX - Very productive conversations.
  59. koolk - IsPlayerinAreaEx code.
  60. TheAlpha - Danish translation.
  61. breadfish - German translation.
  62. Fireburn - Dutch translation.
  63. yom - French translation.
  64. 50p - Polish translation.
  65. Zamaroht - Spanish translation.
  66. Los - Portuguese translation.
  67. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  68. me to strive to better.
  69. Pixels^ - Running XScripters where the idea was born.
  70. Matite - Pestering me to release it and using it.
  71. Very special thanks to:
  72. Thiadmer - PAWN, whose limits continue to amaze me!
  73. Kye/Kalcor - SA:MP.
  74. SA:MP Team past, present and future - SA:MP.
  75. Optional plugins:
  76. Gamer_Z - GPS.
  77. Incognito - Streamer.
  78. Me - sscanf2, fixes2, Whirlpool.
  79. */
  80. #include "..\YSI_Storage\y_amx"
  81. #include "..\YSI_Internal\y_thirdpartyinclude"
  82. // We use "STE" not a string literal as we are trying to use up code space, not
  83. // data space!
  84. #define _CODE_1 SetTimerEx(s,0,0,s,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
  85. #define _CODE_2 _CODE_1;_CODE_1;_CODE_1;_CODE_1;_CODE_1;_CODE_1
  86. static stock
  87. YSI_g_sCodeSpace = -1,
  88. YSI_g_sCodeEnd = -1;
  89. forward _@_y_cgen_@_0();
  90. public _@_y_cgen_@_0()
  91. {
  92. #emit RETN // Invisible return.
  93. //P:F("Someone called \"_@_y_cgen_@_0\" and somehow it didn't crash yet!");
  94. //assert(FALSE); // Always fails.
  95. // Reserve a huge area of "COD" for our own use!
  96. static
  97. s[] = "";
  98. // This reserves about 10kb of space in "COD". If 10kb isn't enough I don't
  99. // know WHAT you're doing - that's enough space for about 200 hooks!
  100. _CODE_2;
  101. _CODE_2;
  102. _CODE_2;
  103. _CODE_2;
  104. _CODE_2;
  105. _CODE_2;
  106. _CODE_2;
  107. _CODE_2;
  108. _CODE_2;
  109. _CODE_2;
  110. _CODE_2;
  111. _CODE_2;
  112. _CODE_2;
  113. _CODE_2;
  114. _CODE_2;
  115. _CODE_2;
  116. // These are blocks of 16 lines. If you ever need to increase this space,
  117. // just duplicate a block of 16 lines. Do not put multiple "_CODE_2;"
  118. // statements on one line - the pre-processor expands this so much that that
  119. // will probably just crash.
  120. _CODE_2;
  121. _CODE_2;
  122. _CODE_2;
  123. _CODE_2;
  124. _CODE_2;
  125. _CODE_2;
  126. _CODE_2;
  127. _CODE_2;
  128. _CODE_2;
  129. _CODE_2;
  130. _CODE_2;
  131. _CODE_2;
  132. _CODE_2;
  133. _CODE_2;
  134. _CODE_2;
  135. _CODE_2;
  136. }
  137. forward _@_y_cgen_@_1();
  138. public _@_y_cgen_@_1()
  139. {
  140. // This function SHOULD come straight after "_@_y_hooks_@_0" in both the
  141. // public functions table and code segment if the compiler is true to form.
  142. }
  143. static stock CGen_GetAddr(const func[])
  144. {
  145. new
  146. entry = AMX_HEADER_PUBLICS + funcidx(func) * 8,
  147. ret = AMX_HEADER_COD + AMX_Read(entry);
  148. // Reset the pointer to this function, so calling it fails.
  149. //AMX_Write(entry, 0);
  150. return ret;
  151. }
  152. static stock CGen_SetupCodeSpace()
  153. {
  154. if (YSI_g_sCodeSpace != -1) return;
  155. // Both these functions can now be safely called - but why would you?
  156. YSI_g_sCodeSpace = CGen_GetAddr("_@_y_cgen_@_0"),
  157. YSI_g_sCodeEnd = CGen_GetAddr("_@_y_cgen_@_1"),
  158. // Rewrite "_@_y_cgen_@_0" to just "return 0;".
  159. AMX_Write(YSI_g_sCodeSpace, _:RelocateOpcode(OP_PROC)),
  160. AMX_Write(YSI_g_sCodeSpace + 4, _:RelocateOpcode(OP_ZERO_PRI)),
  161. AMX_Write(YSI_g_sCodeSpace + 8, _:RelocateOpcode(OP_RETN)),
  162. YSI_g_sCodeSpace += 12;
  163. }
  164. stock CGen_UseCodeSpace(ctx[AsmContext])
  165. {
  166. CGen_SetupCodeSpace();
  167. AsmInitPtr(ctx, YSI_g_sCodeSpace, YSI_g_sCodeEnd - YSI_g_sCodeSpace);
  168. }
  169. stock CGen_GetCodeSpace()
  170. {
  171. CGen_SetupCodeSpace();
  172. return YSI_g_sCodeSpace;
  173. }
  174. stock CGen_AddCodeSpace(num)
  175. {
  176. if (YSI_g_sCodeSpace == -1) P:E("YSI_g_sCodeSpace is -1 in \"CGen_AddCodeSpace\"");
  177. else YSI_g_sCodeSpace += num;
  178. }
  179. #undef _CODE_2
  180. #undef _CODE_1