PSP Classics Configuration Files (Official)

From PS4 Developer wiki
Jump to navigation Jump to search

Configuration files extracted from official packages to improve PSP emulator compatibility on PS4.

Syphon Filter: Dark Mirror[edit | edit source]

config-title.txt
UCUS98641

# SyphonFilterDarkMirror (all regions)

--texclutmode=filter

--bend-30hz-lock=1

--psp-right-stick-action=1
--psp-right-stick-deadzone-x=15
--psp-right-stick-deadzone-y=15
--psp-right-stick-deadzone-semicircle-arc=40

--app-volume=0.8


# following settings are machine-generated
--region-dir=SIEA
--ps4-trophies=1
--ps5-uds=1
--trophies=1

--globalgamedata-dir=global

UCUS98641_patches.lua

-- Lua 5.3
-- Title: Syphon Filter: Dark Mirror

-- Patches for fixing issues with post fx in games using the Syphon Filter/Resistance engine

apiRequest(1.0)	-- request version 1.0 API. Calling apiRequest() is mandatory.

local emuObj = getEmuObject()
local cpu = getAXObject() 

-- The bloom filter uses 1.5 pixel jitter to make a ghetto blur. When up-ressed, this doesn't look good.
-- To compensate, reduce the jitter amount before rendering.
function BloomJitterPlusAdjust()
	-- 1.5 pixel "+" pattern
	emuObj.AdjustUVJitter(0, 4, 0.0, 1.5)
	emuObj.AdjustUVJitter(4, 4, 0.0, -1.5)
	emuObj.AdjustUVJitter(8, 4, 1.5, 0.0)
	emuObj.AdjustUVJitter(12, 4, -1.5, 0.0)
end

function BloomJitterCrossAdjust()
	-- 1.5 pixel "x" pattern
	emuObj.AdjustUVJitter(0, 4, 1.5, 1.5)
	emuObj.AdjustUVJitter(4, 4, -1.5, 1.5)
	emuObj.AdjustUVJitter(8, 4, 1.5, -1.5)
	emuObj.AdjustUVJitter(12, 4, -1.5, -1.5)
end

local jitterFixPlus = emuObj.AddGPUHook(0x40dc000, 0, 16, 0x40d4000, BloomJitterPlusAdjust)
local jitterFixCross = emuObj.AddGPUHook(0x40d4000, 0, 16, 0x40dc000, BloomJitterCrossAdjust)

function depthquery()
	local querystruct = 0x8eda968		-- this is always in a fixed location
	-- the depth query struct is fixed to 24 entries
	-- 0..15 seem to be always used for lights, 16..23 are for npcs
	for i=0,23 do
		local querytype = cpu.ReadMem32(querystruct+0)
		local valid = cpu.ReadMem32(querystruct+1*4)
		if valid ~= 0xffffffff then
			--local queryx = cpu.ReadMem32(querystruct+2*4)
			--local queryy = cpu.ReadMem32(querystruct+3*4)
			--local querydepth = cpu.ReadMem32(querystruct+4*4)
			--local querywidth = cpu.ReadMem32(querystruct+5*4)
			--local queryheight = cpu.ReadMem32(querystruct+6*4)
			--local param1c = cpu.ReadMem32(querystruct+7*4)
			--local param20 = cpu.ReadMem32(querystruct+8*4)
			local param24 = cpu.ReadMem32(querystruct+9*4)
			--local result = cpu.ReadMemFloat(querystruct+10*4)
			if param24 <= 0 then
				cpu.WriteMem32(querystruct+1*4, 0xffffffff)		-- to be removed from the list -> mark as invalid
			else
				if querytype == 3 then		-- n x n coverage based occlusion used by lights. Object opacity is set to (visible_samples / total_samples)
					cpu.WriteMemFloat(querystruct+10*4, 0.0)	-- always hide lights
				else						-- Enemies use a 5-point (corners + center) occlusion check. Object is visible if any of the points is visible.
					cpu.WriteMemFloat(querystruct+10*4, 1.0)	-- set always visible
				end
			end
		end
		querystruct = querystruct + 0x2c
	end
end

cpu.AddHook(0x8c163d4, 0x27bdff80, depthquery)			-- = addiu sp, sp, -0x80
-- the hook code replaces the occlusion function, so just adjust the stack and return
cpu.InsnReplace(0x8c163d8, 0x34040000, 0x27bd0080)		-- addiu sp, sp, 0x80
cpu.InsnReplace(0x8c163dc, 0xafa40048, 0x03e00008)		-- jr ra
cpu.InsnReplace(0x8c163e0, 0x3c0408ee, 0x00000000)		-- nop

Echochrome[edit | edit source]

config-title.txt
UCES01011

# Echochrome (all regions)

# following settings are machine-generated
--region-dir=SIEA
--ps4-trophies=0
--ps5-uds=0
--trophies=0

config-region.txt

--image="data/USER_L0.IMG"
--antialias=SSAA4x
--multisaves=true
--notrophies=true

NPUG80135_patches.lua

-- Lua 5.3
-- Title: Echochrome  NPUG 80135  (US)

--  WBD  5/10/2022	prevent Console Msg about Memory Stick from being displayed
--  WBD  5/22/2022  remove Send and Receive menu options

apiRequest(1.0)	-- request version 1.0 API. Calling apiRequest() is mandatory.

local gpr	= require( "ax-gpr-alias" ) -- you can access Allegrex GPR by alias (gpr.a0 / gpr["a0"])
local emuObj = getEmuObject()
local cpu = getAXObject()

local hook = 0x88468f0

function SkipMsg()
	local v1 = cpu.GetGpr(gpr.v1)
	local v0 = cpu.GetGpr(gpr.v0)
	
	cpu.SetGpr(gpr.a0, v1)
	cpu.SetGpr(gpr.v0, 0)
	cpu.WriteMem32(v1+4, v0)
	cpu.SetPC(hook+0xc)
	print "_NOTE: Skipping Memory Stick Message"
end
cpu.AddHook(hook, 0x00602021, SkipMsg)


function DisableSendReceive()
	cpu.WriteMem32(0x8909940, 0)		-- overwrite Send with 0
	cpu.WriteMem32(0x890991c, 0)		-- overwrite Receive with 0
end
cpu.AddHook(0x8846fb4, 0xae130090, DisableSendReceive)

LocoRoco Midnight Carnival[edit | edit source]

config-title.txt
NPEG00024

# LocorocoMidnightCarnival (all regions)

--ps5-uds=0# following settings are machine-generated
--region-dir=SIEE
--ps4-trophies=0
--ps5-uds=0
--trophies=0

config-region.txt

; Windows configuration file for PSPHD

; Game Image
--image="data\USER_L0.IMG"

; Use this to run the automatic font dumper.
;--boot="host0:../tools/glyphdump.prx"

