Skip to content

Commit

Permalink
Fixed server being able to run without crash for now.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ismoh committed Nov 1, 2023
1 parent adae659 commit 16758ca
Show file tree
Hide file tree
Showing 21 changed files with 588 additions and 1,296 deletions.
8 changes: 8 additions & 0 deletions .debug/grep-jit-profiler.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cat jit.p.log | grep '3.%' -A 3 -B 3 >'30%+.log'
cat jit.p.log | grep '4.%' -A 3 -B 3 >'40%+.log'
cat jit.p.log | grep '5.%' -A 3 -B 3 >'50%+.log'
cat jit.p.log | grep '6.%' -A 3 -B 3 >'60%+.log'
cat jit.p.log | grep '7.%' -A 3 -B 3 >'70%+.log'
cat jit.p.log | grep '8.%' -A 3 -B 3 >'80%+.log'
cat jit.p.log | grep '9.%' -A 3 -B 3 >'90%+.log'
cat jit.p.log | grep '10.%' -A 3 -B 3 >'100%.log'
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,7 @@
"prettier.printWidth": 110,
"lua.debug.settings.luaVersion": "jit",
"lua-local.interpreter": "",
"[shellscript]": {
"editor.defaultFormatter": "foxundermoon.shell-format"
},
}
10 changes: 9 additions & 1 deletion mods/noita-mp/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ entityUtils.include = {
}
entityUtils.exclude = {
byComponentsName = { "WorldStateComponent" },
byFilename = { "particle", "tree_entity.xml", "vegetation", "custom_cards" }
byFilename = {
"controls",
"custom_cards",
"particle",
"player_stats",
"tree_entity.xml",
"vegetation",
"vines",
}
}

entityUtils.remove = {
Expand Down
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 @@ -343,7 +343,7 @@ function Gui:drawPlayMenu()

self.imGui.Separator()
if self.imGui.Button("Start Server!") then
self.server:start(self.server:getAddress(), self.server:getPort())
self.server:preStart(self.server:getAddress(), self.server:getPort())
self.showPlayMenu = false
end
else
Expand All @@ -359,7 +359,7 @@ function Gui:drawPlayMenu()

self.imGui.Separator()
if self.imGui.Button("Join self.server..") then
self.client:connect(serverIp, tonumber(serverPort))
self.client:preConnect(serverIp, tonumber(serverPort))
self.showPlayMenu = false
end
end
Expand Down
5 changes: 5 additions & 0 deletions mods/noita-mp/files/scripts/extensions/tableExtensions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ function table.contains(tbl, key)
if key == "" then
return false, -1
end
for k, v in pairs(tbl) do
if string.contains(key, v) then
return true, k
end
end
end
for k, v in pairs(tbl) do
if v == key then
Expand Down
43 changes: 16 additions & 27 deletions mods/noita-mp/files/scripts/net/Client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ end
---@param port number|nil port number from 1 to max of 65535 or nil
---@param code number|nil connection code 0 = connecting first time, 1 = connected second time with loaded seed
---@see sock.connect
function Client:connect(ip, port, code)
function Client:preConnect(ip, port, code)
local cpc = self.customProfiler:start("Client.connect")

if self:isConnecting() or self:isConnected() then
Expand Down Expand Up @@ -748,20 +748,17 @@ function Client:connect(ip, port, code)
""
)

-- Inheritance: https://ozzypig.com/2018/05/10/object-oriented-programming-in-lua-part-5-inheritance
self.sock.connect(self, code) --sockClientConnect(self, code)

self:connect(code)
-- FYI: If you want to send data after connected, do it in the "connect" callback function
self.customProfiler:stop("Client.connect", cpc16)
self.customProfiler:stop("Client.connect", cpc)
end

---Disconnects from the server. Inherit from sock.disconnect.
---@see sock.disconnect
function Client:disconnect()
--- Disconnects from the server.
---@see SockClient.disconnect
function Client:preDisconnect()
local cpc = self.customProfiler:start("Client.disconnect")
if self.isConnected() then
-- Inheritance: https://ozzypig.com/2018/05/10/object-oriented-programming-in-lua-part-5-inheritance
self.sock.disconnect(self) --sockClientDisconnect(self)
if self:isConnected() then
self:disconnect()
else
self.logger:info(self.logger.channels.network, "Client isn't connected, no need to disconnect!")
end
Expand All @@ -771,15 +768,15 @@ end
local prevTime = 0
---Updates the Client by checking for network events and handling them. Inherit from sock.update.
---@param startFrameTime number required
---@see sock.update
function Client:update(startFrameTime)
---@see SockClient.update
function Client:preUpdate(startFrameTime)
local cpc = self.customProfiler:start("Client.update")
if not self:isConnected() and not self:isConnecting() or self:isDisconnected() then
self.customProfiler:stop("Client.update", cpc)
return
end

self.sendMinaInformation()
self:sendMinaInformation()

--self.entityUtils.destroyClientEntities()
--self.entityUtils.processEntityNetworking()
Expand All @@ -798,11 +795,10 @@ function Client:update(startFrameTime)

