joelday / papyrus-lang

📜Advanced language tools for the Papyrus scripting language.
Other
105 stars 19 forks source link

Default Imports would be a nice QoL #178

Open cppcooper opened 1 year ago

cppcooper commented 1 year ago

Is your feature request related to a problem? Please describe. Not launching VSCode from MO2 means many scripts aren't visible when developing. The solution is to add the paths to the mods that introduce them as imports in the ppj file. It is annoying needing to do this every time I generate a new project.

Describe the solution you'd like It would be nice to have a list of paths that are imported by default to newly generated ppj files.

bigbellyburger commented 1 year ago

I am using a small python script to use the exported MO2 mod list to update my ppj file. It would be nice if this was more integrated, but perhaps this helps someone in the meantime:

import csv, os
import lxml.etree as ET

imports = []

def add_import(name):
    if os.path.isdir(name):
        imports.append(name)

# the game base scripts
add_import("..\Game Root\Data\Scripts\Source")

with open('modlist.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        name = row['#Mod_Name']
        add_import(f"..\Mods\{name}\Source\Scripts")
        add_import(f"..\Mods\{name}\Scripts\Source")

imports.reverse()

parser = ET.XMLParser(remove_blank_text=True)
tree = ET.parse('skyrimse.ppj', parser)
root = tree.getroot()

imports_elem = root.find('{PapyrusProject.xsd}Imports')
imports_elem.clear()

for value in imports:
    elem = ET.Element('{PapyrusProject.xsd}Import')
    elem.text = value
    imports_elem.append(elem)

result = ET.tostring(root, xml_declaration=False, pretty_print=True, encoding='UTF-8')
with open('skyrimse.ppj', 'w') as fp:
    fp.write(result.decode('utf-8'))

This script requires python3 and the lxml module. On Windows you can open cmd.exe and run:

python3 --version
pip3 install lxml

To install it. The python3 command should have Windows install it if not already installed.

The script assumes it is placed alongside your skyrimse.ppj in a directory such as MO2Directory/mods/MyMod.

Then in MO2 do ... -> Export to csv -> Only active (checked) mods from your current profile and save this file as modlist.csv alongside the script, then run the script with python3 ppj-update.py

It will then update your skyrimse.ppj and replace the <Imports> tag.