Skip to content

Commit c905772

Browse files
committed
check how many assets need update before updating
also prompt user if they wish to update, and encourage them to make a backup.
1 parent aab851b commit c905772

File tree

2 files changed

+77
-27
lines changed

2 files changed

+77
-27
lines changed

Assets/FbxExporters/Editor/FbxExportSettings.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -180,27 +180,33 @@ public override void OnInspectorGUI() {
180180
}
181181
EditorGUI.EndDisabledGroup ();
182182

183-
if (!HideRepairMissingScripts ()) {
184-
EditorGUILayout.Space ();
185-
186-
EditorGUI.indentLevel--;
187-
EditorGUILayout.LabelField ("Repair Missing Scripts", EditorStyles.boldLabel);
188-
EditorGUI.indentLevel++;
183+
EditorGUILayout.Space ();
189184

190-
EditorGUILayout.Space ();
185+
EditorGUI.indentLevel--;
186+
EditorGUILayout.LabelField ("Run Component Updater", EditorStyles.boldLabel);
187+
EditorGUI.indentLevel++;
191188

192-
var repairMissingScripts = new GUIContent (
193-
"Repair Missing Scripts",
194-
"Repair missing FbxPrefab scripts in text assets");
195-
if (GUILayout.Button (repairMissingScripts)) {
196-
bool result = FbxExporters.Editor.RepairMissingScripts.ReplaceGUIDInTextAssets ();
189+
EditorGUILayout.Space ();
190+
191+
var repairMissingScripts = new GUIContent (
192+
"Run Component Updater",
193+
"Repair missing FbxPrefab scripts in text assets");
194+
if (GUILayout.Button (repairMissingScripts)) {
195+
var componentUpdater = new FbxExporters.Editor.RepairMissingScripts ();
196+
var filesToRepairCount = componentUpdater.GetAssetsToRepairCount ();
197+
if (filesToRepairCount > 0) {
198+
bool result = UnityEditor.EditorUtility.DisplayDialog ("Component Updater",
199+
string.Format("Found {0} text assets with components requiring update.\n\n" +
200+
"If you choose 'Go Ahead', the components in these text serialized assets " +
201+
"will be automatically updated to work with the latest FBX exporter.\n" +
202+
"You should make a backup before proceeding.", filesToRepairCount),
203+
"I Made a Backup. Go Ahead!", "No Thanks");
197204
if (result) {
198-
UnityEditor.EditorUtility.DisplayDialog ("Finished Repairing Scripts",
199-
"Repaired missing scripts in text serialized assets", "Ok");
200-
} else {
201-
UnityEditor.EditorUtility.DisplayDialog ("Finished Repairing Scripts",
202-
"Couldn't find any assets needing repair", "Ok");
205+
componentUpdater.ReplaceGUIDInTextAssets ();
203206
}
207+
} else {
208+
UnityEditor.EditorUtility.DisplayDialog ("Component Updater",
209+
"Couldn't find any text assets requiring update", "Ok");
204210
}
205211
}
206212

@@ -214,11 +220,6 @@ public override void OnInspectorGUI() {
214220
}
215221
}
216222

217-
private static bool HideRepairMissingScripts(){
218-
var docPath = Application.dataPath + "/FbxExporters/FBX_Exporter_User_Guide_v1.1.0b1.pdf";
219-
return File.Exists(docPath)? false : true;
220-
}
221-
222223
private static string TryFindDCC(string dccPath, string ext, ExportSettings.DCCType dccType){
223224
string dccName = "";
224225
switch (dccType) {

Assets/FbxExporters/Editor/FbxExporterRepairMissingScripts.cs

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class RepairMissingScripts
1515

1616
private const string m_idFormat = "{{fileID: {0}, guid: {1}, type:";
1717

18-
1918
private static string m_forumPackageSearchID;
2019

2120
private static string ForumPackageSearchID {
@@ -38,24 +37,74 @@ private static string CurrentPackageSearchID {
3837
}
3938
}
4039

41-
public static bool ReplaceGUIDInTextAssets ()
40+
private string[] m_assetsToRepair;
41+
42+
public RepairMissingScripts(){
43+
m_assetsToRepair = GetAssetsToRepair ();
44+
}
45+
46+
public int GetAssetsToRepairCount(){
47+
return (m_assetsToRepair != null) ? m_assetsToRepair.Length : 0;
48+
}
49+
50+
public static string[] GetAssetsToRepair()
4251
{
4352
// search project for assets containing old GUID
4453

4554
// ignore if forced binary
4655
if (UnityEditor.EditorSettings.serializationMode == SerializationMode.ForceBinary) {
47-
return false;
56+
return new string[]{};
4857
}
4958

5059
// check all scenes and prefabs
5160
string[] searchFilePatterns = new string[]{ "*.prefab", "*.unity" };
5261

53-
bool replacedGUID = false;
62+
List<string> assetsToRepair = new List<string> ();
5463
foreach (string searchPattern in searchFilePatterns) {
5564
foreach (string file in Directory.GetFiles(Application.dataPath, searchPattern, SearchOption.AllDirectories)) {
56-
replacedGUID |= ReplaceGUIDInFile (file);
65+
if (AssetNeedsRepair (file)) {
66+
assetsToRepair.Add (file);
67+
}
5768
}
5869
}
70+
return assetsToRepair.ToArray ();
71+
}
72+
73+
private static bool AssetNeedsRepair(string filePath)
74+
{
75+
try{
76+
using(var sr = new StreamReader (filePath)){
77+
if(sr.Peek() > -1){
78+
var firstLine = sr.ReadLine();
79+
if(!firstLine.StartsWith("%YAML")){
80+
sr.Close();
81+
return false;
82+
}
83+
}
84+
85+
var contents = sr.ReadToEnd();
86+
if(contents.Contains(ForumPackageSearchID)){
87+
sr.Close();
88+
return true;
89+
}
90+
}
91+
}
92+
catch(IOException e){
93+
Debug.LogError (string.Format ("Failed to check file for component update: {0} (error={1})", filePath, e));
94+
}
95+
return false;
96+
}
97+
98+
public bool ReplaceGUIDInTextAssets ()
99+
{
100+
if (m_assetsToRepair == null) {
101+
return false;
102+
}
103+
104+
bool replacedGUID = false;
105+
foreach (string file in m_assetsToRepair) {
106+
replacedGUID |= ReplaceGUIDInFile (file);
107+
}
59108
if (replacedGUID) {
60109
AssetDatabase.Refresh ();
61110
}

0 commit comments

Comments
 (0)