From 4988ab6ae18aede0c8d8950acc9be3fdc0bf64ac Mon Sep 17 00:00:00 2001 From: Ammar Askar Date: Mon, 31 Dec 2012 07:20:06 +0500 Subject: [PATCH] Added optional colorama dependency to allow colours in windows console Added flag to disable ansi colours. Fixes #10 Now passing plugin loader to plugins onEnable --- README.md | 2 +- networking/DataUtil.py | 1 + networking/NetworkManager.py | 14 +++++++------- pluginloader.py | 18 ++++++++++-------- plugins/IRC.py | 2 +- plugins/PacketDumper.py | 2 +- start.py | 9 +++++++++ 7 files changed, 30 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index b3a63e6..cde6e28 100755 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ In your plugin.py file define a single class with the exact same spellings as yo things happen in the minecraft world. See the [PacketDumper plugin](https://github.com/ammaraskar/pyCraft/blob/master/plugins/PacketDumper.py) as an example. Currently these methods are used: -```onEnable(self, parser)``` This fires when your plugin is loaded and passes the option parser as an argument allowing you to add custom command line options +```onEnable(self, parser, pluginloader)``` This fires when your plugin is loaded and passes the option parser as an argument allowing you to add custom command line options ```onDisable(self)``` This fires when pyCraft is exited cleanly, use it for cleaning up diff --git a/networking/DataUtil.py b/networking/DataUtil.py index ae296e3..3299512 100644 --- a/networking/DataUtil.py +++ b/networking/DataUtil.py @@ -3,6 +3,7 @@ import types from io import BytesIO from pynbt import NBTFile + def readBoolean(FileObject): return struct.unpack('?', FileObject.read(1))[0] diff --git a/networking/NetworkManager.py b/networking/NetworkManager.py index 7390c5f..4321940 100644 --- a/networking/NetworkManager.py +++ b/networking/NetworkManager.py @@ -184,11 +184,8 @@ class PacketListener(threading.Thread): if (response == ""): continue except Exception, e: - if (self.window): - self.window.Status.SetLabel("Ping timeout") - else: - print "Ping timeout" - sys.exit() + print "Ping timeout" + sys.exit() break if (response == "\x00"): packet = PacketListenerManager.handle00(self.FileObject, self.socket) @@ -198,8 +195,11 @@ class PacketListener(threading.Thread): elif (response == "\x03"): packet = PacketListenerManager.handle03(self.FileObject) # Add "\x1b" because it is essential for ANSI escapes emitted by translate_escapes - filtered_string = filter(lambda x: x in string.printable + "\x1b", - Utils.translate_escapes(packet['Message'])) + if not self.connection.options.disableAnsiColours: + filtered_string = filter(lambda x: x in string.printable + "\x1b", + Utils.translate_escapes(packet['Message'])) + else: + filtered_string = filter(lambda x: x in string.printable, packet['Message']) print filtered_string elif (response == "\x04"): diff --git a/pluginloader.py b/pluginloader.py index 8b412b3..029adbe 100644 --- a/pluginloader.py +++ b/pluginloader.py @@ -3,13 +3,12 @@ import imp class PluginLoader(): path = "" - plugins = [] + plugins = {} listeners = [] def __init__(self, path): self.path = path - def loadPlugins(self, parser): for item in os.listdir(self.path): split = os.path.splitext(item) @@ -21,9 +20,9 @@ class PluginLoader(): pluginClass = None try: pluginClass = getattr(m, name)() - self.plugins.append(pluginClass) + self.plugins[name] = pluginClass try: - pluginClass.onEnable(parser) + pluginClass.onEnable(parser, self) except AttributeError: pass try: @@ -34,28 +33,31 @@ class PluginLoader(): print "Plugin " + name + " is malformed" def disablePlugins(self): - for plugin in self.plugins: + for plugin in self.plugins.values(): try: plugin.onDisable() except AttributeError: pass def notifyOptions(self, options): - for plugin in self.plugins: + for plugin in self.plugins.values(): try: plugin.optionsParsed(options) except AttributeError: pass def notify(self, methodName): - for plugin in self.plugins: + for plugin in self.plugins.values(): try: getattr(plugin, methodName)() except AttributeError: pass def getPlugins(self): - return self.plugins + return self.plugins.values() + + def getPlugin(self, name): + return self.plugins[name] def getPacketListeners(self): return self.listeners diff --git a/plugins/IRC.py b/plugins/IRC.py index 3f87143..d967f6f 100644 --- a/plugins/IRC.py +++ b/plugins/IRC.py @@ -2,7 +2,7 @@ class IRC: options = None writeFile = None - def onEnable(self, parser): + def onEnable(self, parser, pluginloader): parser.add_option("-q", "--irc-out-file", dest="ircDump", default="ircdump.txt", help="file to dump messages to") diff --git a/plugins/PacketDumper.py b/plugins/PacketDumper.py index 62d378d..de53469 100644 --- a/plugins/PacketDumper.py +++ b/plugins/PacketDumper.py @@ -7,7 +7,7 @@ class PacketDumper: options = None writeFile = None - def onEnable(self, parser): + def onEnable(self, parser, pluginloader): parser.add_option("-d", "--dump-packets", action="store_true", dest="dumpPackets", default=False, help="run with this argument to dump packets") diff --git a/start.py b/start.py index 76627c1..f415bb6 100644 --- a/start.py +++ b/start.py @@ -4,6 +4,11 @@ import Utils from pluginloader import PluginLoader from networking import PacketSenderManager, NetworkManager from optparse import OptionParser +try: + import colorama + colorama.init() +except ImportError: + pass if __name__ == "__main__": parser = OptionParser() @@ -21,6 +26,10 @@ if __name__ == "__main__": action="store_true", default=False, help="run in offline mode i.e don't attempt to auth via minecraft.net") + parser.add_option("-c", "--disable-console-colours", dest="disableAnsiColours", + action="store_true", default=False, + help="print minecraft chat colours as their equivalent ansi colours") + # pluginLoader pluginLoader = PluginLoader("plugins") pluginLoader.loadPlugins(parser)