File: pingus-level

package info (click to toggle)
pingus 0.7.6-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,672 kB
  • sloc: cpp: 42,080; xml: 2,319; lisp: 521; ruby: 455; ansic: 365; objc: 248; sh: 247; makefile: 140; python: 15
file content (262 lines) | stat: -rwxr-xr-x 8,040 bytes parent folder | download | duplicates (9)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/guile \
--debug -e main -s
!#

;;   Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
;;     
;;   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 2 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., 675 Mass Ave, Cambridge, MA 02139, USA.


;; Helper Application to view level information
(use-modules (xml expat)
	     (xml mixp)
	     (ice-9 format)
	     (oop goops)
	     (ice-9 getopt-long))

(define *output-format* 'normal)

(define (file->string filename)
  (let ((port (open-input-file filename))
	(lst  '()))
    (let loop ((line (read-line port)))
      (cond ((not (eof-object? line))
	     (set! lst (cons line lst))
	     (loop (read-line port)))))
    (close port)
    (apply string-append (reverse lst))))

(define (println . str)
  (for-each display str) (newline))

(define-class <pingus-level> ()
  (filename #:accessor filename
	#:init-value #f)

  (author #:accessor author
	  #:init-value #f)
  
  (levelname #:accessor levelname
	     #:init-value #f)
  
  (description #:accessor description
	       #:init-value #f)

  (comment #:accessor comment
	   #:init-value #f)

  (time #:accessor time 
	#:init-value #f)

  (difficulty #:accessor difficulty
	      #:init-value #f)

  (playable #:accessor playable
	    #:init-value #f)

  (number-to-save #:accessor number-to-save
		  #:init-value #f)

  (number-of-pingus #:accessor number-of-pingus
		    #:init-value #f)
  )

(define-method (file->pingus-level (fname <string>))
  (let* ((plf (make <pingus-level>))
	 (document (file->string fname))
	 (xml-tree (call-with-input-string document
					   mixp:xml->tree)))
    (set! (filename plf) (basename fname))
    (plf:parse plf xml-tree)
    plf))

;; Parse a pingus level file
(define-method (plf:parse (plf <pingus-level>) tree)
  (cond ((not (null? tree))
	 (let ((node (car tree)))
	   (cond ((equal? (car node) 'element)
		  (cond ((equal? (caadr node) "pingus-level")
			 (plf:parse-main plf (cddr node)))
			(else
			 (println "Error: Unknown tag: " (caadr node))
			 )))
		 (else
		  (println "Error: " (car node))))))
	(else
	 (plf:parse (cdr tree)))))

;; Parse the <global> part of a pingus level file
(define-method (plf:parse-global (plf <pingus-level>) tree)
  (let ((node (car tree)))
    ;; Insert global check here
    (for-each (lambda (i)
		(case (car i)
		  ((character-data) #f)
		  ((element)
		   ;;(println "El: " i)
		   (cond ((not (null? (cddr i)))
			  (case (string->symbol (caadr i))
			    ((playable)   
			     (set! (playable plf)   (string->number (car (cdaddr i)))))
			    ((difficulty) 
			     (set! (difficulty plf) (string->number (car (cdaddr i)))))
			    ((time)
			     (set! (time plf)       (string->number (car (cdaddr i)))))
			    ((levelname)
			     (set! (levelname plf)     (car (cdaddr i))))
			    ((author)
			     (set! (author plf)     (car (cdaddr i))))
			    ((comment)
			     (set! (comment plf)    (car (cdaddr i))))

			    ((number-of-pingus)
			     (set! (number-of-pingus plf)    (string->number (car (cdaddr i)))))

			    ((number-to-save)
			     (set! (number-to-save plf)    (string->number (car (cdaddr i)))))

			    (else  #f ));;     (println (caadr i))))
			  )))
		  (else (println "Error: global"))))
	      (cdr tree))))

(define-method (plf:parse-main (plf <pingus-level>) tree)
  (cond ((not (null? tree))  
	 (let ((node (car tree)))
	   (case (car node)
	     ((element)	      
	      (case (string->symbol (caadr node))
		((global) (plf:parse-global plf (cdr node)))
		((groundpiece) #f);(println "-groundpiece-: " (caadr node)))
		((liquid)      #f);(println "-liquid-: " (caadr node)))
		((hotspot)     #f);(println "-hotspot-: " (caadr node)))
		((entrance)    #f);(println "-entrance-: " (caadr node)))
		((exit)        #f);(println "-exit-: " (caadr node)))
		(else          #f)));(println "else: " (caadr node)))))
	     ((character-data) 
	      #f);(println "Cdata: " (cdr node )))
	     (else
	      #f);(println "Unknown: " (cdr node)))
	     ))
	 (plf:parse-main plf (cdr tree)))))

;; (type element content ...)

(define (print-usage)
  (println "Usage: pingus-level [OPTIONS]... [LEVELFILES]...")
  (println "")
  (println "  --verbose    Verbose output")
  (println "  --short      Short one-line output for easy parsing. The format is:")
  (println "               filename:playable:difficulty:time")
  (println "  --help       Print this help")
  (newline)
  (println "Examples:")  
  (println "~~~~~~~~~")
  (println "  % pingus-level --short somelevel.xml")
  (println "  % pingus-level --verbose somelevel.xml")
  (newline))

(define grammar '((verbose (required? #f)
			   (single-char #\v)
			   (value #f))
		  (html (required? #f)
			(value #f))
		  (short (required? #f)
			(single-char #\s)
			(value #f))
		  (help (required? #f)
			(single-char #\h)
			(value #f))))

(define-method (print-short-info (plf <pingus-level>))
  (println (filename plf) ":" (playable plf) ":" (difficulty plf) ":" (time plf)))


(define-method (print-html-info (plf <pingus-level>))
  (println "<h2>"(levelname plf) " (" (filename plf) ")</h2>"
	   "<table>"
	   "<tr>" "<td>Author:</td>" "<td>" (author plf) "</td></tr>"
	   "<tr>" "<td>Difficulty:</td>" "<td>" (or (difficulty plf) "not rated") "</td></tr>"
	   "<tr>" "<td>Save Ratio:</td>" "<td>" (number-to-save plf) "/" (number-of-pingus plf) "</td></tr>"
	   "<tr>" "<td>Playable:</td>" "<td>" (or (playable plf) "not tested") "</td></tr>"
	   "<tr>" "<td>Time:</td>" "<td>" (or (time plf) "-") "</td></tr>"
	   "<tr>" "<td>Comment:</td>" "<td>" (or (comment plf) "-") "</td></tr>"
	   "</table>"
	   ))

(define-method (print-long-info (plf <pingus-level>))
  (println "---- Filename:   " (filename plf) " ----")
  (println "Author:     " (author plf))
  (println "Difficulty: " (difficulty plf))
  (println "Playable:   " (playable plf))
  (println "Time:       " (time plf))
  (println "Comment:    " (comment plf))
  (println))

(define (print-level-info fname)
  (catch #t
	 (lambda ()
	   (let ((plf (file->pingus-level fname)))
	     (cond ((equal? *output-format* 'short)
		    (print-short-info plf))
		   ((equal? *output-format* 'html)
		    (print-html-info plf))
		   (else
		    (print-long-info plf)))))
	 (lambda stuff
	   (println fname ":error: " stuff))))

(define (print-html-header)
  (println "<html>
<head>
<title>Pingus Levels</title>
</head>
<body>
<h1>Pingus Levels</h1>"))

(define (print-html-footer)
  (println "<hr><small>automatically generated by the 'pingus-level' tool on " 
	   (strftime "%d. %b %G" (gmtime (current-time)))
	   " </small></body>"))

(define (main args)
  (catch 'misc-error
	 (lambda ()
	   (let ((result (getopt-long args grammar)))
	     (cond ((assoc-ref result 'verbose)
		    (set! *output-format* 'verbose)))
	     (cond ((assoc-ref result 'short)
		    (set! *output-format* 'short)))
	     (cond ((assoc-ref result 'html)
		    (set! *output-format* 'html)))
	     (cond ((assoc-ref result 'help)
		    (print-usage)
		    (exit 0)))
	     (cond ((not (null? (assoc-ref result '())))
		    (cond ((equal? *output-format* 'html)
			   (print-html-header)))
		    (for-each print-level-info
			      (assoc-ref result '()))
		    (cond ((equal? *output-format* 'html)
			   (print-html-footer))))
		   (else
		    (println "Error: You must supply a level-filename. Use --help for more infos.")
		    (exit 1)
		    ))))
	 (lambda err
	   (println "Error while parsing command line args: " err)
	   (exit 1))))

  ;; EOF ;;