~alaxa27/ultimate-smash-friends/mirror_trunk

« back to all changes in this revision

Viewing changes to pkg/ultimate-smash-friends_1.0-beta-1/usr/lib/usf_modules/config.py

  • Committer: gaby
  • Date: 2009-11-30 17:03:16 UTC
  • Revision ID: gaby@ks22672.kimsufi.com-20091130170316-6lm3v7q0torulfab
adding code package

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
################################################################################
 
2
# copyright 2008 Gabriel Pettier <gabriel.pettier@gmail.com>                   #
 
3
#                                                                              #
 
4
# This file is part of UltimateSmashFriends                                    #
 
5
#                                                                              #
 
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.                                          #
 
10
#                                                                              #
 
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.                                 #
 
15
#                                                                              #
 
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
################################################################################
 
19
 
 
20
import os
 
21
from sys import platform
 
22
from debug_utils import LOG
 
23
# xdg
 
24
if platform in ('linux2', 'bsd'):
 
25
    from xdg.BaseDirectory import xdg_config_home
 
26
else:
 
27
#xgd is not implemented on windows (how surprising ^^) so this is a trivial
 
28
#hack to make it work.
 
29
    xdg_config_home = '.'
 
30
 
 
31
import pygame.locals
 
32
 
 
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'))
 
37
 
 
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())
 
43
        config_file.close()
 
44
        conf.close()
 
45
 
 
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
 
48
        # config file
 
49
 
 
50
def load_config():
 
51
    config = {}
 
52
    config_file = open_conf('config')
 
53
    for line in config_file.readlines():
 
54
        if '=' in line:
 
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])
 
59
    return config
 
60
 
 
61
def load_sound_config():
 
62
    config = {}
 
63
    config_file = open_conf('sound')
 
64
    for line in config_file.readlines():
 
65
        if '=' in line:
 
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])
 
70
    return config
 
71
 
 
72
def load_key_config():
 
73
    keyboard_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]
 
78
        if ':' in line:
 
79
            a,b=line.replace(' ','').split(":")
 
80
            keyboard_config[pygame.locals.__dict__[b]] = a
 
81
    return keyboard_config
 
82
 
 
83
#LOG().log("keyboard_config ",keyboard_config)
 
84
 
 
85
def save_conf():
 
86
    """
 
87
    save the general configuration options.
 
88
    """
 
89
    pass
 
90
 
 
91
str_conf = """
 
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.
 
95
 
 
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).
 
99
 
 
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
 
103
 
 
104
# please also see online documentation of keybindings here:
 
105
# http://code.google.com/p/ultimate-smash-friends/wiki/KeysConfiguration
 
106
"""
 
