Jump to content

UltraEdit JS: Find Orphaned Properties in Papyrus Scripts


fireundubh

Recommended Posts

There are a lot of scripts that have orphaned properties which produce warning or error output.

 

I wrote a JavaScript script for UltraEdit that generates a list of possible orphaned properties in a script (i.e., unused properties.)

 

It's important to remember that while a property might be unused in the script in which it is declared, the property might actually be used by another script. This is especially true for Fallout 4 scripts.

 

So, always search for possible orphans in other scripts before removing them.

if (UltraEdit.document.length > 0)
{
	UltraEdit.perlReOn();
	UltraEdit.activeDocument.top();
	UltraEdit.activeDocument.findReplace.mode = 0;
	UltraEdit.activeDocument.findReplace.matchCase = false;
	UltraEdit.activeDocument.findReplace.regExp = true;
	UltraEdit.activeDocument.findReplace.searchDown = true;
	UltraEdit.activeDocument.findReplace.preserveCase = false;
	UltraEdit.activeDocument.findReplace.selectText = true;

	var results = [];

	while (UltraEdit.activeDocument.findReplace.find("Property [0-9a-zA-Z_]+")) {
		var f = UltraEdit.activeDocument.selection;
		var s = f.replace(/Property /g, "");
		results.push(s);
	}

	var counts = [];
	for (var i = 0; i < results.length; i++) {
		var tmp = countResults(results[i]);
		if (tmp <= 1) {
			counts.push(results[i]);
		}
	}

	UltraEdit.newFile();
	UltraEdit.activeDocument.top();
	if (counts.length > 0) {
		for (var i = 0; i < counts.length; i++) {
			UltraEdit.activeDocument.write(counts[i]);
		}
	} else {
		UltraEdit.activeDocument.write("No possible orphaned properties found.")
	}
}

function countResults(s) {
	UltraEdit.activeDocument.top();

	var results = [];
	while (UltraEdit.activeDocument.findReplace.find(s)) {
		var f = UltraEdit.activeDocument.selection;
		results.push(s);
	}
	return results.length;
}
Link to comment
Share on other sites

Is it possible to extend the script to report which scripts refer to the aforesaid unused properties? This might be very useful in the case of generating reports on the entire .psc collection. :)

Just curious if [0-9a-zA-Z_]+ also works with something like ^\w+$.

Link to comment
Share on other sites

Not really possible with this script. What this does is generate an array of property names, and then iterates through that array, counting the instances of each name. This can take awhile for a large script with a lot of properties, so the performance impact of doing that for each name on each script would be immense. You'd be better off using a database.
 
I just use Agent Ransack to search through all of the scripts for each name.
 
Regarding the RegEx:

  • ^\w+$ would match strings like EndFunction at the start of a line.
  • Another way of matching property names would be [p|P]roperty\h\K\w+.

JavaScript doesn't support \h and \K though.

Link to comment
Share on other sites

You could add an additional check for properties with the "hidden" keywords, since properties with this keyword have for sole meaning to be usable by other scripts (otherwise they'd just be regular internal variables).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...