; Uncomment these to play around with font dumping.
;--fontsave="host0:fontdump"
;--fontreplace="host0:fontreplace"

; Scaling factor (Windows only - 1: 480x272, 2: 960x544, 3: 1440x816, 4: 1920x1088 ... 8: 3840x2176)
--scale=3

; Language selection (Windows only - the PS4 version auto-selects based on the PS4 language setting)
; Switch the PSP language to one of the following:
; "en": English (default)
; "jp": Japanese
; "fr": French
; "es": Spanish
; "de": German
; "it": Italian
; "nl": Dutch
; "pt": Portuguese
; "ru": Russian
; "ko": Korean
; "chs": Chinese Simplified
; "cht": Chinese Traditional
;--lang="en"

; Redirect host0: to a another directory (uncomment to enable).
; By default it's mapped to the working directory where the executable starts
;--host="D:\Sony\PSPHD"

; To dump the original video in PMF format, uncomment the --dumpvideos line
; For example, with the current setting, it will save them in a "D:\Sony\PSPHD\videos" directory (make sure the directory exists)
;--dumpvideos="host0:videos"

; To dump the original audio in Atrac3 format, uncomment the --dumpaudio line
; For example, with the current setting, it will save them in a "D:\Sony\PSPHD\audio" directory (make sure the directory exists)
;--dumpaudio="host0:audio"

; To save the in-game textures as the game runs, uncomment the --texsave line
; For example, with the current setting, it will save them in a "D:\Sony\PSPHD\texdump" directory (make sure the directory exists)
;--texsave="host0:texdump"
;--texmissingsave="host0:texdump"

; To replace specific textures as the game runs, uncomment the --texreplace line
; For example, with the current setting, it will load them from the "D:\Sony\PSPHD\texreplace" directory
; If the --scale setting is set to 4 or 8, the program will also look for replacement textures on '4x' or '8x' directories within this directory
;--texreplace="host0:texreplace"

; This forces alpha blending to on for replaced textures. (uncomment to enable)
; With this we can freely make use of a normal alpha channel on replacement textures
;--replacementalpha=true

; This enables bilinear filtering on replaced textures. (uncomment to enable)
;--replacementfilter=true

; Antialiasing mode. SSAA4x looks best, MSAA4x only smooths edges.
; Choices: off, SSAA4x, MSAA4x
--antialias=SSAA4x

; Allows to switch between replacement and original textures using L3 on the DS4 or the T key.
;--texswitch=true

; Turns on the auto-resampler. Assumes textures in texreplace are at 8x resolution and resamples them at load
;--autoresampler=true

; Turns on filtering of CLUT hashes to avoid repeat indexed textures
;--texclutmode=filter

; Enable Multiple Savegames
--multisaves=true

; Custom locoroco2 texture hasher
;--texcachemode="locoroco2"

NPEG00024_patches.lua

-- Lua 5.3
-- Title: Locoroco Midnight Carnival NPEG-00024  (EU)

--  WBD  5/25/2022	prevent RANKING option from being selected

apiRequest(1.0)	-- request version 1.0 API. Calling apiRequest() is mandatory.

local gpr	= require( "ax-gpr-alias" ) -- you can access Allegrex GPR by alias (gpr.a0 / gpr["a0"])
local emuObj = getEmuObject()
local cpu = getAXObject()


function DisableRanking()
	local a2 = cpu.GetGpr(gpr.a2)
	local a1 = cpu.GetGpr(gpr.a1)
	local check = cpu.ReadMem8(a1+0x2c)			-- make sure we're in the correct menu
	local ptr = a1+a2+0x34
	local new_indx = cpu.ReadMem8(ptr)
	if (new_indx == 2) then
		if (a2 == 2 and check == 0x2b) then		-- moving right
			cpu.WriteMem8(ptr, 3)
			print "_NOTE: NEW changed to 3"

		elseif (a2 == 3 and check == 0x3e) then		-- moving left
			cpu.WriteMem8(ptr, 1)
			print "_NOTE: NEW changed to 1"
		end
	end
end

cpu.AddHook(0x8a205c4, 0x00c58821, DisableRanking)

Patapon 2[edit | edit source]

LUA
UCUS98732

-- Lua 5.3
-- Title:   Patapon 2 PSP - UCUS-98732 (USA)
-- Author:  Ernesto Corvi

-- Changelog:
-- v1.1: US only.  Change the word "Paraget" to "Patagate" in two strings.

apiRequest(1.0)	-- request version 1.0 API. Calling apiRequest() is mandatory.

local gpr		= require( "ax-gpr-alias" ) -- you can access Allegrex GPR by alias (gpr.a0 / gpr["a0"])
local emuObj	= getEmuObject() -- emulator
local axObj		= getAXObject() -- allegrex
local pad		= require("pad")

-- Hook memcpy to catch framebuffer effects
axFuncReplace(0x881D194, "patapon_memcpy")

-- Accelerate some functions
axFuncReplace(0x8804670, "__ptmf_scall")
axFuncReplace(0x8892230, "Renderer__makeWorldMatrix__4psysFv")
axFuncReplace(0x88209A8, "patapon_strcmp")
axFuncReplace(0x8815130, "sceGupSetStatus")
axFuncReplace(0x8851D84, "FMtx44__setRow__3PydFv")
axFuncReplace(0x88437F0, "Script__Talk__Controller__setDeltaTime__3PydFv")
axFuncReplace(0x8843B6C, "Script__Talk__Controller__setCmdId__3PydFv")
axFuncReplace(0x88441DC, "Script__Talk__Controller__getArgValuePtr__3PydFv_0")
axFuncReplace(0x89B25B0, "sceGmoCol4Multiply")
axFuncReplace(0x8910A60, "Gfx__CacheModel__getNodeMtx__6SystemFv")
axFuncReplace(0x8911440, "Gfx__StaticGmoModel__getNodeIdx__6SystemFv")
axFuncReplace(0x89114C8, "Gfx__StaticGmoModel__getNodeMtx__6SystemFv")
axFuncReplace(0x891157C, "Gfx__StaticGmoModel__getNodeMtx__6SystemFv_0")
axFuncReplace(0x881462C, "sceGuEnable", 0x8B3C050) -- param = gupbase pointer
axFuncReplace(0x8814684, "sceGuDisable", 0x8B3C050) -- param = gupbase pointer
axFuncReplace(0x89B2D84, "sceGmoFCurveEval_fastpath", 0x89B2D88) -- param = function entry if not fast path
--axFuncReplace(0x88D0788, "Game__Map__Weather__WindLocus__update__4LaboFv_loop", 0x88D0864) -- param = continue address

