[18] | 1 | # The main program
|
---|
| 2 |
|
---|
[36] | 3 | from config import Config
|
---|
[107] | 4 | from i18n import setLanguage
|
---|
[133] | 5 | from sound import initializeSound
|
---|
[27] | 6 |
|
---|
[29] | 7 | import os
|
---|
[36] | 8 | import sys
|
---|
[27] | 9 |
|
---|
[36] | 10 | #--------------------------------------------------------------------------------------
|
---|
| 11 |
|
---|
| 12 | class StdIOHandler(object):
|
---|
| 13 | """Handler for the standard I/O messages."""
|
---|
| 14 | def __init__(self, gui):
|
---|
| 15 | """Construct the handler."""
|
---|
| 16 | self._gui = gui
|
---|
| 17 |
|
---|
| 18 | def write(self, text):
|
---|
| 19 | """Write the given text into the log."""
|
---|
| 20 | self._gui.writeStdIO(text)
|
---|
| 21 |
|
---|
| 22 | #--------------------------------------------------------------------------------------
|
---|
| 23 |
|
---|
[155] | 24 | def restart(args = []):
|
---|
| 25 | """Restart the program with the given arguments."""
|
---|
| 26 | print "Restarting with args", args
|
---|
| 27 | programPath = os.path.join(os.path.dirname(sys.argv[0]),
|
---|
| 28 | "runmlx.exe" if os.name=="nt" else "runmlx.sh")
|
---|
| 29 | if os.name=="nt":
|
---|
| 30 | import win32api
|
---|
| 31 | try:
|
---|
| 32 | programPath = win32api.GetShortPathName(programPath)
|
---|
| 33 | except:
|
---|
| 34 | programPath = os.path.join(os.path.dirname(sys.argv[0]),
|
---|
| 35 | "runmlx.bat")
|
---|
| 36 | programPath = win32api.GetShortPathName(programPath)
|
---|
| 37 |
|
---|
| 38 | args = [programPath] + args
|
---|
| 39 |
|
---|
| 40 | os.execv(programPath, args)
|
---|
| 41 |
|
---|
| 42 | #--------------------------------------------------------------------------------------
|
---|
| 43 |
|
---|
[36] | 44 | def main():
|
---|
[29] | 45 | """The main operation of the program."""
|
---|
[182] | 46 | from singleton import SingleInstance, raiseCallbackWrapper
|
---|
| 47 |
|
---|
| 48 | instance = SingleInstance("mlx", raiseCallbackWrapper)
|
---|
| 49 | if not instance: return
|
---|
| 50 |
|
---|
[36] | 51 | programDirectory = os.path.dirname(sys.argv[0])
|
---|
| 52 |
|
---|
| 53 | config = Config()
|
---|
[40] | 54 | config.load()
|
---|
[107] | 55 |
|
---|
[155] | 56 | if (len(sys.argv)<=1 or sys.argv[1]!="usedeflang") and config.setupLocale():
|
---|
| 57 | restart(["usedeflang"])
|
---|
| 58 |
|
---|
[107] | 59 | setLanguage(config.getLanguage())
|
---|
[40] | 60 |
|
---|
[123] | 61 | from .gui.gui import GUI
|
---|
[36] | 62 | gui = GUI(programDirectory, config)
|
---|
[38] | 63 |
|
---|
[36] | 64 | sys.stdout = StdIOHandler(gui)
|
---|
| 65 | sys.stderr = StdIOHandler(gui)
|
---|
| 66 |
|
---|
[133] | 67 | initializeSound(os.path.join(programDirectory, "sounds"))
|
---|
| 68 |
|
---|
[38] | 69 | try:
|
---|
| 70 | gui.build(programDirectory)
|
---|
[160] | 71 |
|
---|
[38] | 72 | gui.run()
|
---|
| 73 | finally:
|
---|
| 74 | gui.flushStdIO()
|
---|
| 75 | sys.stdout = sys.__stdout__
|
---|
| 76 | sys.stderr = sys.__stderr__
|
---|
[27] | 77 |
|
---|
[40] | 78 | config.save()
|
---|
| 79 |
|
---|
[38] | 80 | if gui.toRestart:
|
---|
[155] | 81 | restart()
|
---|
[27] | 82 |
|
---|
[36] | 83 | #--------------------------------------------------------------------------------------
|
---|
| 84 |
|
---|
[20] | 85 | if __name__ == "__main__":
|
---|
[182] | 86 | if instance:
|
---|
| 87 | main()
|
---|