Skip to content

Commit

Permalink
Fixed lua-doc mismatch for FileUtils. Added prettier formatting rul…
Browse files Browse the repository at this point in the history
…e for workspace. Fixed circular dependency injection.
  • Loading branch information
Ismoh committed Oct 1, 2023
1 parent 3ac5e0b commit 250a91d
Show file tree
Hide file tree
Showing 18 changed files with 251 additions and 227 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---@meta

---@class cc.FileUtils
---@meta cc.FileUtils
---@class cc.FileUtils
local FileUtils={ }
cc.FileUtils=FileUtils

Expand Down Expand Up @@ -128,7 +127,7 @@ function FileUtils:loadFilenameLookupDictionaryFromFile (filename) end
---* return True if pop up a message box when failed to load an image, false if not.
---@return boolean
function FileUtils:isPopupNotify () end
---*
---*
---@param filename string
---@return array_table
function FileUtils:getValueVectorFromFile (filename) end
Expand Down Expand Up @@ -340,4 +339,4 @@ function FileUtils:listFilesRecursively (dirPath,files) end
function FileUtils:destroyInstance () end
---* Gets the instance of FileUtils.
---@return self
function FileUtils:getInstance () end
function FileUtils:getInstance () end
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@
"setmetatable",
"winapi",
"zstd"
]
],
"prettier.printWidth": 110
}
4 changes: 2 additions & 2 deletions mods/noita-mp/files/scripts/Gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ function Gui:drawSettings()
end
if self.showSettingsSaved then
self.imGui.SameLine()
self.imGui.Text(("Saved into '%s'!"):format(self.fileUtils.GetRelativePathOfNoitaMpSettingsDirectory()))
self.imGui.Text(("Saved into '%s'!"):format(self.fileUtils:GetRelativePathOfNoitaMpSettingsDirectory()))
if GameGetRealWorldTimeSinceStarted() >= self.showSettingsSavedTimer + 10 then
self.showSettingsSaved = false
self.showSettingsSavedTimer = GameGetRealWorldTimeSinceStarted()
Expand Down Expand Up @@ -598,7 +598,7 @@ function Gui:drawAbout()
self.shortcuts.bugReport .. ".")

