Created
July 4, 2017 07:16
-
-
Save Marginal/b2f2f1a428c323e6390b38d2fa194b3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Monitors HUD graphics | |
# | |
from collections import OrderedDict | |
from os.path import basename, isdir, isfile, join | |
from sys import platform | |
from tornado.ioloop import IOLoop | |
from watchdog.observers import Observer | |
from watchdog.events import FileSystemEventHandler | |
from xml.etree import cElementTree as ElementTree | |
if __debug__: | |
from traceback import print_exc | |
class HUDWatcher(FileSystemEventHandler): | |
DEFAULT = OrderedDict([ ('msg', 'colours'), ('hud', 'ff7f00'), ('highlight', 'ffffff') ]) | |
def __init__(self, callback): | |
FileSystemEventHandler.__init__(self) # futureproofing - not need for current version of watchdog | |
if platform == 'darwin': | |
from Foundation import NSSearchPathForDirectoriesInDomains, NSApplicationSupportDirectory, NSUserDomainMask | |
appdata = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, True)[0] | |
elif platform == 'win32': | |
from win32 import shell32 | |
appdata = shell32.SHGetKnownFolderPath(shell32.FOLDERID_LocalAppData) | |
self.folder = appdata and join(appdata, 'Frontier Developments', 'Elite Dangerous', 'Options', 'Graphics') or None | |
self.callback = callback | |
self.observer = None | |
self.msg = self.DEFAULT | |
def get_msg(self): | |
# defer parsing 'til client connects in case file or folder didn't exist when we started | |
if isdir(self.folder) and not self.observer: | |
self.observer = Observer() | |
self.observer.daemon = True | |
self.observer.schedule(self, self.folder) | |
self.observer.start() | |
if isfile(join(self.folder, 'GraphicsConfigurationOverride.xml')): | |
self.parse(join(self.folder, 'GraphicsConfigurationOverride.xml')) | |
return self.msg | |
def stop(): | |
if self.observer: | |
self.observer.join() | |
self.observer = None | |
def on_created(self, event): | |
# watchdog callback | |
if not event.is_directory and basename(event.src_path).lower() == 'graphicsconfigurationoverride.xml': | |
self.parse(event.src_path) | |
IOLoop.current().add_callback(self.callback, self.msg) | |
def on_modified(self, event): | |
# watchdog callback | |
if not event.is_directory and basename(event.src_path).lower() == 'graphicsconfigurationoverride.xml': | |
self.parse(event.src_path) | |
IOLoop.current().add_callback(self.callback, self.msg) | |
def on_deleted(self, event): | |
# watchdog callback | |
if not event.is_directory and basename(event.src_path).lower() == 'graphicsconfigurationoverride.xml': | |
self.msg = self.DEFAULT | |
IOLoop.current().add_callback(self.callback, self.msg) | |
def parse(self, path): | |
def apply(m, r, g, b): | |
out = '' | |
for o in range(3): | |
out = '%02x%s' % (min(max(r * m[0][o] + g * m[1][o] + b * m[2][o], 0), 255), out) | |
return out | |
try: | |
matrix = ElementTree.parse(path).find('GUIColour').find('Default') | |
m = [] | |
for channel in ['MatrixRed', 'MatrixGreen', 'MatrixBlue']: | |
m.append([float(x) for x in matrix.find(channel).text.split(',')]) | |
self.msg = OrderedDict([ | |
('msg', 'colours'), | |
('hud', apply(m, 255, 127, 0)), | |
('highlight', apply(m, 255, 255, 255)), | |
]) | |
except: | |
if __debug__: print_exc() | |
self.msg = self.DEFAULT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment