Skip to content

Commit 4f609da

Browse files
committed
write to temporary file when exporting
if we are replacing a file that already exists
1 parent e4b8f87 commit 4f609da

File tree

1 file changed

+90
-3
lines changed

1 file changed

+90
-3
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,42 @@ private Vector3 GetRecenteredTranslation(Transform t, Vector3 center)
789789

790790
public enum TransformExportType { Local, Global, Reset };
791791

792+
protected const string DefaultFileNamePrefix = "_safe_to_delete__";
793+
protected const string DefaultFileNameExt = ".fbx";
794+
795+
private string MakeFileName(string baseName = null, string prefixName = null, string extName = null)
796+
{
797+
if (baseName==null)
798+
baseName = Path.GetRandomFileName();
799+
800+
if (prefixName==null)
801+
prefixName = DefaultFileNamePrefix;
802+
803+
if (extName==null)
804+
extName = DefaultFileNameExt;
805+
806+
return prefixName + baseName + extName;
807+
}
808+
809+
protected string GetRandomFileNamePath(string pathName, string prefixName = null, string extName = null)
810+
{
811+
string temp;
812+
813+
if (prefixName==null)
814+
prefixName = DefaultFileNamePrefix;
815+
816+
if (extName==null)
817+
extName = DefaultFileNameExt;
818+
819+
// repeat until you find a file that does not already exist
820+
do {
821+
temp = Path.Combine (pathName, MakeFileName(prefixName: prefixName, extName: extName));
822+
823+
} while(File.Exists (temp));
824+
825+
return temp;
826+
}
827+
792828
/// <summary>
793829
/// Export all the objects in the set.
794830
/// Return the number of objects in the set that we exported.
@@ -797,6 +833,22 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
797833
{
798834
exportCancelled = false;
799835
Verbose = true;
836+
837+
// If we are overwriting a file, export first to a temporary file
838+
// in case the export is cancelled.
839+
TempFilePath = LastFilePath;
840+
if (File.Exists (LastFilePath)) {
841+
var directory = string.IsNullOrEmpty (LastFilePath)
842+
? Application.dataPath
843+
: System.IO.Path.GetDirectoryName (LastFilePath);
844+
845+
TempFilePath = GetRandomFileNamePath (directory);
846+
}
847+
848+
if (string.IsNullOrEmpty (TempFilePath)) {
849+
return 0;
850+
}
851+
800852
try {
801853
// Create the FBX manager
802854
using (var fbxManager = FbxManager.Create ()) {
@@ -816,7 +868,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
816868
int fileFormat = EditorTools.ExportSettings.instance.embedTextures? -1 :
817869
fbxManager.GetIOPluginRegistry ().FindWriterIDByDescription ("FBX ascii (*.fbx)");
818870

819-
bool status = fbxExporter.Initialize (LastFilePath, fileFormat, fbxManager.GetIOSettings ());
871+
bool status = fbxExporter.Initialize (TempFilePath, fileFormat, fbxManager.GetIOSettings ());
820872
// Check that initialization of the fbxExporter was successful
821873
if (!status)
822874
return 0;
@@ -897,6 +949,8 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
897949
EditorApplication.update += DeleteFile;
898950
return 0;
899951
}
952+
// delete old file, move temp file
953+
EditorApplication.update += ReplaceFile;
900954

901955
return status == true ? NumNodes : 0;
902956
}
@@ -927,9 +981,34 @@ static bool ExportProgressCallback (float percentage, string status)
927981
return !cancel;
928982
}
929983

984+
/// <summary>
985+
/// On Editor update, deletes the file that got created while exporting.
986+
/// </summary>
930987
static void DeleteFile ()
931988
{
932-
if (File.Exists (LastFilePath)) {
989+
if (File.Exists (TempFilePath)) {
990+
try {
991+
File.Delete (TempFilePath);
992+
} catch (IOException) {
993+
}
994+
995+
if (File.Exists (TempFilePath)) {
996+
Debug.LogWarning ("Failed to delete file: " + TempFilePath);
997+
}
998+
} else {
999+
EditorApplication.update -= DeleteFile;
1000+
AssetDatabase.Refresh ();
1001+
}
1002+
}
1003+
1004+
/// <summary>
1005+
/// On Editor update, replaces the file we are overwriting with
1006+
/// the temp file that was exported to.
1007+
/// </summary>
1008+
static void ReplaceFile ()
1009+
{
1010+
if (!TempFilePath.Equals(LastFilePath) && File.Exists (TempFilePath)) {
1011+
// delete old file
9331012
try {
9341013
File.Delete (LastFilePath);
9351014
} catch (IOException) {
@@ -938,8 +1017,15 @@ static void DeleteFile ()
9381017
if (File.Exists (LastFilePath)) {
9391018
Debug.LogWarning ("Failed to delete file: " + LastFilePath);
9401019
}
1020+
1021+
// rename the new file
1022+
try{
1023+
File.Move(TempFilePath, LastFilePath);
1024+
} catch(IOException){
1025+
Debug.LogWarning (string.Format("Failed to move file {0} to {1}", TempFilePath, LastFilePath));
1026+
}
9411027
} else {
942-
EditorApplication.update -= DeleteFile;
1028+
EditorApplication.update -= ReplaceFile;
9431029
AssetDatabase.Refresh ();
9441030
}
9451031
}
@@ -1191,6 +1277,7 @@ public void Dispose ()
11911277
/// manage the selection of a filename
11921278
/// </summary>
11931279
static string LastFilePath { get; set; }
1280+
static string TempFilePath { get; set; }
11941281

11951282
const string Extension = "fbx";
11961283

0 commit comments

Comments
 (0)