1
################################################################################
2
# copyright 2008 Gabriel Pettier <gabriel.pettier@gmail.com> #
4
# This file is part of UltimateSmashFriends #
6
# UltimateSmashFriends is free software: you can redistribute it and/or modify #
7
# it under the terms of the GNU General Public License as published by #
8
# the Free Software Foundation, either version 3 of the License, or #
9
# (at your option) any later version. #
11
# UltimateSmashFriends is distributed in the hope that it will be useful, #
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14
# GNU General Public License for more details. #
16
# You should have received a copy of the GNU General Public License #
17
# along with UltimateSmashFriends. If not, see <http://www.gnu.org/licenses/>.#
18
################################################################################
21
from sys import platform
22
from debug_utils import LOG
24
if platform in ('linux2', 'bsd'):
25
from xdg.BaseDirectory import xdg_config_home
27
#xgd is not implemented on windows (how surprising ^^) so this is a trivial
28
#hack to make it work.
33
def open_conf(confname):
34
if 'usf' not in os.listdir(os.path.join(xdg_config_home)):
35
LOG().log('creating new config directory')
36
os.mkdir(os.path.join(xdg_config_home,'usf'))
38
if confname+'.cfg' not in os.listdir(os.path.join(xdg_config_home,'usf')):
39
LOG().log('creating '+confname+' config')
40
conf = open('default_'+confname+'.cfg')
41
config_file = open(os.path.join(xdg_config_home,'usf',confname+'.cfg'),'w')
42
config_file.write(conf.read())
46
return open(os.path.join(xdg_config_home,'usf',confname+'.cfg'))
47
# looks like it's the first launch of the game, we need to create the
52
config_file = open_conf('config')
53
for line in config_file.readlines():
55
# allow various comment syntaxes
56
line = line.split('#')[0].split(';')[0].split('//')[0]
57
# yeah eval is evil, but I guess it exists for such situations
58
config[line.split('=')[0].replace(' ','')] = eval(line.split('=')[1])
61
def load_sound_config():
63
config_file = open_conf('sound')
64
for line in config_file.readlines():
66
# allow various comment syntaxes
67
line = line.split('#')[0].split(';')[0].split('//')[0]
68
# yeah eval is evil, but I guess it exists for such situations
69
config[line.split('=')[0].replace(' ','')] = eval(line.split('=')[1])
72
def load_key_config():
74
keys_file = open_conf('keys')
75
for line in keys_file.readlines():
76
#allow various comment syntaxes
77
line = line.split('#')[0].split(';')[0].split('//')[0].split('\n')[0]
79
a,b=line.replace(' ','').split(":")
80
keyboard_config[pygame.locals.__dict__[b]] = a
81
return keyboard_config
83
#LOG().log("keyboard_config ",keyboard_config)
87
save the general configuration options.
92
# this file contain keybindings of players and of the general game
93
# everything on a line after a # is a comment and is ignored
94
# you can start a comment with a ; too.
96
# The syntax of a binding is simple. the first part is the player concerned.
97
# to bind for the player one, start with "PL1" then a '_' and the action concerned.
98
# a complete list of actions will be inserted here later (#TODO).
100
# The second part of the line is the code of the key associated with the action.
101
# Theses are pygame names, and you can find a list at pygame website:
102
# http://www.pygame.org/docs/ref/key.html
104
# please also see online documentation of keybindings here:
105
# http://code.google.com/p/ultimate-smash-friends/wiki/KeysConfiguration
109
# reversed map of keys, based on pygame/sdl documentation
152
91 : "K_LEFTBRACKET",
154
93 : "K_RIGHTBRACKET",
200
268 : "K_KP_MULTIPLY",
261
def save_sound_conf():
263
save the sound configuration.
266
for key in ('SOUND_VOLUME', 'MUSIC_VOLUME', 'SOUND', 'MUSIC'):
267
conf += str(key)+" = "+str(sound_config[key])+"\n"
269
file=open(os.path.join(xdg_config_home,'usf','sound.cfg'),'w')
273
def save_keys_conf():
275
save the configuration of controls.
278
for key in sorted(keyboard_config.keys()):
279
#conf += "%s : %s\n" % ( self.keys[key], self.reverse_keymap[key])
280
conf += str(keyboard_config[key])+" : "+str(reverse_keymap[key])+"\n"
282
# we sort the keys configuration by player so the file is easier to read.
283
file=open(os.path.join(xdg_config_home,'usf','keys.cfg'),'w')
287
config = load_config()
288
keyboard_config = load_key_config()
289
sound_config= load_sound_config()