Skip to content

Commit

Permalink
Send version info to server, use init files
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheeo committed Nov 26, 2014
1 parent cae49f3 commit 623a824
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 22 deletions.
15 changes: 10 additions & 5 deletions src/client/_clientwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import random
import notificatation_system as ns

from fa.game_version import GameVersion

try:
from profile import playerstats
except:
Expand Down Expand Up @@ -1775,17 +1777,20 @@ def process(self, action, stream):
except:
logger.error("Error dispatching JSON: " + action, exc_info=sys.exc_info())



def serialize(self, obj):
if isinstance(obj, GameVersion):
return obj.to_dict()
else:
return GameVersion.serialize_kids(obj)

#
# JSON Protocol v2 Implementation below here
#
def send(self, message):
data = json.dumps(message)
if message["command"] == "hello" :
data = json.dumps(message, default=self.serialize)
if message["command"] == "hello":
logger.info("Outgoing JSON Message: login.")
else :
else:
logger.info("Outgoing JSON Message: " + data)
self.writeToServer(data)

Expand Down
1 change: 1 addition & 0 deletions src/config/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"ENGINE_PATH": join(join(APPDATA_DIR, "repo"), "binary-patch"),
"MODS_PATH": join(join(APPDATA_DIR, "repo"), "mods"),
"MAPS_PATH": join(join(APPDATA_DIR, "repo"), "maps"),
"WRITE_GAME_LOG": False
},
'PROXY': {
'HOST': 'proxy.faforever.com',
Expand Down
3 changes: 3 additions & 0 deletions src/fa/game_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ def serialize_kids(obj):
def to_json(self):
return json.dumps(self._versions, default=self.serialize_kids)

def to_dict(self):
return self._versions

@staticmethod
def from_default_version(result):
return GameVersion.from_dict(result)
37 changes: 26 additions & 11 deletions src/fa/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
# GNU General Public License for more details.
#-------------------------------------------------------------------------------
from fa import mods
from fa.init_file import InitFile
from fa.path import getGameFolderFA
from fa.game_version import GameVersion

from .process import instance

Expand All @@ -29,11 +32,11 @@

from PyQt4 import QtCore

settings = QtCore.QSettings("ForgedAllianceForever", "FA Lobby")

from . import DEFAULT_WRITE_GAME_LOG
from . import DEFAULT_RECORD_REPLAY

from config import Settings


def build_argument_list(game_info, port, arguments=None):
"""
Expand All @@ -46,19 +49,18 @@ def build_argument_list(game_info, port, arguments=None):
raise ValueError("Custom init scripts no longer supported.")

#log file
if settings.value("fa.write_game_log", DEFAULT_WRITE_GAME_LOG, type=bool):
arguments.append("/log")
arguments.append('"' + util.LOG_FILE_GAME + '"')
if Settings.get('WRITE_GAME_LOG', 'FA'):
arguments.append(("log", util.LOG_FILE_GAME))

#live replay
arguments.append('/savereplay')
arguments.append('"gpgnet://localhost/' + str(game_info['uid']) + "/" + str(game_info['recorder']) + '.SCFAreplay"')
arguments.append(('savereplay',
'"gpgnet://localhost/' + str(game_info['uid']) + "/" + str(game_info['recorder']) + '.SCFAreplay"'))

#disable bug reporter
arguments.append('/nobugreport')
arguments.append(('nobugreport', None))

#gpg server emulation
arguments.append('/gpgnet 127.0.0.1:' + str(port))
arguments.append(('gpgnet', '127.0.0.1:' + str(port)))

return arguments

Expand All @@ -67,6 +69,19 @@ def run(game_info, port, arguments=None):
"""
Launches Forged Alliance with the given arguments
"""
logger.info("Play received arguments: %s" % arguments)
arguments = build_argument_list(game_info, port, arguments)
return instance.run(game_info, arguments)
init_file = InitFile()
logger.info("Launching with game_info %r" % game_info)
game_version = game_info['version']

init_file.mount(os.path.join(getGameFolderFA(), 'gamedata'), '/')
init_file.mount(game_version.main_mod.path, '/')

init_path = os.path.join(Settings.get('BIN', 'FA'), 'init_%s.lua' % game_version.main_mod.name)
f = file(init_path, 'w')
f.write(init_file.to_lua())
f.close()

arguments.append(('init', init_path))

return instance.run(game_version, arguments, False, init_file)
2 changes: 1 addition & 1 deletion src/fa/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def run(self, info, arguments, detach=False, init_file=None):
"""
#prepare actual command for launching
executable = os.path.join(config.Settings.get('bin_dir', 'fa'), "ForgedAllianceForever.exe")
command = '"' + executable + '" ' + " ".join(arguments)
command = '"' + executable + '" ' + " ".join(map(lambda (k, v): '/%s %s' % (k, v), arguments))

logger.info("Running FA with info: " + str(info))
logger.info("Running FA via command: " + command)
Expand Down
1 change: 1 addition & 0 deletions src/fa/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
@author thygrrr
"""
import os

import time
import shutil
from types import FloatType, IntType, ListType
Expand Down
1 change: 1 addition & 0 deletions src/games/_gameswidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ def launch_game():
self.client.send(dict(command="game_host",
access="password" if self.ispassworded else "public",
password=self.gamepassword,
version=version,
mod=item.mod,
title=self.gamename,
mapname=self.gamemap,
Expand Down
20 changes: 20 additions & 0 deletions tests/fa/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
__author__ = 'Sheeo'

import os
import pytest

from fa.game_version import GameVersion
from fa.mod import Mod
from git import Version

from config import Settings


FAF_PATH = os.path.join(Settings.get('MODS_PATH', 'FA'), 'faf')

@pytest.fixture(scope='function')
def game_version():
return GameVersion(Version('binary-patch', 'master', None, 'a41659780460fd8829fce87b479beaa8ac78e474'),
Mod('faf', FAF_PATH, Version('faf', '3634', None, 'ed052486a19f7adc1adb3f65451af1a7081d2339')),
[],
'')
39 changes: 34 additions & 5 deletions tests/fa/test_play.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,50 @@
from fa.init_file import InitFile

__author__ = 'Sheeo'

from flexmock import flexmock

process_mock = flexmock()
settings_mock = flexmock()

import fa
import fa.play

fa.play.instance = process_mock
fa.play.Settings = settings_mock


def test_launches_process_with_given_arguments(game_version, tmpdir):
game_info = {
'uid': 0,
'recorder': 'Sheeo',
'version': game_version
}
expected_args = [('some-arg', 'test')]

def validate_args(game_info, args, detach, init_file):
for k, v in expected_args:
assert k in dict(args).keys()
assert dict(args)[k] == v
return True

settings_mock.should_receive('get').with_args('WRITE_GAME_LOG', 'FA').and_return(False)
settings_mock.should_receive('get').with_args('BIN', 'FA').and_return(str(tmpdir))
process_mock.should_receive('run').replace_with(validate_args)
assert fa.run(game_info, 0, expected_args)


def test_launches_process_with_given_arguments():
def test_constructs_and_uses_init_file_from_game_version(game_version, tmpdir):
game_info = {
'uid': 0,
'recorder': 'Sheeo'
'recorder': 'Sheeo',
'version': game_version
}
args = ['/some-arg', 'test']
process_mock.should_receive('run').with_args(game_info, args).and_return(True).once()
assert fa.run(game_info, 0, args)
settings_mock.should_receive('get').with_args('WRITE_GAME_LOG', 'FA').and_return(False)
settings_mock.should_receive('get').with_args('BIN', 'FA').and_return(str(tmpdir))

def validate_args(game_info, args, detach, init_file):
assert dict(args)['init'] == str(tmpdir.join('init_%s.lua' % game_version.main_mod.name))
return True
process_mock.should_receive('run').replace_with(validate_args).once()
assert fa.run(game_info, 0)

1 comment on commit 623a824

@Xinnony
Copy link

@Xinnony Xinnony commented on 623a824 Dec 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#138 ?

Please sign in to comment.