# Copyright (C) 2006-2012 Mortal Coil Games # See LICENSE for details. """ This module provides a Twisted plugin which can run an MV3D server. """ import os import sys from StringIO import StringIO from ConfigParser import ConfigParser from zope.interface import implements #@UnresolvedImport from twisted.plugin import IPlugin from twisted.application.service import IServiceMaker from twisted.python.usage import Options from twisted.internet import reactor class MV3DOptions(Options): """ Command-line options for the MV3D server. """ optParameters = [ ('config-file', 'c', "server.conf", 'The file to load configuration from.'), ('config-name', 'm', "Server", 'The section name in the config file to read from.'), ('port', 'p', 1999, 'Set the Port to listen on.'), ('worldgen-factory', 'f', 'mv3d.server.worldgen.WorldGenerator', 'The class of a factory to use to bootstrap the server\'s world'), ('worldgen-template', 'w', None, 'A template file to use to initialize' 'the world from.'), ('import-from', 'I', None, 'Frozen file to import the world from'), ('num-players', 'n', 0, 'Set the number of players to create if initialize=1'), ('num-cubes', 'C', 0, 'Set the number of cubes to create if initialize=1'), ('terrain-size', 't', 0, 'Number of terrains to create'), ('trees', 'T', 0, 'Set the number of trees'), ] optFlags = [ ('initialize', 'i', 'Initialize a world'), ('grass', 'g', 'Enable grass'), ] class MV3DServerPlugin(object): """ A very basic Twisted plugin for the mv3d server. """ implements(IServiceMaker, IPlugin) tapname = "mv3d" description = "MV3D Server" options = MV3DOptions def makeService(self, mv3dOptions): """ Create a Twisted service based on the mv3dOptions instance provided to us. """ from mv3d.util.conductor import Conductor from mv3d.server.worldgen import setupServer conductor = Conductor() cfg = ConfigParser() configFile = None if mv3dOptions["config-file"] != "-": cfg.read(mv3dOptions["config-file"]) cfg.read(os.path.join("conf", mv3dOptions["config-file"])) name = mv3dOptions["config-name"] configFile = mv3dOptions["config-file"] else: name = sys.stdin.readline().strip() cfg.readfp(StringIO(sys.stdin.read())) conductor.configure(name, cfg, configFile) reactor.callLater(0.2, setupServer, conductor, #@UndefinedVariable mv3dOptions) return conductor thePlugin = MV3DServerPlugin()