<?xml version="1.0" encoding="utf-8" ?>
<EternityPatchFile name="NewStage" enabled="new_stages" comment="This file enables the new stages patch">

<!-- The group of patches to make new stags are divided into three main parts. -->
<!-- All functions in this file will use version 1.08 (dump_09) addresses/signatures unless stated otherwise. -->

<!-- Naming convention ("unnamed" here means the real name is unknown)
	 stage_ -> unnamed object at offset 0x1A20 in ??? (TODO: check which object) (1.08 offset)
	 stageu_ -> unnamed object. Gets assigned at offset 0x25C0 of ??? (TODO: check which object) and 0x1C0 of XG::Game::Common::VCmnDemoStage (1.08 offsets)
	 stagev_ -> unnamed object, at offset 0x90 of ??? (TODO: check which object) (1.08 offset)
	 stagew_ -> unnamed object, its first value is a XG::Eva::EmCamera
	 stagex_ -> another unnamed object...
	 stagey_ -> unnamed object at offset of 0xB8 of ??? (TODO: check which object) (1.08 offset)
	 stagez_ -> unnamed object at offset 0xA0 of ??? (TODO: check which object) (1.08 offset)
	 stagem_ -> unnamed object at offset 0x5A8 of XG::Game::MainMenu (1.08 offset)
	 stagegt_ -> unnamed object at offset 0x3798 of XG::Game::Battle::Core::MainSystem (1.08 offset)	 
	 misc_ -> unnamed object, belonging to several UI classes
	 stageh_ -> for functions without object
	 scstage_ -> XG::Game::Battle::SceneCtrlStage
	 vcdstage_ -> XG::Game::Common::VCmnDemoStage
	 lcmap_ -> XG::Game::Lobby::Cmap
	 qrman_ -> XG::Game::Quest::QSTResourceManager
	 vcdplayer_ -> XG::Game::Common::VCmnDemoPlayer
	 mmenu_ -> XG::Game::MainMenu
	 stagesele_ -> XG::Game::Menu::SelectStage
	 shactor_ -> XG::Game::Battle::Shot::Actor
	 btlsound_ -> XG::Game::Battle::Core::BtlSound
	 btlnormal_ -> XG::Game::Event::BattleNormal
-->

<!-- 
Part1: increasing the number of stages (patching the definition data in the exe)
This part is divided into X subparts.
-->

