#!/usr/bin/python -u

# Dedicated Control handler script for OpenLieroX
# (http://openlierox.sourceforge.net)

import os, sys, time

# Print Python script errors to external file -
# on Windows it cannot print errors to console
# We should start logging as soon as possible, or we won't get errors about invalid config file
if sys.platform == "win32":
	sys.stderr = open("dedicated_control_errors.log", "w", 0)

# Add current dir to module search path
sys.path.append( os.getcwd() )
sys.path.append( os.path.dirname(sys.argv[0]) )

import dedicated_control_io as io # control handler

cfg_name = "cfg/dedicated_config"
if len(sys.argv) > 1:
	cfg_name = sys.argv[1]

# Add dir with config file to module search path
try:
	sys.path.append(os.path.dirname(io.getFullFileName(cfg_name+".py")))
except:
	pass

exec("import %s as cfg" % os.path.basename(cfg_name)) # Per-host config
__builtins__.cfg = cfg # Make it accessible from all modules

import dedicated_control_handler as hnd # control handler


## The game loop ##

hnd.init()

io.messageLog("Dedicated_control started",io.LOG_INFO)

# main loop
while hnd.gameState != hnd.GAME_QUIT:
	hnd.signalHandler(io.getSignal())

io.messageLog("Dedicated_control stopped",io.LOG_INFO)