axFuncReplace(0x890FCBC, "convertLowerString__6SystemFv")
axFuncReplace(0x8877104, "convertLowerString__6SystemFv") -- convertLowerString__21@unnamed@BNDFile_cpp@Fv, same functionality
axFuncReplace(0x897927C, "__extendsfdf2")
axFuncReplace(0x8979AF8, "__muldf3")
axFuncReplace(0x8979A8C, "__adddf3")
axFuncReplace(0x897A750, "__truncdfsf2")
axFuncReplace(0x88CBD64, "RemapPacket", 0x8ACC394) -- param comes from RemapPacketAddr24 & RemapPacketAddr16
axFuncReplace(0x890FEA0, "patapon_strcmp") -- strcmpFast__6SystemFv ('fast' strcmp)


-- Switch Select command to "Options" button.
emuObj.PadSetButtonsMode(pad.BUTTONS_MODE_OPTION_IS_SELECT)

-- Remove "Transfer Patapon Saved Data" option from New Game option
--axInsnReplace(0x8A46660, 0x24070002, 0x24070001) -- li  a3,0x2  (change 2 menu options to 1)
emuObj.RemapSavedata("UCUS98711", "CUSA06171", "504e802b04a1838c32b616abbe0b475fbea1c823825ef0df06cc2bad129ce2f7")

-- Fix "Tree of Life" unaligned lines and shadows
axObj.AddHook(0x884600C, 0x8c850010, function() -- PSP::Gfx::PrimitiveContext::setVertex2f
	local context = axObj.GetGpr(gpr.a0)
	local cmdCount = axObj.ReadMem32(context + 0x14)
	if (cmdCount & 1) == 1 then -- we have at least 2 commands
		local cmdBuffer = axObj.ReadMem32(context + 0x10)
		local cx = axObj.GetFpr(12)
		local cy = axObj.GetFpr(13)
		local ox = axObj.ReadMemFloat(cmdBuffer-12)
		local oy = axObj.ReadMemFloat(cmdBuffer-8)
		
		if cx == ox then
			axObj.SetFpr(12, cx + 0.5)
			axObj.WriteMemFloat(cmdBuffer-12, ox + 0.5)
		elseif cy == oy then
			axObj.SetFpr(13, cy + 0.5)
			axObj.WriteMemFloat(cmdBuffer-8, oy + 0.5)
		end
	end
end)

-- LANGUAGES --
local langCode = "us" -- default
--local languageList = {"Japanese","English","UK","Italian","German","Spanish","French","Korean","Chinese","ns","nf","Dutch","Portuguese","gr","Belgium"}
--local gameLanguageCode = {"jp","us","uk","it","de","sp","fr","kr","cn","ns","nf","nl","pt","gr","be"}
local languageList = {"","English","","","","","","","","Spanish","French","","","",""}
local gameLanguageCode = {"","us","","","","","","","","ns","nf","","","",""}

-- US: english = 1, spanish = 9, french = 0xA (from Localize__Manager__setDefaultLangage)
-- EU: uk = 2, italian = 3, german = 4, spanish = 5, french = 6
-- JP: jp = 0
-- HP: ko = 7, ch = 8, 
	
-- GLOBAL FUNCTIONS --

local GLOBAL = 0x8B7D088 -- reawakeDeathUnit - offset 0x88A95E8 in $v1

function getGlobalAddr() -- gets Global to user data.
	-- address comes from: Labo::Bases::Camp::Controller::reawakeDeathUnit - offset 0x88A95E8 in $v1
	-- add 0x20 to address to align it with the word "none"
	
	local main = axObj.ReadMem32(GLOBAL)				-- fixed Global address from: 0x88A95E8 -> 0x8c9fbc0
	if main == 0xFFFFFFFF or main == 0 then
		return 0xFFFFFFFF
	end
	local ptr = axObj.ReadMem32(main + 0x10c0)			--> sets to: 0x8d07840
	if ptr == 0xFFFFFFFF or ptr == 0 then
		return 0xFFFFFFFF
	end

	return ptr
end

-- MAIN FUNCTIONS --

local H1 = function() -- Labo::Bases::Camp::Scene::updateTips
	local v0 = axObj.GetGpr(gpr.v0)
	if v0 > 0 then
		emuObj.ThrottleMax()
	else
		emuObj.ThrottleNormal()
	end
end

local H2 = function() -- GameSystem__SaveDataController__setStateMessage
	local ptr = axObj.GetGpr(gpr.a2)
	local str = axObj.ReadMemStr16(ptr)
	local newStr = "Now loading. Do not turn off the power."
	-- english: Now loading./Do not remove the Memory Stick Duo™ /or turn off the system power.
	-- spanish: Cargando./No extraigas el Memory Stick Duo™ /ni apagues el sistema.
	-- french: Chargement./Ne pas retirer le Memory Stick Duo™ /ou éteindre le système.
	-- italian: Caricamento in corso.../Non rimuovere il Memory Stick Duo™ /e non spegnere il sistema.
	-- german: Lädt./Bitte den Memory Stick Duo™ nicht entfernen /und das System nicht ausschalten.
	-- japanese: システムデータをロード中です。/Memory Stick Duo™を抜き挿ししたり、/電源を切ったりしないでください。
	-- chinese: 系統資料載入中。/請勿插拔Memory Stick Duo™/或是關閉主機的電源。
	-- korean: 시스템 데이터를 불러오는 중입니다./Memory Stick Duo™를 빼거나/전원을 끄지 마십시오.
	
	if string.find(str, "Now loading") then
		newStr = "Now loading."										-- English: Now loading./Do not remove the Memory Stick Duo™ /or turn off the system power.
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "Checking Memory Stick") then
		newStr = "Please wait."										-- English: Checking Memory Stick Duo™.
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "Now saving")then
		newStr = "Now saving."										-- English: Now saving./Do not remove the Memory Stick Duo™ /or turn off the system power.
		axObj.WriteMemStr16Z(ptr, newStr)
--
	elseif string.find(str, "Cargando.") then
		newStr = "Cargando..."										-- Spanish: Cargando./No extraigas el Memory Stick Duo™ /ni apagues el sistema.
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "Guardando...")then
		newStr = "Espera..."										-- Spanish: Guardando.../No extraigas el Memory Stick Duo™ /ni apagues el sistema.
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "Comprobando el Memory Stick") then
		newStr = "Guardando..."										-- Spanish: Comprobando el Memory Stick Duo™.
		axObj.WriteMemStr16Z(ptr, newStr)
--
	elseif string.find(str, "Chargement.") then
		newStr = "Chargement."										-- French: Chargement./Ne pas retirer le Memory Stick Duo™ /ou éteindre le système.
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "Vérification du Memory Stick")then
		newStr = "Veuillez patienter."								-- French: Vérification du Memory Stick Duo™.
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "Sauvegarde.") then
		newStr = "Sauvegarde en cours."								-- French: Sauvegarde./Ne pas retirer le Memory Stick Duo™ /ou éteindre le système.
		axObj.WriteMemStr16Z(ptr, newStr)
