1- using System . Collections ;
2- using System . Collections . Generic ;
1+ using System . Collections . Generic ;
32using UnityEngine ;
4- using UnityEngine . Events ;
53using System ;
64using System . IO ;
75using System . Threading ;
8- using System . Linq ;
96using System . Globalization ;
107
118namespace UXF
@@ -32,6 +29,16 @@ public class FileSaver : LocalFileDataHander
3229 [ Tooltip ( "Enable to print debug messages to the console." ) ]
3330 public bool verboseDebug = false ;
3431
32+ /// <summary>
33+ /// Enable backing up the session directory if it already exists when a session starts. If
34+ /// not true, this will overwrite the is the UXF runs with the same experiment name, ppid,
35+ /// and session number again.
36+ /// </summary>
37+ [ Tooltip ( "Enable backing up the session directory if it already exists when a session starts. If" +
38+ "not true, this will overwrite the is the UXF runs with the same experiment name, ppid," +
39+ "and session number again." ) ]
40+ public bool backupSessionIfExists = true ;
41+
3542 /// <summary>
3643 /// An action which does nothing.
3744 /// </summary>
@@ -46,7 +53,6 @@ public class FileSaver : LocalFileDataHander
4653
4754 bool quitting = false ;
4855
49-
5056 /// <summary>
5157 /// Starts the FileSaver Worker thread.
5258 /// </summary>
@@ -59,6 +65,37 @@ public override void SetUp()
5965
6066 quitting = false ;
6167 Directory . CreateDirectory ( base . StoragePath ) ;
68+ string sessionDirectory = GetSessionPath ( session . experimentName , session . ppid , session . number ) ;
69+ if ( Directory . Exists ( sessionDirectory ) )
70+ {
71+ Utilities . UXFDebugLogWarning ( $ "Session directory { sessionDirectory } already exists.") ;
72+ if ( backupSessionIfExists )
73+ {
74+ string suffix = Directory . GetLastWriteTime ( sessionDirectory ) . ToString ( "dd-MM-yyyy-HH-mm-FF" ) ;
75+ string backupSessionDirectory = $ "{ sessionDirectory } _{ suffix } ";
76+
77+ int idx = 1 ;
78+ while ( Directory . Exists ( backupSessionDirectory ) )
79+ {
80+ backupSessionDirectory = $ "{ backupSessionDirectory } _{ idx } ";
81+ }
82+
83+ try
84+ {
85+ Directory . Move ( sessionDirectory , backupSessionDirectory ) ;
86+ Utilities . UXFDebugLogWarning ( $ "Trying to backup { sessionDirectory } to { backupSessionDirectory } .") ;
87+ }
88+ catch ( Exception e )
89+ {
90+ Utilities . UXFDebugLogError ( $ "Failed to backup { sessionDirectory } to { backupSessionDirectory } with exception ({ e } ): { e . Message } ") ;
91+ throw e ;
92+ }
93+ }
94+ else
95+ {
96+ Utilities . UXFDebugLogWarning ( $ "backupSessionIfExists is False. Content in { sessionDirectory } may be overwritten.") ;
97+ }
98+ }
6299
63100 if ( ! IsActive )
64101 {
0 commit comments