Skip to content

Commit b667f7e

Browse files
committed
code review fixes
1 parent 4f609da commit b667f7e

File tree

1 file changed

+61
-93
lines changed

1 file changed

+61
-93
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 61 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -789,42 +789,6 @@ 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-
828792
/// <summary>
829793
/// Export all the objects in the set.
830794
/// Return the number of objects in the set that we exported.
@@ -834,22 +798,23 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
834798
exportCancelled = false;
835799
Verbose = true;
836800

837-
// If we are overwriting a file, export first to a temporary file
801+
// Export first to a temporary file
838802
// 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);
803+
// This way we won't overwrite existing files.
804+
try{
805+
m_tempFilePath = Path.GetTempFileName();
806+
}
807+
catch(IOException){
808+
return 0;
846809
}
810+
m_lastFilePath = LastFilePath;
847811

848-
if (string.IsNullOrEmpty (TempFilePath)) {
812+
if (string.IsNullOrEmpty (m_tempFilePath)) {
849813
return 0;
850814
}
851815

852816
try {
817+
bool status = false;
853818
// Create the FBX manager
854819
using (var fbxManager = FbxManager.Create ()) {
855820
// Configure fbx IO settings.
@@ -868,7 +833,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
868833
int fileFormat = EditorTools.ExportSettings.instance.embedTextures? -1 :
869834
fbxManager.GetIOPluginRegistry ().FindWriterIDByDescription ("FBX ascii (*.fbx)");
870835

871-
bool status = fbxExporter.Initialize (TempFilePath, fileFormat, fbxManager.GetIOSettings ());
836+
status = fbxExporter.Initialize (m_tempFilePath, fileFormat, fbxManager.GetIOSettings ());
872837
// Check that initialization of the fbxExporter was successful
873838
if (!status)
874839
return 0;
@@ -916,7 +881,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
916881
exportProgress = this.ExportComponents (
917882
unityGo, fbxScene, fbxRootNode, exportProgress,
918883
count, Vector3.zero, TransformExportType.Reset);
919-
if (exportProgress < 0) {
884+
if (exportCancelled || exportProgress < 0) {
920885
Debug.LogWarning ("Export Cancelled");
921886
return 0;
922887
}
@@ -929,7 +894,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
929894
foreach (var unityGo in revisedExportSet) {
930895
exportProgress = this.ExportComponents (unityGo, fbxScene, fbxRootNode,
931896
exportProgress, count, center, TransformExportType.Global);
932-
if (exportProgress < 0) {
897+
if (exportCancelled || exportProgress < 0) {
933898
Debug.LogWarning ("Export Cancelled");
934899
return 0;
935900
}
@@ -942,23 +907,27 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
942907
// cleanup
943908
fbxScene.Destroy ();
944909
fbxExporter.Destroy ();
910+
}
945911

946-
if (exportCancelled) {
947-
Debug.LogWarning ("Export Cancelled");
948-
// delete the file that got created
949-
EditorApplication.update += DeleteFile;
950-
return 0;
951-
}
952-
// delete old file, move temp file
953-
EditorApplication.update += ReplaceFile;
954-
955-
return status == true ? NumNodes : 0;
912+
if (exportCancelled) {
913+
Debug.LogWarning ("Export Cancelled");
914+
return 0;
956915
}
957-
} finally {
916+
// delete old file, move temp file
917+
ReplaceFile();
918+
AssetDatabase.Refresh();
919+
920+
return status == true ? NumNodes : 0;
921+
}
922+
finally {
958923
// You must clear the progress bar when you're done,
959924
// otherwise it never goes away and many actions in Unity
960925
// are blocked (e.g. you can't quit).
961926
EditorUtility.ClearProgressBar ();
927+
928+
// make sure the temp file is deleted, no matter
929+
// when we return
930+
DeleteTempFile();
962931
}
963932
}
964933

@@ -982,51 +951,49 @@ static bool ExportProgressCallback (float percentage, string status)
982951
}
983952

984953
/// <summary>
985-
/// On Editor update, deletes the file that got created while exporting.
954+
/// Deletes the file that got created while exporting.
986955
/// </summary>
987-
static void DeleteFile ()
956+
private void DeleteTempFile ()
988957
{
989-
if (File.Exists (TempFilePath)) {
990-
try {
991-
File.Delete (TempFilePath);
992-
} catch (IOException) {
993-
}
958+
if (!File.Exists (m_tempFilePath)) {
959+
return;
960+
}
994961

995-
if (File.Exists (TempFilePath)) {
996-
Debug.LogWarning ("Failed to delete file: " + TempFilePath);
997-
}
998-
} else {
999-
EditorApplication.update -= DeleteFile;
1000-
AssetDatabase.Refresh ();
962+
try {
963+
File.Delete (m_tempFilePath);
964+
} catch (IOException) {
965+
}
966+
967+
if (File.Exists (m_tempFilePath)) {
968+
Debug.LogWarning ("Failed to delete file: " + m_tempFilePath);
969+
EditorApplication.update -= DeleteTempFile;
1001970
}
1002971
}
1003972

1004973
/// <summary>
1005-
/// On Editor update, replaces the file we are overwriting with
974+
/// Replaces the file we are overwriting with
1006975
/// the temp file that was exported to.
1007976
/// </summary>
1008-
static void ReplaceFile ()
977+
private void ReplaceFile ()
1009978
{
1010-
if (!TempFilePath.Equals(LastFilePath) && File.Exists (TempFilePath)) {
1011-
// delete old file
1012-
try {
1013-
File.Delete (LastFilePath);
1014-
} catch (IOException) {
1015-
}
979+
if (m_tempFilePath.Equals (m_lastFilePath) || !File.Exists (m_tempFilePath)) {
980+
return;
981+
}
982+
// delete old file
983+
try {
984+
File.Delete (m_lastFilePath);
985+
} catch (IOException) {
986+
}
1016987

1017-
if (File.Exists (LastFilePath)) {
1018-
Debug.LogWarning ("Failed to delete file: " + LastFilePath);
1019-
}
988+
if (File.Exists (m_lastFilePath)) {
989+
Debug.LogWarning ("Failed to delete file: " + m_lastFilePath);
990+
}
1020991

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-
}
1027-
} else {
1028-
EditorApplication.update -= ReplaceFile;
1029-
AssetDatabase.Refresh ();
992+
// rename the new file
993+
try{
994+
File.Move(m_tempFilePath, m_lastFilePath);
995+
} catch(IOException){
996+
Debug.LogWarning (string.Format("Failed to move file {0} to {1}", m_tempFilePath, m_lastFilePath));
1030997
}
1031998
}
1032999

@@ -1277,7 +1244,8 @@ public void Dispose ()
12771244
/// manage the selection of a filename
12781245
/// </summary>
12791246
static string LastFilePath { get; set; }
1280-
static string TempFilePath { get; set; }
1247+
private string m_tempFilePath { get; set; }
1248+
private string m_lastFilePath { get; set; }
12811249

12821250
const string Extension = "fbx";
12831251

0 commit comments

Comments
 (0)