ParadoxGameConverters / Vic2ToHoI4

Converts Victoria 2 saves into Hearts of Iron 4 mods.
MIT License
28 stars 21 forks source link

Resources deleted on conversion #1705

Closed NicknamesAreNotHard closed 4 months ago

NicknamesAreNotHard commented 4 months ago

When converting, in the resulting hoi4 save-file, there are no resources on the map.

NicknamesAreNotHard commented 4 months ago

I made a python script that i used to fix this, but its a temporary fix for anyone experiencing the same issue. I used the basegame state data to insert into the mod file, as it was the most effective from my attempts. In the destination folder you have to put the mod from the converter.

import os
import re

# Define the paths to the source and destination folders using raw strings
source_folder = r'Z:\SteamLibrary\steamapps\common\Hearts of Iron IV\history\states' # INSERT SOURCE FOLDER FOLDER. EXAMPLE: 
destination_folder = r'C:\Users\User\Documents\Paradox Interactive\Hearts of Iron IV\mod\H_Alhpa\history\states' #INSERT DESTINATION FOLDER. EXAMPLE: 

# Check if source and destination folders exist
if not os.path.exists(source_folder):
    raise FileNotFoundError(f"Source folder does not exist: {source_folder}")

if not os.path.exists(destination_folder):
    raise FileNotFoundError(f"Destination folder does not exist: {destination_folder}")

# Function to extract the resources block from a file
def extract_resources(file_path):
    with open(file_path, 'r') as file:
        content = file.read()
        # Using regex to find the resources block
        match = re.search(r'resources=\{[^}]*\}', content)
        if match:
            return match.group(0)
    return None

# Function to replace the resources block in the destination file
def replace_resources(destination_file_path, resources):
    with open(destination_file_path, 'r') as file:
        content = file.read()`

    # Using regex to replace or add the resources block
    if re.search(r'resources=\{[^}]*\}', content):
        new_content = re.sub(r'resources=\{[^}]*\}', resources, content)
    else:
        # Assuming we add the resources block just before the closing bracket of the state
        new_content = re.sub(r'(?=\n\})', f'\n\t{resources}', content)

    with open(destination_file_path, 'w') as file:
        file.write(new_content)

# Iterate through all files in the source folder
for filename in os.listdir(source_folder):
    if filename.endswith('.txt'):
        # Extract the number from the filename
        file_number = filename.split('-')[0]
        source_file_path = os.path.join(source_folder, filename)
        destination_file_path = os.path.join(destination_folder, f'{file_number}.txt')

        # Check if the source file exists
        if not os.path.exists(source_file_path):
            print(f'Source file {source_file_path} does not exist, skipping.')
            continue

        # Extract the resources block from the source file
        resources = extract_resources(source_file_path)

        if resources:
            # Check if the destination file exists
            if os.path.exists(destination_file_path):
                # Replace or add the resources block in the corresponding destination file
                replace_resources(destination_file_path, resources)
                print(f'Updated {destination_file_path} with resources: {resources}')
            else:
                print(f'Destination file {destination_file_path} does not exist, skipping.')
        else:
            print(f'No resources block found in {source_file_path}')
Idhrendur commented 4 months ago

If you convert too early that's what happens. Resource amounts are based on the bottom row on technologies.