107
 
 
108
reverse_keymap = {
 
109
    # reversed map of keys, based on pygame/sdl documentation
 
110
    1 : "KMOD_LSHIFT",
 
111
    2 : "KMOD_RSHIFT",
 
112
    3 : "KMOD_SHIFT",
 
113
    8 : "K_BACKSPACE",
 
114
    9 : "K_TAB",
 
115
    12 : "K_CLEAR",
 
116
    13 : "K_RETURN",
 
117
    19 : "K_PAUSE",
 
118
    27 : "K_ESCAPE",
 
119
    32 : "K_SPACE",
 
120
    33 : "K_EXCLAIM",
 
121
    34 : "K_QUOTEDBL",
 
122
    35 : "K_HASH",
 
123
    36 : "K_DOLLAR",
 
124
    38 : "K_AMPERSAND",
 
125
    39 : "K_QUOTE",
 
126
    40 : "K_LEFTPAREN",
 
127
    41 : "K_RIGHTPAREN",
 
128
    42 : "K_ASTERISK",
 
129
    43 : "K_PLUS",
 
130
    44 : "K_COMMA",
 
131
    45 : "K_MINUS",
 
132
    46 : "K_PERIOD",
 
133
    47 : "K_SLASH",
 
134
    48 : "K_0",
 
135
    49 : "K_1",
 
136
    50 : "K_2",
 
137
    51 : "K_3",
 
138
    52 : "K_4",
 
139
    53 : "K_5",
 
140
    54 : "K_6",
 
141
    55 : "K_7",
 
142
    56 : "K_8",
 
143
    57 : "K_9",
 
144
    58 : "K_COLON",
 
145
    59 : "K_SEMICOLON",
 
146
    60 : "K_LESS",
 
147
    61 : "K_EQUALS",
 
148
    62 : "K_GREATER",
 
149
    63 : "K_QUESTION",
 
150
    64 : "KMOD_LCTRL",
 
151
    64 : "K_AT",
 
152
    91 : "K_LEFTBRACKET",
 
153
    92 : "K_BACKSLASH",
 
154
    93 : "K_RIGHTBRACKET",
 
155
    94 : "K_CARET",
 
156
    95 : "K_UNDERSCORE",
 
157
    96 : "K_BACKQUOTE",
 
158
    97 : "K_a",
 
159
    98 : "K_b",
 
160
    99 : "K_c",
 
161
    100 : "K_d",
 
162
    101 : "K_e",
 
163
    102 : "K_f",
 
164
    103 : "K_g",
 
165
    104 : "K_h",
 
166
    105 : "K_i",
 
167
    106 : "K_j",
 
168
    107 : "K_k",
 
169
    108 : "K_l",
 
170
    109 : "K_m",
 
171
    110 : "K_n",
 
172
    111 : "K_o",
 
173
    112 : "K_p",
 
174
    113 : "K_q",
 
175
    114 : "K_r",
 
176
    115 : "K_s",
 
177
    116 : "K_t",
 
178
    117 : "K_u",
 
179
    118 : "K_v",
 
180
    119 : "K_w",
 
181
    120 : "K_x",
 
182
    121 : "K_y",
 
183
    122 : "K_z",
 
184
    127 : "K_DELETE",
 
185
    128 : "KMOD_RCTRL",
 
186
    192 : "KMOD_CTRL",
 
187
    256 : "KMOD_LALT",
 
188
    256 : "K_KP0",
 
189
    257 : "K_KP1",
 
190
    258 : "K_KP2",
 
191
    259 : "K_KP3",
 
192
    260 : "K_KP4",
 
193
    261 : "K_KP5",
 
194
    262 : "K_KP6",
 
195
    263 : "K_KP7",
 
196
    264 : "K_KP8",
 
197
    265 : "K_KP9",
 
198
    266 : "K_KP_PERIOD",
 
199
    267 : "K_KP_DIVIDE",
 
200
    268 : "K_KP_MULTIPLY",
 
201
    269 : "K_KP_MINUS",
 
202
    270 : "K_KP_PLUS",
 
203
    271 : "K_KP_ENTER",
 
204
    272 : "K_KP_EQUALS",
 
205
    273 : "K_UP",
 
206
    274 : "K_DOWN",
 
207
    275 : "K_RIGHT",
 
208
    276 : "K_LEFT",
 
209
    277 : "K_INSERT",
 
210
    278 : "K_HOME",
 
211
    279 : "K_END",
 
212
    280 : "K_PAGEUP",
 
213
    281 : "K_PAGEDOWN",
 
214
    282 : "K_F1",
 
215
    283 : "K_F2",
 
216
    284 : "K_F3",
 
217
    285 : "K_F4",
 
218
    286 : "K_F5",
 
219
    287 : "K_F6",
 
220
    288 : "K_F7",
 
221
    289 : "K_F8",
 
222
    290 : "K_F9",
 
223
    291 : "K_F10",
 
224
    292 : "K_F11",
 
225
    293 : "K_F12",
 
226
    294 : "K_F13",
 
227
    295 : "K_F14",
 
228
    296 : "K_F15",
 
229
    300 : "K_NUMLOCK",
 
230
    301 : "K_CAPSLOCK",
 
231
    302 : "K_SCROLLOCK",
 
232
    303 : "K_RSHIFT",
 
233
    304 : "K_LSHIFT",
 
234
    305 : "K_RCTRL",
 
235
    306 : "K_LCTRL",
 
236
    307 : "K_RALT",
 
237
    308 : "K_LALT",
 
238
    309 : "K_RMETA",
 
239
    310 : "K_LMETA",
 
240
    311 : "K_LSUPER",
 
241
    312 : "K_RSUPER",
 
242
    313 : "K_MODE",
 
243
    315 : "K_HELP",
 
244
    316 : "K_PRINT",
 
245
    317 : "K_SYSREQ",
 
246
    318 : "K_BREAK",
 
247
    319 : "K_MENU",
 
248
    320 : "K_POWER",
 
249
    321 : "K_EURO",
 
250
    323 : "K_LAST",
 
251
    512 : "KMOD_RALT",
 
252
    768 : "KMOD_ALT",
 
253
    1024 : "KMOD_LMETA",
 
254
    2048 : "KMOD_RMETA",
 
255
    3072 : "KMOD_META",
 
256
    4096 : "KMOD_NUM",
 
257
    8192 : "KMOD_CAPS",
 
258
    16384 : "KMOD_MODE",
 
259
}
 
260
 
 
261
def save_sound_conf():
 
262
    """
 
263
    save the sound configuration.
 
264
    """
 
265
    conf = ""
 
266
    for key in ('SOUND_VOLUME', 'MUSIC_VOLUME', 'SOUND', 'MUSIC'):
 
267
        conf += str(key)+" = "+str(sound_config[key])+"\n"
 
268
 
 
269
    file=open(os.path.join(xdg_config_home,'usf','sound.cfg'),'w')
 
270
    file.write(conf)
 
271
    file.close()
 
272
 
 
273
def save_keys_conf():
 
274
    """
 
275
    save the configuration of controls.
 
276
    """
 
277
    conf = ""
 
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"
 
281
 
 
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')
 
284
    file.write(conf)
 
285
    file.close()
 
286
 
 
287
config = load_config()
 
288
keyboard_config = load_key_config()
 
289
sound_config= load_sound_config()
 
290