2014-02-09 16:37:11 +00:00
|
|
|
|
|
|
|
def preprocess(lines, flags):
|
|
|
|
output = []
|
|
|
|
skip_level = 0
|
|
|
|
for line in lines:
|
|
|
|
line = line.split("//")[0].strip() # Remove comment
|
|
|
|
if line == "":
|
|
|
|
continue
|
|
|
|
if line[0] == "#":
|
2016-01-01 20:00:17 +00:00
|
|
|
if line.startswith("#ifdef"):
|
2016-01-01 20:39:34 +00:00
|
|
|
cond = line[7:].strip()
|
2014-02-09 16:37:11 +00:00
|
|
|
if skip_level > 0 or cond not in flags:
|
|
|
|
skip_level += 1
|
|
|
|
elif line.startswith("#else"):
|
|
|
|
if skip_level == 0:
|
|
|
|
skip_level = 1
|
|
|
|
elif skip_level == 1:
|
|
|
|
skip_level = 0
|
|
|
|
elif line.startswith("#endif"):
|
|
|
|
if skip_level > 0:
|
|
|
|
skip_level -= 1
|
|
|
|
continue
|
|
|
|
if skip_level == 0:
|
|
|
|
output.append(line)
|
|
|
|
return output
|
|
|
|
|
|
|
|
def get_id(name_id_mapping, name):
|
|
|
|
try:
|
2015-03-09 09:36:34 +00:00
|
|
|
# if the name of the node is known then return the location with the name_id_mapping list
|
2014-02-09 16:37:11 +00:00
|
|
|
return name_id_mapping.index(name)
|
|
|
|
except:
|
2015-03-09 09:36:34 +00:00
|
|
|
# else add the name of the node to the list then return the location
|
2014-02-09 16:37:11 +00:00
|
|
|
name_id_mapping.append(name)
|
|
|
|
return len(name_id_mapping)-1
|
|
|
|
|
|
|
|
def read_content(flags):
|
|
|
|
with open("map_content.txt", "r") as f:
|
|
|
|
lines = f.readlines()
|
2015-03-09 09:36:34 +00:00
|
|
|
|
2014-02-09 16:37:11 +00:00
|
|
|
lines = preprocess(lines, flags)
|
2015-03-09 09:36:34 +00:00
|
|
|
# if you map to air, then unknown blocks will be ignored
|
2015-11-17 07:51:43 +00:00
|
|
|
name_id_mapping = ["air"]
|
|
|
|
# name_id_mapping = ["mcblock:unknown"]
|
2015-03-09 09:36:34 +00:00
|
|
|
bd = {} # bd is block data, and keeps a list of the node names in the block
|
|
|
|
# iterate through all the lines in the map_content.txt file
|
2014-02-09 16:37:11 +00:00
|
|
|
for line in lines:
|
|
|
|
s = line.split("\t")
|
|
|
|
if len(s) >= 2:
|
|
|
|
r = s[1].split(" ")
|
|
|
|
if len(r) == 0:
|
|
|
|
print(line)
|
|
|
|
raise ValueError("Malformed data")
|
|
|
|
name = r[0]
|
|
|
|
param2 = 0
|
|
|
|
for i in range(1, len(r)):
|
|
|
|
if r[i] != "":
|
|
|
|
param2 = int(r[i])
|
|
|
|
break
|
|
|
|
t = s[0].split(" ")
|
|
|
|
if len(t) == 2:
|
|
|
|
for data in t[1].split(","):
|
|
|
|
key = (int(t[0]), int(data))
|
|
|
|
if key not in bd:
|
|
|
|
bd[key] = (get_id(name_id_mapping, name), param2)
|
|
|
|
elif len(t) == 1:
|
|
|
|
for data in range(16):
|
|
|
|
key = (int(t[0]), data)
|
|
|
|
if key not in bd:
|
|
|
|
bd[key] = (get_id(name_id_mapping, name), param2)
|
|
|
|
|
2015-02-16 07:30:37 +00:00
|
|
|
#blocks_len = max([i[0] for i in bd.keys()])+1
|
|
|
|
blocks_len = 4096
|
2014-02-09 16:37:11 +00:00
|
|
|
blocks = [[(0, 0)]*16 for i in range(blocks_len)]
|
|
|
|
for (id, data), value in bd.items():
|
|
|
|
blocks[id][data] = value
|
|
|
|
return name_id_mapping, blocks
|