-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
92 lines (73 loc) · 1.6 KB
/
app.rb
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
require 'rubygems'
require 'sinatra'
require 'haml'
require 'sass'
require 'xmlsimple'
require 'json'
require 'pp'
get '/' do
haml :index, :layout => true
end
get '/levels.json' do
content_type :json
levels = Dir[ "public/level_packs/*/*.json" ].inject( "" ) do | str, path |
str + IO.read( path )
end
levels
end
get '/sprite_atlases.json' do
content_type :json
atlases = Dir[ "public/sprite_atlases/*/*.plist" ].inject( {} ) do | hsh, path |
atlas_name = File.dirname( path )[ /\/([^\/]+)$/, 1 ]
hsh[ atlas_name ] = PlistImporter.xml_from_plist( path )
hsh
end
atlases.to_json
end
post '/import/levels' do
# allow user to edit files
end
post '/import/textures' do
# import a plist and an image texture
end
post "/levels/save" do
content_type :json
puts params.inspect
end
post '/export' do
# save all levels to /levels
# levels are exported as json structures
end
get '/main.css' do
content_type 'text/css', :charset => 'utf-8'
sass :stylesheet
end
class PlistImporter
attr_reader :plist
def self.xml_from_plist( plist )
new( plist ).load
end
def initialize( plist )
@plist = plist
end
def load
xml = XmlSimple.xml_in( IO.read( plist ) )
dict = xml[ "dict" ]
dict.inject( {} ) do | hsh, item |
hsh.update( flatten( item.dup ) )
end
end
def flatten( hash )
keys = hash[ "key" ]
values = hash[ "dict" ] || hash[ "integer" ]
keys.inject( {} ) do | hsh, item |
value = values.to_a.shift
hsh[ item ] = if value.is_a? Hash
flatten( value )
else
value
end
hsh
end
end
end