-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
executable file
·136 lines (112 loc) · 4.76 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# main.py
#
# Copyright 2015 Elia Argentieri <elia.argentieri@openmailbox.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import pygame
import argparse
import traceback
import logging
import os
import sys
import gettext
import utils
import resources
os.chdir(os.path.dirname(os.path.abspath(__file__)))
VERSION = utils.get_version()
# Gettext work around for Windows
if sys.platform.startswith('win'):
logging.debug('Windows detected')
import locale
if os.getenv('LANG') is None:
logging.debug('Windows did not provide the LANG environment variable.')
lang, enc = locale.getdefaultlocale()
os.environ['LANG'] = lang
logging.debug('Language: %s' % lang)
gettext.install('ice-emblem', resources.LOCALE_PATH) # load translations
# command-line argument parsing
parser = argparse.ArgumentParser(description=_('Ice Emblem, the free software clone of Fire Emblem'))
parser.add_argument('--version', action='version', version='Ice Emblem ' + VERSION)
parser.add_argument('-s', '--skip', action='store_true', help=_('Skip main menu'), required=False)
parser.add_argument('-m', '--map', action='store', help=_('Which map to load'), default=None, required=False)
parser.add_argument('-l', '--logging', action='store', help=_('Choose logging level'), default=20, type=int,
required=False)
parser.add_argument('-d', '--debug', action='store_const', help=_('Debug mode'), const=0, dest='logging')
parser.add_argument('-f', '--file', action='store', help=_('Log file'), default=None, required=False)
args = parser.parse_args()
# log to screen
logging.basicConfig(level=args.logging, filename=args.file, filemode='a')
logging.info(_('Welcome to %s!') % ('Ice Emblem ' + VERSION))
logging.info(_('You are using Pygame version %s.') % pygame.version.ver)
if pygame.version.vernum < (2, 0, 0):
logging.warning(_('You are running a version of Pygame that might be outdated.'))
logging.warning(_('Ice Emblem is tested only with Pygame 2.0.0+.'))
def launch():
import display
display.initialize()
import game
map_file = None
if args.map is not None:
map_file = resources.map_path(args.map)
logging.debug(_('Loading map: %s') % map_file)
elif args.skip:
map_file = resources.map_path('default.tmx')
logging.debug(_('Loading default map: %s') % map_file)
else:
logging.debug(_('No map on command line: choose the map via the main menu'))
game.play(map_file)
if args.logging < 20:
# When in debug mode launch without kind error message
launch()
else:
try:
launch()
except (KeyboardInterrupt, SystemExit):
# game was interrupted by the user
print(_("Interrupted by user, exiting."))
# we're not playing anymore, go away
utils.return_to_os()
except:
# other error
kind_error_message = _("Oops, something went wrong. Dumping brain contents:\n"
"\n"
"%s\n"
"%s\n"
"%s\n"
"\n"
"Please open a report on our issue tracker located at %s\n"
"along with a short description of what you did when this crash happened\n"
"so that the error can be fixed.\n"
"\n"
"Thank you!\n"
"-- the Ice Emblem team\n"
) % ('-' * 80 + '\n', traceback.format_exc(), '-' * 80,
"https://gitlab.com/Elinvention/ice-emblem/issues")
print(kind_error_message)
fname = args.file if args.file else "traceback.log"
with open(fname, 'a') as f:
traceback.print_exc(file=f)
f.write('\n' + '-' * 80 + '\n\n')
# we're not playing anymore, go away
utils.return_to_os()
# we got here, so everything was normal
print()
print("-" * 80)
print(_("Game terminated normally."))
utils.return_to_os()