2016-10-04 15:42:36 +05:00
|
|
|
// URTator - Urban Terror server browser and game launcher, written in
|
|
|
|
// Go.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2016, Stanslav N. a.k.a pztrn (or p0z1tr0n)
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under Terms and Conditions of GNU General Public License
|
|
|
|
// version 3 or any higher.
|
|
|
|
// ToDo: put full text of license here.
|
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
// stdlib
|
2016-10-06 22:19:03 +05:00
|
|
|
"errors"
|
2016-10-04 15:42:36 +05:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
// local
|
2016-10-06 13:55:03 +05:00
|
|
|
"github.com/pztrn/urtrator/cache"
|
2016-10-11 13:14:08 +05:00
|
|
|
"github.com/pztrn/urtrator/clipboardwatcher"
|
2016-10-05 20:46:02 +05:00
|
|
|
"github.com/pztrn/urtrator/colorizer"
|
2016-10-04 15:42:36 +05:00
|
|
|
"github.com/pztrn/urtrator/configuration"
|
|
|
|
"github.com/pztrn/urtrator/database"
|
|
|
|
"github.com/pztrn/urtrator/eventer"
|
|
|
|
"github.com/pztrn/urtrator/launcher"
|
|
|
|
"github.com/pztrn/urtrator/requester"
|
2016-12-13 08:01:48 +05:00
|
|
|
"github.com/pztrn/urtrator/timer"
|
2016-10-04 15:42:36 +05:00
|
|
|
|
|
|
|
// Github
|
|
|
|
"github.com/mattn/go-gtk/gtk"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Context struct {
|
2016-10-06 13:55:03 +05:00
|
|
|
// Caching.
|
|
|
|
Cache *cache.Cache
|
2016-10-11 13:14:08 +05:00
|
|
|
// Clipboard watcher.
|
|
|
|
Clipboard *clipboardwatcher.ClipboardWatcher
|
2016-10-05 20:46:02 +05:00
|
|
|
// Colors parser and prettifier.
|
|
|
|
Colorizer *colorizer.Colorizer
|
2016-10-04 15:42:36 +05:00
|
|
|
// Configuration.
|
|
|
|
Cfg *configuration.Config
|
|
|
|
// Database.
|
|
|
|
Database *database.Database
|
|
|
|
// Eventer.
|
|
|
|
Eventer *eventer.Eventer
|
|
|
|
// Game launcher.
|
|
|
|
Launcher *launcher.Launcher
|
|
|
|
// Requester, which requests server's information.
|
|
|
|
Requester *requester.Requester
|
2016-12-13 08:01:48 +05:00
|
|
|
// Timer.
|
|
|
|
Timer *timer.Timer
|
2016-10-04 15:42:36 +05:00
|
|
|
}
|
|
|
|
|
2016-10-06 22:19:03 +05:00
|
|
|
func (ctx *Context) Close() error {
|
2016-10-04 15:42:36 +05:00
|
|
|
fmt.Println("Closing URTrator...")
|
|
|
|
|
2016-10-06 22:19:03 +05:00
|
|
|
launched := ctx.Launcher.CheckForLaunchedUrbanTerror()
|
|
|
|
if launched != nil {
|
|
|
|
return errors.New("Urban Terror is launched!")
|
|
|
|
}
|
2016-10-08 19:57:33 +05:00
|
|
|
ctx.Cache.FlushProfiles(map[string]string{})
|
|
|
|
ctx.Cache.FlushServers(map[string]string{})
|
2016-10-04 15:42:36 +05:00
|
|
|
ctx.Database.Close()
|
|
|
|
|
|
|
|
// At last, close main window.
|
|
|
|
gtk.MainQuit()
|
2016-10-06 22:19:03 +05:00
|
|
|
return nil
|
2016-10-04 15:42:36 +05:00
|
|
|
}
|
|
|
|
|
2016-10-06 13:55:03 +05:00
|
|
|
func (ctx *Context) initializeCache() {
|
|
|
|
ctx.Cache = cache.New(ctx.Database, ctx.Eventer)
|
|
|
|
ctx.Cache.Initialize()
|
2016-10-11 13:14:08 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) InitializeClipboardWatcher() {
|
2016-10-12 19:23:10 +05:00
|
|
|
ctx.Clipboard = clipboardwatcher.New(ctx.Cache, ctx.Eventer)
|
2016-10-11 13:14:08 +05:00
|
|
|
ctx.Clipboard.Initialize()
|
2016-10-06 13:55:03 +05:00
|
|
|
}
|
|
|
|
|
2016-10-05 20:46:02 +05:00
|
|
|
func (ctx *Context) initializeColorizer() {
|
|
|
|
ctx.Colorizer = colorizer.New()
|
|
|
|
ctx.Colorizer.Initialize()
|
|
|
|
}
|
|
|
|
|
2016-10-04 15:42:36 +05:00
|
|
|
func (ctx *Context) initializeConfig() {
|
|
|
|
ctx.Cfg = configuration.New()
|
|
|
|
ctx.Cfg.Initialize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) initializeDatabase() {
|
|
|
|
ctx.Database = database.New(ctx.Cfg)
|
2016-10-05 01:03:46 +05:00
|
|
|
ctx.Database.Initialize(ctx.Cfg)
|
2016-10-04 15:42:36 +05:00
|
|
|
ctx.Database.Migrate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) initializeEventer() {
|
|
|
|
ctx.Eventer = eventer.New()
|
|
|
|
ctx.Eventer.Initialize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) initializeLauncher() {
|
|
|
|
ctx.Launcher = launcher.New()
|
|
|
|
ctx.Launcher.Initialize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) initializeRequester() {
|
2016-12-13 08:01:48 +05:00
|
|
|
ctx.Requester = requester.New(ctx.Cache, ctx.Eventer, ctx.Cfg, ctx.Timer)
|
2016-10-04 15:42:36 +05:00
|
|
|
ctx.Requester.Initialize()
|
|
|
|
}
|
|
|
|
|
2016-12-13 08:01:48 +05:00
|
|
|
func (ctx *Context) initializeTimer() {
|
|
|
|
ctx.Timer = timer.New(ctx.Eventer, ctx.Cfg)
|
|
|
|
ctx.Timer.Initialize()
|
|
|
|
}
|
|
|
|
|
2016-10-04 15:42:36 +05:00
|
|
|
func (ctx *Context) Initialize() {
|
|
|
|
fmt.Println("Initializing application context...")
|
2016-10-05 20:46:02 +05:00
|
|
|
ctx.initializeColorizer()
|
2016-10-04 15:42:36 +05:00
|
|
|
ctx.initializeConfig()
|
|
|
|
ctx.initializeDatabase()
|
|
|
|
ctx.initializeEventer()
|
2016-10-06 13:55:03 +05:00
|
|
|
ctx.initializeCache()
|
2016-10-04 15:42:36 +05:00
|
|
|
ctx.initializeLauncher()
|
2016-12-13 08:01:48 +05:00
|
|
|
ctx.initializeTimer()
|
2016-10-04 15:42:36 +05:00
|
|
|
ctx.initializeRequester()
|
|
|
|
}
|