From f2e582622e25b4959e8df4d1dad017faabdd4c90 Mon Sep 17 00:00:00 2001 From: David Vogel Date: Sat, 23 Jul 2022 22:36:14 +0200 Subject: [PATCH] Add LuaNXML library - Change VSCode lua addon to not ignore submodules - Add function to read resolutions from Noita config file --- .gitmodules | 3 +++ .vscode/settings.json | 3 ++- README.md | 4 ++++ files/libraries/coordinates.lua | 20 ++++++++++++++++++++ files/libraries/luanxml | 1 + init.lua | 10 ++++++++++ 6 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 160000 files/libraries/luanxml diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..3977915 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "files/libraries/luanxml"] + path = files/libraries/luanxml + url = https://github.com/zatherz/luanxml diff --git a/.vscode/settings.json b/.vscode/settings.json index 4c8b7f8..9cc708f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -35,5 +35,6 @@ "Lua.runtime.version": "LuaJIT", "Lua.format.defaultConfig": { "max_line_length": "512" - } + }, + "Lua.workspace.ignoreSubmodules": false } \ No newline at end of file diff --git a/README.md b/README.md index f1464eb..7f11081 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,10 @@ The rectangles for the different capture modes are defined in `.../Noita/mods/no As the resulting stitched image is really big, you can read [this comment](https://github.com/Dadido3/noita-mapcap/issues/7#issuecomment-723591552) that addresses how you can view, convert or even self-host your images. +## Acknowledgements + +This addon uses the [LuaNXML](https://github.com/zatherz/luanxml) library by [Zatherz](https://github.com/zatherz). + ## License [MIT](LICENSE) diff --git a/files/libraries/coordinates.lua b/files/libraries/coordinates.lua index e17d78e..159c810 100644 --- a/files/libraries/coordinates.lua +++ b/files/libraries/coordinates.lua @@ -10,6 +10,8 @@ -------------------------- local CameraAPI = require("noita-api.camera") +local NXML = require("luanxml.nxml") +local Utils = require("noita-api.utils") local Vec2 = require("noita-api.vec2") ---------- @@ -26,6 +28,24 @@ local Coords = { VirtualResolution = Vec2(0, 0), } +---Reads and updates the internal, window and virtual resolutions from Noita's config files and API. +---@return any error +function Coords:ReadResolutions() + local filename = Utils.GetSpecialDirectory("save-shared") .. "config.xml" + + local f, err = io.open(filename, "r") + if not f then return err end + + local xml = NXML.parse(f:read("*a")) + + self.WindowResolution = Vec2(tonumber(xml.attr["window_w"]), tonumber(xml.attr["window_h"])) + self.InternalResolution = Vec2(tonumber(xml.attr["internal_size_w"]), tonumber(xml.attr["internal_size_h"])) + self.VirtualResolution = Vec2(tonumber(MagicNumbersGetValue("VIRTUAL_RESOLUTION_X")), tonumber(MagicNumbersGetValue("VIRTUAL_RESOLUTION_Y"))) + + f:close() + return nil +end + ---Returns the size of the internal rectangle in window/screen coordinates. ---The internal rect is always uniformly scaled to fit inside the window rectangle. ---@return Vec2 diff --git a/files/libraries/luanxml b/files/libraries/luanxml new file mode 160000 index 0000000..03d2890 --- /dev/null +++ b/files/libraries/luanxml @@ -0,0 +1 @@ +Subproject commit 03d28907ccced296e5b2f8b16303a312ab4eaa3b diff --git a/init.lua b/init.lua index 1ebf62a..8317619 100644 --- a/init.lua +++ b/init.lua @@ -16,6 +16,12 @@ if not async then require("coroutines") -- Loads Noita's coroutines library from `data/scripts/lib/coroutines.lua`. end +-------------------------- +-- Load library modules -- +-------------------------- + +local Coords = require("coordinates") + ------------------------------- -- Load and run script files -- ------------------------------- @@ -23,6 +29,7 @@ end dofile("mods/noita-mapcap/files/external.lua") dofile("mods/noita-mapcap/files/capture.lua") dofile("mods/noita-mapcap/files/ui.lua") +--dofile("mods/noita-mapcap/files/blablabla.lua") -------------------- -- Hook callbacks -- @@ -78,6 +85,9 @@ end ---The last point where the Mod API is available. ---After this materials.xml will be loaded. function OnMagicNumbersAndWorldSeedInitialized() + -- Get resolutions for correct coordinate transformations. + -- This needs to be done once all magic numbers are set. + Coords:ReadResolutions() end ---Called when the game is paused or unpaused.