--
	elseif string.find(str, "Caricamento in corso") then
		newStr = "Caricamento in corso."							-- Italian: Caricamento in corso.../Non rimuovere il Memory Stick Duo™ /e non spegnere il sistema.
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "Controllo del Memory Stick")then
		newStr = "Attendi."											-- Italian: Controllo del Memory Stick Duo™.
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "Salvataggio in corso") then
		newStr = "Salvataggio in corso."							-- Italian: Salvataggio in corso.../Non rimuovere il Memory Stick Duo™ /e non spegnere il sistema.
		axObj.WriteMemStr16Z(ptr, newStr)
--
	elseif string.find(str, "Lädt") then
		newStr = "Lädt."											-- German: Lädt./Bitte den Memory Stick Duo™ nicht entfernen /und das System nicht ausschalten.
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "Memory Stick Duo™ wird überprüft")then
		newStr = "Bitte warten."									-- German: Memory Stick Duo™ wird überprüft.
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "Speichert.") then
		newStr = "Speichert."										-- German: Speichert./Bitte den Memory Stick Duo™ nicht entfernen und /das System nicht ausschalten.
		axObj.WriteMemStr16Z(ptr, newStr)
--
	elseif string.find(str, "システムデータをロード中です。") then
		newStr = "ロード中。"											-- Japanese: システムデータをロード中です。/Memory Stick Duo™を抜き挿ししたり、/電源を切ったりしないでください。
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "Memory Stick Duo™のチェック中です。")then
		newStr = "お待ちください。"										-- Japanese: Memory Stick Duo™のチェック中です。
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "システムデータをセーブ中です。") then
		newStr = "セーブ中。"											-- Japanese: システムデータをセーブ中です。/Memory Stick Duo™を抜き挿ししたり、/電源を切ったりしないでください。
		axObj.WriteMemStr16Z(ptr, newStr)
--	
	elseif string.find(str, "系統資料載入中。") then
		newStr = "載入中。"											-- Chinese: 系統資料載入中。/請勿插拔Memory Stick Duo™/或是關閉主機的電源。
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "Memory Stick Duo™確認中。")then
		newStr = "請稍候。"											-- Chinese: Memory Stick Duo™確認中。
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "保存中。") then
		newStr = "存檔中。"											-- Chinese: 保存中。/請勿插拔Memory Stick Duo™/或是關閉主機的電源。
		axObj.WriteMemStr16Z(ptr, newStr)
--
	elseif string.find(str, "시스템 데이터를 불러오는 중입니다") then
		newStr = "불러오는 중입니다."										-- Korean: 시스템 데이터를 불러오는 중입니다./Memory Stick Duo™를 빼거나/전원을 끄지 마십시오.
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "Memory Stick Duo™ 체크 중입니다")then
		newStr = "잠시 기다려 주십시오."									-- Korean: Memory Stick Duo™ 체크 중입니다.
		axObj.WriteMemStr16Z(ptr, newStr)
	elseif string.find(str, "시스템 데이터를 저장 중입니다") then
		newStr = "저장 중입니다."										-- Korean: 시스템 데이터를 저장 중입니다./Memory Stick Duo™를 빼거나,/전원을 끄지 마십시오.
		axObj.WriteMemStr16Z(ptr, newStr)
	end
end

local H3 = function()	-- Localize__Manager__getLanguageName
	local v0 = axObj.GetGpr(gpr.v0)
	local langPtr = axObj.ReadMem32(v0)
	langCode = axObj.ReadMemStr(langPtr)
end

local bundleFileName = ""
local H4 = function() -- System::Util__BNDFile__doNameCallback
	local ptr = axObj.GetGpr(gpr.a0)
	bundleFileName = axObj.ReadMemStr(ptr)
end

local H5 = function() -- Labo::GameSystem__callbackFunc_MemoryObject
	local ptr = axObj.GetGpr(gpr.s5)
	local dataPtr = axObj.ReadMem32(ptr)
	
	if bundleFileName == "colony_data.pac" then
		axObj.WriteMem16(dataPtr + 0x79ba, 4)
	end
end

-- Black Smith - Remove "KeyGuide" bug when hit anvil head while quenching --
local blackSmithRunning = false
local H6 = function() -- Sound__SubGame__Blacksmith__Command__start__4LaboFv
	blackSmithRunning = true
end
local H7 = function() -- Sound__BeatCommander__endSubGame__4LaboFv
	if blackSmithRunning == true then
		local a0 = axObj.GetGpr(gpr.a0)
		axObj.WriteMem8(a0 + 0x19c, 1)	-- set memory to 1 (instead of 0) otherwise brings up bad "KeyGuide"
	end
	
	blackSmithRunning = false
end

-- TIPS System hacks --
axInsnReplace(0x8A73A44, 0x3C0342A8, 0x3C0342A4) -- change required number of Tips from 84 to 82.
local H8 = function() -- GameSystem__Tips__Viewer__setup__4LaboFv
	local global = getGlobalAddr()
	if global == 0xFFFFFFFF then
		return
	end

	local unitTable = axObj.ReadMem32(global + 0x1068)	--> sets to: 0x8d13580 (unit table)
	local tipsPtr = unitTable + 0x1F5E4
	
	local badTips = axObj.ReadMem32(tipsPtr + 8)
	badTips = badTips & 0xFFBBFFFF		-- remove tips 0x53 and 0x57 as they referred to Multiplayer
	axObj.WriteMem32(tipsPtr + 8, badTips)
end

-- Remove AutoSave menu from initial startup --
local H9 = function() -- GameSystem__SaveDataController__update__4LaboFv
	local s3 = axObj.GetGpr(gpr.s3)
	local flg = axObj.ReadMem32(s3 + 0x7eed)
	local val = axObj.ReadMem32(s3 + 0x7efc)
	
	if flg == 0 and val == 0x14 then
		axObj.WriteMem32(s3 + 0x7efc, 0x13)
	end
end

-- Change game save name to "Patapon 2 Remastered" --
local H10 = function() -- Utility__SaveDataUtility__setDataParamsfoString
	local ptr = axObj.GetGpr(gpr.s1)
	local strPtr = axObj.ReadMem32(ptr)
	local newStr = "PATAPON™ 2 Remastered"
	
	axObj.WriteMemStrZ(strPtr, newStr)
end

-- Change description for the "Friendship" weapon to remove game sharing text --
local	itemsTable = {}
		itemsTable[1] = "Proof of friendship establish"			-- English
		itemsTable[2] = "Prueba de la amistad establecida"		-- Spanish
		itemsTable[3] = "Preuve de l'amitié établie"			-- French
		itemsTable[4] = "Preuve d'une amitié"					-- French alt
		
