Skip to content

Commit 6b4c8b7

Browse files
authored
Merge pull request #57 from Unity-Technologies/UNI-20471-write-to-temp-file-when-exporting
UNI-20471 write to temporary file when exporting
2 parents e93813a + 6ef435a commit 6b4c8b7

File tree

1 file changed

+78
-24
lines changed

1 file changed

+78
-24
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,24 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
907907
{
908908
exportCancelled = false;
909909
Verbose = true;
910+
911+
// Export first to a temporary file
912+
// in case the export is cancelled.
913+
// This way we won't overwrite existing files.
914+
try{
915+
m_tempFilePath = Path.GetTempFileName();
916+
}
917+
catch(IOException){
918+
return 0;
919+
}
920+
m_lastFilePath = LastFilePath;
921+
922+
if (string.IsNullOrEmpty (m_tempFilePath)) {
923+
return 0;
924+
}
925+
910926
try {
927+
bool status = false;
911928
// Create the FBX manager
912929
using (var fbxManager = FbxManager.Create ()) {
913930
// Configure fbx IO settings.
@@ -926,7 +943,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
926943
int fileFormat = EditorTools.ExportSettings.instance.embedTextures? -1 :
927944
fbxManager.GetIOPluginRegistry ().FindWriterIDByDescription ("FBX ascii (*.fbx)");
928945

929-
bool status = fbxExporter.Initialize (LastFilePath, fileFormat, fbxManager.GetIOSettings ());
946+
status = fbxExporter.Initialize (m_tempFilePath, fileFormat, fbxManager.GetIOSettings ());
930947
// Check that initialization of the fbxExporter was successful
931948
if (!status)
932949
return 0;
@@ -983,7 +1000,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
9831000
exportProgress = this.ExportComponents (
9841001
unityGo, fbxScene, fbxRootNode, exportProgress,
9851002
count, Vector3.zero, TransformExportType.Reset);
986-
if (exportProgress < 0) {
1003+
if (exportCancelled || exportProgress < 0) {
9871004
Debug.LogWarning ("Export Cancelled");
9881005
return 0;
9891006
}
@@ -996,7 +1013,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
9961013
foreach (var unityGo in revisedExportSet) {
9971014
exportProgress = this.ExportComponents (unityGo, fbxScene, fbxRootNode,
9981015
exportProgress, count, center, TransformExportType.Global);
999-
if (exportProgress < 0) {
1016+
if (exportCancelled || exportProgress < 0) {
10001017
Debug.LogWarning ("Export Cancelled");
10011018
return 0;
10021019
}
@@ -1008,21 +1025,27 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
10081025
// cleanup
10091026
fbxScene.Destroy ();
10101027
fbxExporter.Destroy ();
1028+
}
10111029

1012-
if (exportCancelled) {
1013-
Debug.LogWarning ("Export Cancelled");
1014-
// delete the file that got created
1015-
EditorApplication.update += DeleteFile;
1016-
return 0;
1017-
}
1018-
1019-
return status == true ? NumNodes : 0;
1030+
if (exportCancelled) {
1031+
Debug.LogWarning ("Export Cancelled");
1032+
return 0;
10201033
}
1021-
} finally {
1034+
// delete old file, move temp file
1035+
ReplaceFile();
1036+
AssetDatabase.Refresh();
1037+
1038+
return status == true ? NumNodes : 0;
1039+
}
1040+
finally {
10221041
// You must clear the progress bar when you're done,
10231042
// otherwise it never goes away and many actions in Unity
10241043
// are blocked (e.g. you can't quit).
10251044
EditorUtility.ClearProgressBar ();
1045+
1046+
// make sure the temp file is deleted, no matter
1047+
// when we return
1048+
DeleteTempFile();
10261049
}
10271050
}
10281051

@@ -1045,20 +1068,49 @@ static bool ExportProgressCallback (float percentage, string status)
10451068
return !cancel;
10461069
}
10471070

1048-
static void DeleteFile ()
1071+
/// <summary>
1072+
/// Deletes the file that got created while exporting.
1073+
/// </summary>
1074+
private void DeleteTempFile ()
10491075
{
1050-
if (File.Exists (LastFilePath)) {
1051-
try {
1052-
File.Delete (LastFilePath);
1053-
} catch (IOException) {
1054-
}
1076+
if (!File.Exists (m_tempFilePath)) {
1077+
return;
1078+
}
10551079

1056-
if (File.Exists (LastFilePath)) {
1057-
Debug.LogWarning ("Failed to delete file: " + LastFilePath);
1058-
}
1059-
} else {
1060-
EditorApplication.update -= DeleteFile;
1061-
AssetDatabase.Refresh ();
1080+
try {
1081+
File.Delete (m_tempFilePath);
1082+
} catch (IOException) {
1083+
}
1084+
1085+
if (File.Exists (m_tempFilePath)) {
1086+
Debug.LogWarning ("Failed to delete file: " + m_tempFilePath);
1087+
}
1088+
}
1089+
1090+
/// <summary>
1091+
/// Replaces the file we are overwriting with
1092+
/// the temp file that was exported to.
1093+
/// </summary>
1094+
private void ReplaceFile ()
1095+
{
1096+
if (m_tempFilePath.Equals (m_lastFilePath) || !File.Exists (m_tempFilePath)) {
1097+
return;
1098+
}
1099+
// delete old file
1100+
try {
1101+
File.Delete (m_lastFilePath);
1102+
} catch (IOException) {
1103+
}
1104+
1105+
if (File.Exists (m_lastFilePath)) {
1106+
Debug.LogWarning ("Failed to delete file: " + m_lastFilePath);
1107+
}
1108+
1109+
// rename the new file
1110+
try{
1111+
File.Move(m_tempFilePath, m_lastFilePath);
1112+
} catch(IOException){
1113+
Debug.LogWarning (string.Format("Failed to move file {0} to {1}", m_tempFilePath, m_lastFilePath));
10621114
}
10631115
}
10641116

@@ -1356,6 +1408,8 @@ public void Dispose ()
13561408
/// manage the selection of a filename
13571409
/// </summary>
13581410
static string LastFilePath { get; set; }
1411+
private string m_tempFilePath { get; set; }
1412+
private string m_lastFilePath { get; set; }
13591413

13601414
const string Extension = "fbx";
13611415

0 commit comments

Comments
 (0)