Skip to content

Commit

Permalink
Fixed inheritance. Reworked plenty of files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ismoh committed Sep 19, 2023
1 parent f5c6932 commit be985eb
Show file tree
Hide file tree
Showing 10 changed files with 1,042 additions and 929 deletions.
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ function ExampleClass:yetAnotherPublicFunction(param)
end

---Constructor of the class. This is mandatory!
---@param objectToInheritFrom ExampleClass This can be any object that you want to inherit from.
---@param objectOfExampleClass ExampleClass
---@return ExampleClass
function ExampleClass:new(objectToInheritFromOrObjectItself, customProfiler, importClass, foo, bar)
local exampleObject = objectToInheritFromOrObjectItself or self or {} -- Use self if this is called as a class constructor
function ExampleClass:new(objectOfExampleClass, customProfiler, importClass, foo, bar)
objectOfExampleClass = objectOfExampleClass or self or {} -- Use self if this is called as a class constructor
setmetatable(exampleObject, self)
self.__index = self

Expand All @@ -130,7 +130,7 @@ function ExampleClass:new(objectToInheritFromOrObjectItself, customProfiler, imp
self.bar = bar or require("bar"):new()

customProfiler:stop("ExampleClass:new", cpc)
return exampleObject
return objectOfExampleClass
end

---Still need to return the class table at the end of the file,
Expand Down
58 changes: 49 additions & 9 deletions mods/noita-mp/files/scripts/Gui.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
---Everything regarding ImGui: Credits to @dextercd
--- @class Gui
local Gui = {}
local Gui = {
--[[ Imports ]]

---@type Client
client = nil,
---@type CustomProfiler
customProfiler = nil,
---@type GuidUtils
guidUtils = nil,
---@type ImGui
imGui = nil,
---@type MinaUtils
minaUtils = nil,
---@type NoitaMpSettings
noitaMpSettings = nil,

--[[ Attributes ]]


}

if not load_imgui then
function OnWorldInitialized()
Expand All @@ -14,14 +33,6 @@ if not load_imgui then
error("Missing ImGui.", 2)
end

local imGui = load_imgui({ version = "1.11.0", mod = "noita-mp" })

local CustomProfiler = require("CustomProfiler")
local NoitaMpSettings = require("NoitaMpSettings")
local MinaUtils = require("MinaUtils")
local GuidUtils = require("GuidUtils")
local Client = require("Client")

--- Can't know the width before creating the window.. Just an initial value, it's updated to the real value once we can call imgui.GetWindowWidth()
local menuBarWidth = 100
local function getMenuBarPosition(position)
Expand Down Expand Up @@ -710,4 +721,33 @@ function Gui.new()
return self
end

---Gui constructor.
---@param guiObject Gui|nil optional
---@param client Client required
---@param customProfiler CustomProfiler required
---@param guidUtils GuidUtils|nil optional
---@param minaUtils MinaUtils|nil optional
---@param noitaMpSettings NoitaMpSettings|nil optional
---@return Gui
function Gui:new(guiObject, client, customProfiler, guidUtils, minaUtils, noitaMpSettings)
guiObject = guiObject or self or {} -- Use self if this is called as a class constructor
setmetatable(guiObject, self)
self.__index = self

local cpc = customProfiler:start("ExampleClass:new")

-- Initialize all imports to avoid recursive imports
self.client = client or error("Client is required!", 2)
self.noitaMpSettings = noitaMpSettings or require("NoitaMpSettings")
:new(nil, customProfiler, self, nil, nil, nil, nil, nil, nil)
self.customProfiler = customProfiler or require("CustomProfiler")
:new(nil, nil, noitaMpSettings, nil, nil, nil, nil)
self.guidUtils = guidUtils or require("GuidUtils")--:new()
self.imGui = load_imgui({ version = "1.11.0", mod = "noita-mp" })
self.minaUtils = minaUtils or require("MinaUtils"):new()

customProfiler:stop("ExampleClass:new", cpc)
return guiObject
end

return Gui
63 changes: 42 additions & 21 deletions mods/noita-mp/files/scripts/NoitaMpSettings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ local NoitaMpSettings = {

---@type CustomProfiler
customProfiler = nil,
---@type guiI
guiI = nil,
---@type Gui
gui = nil,
---@type FileUtils
fileUtils = nil,
---@type json
Expand Down Expand Up @@ -150,7 +150,7 @@ function NoitaMpSettings:get(key, dataType)
return convertToDataType(self, "", dataType)
end
self.customProfiler:stop("NoitaMpSettings.get", cpc)
return convertToDataType(self, cachedSettings[key], dataType)
return convertToDataType(self, self.cachedSettings[key], dataType)
end

---Loads the settings from the settings file and put those into the cached settings.
Expand All @@ -173,39 +173,60 @@ function NoitaMpSettings:save()
end

self.fileUtils.WriteFile(settingsFilePath, self.json.encode(self.cachedSettings))
if self.guiI then
self.guiI.setShowSettingsSaved(true)
if self.gui then
self.gui.setShowSettingsSaved(true)
end
end

---NoitaMpSettings constructor.
---@param noitaMpSettingsObject NoitaMpSettings|nil
---@param noitaMpSettings NoitaMpSettings|nil
---@param customProfiler CustomProfiler|nil
---@param guiI guiI required
---@param gui Gui required
---@param fileUtils FileUtils|nil
---@param json json|nil
---@param lfs LuaFileSystem|nil
---@param logger Logger|nil
---@param utils Utils|nil
---@param winapi winapi|nil
---@return NoitaMpSettings
function NoitaMpSettings:new(noitaMpSettingsObject, customProfiler, guiI, fileUtils, json, lfs, logger, utils, winapi)
local noitaMpSettings = noitaMpSettingsObject or self or {} -- Use self if this is called as a class constructor
setmetatable(noitaMpSettings, self)
self.__index = self
function NoitaMpSettings:new(noitaMpSettings, customProfiler, gui, fileUtils, json, lfs, logger, utils, winapi)
noitaMpSettings = setmetatable(noitaMpSettings or self, NoitaMpSettings)

-- Initialize all imports to avoid recursive imports
self.customProfiler = customProfiler or require("CustomProfiler"):new(nil, nil, self, nil, nil, nil, nil)
if not noitaMpSettings.customProfiler then
self.customProfiler = customProfiler or require("CustomProfiler"):new(nil, nil, self, nil, nil, nil, nil)
end
local cpc = self.customProfiler:start("NoitaMpSettings:new")
self.guiI = guiI or error("NoitaMpSettings:new requires a guiI object", 2)
self.fileUtils = fileUtils or require("FileUtils"):new()
self.json = json or require("json")
self.lfs = lfs or require("lfs")
self.logger = logger or require("Logger"):new(nil, customProfiler)
self.utils = utils or require("Utils"):new()
self.winapi = winapi or require("winapi")

customProfiler:stop("ExampleClass:new", cpc)

if not noitaMpSettings.gui then
self.gui = gui --or error("NoitaMpSettings:new requires a Gui object", 2)
end

if not noitaMpSettings.fileUtils then
self.fileUtils = fileUtils or self.customProfiler.fileUtils or require("FileUtils")--:new()
end

if not noitaMpSettings.json then
self.json = json or require("json")
end

if not noitaMpSettings.lfs then
self.lfs = lfs or require("lfs")
end

if not noitaMpSettings.logger then
self.logger = logger or require("Logger"):new(nil, self.customProfiler)
end

if not noitaMpSettings.utils then
self.utils = utils or self.customProfiler.utils or require("Utils")--:new()
end

if not noitaMpSettings.winapi then
self.winapi = winapi or self.customProfiler.winapi or require("winapi")
end

self.customProfiler:stop("ExampleClass:new", cpc)
return noitaMpSettings
end

Expand Down
Loading

0 comments on commit be985eb

Please sign in to comment.