User Tools

Site Tools


DSA Converter

Summary

Below is an example demonstrating how you can convert ascii scripts (*.dsa) to their binary (*.dsb) or encrypted (*.dse) counterpart, via script.

API Areas of Interest

Example

DSA_Converter.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	// Get keyboard modifier state
	var s_bShiftPressed = App.modifierKeyState() & 0x02000000;
	var s_bControlPressed = App.modifierKeyState() & 0x04000000;
	var s_bAltPressed = App.modifierKeyState() & 0x08000000;
	var s_bMetaPressed = App.modifierKeyState() & 0x10000000;
 
	/*********************************************************************/
	// String : A function for retrieving a translation if one exists
	function text( sText )
	{
		// If the version of the application supports qsTr()
		if( typeof( qsTr ) != "undefined" ){
			// Return the translated (if any) text
			return qsTr( sText );
		}
 
		// Return the original text
		return sText;
	};
 
	/*********************************************************************/
	// Declare path variables
	var sSrcPath, sDestPath;
 
	// If the user had the shift key pressed when the script was executed
	if( s_bShiftPressed ){
		// Prompt the user for a directory to batch process
		sSrcPath = FileDialog.doDirectoryDialog( text( "Select a Directory" ) );
	// If the shift key was not pressed
	} else {
		// Prompt the user for an ascii script
		sSrcPath = FileDialog.doFileDialog( true, text( "Select the source file" ),
			"", "DAZ Script Ascii (*.dsa)" );
	}
 
	// If the user did not cancel
	if( sSrcPath ){
		// Create a file info object for the source script(s)
		var oFileInfo = new DzFileInfo( sSrcPath );
 
		// Create a directory object for the source script(s)
		var oDir = new DzDir( s_bShiftPressed ? sSrcPath : oFileInfo.path() );
 
		// Create a new script object
		var oScript = new DzScript();
 
		// Create an array to hold source file short names
		var aScripts = [];
 
		// Get the basename of the file
		var sShortName = String("%1.dsa").arg( oFileInfo.baseName() );
 
		// Clean up
		delete oFileInfo;
		oFileInfo = undefined;
 
		// If we are batch processing, get a list of all ascii scripts, otherwise just add the file
		aScripts = s_bShiftPressed ? oDir.entryList( "*.dsa", oDir.Files ) : [ sShortName ];
 
		// While there are source files to be converted
		while( aScripts.length ){
			// Let the user know we are busy
			setBusyCursor();
 
			// Make sure we are working with a new script
			oScript.clear();
 
			// Get the short filename
			sShortName = aScripts.pop();
 
			// Create a file info object
			oFileInfo = new DzFileInfo( sShortName );
 
			// Construct the source file path
			sSrcPath = String("%1/%2").arg( oDir.absPath() ).arg( sShortName );
 
			// Construct the destination file path;
			// if the user was holding the control key when the script was executed,
			// use the encrypted extension, otherwise use the binary extension
			sDestPath = String( "%1/%2.%3" )
				.arg( oDir.absPath() )
				.arg( oFileInfo.baseName() )
				.arg( s_bControlPressed ? "dse" : "dsb" );
 
			// Define common strings
			var sInformation = text( "Information" );
			var sWarning = text( "Warning" );
			var sButton = text( "&OK" );
 
			// If an error occurs when loading the chosen file into the script object
			if( !oScript.loadFromFile( sSrcPath ) ){
				// Let the user know we are done
				clearBusyCursor();
				// Warn the user
				MessageBox.warning( text( "An error occured while reading the file:\n%1" )
					.arg( sSrcPath ), sWarning, sButton, "" );
				// Next!!
				continue;
			// If no errors occur
			} else {
				// If the script syntax is flawed
				if( !oScript.checkSyntax() ){
					// Let the user know we are done
					clearBusyCursor();
					// Warn the user
					MessageBox.warning( text( "The syntax of the source file is invalid:\n%1" )
						.arg( sSrcPath ), sWarning, sButton, "" );
					// Next!!
					continue;
				}
 
				// If the application version is 4.11.0.130 or newer
				if( App.version64 >= 0x0004000b00000082 ){
					// Save the file;
					// if the user was holding the control key when the script was executed,
					// write the encrypted script, otherwise write a binary script and
					// record if there are any errors
					nWriteErr = oScript.saveFile( sDestPath,
						s_bControlPressed ? DzScript.EncDAZScriptFile : DzScript.DAZScriptFile );
				// If the application version is older than 4.11.0.130
				} else {
					// Save the file, using the deprecated API;
					// if the user was holding the control key when the script was executed,
					// write the encrypted script, otherwise write a binary script and
					// record if there are any errors
					nWriteErr = oScript.saveToFile( sDestPath,
						s_bControlPressed ? DzScript.EncDAZScriptFile : DzScript.DAZScriptFile );
				}
 
				// Let the user know we are done
				clearBusyCursor();
 
				// If there were no errors writing the file
				if( nWriteErr == 0x00000000 ){
					// Inform the user
					MessageBox.information(
						text( "Saved %1 script:\n%2" )
							.arg( s_bControlPressed ? "encrypted" : "binary" )
							.arg( sDestPath ),
						sInformation, sButton );
				// If an error occurs
				} else {
					// Warn the user
					MessageBox.warning( text( "An error occured while trying to save:\n%1" )
						.arg( sDestPath ), sWarning, sButton, "" );
				}
			}
 
			// If we are batch processing
			if( s_bShiftPressed ){
				// Next!!
				continue;
			}
		}
	}
 
// Finalize the function and invoke
})();