<!-- Part1 Redirecting the acceses to the XV2StageDef1 strucure -->

	<!-- Signature 1.09 -->
	<Patch name="PatchStageDef1Top1" type="notify" search_start="0x1E76B4" comment="Takes place in stage_func1" >
		<Instruction code="48 8D 05 XXXXXXXX" comment="lea rax, stage_defs1" />
		<Instruction code="48 03 F8" comment="add rdi, rax" />
		<Instruction code="C7 44 24 20 80 00 00 00" comment="mov dword ptr[rsp+20h], 80h" />
		<Instruction code="41 B9 01 00 00 00" comment="mov r9d, 1" />
		<Instruction code="4C 8D 05 XXXXXXXX" comment="lea r8, 'data/stage/template.map'" />
		<Instruction code="33 D2" comment="xor edx, edx" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Main" />
	</Patch>
	
	<Patch name="PatchStageDef1Top2" type="notify" search_start="0x253385" comment="Takes place in scstage_func1" >
		<Instruction code="48 8D 05 XXXXXXXX" comment="lea rax, stage_defs1" />
		<Instruction code="48 03 F0" comment="add rsi, rax" />
		<Instruction code="48 8B 16" comment="mod rdx, [rsi]" />
		<Instruction code="48 C7 47 18 0F 00 00 00" comment="mov qword ptr [rdi+18h], 0Fh" />
		<Instruction code="48 89 5F 10" comment="mov [rdi+10h], rbx" />
		<Instruction code="48 83 7F 18 10" comment="mov qword ptr [rdi+18h], 10h" />
		<Instruction code="72 05" comment="jb +05" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Top" />
	</Patch>
	
	<Patch name="PatchStageDef1Top3" type="notify" search_start="0x262C69" comment="Takes place in stageu_ctor" >
		<Instruction code="48 8D 05 XXXXXXXX" comment="lea rax, stage_defs1" />
		<Instruction code="4C 8B 44 02 08" comment="mov r8, [rdx+rax+8]" />
		<Instruction code="48 8B 14 02" comment="mod rdx, [rdx+rax]" />
		<Instruction code="48 8B CB" comment="mov rcx, rbx" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		<Instruction code="8B 83 20 09 00 00" comment="mov eax, [rbx+920h]" />
		<Instruction code="89 83 E8 08 00 00" comment="mov [rbx+8E8h], eax" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Top" />
	</Patch>
	
	<Patch name="PatchStageDef1Top4" type="notify" search_start="0x265FD6" comment="Takes place in stageu_func1" >
		<Instruction code="89 91 20 09 00 00" comment="mov [rcx+920h], edx" />
		<Instruction code="48 8D 05 XXXXXXXX" comment="lea rax, stage_defs1" />	
		<Instruction code="48 63 D2" comment="movsxd rdx, edx" />	
		<Instruction code="48 8B D9" comment="mov rbx, rcx" />
		<Instruction code="48 C1 E2 05" comment="shl rdx, 5" />		
		<Instruction code="4C 8B 44 02 08" comment="mov r8, [rdx+rax+8]" />
		<Instruction code="48 8B 14 02" comment="mod rdx, [rdx+rax]" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />		
		
		<Notify inst_index="1" inst_offset="3" value="PatchStageDef1Top" />
	</Patch>
	
	<Patch name="PatchStageDef1Top5" type="notify" search_start="0x2AD995" comment="Takes place in vcdstage_func1" >
		<Instruction code="48 8D 05 XXXXXXXX" comment="lea rax, stage_defs1" />
		<Instruction code="48 03 F0" comment="add rsi, rax" />		
		<Instruction code="C7 44 24 20 80 00 00 00" comment="mov dword ptr[rsp+20h], 80h" />		
		<Instruction code="41 B9 01 00 00 00" comment="mod r9d, 1" />
		<Instruction code="4C 8D 05 XXXXXXXX" comment="lea r8, 'data/stage/template.map'" />
		<Instruction code="41 8B D1" comment="mov edx, r9d" />
		<Instruction code="48 8D 8B D0 01 00 00" comment="lea rcx, [rbx+1D0h]" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Top" />
	</Patch>
	
	<Patch name="PatchStageDef1Top6" type="notify" search_start="0x37724F" comment="Takes place in stagev_func1" >
		<Instruction code="4C 8D 35 XXXXXXXX" comment="lea r14, stage_defs1" />
		<Instruction code="4A 8B 5C 36 10" comment="mov rbx, [rsi+r14+10h]" />		
		<Instruction code="41 8B C9" comment="mov ecx, r9d" />		
		<Instruction code="E8 XXXXXXXX" comment="call is_stage_3B; stage 3b is the 'BFwis' stage" />
		<Instruction code="85 C0" comment="test eax, eax" />
		<Instruction code="74 05" comment="jz +5" />
		<Instruction code="4A 8B 5C 36 08" comment="mov rbx, [rsi+r14+8]" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Top" />
	</Patch>
	
	<Patch name="PatchStageDef1Top7" type="notify" search_start="0x3C8C5E" comment="Takes place in lcmap_func1" >
		<Instruction code="4C 8B 3D XXXXXXXX" comment="mov r15, cs:stage_defs1" />
		<Instruction code="BF 0D 00 00 00" comment="mov edi, 0Dh" />		
		<Instruction code="48 81 C3 88 00 00 00" comment="add rbx, 88h" />		
		<Instruction code="48 89 74 24 40" comment="mov [rsp+40h], rsi" />
		<Instruction code="8D 6F FF" comment="lea ebp, [rdi-1]" />
		<Instruction code="0F 1F 80 00 00 00 00" comment="long nop" />
		<Instruction code="48 83 3B 00" comment="cmp qword ptr [rbx], 0" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Top" />
	</Patch>
	
	<Patch name="PatchStageDef1Top8" type="notify" search_start="0x3C8D34" comment="Takes place in lcmap_func2" >
		<Instruction code="48 8B 35 XXXXXXXX" comment="mov rsi, cs:stage_defs1" />
		<Instruction code="48 89 B4 24 00 01 00 00" comment="mov [rsp+100h], rsi" />		
		<Instruction code="45 33 F6" comment="xor r14d, r14d" />		
		<Instruction code="4C 39 75 20" comment="cmp [rbp+20h], r14" />
		<Instruction code="0F 85 2F 02 00 00" comment="jnz +0x2F2" />
		<Instruction code="49 8B CD" comment="mov rcx, r13" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Top" />
	</Patch>
	
	<Patch name="PatchStageDef1Top9" type="notify" search_start="0x63303C" comment="Takes place in qrman_func1" >
		<Instruction code="48 8D 05 XXXXXXXX" comment="lea rax, stage_defs1" />
		<Instruction code="48 03 F8" comment="add rdi, rax" />		
		<Instruction code="48 8B 17" comment="mov rdx, [rdi]" />	
		<Instruction code="49 C7 43 E8 0F 00 00 00" comment="mov qword ptr [r11-18h], 0Fh" />
		<Instruction code="33 DB" comment="xor ebx, ebx" />
		<Instruction code="49 89 5B E0" comment="mov [r11-20h], rbx" />
		<Instruction code="88 5C 24 38" comment="mov byte ptr [rsp+38h], bl" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Top" />
	</Patch>
	
	<Patch name="PatchStageDef1Top10" type="notify" search_start="0x633203" comment="Takes place in qrman_func2" >
		<Instruction code="48 8D 05 XXXXXXXX" comment="lea rax, stage_defs1" />
		<Instruction code="48 8B 14 01" comment="mov rdx, [rcx+rax]" />		
		<Instruction code="48 C7 44 24 40 0F 00 00 00" comment="mod qword ptr [rsp+40h], 0Fh" />	
		<Instruction code="45 33 C0" comment="xor r8d, r8d" />
		<Instruction code="4C 89 44 24 38" comment="mov [rsp+38h], r8" />
		<Instruction code="44 88 44 24 28" comment="mov [rsp+28h], r8b" />
		<Instruction code="44 38 02" comment="cmp [rdx], r8b" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Top" />
	</Patch>	
	
	<Patch name="PatchStageDef1Off8_2" type="notify" search_start="0x13F5B3" comment="Takes place in stagew_func1" >
		<Instruction code="48 8D 3D XXXXXXXX" comment="lea rdi, stage_defs1_p8" />
		<Instruction code="48 8B 3C 38" comment="mov rdi, [rax+rdi]" />		
		<Instruction code="48 8B CF" comment="mod rcx, rdi" />	
		<Instruction code="FF 15 XXXXXXXX" comment="call strncmp" />
		<Instruction code="85 C0" comment="test eax, eax" />
		<Instruction code="74 1A" comment="jz +1A" />
		<Instruction code="41 B8 04 00 00 00" comment="mov r8d, 4" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Off8" />
	</Patch>
	
	<!-- signature 1.09.1 -->
	<Patch name="PatchStageDef1Off8_3" type="notify" search_start="0x2C8072" comment="Takes place in stagex_func1" >
		<Instruction code="48 8D 15 XXXXXXXX" comment="lea rdx, stage_defs1_p8" />
		<Instruction code="48 8B 14 10" comment="mov rdx, [rax+rdx]" />		
		<Instruction code="80 3A 00" comment="cmp byte ptr[rdx], 0" />	
		<Instruction code="75 05" comment="jnz +5" />
		<Instruction code="4C 8B C3" comment="mov r8, rbx" />
		<Instruction code="EB 13" comment="jmp +13" />
		<Instruction code="49 83 C8 FF" comment="or r8, -1" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Off8_Remainder13" />
	</Patch>
	
	<!-- Note, this patch has same signature as above because stagex_func1 and stagex_func2 are very similar or identical -->
	<!-- This could be problematic in an update because the patch doesn't change the function signature, so it could patch twice same function -->
	<!-- Atm, this will be solved by a remainder in the dll to update the patches addresses -->	
	<!-- signsture 1.09.1 -->
	<Patch name="PatchStageDef1Off8_4" type="notify" search_start="0x2C83E2" comment="Takes place in stagex_func2" >
		<Instruction code="48 8D 15 XXXXXXXX" comment="lea rdx, stage_defs1_p8" />
		<Instruction code="48 8B 14 10" comment="mov rdx, [rax+rdx]" />		
		<Instruction code="80 3A 00" comment="cmp byte ptr[rdx], 0" />	
		<Instruction code="75 05" comment="jnz +5" />
		<Instruction code="4C 8B C3" comment="mov r8, rbx" />
		<Instruction code="EB 13" comment="jmp +13" />
		<Instruction code="49 83 C8 FF" comment="or r8, -1" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Off8_Remainder14" />
	</Patch>
	
	<Patch name="PatchStageDef1Off8_5" type="notify" search_start="0x2AA61E" comment="Takes place in vcdplayer_func1" >
		<Instruction code="48 8D 1D XXXXXXXX" comment="lea rbx, stage_defs1_p8" />
		<Instruction code="48 8B 13" comment="mov rdx, [rbx]" />		
		<Instruction code="80 3A 00" comment="cmp byte ptr[rdx], 0" />	
		<Instruction code="75 05" comment="jnz +5" />
		<Instruction code="45 33 C0" comment="xor r8d, r8d" />
		<Instruction code="EB 0E" comment="jmp +E" />
		<Instruction code="49 83 C8 FF" comment="or r8, -1" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Off8" />
	</Patch>
	
	<Patch name="PatchStageDef1Off8_6" type="notify" search_start="0x3700D8" comment="Takes place in stagey_func1" >
		<Instruction code="4C 8D 0D XXXXXXXX" comment="lea r9, stage_defs1_p8" />
		<Instruction code="48 8D 1D XXXXXXXX" comment="lea rbx, stage_defs1_bottom_p8" />		
		<Instruction code="66 66 0F 1F 84 00 00 00 00 00" comment="very big nop" />	
		<Instruction code="49 8B 09" comment="mov rcx, [r9]" />
		<Instruction code="4D 8B C3" comment="mov r8, r11" />		
		<Instruction code="4C 2B C1" comment="sub r8, rcx" />
		<Instruction code="0F 1F 80 00 00 00 00" comment="big nop" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Off8" />
	</Patch>
	
	<Patch name="PatchStageDef1Off8_7" type="notify" search_start="0x37717D" comment="Takes place in stagev_func1" >
		<Instruction code="48 8D 1D XXXXXXXX" comment="lea rbx, stage_defs1_p8" />
		<Instruction code="4C 8B CB" comment="mov r9, rbx" />
		<Instruction code="4C 8D 25 XXXXXXXX" comment="lea r12, stage_defs1_bottom_p8" />		
		<Instruction code="66 90" comment="xchg ax, ax, aka double-nop" />	
		<Instruction code="49 8B 01" comment="mov rax, [r9]" />
		<Instruction code="4C 8B C6" comment="mov r8, rsi" />
		<Instruction code="4C 2B C0" comment="sub r8, rax" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Off8" />
	</Patch>
	
	<Patch name="PatchStageDef1Off8_8" type="notify" search_start="0x379C42" comment="Takes place in stagez_func1" >
		<Instruction code="74 45" comment="jz +45" />
		<Instruction code="45 8B D7" comment="mov r10d, r15d" />
		<Instruction code="4C 8D 0D XXXXXXXX" comment="lea r9, stage_defs1_p8" />	
		<Instruction code="66 90" comment="xchg ax, ax, aka double-nop" />			
		<Instruction code="49 8B 09" comment="mov rcx, [r9]" />	
		<Instruction code="4D 8B C3" comment="mov r8, r11" />
		<Instruction code="4C 2B C1" comment="sub r8, rcx" />
		<Instruction code="0F 1F 80 00 00 00 00" comment="long nop" />
		
		<Notify inst_index="2" inst_offset="3" value="PatchStageDef1Off8" />
	</Patch>
	
	<Patch name="PatchStageDef1Off8_9" type="notify" search_start="0x379CB4" comment="Takes place in stagez_func1" >
		<Instruction code="74 49" comment="jz +49" />
		<Instruction code="45 8B D7" comment="mov r10d, r15d" />
		<Instruction code="4C 8D 0D XXXXXXXX" comment="lea r9, stage_defs1_p8" />		
		<Instruction code="49 8B 09" comment="mov rcx, [r9]" />	
		<Instruction code="4D 8B C3" comment="mov r8, r11" />
		<Instruction code="4C 2B C1" comment="sub r8, rcx" />
		<Instruction code="0F 1F 80 00 00 00 00" comment="long nop" />
		
		<Notify inst_index="2" inst_offset="3" value="PatchStageDef1Off8" />
	</Patch>
	
	<Patch name="PatchStageDef1Off8_10" type="notify" search_start="0x548DE0" comment="Takes place in stagem_func1" >
		<Instruction code="4C 8D 25 XXXXXXXX" comment="lea r12, stage_defs1_p8" />
		<Instruction code="66 0F 1F 84 00 00 00 00  00" comment="big nop" />
		<Instruction code="45 33 C9" comment="xor r9d, r9d" />		
		<Instruction code="44 8B C6" comment="mov r8d, esi" />	
		<Instruction code="49 C1 E0 05" comment="shl r8, 5" />
		<Instruction code="48 8D 8C 24 80 00 00 00" comment="lea rcx, [rsp+80h]" />
		<Instruction code="41 8D 51 20" comment="lea edx, [r9+20h]" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Off8" />
	</Patch>
	
	<!-- This patch has the stage_defs1 address split into two parts. The dll is only notified of the first.
	     The dll will assume that the second component is at a fixed relative address from the first.
		 This could make the patch in the dll broken every time the patch signature changes -->
	<Patch name="PatchStageDef1Off8_11" type="notify" search_start="0x5544B6" comment="Takes place in misc_func1" >
		<Instruction code="48 8D 3D XXXXXXXX" comment="lea rdi, XXXXXXXX" />
		<Instruction code="41 83 FE 3F" comment="cmp r9d, 3Fh; 3F -> num of stages-1" />
		<Instruction code="77 33" comment="ja +33h" />
		<Instruction code="49 8B C6" comment="mov rax, r14" />
		<Instruction code="48 C1 E0 05" comment="shl rax, 5" />		
		<Instruction code="48 8B 94 38 XXXXXXXX" comment="mov rdx, [rax+rdi+XXXXXXXXh]" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1Off8_2" />
	</Patch>
	
	<Patch name="PatchStageDef1CHR" type="notify" search_start="0x53AE0D" comment="Takes place in mmenu_func1" >
		<Instruction code="48 8B 15 XXXXXXXX" comment="mov rdx, stage_def1_CHR" />
		<Instruction code="49 C7 43 E0 0F 00 00 00" comment="mov qword ptr [r11-20h], 0Fh" />
		<Instruction code="49 C7 43 D8 00 00 00 00" comment="mov qword ptr [r11-28h], 0" />
		<Instruction code="C6 44 24 40 00" comment="mov byte ptr[rsp+40h], 0" />
		<Instruction code="48 83 CB FF" comment="or rbx, -1" />
		<Instruction code="80 3A 00" comment="cmp byte ptr[rdx], 0" />
		<Instruction code="75 05" comment="jnz +5" />		
	
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1CHR" />
	</Patch>
	
	<Patch name="PatchStageDef1CHROff8" type="notify" search_start="0x53AE91" comment="Takes place in mmenu_func1" >
		<Instruction code="48 8D 4C 24 40" comment="lea rcx, [rsp+40h]" />
		<Instruction code="E8 XXXXXXXX" comment="call string_append" />
		<Instruction code="48 8B 15 XXXXXXXX" comment="mov rdx, stage_def1_CHR_p8" />
		<Instruction code="80 3A 00" comment="cmp byte ptr[rdx], 0" />	
		<Instruction code="75 09" comment="jnz +9" />
		<Instruction code="33 DB" comment="xor ebx, ebx" />
		<Instruction code="EB 0E" comment="jmp +E" />	
	
		<Notify inst_index="2" inst_offset="3" value="PatchStageDef1CHROff8" />
	</Patch>
	
	<Patch name="PatchStageDef1CHROff10" type="notify" search_start="0x53AE4F" comment="Takes place in mmenu_func1" >
		<Instruction code="E8 XXXXXXXX" comment="call string_copy" />
		<Instruction code="90" comment="nop" />
		<Instruction code="48 8B 15 XXXXXXXX" comment="mov rdx, stage_def1_CHR_p10" />
		<Instruction code="80 3A 00" comment="cmp byte ptr[rdx], 0" />	
		<Instruction code="75 05" comment="jnz +5" />
		<Instruction code="45 33 C0" comment="xor r8d, r8d" />
		<Instruction code="EB 14" comment="jmp +14" />	
	
		<Notify inst_index="2" inst_offset="3" value="PatchStageDef1CHROff10" />
	</Patch>
	
	<Patch name="PatchStageDef1BottomOff8_1" type="notify" search_start="0x2AA699" comment="Takes place in vcdplayer_func1" >
		<Instruction code="48 8D 05 XXXXXXXX" comment="lea rax, stage_def1_bottom_p8" />
		<Instruction code="48 3B D8" comment="cmp rbx, rax" />
		<Instruction code="7D 0C" comment="jge +0C" />
		<Instruction code="E9 7B FF FF FF" comment="jmp -85" />
		<Instruction code="44 89 BF 5C 05 00 00" comment="mov [rdi+55Ch], r15d" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1BottomOff8" />		
	</Patch>
	
	<!-- This patch takes place at same address than PatchStageDef1Off8_6 -->
	<Patch name="PatchStageDef1BottomOff8_2" type="notify" search_start="0x3700D8" comment="Takes place in stagey_func1" >
		<Instruction code="4C 8D 0D XXXXXXXX" comment="lea r9, stage_defs1_p8" />
		<Instruction code="48 8D 1D XXXXXXXX" comment="lea rbx, stage_defs1_bottom_p8" />		
		<Instruction code="66 66 0F 1F 84 00 00 00 00 00" comment="very big nop" />	
		<Instruction code="49 8B 09" comment="mov rcx, [r9]" />
		<Instruction code="4D 8B C3" comment="mov r8, r11" />		
		<Instruction code="4C 2B C1" comment="sub r8, rcx" />
		<Instruction code="0F 1F 80 00 00 00 00" comment="big nop" />
		
		<Notify inst_index="1" inst_offset="3" value="PatchStageDef1BottomOff8" />
	</Patch>
	
	<!-- This patch takes place at same address than PatchStageDef1Off8_7 -->
	<Patch name="PatchStageDef1BottomOff8_3" type="notify" search_start="0x37717D" comment="Takes place in stagev_func1" >
		<Instruction code="48 8D 1D XXXXXXXX" comment="lea rbx, stage_defs1_p8" />
		<Instruction code="4C 8B CB" comment="mov r9, rbx" />
		<Instruction code="4C 8D 25 XXXXXXXX" comment="lea r12, stage_defs1_bottom_p8" />		
		<Instruction code="66 90" comment="xchg ax, ax, aka double-nop" />	
		<Instruction code="49 8B 01" comment="mov rax, [r9]" />
		<Instruction code="4C 8B C6" comment="mov r8, rsi" />
		<Instruction code="4C 2B C0" comment="sub r8, rax" />
		
		<Notify inst_index="2" inst_offset="3" value="PatchStageDef1BottomOff8" />
	</Patch>
	
	<Patch name="PatchStageDef1BottomOff8_4" type="notify" search_start="0x379C35" comment="Takes place in stagez_func1" >
		<Instruction code="48 8D 1D XXXXXXXX" comment="lea rbx, stage_defs1_bottom_p8" />
		<Instruction code="4C 8B D8" comment="mov r11, rax" />
		<Instruction code="48 85 C0" comment="test rax, rax" />		
		<Instruction code="74 45" comment="jz +45" />	
		<Instruction code="45 8B D7" comment="mov r10d, r15d" />
		<Instruction code="4C 8D 0D XXXXXXXX" comment="lea r9, stage_defs1_p8" />
		<Instruction code="66 90" comment="xchg ax, ax, aka double nop" />
		<Instruction code="49 8B 09" comment="mov rcx, [r9]" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1BottomOff8" />
	</Patch>
	
	<Patch name="PatchStageDef1BottomOff8_5" type="notify" search_start="0x379CEA" comment="Takes place in stagez_func1" >
		<Instruction code="48 8D 05 XXXXXXXX" comment="lea rax, stage_defs1_bottom_p8" />
		<Instruction code="49 83 C1 20" comment="add r9, 20h" />
		<Instruction code="4C 3B C8" comment="cmp r9, rax" />		
		<Instruction code="7C C6" comment="jl -3A" />	
		<Instruction code="EB 03" comment="jmp +3" />
		<Instruction code="44 89 17" comment="mov [rdi], r10d" />
		<Instruction code="FF C3" comment="inc ebx" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef1BottomOff8" />
	</Patch>
	
