"""Automate Snowrunner save file renaming described in https://steamcommunity.com/sharedfiles/filedetails/?id=2530914231"""
import argparse
import csv
import os
import shutil

def load_csv(csv_path):
    save_dict = {}
    with open(csv_path) as csvfile:
        csv_reader = csv.reader(csvfile, delimiter=",", quotechar="'")
        for row in csv_reader:
            if row[0] in save_dict:
                save_dict[row[0]].append(row[1])
            else:
                save_dict[row[0]] = [row[1]]
    return save_dict

def copy_file(input_dir, input_name, output_dir, output_name, dry_run):
    input = os.path.join(input_dir, input_name)
    output = os.path.join(output_dir, f"{output_name}.cfg")
    print(f"Copy {input} to {output}")
    if not dry_run:
        shutil.copy(input, output)

def copy_rename_save(save_dict, input_dir, output_dir, dry_run):
    for base_decoded_filename, hashed_filenames in save_dict.items():
        if len(hashed_filenames) == 1:
            copy_file(input_dir, hashed_filenames[0], output_dir, base_decoded_filename, dry_run)
        elif 2 <= len(hashed_filenames) < 99:
            for idx, hashed_filename in enumerate(hashed_filenames):
                copy_file(input_dir, hashed_filename, output_dir, f"{base_decoded_filename}{idx+1:02}", dry_run)
        else:
            raise ValueError(f"Invalid number of hashed filenames ({len(hashed_filenames)}) for file {base_decoded_filename}")

if __name__=="__main__":
    parser = argparse.ArgumentParser(description="Rename Snowrunner save files from Windows Store to Steam convention")
    parser.add_argument("--input-csv", help="CSV generated from https://www.save-editor.com/tools/wse_save_file_name_check_for_microsoft_store.html", required=True)
    parser.add_argument("--input-save-directory", help="Snowrunner Windows Store savegame directory", required=True)
    parser.add_argument("--output-save-directory", help="Snowrunner Steam savegame directory", required=True)
    parser.add_argument("--dry-run", action="store_true", default=False, help="Enable dry run, do not copy files")
    args = parser.parse_args()

    save_dict = load_csv(args.input_csv)
    #print(save_dict)
    if not os.path.exists(args.input_save_directory) or not os.path.isdir(args.input_save_directory):
        parser.error(f"Error accessing {args.input_save_directory}")
    if not args.dry_run:
        os.makedirs(args.output_save_directory, exist_ok=True)
    copy_rename_save(save_dict, args.input_save_directory, args.output_save_directory, args.dry_run)

Add automated Snowrunner save file renaming
Add automated Snowrunner save file renaming
