1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | # This program is free software: you can redistribute it and/or modify |
---|
4 | # it under the terms of the GNU General Public License as published by |
---|
5 | # the Free Software Foundation, either version 3 of the License, or |
---|
6 | # (at your option) any later version. |
---|
7 | |
---|
8 | # This program is distributed in the hope that it will be useful, |
---|
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
11 | # GNU General Public License for more details. |
---|
12 | |
---|
13 | # You should have received a copy of the GNU General Public License |
---|
14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
15 | |
---|
16 | import re |
---|
17 | import os |
---|
18 | import sys |
---|
19 | from StringIO import StringIO |
---|
20 | import code |
---|
21 | |
---|
22 | class Console: |
---|
23 | def __init__(self, app_listener): |
---|
24 | """ |
---|
25 | @type appListener: ApplicationListener |
---|
26 | @param appListener: ApplicationListener object providing a link with |
---|
27 | the Controller, the view and the GameModel""" |
---|
28 | exit_help = "Terminate application" |
---|
29 | grid_help = "Toggle grid display" |
---|
30 | run_help = "Toggle player run/walk" |
---|
31 | help_help = "Show this help string" |
---|
32 | load_help = "Usage: load directory file" |
---|
33 | python_help = "Run some python code" |
---|
34 | quit_help = "Terminate application" |
---|
35 | save_help = "Usage: save directory file" |
---|
36 | pause_help = "Pause/Unpause the game" |
---|
37 | |
---|
38 | self.commands = [ |
---|
39 | {"cmd":"exit" ,"callback":self.handleQuit ,"help": exit_help}, |
---|
40 | {"cmd":"grid" ,"callback":self.handleGrid ,"help": grid_help}, |
---|
41 | {"cmd":"help" ,"callback":self.handleHelp ,"help": help_help}, |
---|
42 | {"cmd":"load" ,"callback":self.handleLoad ,"help": load_help}, |
---|
43 | {"cmd":"pause" ,"callback":self.handlePause ,"help": pause_help}, |
---|
44 | {"cmd":"python","callback":self.handlePython,"help": python_help}, |
---|
45 | {"cmd":"run" ,"callback":self.handleRun ,"help": run_help}, |
---|
46 | {"cmd":"save" ,"callback":self.handleSave ,"help": save_help}, |
---|
47 | {"cmd":"quit" ,"callback":self.handleQuit ,"help": quit_help}, |
---|
48 | ] |
---|
49 | self.app_listener = app_listener |
---|
50 | self.view = self.app_listener.view |
---|
51 | self.model = self.app_listener.model |
---|
52 | self.game_state = self.app_listener.view.model.game_state |
---|
53 | self.console_locals = {"__name__":"__paprg_console__",\ |
---|
54 | "__doc__": None,\ |
---|
55 | "app_listener":self.app_listener,\ |
---|
56 | "model":self.app_listener.model,\ |
---|
57 | "view":self.app_listener.view,\ |
---|
58 | "engine":self.app_listener.engine} |
---|
59 | |
---|
60 | self.console = code.InteractiveConsole(self.console_locals) |
---|
61 | |
---|
62 | def handleQuit(self, command): |
---|
63 | """Implements the quit console command |
---|
64 | @type command: string |
---|
65 | @param command: The command to run |
---|
66 | @return: The resultstring""" |
---|
67 | self.app_listener.quitGame() |
---|
68 | return "Terminating ..." |
---|
69 | |
---|
70 | def handlePause(self, command): |
---|
71 | """Implements the pause console command |
---|
72 | @type command: string |
---|
73 | @param command: The command to run |
---|
74 | @return: The resultstring""" |
---|
75 | self.model.togglePause() |
---|
76 | return "Game (un)paused" |
---|
77 | |
---|
78 | def handleGrid(self, command): |
---|
79 | """Implements the grid console command |
---|
80 | @type command: string |
---|
81 | @param command: The command to run |
---|
82 | @return: The resultstring""" |
---|
83 | self.app_listener.view.active_map.toggleRenderer('GridRenderer') |
---|
84 | return "Grid toggled" |
---|
85 | |
---|
86 | def handleRun(self, command): |
---|
87 | """Toggles run/walk mode of the PC player |
---|
88 | @type command: string |
---|
89 | @param command: The command to run. |
---|
90 | @return: The response""" |
---|
91 | if self.app_listener.model.pc_run == 1: |
---|
92 | self.app_listener.model.pc_run = 0 |
---|
93 | return "PC is now walking" |
---|
94 | else: |
---|
95 | self.app_listener.model.pc_run = 1 |
---|
96 | return "PC is now running" |
---|
97 | |
---|
98 | def handleHelp(self, command): |
---|
99 | """Implements the help console command |
---|
100 | @type command: string |
---|
101 | @param command: The command to run |
---|
102 | @return: The resultstring""" |
---|
103 | res = "" |
---|
104 | for cmd in self.commands: |
---|
105 | res += "%10s: %s\n" % (cmd["cmd"], cmd["help"]) |
---|
106 | return res |
---|
107 | |
---|
108 | def handlePython(self,command): |
---|
109 | user_code = command[7:len(command)] |
---|
110 | |
---|
111 | codeOut = StringIO() |
---|
112 | |
---|
113 | #make stdout and stderr write to our file, not the terminal |
---|
114 | sys.stdout = codeOut |
---|
115 | sys.stderr = codeOut |
---|
116 | |
---|
117 | #Workaround it not being possible to enter a blank line in the console |
---|
118 | if user_code == " ": |
---|
119 | user_code = "" |
---|
120 | |
---|
121 | #Process the code |
---|
122 | self.console.push(user_code) |
---|
123 | if len(self.console.buffer) == 0: |
---|
124 | output = codeOut.getvalue() |
---|
125 | else: |
---|
126 | output = "..." |
---|
127 | |
---|
128 | |
---|
129 | #restore stdout and stderr |
---|
130 | sys.stdout = sys.__stdout__ |
---|
131 | sys.stderr = sys.__stderr__ |
---|
132 | |
---|
133 | temp_output = output |
---|
134 | output = "" |
---|
135 | counter = 0 |
---|
136 | |
---|
137 | #Make the output fit in the console screen |
---|
138 | for char in temp_output: |
---|
139 | counter += 1 |
---|
140 | if char == "\n": |
---|
141 | counter = 0 |
---|
142 | elif counter == 110: |
---|
143 | output += "\n" |
---|
144 | counter = 0 |
---|
145 | output += char |
---|
146 | |
---|
147 | return output |
---|
148 | |
---|
149 | def handleLoad(self, command): |
---|
150 | """Implements the load console command |
---|
151 | @type command: string |
---|
152 | @param command: The command to run |
---|
153 | @return: The resultstring""" |
---|
154 | result = None |
---|
155 | load_regex = re.compile('^load') |
---|
156 | l_matches = load_regex.match(command.lower()) |
---|
157 | if (l_matches is not None): |
---|
158 | end_load = l_matches.end() |
---|
159 | try: |
---|
160 | l_args = command.lower()[end_load + 1:].strip() |
---|
161 | l_path, l_filename = l_args.split(' ') |
---|
162 | self.app_listener.model.load_save = True |
---|
163 | self.app_listener.model.savegame = os.path.join(l_path, l_filename) |
---|
164 | result = "Load triggered" |
---|
165 | |
---|
166 | except Exception, l_error: |
---|
167 | self.app_listener.engine.getGuiManager().getConsole().println('Error: ' + str(l_error)) |
---|
168 | result="Failed to load file" |
---|
169 | |
---|
170 | return result |
---|
171 | |
---|
172 | def handleSave(self, command): |
---|
173 | """Implements the save console command |
---|
174 | @type command: string |
---|
175 | @param command: The command to run |
---|
176 | @return: The resultstring""" |
---|
177 | save_regex = re.compile('^save') |
---|
178 | s_matches = save_regex.match(command.lower()) |
---|
179 | if (s_matches != None): |
---|
180 | end_save = s_matches.end() |
---|
181 | try: |
---|
182 | s_args = command.lower()[end_save+1:].strip() |
---|
183 | s_path, s_filename = s_args.split(' ') |
---|
184 | self.app_listener.model.save(s_path, s_filename) |
---|
185 | result = "Saved to file: " + s_path + s_filename |
---|
186 | |
---|
187 | except Exception, s_error: |
---|
188 | self.app_listener.engine.getGuiManager().getConsole(). \ |
---|
189 | println('Error: ' + str(s_error)) |
---|
190 | result = "Failed to save file" |
---|
191 | return result |
---|
192 | |
---|
193 | def handleConsoleCommand(self, command): |
---|
194 | """Implements the console logic |
---|
195 | @type command: string |
---|
196 | @param command: The command to run |
---|
197 | @return: The response string """ |
---|
198 | result = None |
---|
199 | for cmd in self.commands: |
---|
200 | regex = re.compile('^' + cmd["cmd"]) |
---|
201 | if regex.match(command.lower()): |
---|
202 | result=cmd["callback"](command) |
---|
203 | |
---|
204 | if result is None: |
---|
205 | result = self.handlePython("python " + command) |
---|
206 | return result |
---|