hewiefreeman / GopherGameServer

:trophy: Feature packed, easy-to-use game server API for Go back-ends and Javascript clients. Tutorials and examples included!
https://hewiefreeman.github.io/GopherGameServer
Apache License 2.0
176 stars 21 forks source link

SetLoginCallback throws nil pointer dereference #6

Open perling1 opened 2 years ago

perling1 commented 2 years ago

Whenever i set the SetLoginCallback function, i get a runtime fatal panic

panic: runtime error: invalid memory address or nil pointer dereference github.com/hewiefreeman/GopherGameServer.SetLoginCallback({0xed0fa0, 0xf327a8}) [C:/Users/mbd/go/pkg/mod/github.com/hewiefreeman/]()!gopher!game!server@v0.0.0-20211216002529-88cc3fabb02c/callbacks.go:125 +0xc2

Is it because there is no database initialized? You can recreate the error with:

func main() {
    settings := gopher.ServerSettings{
        ServerName:     "myserv01",
        MaxConnections: 6,

        HostName:  "localhost",
        HostAlias: "localhost",
        IP:        "localhost",
        Port:      8080,

        TLS:         false,
        CertFile:    "",
        PrivKeyFile: "",

        OriginOnly: false,

        MultiConnect:   false,
        KickDupOnLogin: false,

        UserRoomControl:   true,
        RoomDeleteOnLeave: true,

        EnableSqlFeatures: false,
        SqlIP:             "localhost",
        SqlPort:           3306,
        SqlProtocol:       "tcp",
        SqlUser:           "user",
        SqlPassword:       "password",
        SqlDatabase:       "database",
        EncryptionCost:    4,
        CustomLoginColumn: "",
        RememberMe:        false,

        EnableRecovery:   false,
        RecoveryLocation: "C:/",
        AdminLogin:       "admin",
        AdminPassword:    "1234",
    }

 setErrs := gopher.SetLoginCallback(clientLoggedIn)
    if setErrs != nil {
        fmt.Println(setErrs)
    }
gopher.Start(&settings)
}

func clientLoggedIn(userName string, databaseID int, receivedColumns map[string]interface{}, clientColumns map[string]interface{}) bool {
    fmt.Println("Entered")
    return true
}
perling1 commented 2 years ago

To give more insight, the callback onlogout works as described. It doesnt throw a nil pointer panic.

func main() {
    settings := gopher.ServerSettings{
        ServerName:     "myserv01",
        MaxConnections: 6,

        HostName:  "localhost",
        HostAlias: "localhost",
        IP:        "localhost",
        Port:      8080,

        TLS:         false,
        CertFile:    "",
        PrivKeyFile: "",

        OriginOnly: false,

        MultiConnect:   false,
        KickDupOnLogin: false,

        UserRoomControl:   true,
        RoomDeleteOnLeave: true,

        EnableSqlFeatures: false,
        SqlIP:             "localhost",
        SqlPort:           3306,
        SqlProtocol:       "tcp",
        SqlUser:           "user",
        SqlPassword:       "password",
        SqlDatabase:       "database",
        EncryptionCost:    4,
        CustomLoginColumn: "",
        RememberMe:        false,
        EnableRecovery:   false,
        RecoveryLocation: "C:/",
        AdminLogin:       "admin",
        AdminPassword:    "1234",
    }
    setErrs := gopher.SetLogoutCallback(clientLoggedOut)
    if setErrs != nil {
        fmt.Println(setErrs)
    }
    gopher.Start(&settings)
}

func clientLoggedOut(userName string, databaseID int) {
    fmt.Println("Entered")
}

The nil pointer comes from line125 in callbacks.go its the only line of code that tries to access the setting by *

if (*settings).EnableSqlFeatures {

hewiefreeman commented 2 years ago

Oh, this is an interesting problem. Thanks for bringing it to my attention! It seems there was an oversight on the flow of starting the server. For now, you could make a method UpdateSettings(s *ServerSettings) in server.go that replaces the // Set server settings potion of the Start() function (just cut-paste into the new function). Then you can update the settings before calling SetLogoutCallback() or anything else that needs it before starting. I just don't have the time to get around to this for a day or so.

perling1 commented 2 years ago

Another problem is golang cant include pullrequests from github modules. I have modified the server.go, but i cant get it loaded in my project as its not a "master" oder "stable" version.

hewiefreeman commented 2 years ago

This project hasn't been set up with go modules. At least not yet. I've been able to use GGS along with other Go APIs without modules. Are you sure your project is in the right gopath to prevent it from using modules?

perling1 commented 2 years ago

I was maybe to unspecific. I use a go.mod file with my own fork

go.mod
require (
    github.com/perling1/GopherGameServer v0.0.0-20220214170824-f4a7d5e6619a // indirect
)

In the main testserver.go file:

import (
... 
gopher "github.com/perling1/GopherGameServer"
    "github.com/perling1/GopherGameServer/actions"
    "github.com/perling1/GopherGameServer/core"
)
..
       gopher.UpdateServerSettings(&settings)
    setErrs = gopher.SetLoginCallback(clientLoggedIn)

Because if i import your GGS github.com/hewiefreeman/GopherGameServer/ my pullrequest wasnt imported , so the func "gopher.UpdateServerSettings(&settings) " didnt exist.

Its a big hassle to import "github libs" that change. you have to use terminal commands to reset caches go mod -clean go mod tidy go get github.com/perling1/GopherGameServer@master

Thats an annoying act while using "go and mod". Its not due to your project.