-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.py
More file actions
133 lines (117 loc) · 4.06 KB
/
server.py
File metadata and controls
133 lines (117 loc) · 4.06 KB
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
import factorio
import flask
import json
from PIL import Image, ImageChops
import StringIO
import sys
app = flask.Flask(__name__)
if len(sys.argv) > 1:
data = factorio.load_factorio(mod_path=sys.argv[1])
else:
data = factorio.load_factorio()
@app.route('/image/<path:image>')
def show_image(image):
resp = flask.Response(mimetype="image/png")
resp.set_data(data.load_path(image))
return resp
@app.route('/icon/<table>/<entry>')
def show_icon(table, entry):
try:
entry = data.load_pseudo_table(table)[entry]
except KeyError:
flask.abort(404)
if entry.icon:
return show_image(entry.icon)
elif entry.icons == [] and table == "recipe":
keys = entry.results.keys()
assert len(keys) == 1
return show_icon("item", keys[0])
# Get the icons, and add the layers to the icons.
icons = entry.icons
base_icon = icons[0]
image = Image.open(StringIO.StringIO(
data.load_path(base_icon['icon'])))
image = Image.new("RGBA", image.size)
for icon in icons:
if icon['icon']:
sub_image = Image.open(StringIO.StringIO(data.load_path(icon['icon'])))
else:
sub_image = Image.new("RGBA", image.size, (1, 1, 1, 1))
if icon['tint']:
def tint_component(base, tint):
x = float(base)/256.0
weight = x - 0.5
val = x + tint * (1.0 - 4.0 * weight * weight)
return int(val*256)
tints = [icon['tint'].get(c, 0.0) for c in "rgba"]
tints[0] *= tints[-1]
tints[1] *= tints[-1]
tints[2] *= tints[-1]
def tint_value(val):
return (tint_component(val[0], tints[0]),
tint_component(val[1], tints[1]),
tint_component(val[2], tints[2]),
tint_component(val[3], tints[3]))
pix = sub_image.load()
for row in range(sub_image.size[0]):
for col in range(sub_image.size[1]):
val = tint_value(pix[row, col])
pix[row, col] = val
image.paste(sub_image, None, sub_image)
# Convert the image into a response.
resp = flask.Response(mimetype="image/png")
respfd = StringIO.StringIO()
image.save(respfd, "png")
resp.set_data(respfd.getvalue())
respfd.close()
return resp
@app.route('/data/lua_raw.json')
def full_lua():
def encode_lua(obj):
resp = dict()
for key, value in obj.items():
resp[key] = value
if all(x + 1 in resp for x in range(len(resp))):
return list(resp[x + 1] for x in range(len(resp)))
return resp
resp = flask.Response(mimetype="application/json")
json.dump(data.lua.globals().data.raw, resp.stream, default=encode_lua,
sort_keys=True)
return resp
@app.route('/data/<table>.json')
def json_table(table):
if table not in data._data:
flask.abort(404)
resp = flask.Response(mimetype="application/json")
json.dump(dict((n, v.to_json()) for n, v in data.load_table(table).items()),
resp.stream, sort_keys=True)
return resp
@app.route('/data/full-<table>.json')
def json_full_table(table):
resp = flask.Response(mimetype="application/json")
json.dump(dict((n, v.to_json()) for n, v in data.load_pseudo_table(table).items()),
resp.stream, sort_keys=True)
return resp
@app.route('/data/l10n.json')
def json_data():
resp = flask.Response(mimetype="application/json")
json.dump(data.get_l10n_tables(), resp.stream, sort_keys=True)
return resp
@app.route('/item-info')
def item_info():
return flask.render_template('item-info.html')
@app.route('/tech-tree')
def tech_tree():
return flask.render_template('tech-tree.html')
@app.route('/ratio')
def ratio():
return flask.render_template('ratio.html')
@app.route('/')
def index():
return flask.render_template('index.html')
# XXX
@app.route('/scripts/<path:script>')
def script(script):
return flask.render_template(script)
if __name__ == '__main__':
app.run(debug=True)