Description : |
Below is the source for a simple DAZ Script that will convert ascii scripts (*.ds) to their encrypted counterpart (*.dsb). |
Concepts Covered : |
|
Source : ./samples/ |
00001 /********************************************************************** 00002 File: ds2dsb.ds 00003 00004 Copyright © 2002-2006 DAZ Productions. All Rights Reserved. 00005 00006 This file is part of the DAZ Script Documentation. 00007 00008 This file may be used only in accordance with the DAZ Script 00009 license provided with the DAZ Script Documentation. 00010 00011 The contents of this file may not be disclosed to third parties, 00012 copied or duplicated in any form, in whole or in part, without the 00013 prior written permission of DAZ Productions, except as explicitly 00014 allowed in the DAZ Script license. 00015 00016 See http://www.daz3d.com to contact DAZ Productions or for more 00017 information about DAZ Script. 00018 **********************************************************************/ 00019 /***************************** 00020 Script globals 00021 *****************************/ 00022 const g_bSHIFT_PRESSED = shiftPressed(); 00023 const g_bCONTROL_PRESSED = ctrlPressed(); 00024 00025 /*********************************************************************/ 00026 // Path variables 00027 var sSrcPath, sDestPath; 00028 // If the user had the shift key pressed 00029 if( g_bSHIFT_PRESSED ) 00030 // Prompt for a directory (batch) 00031 sSrcPath = FileDialog.doDirectoryDialog( "Select a Directory" ); 00032 // If no modifier was pressed 00033 else 00034 // Prompt for an ascii script 00035 sSrcPath = FileDialog.doFileDialog( true, "Select the source file", "", "DAZ Script Plain Text (*.ds)" ); 00036 00037 // If the user didn't cancel 00038 if( sSrcPath ){ 00039 // Create a file object for the source script(s) 00040 var oFile = new DzFile( sSrcPath ); 00041 // Create a directory object for the source script(s) 00042 var oDir = new DzDir( g_bSHIFT_PRESSED ? sSrcPath : oFile.path() ); 00043 // Create a new script object 00044 var oScript = new DzScript; 00045 // Create an array to hold source file short names 00046 var aScripts = new Array; 00047 // Get the basename of the file 00048 var sShortName = String( "%1.ds" ).arg( oFile.baseName() ); 00049 // Clean up 00050 delete oFile; 00051 oFile = undefined; 00052 00053 // Get the list of all ascii scripts 00054 aScripts = g_bSHIFT_PRESSED ? oDir.entryList( "*.ds", oDir.Files ) : [ sShortName ]; 00055 00056 // While there are source files to be converted 00057 while( aScripts.length ){ 00058 // Let the user know we're busy 00059 setBusyCursor(); 00060 00061 // Make sure we're working with a new script 00062 oScript.clear(); 00063 00064 // Get the short filename 00065 sShortName = aScripts.pop(); 00066 // Construct the source file path 00067 sSrcPath = String( "%1/%2" ).arg( oDir.absPath() ).arg( sShortName ); 00068 // Construct the destination file path 00069 sDestPath = String( "%1b" ).arg( sSrcPath ); 00070 00071 // If an error occurs when loading the chosen file into the script object 00072 if( !oScript.loadFromFile( sSrcPath ) ){ 00073 // Let the user know we're done 00074 clearBusyCursor(); 00075 // Warn the user 00076 MessageBox.warning( String( "An error occured while reading the file:\n%1" ).arg( sSrcPath ), "Warning", "&OK", "" ); 00077 // NEXT!!! 00078 continue; 00079 } 00080 // If no error occurs 00081 else{ 00082 // If the script syntax is flawed 00083 if( !oScript.checkSyntax() ){ 00084 // Let the user know we're done 00085 clearBusyCursor(); 00086 // Warn the user 00087 MessageBox.warning( String( "The syntax of the source file is invalid:\n%1" ).arg( sSrcPath ), "Warning", "&OK", "" ); 00088 // NEXT!!! 00089 continue; 00090 } 00091 00092 // Write the encrypted script, record if there are errors 00093 var nWriteErr = oScript.saveToFile( sDestPath, oScript.EncDAZScriptFile ); 00094 00095 // Let the user know we're done 00096 clearBusyCursor(); 00097 00098 // If there were no errors writing the file 00099 if( nWriteErr == 0x00000000 ) 00100 // Inform the user 00101 MessageBox.information( String( "Saved encrypted script:\n%1" ).arg( sDestPath ), "Information", "&OK" ); 00102 // If an error occurs 00103 else 00104 // Warn the user 00105 MessageBox.warning( String( "An error occured while trying to save:\n%1").arg( sDestPath ), "Warning", "&OK", "" ); 00106 } 00107 00108 // If the user had the shift key pressed 00109 if( g_bSHIFT_PRESSED ) 00110 // NEXT!!! 00111 continue; 00112 } 00113 } |