--self.entityUtils.destroyClientEntities()
--self.entityUtils.syncEntityData()
self.entityUtils.syncDeadNuids()
self.entityUtils:syncDeadNuids(nil, self)
end

-- Inheritance: https://ozzypig.com/2018/05/10/object-oriented-programming-in-lua-part-5-inheritance
self.update(self) --sockClientUpdate(self)
self:update()
self.customProfiler:stop("Client.update", cpc)
end

Expand Down Expand Up @@ -950,7 +946,7 @@ end
---@see Server.amIServer
function Client:amIClient()
--local cpc24 = self.customProfiler:start("Client.amIClient") DO NOT PROFILE, stack overflow error! See self.customProfiler.lua
if not self.server.amIServer() then
if not self.server:amIServer() then
--self.customProfiler:stop("Client.amIClient", cpc24)
return true
end
Expand Down Expand Up @@ -1011,14 +1007,13 @@ function Client.new(clientObject, serverOrAddress, port, maxChannels, server, np
if not clientObject.logger or clientObject.logger ~= clientObject.noitaMpSettings.logger then
clientObject.logger = clientObject.noitaMpSettings.logger or error("Client:new requires a server object!", 2)
end
if not clientObject.messagePack then
clientObject.messagePack = server.messagePack or require("MessagePack")
end
if not clientObject.minaUtils then
clientObject.minaUtils = server.minaUtils or require("MinaUtils")
end
if not clientObject.networkUtils then
clientObject.networkUtils = server.networkUtils or require("NetworkUtils")
:new(clientObject.customProfiler, clientObject.guidUtils, clientObject.logger,
clientObject.networkCacheUtils, clientObject.utils)
end
if not clientObject.noitaPatcherUtils then
clientObject.noitaPatcherUtils = server.noitaPatcherUtils or
Expand All @@ -1028,12 +1023,6 @@ function Client.new(clientObject, serverOrAddress, port, maxChannels, server, np
if not clientObject.server then
clientObject.server = server or error("Client:new requires a server object!", 2)
end
if not clientObject.sock then
clientObject.sock = server.sock or require("sock")
end
if not clientObject.zstandard then
clientObject.zstandard = server.zstandard or require("zstd")
end
if not clientObject.utils then
clientObject.utils = server.utils or error("Client:new requires a server object!", 2)
end
Expand Down
72 changes: 23 additions & 49 deletions mods/noita-mp/files/scripts/net/Server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ end
---Starts a server on ip and port. Both can be nil, then ModSettings will be used.
---@param ip string|nil localhost or 127.0.0.1 or nil
---@param port number|nil port number from 1 to max of 65535 or nil
function Server:start(ip, port)
function Server:preStart(ip, port)
local cpc = self.customProfiler:start("Server.start")
if not ip then
--ip = tostring(ModSettingGet("noita-mp.server_ip"))
Expand All @@ -712,7 +712,7 @@ function Server:start(ip, port)

self.logger:info(self.logger.channels.network, ("Starting server on %s:%s.."):format(ip, port))

local success = getmetatable(self).__index.start(self, ip, port)
local success = self:start(ip, port, self.fileUtils, self.logger)
if success == true then
self.logger:info(self.logger.channels.network, ("Server started on %s:%s"):format(self:getAddress(), self:getPort()))

Expand Down Expand Up @@ -759,7 +759,7 @@ local prevTime = 0
---Updates the server by checking for network events and handling them.
---@param startFrameTime number required
---@see SockServer.update
function Server:update(startFrameTime)
function Server:preUpdate(startFrameTime)
local cpc = self.customProfiler:start("Server.update")
if not self:isRunning() then
--if not self.host then server not established
Expand All @@ -777,7 +777,7 @@ function Server:update(startFrameTime)
end
self:sendMinaInformation()

self.entityUtils:syncEntities(startFrameTime)
self.entityUtils:syncEntities(startFrameTime, self, nil)

local nowTime = GameGetRealWorldTimeSinceStarted() * 1000 -- *1000 to get milliseconds
local elapsedTime = nowTime - prevTime
Expand All @@ -789,12 +789,12 @@ function Server:update(startFrameTime)
--updateVariables()

--self.entityUtils:syncEntityData()
self.entityUtils:syncDeadNuids()
self.entityUtils:syncDeadNuids(self, nil)
--end
self.customProfiler:stop("Server.update.tick", cpc1)
end

getmetatable(self).__index.update(self)
self:update()
self.customProfiler:stop("Server.update", cpc)
end

Expand Down Expand Up @@ -851,7 +851,7 @@ function Server:sendNewNuid(ownerName, ownerGuid, entityId, serializedEntityStri
end

local event = self.networkUtils.events.newNuid.name
local data = { self.networkUtils.getNextNetworkMessageId(), ownerName, ownerGuid, entityId, serializedEntityString, nuid, x, y,
local data = { self.networkUtils:getNextNetworkMessageId(), ownerName, ownerGuid, entityId, serializedEntityString, nuid, x, y,
initialSerializedEntityString }
local sent = self:sendToAll(event, data)

Expand Down Expand Up @@ -922,7 +922,7 @@ function Server:sendMinaInformation()
local guid = self.minaUtils:getLocalMinaGuid()
local entityId = self.minaUtils:getLocalMinaEntityId()
local nuid = self.minaUtils:getLocalMinaNuid()
local _name, _guid, _nuid, _filename, health, rotation, velocity, x, y = NoitaComponentUtils.getEntityData(entityId) -- TODO: rework this
local _name, _guid, _nuid, _filename, health, rotation, velocity, x, y = self.noitaComponentUtils:getEntityData(entityId) -- TODO: rework this
local data = {
self.networkUtils:getNextNetworkMessageId(), self.fileUtils:GetVersionByFile(), name, guid, entityId, nuid, { x = x, y = y }, health
}
Expand Down Expand Up @@ -1051,9 +1051,12 @@ function Server.new(address, port, maxPeers, maxChannels, inBandwidth, outBandwi
serverObject.logger, nil)
end

if not serverObject.networkUtils then
---@type NetworkUtils
serverObject.networkUtils = require("NetworkUtils") --:new()
if not serverObject.guidUtils then
---@type GuidUtils
---@see GuidUtils
serverObject.guidUtils = require("GuidUtils")
:new(nil, serverObject.customProfiler, serverObject.fileUtils, serverObject.logger,
nil, nil, serverObject.utils)
end

if not serverObject.networkCache then
Expand All @@ -1066,6 +1069,13 @@ function Server.new(address, port, maxPeers, maxChannels, inBandwidth, outBandwi
serverObject.networkCacheUtils = require("NetworkCacheUtils") --:new()
end

if not serverObject.networkUtils then
---@type NetworkUtils
serverObject.networkUtils = require("NetworkUtils")
:new(serverObject.customProfiler, serverObject.guidUtils, serverObject.logger,
serverObject.networkCacheUtils, serverObject.utils)
end

if not serverObject.noitaComponentUtils then
---@type NoitaComponentUtils
serverObject.noitaComponentUtils = require("NoitaComponentUtils")
Expand All @@ -1091,55 +1101,18 @@ function Server.new(address, port, maxPeers, maxChannels, inBandwidth, outBandwi
: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
serverObject.nuidUtils, serverObject, serverObject.noitaMpSettings.utils, np) or
error("Unable to create EntityUtils!", 2)

serverObject.entityCache.entityUtils = serverObject.entityUtils
end

if not serverObject.guidUtils then
---@type GuidUtils
---@see GuidUtils
serverObject.guidUtils = require("GuidUtils")
:new(nil, serverObject.customProfiler, serverObject.fileUtils, serverObject.logger,
nil, nil, serverObject.utils)
end

if not serverObject.messagePack then
serverObject.messagePack = require("MessagePack")
end

if not serverObject.noitaPatcherUtils then
serverObject.noitaPatcherUtils = require("NoitaPatcherUtils")
:new(nil, nil, serverObject.customProfiler, np)
end

--if not serverObject.sock then
-- serverObject.sock = require("sock")
--end
if not serverObject.zstandard then
serverObject.zstandard = require("zstd")
end

-- [[ Attributes ]]
serverObject.address = address
serverObject.port = port
serverObject.host = nil
serverObject.messageTimeout = 0
serverObject.maxChannels = maxChannels
serverObject.maxPeers = maxPeers
serverObject.sendMode = "reliable"
serverObject.defaultSendMode = "reliable"
serverObject.sendChannel = 0
serverObject.defaultSendChannel = 0
serverObject.peers = {}
serverObject.clients = {}
--serverObject.listener = newListener()
--serverObject.logger = newLogger("SERVER")
serverObject.serialize = nil
serverObject.deserialize = nil
serverObject.packetsSent = 0
serverObject.packetsReceived = 0

serverObject.acknowledgeMaxSize = 500
serverObject.guid = nil
Expand All @@ -1158,6 +1131,7 @@ function Server.new(address, port, maxPeers, maxChannels, inBandwidth, outBandwi
serverObject.guid = tostring(serverObject.noitaMpSettings:get("noita-mp.guid", "string"))
serverObject.guidUtils:setGuid(nil, serverObject, serverObject.guid)

print("Server loaded, instantiated and inherited!")
serverObject.customProfiler:stop("Server.new", cpc)
return serverObject
end
Expand Down
6 changes: 0 additions & 6 deletions mods/noita-mp/files/scripts/net/SockClient.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,6 @@ function SockClient:establishClient(serverOrAddress, port)
self.connection = serverOrAddress
self.connectId = self.connection:connect_id()
end

-- Serialization is set in Client.setConfigSettings()
--if bitserLoaded then
-- self:setSerialization(bitser.dumps, bitser.loads)
--end
--self:setSerialization(zstandard.compress, zstandard.decompress)
end

--- Check for network events and handle them.
Expand Down
Loading

0 comments on commit 16758ca

Please sign in to comment.