#!/usr/bin/python -u

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

# We're expecting this file to be run from "scripts" dir - that's from rev. 3260 or so
# TODO: NO, we are not expecting this!
# It was only done because this script is buggy and does not work otherwise.

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)

def MainLoop():
	hnd.signalHandler(io.getSignal())


while hnd.gameState != hnd.GAME_QUIT:
	MainLoop()

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