User Tools

Site Tools


Version Conversion

Summary

Below is an example demonstrating how you can convert a Daz Studio version string into its 32 and 64 bit hexadecimal and decimal values, via script. This is useful because it can be used to quickly obtain the decimal version number to check against when providing support for multiple versions of the application with different APIs.

For example:

if( App.version >= 67109431 ){ //0x04000237 //4.0.2.55
	//...
} else {
	//...
}
if( App.version64 >= 1125899906973751 ){ //0x0004000000020037 //4.0.2.55
	//...
} else {
	//...
}

API Areas of Interest

Example

Version_Conversion.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	/*********************************************************************/
	// 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;
	};
 
	/*********************************************************************/
	// String : A function to convert a decimal number to a hexadecimal number
	function decToHex( vValue )
	{
		// Assign the initial value
		var nValue = vValue;
		// If the value is a string instead of a number
		if( typeof( nValue ) == "string" ){
			// Convert the string value to a decimal number
			nValue = parseInt( vValue, 10 );
		}
 
		// Return the value converted to hexadecimal
		return nValue.toString( 16 );
	};
 
	/*********************************************************************/
	// Number : A function to convert a hexadecimal number to a decimal number
	function hexToDec( sValue )
	{
		// Return the value converted from hexadecimal
		return parseInt( sValue, 16 );
	};
 
	/*********************************************************************/
	// Array<String> : A function to convert a version string into an array of hexadecimal values
	function versionStringToHexArray( sVersion )
	{
		// Split the version string into an array
		var aInVersion = sVersion.split(".");
		// Declare a presized output array
		var aOutVersion = new Array(4);
 
		// Declare a variable to keep track of where we are at
		var nInCount = 0;
		// Iterate over the version string array
		for( nInCount = 0; nInCount < aInVersion.length; nInCount += 1 ){
			// Populate the output array with the hexadecimal converted value
			aOutVersion[ nInCount ] = decToHex( aInVersion[ nInCount ] );
		}
 
		// Iterate over any remaining positions not provided by the input string
		for( var nOutCount = nInCount; nOutCount < aOutVersion.length; nOutCount += 1 ){
			// Fill the output array with 0's
			aOutVersion[ nOutCount ] = "0";
		}
 
		return aOutVersion;
	};
 
	/*********************************************************************/
	// String : A function to convert a version string into a hexadecimal value
	function versionStringToHex( sVersion, bSixtyFour )
	{
		// Get an array of hexadecimal values converted from the version string
		var aVersion = versionStringToHexArray( sVersion );
 
		// 1 = Major
		// 2 = Minor
		// 3 = Revision
		// 4 = Build
 
		//64bit = 0x1111222233334444
		//32bit = 0x11223344
 
		// Iterate over the array
		var sVersion = "";
		for( var i = 0; i < aVersion.length; i += 1 ){
			// Populate the array with zero padded versions of the hexadecimal values
			// Zero padding is dependent on bit depth
			sVersion = String("000%1").arg( aVersion[ i ] );
			aVersion[ i ] = sVersion.substring( bSixtyFour ? sVersion.length - 4 : sVersion.length - 2 );
		}
 
		// Construct the hexadecimal value from its parts
		var sHexVersion = String("0x%1").arg( aVersion.join("") );
 
		return sHexVersion;
	};
 
	/*********************************************************************/
	// Number : A function to convert a version string into a decimal value
	function versionStringToDec( sVersion, nBit )
	{
		// Get a hexadecimal conversion of the version string
		var sHexVersion = versionStringToHex( sVersion, nBit );
		// Return the decimal conversion of the hexadecimal conversion
		return hexToDec( sHexVersion );
	};
 
	/*********************************************************************/
	// void : A function to update the conversion line edits
	function update()
	{
		// Construct a version string from the 4 version fields, replacing
		// empty values with zeros so that we always produce a valid result
		var sVersion = String("%1.%2.%3.%4")
			.arg( wEditMajor.text.isEmpty() ? 0 : wEditMajor.text )
			.arg( wEditMinor.text.isEmpty() ? 0 : wEditMinor.text )
			.arg( wEditRevision.text.isEmpty() ? 0 : wEditRevision.text )
			.arg( wEditBuild.text.isEmpty() ? 0 : wEditBuild.text );
 
		// Update the conversion fields
		wEdit32Dec.text = versionStringToDec( sVersion );
		wEdit32Hex.text = versionStringToHex( sVersion );
		wEdit64Dec.text = versionStringToDec( sVersion, true );
		wEdit64Hex.text = versionStringToHex( sVersion, true );
	};
 
	/*********************************************************************/
	// Get the current style
	var oStyle = App.getStyle();
	// Get the general margin
	var nMargin = oStyle.pixelMetric( "DZ_GeneralMargin" );
 
	// Create a basic dialog
	var wDlg = new DzBasicDialog();
 
	// Get the wrapped widget
	var oDlgWgt = wDlg.getWidget();
 
	// Set the title of the dialog
	wDlg.caption = "Version Conversion";
 
	// Strip the space for a settings key
	var sKey = wDlg.caption.replace( / /g, "" ) + "Dlg";
 
	// Set a [unique] object name on the wrapped dialog widget;
	// this is used for recording position and size separately
	// from all other [uniquely named] DzBasicDialog instances
	oDlgWgt.objectName = sKey;
 
	// Create the main widget
	var wMainWgt = new DzWidget( wDlg );
 
	// Create the main layout
	var lytMain = new DzGridLayout( wMainWgt );
	lytMain.margin = nMargin;
	lytMain.spacing = nMargin;
 
	// Initialize working variables
	var aVersion = App.longVersionString.split(".");
	var nLabelCol = 0;
	var nValueCol = 1;
	var nNumValueCol = 1;
	var nRow = 0;
 
	// Create the version label
	var wLbl = new DzLabel( wMainWgt );
	wLbl.text = text( "Version :" );
	wLbl.alignment = DzWidget.AlignVCenter | DzWidget.AlignRight;
	// If the application version is 4.10.0.22 or newer
	if( App.version64 >= 0x0004000a00000016 ){
		// Disable eliding
		wLbl.elideMode = DzWidget.ElideNone;
	}
	lytMain.addWidget( wLbl, nRow, nLabelCol );
 
	// Create a line edit for version major, populate it, mask it and connect it
	var wEditMajor = new DzLineEdit( wMainWgt );
	wEditMajor.text = aVersion[0];
	wEditMajor.inputMask = "D";
	wEditMajor.alignment = DzWidget.AlignCenter;
	lytMain.addWidget( wEditMajor, nRow, nNumValueCol );
	connect( wEditMajor, "textChanged(const QString&)", update );
 
	nNumValueCol += 1;
 
	// Create a line edit for version minor, populate it, mask it and connect it
	var wEditMinor = new DzLineEdit( wMainWgt );
	wEditMinor.text = aVersion[1];
	wEditMajor.inputMask = "D";
	wEditMinor.alignment = DzWidget.AlignCenter;
	lytMain.addWidget( wEditMinor, nRow, nNumValueCol );
	connect( wEditMinor, "textChanged(const QString&)", update );
 
	nNumValueCol += 1;
 
	// Create a line edit for version revision, populate it, mask it and connect it
	var wEditRevision = new DzLineEdit( wMainWgt );
	wEditRevision.text = aVersion[2];
	wEditMajor.inputMask = "D";
	wEditRevision.alignment = DzWidget.AlignCenter;
	lytMain.addWidget( wEditRevision, nRow, nNumValueCol );
	connect( wEditRevision, "textChanged(const QString&)", update );
 
	nNumValueCol += 1;
 
	// Create a line edit for version build, populate it, mask it and connect it
	var wEditBuild = new DzLineEdit( wMainWgt );
	wEditBuild.text = aVersion[3];
	wEditMajor.inputMask = "D";
	wEditBuild.alignment = DzWidget.AlignCenter;
	lytMain.addWidget( wEditBuild, nRow, nNumValueCol );
	connect( wEditBuild, "textChanged(const QString&)", update );
 
	nRow += 1;
 
	// Create widgets for 32 bit decimal output
	wLbl = new DzLabel( wMainWgt );
	wLbl.text = text( "32bit Dec :" );
	wLbl.alignment = DzWidget.AlignVCenter | DzWidget.AlignRight;
	// If the application version is 4.10.0.22 or newer
	if( App.version64 >= 0x0004000a00000016 ){
		// Disable eliding
		wLbl.elideMode = DzWidget.ElideNone;
	}
	lytMain.addWidget( wLbl, nRow, nLabelCol );
 
	var wEdit32Dec = new DzLineEdit( wMainWgt );
	wEdit32Dec.readOnly = true;
	lytMain.addWidget( wEdit32Dec, nRow, nValueCol, 1, nNumValueCol );
 
	nRow += 1;
 
	// Create widgets for 32 bit hexadecimal output
	wLbl = new DzLabel( wMainWgt );
	wLbl.text = text( "32bit Hex :" );
	wLbl.alignment = DzWidget.AlignVCenter | DzWidget.AlignRight;
	// If the application version is 4.10.0.22 or newer
	if( App.version64 >= 0x0004000a00000016 ){
		// Disable eliding
		wLbl.elideMode = DzWidget.ElideNone;
	}
	lytMain.addWidget( wLbl, nRow, nLabelCol );
 
	var wEdit32Hex = new DzLineEdit( wMainWgt );
	wEdit32Hex.readOnly = true;
	lytMain.addWidget( wEdit32Hex, nRow, nValueCol, 1, nNumValueCol );
 
	nRow += 1;
 
	// Create widgets for 64 bit decimal output
	wLbl = new DzLabel( wMainWgt );
	wLbl.text = text( "64bit Dec :" );
	wLbl.alignment = DzWidget.AlignVCenter | DzWidget.AlignRight;
	// If the application version is 4.10.0.22 or newer
	if( App.version64 >= 0x0004000a00000016 ){
		// Disable eliding
		wLbl.elideMode = DzWidget.ElideNone;
	}
	lytMain.addWidget( wLbl, nRow, nLabelCol );
 
	var wEdit64Dec = new DzLineEdit( wMainWgt );
	wEdit64Dec.readOnly = true;
	lytMain.addWidget( wEdit64Dec, nRow, nValueCol, 1, nNumValueCol );
 
	nRow += 1;
 
	// Create widgets for 64 bit hexadecimal output
	wLbl = new DzLabel( wMainWgt );
	wLbl.text = text( "64bit Hex :" );
	wLbl.alignment = DzWidget.AlignVCenter | DzWidget.AlignRight;
	// If the application version is 4.10.0.22 or newer
	if( App.version64 >= 0x0004000a00000016 ){
		// Disable eliding
		wLbl.elideMode = DzWidget.ElideNone;
	}
	lytMain.addWidget( wLbl, nRow, nLabelCol );
 
	var wEdit64Hex = new DzLineEdit( wMainWgt );
	wEdit64Hex.readOnly = true;
	lytMain.addWidget( wEdit64Hex, nRow, nValueCol, 1, nNumValueCol );
 
	nRow += 1;
 
	// Add the main widget to the dialog
	wDlg.addWidget( wMainWgt );
 
	// Set the text on the accept button
	wDlg.setAcceptButtonText( text( "&Close" ) );
	// Hide the cancel button
	wDlg.showCancelButton( false );
 
	// Update the various conversion fields
	update();
 
	// Get the minimum size of the dialog
	var sizeHint = oDlgWgt.minimumSizeHint;
 
	// Set the fixed size of the dialog
	wDlg.setFixedSize( 230, sizeHint.height );
 
	// Display the dialog
	wDlg.exec();
 
// Finalize the function and invoke
})();