<!-- Part 2: Redirecting the acceses to the ssid_to_idx array -->
	<Patch name="PatchStageSsidToIdx1" type="notify" search_start="0x504CFE" comment="Takes place in stagesele_vfunc_0068" >
		<Instruction code="78 12" comment="js +12" />
		<Instruction code="48 98" comment="cdqe" />
		<Instruction code="48 8D 0D XXXXXXXX" comment="lea rcx, ssid_to_idx" />
		<Instruction code="8B 04 81" comment="mov eax, [rcx+rax*4]" />
		<Instruction code="89 87 F8 01 00 00" comment="mov [rdi+1F8h], eax" />
		<Instruction code="48 8B 5C 24 30" comment="mov rbx, [rbp+30h]" />
		<Instruction code="48 83 C4 20" comment="add rsp, 20h" />
		
		<Notify inst_index="2" inst_offset="3" value="PatchStageSsidToIdxMain" />
	</Patch>
	
	<Patch name="PatchStageSsidToIdx2" type="notify" search_start="0x5053FA" comment="Takes place in stagesele_func1" >
		<Instruction code="85 DB" comment="test ebx, ebx" />
		<Instruction code="78 27" comment="js +27" />
		<Instruction code="48 8D 15 XXXXXXXX" comment="lea rdx, ssid_to_idx" />
		<Instruction code="48 8B C8" comment="mov rcx, rax" />
		<Instruction code="8B 1C 9A" comment="mov ebx, [rdx+rbx*4]" />
		<Instruction code="E8 XXXXXXXX" comment="call dummy_returns_rcx" />
		<Instruction code="48 8B C8" comment="mov rcx, rax" />
		
		<Notify inst_index="2" inst_offset="3" value="PatchStageSsidToIdx" />
	</Patch>
	
	<Patch name="PatchStageSsidToIdx3" type="notify" search_start="0x50549E" comment="Takes place in stagesele_func2" >
		<Instruction code="BE 24 00 00 00" comment="mov esi, 24h" />
		<Instruction code="48 89 44 24 60" comment="mov [rsp+60h], rax" />
		<Instruction code="4C 8D 35 XXXXXXXX" comment="lea r14, ssid_to_idx" />
		<Instruction code="48 8D 9F 08 02 00 00" comment="lea rbx, [rdi+208h]" />
		<Instruction code="48 8D AF 48 06 00 00" comment="lea rbp, [rdi+648h]" />
		<Instruction code="44 8D 66 FE" comment="lea r12d, [rsi-2]; Num of ss stages" />
				
		<Notify inst_index="2" inst_offset="3" value="PatchStageSsidToIdx" />
	</Patch>
	
