ddnet/scripts/cmd5.py

37 lines
1.1 KiB
Python
Raw Normal View History

2009-06-07 14:40:58 +00:00
import hashlib, sys, re
2007-08-22 21:13:33 +00:00
alphanum = "0123456789abcdefghijklmnopqrstuvwzyxABCDEFGHIJKLMNOPQRSTUVWXYZ_"
def cstrip(lines):
d = "".encode()
2007-08-22 21:13:33 +00:00
for l in lines:
l = re.sub("#.*".encode(), "".encode(), l)
l = re.sub("//.*".encode(), "".encode(), l)
d += l + " ".encode()
d = re.sub("\/\*.*?\*/".encode(), "".encode(), d) # remove /* */ comments
d = d.replace("\t".encode(), " ".encode()) # tab to space
d = re.sub(" *".encode(), " ".encode(), d) # remove double spaces
d = re.sub("".encode(), "".encode(), d) # remove /* */ comments
2007-08-22 21:13:33 +00:00
d = d.strip()
# this eats up cases like 'n {'
i = 1
while i < len(d)-2:
if d[i:i + 1] == " ":
if not (d[i-1:i] in alphanum and d[i+1:i + 2] in alphanum):
2007-08-22 21:13:33 +00:00
d = d[:i] + d[i+1:]
i += 1
return d
f = "".encode()
2007-08-22 21:13:33 +00:00
for filename in sys.argv[1:]:
2011-01-06 11:41:24 +00:00
f += cstrip([l.strip() for l in open(filename, "rb")])
2007-08-22 21:13:33 +00:00
hash = hashlib.md5(f).hexdigest().lower()[16:]
2010-05-29 15:20:18 +00:00
# TODO: refactor hash that is equal to the 0.5 hash, remove when we
# TODO: remove when we don't need it any more
2011-01-18 05:50:24 +00:00
if hash == "f16c2456fc487748":
2010-05-29 15:20:18 +00:00
hash = "b67d1f1a1eea234e"
print('#define GAME_NETVERSION_HASH "%s"' % hash)