y_cgen.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #if defined _INC_y_cgen
  2. #endinput
  3. #endif
  4. #define _INC_y_cgen
  5. #include "..\YSI_Storage\y_amx"
  6. #include "..\YSI_Internal\amx_assembly"
  7. // We use "STE" not a string literal as we are trying to use up code space, not
  8. // data space!
  9. #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)
  10. #define _CODE_2 _CODE_1;_CODE_1;_CODE_1;_CODE_1;_CODE_1;_CODE_1
  11. static stock
  12. YSI_g_sCodeSpace = -1,
  13. YSI_g_sCodeEnd = -1;
  14. forward _@_y_cgen_@_0();
  15. public _@_y_cgen_@_0()
  16. {
  17. #emit RETN // Invisible return.
  18. //P:F("Someone called \"_@_y_cgen_@_0\" and somehow it didn't crash yet!");
  19. //assert(FALSE); // Always fails.
  20. // Reserve a huge area of "COD" for our own use!
  21. static
  22. s[] = "";
  23. // This reserves about 10kb of space in "COD". If 10kb isn't enough I don't
  24. // know WHAT you're doing - that's enough space for about 200 hooks!
  25. _CODE_2;
  26. _CODE_2;
  27. _CODE_2;
  28. _CODE_2;
  29. _CODE_2;
  30. _CODE_2;
  31. _CODE_2;
  32. _CODE_2;
  33. _CODE_2;
  34. _CODE_2;
  35. _CODE_2;
  36. _CODE_2;
  37. _CODE_2;
  38. _CODE_2;
  39. _CODE_2;
  40. _CODE_2;
  41. // These are blocks of 16 lines. If you ever need to increase this space,
  42. // just duplicate a block of 16 lines. Do not put multiple "_CODE_2;"
  43. // statements on one line - the pre-processor expands this so much that that
  44. // will probably just crash.
  45. _CODE_2;
  46. _CODE_2;
  47. _CODE_2;
  48. _CODE_2;
  49. _CODE_2;
  50. _CODE_2;
  51. _CODE_2;
  52. _CODE_2;
  53. _CODE_2;
  54. _CODE_2;
  55. _CODE_2;
  56. _CODE_2;
  57. _CODE_2;
  58. _CODE_2;
  59. _CODE_2;
  60. _CODE_2;
  61. }
  62. forward _@_y_cgen_@_1();
  63. public _@_y_cgen_@_1()
  64. {
  65. // This function SHOULD come straight after "_@_y_hooks_@_0" in both the
  66. // public functions table and code segment if the compiler is true to form.
  67. }
  68. static stock CGen_GetAddr(const func[])
  69. {
  70. new
  71. entry = AMX_HEADER_PUBLICS + funcidx(func) * 8,
  72. ret = AMX_HEADER_COD + AMX_Read(entry);
  73. // Reset the pointer to this function, so calling it fails.
  74. //AMX_Write(entry, 0);
  75. return ret;
  76. }
  77. static stock CGen_SetupCodeSpace()
  78. {
  79. if (YSI_g_sCodeSpace != -1) return;
  80. // Both these functions can now be safely called - but why would you?
  81. YSI_g_sCodeSpace = CGen_GetAddr("_@_y_cgen_@_0"),
  82. YSI_g_sCodeEnd = CGen_GetAddr("_@_y_cgen_@_1"),
  83. // Rewrite "_@_y_cgen_@_0" to just "return 0;".
  84. AMX_Write(YSI_g_sCodeSpace, _:RelocateOpcode(OP_PROC)),
  85. AMX_Write(YSI_g_sCodeSpace + 4, _:RelocateOpcode(OP_ZERO_PRI)),
  86. AMX_Write(YSI_g_sCodeSpace + 8, _:RelocateOpcode(OP_RETN)),
  87. YSI_g_sCodeSpace += 12;
  88. }
  89. stock CGen_UseCodeSpace(ctx[AsmContext])
  90. {
  91. CGen_SetupCodeSpace();
  92. AsmInitPtr(ctx, YSI_g_sCodeSpace, YSI_g_sCodeEnd - YSI_g_sCodeSpace);
  93. }
  94. stock CGen_GetCodeSpace()
  95. {
  96. CGen_SetupCodeSpace();
  97. return YSI_g_sCodeSpace;
  98. }
  99. stock CGen_AddCodeSpace(num)
  100. {
  101. if (YSI_g_sCodeSpace == -1) P:E("YSI_g_sCodeSpace is -1 in \"CGen_AddCodeSpace\"");
  102. else YSI_g_sCodeSpace += num;
  103. }
  104. #undef _CODE_2
  105. #undef _CODE_1