<!-- Part 3: Redirecting the acceses to the stage_f6 array -->
	<Patch name="PatchStageF6" type="notify" search_start="0x256577" comment="Takes place in shactor_vfunc_0018_" >
		<Instruction code="48 63 90 84 00 00 00" comment="movsxd rdx, dword ptr [rax+84h]" />
		<Instruction code="85 D2" comment="test edx, edx" />
		<Instruction code="78 13" comment="js +13" />
		<Instruction code="48 8B C2" comment="mov rax, rdx" />
		<Instruction code="48 8D 15 XXXXXXXX" comment="lea rdx, stage_f6" />
		<Instruction code="8B 04 82" comment="mov eax, [rdx+rax*4]" />
		<Instruction code="89 81 1C 01 00 00" comment="mov [rcx+11Ch], eax" />
		
		<Notify inst_index="4" inst_offset="3" value="PatchStageF6" />
	</Patch>
	
<!-- Part 4: Redirecting the acceses to the stage_defs2 array -->	

	<Patch name="PatchStageDef2Top1" type="notify"  search_start="0x1B519A" comment="Takes place in stagegt_default" >
		<Instruction code="4C 8D 0D XXXXXXXX" comment="lea r9, stage_defs2" />
		<Instruction code="BB 40 00 00 00" comment="mov ebx, 40; number of stages" />
		<Instruction code="4C 8D 1D XXXXXXXX" comment="lea r11, stage_defs2_p8" />
		<Instruction code="4C 2B C9" comment="sub r9, rcx" />
		<Instruction code="4C 8D 15 XXXXXXXX" comment="lea r10, stage_defs2_p4" />
		<Instruction code="4C 2B D9" comment="sub r11, rcx" />
		<Instruction code="4C 2B D1" comment="sub r10, rcx" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageDef2Main" />
	</Patch>
	
	<!-- The 2 next patches are deleted because this function is now reimplemented by the patcher -->
	<!--
	<Patch name="PatchStageDef2Off8" type="notify"  search_start="0x1B519A" comment="Takes place in stagegt_default" >
		<Instruction code="4C 8D 0D XXXXXXXX" comment="lea r9, stage_defs2" />
		<Instruction code="BB 40 00 00 00" comment="mov ebx, 40; number of stages" />
		<Instruction code="4C 8D 1D XXXXXXXX" comment="lea r11, stage_defs2_p8" />
		<Instruction code="4C 2B C9" comment="sub r9, rcx" />
		<Instruction code="4C 8D 15 XXXXXXXX" comment="lea r10, stage_defs2_p4" />
		<Instruction code="4C 2B D9" comment="sub r11, rcx" />
		<Instruction code="4C 2B D1" comment="sub r10, rcx" />
		
		<Notify inst_index="2" inst_offset="3" value="PatchStageDef2Off8" />
	</Patch>
	
	-- This patch has same signature as two above ones --
	<Patch name="PatchStageDef2Off4" type="notify"  search_start="0x1B519A" comment="Takes place in stagegt_default" >
		<Instruction code="4C 8D 0D XXXXXXXX" comment="lea r9, stage_defs2" />
		<Instruction code="BB 40 00 00 00" comment="mov ebx, 40; number of stages" />
		<Instruction code="4C 8D 1D XXXXXXXX" comment="lea r11, stage_defs2_p8" />
		<Instruction code="4C 2B C9" comment="sub r9, rcx" />
		<Instruction code="4C 8D 15 XXXXXXXX" comment="lea r10, stage_defs2_p4" />
		<Instruction code="4C 2B D9" comment="sub r11, rcx" />
		<Instruction code="4C 2B D1" comment="sub r10, rcx" />
		
		<Notify inst_index="4" inst_offset="3" value="PatchStageDef2Off4" />
	</Patch>
	-->
	
	<Patch name="PatchStageDef2Top2" type="notify"  search_start="0x1B5229" comment="Takes place in stagegt_func2" >
		<Instruction code="48 63 C2" comment="movsxd rax, edx" />
		<Instruction code="48 6B C8 07" comment="imul rcx, rax, 7" />
		<Instruction code="41 8B C0" comment="mov eax, r8d" />
		<Instruction code="48 03 C8" comment="add rcx, rax" />
		<Instruction code="48 8D 04 49" comment="lea rax, [rcx+rcx*2]" />
		<Instruction code="48 8D 0D XXXXXXXX" comment="lea rcx, stage_defs2" />
		<Instruction code="48 8D 04 C1" comment="lea rax, [rcx+rax*8]" />
		
		<Notify inst_index="5" inst_offset="3" value="PatchStageDef2Top" />
	</Patch>
	
<!-- Part 5: Redirecting the acceses to the stage_sounds array -->	

	<Patch name="PatchStageSounds" type="notify" search_start="0x1D33BD" comment="Takes place in btlsound_func1" >
		<Instruction code="48 8D 0D XXXXXXXX" comment="lea rcx, stage_sounds" />
		<Instruction code="48 8B 0C C1" comment="mov rcx, [rcx+rax*8]" />
		<Instruction code="48 85 C9" comment="test rcx, rcx" />
		<Instruction code="74 DA" comment="jz -26" />
		<Instruction code="C7 43 10 41 01 00 00" comment="mov dword ptr [rbx+10h], 141h" />
		
		<Notify inst_index="0" inst_offset="3" value="PatchStageSoundsMain" />
	</Patch>
	
<!-- Part 6: Redirecting the accesses to the stage_music array -->

	<Patch name="PatchStageMusic" type="notify" search_start="0x37F80D" comment="Takes place in btlnormal_func1" >
		<Instruction code="48 8D 0D XXXXXXXX" comment="lea rcx, stage_music" />
		<Instruction code="48 63 43 1C" comment=" movsxd rax, dword ptr [rbx+1Ch]" />
		<Instruction code="8B 14 81" comment="mov edx, [rcx+rax*4]" />
		<Instruction code="74 08" comment="jz +8" />
		<Instruction code="8B 43 2C" comment="mov eax, [rbx+2Ch]" />
	
		<Notify inst_index="0" inst_offset="3" value="PatchStageMusicMain" />
	</Patch>
	