function changeStrings(strPtr) -- modify strings
	local str = axObj.ReadMemStr16(strPtr) -- ptr to the string
	local strFind = string.gsub(str, "/", " ")	-- change any slash to space to strings can match.
	strFind = string.gsub(strFind, "  ", " ")	-- change any double spaces due to above change to only 1 space.
	
	for i = 1, #itemsTable do
		local startPos, endPos = string.find(strFind, itemsTable[i])
	
		if startPos ~= nil then
			str = string.sub(str, 1, (startPos-1) )
			-- SPECIAL fixes --
			str = string.gsub(str, "Un oiseau avec de bons HP et /une grosse attaque.","Un oiseau qui augmente vos/HP.") -- French incorrect grammar (bird)
			str = string.gsub(str, "aumente","augmente") -- French incorrect grammar.

			axObj.WriteMemStr16Z(strPtr, str)
		end
	end
end
		
local H11 = function() -- from Bases__Item__ExplanationWindow__setItemId
	local ptr = axObj.GetGpr(gpr.a0)
	local a2 = axObj.GetGpr(gpr.a2)
	local valPtr = axObj.ReadMem32(ptr)
	local v1 = valPtr + 8
	local v0 = a2 * 4
	v0 = v0 + v1
	local strPtr = axObj.ReadMem32(v0)
	strPtr = strPtr + valPtr

	changeStrings(strPtr)
end

-- Skip Memory message when selecting "Delete Save Data" on "Save the adventure" dialogue.
local menuSkip = false
local H12 = function()		-- Bases__Talk__CommandCamp__showDialog
	menuSkip = true
end
local H13 = function()		-- Bases__Talk__CommandCamp__showDialog
	if menuSkip == true then
		axObj.SetGpr(gpr.v1, 0)
		menuSkip = false
	end
end
local H14 = function()		-- Bases__Talk__CommandCamp__showDialog
	if menuSkip == true then
		axObj.SetPC(0x88D908C)
	end
end


-- HOOKS --
local hook1  = axObj.AddHook(0x88E9564, 0x0002102B, H1)		-- Labo::Bases__Camp__Scene__updateTips
local hook2  = axObj.AddHook(0x899E718, 0x00403021, H2)		-- GameSystem__SaveDataController__setStateMessage
local hook3  = axObj.AddHook(0x897771C, 0x00431021, H3)		-- Localize__Manager__getLanguageName
local hook4  = axObj.AddHook(0x88774D0, 0x00402821, H4)		-- System::Util__BNDFile__doNameCallback
local hook5  = axObj.AddHook(0x886889C, 0x02002021, H5)		-- Labo::GameSystem__callbackFunc_MemoryObject
local hook6  = axObj.AddHook(0x898BCDC, 0x27BDFFE0, H6)		-- Sound__SubGame__Blacksmith__Command__start__4LaboFv
local hook7  = axObj.AddHook(0x89220F8, 0xAC80019C, H7)		-- Sound__BeatCommander__endSubGame__4LaboFv
local hook8  = axObj.AddHook(0x8A734DC, 0x27BDFFC0, H8)		-- GameSystem__Tips__Viewer__setup__4LaboFv
local hook9  = axObj.AddHook(0x899CF4C, 0x92637EED, H9)		-- GameSystem__SaveDataController__update__4LaboFv
local hook10 = axObj.AddHook(0x88E7E48, 0x2490009C, H10)	-- Utility__SaveDataUtility__setDataParamsfoString
local hook11 = axObj.AddHook(0x897CF90, 0x26041718, H11)	-- Bases__Item__ExplanationWindow__setItemId
local hook12 = axObj.AddHook(0x88D9114, 0xAEA00014, H12)	-- Bases__Talk__CommandCamp__showDialog
local hook13 = axObj.AddHook(0x88D90B4, 0x9063C550, H13)	-- Bases__Talk__CommandCamp__showDialog
local hook14 = axObj.AddHook(0x88D907C, 0x8FA2003C, H14)	-- Bases__Talk__CommandCamp__showDialog
local hook15 = axObj.AddHook(0x8A394B4, 0x26241718, H11)	-- Bases__Organization__Managed__ItemSelectWindow__onSlotChange (note uses H11)


-- US game bug fix.  "Paraget" -> "Patagate" --
axObj.AddHook(0x88DC5F4, 0x00409021, function() -- from Bases__Camp__Person__Behavior__Idle__update__4LaboFv
	local strPtr = axObj.GetGpr(gpr.v0) + 0xE	-- 0xE skip over X/Y positions.
	local str = axObj.ReadMemStr16(strPtr)
	
	if string.find(str, "The Paraget will") then
		-- org string: The Paraget will teleport you to/another world…
		local newStr = "The Patagate can teleport you to/another world…"
		axObj.WriteMemStr16Z(strPtr, newStr)
	elseif string.find(str, "Paraget Shrine") then
		-- org string: This is the Paraget Shrine./Only <N0> and <N1> can/enter.
		local newStr = "This is the Patagate Shrine/Only <N0> and <N1> can/enter."
		axObj.WriteMemStr16Z(strPtr, newStr)
	end
end)

--[[
axObj.AddHook(0x08982F48, 0x24070001, function()
	if emuObj.NeoMode() then
		axObj.SetGpr(gpr.a3, 0) -- turn on 60fps mode
	end
end)
]]--

--- MOVIE FIX ---
local movieLastTime = 0.0
local movieFix = function() -- Labo::GameSystem__MovieModule__render
	local t = axObj.GetFpr(0)
	if t == 0 and movieLastTime > 0 then
		local s0 = axObj.GetGpr(gpr.s0)
		axObj.WriteMemFloat(s0 + 0x44, movieLastTime)
	elseif t > 0 then
		movieLastTime = t
	end
end

axObj.AddHook(0x8A6A1DC, 0xe6000044, movieFix)


--- PAT2-16 FIX ---
axObj.AddHook(0x890E9E0, 0x02002021, function() -- Labo::Game__Gimmick__WindowAttacher__update
	local ptr = axObj.GetGpr(gpr.sp) + 0x28
	local posy = axObj.ReadMemFloat(ptr + 4)
	
	if posy < -30 then
		local obj = axObj.GetGpr(gpr.a0)
		axObj.WriteMem8(obj + 0x2a8, 0)
	end
end)

Resistance: Retribution[edit | edit source]

LUA
UCUS98668

-- Lua 5.3
-- Title: Resistance: Retribution

-- Patches for fixing issues with post fx in games using the Syphon Filter/Resistance engine
-- Oct 28 2022 - Disabling offensive voice line (Ali Burakaz)

