<?xml version="1.0" encoding="utf-8" ?>
<EternityPatchFile name="MiscPatches" comment="This file contains miscelaneous patches" >	

	<!-- All addreses are 1.08 unless stated otherwise -->		
	
	<Patch name="PatchGameVersionString" type="hook" search_start="0x51406A" >
		<Instruction code="4C 8D 87 68 03 00 00" comment="lea r8, [rdi+368h]; comment=version_string" />
		<Instruction code="48 8B CF" comment="mov rcx, rdi" />
		<Instruction code="8D 55 04" comment="lea edx, [rbp+4]" />
		<Instruction code="E8 XXXXXXXX" comment="call send_method" />
		<Instruction code="8B 87 1C 02 00 00" comment="mov eax, [rdi+21Ch]" />
		
		<Hook type="call" inst_index="3" value="PatchGameVersionString" setup="SetupPatchGameVersionString" />
	</Patch>
	
	<Patch name="Pollution, go away!" enabled="excessive_air_contamination" type="write" search_start="0x2AF6D4" >
		<Instruction code="48 8B 0D XXXXXXXX" comment="mov rcx, contaminator_singleton" />
		<Instruction code="48 8D 15 XXXXXXXX" comment="lea rdx, string" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		<Instruction code="33 C0" comment="xor eax, eax" />
		<Instruction code="C7 43 18 02 00 00 00" comment="mov dword ptr [rbx+18h], 2" />
		<Instruction code="48 83 C4 20" comment="add rsp, 20h" />
		<Instruction code="5B" comment="pop rbx" />
		<Instruction code="C3" comment="ret" />
		
		<Write inst_index="4" inst_offset="3" value="01" />
	</Patch>
	
	<Patch name="Pollution, go away2!" type="write" enabled="excessive_air_contamination" search_start="0x2B0154" >
		<Instruction code="0F 85 B6 01 00 00" comment="jne +0x1B6" />
		<Instruction code="4C 8B C2" comment="mov r8, rdx" />
		<Instruction code="48 8D 15 XXXXXXXX" comment="lea rdx, string" />
		<Instruction code="48 8B 0D XXXXXXXX" comment="mov rcx, contaminator_singleton" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		<Instruction code="48 8B D8" comment="mov rbx, rax" />
		
		<Write value="E9 B6 01 00 00   90" />
	</Patch>	
	
	<Patch name="UnlockNXExclusive" type="hook" enabled="XV1CpkExists()" search_start="0x2D9FB0" comment="address 1.09" >
		<Instruction code="40 53" comment="push rbx" />
		<Instruction code="48 83 EC 20" comment="sub rsp, 20h" />
		<Instruction code="83 F9 1D" comment="cmp ecx, 1D" />
		<Instruction code="7C 0B" comment="jl +0xB" />
		<Instruction code="B8 01 00 00 00" comment="mov eax, 1" />
		<Instruction code="48 83 C4 20" comment="add rsp, 20h" />
		
		<Hook value="Function73" setup="SetupFunction73" />	
	</Patch>
	
	<Patch name="UnlockCheck138" type="hook" search_start="0x2DC830" comment="addr 1.09.1" >
		<Instruction code="8B D1" comment="mov edx, ecx" />
		<Instruction code="83 F9 1A" comment="cmp ecx, 1Ah" />
		<Instruction code="72 06" comment="jb +06" />
		<Instruction code="B8 01 00 00 00" comment="mov eax, 1" />
		<Instruction code="C3" comment="ret" />
		<Instruction code="48 63 05 XXXXXXXX" comment="movsxd rax, XXXXXXXX" />
		<Instruction code="83 F8 0C" comment="cmp eax, 0Ch" />
		<Instruction code="0F 87 AA 00 00 00" comment="ja +AA" />
		
		<Hook value="Function138" setup="SetupFunction138" />	
	</Patch>
	
	<!-- Address 1.09 -->
	<Patch name="UnlimitedItemInfo" type="hook" search_start="0x5910F0" comment="Takes place in get_item_information" >
		<Instruction code="48 89 5C 24 08" comment="mov [rsp+8], rbx" />
		<Instruction code="57" comment="push rdi" />
		<Instruction code="48 83 EC 20" comment="sub rsp, 20h" />
		<Instruction code="48 63 C2" comment="movsxd rax, edx" />
		<Instruction code="48 8B D9" comment="mov rbx, rcx" />
		<Instruction code="48 8D 0D XXXXXXXX" comment="lea rcx, XXXXXXXX" />
		<Instruction code="8B 3C C1" comment="mov edi, [rcx+rax*8]" />
		
		<Hook value="GetItemInfoPatched" setup="SetupGetItemInfo" />
	</Patch>
	
	<!-- Address 1.09 -->
	<Patch name="GetUnknownItemAddress" type="notify" search_start="0x59111A" comment="Takes place in get_item_information" >
		<Instruction code="8B D7" comment="mov edx, edi" />
		<Instruction code="48 8B CB" comment="mov rcx, rbx" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		<Instruction code="48 8B 5C 24 30" comment="mov rbx, [rsp+30h]" />
		<Instruction code="48 8B C8" comment="mov rcx, rax" />
		<Instruction code="48 85 C9" comment="test rcx, rcx" />
		<Instruction code="48 8D 05 XXXXXXXX" comment="lea rax, 'Unknown Item Information'" />
		<Instruction code="48 0F 45 C1" comment="cmovnz rax, rcx" />
		
		<Notify inst_index="6" inst_offset="3" value="OnUnknownItemAddressLocated" />
	</Patch>
	
	<Patch name="LocateGameLanguageVar_Item" 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_Item" />
	</Patch>
	
	<!-- Signature 1.09.1 -->
	<Patch name="PatchUntransformCutScenes" type="hook" enabled="dont_untransform_cut_scenes" search_start="0x1F749A" >
		<Instruction code="BA 01 00 00 00" comment="mov edx, 1" />
		<Instruction code="48 8B CF" comment="mov rcx, rdi" />
		<Instruction code="E8 XXXXXXXX" comment="call Mob_Untransform" />
		<Instruction code="48 8B 87 18 11 00 00" comment="mov rax, [rdi+1118h]" />
		<Instruction code="89 A8 B0 00 00 00" comment="mov [rax+0B0h], ebp" />
		<Instruction code="FF C6" comment="inc esi" />
		<Instruction code="48 83 C3 08" comment="add rbx, 8" />
		<Instruction code="48 81 FB 00 37 00 00" comment="cmp rbx, 3700" />
		
		<Hook type="call" inst_index="2" value="PatchUntransform" setup="SetupUntransform" />
	</Patch>
	
	<!-- Signature 1.09 -->
	<Patch name="PatchUntransformCutScenes2" type="hook" enabled="dont_untransform_cut_scenes" search_start="0x15B188" >
		<Instruction code="BA 01 00 00 00" comment="mov edx, 1" />
		<Instruction code="48 8B CF" comment="mov rcx, rdi" />
		<Instruction code="E8 XXXXXXXX" comment="call Mob_Untransform" />
		<Instruction code="48 8B CF" comment="mov rcx, rdi" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		<Instruction code="48 8B 0D XXXXXXXX" comment="mov rcx, XXXXXXXX" />
		<Instruction code="48 8D 97 A0 09 00 00" comment="lea rdx, [rdi+9A0h]" />
		<Instruction code="41 83 C9 FF" comment="or r9d, 0FFFFFFFFh" />
		<Instruction code="45 8D 41 06" comment="lea r8d, [r9+6]" />
		
		<Hook type="call" inst_index="2" value="PatchUntransform" />
	</Patch>
	
	<!-- Signature 1.09.1 -->
	<Patch name="MultiSelectExpert_CS_Ctor" type="hook" enabled="enable_multiselect_expert" search_start="0x3BFA1A" >
		<Instruction code="44 8B C6" comment="mov r8d, esi" />
		<Instruction code="41 8B D4" comment="mov edx, r12d" />
		<Instruction code="49 8B CA" comment="mov rcx, r10" />
		<Instruction code="E8 XXXXXXXX" comment="call chasel_ctor" />
		<Instruction code="EB 03" comment="jmp +3" />
		<Instruction code="49 8B C6" comment="mov rax, r14" />
		<Instruction code="49 89 47 18" comment="mov [r15+18h], rax" />
		
		<Hook type="call" inst_index="3" value="ChaselCtorHooked" setup="SetupChaselCtor" />
	</Patch>	
	
	<!-- Signature 1.09.1 -->
	<Patch name="MultiSelectExpert_Locate_NumAllies" type="notify" enabled="enable_multiselect_expert" search_start="0x3BFEEC" >
		<Instruction code="BE 02 00 00 00" comment="mov esi, 2; num allies" />
		<Instruction code="48 89 75 A0" comment="mov [rbp-60h], esi" />
		<Instruction code="8B D6" comment="mov edx, esi" />
		<Instruction code="48 8B 0D XXXXXXXX" comment="mov rcx, XG::Game::Menu::BattleSpecManager::singleton" />
		<Instruction code="E8 XXXXXXXX" />
		<Instruction code="89 45 94" comment="mov [rbp-6Ch], eax" />
		
		<Notify inst_index="0" inst_offset="1" value="OnNumAlliesLocated" />		
	</Patch>
	
	<!-- The below patch is MANDATORY for the whole patcher, as the patcher may have other uses for the main core singleton -->
	<!-- DON'T DELETE/DISABLE the patch below.
	
	<!-- when/if this patch gets broken, the cockpit parameter of the structure may have changed -->
	<Patch name="LocateBattleCoreMainSystem_singleton" type="notify" search_start="0x6C2BB2" comment="addr 1.09" >
		<Instruction code="48 8B 05 XXXXXXXX" comment="mov rax, XG::Game::Battle::Core::MainSystem::singleton" />
		<Instruction code="48 8B 98 90 3F 00 00" comment="mov rbx, singleton->cockpit" />
		<Instruction code="48 85 DB" comment="test rbx, rbx" />
		<Instruction code="74 1F" comment="jz +0x1F" />
		
		<Notify inst_index="0" inst_offset="3" value="SetupBattleCoreMainSystem_singleton" />
	</Patch>
	
	<!-- This patch is used by more than one functionality -->
	<!-- This will also be called from derived class for ohzaru mob -->
	<Patch name="PatchMobDtor" type="hook" search_start="0x14B410" comment="signature based on 1.09.1" >
		<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], -2" />
		<Instruction code="48 89 5C 24 40" comment="mov [rsp+40h], rbx" />
		<Instruction code="48 8B F9" comment="mob rdi, rcx" />
		<Instruction code="48 8D 05 XXXXXXXX" comment="lea rax, XXXXXXXX" />
		<Instruction code="48 89 01" comment="mov [rcx], rax" />
		<Instruction code="E8 XXXXXXXX" comment="call XXXXXXXX" />
		<Instruction code="90" comment="nop" />
		<Instruction code="48 8D 8F F0 2F 00 00" comment="lea rcx, [rdi+2FF0h]" />
		
		<Hook value="MobDtorPatched" setup="SetupMobDtor" />
	</Patch>

</EternityPatchFile>
