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 launcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
// stdlib
|
2016-10-06 22:19:03 +05:00
|
|
|
"errors"
|
2016-10-04 15:42:36 +05:00
|
|
|
"fmt"
|
2016-10-04 18:09:14 +05:00
|
|
|
"os"
|
2016-10-04 15:42:36 +05:00
|
|
|
"os/exec"
|
2016-10-09 13:15:19 +05:00
|
|
|
"runtime"
|
2016-10-04 18:09:14 +05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
// local
|
|
|
|
"github.com/pztrn/urtrator/datamodels"
|
2016-10-06 22:19:03 +05:00
|
|
|
|
|
|
|
// Github
|
|
|
|
"github.com/mattn/go-gtk/gtk"
|
2016-10-04 15:42:36 +05:00
|
|
|
)
|
|
|
|
|
2016-10-06 22:19:03 +05:00
|
|
|
type Launcher struct {
|
|
|
|
// Flags.
|
|
|
|
// Is Urban Terror launched ATM?
|
|
|
|
launched bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Launcher) CheckForLaunchedUrbanTerror() error {
|
|
|
|
if l.launched {
|
|
|
|
mbox_string := "Game is launched.\n\nCannot quit, because game is launched.\nQuit Urban Terror to exit URTrator!"
|
|
|
|
m := gtk.NewMessageDialog(nil, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, mbox_string)
|
|
|
|
m.Response(func() {
|
|
|
|
m.Destroy()
|
|
|
|
})
|
|
|
|
m.Run()
|
|
|
|
return errors.New("User didn't select valid profile, mismatch with server's version.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-10-04 15:42:36 +05:00
|
|
|
|
2016-10-04 18:09:14 +05:00
|
|
|
func (l *Launcher) findFreeDisplay() string {
|
|
|
|
current_display_raw := os.Getenv("DISPLAY")
|
|
|
|
current_display, _ := strconv.Atoi(strings.Split(current_display_raw, ":")[1])
|
|
|
|
current_display += 1
|
|
|
|
return strconv.Itoa(current_display)
|
|
|
|
}
|
|
|
|
|
2016-10-04 15:42:36 +05:00
|
|
|
func (l *Launcher) Initialize() {
|
|
|
|
fmt.Println("Initializing game launcher...")
|
|
|
|
}
|
|
|
|
|
2016-10-08 19:57:33 +05:00
|
|
|
func (l *Launcher) Launch(server_profile *datamodels.Server, user_profile *datamodels.Profile, password string, additional_parameters []string, callback func()) {
|
2016-10-04 15:42:36 +05:00
|
|
|
// ToDo: only one instance of Urban Terror should be launched, so button
|
|
|
|
// should be disabled.
|
|
|
|
fmt.Println("Launching Urban Terror...")
|
|
|
|
|
|
|
|
done := make(chan bool, 1)
|
2016-10-04 18:09:14 +05:00
|
|
|
|
2016-10-04 15:42:36 +05:00
|
|
|
// Create launch string.
|
2016-10-04 18:09:14 +05:00
|
|
|
var launch_bin string = ""
|
2016-10-08 19:57:33 +05:00
|
|
|
launch_bin, err := exec.LookPath(user_profile.Binary)
|
2016-10-04 18:09:14 +05:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
|
2016-10-08 19:57:33 +05:00
|
|
|
server_address := server_profile.Ip + ":" + server_profile.Port
|
|
|
|
|
2016-10-04 18:09:14 +05:00
|
|
|
var launch_params []string
|
2016-10-08 19:57:33 +05:00
|
|
|
if len(server_address) > 0 {
|
|
|
|
launch_params = append(launch_params, "+connect", server_address)
|
2016-10-04 18:09:14 +05:00
|
|
|
}
|
2016-10-04 15:42:36 +05:00
|
|
|
if len(password) > 0 {
|
2016-10-04 18:09:14 +05:00
|
|
|
launch_params = append(launch_params, "+password", password)
|
|
|
|
}
|
2016-10-08 19:57:33 +05:00
|
|
|
if len(user_profile.Additional_params) > 0 {
|
|
|
|
additional_params := strings.Split(user_profile.Additional_params, " ")
|
2016-10-04 18:09:14 +05:00
|
|
|
launch_params = append(launch_params, additional_params...)
|
|
|
|
}
|
2016-10-08 19:57:33 +05:00
|
|
|
if len(additional_parameters) > 0 {
|
|
|
|
for i := range additional_parameters {
|
|
|
|
launch_params = append(launch_params, additional_parameters[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if user_profile.Second_x_session == "1" {
|
2016-10-04 18:09:14 +05:00
|
|
|
fmt.Println(launch_params)
|
|
|
|
launch_params = append([]string{launch_bin}, launch_params...)
|
|
|
|
display := l.findFreeDisplay()
|
|
|
|
launch_bin, err = exec.LookPath("xinit")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
launch_params = append(launch_params, "--", ":" + display)
|
2016-10-04 15:42:36 +05:00
|
|
|
}
|
2016-10-04 18:09:14 +05:00
|
|
|
fmt.Println(launch_params)
|
2016-10-04 15:42:36 +05:00
|
|
|
go func() {
|
|
|
|
go func() {
|
2016-10-04 18:09:14 +05:00
|
|
|
cmd := exec.Command(launch_bin, launch_params...)
|
|
|
|
out, err1 := cmd.Output()
|
|
|
|
if err1 != nil {
|
|
|
|
fmt.Println("Launch error: " + err1.Error())
|
2016-10-04 15:42:36 +05:00
|
|
|
}
|
2016-10-04 18:09:14 +05:00
|
|
|
fmt.Println(string(out))
|
2016-10-04 15:42:36 +05:00
|
|
|
done <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <- done:
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|