apiRequest(1.0)	-- request version 1.0 API. Calling apiRequest() is mandatory.

local gpr	= require( "ax-gpr-alias" ) -- you can access Allegrex GPR by alias (gpr.a0 / gpr["a0"])
local emuObj = getEmuObject()
local cpu = getAXObject()



function Resistance_HookBloom(module, bloomFuncOffset, downscaleFuncOffset, sliceDrawFuncOffset, dispListAddr)
	-- the game does downscaling in 4 pieces
	-- patch the downscale piecing into a single draw call
	AX_AddHook(module, downscaleFuncOffset + 0x80, 0x4600a406, function()
		-- first pass: 256x256, 224x256, 256x16, 224x16
		AX_SetFpr(14, 480.0)
		AX_SetFpr(15, 272.0)
	end)
	AX_AddHook(module, downscaleFuncOffset + 0xac, 0x4600a406, function()
		-- second pass: 128x128, 112x128, 128x8, 112x8
		AX_SetFpr(14, 240.0)
		AX_SetFpr(15, 136.0)
	end)
	AX_AddHook(module, downscaleFuncOffset + 0xc0, 0x3c0443f0, function()
		-- skip the rest of the pieces
		AX_SetPC(AX_GetPC() + 0xf8)
	end)

	-- skip jitters
	AX_AddHook(module, bloomFuncOffset + 0x378, 0x00002825, function()
		AX_SetPC(AX_GetPC() + 0xc)
	end)
	AX_AddHook(module, bloomFuncOffset + 0x394, 0x4600f306, function()
		AX_SetPC(AX_GetPC() + 0xc)
	end)
	AX_AddHook(module, bloomFuncOffset + 0x3ac, 0x4600f306, function()
		AX_SetPC(AX_GetPC() + 0xc)
	end)

	AX_AddHook(module, bloomFuncOffset + 0x344, 0x8e040050, function()
		local listPtr = AX_ReadMem32(dispListAddr)

		local blur_op = GE_AddCommandHook(function()
			GE_ApplyGaussianBlur(0x40dc000, 0, 0, 120, 68, 1.15)
			GE_ApplyGaussianBlur(0x40dc000, 0, 0, 120, 68, 1.15)
			GE_ApplyGaussianBlur(0x40dc000, 0, 0, 120, 68, 1.15)
		end)
	
		AX_WriteMem32(listPtr, blur_op)
		listPtr = listPtr + 4
		AX_WriteMem32(dispListAddr, listPtr)
	end)

	-- final blend 0x40dc000 instead of 0x40d4000 (temp FB usage changes since we skipped the jitter passes)	
	AX_AddHook(module, bloomFuncOffset + 0x3b8, 0x8fc50058, function()		
		AX_SetGpr(gpr.a0, 0x40dc000)
		AX_SetPC(AX_GetPC() + 0xc)
	end)

	-- replace the 64 pixel slicing with 480 pixels, effectively disabling it
	AX_InsnReplace(module, sliceDrawFuncOffset + 0x88, 0x3c044280, 0x3c0443f0)		-- lui a0,0x4280 -> lui a0,0x43f0 (64.0f -> 480.0f)
end

Resistance_HookBloom("disc0:/PSP_GAME/USRDIR/RS.PRX", 0x4951a0, 0x4944d8, 0x493b14, 0x8defc64)

--[[

-- Modes:
--  * DISABLE: Prevent the bloom entirely, which makes things more washed out.
--  * ADJUST: Adjust the blurring jitter to rendered pixels, keeping better colors.
BloomMode = "ADJUST"

-- The bloom filter uses 1.5 pixel jitter to make a ghetto blur. When up-ressed, this doesn't look good.
-- To compensate, reduce the jitter amount before rendering.
function BloomJitterAdjust()
	-- Reduce the jitter scale value from 0.5 by VideoScale.  Currently in f12.
	cpu.SetFpr(12, 0.5 / emuObj.VideoScale())
end

-- A function is used for 2D rectangle blitting, and copies columns of 64 pixels.
-- This was an optimization on the PSP, but only slows us down and introduces UV clamp issues.
-- This function forces it to allow 512 wide columns (the widest you can sample from a texture.)
function BloomUVClampAdjust()
	-- At this point, a0 has been set to 64.0, replace with 512.0.
	cpu.SetGpr(gpr.a0, 0x44000000)
end

-- This func draws the screen with a UV offset to blur more, which looks bad at high enough res.
function BloomBlendAdjust()
	-- Normally, u0=1, u1=257.  We reduce this a lot to look better on uprendering.
	cpu.SetFpr(12, 0.25 / emuObj.VideoScale())
	cpu.SetFpr(14, 256.0 + 0.25 / emuObj.VideoScale())
end

function BloomDisable()
	if BloomMode == "DISABLE" then
		-- This just immediately returns from the function that writes the bloom displaylist data.
		cpu.SetPC(cpu.GetPC() + 0x08CA9210 - 0x08CA8BA0);
	end
end

cpu.AddHook("Resistance", 0x08CA8F0C - 0x08813A00, 0x44846000, BloomJitterAdjust)
cpu.AddHook("Resistance", 0x08CA759C - 0x08813A00, 0x3C044280, BloomUVClampAdjust)
cpu.AddHook("Resistance", 0x08CA7B00 - 0x08813A00, 0x46007C86, BloomBlendAdjust)
cpu.AddHook("Resistance", 0x08CA8BA0 - 0x08813A00, 0x27BDFFA0, BloomDisable)
]]--

AllowDepthRead = 0
LastDepthReadIrqCount = 0

-- These are the beginning and end of the function calling sceGeListSync.
-- It uses interrupts to trigger depth reads.
-- We miss objects if we do our once-per-frame depth read outside this function.
function OnListSyncStart()
	AllowDepthRead = 1
end
function OnListSyncEnd()
	LastDepthReadIrqCount = AllowDepthRead
	AllowDepthRead = 0
end

-- This hooks the entry into the function that performs depth reads.
-- It's called outside the ListSync too, but that is before all objects are drawn.
-- Reading depth every time this is called would be expensive, we want to do once per frame.
-- See Construction Zone: Mech Ride.
function OnDepthListReader()
	-- We want to pick up the last depth read in a frame, or else we get wrong data.
	-- However, different areas have different counts.
	local target = LastDepthReadIrqCount - 1
	if target == -1 then
		target = 1
	end

	if AllowDepthRead == target then
		-- Find the relocated global offset.
		local offset = cpu.GetPC() - 0x08C82158;
		-- Okay, now the global containing the depthbuffer address is at...
		local depthAddr = 0x08E080C8 + offset
		local t4 = cpu.ReadMem32(depthAddr)
		emuObj.ApplyDepthReadback(t4, 512, 512, 272, 2)
	end
	if AllowDepthRead >= 1 then
		AllowDepthRead = AllowDepthRead + 1
	end
