rebel1324 / NutScript

A free role-play framework for Garry's Mod.
http://nutscript.net
MIT License
65 stars 31 forks source link

Schema name "Unknown" until first ply connects #191

Closed blt950 closed 5 years ago

blt950 commented 6 years ago

Seems after the recent restructure of how the schemas load in and order, now all servers start with "NS - Unknown" as gamemode, and this gets fixed once the first player joins.

rebel1324 commented 6 years ago

hmm strange. let me investigate.

brianhang commented 5 years ago

Do you still have this issue? I can't seem to replicate it.

blt950 commented 5 years ago

I still have this issue yes. Run the 1.1 version with HL2RP schema and it should replicate. I replicated it right now. To be precise, this is not the beta-version, so I'm not sure if that's fixed there. Once I join it starts displaying correctly "NS - HL2RP" image

blt950 commented 5 years ago

I had a suspicion which seems to be correct. This bug is related to database connection. This only occurs when you use Mysql server, so if you're trying to reproduce this with Sqlite - you won't manage to. I think there's some hook waiting for DB connection before setting gamemode name?

brianhang commented 5 years ago

Since the new version of NS will remove the NS prefix stuff anyways, you should probably overwrite the GetGameDescription hook to return the schema name in the schema with the "NS" prefix (or without).

blt950 commented 5 years ago

I've already done that, but it doesn't solve the issue. Then I would need to hardcode-edit the GetGameDescription in the framework for it to show correctly at server launch. Are you still not able to reproduce after switching to MySQL?

brianhang commented 5 years ago

It wouldn't have to be in the framework. You could add it to the schema. Maybe the framework shouldn't really be restricting people's gamemode names.

Also, this was after switching to MySQL with the mysqloo module.

blt950 commented 5 years ago

I know, I'm trying to say it doesn't work unless I put it in the framework. I've already GetGameDescription in sh_schema, but still it says unknown until the first player joins. If you can't reproduce it with Nut 1.1 (non-beta) and mysqloo, then that's weird - I'll start digging and debugging it, perhaps I'll figure it out. I've reinstalled multiple nutscript servers since the first post here, and I'm still struggling.

blt950 commented 5 years ago

Ok my first digging shows following:

This is the trace from sh_schema.lua, and it's only called after first player joins the server, which fixes the issue of schema name.

        1: Line 32      "Trace"         lua/includes/extensions/debug.lua
        2: Line 7       "nil"           gamemodes/hl2rp/schema/sh_schema.lua
        3: Line 428     "nil"           gamemodes/nutscript/gamemode/core/libs/sh_plugin.lua

sh_plugin.lua from framework at 428 is calling hooks, leading all the way back to that nut.plugin.initialize() which runs at function GM:OnDatabaseLoaded() in shared.lua. OnDatabaseLoaded() is called with hook.Run("OnDatabaseLoaded") in init.lua which ran under nut.db.connect(function() at line 23. Which leads to line 191 in sv_database.lua in nutscript who does dbModule.connect(callback).

This makes sense, as once the first player joins, the console prints out message that database has connected. dbModule.connect(callback) is where it stops, and the callback of nut.db.connect( is not ran before the first player runs, that's also then OnDatabaseLoaded hook is run and makes the sh_plugin run the SCHEMA:GetGameDescription and all other hooks.

If you run sqlite the database connects immediately after start. mysqloo runs async, so it seems the library/bin is waiting for the player to join the server before actually connecting and trigger the callback which then loads in all plugins (and schema as it is a plugin). The reason of the schema being "Unknown" to start with is that the schema is not loaded before "OnDatabaseLoaded" is called from a successful connection which the loads schema and plugin, therefore the framework fallback on "Unknown".

I guess you know a better solution, but one solution is perhaps triggering plugin loads at SetupDatabase or earlier hook instead of OnDatabaseLoaded is a solution, then everything would load regardless of database connection, but I don't think that's a good idea if some plugins rely on this DB-connection.

brianhang commented 5 years ago

I see, in the 1.1-beta branch, nut.plugin.initialize is called in the normal Initialize hook. So it's probably safe just to make the same change. If a plugin were to rely on the database connection, it should probably do the query stuff in the OnDatabaseLoaded hook.

brianhang commented 5 years ago

Can you test the changes here? https://github.com/rebel1324/NutScript/pull/254/files

blt950 commented 5 years ago

The change fixed the issue, yes 👍