<!-- Part 7: Patching number of stages -->
<!-- There is currently a limit of 255 stages due to 8 bits constants -->

	<!-- Signature 1.09.1 -->
	<Patch name="PatchStageNum1" type="notify" search_start="0x2C8036" comment="Takes place in stagex_func1" >		
		<Instruction code="83 7C 24 6C 40" comment="cmp dword ptr [rsp+6Ch], 40h -> stage num" />
		<Instruction code="0F 83 89 00 00 00" comment="jnb +0x89" />
		<Instruction code="48 8B 15 XXXXXXXX" comment="mov rdx, cs:XXXXXXXX" />
		<Instruction code="80 3A 00" comment="cmp byte ptr [rdx], 0" />
		<Instruction code="75 05" comment="jnz +5" />
		<Instruction code="4C 8B C3" comment="mov r8, rbx" />
		<Instruction code="EB 0E" comment="jmp +E" />
		
		<Notify inst_index="0" inst_offset="4" value="PatchStageNum_8" />	
	</Patch>

	<!-- Note, this patch has same signature as above because stagex_func1 and stagex_func2 are very similar or identical -->
	<!-- Signature 1.09.1 -->
	<Patch name="PatchStageNum2" type="notify" search_start="0x2C83A6" comment="Takes place in stagex_func2" >		
		<Instruction code="83 7C 24 6C 40" comment="cmp dword ptr [rsp+6Ch], 40h -> stage num" />
		<Instruction code="0F 83 89 00 00 00" comment="jnb +0x89" />
		<Instruction code="48 8B 15 XXXXXXXX" comment="mov rdx, cs:XXXXXXXX" />
		<Instruction code="80 3A 00" comment="cmp byte ptr [rdx], 0" />
		<Instruction code="75 05" comment="jnz +5" />
		<Instruction code="4C 8B C3" comment="mov r8, rbx" />
		<Instruction code="EB 0E" comment="jmp +E" />
		
		<Notify inst_index="0" inst_offset="4" value="PatchStageNum_8" />
	
	</Patch>
	
	<!-- This patch has same signature as PatchStageDef1Off8_11. This one MUST go always below !-->
	<Patch name="PatchStageNum3" type="notify" search_start="0x5544B6" comment="Takes place in misc_func1" >
		<Instruction code="48 8D 3D XXXXXXXX" comment="lea rdi, XXXXXXXX" />
		<Instruction code="41 83 FE 3F" comment="cmp r9d, 3Fh; 3F -> num of stages-1" />
		<Instruction code="77 33" comment="ja +33h" />
		<Instruction code="49 8B C6" comment="mov rax, r14" />
		<Instruction code="48 C1 E0 05" comment="shl rax, 5" />		
		<Instruction code="48 8B 94 38 XXXXXXXX" comment="mov rdx, [rax+rdi+XXXXXXXXh]" />
		
		<Notify inst_index="1" inst_offset="3" value="PatchStageNumM1_8" />
	</Patch>
	
	<!-- This patch is NOT needed. Or rather, it must not be done -->
	<!-- This patch has same asignature as PatchStageSsidToIdx3. This one MUST go always below !-->
	<!--<Patch name="PatchStageNum4" type="notify" search_start="0x50549E" comment="Takes place in stagesele_func2" >
		<Instruction code="BE 24 00 00 00" comment="mov esi, 24h" />
		<Instruction code="48 89 44 24 60" comment="mov [rsp+60h], rax" />
		<Instruction code="4C 8D 35 XXXXXXXX" comment="lea r14, ssid_to_idx" />
		<Instruction code="48 8D 9F 08 02 00 00" comment="lea rbx, [rdi+208h]" />
		<Instruction code="48 8D AF 48 06 00 00" comment="lea rbp, [rdi+648h]" />
		<Instruction code="44 8D 66 FE" comment="lea r12d, [rsi-2]; Num of ss stages" />
				
		<Notify inst_index="5" inst_offset="3" value="PatchStageSsNumComplex" />
	</Patch>-->
	
	<!-- This patch is deleted because this function is now reimplemented by the patcher -->
	<!-- This patch has the same signature as PatchStageDef2Top1. This one MUST go always below !-->
	<!--<Patch name="PatchStageNum5" type="notify"  search_start="0x1B519A" comment="Takes place in stagegt_default" >
		<Instruction code="4C 8D 0D XXXXXXXX" comment="lea r9, stage_defs2" />
		<Instruction code="BB 40 00 00 00" comment="mov ebx, 40; number of stages" />
		<Instruction code="4C 8D 1D XXXXXXXX" comment="lea r11, stage_defs2_p8" />
		<Instruction code="4C 2B C9" comment="sub r9, rcx" />
		<Instruction code="4C 8D 15 XXXXXXXX" comment="lea r10, stage_defs2_p4" />
		<Instruction code="4C 2B D9" comment="sub r11, rcx" />
		<Instruction code="4C 2B D1" comment="sub r10, rcx" />
		
		<Notify inst_index="1" inst_offset="1" value="PatchStageNum_32" />
	</Patch>-->
	
	<!-- This patch is deleted because this function is now reimplemented by the patcher -->
	<!--<Patch name="PatchStageNum6" type="notify"  search_start="0x1B5454" comment="Takes place in stagegt_gettarget" >
		<Instruction code="83 FA 40" comment="cmp edx, 40h; num of stages" />
		<Instruction code="73 1C" comment="jnb +1C" />
		<Instruction code="41 83 F8 07" comment="cmp r8d, 7; num of gates" />
		<Instruction code="73 16" comment="jnb +16" />
		<Instruction code="48 63 C2" comment="movsxd rax, edx" />
		<Instruction code="48 6B D0 07" comment="imul rdx, rax, 7" />
		<Instruction code="49 63 C0" comment="movsxd rax, r8d" />
		
		<Notify inst_index="0" inst_offset="2" value="PatchStageNum_8" />
	</Patch>-->
	
	<!-- This patch is deleted because stagegt_findgate is reimplemented by the patcher now -->
	<!--<Patch name="PatchStageNum7" type="notify"  search_start="0x1B5254" comment="Takes place in stagegt_findgate" >
		<Instruction code="83 FA 40" comment="cmp edx, 40h; num of stages" />
		<Instruction code="73 2D" comment="jnb +2D" />
		<Instruction code="48 63 C2" comment="movsxd rax, edx" />
		<Instruction code="4C 69 C8 A8 00 00 00" comment="imul r9, rax, 0A8h; A8 = sizeof(XV2StageDef2)" />
		<Instruction code="4C 03 C9" comment="add r9, rcx" />		
		<Instruction code="33 C0" comment="xor eax, eax" />
		<Instruction code="49 8D 49 08" comment="lea rcx, [r9+8]" />
		
		<Notify inst_index="0" inst_offset="2" value="PatchStageNum_8" />
	</Patch>-->
	
	<Patch name="PatchStageNum8" type="notify" search_start="0x1D3399" comment="This function takes place in btlsound_func1" >	
		<Instruction code="83 FA 3F" comment="cmp edx, 3Fh; 3F -> num of stages-1" />
		<Instruction code="77 59" comment="ja +59" />
		<Instruction code="44 8B 41 10" comment="mov r8d, [rcx+10h]" />
		<Instruction code="45 85 C0" comment="test r8d, r8d" />
		<Instruction code="78 09" comment="js +9" />
		<Instruction code="41 8B C0" comment="mov eax, r8d" />
		
		<Notify inst_index="0" inst_offset="2" value="PatchStageNumM1_8" />
	</Patch>
	
<!-- Part 8: Patching the stage_eve function -->

	<!-- This is a debug patch to dump the original stages data before we do any patch on it" -->
	<Patch name="DumpEve" type="notify" enabled="IsDebuggingStages()" search_start="0x2C4EE0" comment="The hooked function is stage_eve" >
		<Instruction code="48 83 EC 28" comment="sub rsp, 28h" />
		<Instruction code="4C 63 C9" comment="movsx r9, ecx" />
		<Instruction code="4C 8B D2" comment="mov r10, rdx" />
		<Instruction code="48 8D 15 XXXXXXXX" comment="lea rdx, XXXXXXXX" />
		<Instruction code="41 83 F9 3D" comment="cmp r9d, 3D; 3D -> num of stages" />
		<Instruction code="77 52" comment="ja +52h" />
		<Instruction code="42 0F B6 84 0A XXXXXXXX" comment="movzx eax, XXXXXXXX[rdx+r9]" />
		<Instruction code="44 8B 84 82 XXXXXXXX" comment="mod r8d, XXXXXXXX[rdx+rax*4]" />
		<Instruction code="4C 03 C2" comment="add r8, rdx" />
		<Instruction code="41 FF E0" comment="jmp rdx" />
		
		<Notify value="SetupStageEve" />
	</Patch>
	
	<Patch name="LocateStringCopy" type="notify" search_start="0x2C4F15" comment="Takes place in stage_eve" >
		<Instruction code="80 3A 00" comment="cmp byte ptr [rdx], 0" />
		<Instruction code="75 12" comment="jnz +12" />
		<Instruction code="45 33 C0" comment="xor r8d, r8d" />
		<Instruction code="49 8B CA" comment="mov rcx, r10" />
		<Instruction code="E8 XXXXXXXX" comment="call string_copy" />
		<Instruction code="33 C0" comment="xor eax, eax" />
		<Instruction code="48 83 C4 28" comment="add rsp, 28h" />
		<Instruction code="C3" comment="ret" />
		<Instruction code="49 83 C8 FF" comment="or r8, -1" />
		
		<Notify inst_index="4" inst_offset="1" value="OnStringCopyLocated" />
	</Patch>

	<!-- This patch has same signature as DumpEve but must be loaded always after -->
	<Patch name="ReimplementStageEve" type="hook" search_start="0x2C4EE0" comment="The hooked function is stage_eve" >
		<Instruction code="48 83 EC 28" comment="sub rsp, 28h" />
		<Instruction code="4C 63 C9" comment="movsx r9, ecx" />
		<Instruction code="4C 8B D2" comment="mov r10, rdx" />
		<Instruction code="48 8D 15 XXXXXXXX" comment="lea rdx, XXXXXXXX" />
		<Instruction code="41 83 F9 3D" comment="cmp r9d, 3D; 3D -> num of stages" />
		<Instruction code="77 52" comment="ja +52h" />
		<Instruction code="42 0F B6 84 0A XXXXXXXX" comment="movzx eax, XXXXXXXX[rdx+r9]" />
		<Instruction code="44 8B 84 82 XXXXXXXX" comment="mod r8d, XXXXXXXX[rdx+rax*4]" />
		<Instruction code="4C 03 C2" comment="add r8, rdx" />
		<Instruction code="41 FF E0" comment="jmp rdx" />
		
		<Hook value="StageEveReimplemented" />
	</Patch>
	