end

cpu.AddHook("Resistance", 0x08B345B8 - 0x08817A00, 0x27BDFFF0, OnListSyncStart)
cpu.AddHook("Resistance", 0x08B34644 - 0x08817A00, 0x8FBF000C, OnListSyncEnd)
cpu.AddHook("Resistance", 0x08C82158 - 0x08817A00, 0x27BDFFA0, OnDepthListReader)

-- This is an unfortunate patch for a fade-in issue for some cutscenes.
-- They're fading in too early, which allows you to see strange animations that you shouldn't.
-- We delay the fade here, although it's not clear what should actually be delaying the fade.
-- Multiple Bonn missions have this problem.
function DelayCinematicFadeIn()
	-- Normally, this is set to 0 or 12 frames, we only change 12.
	local delayAddr = cpu.GetGpr(gpr.s0) + 0x52
	if cpu.ReadMem16(delayAddr) == 12 then
		cpu.WriteMem16(delayAddr, 24)
	end
end

cpu.AddHook("Resistance", 0x08864170 - 0x08817A00, 0xA200006C, DelayCinematicFadeIn)

-- No longer supported in main.
--cpu.FuncReplace(0x8d05d20, "lr2_memset", 3)

-- Prevent Multiplayer from being selected from Main Menu
function SelectDown()
	local v0 = cpu.GetGpr(gpr.v0)
	if (v0 == 0x9cc0fb0) then
		print "_NOTE: Skipping Multiplayer on DOWN selection"
		cpu.SetGpr(gpr.v0, 0x9cc11e0)
		cpu.SetGpr(gpr.a0, 0x9cc11e0)
	end
end

function SelectUp()
	local v0 = cpu.GetGpr(gpr.v0)
	if (v0 == 0x9cc0fb0) then
		print "_NOTE: Skipping Multiplayer on UP selection"
		cpu.SetGpr(gpr.v0, 0x9cc0d80)
		cpu.SetGpr(gpr.a2, 0x9cc0d80)
	end
end


cpu.AddHook(0x8c917c0, 0x00402025, SelectDown)
cpu.AddHook(0x8c91914, 0x00403025, SelectUp)


--Remove offensive voice line

local i_read_line = 0x08D38A40
local op_read_line = 0x80c40000


function disableLine()
	
	voice_id = cpu.ReadMemStr(cpu.GetGpr(gpr.a2))
	
	if voice_id == "10307" then
		print("Offensive line detected. Disabling...")
		--Overwrite line ID with garbage
		cpu.WriteMem32(cpu.GetGpr(gpr.a2) + 0x20, 0xFFFF0000)
	end

end

cpu.AddHook(i_read_line, op_read_line, disableLine)


--Removing multiplayer option from menu

local id_campaign = "bracketbutton01" --3
local id_multiplayer = "bracketbutton06" --8
local id_options = "bracketbutton03"--5

local id_main_menu = 0xF

local i_set_menu = 0x8c82184 - 0x8813a00
local op_set_menu = 0x8ca50004

function disable_multiplayer()
	local mem_menu_cursor = cpu.GetGpr(gpr.s1) + 0x2C

	local new_menu_option = cpu.GetGpr(gpr.s0)
	local old_menu_option = cpu.ReadMem32(mem_menu_cursor)
	
	local id_old = cpu.ReadMem32(old_menu_option + 0x18)
	local id_new = cpu.ReadMem32(new_menu_option + 0x18)
	local id_menu = cpu.ReadMem32(new_menu_option + 0xC)
	
	
	--print(string.format("Menu ID %x", id_menu))
	--print(string.format("New ID %x", id_new))
	--print(string.format("Old ID %x", id_old))
	--print(string.format("New pointer %x", new_menu_ptr))
	--print(string.format("Old pointer %x", old_menu_ptr))
	--for i = 0, 0x10, 1 do
	--	print(string.format("%x", i*4))
	--	print(string.format("found new %x",cpu.ReadMem32(new_menu_ptr + i*4)))
	--	print(string.format("found old %x",cpu.ReadMem32(old_menu_ptr + i*4)))
	--end
	
	new_menu_ptr = cpu.ReadMem32(new_menu_option + 4)
	old_menu_ptr = cpu.ReadMem32(old_menu_option + 4)
	
	if new_menu_ptr < 0x8800000 or old_menu_ptr < 0x8800000 then
		print("NULL MENU STRING ERROR, ABORTING")
		return
	end
	
	new_button_string =  cpu.ReadMemStr(new_menu_ptr)
	old_button_string =  cpu.ReadMemStr(old_menu_ptr)
	
	
	print("New", cpu.ReadMemStr(new_menu_ptr))
	print("Old", cpu.ReadMemStr(old_menu_ptr))
	
	
	if new_button_string ~= id_multiplayer or id_menu ~= id_main_menu then
		--Not in entering multiplayer
		return
	end
	
	if old_button_string == id_options then
		print("_NOTE: Skipping Multiplayer on UP selection")
		cpu.WriteMem32(mem_menu_cursor, new_menu_option  - 0x230)
		cpu.SetGpr(gpr.s0, new_menu_option - 0x230)
	elseif old_button_string == id_campaign then
		print("_NOTE: Skipping Multiplayer on DOWN selection")
		cpu.WriteMem32(mem_menu_cursor, new_menu_option  + 0x230)
		cpu.SetGpr(gpr.s0, new_menu_option + 0x230)
	end
end

local i_read_string = 0x8d61000 - 0x8813a00
local op_read_string = 0x80850000

function erase_multiplayer_string()
	local string_ = cpu.ReadMemStr(cpu.GetGpr(gpr.a0))
	
	--print(string_)
	
	if 	string_ == "MULTIPLAYER" or 
		string_ == "MULTIJUGADOR" or 
		string_ == "MULTIJOUEUR" or 
		string_ == "MEHRSPIELER" or
		string_ == "MULTIGIOCATORE"
		then
		--Terminate string
		cpu.WriteMem8(cpu.GetGpr(gpr.a0), 0x0)
	end

end

cpu.AddHook("Resistance", i_read_string, op_read_string, erase_multiplayer_string)
cpu.AddHook("Resistance", i_set_menu, op_set_menu, disable_multiplayer)

