Aug 30 2006
by Xieon


-----------------------------------------------------------------
  Grand Theft Auto: San Andreas Mission Coding Patch
-----------------------------------------------------------------

   [1]. What is it?

   Here is continuation of the concept which CyQ made 
   for VC 1.0 (http://www.gtaforums.com/index.php?showtopic=217910),
   namely modifying a lot of game addresses directly from the main.scm,
   and this could be very useful thing for GTA:SA.
  
   As it was found out, in the SA it's impossible to apply the same 
   technique, as in the VC, because any access to the opcode jumptables,
   even if it was made by opcode, causes Access Violation error. 
   However, i found another way. This version of the patch adds to the game
   three new opcodes - for reading memory addresses, for writing and the
   opcode calling the VirtualProtect WinAPI function, for unlocking 
   read/write-protected addresses. They replace some nops and acts like the any
   other normal opcode. 



   [2]. How it works?

   It's just pure assembler code placed in one of the gaps in the gta_sa.exe and
   new pointers to it in the opcode jumptables.



   [3]. Install notes.
   
   First run patch and apply it. Backup and finding of a game folder it does 
   automatically, if this has not happened, choose folder yourself. 

   Then open SASCM.INI from the your mission editor and proceed to following lines:

   00c3=0,nop
   00c4=0,nop

   Change them to the following ones:

   00c3=3,read_mem_address %1d% type %2d% value_to %3d%
   00c4=3,write_mem_address %1d% type %2d% value %3d%

   After this proceed to the line:

   0181=0,nop

   And change to:

   0181=3,virtual_protect_change_at %1d% size %2d% new_protect %3d%

   Done, you have received three new working opcodes :) Example of their use:

   0181: virtual_protect_change_at 8796548 size 4 new_protect 4
   00C4: write_mem_address 8796548 type 2 value 0.002
   0181: virtual_protect_change_at 8796548 size 4 new_protect -1 

   8796548 (0x863984) - is the memory address where value of gravity stored,
   we reduced it for jumping like on the Moon :) Because simple changing it causes 
   game crash we remove the memory protection by 0181.  



   [4]. Opcodes description.

   00C3, 00C4:

   	Names speak for themselves, from you is required only to specify address
        (which can be taken from gtamodding.com or topic at the GTAForums or found by you),
        new value and how much to change (type):

   	0  Byte 		(1 byte)
   	1  Word 		(2 bytes)	
   	2  Double word 	(4 bytes)

	You can freely use both variables and numbers, except when you are reading address - 
	the place to store must be the variable.

   0181:

	This opcode is simply calls VirtualProtect function from kernel32.dll which is part
	of the Win32 API. 	

	You specify the address, size (in bytes) and new protection flags integer, which can be:

   	1 	 PAGE_NOACCESS
   	2 	 PAGE_READONLY
   	4 	 PAGE_READWRITE
   	8 	 PAGE_WRITECOPY
   	16 	 PAGE_EXECUTE
   	32 	 PAGE_EXECUTE_READ
   	64 	 PAGE_EXECUTE_READWRITE
   	128 	 PAGE_EXECUTE_WRITECOPY
   	256 	 PAGE_GUARD
   	512 	 PAGE_NOCACHE

	If you write -1 (like in example) opcode will use the last removed protection. 
	You also may use variable for this parameter and combine/specify the needed flags by 
	set_bit opcode. 

	Use mode 4 (PAGE_READWRITE) when want to make the memory address writable and/or readable.


   [5]. End of the readme.

   I don't bear any responsibility for use of this patch, use it at own risk and have fun.

   Any problem, note, etc: contact me at mail@sannybuilder.com

   Good luck and happy GTA modding! =)	
 