'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' SafeSave.vbs
' Scene versioner and validity checker
' Adds or increments a version number on the scene name,
' Saves the scene then checks validity by reloading scene
'
' Kim Aldis, 13/2/2000
'
'
' Bugs:-
'    Name/version delimitter is '-'. Possibility of Scene Name being truncated
'	if '-' exists elsewhere in the scene name.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim myVersion, SceneRoot
Dim regEx, Match, Matches 
Dim SceneName, ProjectName
	
	SceneName = GetValue( "Project.Scene" )
	ProjectName = GetValue( "Project.Dir" )
	
	
	set regEx = New RegExp 					' Create a regular expression.
	regEx.Pattern = "-[0-9]+$" 				' Look for '-' followed by any numerical combo at end of string. 
	regEx.IgnoreCase = True 					' Make it case insensetive, although who cares.
	regEx.Global = True 						' Set global applicability. 
	
	Set Matches = regEx.Execute(SceneName) ' Execute search for a number on the tail 
	
	For Each Match in Matches 					' Iterate Matches collection.
		RetStr =  Match.Value
	Next
	
	if RetStr = "" then							' No version number yet
		myVersion = "1"
		SceneRoot = SceneName
	else											' Else, we have a version number so do the biz
	
	
		myVersion = Replace( RetStr, "-", "" )	'Get rid of the '-'
		regEx.Pattern = "^.*-"
	
		Set Matches = regEx.Execute(SceneName) ' Now go get the scene name without the version num 
	
		For Each Match in Matches 					' Iterate Matches collection.
			RetStr =  Match.Value
		Next

		SceneRoot = Replace( RetStr, "-", "" )	' Again, lose the '-'
	
		myVersion = myVersion + 1					' Bump the version number
		
	end if
	
	SetValue "Project.Scene.Name", SceneRoot & "-" & myVersion 	' replace the scene name
	
	
	SceneName = ProjectName & "\" & SceneRoot & "-" & myVersion & ".scn"

	SaveSceneAs( SceneName )			' Save the scene to new name
	OpenScene	SceneName			' Load it back, see if it's OK


	msgbox( "You have just saved and reloaded your scene as " & SceneRoot & "-" & myVersion & SceneName & "   Have a nice day" )