-- Called when intel is taken by the player.
function OnIntel()
	-- Used to remove autoaim intel.
	
	-- Intel is a string added to a list which seems to be located near or on the stack somehow.
	-- This list includes various in-game options, settings, and the intel the player has.
	-- A series of functions is called to add intel to inventory (starting with 0883D780, ending
	-- with 08D0678C), this hooks into the second of these functions, and nulls out the start
	-- of the string. Not sure if this copies a null string to inventory, or just cancels the
	-- whole operation (seems to cancel the operation), but it works either way.
	-- Aim assist intel is written to BF691E4 on SIEA
	
	print("====")
	print("OnIntel")
	
	-- String is held in A0, get it.
	local s = cpu.ReadMemStr(cpu.GetGpr(gpr.a0))
	
	-- Print for debug.
	print("String: ", s)
	
	-- Is it the right intel? If so, null out first char.
	if s == "INTEL_4_0_0" then
		print("Aim assist detected.")
		
		-- Set first character to NULL.
		cpu.WriteMem8(cpu.GetGpr(gpr.a0), 0)
	end
	
	print("====")
	
end

cpu.AddHook("Resistance", 0x289BC, 0x00E04025, OnIntel)

function OnIntelShow()

	print("====")
	print("OnIntelShow")
	
	-- Get the current intel number, held at an odd offset from S0.
	local intel1 = cpu.ReadMem32(cpu.GetGpr(gpr.s0) + 0x54)
	local intel2 = cpu.ReadMem32(cpu.GetGpr(gpr.s0) + 0x5C)
	local intel3 = cpu.ReadMem32(cpu.GetGpr(gpr.s0) + 0x60)
	
	-- Print for debug.
	print("Intel number: ", intel1, intel2, intel3)

	-- Is it the aim assist intel? If so, skip it.
	if intel1 == 4 and intel2 == 0 and intel3 == 0 then
	
		print("Aim assist intel detected.")
		print("Skipping")
	
		-- Skip past getting the intel entirely, including all text displays about it,
		-- and actually recieving it.
		cpu.SetPC(cpu.GetPC() + 0x178); -- 0x08835118
	
	end
	
	print("====")
	
end

cpu.AddHook("Resistance", 0x215A0, 0x82640000, OnIntelShow) -- 0x08834FA0

-- Called when opening the options menu.
function OnOptionMenu()
	-- Used to remove option from menu.
	
	print("====")
	print("OnOptionMenu")
	
	-- Get base address of widget array.
	local base = cpu.ReadMem32(cpu.GetGpr(gpr.a0) + 0x4)
	
	-- This string helps us know if we're on the main menu.
	local s = cpu.ReadMemStr(base)
	
	-- This points into the widget list. The list is not an array, its items are
	-- dynamically allocated, and seem to be in a doubly linked list of some kind.
	-- Possibly a hierarchy, but it's an odd structure regardless. Anyway, this points in, but
	-- to an odd location.
	local origin = cpu.ReadMem32(cpu.GetGpr(gpr.a0) + 0x88)
	
	-- Print for debug.
	print("Base: ", string.format("%x", base))
	print("Base string: ", s)
	print("Origin pointer: ", string.format("%x", origin))
	
	-- +0x34 has a pointer to the first location in the double linked list we need.
	local address = cpu.ReadMem32(origin + 0x34)
	
	-- Address now contains the base address of the AIM ASSIST string.
	print("String address: ", string.format("%x", address))
	
	-- Zero out first letter of AIM ASSIST string to prevent rendering.
	cpu.WriteMem8(address + 0x120, 0)
	
	-- Now, we need to progress through the linked list 4 times forward, to reach
	-- the address of the text and background for the on/off switch.
	
	address = cpu.ReadMem32(address + 0x4)
	print("address step 1: ", string.format("%x", address))
	
	address = cpu.ReadMem32(address + 0x4)
	print("address step 2: ", string.format("%x", address))
	
	address = cpu.ReadMem32(address + 0x4)
	print("address step 3: ", string.format("%x", address))
	
	address = cpu.ReadMem32(address + 0x4)
	print("address step 4: ", string.format("%x", address))
	
	-- Address now contains the base address to the switch.

	-- Make the "on/off" text go away by possibly setting font size to 0.
	cpu.WriteMem16(address + 0xDE, 0)
	
	-- Make background of on/off button go away by setting its position to offscreen.
	cpu.WriteMem16(address + 0xDE + 0xB0, 0)
	
	print("====")
	
end
-- 088D7730
cpu.AddHook("Resistance", 0xC3D2C, 0xAFBF003C, OnOptionMenu) -- 0x088D772C


local isCinematic = false

-- detect the use of cinematic camera
function UpdateCinemaCam()
	isCinematic = true
end
AX_AddHook("disc0:/PSP_GAME/USRDIR/RS.PRX", 0x885f8e0-0x8813a00, 0x27bdfff0, UpdateCinemaCam)

-- depth readback latency can cause glitches when used with a moving camera and rapid angle changes (e.g. cinematics)
-- force NPCs to always have visibility during cinematics to counter this
function PatchDepthQuery()
	local query_ptr = AX_GetGpr(gpr.a0)
	local type = AX_ReadMem32(query_ptr)
	if isCinematic and type == 1 then
		AX_SetFpr(0, 1.0)
	end
end
AX_AddHook("disc0:/PSP_GAME/USRDIR/RS.PRX", 0x8c7da3c-0x8813a00, 0xc480002c, PatchDepthQuery)

EM_AddVsyncHook(function() isCinematic = false end)


-- Cap the number of particle groups being drawn
-- The game uses CPU time measurements to decide how many particles to draw.
-- If the CPU is running too fast, it can lead to display list buffer overflows and subsequent crashes.
-- Killing two or more Titans at the same time is a consistent way to trigger this issue.
local numParticleGroups = 0
function DrawParticleGroups()
	if numParticleGroups >= 100 then
		AX_SetGpr(gpr.sp, AX_GetGpr(gpr.sp) + 0x180)
		AX_SetPC(AX_GetGpr(gpr.ra))
	else
		numParticleGroups = numParticleGroups + 1
	end
end
AX_AddHook("Resistance", 0x8ccf690-0x8813a00, 0x27bdfe80, DrawParticleGroups)


local dlbase = 0
function EndFrame()
	local dlptr = AX_ReadMem32(0x8defc64) & 0xfffffff

	local dl1base = AX_ReadMem32(0x8defc54)
	local dl1end = AX_ReadMem32(0x8defc68)
	local dl2base = AX_ReadMem32(0x8defc58)
	local dl2end = AX_ReadMem32(0x8defc6c)

	if (dlbase == dl1base and dlptr > dl1end) or (dlbase == dl2base and dlptr > dl2end) then
		print(string.format("Display list overflow detected (base = %08X, dlptr = %08X)", dlbase, dlptr))
	end

	numParticleGroups = 0
end
AX_AddHook("Resistance", 0x8c76db8-0x8813a00, 0x27bdfff0, EndFrame)

function StartFrame()
	dlbase = AX_GetGpr(gpr.a0) & 0xfffffff
end
AX_AddHook("Resistance", 0x8b30580-0x8813a00, 0x00a42025, StartFrame)