Add option to disable most post FX

- Add file patching logic
This commit is contained in:
David Vogel 2022-07-29 11:29:14 +02:00
parent 58803cad1d
commit b4a0b26dfd
4 changed files with 43 additions and 9 deletions

View File

@ -58,7 +58,7 @@ function Check:Regular(interval)
end
-- Check if we have the required settings.
local config, magic = Modification.RequiredChanges()
local config, magic, patches = Modification.RequiredChanges()
if config["fullscreen"] then
local expected = tonumber(config["fullscreen"])
if expected ~= Coords.FullscreenMode then
@ -93,7 +93,7 @@ function Check:Regular(interval)
end
-- Request a restart if the user has changed specific mod settings.
local restartModSettings = {"disable-background", "disable-physics", "disable-postfx", "disable-shaders-gui-ai"}
local restartModSettings = {"disable-background", "disable-physics", "disable-postfx"}
for i, v in ipairs(restartModSettings) do
local settingID = "noita-mapcap." .. v
if ModSettingGetNextValue(settingID) ~= ModSettingGet(settingID) then

View File

@ -6,6 +6,12 @@
-- Noita settings/configuration modifications.
-- We try to keep persistent modifications to a minimum, but some things have to be changed in order for the mod to work correctly.
-- There are 4 ways Noita can be modified by code:
-- - `config.xml`: These are persistent, and Noita needs to be force closed when changed from inside a mod.
-- - `magic_numbers.xml`: Persistent per world, can only be applied at mod startup.
-- - Process memory: Volatile, can be modified at runtime. Needs correct memory addresses to function.
-- - File patching: Volatile, can only be applied at mod startup.
--------------------------
-- Load library modules --
--------------------------
@ -113,12 +119,28 @@ function Modification.SetMemoryOptions(memory)
end
end
---Applies patches to game files based on in the given table.
---
---Should be called on mod initialization only.
---@param patches table
function Modification.PatchFiles(patches)
-- Change constants in post_final.frag.
if patches.PostFinalConst then
local postFinal = ModTextFileGetContent("data/shaders/post_final.frag")
for k, v in pairs(patches.PostFinalConst) do
postFinal = postFinal:gsub(string.format("const bool %s%%s+=[^;]+;", k), string.format("const bool %s = %s;", k, tostring(v)))
end
ModTextFileSetContent("data/shaders/post_final.frag", postFinal)
end
end
---Returns tables with user requested game configuration changes.
---@return table config -- List of `config.xml` attributes that should be changed.
---@return table magic -- List of `magic_number.xml` attributes that should be changed.
---@return table memory -- List of options in RAM of this process that should be changed.
---@return table patches -- List of patches that should be applied to game files.
function Modification.RequiredChanges()
local config, magic, memory = {}, {}, {}
local config, magic, memory, patches = {}, {}, {}, {}
-- Does the user request a custom resolution?
local customResolution = (ModSettingGet("noita-mapcap.custom-resolution-live") and ModSettingGet("noita-mapcap.capture-mode") == "live")
@ -154,13 +176,24 @@ function Modification.RequiredChanges()
magic["DEBUG_PAUSE_BOX2D"] = ModSettingGet("noita-mapcap.disable-physics") and "1" or "0"
magic["DEBUG_DISABLE_POSTFX_DITHERING"] = ModSettingGet("noita-mapcap.disable-postfx") and "1" or "0"
if ModSettingGet("noita-mapcap.disable-postfx") then
patches.PostFinalConst = {
ENABLE_REFRACTION = false,
ENABLE_LIGHTING = false,
ENABLE_FOG_OF_WAR = false,
ENABLE_GLOW = false,
ENABLE_GAMMA_CORRECTION = false,
ENABLE_PATH_DEBUG = false,
}
end
if ModSettingGet("noita-mapcap.disable-shaders-gui-ai") then
memory["mPostFxDisabled"] = 1
memory["mGuiDisabled"] = 1
memory["mFreezeAI"] = 1
end
return config, magic, memory
return config, magic, memory, patches
end
---Sets the camera free if required by the mod settings.

View File

@ -56,9 +56,10 @@ dofile("mods/noita-mapcap/files/ui.lua")
---Called in order upon loading a new(?) game.
function OnModPreInit()
-- Set magic numbers and other stuff based on mod settings.
local config, magic, memory = Modification.RequiredChanges()
local config, magic, memory, patches = Modification.RequiredChanges()
Modification.SetMagicNumbers(magic)
Modification.SetMemoryOptions(memory)
Modification.PatchFiles(patches)
-- Override virtual resolution and some other stuff.
--ModMagicNumbersFileAdd("mods/noita-mapcap/files/magic-numbers/1024.xml")
@ -99,7 +100,7 @@ end
---Called *every* time the game is about to start updating the world.
function OnWorldPreUpdate()
Message:CatchException("OnWorldPreUpdate", function ()
Message:CatchException("OnWorldPreUpdate", function()
-- Coroutines aren't run every frame in this lua sandbox, do it manually here.
wake_up_waiting_threads(1)
@ -109,7 +110,7 @@ end
---Called *every* time the game has finished updating the world.
function OnWorldPostUpdate()
Message:CatchException("OnWorldPostUpdate", function ()
Message:CatchException("OnWorldPostUpdate", function()
-- Reload mod every 60 frames.
-- This allows live updates to the mod while Noita is running.
-- !!! DISABLE THIS LINE AND THE CORRESPONDING REQUIRE BEFORE COMMITTING !!!
@ -144,7 +145,7 @@ end
function OnPausedChanged(isPaused, isInventoryPause)
-- Set some stuff based on mod settings.
-- Normally this would be in `OnModSettingsChanged`, but that doesn't seem to be called.
local config, magic, memory = Modification.RequiredChanges()
local config, magic, memory, patches = Modification.RequiredChanges()
Modification.SetMemoryOptions(memory)
end

View File

@ -280,7 +280,7 @@ modSettings = {
{
id = "disable-postfx",
ui_name = " Disable post FX",
ui_description = "Will disable the following postprocessing:\n- Dithering",
ui_description = "Will disable the following postprocessing:\n- Dithering\n- Refraction\n- Lighting\n- Fog of war\n- Glow\n- Gamma correction",
value_default = DebugAPI.IsDevBuild(), -- Defaults to true in dev build, false in regular Noita.
scope = MOD_SETTING_SCOPE_RUNTIME_RESTART,
},