I don't know if this has been done before, but I just did it and I though I'd share it.
This python script will check if it is safe to copy over your init.txt, d_init.txt, announcements.txt, and quite likely other files located in your data/init folder, from older versions of DF. The logic works like this:
Check if the line count is different. If yes, return unsafe.
For each line:
- if line starts with '[' and contains at least 1 ':': check if all text before the first : is the same from the corresponding line from the previous version, and if the amount of ':' is the same. if not, return unsafe.
- else: check if the entire line is the same. if not, return unsafe.
Return safe.
Caveats:
- It does NOT detect if the expected arguments for a tag have changed, although it DOES detect if the NUMBER of arguments has changed.
- - This will likely cause issues in announcements.txt, and you can disable this by removing the part from line 16 between " or " and the last ":".
- Finally, it also depends on the assumption that there are no lines with more than one tag in them, but I've never seen that in an init file, so I guess that should be fine.
Here's the script:
import sys
with open("d_init_new") as f:
lines_new = f.readlines()
with open("d_init_old") as f:
lines_old = f.readlines()
if len(lines_new) != len(lines_old):
print("Not safe, lines added or removed.")
sys.exit()
for i in range(0, len(lines_new)):
if lines_new[i][0] == '[' and ':' in lines_new[i]:
# All text between [ and : the same as old, and no arguments have been added or removed?
i_colon = lines_new[i].index(':')
if lines_new[i][:i_colon] != lines_old[i][:i_colon] or lines_new[i].count(':') != lines_old[i].count(':'):
print("Not safe, tag line " + str(i + 1) + " changed.")
sys.exit()
else:
# All text on line the same as old?
if lines_new[i] != lines_old[i]:
print("Not safe, non-tag line " + str(i + 1) + " changed.")
sys.exit()
print("Safe to copy.")
I'm releasing this script into public domain, do whatever you want.The first few lines, the ones with
d_init_new.txt and
d_init_old.txt, contain the file names for the old and new (vanilla) init files. You can change and swap them around as you please.
I tested all of its logic and I'm pretty sure this will accurately tell you if it's safe to copy over old init files. But as always, if you find that it does not, please tell me. If you have any tips or remarks about the code, that's also welcome.
Edit: Realized that it detecting differences in the number of arguments causes issues in announcements.txt. Added that to the list.