<!-- Part 9: Misc patches -->

	<!-- Signature 1.09 -->
	<Patch name="OverrideFarClip" type="hook" search_start="0x13E4D0" comment="Hooked function is stagew_func1" >
		<Instruction code="40 53" comment="push rbx" />
		<Instruction code="48 83 EC 60" comment="sub rsp, 60h" />
		<Instruction code="48 8B 05 XXXXXXXX" comment="mov rax, XG_Game_Battle_Core_MainSystem_singleton" />
		<Instruction code="48 8B D9" comment="mov rbx, rcx" />
		<Instruction code="0F 29 74 24 50" comment="mov [rsp+50h], xmm6" />
		<Instruction code="0F 29 7C 24 40" comment="mov [rsp+40h], xmm7" />
		<Instruction code="0F 57 FF" comment="xorps xmm7, xmm7" />
		<Instruction code="44 0F 29 44 24 30" comment="mov [rsp+30h], xmm8" />
		
		<Hook value="OverrideFarClip" setup="SetupSetFarClip" />
	</Patch>
	
	<!-- This patch is required by the one above to get the current stage id -->
	<!-- This patch doesn't only locates the singleton, but it also "protects" the implementation of the GetCurrentStage in the DLL -->
	<!-- Any change in the 3FA0 and/or 920 would require the implementation of GetCurrentStage to be corrected -->
	<!-- Signature 1.09 -->
	<Patch name="LocateCoreMainSystemForStages" type="notify" search_start="0x13E554" comment="Takes place in stagew_func1" >
		<Instruction code="48 8B 05 XXXXXXXX" comment="mov rax, cs:XG_Game_Battle_Core_MainSystem_singleton" />
		<Instruction code="0F 2F F7" comment="comiss xmm6, xmm7" />
		<Instruction code="76 72" comment="jbe +72" />
		<Instruction code="48 85 C0" comment="test rax, rax" />
		<Instruction code="74 6D" comment="je +6D" />
		<Instruction code="48 8B 88 A0 3F 00 00" comment="mov rcx, [rax+3FA0h]" />
		<Instruction code="48 85 C9" comment="test rcx, rcx" />
		<Instruction code="74 61" comment="je 61" />
		<Instruction code="48 63 81 20 09 00 00" comment="movsxd eax, [rcx+920h]; eax = stage_id" />
		
		<Notify inst_index="0" inst_offset="3" value="OnCoreMainSystemLocated_Stages" />
	</Patch>
	
	<!-- This patch is only to protect the implementation of OverrideFarClip -->
	<!-- Any change in the 0xD0 would require the OverrideFarClip to be updated in the dll -->
	<!-- Signature 1.09 -->
	<Patch name="ProtectOverrideFarClip" type="notify" search_start="0x13E5F3" comment="Takes place in stagew_func1" >
		<Instruction code="83 88 EC 00 00 00 06" comment="or dword ptr [rax+0ECh], 6" />
		<Instruction code="F3 0F 11 B0 D0 00 00 00" comment="movss dword ptr [rax+0D0h], xmm6" />
		<Instruction code="48 8B 03" comment="mov rax, [rbx]" />
		<Instruction code="0F 28 74 24 50" comment="movaps xmm6, [rsp+50h]" />
		
		<Notify value="NULL" />
	</Patch>
	
