From 3b0d16027ea785e288d6315cb763faf7549c1973 Mon Sep 17 00:00:00 2001 From: "FunnyMan3595 (Charlie Nolan)" Date: Wed, 10 Oct 2007 21:42:45 -0500 Subject: [PATCH] Rewrite continues, mod importing moved to mod_import. --- args.py | 5 ++++- engine/items.py | 13 ++----------- engine/map_functions.py | 14 ++------------ engine/maps.py | 20 +++++--------------- engine/mod_import.py | 14 ++++++++++++++ engine/monsters.py | 14 +++----------- engine/spells.py | 13 ++----------- 7 files changed, 32 insertions(+), 61 deletions(-) rewrite engine/map_functions.py (100%) rewrite engine/maps.py (88%) create mode 100644 engine/mod_import.py diff --git a/args.py b/args.py index a7d95e4..760e682 100644 --- a/args.py +++ b/args.py @@ -1,3 +1,6 @@ from sys import argv -mod = argv[-1] +if len(argv) > 1: + mod = argv[-1] +else: + mod = "" diff --git a/engine/items.py b/engine/items.py index a222d06..af0188a 100644 --- a/engine/items.py +++ b/engine/items.py @@ -1,11 +1,2 @@ -from args import mod - -try: - items = __import__('mods.%s.items' % mod, globals(), locals(), ['']) -except ImportError: - import main_game.items as items - -# Yes, this is ugly. Unfortunately, this is the only way to emulate -# from mods..items import *. Since this is the cleanest way to use -# monsters, items, etc., this is what we do. -globals().update(items.__dict__) +from mod_import import do_mod_import +do_mod_import("items", globals()) diff --git a/engine/map_functions.py b/engine/map_functions.py dissimilarity index 100% index c2a64f4..1f76f12 100644 --- a/engine/map_functions.py +++ b/engine/map_functions.py @@ -1,12 +1,2 @@ -from args import mod -from random import choice as randchoice - -try: - map_function_module = __import__('mods.%s.map_functions' % mod, globals(), locals(), ['']) -except ImportError: - import main_game.map_functions as map_function_module - -# Yes, this is ugly. Unfortunately, this is the only way to emulate -# from mods.. import *. Since this is the cleanest way to use -# monsters, items, etc., this is what we do. -globals().update(map_function_module.__dict__) +from mod_import import do_mod_import +do_mod_import("map_functions", globals()) diff --git a/engine/maps.py b/engine/maps.py dissimilarity index 88% index 71539e8..8477276 100644 --- a/engine/maps.py +++ b/engine/maps.py @@ -1,15 +1,5 @@ -from args import mod -from random import choice as randchoice - -try: - map_module = __import__('mods.%s.maps' % mod, globals(), locals(), ['']) -except ImportError: - import main_game.maps as map_module - -# Yes, this is ugly. Unfortunately, this is the only way to emulate -# from mods..monsters import *. Since this is the cleanest way to use -# monsters, items, etc., this is what we do. -globals().update(map_module.__dict__) - -for game_map in maps.values(): - game_map.validate(maps) +from mod_import import do_mod_import +do_mod_import("maps", globals()) + +for game_map in maps.values(): + game_map.validate(maps) diff --git a/engine/mod_import.py b/engine/mod_import.py new file mode 100644 index 0000000..8554710 --- /dev/null +++ b/engine/mod_import.py @@ -0,0 +1,14 @@ +# This file handles importing modules from a specific mod, or, if the module is +# not present, from the main game. +from args import mod + +def do_mod_import(module, globals): + try: + module = __import__('mods.%s.%s' % (mod, module), globals, locals(), ['']) + except ImportError: + module = __import__('main_game.%s' % module, globals, locals(), ['']) + + # Yes, this is ugly. Unfortunately, this is the only way to emulate + # from mods.. import *. Since this is the cleanest way to + # use monsters, items, etc., this is what we do. + globals.update(module.__dict__) diff --git a/engine/monsters.py b/engine/monsters.py index 93258df..63f20d0 100644 --- a/engine/monsters.py +++ b/engine/monsters.py @@ -1,15 +1,7 @@ -from args import mod -from random import choice as randchoice - -try: - monster_module = __import__('mods.%s.monsters' % mod, globals(), locals(), ['']) -except ImportError: - import main_game.monsters as monster_module +from mod_import import do_mod_import +do_mod_import("monsters", globals()) -# Yes, this is ugly. Unfortunately, this is the only way to emulate -# from mods..monsters import *. Since this is the cleanest way to use -# monsters, items, etc., this is what we do. -globals().update(monster_module.__dict__) +from random import choice as randchoice class monster_db(object): def __init__(self): diff --git a/engine/spells.py b/engine/spells.py index 98eaf61..a48ddce 100644 --- a/engine/spells.py +++ b/engine/spells.py @@ -1,11 +1,2 @@ -from args import mod - -try: - spells = __import__('mods.%s.spells' % mod, globals(), locals(), ['']) -except ImportError: - import main_game.spells as spells - -# Yes, this is ugly. Unfortunately, this is the only way to emulate -# from mods..spells import *. Since this is the cleanest way to use -# monsters, items, etc., this is what we do. -globals().update(spells.__dict__) +from mod_import import do_mod_import +do_mod_import("spells", globals()) -- 2.11.4.GIT