self.imGui.Separator()
self.imGui.Text("NoitaMP - Noita Multiplayer version " .. self.fileUtils.GetVersionByFile())
self.imGui.Text("NoitaMP - Noita Multiplayer version " .. self.fileUtils:GetVersionByFile())
self.imGui.Text("Made by Ismoh#0815 and many other awesome contributers!")
self.imGui.Text("Homepage: https://github.com/Ismoh/NoitaMP")
self.imGui.SameLine()
Expand Down
65 changes: 26 additions & 39 deletions mods/noita-mp/files/scripts/NoitaMpSettings.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
--- NoitaMpSettings: Replacement for Noita ModSettings.
--- @class NoitaMpSettings
local NoitaMpSettings = {
--[[ Imports ]]

---@type CustomProfiler
customProfiler = nil,
---@type Gui
gui = nil,
---@type FileUtils
fileUtils = nil,
---@type json
json = nil,
---@type LuaFileSystem
lfs = nil,
---@type Logger
logger = nil,
---@type Utils
utils = nil,
---@type winapi
winapi = nil,

--[[ Attributes ]]

---Settings cache. Makes it possible to access settings without reading the file every time.
Expand Down Expand Up @@ -62,17 +43,17 @@ local once = false
---@param self NoitaMpSettings required
---@return string path
local getSettingsFilePath = function(self)
local path = ("%s%ssettings.json"):format(self.fileUtils.GetAbsolutePathOfNoitaMpSettingsDirectory(), pathSeparator)
local path = ("%s%ssettings.json"):format(self.fileUtils:GetAbsolutePathOfNoitaMpSettingsDirectory(), pathSeparator)

if self:isMoreThanOneNoitaProcessRunning() then
local defaultSettings = nil
if self.fileUtils.Exists(path) and not once then
defaultSettings = self.fileUtils.ReadFile(path)
if self.fileUtils:Exists(path) and not once then
defaultSettings = self.fileUtils:ReadFile(path)
end
path = ("%s%slocal%ssettings-%s.json")
:format(self.fileUtils.GetAbsolutePathOfNoitaMpSettingsDirectory(), pathSeparator, pathSeparator, self.winapi.get_current_pid())
:format(self.fileUtils:GetAbsolutePathOfNoitaMpSettingsDirectory(), pathSeparator, pathSeparator, self.winapi.get_current_pid())
if defaultSettings then
self.fileUtils.WriteFile(path, defaultSettings)
self.fileUtils:WriteFile(path, defaultSettings)
once = true
end
end
Expand Down Expand Up @@ -100,9 +81,9 @@ end
---Removes all settings and creates a new settings file.
function NoitaMpSettings:clearAndCreateSettings()
local cpc = self.customProfiler:start("NoitaMpSettings.clearAndCreateSettings")
local settingsDir = ("%s%slocal"):format(self.fileUtils.GetAbsolutePathOfNoitaMpSettingsDirectory(), pathSeparator) --FileUtils.GetAbsolutePathOfNoitaMpSettingsDirectory()
if self.fileUtils.Exists(settingsDir) then
self.fileUtils.RemoveContentOfDirectory(settingsDir)
local settingsDir = ("%s%slocal"):format(self.fileUtils:GetAbsolutePathOfNoitaMpSettingsDirectory(), pathSeparator) --FileUtils.GetAbsolutePathOfNoitaMpSettingsDirectory()
if self.fileUtils:Exists(settingsDir) then
self.fileUtils:RemoveContentOfDirectory(settingsDir)
self.logger:info(self.logger.channels.initialize, ("Removed old settings in '%s'!"):format(settingsDir))
else
self.lfs.mkdir(settingsDir)
Expand All @@ -122,7 +103,7 @@ function NoitaMpSettings:set(key, value)
end

local settingsFilePath = getSettingsFilePath(self)
if self.utils.IsEmpty(self.cachedSettings) or not self.fileUtils.Exists(settingsFilePath) then
if self.utils.IsEmpty(self.cachedSettings) or not self.fileUtils:Exists(settingsFilePath) then
self:load()
end

Expand All @@ -140,7 +121,7 @@ function NoitaMpSettings:get(key, dataType)
local cpc = self.customProfiler:start("NoitaMpSettings.get")

local settingsFilePath = getSettingsFilePath(self)
if self.utils.IsEmpty(self.cachedSettings) or not self.fileUtils.Exists(settingsFilePath) then
if self.utils.IsEmpty(self.cachedSettings) or not self.fileUtils:Exists(settingsFilePath) then
self:load()
end

Expand All @@ -157,11 +138,11 @@ end
function NoitaMpSettings:load()
local settingsFilePath = getSettingsFilePath(self)

if not self.fileUtils.Exists(settingsFilePath) then
if not self.fileUtils:Exists(settingsFilePath) then
self:save()
end

local contentString = self.fileUtils.ReadFile(settingsFilePath)
local contentString = self.fileUtils:ReadFile(settingsFilePath)
self.cachedSettings = self.json.decode(contentString)
end

Expand All @@ -172,7 +153,7 @@ function NoitaMpSettings:save()
self.cachedSettings["pid"] = self.winapi.get_current_pid()
end

self.fileUtils.WriteFile(settingsFilePath, self.json.encode(self.cachedSettings))
self.fileUtils:WriteFile(settingsFilePath, self.json.encode(self.cachedSettings))
if self.gui then
self.gui.setShowSettingsSaved(true)
end
Expand All @@ -193,18 +174,24 @@ function NoitaMpSettings:new(noitaMpSettings, customProfiler, gui, fileUtils, js
---@class NoitaMpSettings
noitaMpSettings = setmetatable(noitaMpSettings or self, NoitaMpSettings)

-- Initialize all imports to avoid recursive imports
--[[ Imports ]]
--Initialize all imports to avoid recursive imports

if not noitaMpSettings.customProfiler then
noitaMpSettings.customProfiler = customProfiler or require("CustomProfiler"):new(nil, nil, self, nil, nil, nil, nil)
---@type CustomProfiler
---@see CustomProfiler
noitaMpSettings.customProfiler = customProfiler or
require("CustomProfiler")
:new(nil, nil, noitaMpSettings, nil, nil, nil, nil)
end
local cpc = noitaMpSettings.customProfiler:start("NoitaMpSettings:new")

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

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

if not noitaMpSettings.json then
Expand All @@ -216,15 +203,15 @@ function NoitaMpSettings:new(noitaMpSettings, customProfiler, gui, fileUtils, js
end

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

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

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

noitaMpSettings.customProfiler:stop("ExampleClass:new", cpc)
Expand Down
2 changes: 1 addition & 1 deletion mods/noita-mp/files/scripts/init/init_.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local checkMandatoryDependencyMods = function()
---@class NoitaMpSettings
local noitaMpSettings = require("NoitaMpSettings")
:new(nil, nil, gui, nil, nil, nil, nil)
---@class FileUtils
---@type FileUtils
local fileUtils = require("FileUtils")
:new(nil, noitaMpSettings.customProfiler, nil, noitaMpSettings, nil, nil)

Expand Down
2 changes: 1 addition & 1 deletion mods/noita-mp/files/scripts/init/init_package_loading.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ if current_clib_extension then
---@class NoitaMpSettings
local noitaMpSettings = require("NoitaMpSettings")
:new(nil, nil, gui, nil, nil, nil, nil)
---@class FileUtils
---@type FileUtils
local fileUtils = require("FileUtils")
:new(nil, noitaMpSettings.customProfiler, nil, noitaMpSettings, nil, nil)

Expand Down
2 changes: 1 addition & 1 deletion mods/noita-mp/files/scripts/net/Client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ function Client:new(clientObject, serverOrAddress, port, maxChannels, server)
local cpc = clientObject.customProfiler:start("Client:new")

if not clientObject.globalUtils then
clientObject.globalUtils = server.globalUtils or require("GlobalUtils")
clientObject.globalUtils = server.globalsUtils or require("GlobalUtils")
end
if not clientObject.networkCache then
clientObject.networkCache = server.networkCache or error("Client:new requires a server object!", 2)
Expand Down
102 changes: 69 additions & 33 deletions mods/noita-mp/files/scripts/net/Server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -961,13 +961,13 @@ function Server:getAckCacheSize()
end

---Server constructor. Inherits from SockServer sock.newServer.
---@param serverObject Server
---@param address string|nil
---@param port number|nil
---@param maxPeers number|nil
---@param maxChannels number|nil
---@param inBandwidth number|nil
---@param outBandwidth number|nil
---@param serverObject Server|nil optional
---@param address string|nil optional
---@param port number|nil optional
---@param maxPeers number|nil optional
---@param maxChannels number|nil optional
---@param inBandwidth number|nil optional
---@param outBandwidth number|nil optional
---@return Server
function Server:new(serverObject, address, port, maxPeers, maxChannels, inBandwidth, outBandwidth)
---@class Server : SockServer
Expand All @@ -979,70 +979,106 @@ function Server:new(serverObject, address, port, maxPeers, maxChannels, inBandwi
--Initialize all imports to avoid recursive imports

if not serverObject.noitaMpSettings then
---@type NoitaMpSettings
serverObject.noitaMpSettings = require("NoitaMpSettings")
:new(nil, nil, nil, nil, nil, nil, nil, nil, nil)
end

if not serverObject.customProfiler then
serverObject.customProfiler = serverObject.noitaMpSettings.customProfiler or require("CustomProfiler")
---@type CustomProfiler
serverObject.customProfiler = serverObject.noitaMpSettings.customProfiler or
require("CustomProfiler")
:new(nil, nil, serverObject.noitaMpSettings, nil, nil, nil, nil)
end
local cpc = serverObject.customProfiler:start("Server:new")

if not serverObject.logger or type(serverObject.logger) ~= "Logger" then
---@type Logger
serverObject.logger = serverObject.noitaMpSettings.logger or
require("Logger")
:new(nil, serverObject.customProfiler) or
error("serverObject.logger must not be nil!", 2)
end

if not serverObject.utils then
serverObject.utils = serverObject.noitaMpSettings.utils or error("serverObject.noitaMpSettings.utils must not be nil!", 2)
---@type Utils
serverObject.utils = serverObject.noitaMpSettings.utils or
error("serverObject.noitaMpSettings.utils must not be nil!", 2)
end
if not serverObject.logger then
serverObject.logger = serverObject.noitaMpSettings.logger or error("serverObject.noitaMpSettings.logger must not be nil!", 2)

if not serverObject.globalsUtils then
---@type GlobalsUtils
---@see GlobalsUtils
serverObject.globalsUtils = require("GlobalsUtils")
:new(nil, serverObject.customProfiler, serverObject.logger, nil, serverObject.utils) or
error("Unable to create GlobalsUtils!", 2)
end

if not serverObject.networkVscUtils then
---@type NetworkVscUtils
serverObject.networkVscUtils = require("NetworkVscUtils") --:new()
end

if not serverObject.minaUtils then
---@type MinaUtils
serverObject.minaUtils = require("MinaUtils")
--:new()
:new(nil, serverObject.customProfiler, serverObject.globalsUtils, serverObject.logger,
serverObject.networkVscUtils, serverObject.noitaMpSettings, serverObject.noitaMpSettings.utils) or
error("Unable to create MinaUtils!", 2)
end

if not serverObject.nuidUtils then
serverObject.nuidUtils = require("NuidUtils")
--:new()
end

if not serverObject.networkVscUtils then
serverObject.networkVscUtils = require("NetworkVscUtils")
--:new()
---@type NuidUtils
serverObject.nuidUtils = require("NuidUtils") --:new()
end

if not serverObject.networkUtils then
serverObject.networkUtils = require("NetworkUtils")
--:new()
---@type NetworkUtils
serverObject.networkUtils = require("NetworkUtils") --:new()
end

if not serverObject.networkCache then
serverObject.networkCache = require("NetworkCache")
--:new()
---@type NetworkCache
serverObject.networkCache = require("NetworkCache") --:new()
end

if not serverObject.networkCacheUtils then
serverObject.networkCacheUtils = require("NetworkCacheUtils")
--:new()
---@type NetworkCacheUtils
serverObject.networkCacheUtils = require("NetworkCacheUtils") --:new()
end

if not serverObject.noitaComponentUtils then
serverObject.noitaComponentUtils = require("NoitaComponentUtils")
--:new()
---@type NoitaComponentUtils
serverObject.noitaComponentUtils = require("NoitaComponentUtils") --:new()
end

if not serverObject.globalsUtils then
serverObject.globalsUtils = require("GlobalsUtils")
--:new()
if not serverObject.entityCache then
---@type EntityCache
serverObject.entityCache = require("EntityCache")
:new(nil, serverObject.customProfiler, nil, serverObject.noitaMpSettings.utils)
end

if not serverObject.entityCacheUtils then
---@type EntityCacheUtils
---@see EntityCacheUtils
serverObject.entityCacheUtils = require("EntityCacheUtils")
:new(nil, serverObject.customProfiler, serverObject.entityCache, serverObject.noitaMpSettings.utils)
end

if not serverObject.entityUtils then
serverObject.entityUtils = require("EntityUtils")
:new(nil, {}, serverObject.customProfiler, nil, nil, serverObject.globalsUtils,
serverObject.noitaMpSettings.logger, serverObject.minaUtils, serverObject.networkUtils, serverObject.networkVscUtils,
serverObject.noitaComponentUtils, serverObject.nuidUtils, serverObject, serverObject.noitaMpSettings.utils)
:new(nil, {}, serverObject.customProfiler, serverObject.entityCacheUtils, serverObject.entityCache,
serverObject.globalsUtils, serverObject.noitaMpSettings.logger, serverObject.minaUtils,
serverObject.networkUtils, serverObject.networkVscUtils, serverObject.noitaComponentUtils,
serverObject.nuidUtils, serverObject, serverObject.noitaMpSettings.utils) or
error("Unable to create EntityUtils!", 2)

serverObject.entityCache.entityUtils = serverObject.entityUtils
end

if not serverObject.fileUtils then
---@type FileUtils
---@see FileUtils
serverObject.fileUtils = require("FileUtils")
:new(nil, serverObject.customProfiler, serverObject.logger, serverObject.noitaMpSettings, nil, serverObject.utils)
end
Expand Down
3 changes: 1 addition & 2 deletions mods/noita-mp/files/scripts/util/EntityCache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ function EntityCache:new(entityCacheObject, customProfiler, entityUtils, utils)

if not entityCacheObject.entityUtils then
---@type EntityUtils
entityCacheObject.entityUtils = entityUtils or
error("EntityCache:new requires 'entityUtils' as parameter!")
entityCacheObject.entityUtils = entityUtils
end

if not entityCacheObject.utils then
Expand Down
Loading

0 comments on commit 250a91d

Please sign in to comment.