""" Retromancer configuration. """ import os import sys try: import wx import Keystroke except: print "* Warning: Failed to import wx in Config" import traceback import pickle from Global import Log, LogException class KeyConfig: """ Constants: Names of the buttons available for keyboard configuration """ ButtonA = 0 ButtonB = 1 Select = 2 Start = 3 Up = 4 Down = 5 Left = 6 Right = 7 ButtonX = 8 ButtonY = 9 LeftTrigger = 10 RightTrigger = 11 QuestReset = 12 QuestAbort = 13 KeyCount = 14 Names = {ButtonA: "Button A", ButtonB: "Button B", Start: "Start", Select: "Select", Up: "Up", Down: "Down", Left: "Left", Right: "Right", ButtonX: "Button X", ButtonY: "Button Y", LeftTrigger: "Left trigger", RightTrigger: "RightTrigger", QuestReset: "Restart quest", QuestAbort: "Quit", } class DirectoryConfig: NES = 0 Arcade = 1 GBA = 2 DirCount = 3 Names = {NES: "NES ROMs", Arcade: "Arcade ROMs", GBA: "GBA ROMs"} def GetFixedInstructions(InstructionsString, Config): """ Given InstructionsString, replace placeholders ($A and $B and so forth) with configured key names. """ if not InstructionsString: return "" PrevDollar = -1 PrevTagEnd = 0 NewInstructions = "" while (1): DollarPos = InstructionsString.find("$", PrevDollar + 1) if DollarPos == -1: NewInstructions += InstructionsString[PrevTagEnd:] break # Try to substitute in another character: TagEndPos = DollarPos + 1 while (TagEndPos < len(InstructionsString)): if InstructionsString[TagEndPos] in " \r\n\t": break TagEndPos += 1 Tag = InstructionsString[DollarPos:TagEndPos].upper() NewInstructions += InstructionsString[PrevTagEnd:DollarPos] TagStr = Config.GetTagReplacement(Tag) NewInstructions += TagStr PrevTagEnd = TagEndPos PrevDollar = DollarPos return NewInstructions class NecroConfig: """ Singleton class for user-configurable settings. """ FileName = "Retromancer.cfg" Version = 1 def __init__(self): # Default: self.Initialize() def GetTagReplacement(self, Tag): """ Given a tag of the form $NAME, return the name of the keystroke for the button. """ KeyCode = None LameName = "<%s>"%(Tag[1].upper() + Tag[2:].lower()) if Tag == "$A": KeyCode = self.KeyConfig[KeyConfig.ButtonA] elif Tag == "$B": KeyCode = self.KeyConfig[KeyConfig.ButtonB] elif Tag == "$START": KeyCode = self.KeyConfig[KeyConfig.Start] elif Tag == "$SELECT": KeyCode = self.KeyConfig[KeyConfig.Select] elif Tag == "$X": KeyCode = self.KeyConfig[KeyConfig.ButtonX] elif Tag == "$Y": KeyCode = self.KeyConfig[KeyConfig.ButtonY] return Keystroke.KeystrokeNames.get(KeyCode, LameName) def Initialize(self): """ Set our configuration to reasonable default values: """ self.KeyConfig = [wx.WXK_ALT, wx.WXK_CONTROL, # A and B wx.WXK_TAB, wx.WXK_RETURN, # Select and start wx.WXK_UP, wx.WXK_DOWN, wx.WXK_LEFT, wx.WXK_RIGHT, # Directions ord("Z"), ord("X"), # X and Y ord("A"), ord("S"), # Left-trigger and right-trigger ord("R"), wx.WXK_ESCAPE, 0, 0] # padding self.Directories = {} self.Directories[DirectoryConfig.NES] = r"c:\emu\nes\NESRen" self.Directories[DirectoryConfig.Arcade] = r"c:\emu\mame\roms" self.Directories[DirectoryConfig.GBA] = r"c:\emu\gba\GBARen" self.CurrentPlayer = None def Save(self): File = open(self.FileName, "wb") pickle.dump(self.Version, File) pickle.dump(self.KeyConfig, File) pickle.dump(self.Directories, File) Log(">>>Config save current player: %s"%self.CurrentPlayer) pickle.dump(self.CurrentPlayer, File) File.close() def Load(self): try: File = open(self.FileName, "rb") except: # No config file? That's ok, we're initialized to defaults. return try: ConfigVersion = pickle.load(File) self.KeyConfig = pickle.load(File) self.Directories = pickle.load(File) self.CurrentPlayer = pickle.load(File) except: LogException() self.Initialize() File.close() # Ensure all members are initialized: return 1