<!-- Part 10: Increase the memory of XG::Game::Battle::Core::MainSystem, and redirect stagems to the end -->	
	
	<!-- stagegt_ object has at beginning a buffer of 0x2A00, which is 0xA8 (size of XV2StageDef2) * num of stages 
		 After 0x2A00, it holds 7*num_of_stages 32bits variables 
		 (Total size in 1.08: 0x3100)
		 The address of this object within XG::Game::Battle::Core::MainSystem must be changed from 0x5FB8 to the end (and allocation size of Core::MainSystem increased) 
		 Then we must change all acceses to 0x5FB8 in Core::MainSystem to the new location.
		 In some points, it may be better to reimplement some functions 
	-->
	
	<!-- This patch needs change in the dll when/if stagems gets moved from 0x3798 to other place -->
	<!-- This patch is debug only, not needed in release version -->	
	<!-- Signature 1.09.1 -->
	<Patch name="SetMainSystemCtorDeadMemory" type="hook" enabled="IsDebuggingStages()" search_start="0x125D85" >
		<Instruction code="41 83 C8 FF" comment="or r8d, FFFFFFFF" />
		<Instruction code="33 D2" comment="xor edx, edx" />
		<Instruction code="B9 80 9A 00 00" comment="mov ecx, 0x9A80" />
		<Instruction code="FF15 XXXXXXXX" comment="call new[]" />
		<Instruction code="48 89 44 24 68" comment="mov [rsp+68h], rax" />
		<Instruction code="48 85 C0" comment="test eax, eax" />
		<Instruction code="74 0B" comment="jz +0xB" />
		<Instruction code="48 8B C8" comment="mov rcx, rax" />
		<Instruction code="E8 XXXXXXXX" comment="call XG::Game::Battle::Core::MainSystem ctor" />
		
		<Hook type="call" inst_index="8" value="HookedMainSystem_Ctor" setup="SetupMainSystem_Ctor" />
	</Patch>
	
	<!-- This patch has the same signature as the above one, but it needs to be always below  -->
	<!-- This patch needs change in the dll when/if stagems gets moved from 0x3798 to other place -->
	<!-- Signature 1.09.1 -->
	<Patch name="IncreaseCoreMainSystemMemory" type="notify" search_start="0x125D85" >
		<Instruction code="41 83 C8 FF" comment="or r8d, FFFFFFFF" />
		<Instruction code="33 D2" comment="xor edx, edx" />
		<Instruction code="B9 80 9A 00 00" comment="mov ecx, 0x9A80" />
		<Instruction code="FF15 XXXXXXXX" comment="call new[]" />
		<Instruction code="48 89 44 24 68" comment="mov [rsp+68h], rax" />
		<Instruction code="48 85 C0" comment="test eax, eax" />
		<Instruction code="74 0B" comment="jz +0xB" />
		<Instruction code="48 8B C8" comment="mov rcx, rax" />
		<Instruction code="E8 XXXXXXXX" comment="call XG::Game::Battle::Core::MainSystem ctor" />
		
		<Notify inst_index="2" inst_offset="1" value="IncreaseCoreMainSystemMemory" />
	</Patch>
	
	<!-- This patch needs change in the dll when/if stagems gets moved from 0x3798 to other place -->
	<!-- This patch is debug only, not needed in release version -->	
	<Patch name="UnsetMainSystemCtorDeadMemory" type="hook" enabled="IsDebuggingStages()" search_start="0x1B8340" >
		<Instruction code="40 57" comment="push rdi" />
		<Instruction code="48 83 EC 30" comment="sub rsp, 30h" />
		<Instruction code="48 C7 44 24 20 FE FF FF FF" comment="mov qword ptr[rsp+20h], -1" />
		<Instruction code="48 89 5C 24 48" comment="mov [rsp+48h], rbx" />
		<Instruction code="48 8B F9" comment="mov rdi, rcx" />
		<Instruction code="48 8D 05 XXXXXXXX" comment="lea rax, XG::Game::Battle::Core::MainSystem vtable" />
		<Instruction code="48 89 01" comment="mov [rcx], rax" />
		<Instruction code="C7 05 XXXXXXXX 00 00 00 00" comment="mov XXXXXXXX, 0" />
		<Instruction code="83 61 24 FD" comment="and dword ptr [rcx+24h], 0FFFFFFFDh" />
		
		<Hook value="HookedMainSystem_Dtor" setup="SetupMainSystem_Dtor" />
	</Patch>
	
	<Patch name="ReimplementStageGtClear" type="hook" search_start="0x1B50D0" comment="The hooked function is stagegt_clear" >
		<Instruction code="48 8D 41 24" comment="lea rax, [rcx+24h]" />
		<Instruction code="33 D2" comment="xor edx, edx" />
		<Instruction code="B9 40 00 00 00" comment="mov ecx, 40h; num of stages" />
		<Instruction code="0F 1F 44 00 00" comment="penta nop" />
		<Instruction code="48 89 50 DC" comment="mov [rax-24h], rdx" />
		<Instruction code="48 89 50 F4" comment="mov [rax-0Ch], rdx" />
		<Instruction code="C7 40 E4 FF FF FF FF" comment="mov dword ptr [rax-1Ch], 0FFFFFFFFh" />
		
		<Hook value="StageGtClearReimplemented" />		
	</Patch>
	
	<Patch name="ReimplementStageGtDefault" type="hook"  search_start="0x1B5190" comment="The hooked function is stagegt_default" >
		<Instruction code="48 89 5C 24 08" comment="mov [rsp+8], rbx" />
		<Instruction code="48 89 7C 24 10" comment="mov [rsp+10], rdi" />
		<Instruction code="4C 8D 0D XXXXXXXX" comment="lea r9, stage_defs2" />
		<Instruction code="BB 40 00 00 00" comment="mov ebx, 40; number of stages" />
		<Instruction code="4C 8D 1D XXXXXXXX" comment="lea r11, stage_defs2_p8" />
		<Instruction code="4C 2B C9" comment="sub r9, rcx" />
		<Instruction code="4C 8D 15 XXXXXXXX" comment="lea r10, stage_defs2_p4" />
		<Instruction code="4C 2B D9" comment="sub r11, rcx" />
		<Instruction code="4C 2B D1" comment="sub r10, rcx" />
		
		<Hook value="StageGtDefaultReimplemented" />
	</Patch>
	
	<!-- This patch needs update when size of XG::Game::Battle::Core::MainSystem changes -->
	<!-- This patch is not really needed because the called function ignores rcx (this), but anyway... -->
	<!-- Signature 1.09.1 -->
	<Patch name="RedirectStageGtFunc2" type="write" search_start="0x28A410" >
		<Instruction code="8B 97 20 09 00 00" comment="mov edx, [rdi+920h]" />
		<Instruction code="49 8D 8E C8 5F 00 00" comment="lea rcx, [r14+5FC8h]" />
		<Instruction code="44 8B C3" comment="mov r8d, ebx" />
		<Instruction code="E8 XXXXXXXX" comment="call stagegt_func2" />
		<Instruction code="48 85 C0" comment="test rax, rax" />
		<Instruction code="74 36" comment="jz +0x36" />
		
		<Write inst_index="1" inst_offset="3" value="70 9A" />	
	</Patch>
	
	<Patch name="ReimplementStageGtFindGate" type="hook"  search_start="0x1B5250" comment="The hooked function is stagegt_findgate" >
		<Instruction code="85 D2" comment="test edx, edx" />
		<Instruction code="78 32" comment="js +0x32" />
		<Instruction code="83 FA 40" comment="cmp edx, 40h; num of stages" />
		<Instruction code="73 2D" comment="jnb +2D" />
		<Instruction code="48 63 C2" comment="movsxd rax, edx" />
		<Instruction code="4C 69 C8 A8 00 00 00" comment="imul r9, rax, 0A8h; A8 = sizeof(XV2StageDef2)" />
		<Instruction code="4C 03 C9" comment="add r9, rcx" />		
		<Instruction code="33 C0" comment="xor eax, eax" />
		<Instruction code="49 8D 49 08" comment="lea rcx, [r9+8]" />
		
		<Hook value="StageGtFindGateReimplemented" />
	</Patch>
	
	<Patch name="ReimplementStageGtFindGateByName" type="hook" search_start="0x1B52A0" comment="The hooked function is stagegt_findgatebyname" >
		<Instruction code="48 89 5C 24 08" comment="mov [rsp+8], rbx" />
		<Instruction code="48 89 7C 24 10" comment="mov [rsp+10], rdi" />
		<Instruction code="48 63 C2" comment="movsxd rax, edx" />
		<Instruction code="49 8B F8" comment="mov rdi, r8" />
		<Instruction code="48 69 D8 A8 00 00 00" comment="imul rbx, rax, 0A8h; A8 = sizeof(XV2StageDef2)" />
		<Instruction code="48 03 D9" comment="add rbx, rcx" />
		<Instruction code="45 33 C9" comment="xor r9d, r9d" />
		<Instruction code="4C 8B D3" comment="mov r10, rbx" />
		<Instruction code="49 8B 02" comment="mov rax, [r10]" />
		
		<Hook value="StageGtFindGateByNameReimplemented" />
	</Patch>
	
	<Patch name="ReimplementStageGtGetGate" type="hook" search_start="0x1B5340" comment="The hooked function is stagegt_getgate" >
		<Instruction code="4C 8B C9" comment="mov r9, rcx" />
		<Instruction code="41 83 F8 07" comment="cmp r8d, 7" />
		<Instruction code="72 03" comment="jb +03" />
		<Instruction code="33 C0" comment="xor eax, eax" />
		<Instruction code="C3" comment="ret" />
		<Instruction code="48 63 C2" comment="movsxd rax, edx" />
		<Instruction code="48 6B C8 07" comment="imul rcx, rax, 7" />
		
		<Hook value="StageGtGetGateReimplemented" />
	</Patch>
	
	<Patch name="ReimplementStageGtGetExtraByTarget" type="hook" search_start="0x1B5370" comment="The hooked function is stagegt_getextrabytarget" >
		<Instruction code="4C 8B D1" comment="mov r10, rcx" />
		<Instruction code="85 D2" comment="test edx, edx" />
		<Instruction code="78 31" comment="js +0x31" />
		<Instruction code="83 FA 40" comment="cmp edx, 40h; num of stages" />
		<Instruction code="73 2C" comment="jnb +0x2C" />
		<Instruction code="4C 63 DA" comment="movsxd r11, edx" />
		<Instruction code="45 33 C9" comment="xor r9d, r9d" />
		<Instruction code="49 69 C3 A8 00 00 00" comment="imul rax, r11, 0A8h; A8 = sizeof(XV2StageDef2)" />
		
		<Hook value="StageGtGetExtraByTargetReimplemented"/>
	</Patch>
	
	<Patch name="ReimplementStageGtGetExtraByName" type="hook" search_start="0x1B53D0" comment="The hooked function is stagegt_getextrabyname" >
		<Instruction code="48 89 5C 24 08" comment="mov [rsp+8], rbx" />
		<Instruction code="48 89 7C 24 10" comment="mov [rsp+10], rdi" />		
		<Instruction code="48 63 FA" comment="movsxd rdi, edx" />
		<Instruction code="4D 8B D8" comment="mov r11, r8" />
		<Instruction code="4C 69 D7 A8 00 00 00" comment="imul r10, rdi, 0A8h; A8 = sizeof(XV2StageDef2)" />
		<Instruction code="48 8B D9" comment="mov rbx, rcx" />
		<Instruction code="4C 03 D1" comment="add r10, rcx" />
		
		<Hook value="StageGtGetExtraByNameReimplemented"/>
	</Patch>
	
	<Patch name="ReimplementStageGtGetTarget" type="hook" search_start="0x1B5450" comment="The hooked function is stagegt_gettarget" >
		<Instruction code="85 D2" comment="test edx, edx" />
		<Instruction code="78 21" comment="js +21" />
		<Instruction code="83 FA 40" comment="cmp edx, 40h; num of stages" />
		<Instruction code="73 1C" comment="jnb +1C" />
		<Instruction code="41 83 F8 07" comment="cmp r8d, 7; num of gates" />
		<Instruction code="73 16" comment="jnb +16" />
		<Instruction code="48 63 C2" comment="movsxd rax, edx" />
		<Instruction code="48 6B D0 07" comment="imul rdx, rax, 7" />
		<Instruction code="49 63 C0" comment="movsxd rax, r8d" />
		
		<Hook value="StageGtGetTargetReimplemented" />
	</Patch>
	
	<Patch name="ReimplementStageGtClearExtra" type="hook" search_start="0x1B5480" comment="The hooked function is stagegt_clearextra" >
		<Instruction code="33 D2" comment="xor edx, edx" />
		<Instruction code="48 8D 81 00 2A 00 00" comment="lea rax, [rcx+2A00h]" />
		<Instruction code="8D 4A 1C" comment="lea ecx, [rdx+1Ch]" />
		<Instruction code="0F 1F 40 00" comment="tetra-nop" />
		<Instruction code="48 89 10" comment="mov [rax], rdx" />
		
		<Hook value="StageGtClearExtraReimplemented" />
	</Patch>
	
	<Patch name="ReimplementStageGtSet" type="hook" search_start="0x1B54C0" comment="The hooked function is stagegt_set" >
		<Instruction code="4C 8B D9" comment="mov r11, rcx" />
		<Instruction code="83 FA 40" comment="cmp edx, 40h; num of stages" />
		<Instruction code="73 63" comment="jnb +63" />
		<Instruction code="85 D2" comment="test edx, edx" />
		<Instruction code="78 5F" comment="js +5F" />
		<Instruction code="44 8B 54 24 28" comment="mov r10d, [rsp+28h]" />
		
		<Hook value="StageGtSetReimplemented" />		
	</Patch>
	
	<Patch name="ReimplementStageGtSetExtraByTarget" type="hook" search_start="0x1B5530" comment="The hooked function is stagegt_setextrabytarget" >
		<Instruction code="4C 8B D9" comment="mov r11, rcx" />
		<Instruction code="33 C0" comment="xor eax, eax" />
		<Instruction code="48 63 CA" comment="movsxd rcx, edx" />
		<Instruction code="48 69 D1 A8 00 00 00" comment="imul rdx, rcx, 0A8h; A8 = sizeof(XV2StageDef2)" />
		<Instruction code="48 83 C2 08" comment="add rdx, 8" />
		<Instruction code="49 03 D3" comment="add rdx, r11" />
		
		<Hook value="StageGtSetExtraByTargetReimplemented" />
	</Patch>
	
	<Patch name="ReimplementStageGtSetExtraByName" type="hook" search_start="0x1B5580" comment="The hooked function is stagegt_setextrabyname" >
		<Instruction code="48 89 5C 24 08" comment="mov [rsp+8], rbx" />
		<Instruction code="48 89 74 24 10" comment="mov [rsp+10], rsi" />
		<Instruction code="48 89 7C 24 18" comment="mov [rsp+18], rdi" />
		<Instruction code="48 63 F2" comment="mov rsi, edx" />
		<Instruction code="49 8B D8" comment="mov rbx, r8" />
		<Instruction code="4C 69 DE A8 00 00 00" comment="imul r11, rsi, 0A8h; A8 = sizeof(XV2StageDef2)" />
		<Instruction code="48 8B F9" comment="mov rdi, rcx" />
		
		<Hook value="StageGtSetExtraByNameReimplemented" />
	</Patch>
	
	<Patch name="ReimplementStageGtSetUnk10ByTarget" type="hook" search_start="0x1B5620" comment="The hooked function is stagegt_setunk10bytarget" >
		<Instruction code="48 63 C2" comment="movsxd rax, edx" />
		<Instruction code="4C 69 D0 A8 00 00 00" comment="imul r10, rax, 0A8h; A8 = sizeof(XV2StageDef2)" />
		<Instruction code="4C 03 D1" comment="add r10, rcx" />
		<Instruction code="33 C0" comment="xor eax, eax" />
		<Instruction code="49 8D 4A 08" comment="lea rcx, [r10+8]" />
		<Instruction code="8B 11" comment="mov edx, [rcx]" />
		
		<Hook value="StageGtSetUnk10ByTargetReimplemented" />
	</Patch>
	
	<!-- Part 11: patching stagesele -->
	
	<!-- Documentation of stagesele object (updated to 1.08)
		Size: 0xB10
		0-1EF: parent object (XG::Game::Menu::OggyBase)
		208-647: array of 0x22 (num ss stages) * 0x20 (size of Name object) -> names of stages
		648-A87: array of 0x22 (numm ss stages) * 0x20 (size of Name object) -> names of STAGE02 images names (for the textures "substitution" feature of iggy) Example: IMG_STAGE02_BFpln
		
		Documentation of Name object
		0 -> if length in CHARS is < 8 -> pointer to name (utf16), otherwise it is a 16 bytes buffer containing the small name
		0x10: length
		0x18: length2 (this is the checked one?)		
	-->
	
	<!-- Unlike with chars, the working plan to make stagesele recognize new stages will be very different. 
	     It will mostly based on the iggy modification calling special functions for the modded stages.
		 In this way, doing a big amount of patches can be avoided.
		 
		 We need to locate several game functions that the patcher will use to implement the native functionality provided to the special iggy.
	-->
	
	<Patch name="LocateGameLanguageVar" type="notify" search_start="0x2B1DDB" >
		<Instruction code="C7 05 XXXXXXXX 09 00 00 00" comment="mov game_language, 9" />
		<Instruction code="E9 C0 00 00 00" comment="jmp +0xC0" />
		<Instruction code="48 8D 15 XXXXXXXX" comment="lea rdx, 'chinese'" />
		<Instruction code="48 8B CB" comment="mov rcx, rbx" />
		<Instruction code="E8 XXXXXXXX" comment="call strcmp" />
		<Instruction code="85 C0" comment="test eax, eax" />
		<Instruction code="75 0F" comment="jnz +0x7F" />
		<Instruction code="C7 05 XXXXXXXX 0A 00 00 00" comment="mov game_language, 0xA" />
		
		<Notify inst_index="0" inst_offset="2" value="OnGameLanguageLocated" />
	</Patch>
	
	<Patch name="PatchGetStageName" type="hook" search_start="0x546FC0" >
		<Instruction code="4C 8B 49 38" comment="mov r9, [rcx+38h]" />
		<Instruction code="48 8B 49 40" comment="mov rcx, [rcx+40h]" />
		<Instruction code="4C 3B C9" comment="cmp r9, rcx" />
		<Instruction code="75 11" comment="jnz +0x11" />
		<Instruction code="33 C9" comment="xor eax, eax" />
		
		<Hook value="GetStageNamePatched" setup="SetupGetStageName" />
	</Patch>
	
	<Patch name="LocateGameDataSingleton" type="notify" search_start="0x505492" comment="Takes place in stagesele_func2" >
		<Instruction code="48 8B 0D XXXXXXXX" comment="mov rcx, XG::Game::GameData::singleton" />
		<Instruction code="E8 XXXXXXXX" comment="call get_selected_before_object" />
		<Instruction code="BE 24 00 00 00" comment="mov esi, 24h" />
		<Instruction code="48 89 44 24 60" comment="mov [rsp+60h], rax" />
		<Instruction code="4C 8D 35 XXXXXXXX" comment="lea r14, ssid_to_idx" />
		<Instruction code="48 8D 9F 08 02 00 00" comment="lea rbx, [rdi+208h]" />
		<Instruction code="48 8D AF 48 06 00 00" comment="lea rbp, [rdi+648h]" />
		
		<Notify inst_index="0" inst_offset="3" value="OnGameDataSingleton_Located" />
	</Patch>
	
	<!-- This patch has same signature as above one -->
	<Patch name="LocateGetSelectedBeforeObject" type="notify" search_start="0x505492" comment="Takes place in stagesele_func2" >
		<Instruction code="48 8B 0D XXXXXXXX" comment="mov rcx, XG::Game::GameData::singleton" />
		<Instruction code="E8 XXXXXXXX" comment="call get_selected_before_object" />
		<Instruction code="BE 24 00 00 00" comment="mov esi, 24h" />
		<Instruction code="48 89 44 24 60" comment="mov [rsp+60h], rax" />
		<Instruction code="4C 8D 35 XXXXXXXX" comment="lea r14, ssid_to_idx" />
		<Instruction code="48 8D 9F 08 02 00 00" comment="lea rbx, [rdi+208h]" />
		<Instruction code="48 8D AF 48 06 00 00" comment="lea rbp, [rdi+648h]" />
		
		<Notify inst_index="1" inst_offset="1" value="OnGetSelectedBeforeObject_Located" />
	</Patch>
	
	<Patch name="LocateHasStageBeenSelectedBefore" type="notify" search_start="0x505548" comment="Takes place in stagesele_func2" >
		<Instruction code="41 8B D7" comment="mov edx, r15d" />
		<Instruction code="E8 XXXXXXXX" comment="call has_stage_been_selected_before" />
		<Instruction code="8D 56 44" comment="lea edx, [rsi+44h]" />
		<Instruction code="44 8B C0" comment="mov r8d, eax" />
		<Instruction code="48 8B CF" comment="mov rcx, rdi" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		<Instruction code="45 8B CF" comment="mov r9d, r15d" />
		
		<Notify inst_index="1" inst_offset="1" value="OnHasStageBeenSelectedBefore_Located" />
	</Patch>
	
	<!-- If this breaks, we need to change STAGEGT_ADDRESS in the dll -->
	<Patch name="ProtectGatesReimplementation" type="notify" search_start="0x1D92EC" comment="signature 1.091" >
		<Instruction code="49 8D 8C 24 C8 5F 00 00" comment="lea rcx, [r12+5FC8h]" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		<Instruction code="48 8B F8" comment="mov rdi, rax" />
		<Instruction code="44 8B C3" comment="mov r8d, ebx" />
		<Instruction code="8B D6" comment="mov edx, esi" />
		<Instruction code="49 8D 8C 24 C8 5F 00 00" comment="lea rcx, [r12+5FC8h]" />
		
		<Notify value="NULL" />
	</Patch>

</EternityPatchFile>