Skip to content

Commit

Permalink
Config / DB now its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
stratic-dev committed Oct 12, 2024
1 parent 44692e9 commit 5f975c9
Show file tree
Hide file tree
Showing 76 changed files with 3,235 additions and 2,345 deletions.
27 changes: 16 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package _config
package config

import (
"fmt"
"log"
"net"
"os"
"strings"
"sync"
"time"

"github.com/spf13/viper"
Expand Down Expand Up @@ -299,15 +300,10 @@ type EntranceChannelInfo struct {
CurrentPlayers uint16
}

var ErupeConfig *Config

func init() {
var err error
ErupeConfig, err = LoadConfig()
if err != nil {
preventClose(fmt.Sprintf("Failed to load config: %s", err.Error()))
}
}
var (
once sync.Once
ErupeConfig *Config
)

// getOutboundIP4 gets the preferred outbound ip4 of this machine
// From https://stackoverflow.com/a/37382208
Expand Down Expand Up @@ -368,7 +364,16 @@ func LoadConfig() (*Config, error) {

return c, nil
}

func GetConfig() *Config {
once.Do(func() {
var err error
ErupeConfig, err = LoadConfig()
if err != nil {
preventClose(fmt.Sprintf("Failed to load config: %s", err.Error()))
}
})
return ErupeConfig
}
func preventClose(text string) {
if ErupeConfig.DisableSoftCrash {
os.Exit(0)
Expand Down
180 changes: 59 additions & 121 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
_config "erupe-ce/config"
"erupe-ce/config"
"fmt"
"net"
"os"
Expand All @@ -13,27 +13,19 @@ import (
"erupe-ce/server/api"
"erupe-ce/server/channelserver"
"erupe-ce/server/discordbot"
"erupe-ce/server/entranceserver"
"erupe-ce/server/entrance"
"erupe-ce/server/sign"
"erupe-ce/utils/db"
"erupe-ce/utils/logger"

"erupe-ce/server/signserver"
"erupe-ce/utils/gametime"

"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"go.uber.org/zap"
)

var mainLogger logger.Logger

// Temporary DB auto clean on startup for quick development & testing.
func cleanDB(db *sqlx.DB) {
_ = db.MustExec("DELETE FROM guild_characters")
_ = db.MustExec("DELETE FROM guilds")
_ = db.MustExec("DELETE FROM characters")
_ = db.MustExec("DELETE FROM users")
}

var Commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
Expand All @@ -54,113 +46,35 @@ func initLogger() {
mainLogger = logger.Get().Named("main")

}

func main() {
var err error

config := _config.ErupeConfig
config := config.GetConfig()
initLogger()
mainLogger.Info(fmt.Sprintf("Starting Erupe (9.3b-%s)", Commit()))
mainLogger.Info(fmt.Sprintf("Client Mode: %s (%d)", config.ClientMode, config.ClientID))

if config.Database.Password == "" {
preventClose("Database password is blank")
}

if net.ParseIP(config.Host) == nil {
ips, _ := net.LookupIP(config.Host)
for _, ip := range ips {
if ip != nil {
config.Host = ip.String()
break
}
}
if net.ParseIP(config.Host) == nil {
preventClose("Invalid host address")
}
}

// Discord bot
var discordBot *discordbot.DiscordBot = nil
checkAndExitIf(config.Database.Password == "", "Database password is blank")

if config.Discord.Enabled {
bot, err := discordbot.NewDiscordBot(discordbot.Options{
Config: _config.ErupeConfig,
})
DiscordFailMsg := "Discord: Failed to start, %s"
if err != nil {
preventClose(fmt.Sprintf(DiscordFailMsg, err.Error()))
}
resolveHostIP()

// Discord bot
err = bot.Start()

if err != nil {
preventClose(fmt.Sprintf(DiscordFailMsg, err.Error()))
}

discordBot = bot

_, err = discordBot.Session.ApplicationCommandBulkOverwrite(discordBot.Session.State.User.ID, "", discordbot.Commands)
if err != nil {
preventClose(fmt.Sprintf(DiscordFailMsg, err.Error()))
}

mainLogger.Info("Discord: Started successfully")
} else {
mainLogger.Info("Discord: Disabled")
}
discordBot := initializeDiscordBot()

// Create the postgres DB pool.
connectString := fmt.Sprintf(
"host='%s' port='%d' user='%s' password='%s' dbname='%s' sslmode=disable",
config.Database.Host,
config.Database.Port,
config.Database.User,
config.Database.Password,
config.Database.Database,
)

db, err := sqlx.Open("postgres", connectString)
database, err := db.InitDB(config)
if err != nil {
preventClose(fmt.Sprintf("Database: Failed to open, %s", err.Error()))
}

// Test the DB connection.
err = db.Ping()
if err != nil {
preventClose(fmt.Sprintf("Database: Failed to ping, %s", err.Error()))
}
mainLogger.Info("Database: Started successfully")

// Clear stale data
if config.DebugOptions.ProxyPort == 0 {
_ = db.MustExec("DELETE FROM sign_sessions")
}
_ = db.MustExec("DELETE FROM servers")
_ = db.MustExec(`UPDATE guild_characters SET treasure_hunt=NULL`)

// Clean the DB if the option is on.
if config.DebugOptions.CleanDB {
mainLogger.Info("Database: Started clearing...")
cleanDB(db)
mainLogger.Info("Database: Finished clearing")
mainLogger.Fatal(fmt.Sprintf("Database initialization failed: %s", err))
}

mainLogger.Info(fmt.Sprintf("Server Time: %s", gametime.TimeAdjusted().String()))

// Now start our server(s).

// Entrance server.
entranceLogger := logger.Get().Named("entrance")

var entranceServer *entranceserver.Server
var entranceServer *entrance.EntranceServer
if config.Entrance.Enabled {
entranceServer = entranceserver.NewServer(
&entranceserver.Config{
Logger: entranceLogger,
ErupeConfig: _config.ErupeConfig,
DB: db,
})
entranceServer = entrance.NewServer()
err = entranceServer.Start()
if err != nil {
preventClose(fmt.Sprintf("Entrance: Failed to start, %s", err.Error()))
Expand All @@ -171,13 +85,9 @@ func main() {
}

// Sign server.
var signServer *signserver.Server
var signServer *sign.SignServer
if config.Sign.Enabled {
signServer = signserver.NewServer(
&signserver.Config{
ErupeConfig: _config.ErupeConfig,
DB: db,
})
signServer = sign.NewServer()
err = signServer.Start()
if err != nil {
preventClose(fmt.Sprintf("Sign: Failed to start, %s", err.Error()))
Expand All @@ -188,23 +98,18 @@ func main() {
}

// Api server
var ApiServer *api.APIServer
var apiServer *api.APIServer
if config.API.Enabled {
ApiServer = api.NewAPIServer(
&api.Config{
ErupeConfig: _config.ErupeConfig,
DB: db,
})
err = ApiServer.Start()
apiServer = api.NewAPIServer()
err = apiServer.Start()
if err != nil {
preventClose(fmt.Sprintf("API: Failed to start, %s", err.Error()))
}
mainLogger.Info("API: Started successfully")
} else {
mainLogger.Info("API: Disabled")
}

var channelServers []*channelserver.Server
var channelServers []*channelserver.ChannelServer
if config.Channel.Enabled {
channelQuery := ""
si := 0
Expand All @@ -214,10 +119,8 @@ func main() {
for i, ce := range ee.Channels {
sid := (4096 + si*256) + (16 + ci)
c := *channelserver.NewServer(&channelserver.Config{
ID: uint16(sid),
ErupeConfig: _config.ErupeConfig,
DB: db,
DiscordBot: discordBot,
ID: uint16(sid),
DiscordBot: discordBot,
})
if ee.IP == "" {
c.IP = config.Host
Expand All @@ -242,7 +145,7 @@ func main() {
}

// Register all servers in DB
_ = db.MustExec(channelQuery)
_ = database.MustExec(channelQuery)

for _, c := range channelServers {
c.Channels = channelServers
Expand Down Expand Up @@ -278,7 +181,7 @@ func main() {
}

if config.API.Enabled {
ApiServer.Shutdown()
apiServer.Shutdown()
}

if config.Entrance.Enabled {
Expand All @@ -295,11 +198,46 @@ func wait() {
}

func preventClose(text string) {
if _config.ErupeConfig.DisableSoftCrash {
if config.GetConfig().DisableSoftCrash {
os.Exit(0)
}
mainLogger.Error(fmt.Sprintf(("\nFailed to start Erupe:\n" + text)))
go wait()
mainLogger.Error(fmt.Sprintf(("\nPress Enter/Return to exit...")))
os.Exit(0)
}

func checkAndExitIf(condition bool, message string) {
if condition {
preventClose(message)
}
}

func resolveHostIP() {
if net.ParseIP(config.GetConfig().Host) == nil {
ips, err := net.LookupIP(config.GetConfig().Host)
if err == nil && len(ips) > 0 {
config.GetConfig().Host = ips[0].String()
}
checkAndExitIf(net.ParseIP(config.GetConfig().Host) == nil, "Invalid host address")
}
}

func initializeDiscordBot() *discordbot.DiscordBot {
if !config.GetConfig().Discord.Enabled {
mainLogger.Info("Discord: Disabled")
return nil
}

bot, err := discordbot.NewDiscordBot()
checkAndExitIf(err != nil, fmt.Sprintf("Discord: Failed to start, %s", err))

err = bot.Start()
checkAndExitIf(err != nil, fmt.Sprintf("Discord: Failed to start, %s", err))

_, err = bot.Session.ApplicationCommandBulkOverwrite(bot.Session.State.User.ID, "", discordbot.Commands)
checkAndExitIf(err != nil, fmt.Sprintf("Discord: Failed to register commands, %s", err))

mainLogger.Info("Discord: Started successfully")
return bot
}
4 changes: 2 additions & 2 deletions network/crypt_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package network
import (
"encoding/hex"
"errors"
_config "erupe-ce/config"
"erupe-ce/config"
"erupe-ce/network/crypto"
"fmt"
"io"
Expand Down Expand Up @@ -51,7 +51,7 @@ func (cc *CryptConn) ReadPacket() ([]byte, error) {
var encryptedPacketBody []byte

// Don't know when support for this was added, works in Forward.4, doesn't work in Season 6.0
if _config.ErupeConfig.ClientID < _config.F1 {
if config.GetConfig().ClientID < config.F1 {
encryptedPacketBody = make([]byte, cph.DataSize)
} else {
encryptedPacketBody = make([]byte, uint32(cph.DataSize)+(uint32(cph.Pf0-0x03)*0x1000))
Expand Down
4 changes: 2 additions & 2 deletions network/mhfpacket/msg_mhf_acquire_cafe_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package mhfpacket

import (
"errors"
_config "erupe-ce/config"
"erupe-ce/config"

"erupe-ce/network"
"erupe-ce/utils/byteframe"
Expand Down Expand Up @@ -30,7 +30,7 @@ func (m *MsgMhfAcquireCafeItem) Parse(bf *byteframe.ByteFrame) error {
m.ItemType = bf.ReadUint16()
m.ItemID = bf.ReadUint16()
m.Quant = bf.ReadUint16()
if _config.ErupeConfig.ClientID >= _config.G1 {
if config.GetConfig().ClientID >= config.G1 {
m.PointCost = bf.ReadUint32()
} else {
m.PointCost = uint32(bf.ReadUint16())
Expand Down
6 changes: 3 additions & 3 deletions network/mhfpacket/msg_mhf_apply_dist_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package mhfpacket

import (
"errors"
_config "erupe-ce/config"
"erupe-ce/config"
"erupe-ce/network"
"erupe-ce/utils/byteframe"
)
Expand All @@ -26,10 +26,10 @@ func (m *MsgMhfApplyDistItem) Parse(bf *byteframe.ByteFrame) error {
m.AckHandle = bf.ReadUint32()
m.DistributionType = bf.ReadUint8()
m.DistributionID = bf.ReadUint32()
if _config.ErupeConfig.ClientID >= _config.G8 {
if config.GetConfig().ClientID >= config.G8 {
m.Unk2 = bf.ReadUint32()
}
if _config.ErupeConfig.ClientID >= _config.G10 {
if config.GetConfig().ClientID >= config.G10 {
m.Unk3 = bf.ReadUint32()
}
return nil
Expand Down
Loading

0 comments on commit 5f975c9

Please sign in to comment.