Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Quickly check if it's safe to copy over your old init files  (Read 1152 times)

Foxite

  • Bay Watcher
    • View Profile
Quickly check if it's safe to copy over your old init files
« on: November 23, 2017, 09:47:41 am »

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:

Spoiler (click to show/hide)

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:
Code: [Select]
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.
« Last Edit: November 23, 2017, 09:55:18 am by latias1290 »
Logged
The best way to demonstrate it to him is take a save of 40 year old fortress with 150 dwarves in it on a good sized embark with a volcano that just breached the circus and install it on his gaming rig and watch it bring his rig to its knees.

Deon

  • Bay Watcher
  • 💀 💀 💀 💀 💀
    • View Profile
Re: Quickly check if it's safe to copy over your old init files
« Reply #1 on: November 23, 2017, 04:04:55 pm »

Cool. Although I find it easier to use WinMerge.
Logged
▬(ஜ۩۞۩ஜ)▬
✫ DF Wanderer ✫ - the adventure mode crafting and tweaks
✫ Cartographer's Lounge ✫ - a custom worldgen repository