Example #1
0
 internal void   AddCachedComponentFixes(CachedLineFix line)
 {
     if (this.useCache == true)
     {
         this.cachedComponentFixes.Add(line);
     }
 }
Example #2
0
        public void     FixLine(NGMissingScriptRecoveryWindow window, string componentID, Type type, string fixedLine = null)
        {
            string prefabPath = AssetDatabase.GetAssetPath(window.Target);

            string[] lines = File.ReadAllLines(prefabPath);

            for (int m = 0; m < lines.Length; m++)
            {
                if (lines[m].Length > 12 && lines[m][0] == '-' && lines[m][2] == '-' && lines[m].EndsWith(componentID) == true)
                {
                    for (; m < lines.Length; m++)
                    {
                        if (lines[m].StartsWith("  m_Script") == true)
                        {
                            if (fixedLine == null)
                            {
                                CachedLineFix lineFix = window.FindCachedComponentFixes(lines[m]);

                                // Get the cached line only if the Type matches.
                                if (lineFix != null && lineFix.type == type)
                                {
                                    fixedLine = lineFix.fixedLine;
                                }
                                else
                                {
                                    fixedLine = this.ExtractFixGUIDFromTempPrefab();

                                    if (string.IsNullOrEmpty(fixedLine) == true)
                                    {
                                        InternalNGDebug.LogError("Impossible to extract new GUID from temp prefab. Recovery aborted.");
                                        return;
                                    }

                                    if (lineFix != null)
                                    {
                                        lineFix.fixedLine = fixedLine;
                                        lineFix.type      = type;
                                    }
                                    else
                                    {
                                        window.AddCachedComponentFixes(new CachedLineFix()
                                        {
                                            brokenLine = lines[m], fixedLine = fixedLine, type = type
                                        });
                                    }
                                }
                            }

                            lines[m] = fixedLine;

                            try
                            {
                                File.WriteAllLines(prefabPath, lines);
                                AssetDatabase.Refresh();
                            }
                            catch (IOException ex)
                            {
                                InternalNGDebug.LogException("Recovering \"" + prefabPath + "\" failed. File seems to be locked. Please try to recover again or restart Unity or your computer.", ex);
                            }
                            catch (Exception ex)
                            {
                                InternalNGDebug.LogException("Recovering \"" + prefabPath + "\" failed. Please try to recover again.", ex);
                            }

                            break;
                        }
                    }

                    break;
                }
            }
        }
Example #3
0
        public void     Draw(NGMissingScriptRecoveryWindow window, bool force = false)
        {
            if (this.cachedFields == null)
            {
                StringBuilder buffer = Utility.GetBuffer();

                for (int i = 0; i < this.fields.Count; i++)
                {
                    buffer.AppendLine(this.fields[i]);
                }

                if (buffer.Length > 0)
                {
                    buffer.Length -= Environment.NewLine.Length;
                }

                this.cachedFields = Utility.ReturnBuffer(buffer);
            }

            EditorGUI.BeginDisabledGroup(this.fields.Count <= 0);
            {
                Utility.content.text  = this.name;
                Utility.content.image = Utility.GetIcon(this.asset ? this.asset.GetInstanceID() : 0);
                this.open             = EditorGUILayout.Foldout(this.open, Utility.content) && this.fields.Count > 0 || force;
                Utility.content.image = null;
            }
            EditorGUI.EndDisabledGroup();

            if (this.fields.Count == 0 && this.asset == null)
            {
                EditorGUILayout.HelpBox("Component seems to have absolutely no fields. Recovery can not proceed.", MessageType.Warning);
            }

            if (this.open == false)
            {
                return;
            }

            ++EditorGUI.indentLevel;
            this.fieldsOpen = EditorGUILayout.Foldout(this.fieldsOpen, "Fields found (" + this.fields.Count + ")");
            if (this.fieldsOpen == true)
            {
                ++EditorGUI.indentLevel;
                Utility.content.text = this.cachedFields;
                float h = EditorStyles.label.CalcHeight(Utility.content, window.position.width - 60F);
                //float h = this.fields.Count * 15F;
                float totalH = h;

                if (h >= NGMissingScriptRecoveryWindow.MaxFieldsHeight)
                {
                    h = NGMissingScriptRecoveryWindow.MaxFieldsHeight;
                }

                //Utility.content.text = this.cachedFields;
                //Rect	viewRect = new Rect();
                //Rect	body = new Rect(4F, GUILayoutUtility.GetLastRect().yMax, window.position.width, 0F);
                //Rect	r2 = GUILayoutUtility.GetRect(0F, h);
                //body.height = r2.height;
                //viewRect.height = EditorStyles.label.CalcSize(Utility.content).y;

                //EditorGUI.DrawRect(body, Color.blue);
                //this.scrollPositionFields = GUI.BeginScrollView(body, this.scrollPositionFields, viewRect);
                //{
                //	Rect	selectableRect = body;
                //	selectableRect.x = 0F;
                //	selectableRect.y = 0F;
                //	selectableRect.width = body.width - (viewRect.height > body.height ? 16F : 0F);
                //	selectableRect.height = viewRect.height > body.height ? EditorStyles.label.CalcHeight(Utility.content, selectableRect.width) : viewRect.height;
                //	EditorGUI.SelectableLabel(selectableRect, this.cachedFields, EditorStyles.label);
                //}
                //GUI.EndScrollView();

                // TODO Fix bottom margin/padding?
                EditorGUI.HelpBox(new Rect(30F, GUILayoutUtility.GetLastRect().yMax, window.position.width - 60F, h + 10F), string.Empty, MessageType.None);
                //if (useScrollbar == true)
                this.scrollPositionFields = EditorGUILayout.BeginScrollView(this.scrollPositionFields, GUILayoutOptionPool.Height(h + 5F), GUILayoutOptionPool.Width(window.position.width - 30F));
                //else
                //EditorGUI.HelpBox(new Rect(4F, GUILayoutUtility.GetLastRect().yMax, window.position.width, h), string.Empty, MessageType.None);
                {
                    EditorGUILayout.SelectableLabel(this.cachedFields, EditorStyles.label, GUILayoutOptionPool.Height(totalH));
                    //for (int j = 0; j < this.fields.Count; j++)
                    //	EditorGUILayout.LabelField(this.fields[j]);
                }
                //if (useScrollbar == true)
                EditorGUILayout.EndScrollView();
                --EditorGUI.indentLevel;
            }

            CachedLineFix lineFix = window.FindCachedComponentFixes(this.line);

            if (lineFix != null)
            {
                if (GUILayout.Button("Recover From Cache (" + lineFix.type + ")") == true)
                {
                    this.FixLine(window, this.componentID, lineFix.type);
                    window.Diagnose(window.Target);
                }
            }

            EditorGUILayout.LabelField("Potential types:");
            ++EditorGUI.indentLevel;
            if (this.potentialTypes.Count == 0)
            {
                EditorGUILayout.LabelField("No type available.");
            }
            else
            {
                for (int l = 0; l < this.potentialTypes.Count; l++)
                {
                    EditorGUILayout.BeginHorizontal();
                    {
                        GUILayout.Space(30F);

                        if (GUILayout.Button(this.potentialTypes[l].type.FullName, GUILayoutOptionPool.ExpandWidthFalse) == true)
                        {
                            this.FixMissingComponent(window, this.componentID, this.potentialTypes[l].type);
                            window.Diagnose(window.Target);
                            return;
                        }

                        if (this.potentialTypes[l].matchingFields == this.fields.Count &&
                            this.potentialTypes[l].fields.Length == this.fields.Count)
                        {
                            GUILayout.Label("(Perfect match)", GUILayoutOptionPool.ExpandWidthFalse);
                        }
                        else
                        {
                            int extraFields = this.potentialTypes[l].fields.Length == this.potentialTypes[l].matchingFields ? this.potentialTypes[l].matchingFields - this.fields.Count : this.potentialTypes[l].fields.Length - this.potentialTypes[l].matchingFields;
                            GUILayout.Label("(Fields: " + this.potentialTypes[l].matchingFields + " matching" + (extraFields > 0 ? ", +" + extraFields + " extra" : (extraFields < 0 ? ", " + extraFields + " missing" : string.Empty)) + ")", GUILayoutOptionPool.ExpandWidthFalse);
                        }

                        if (Event.current.type == EventType.MouseMove)
                        {
                            Rect r = GUILayoutUtility.GetLastRect();

                            r.xMin = 0F;
                            if (r.Contains(Event.current.mousePosition) == true)
                            {
                                if (this.hoverPopup != null)
                                {
                                    if (this.hoverPopup.potentialType.type != this.potentialTypes[l].type)
                                    {
                                        if (this.hoverPopup.editorWindow != null)
                                        {
                                            this.hoverPopup.editorWindow.Close();
                                        }
                                        this.hoverPopup = new HighlightMatchesPopup(this, this.potentialTypes[l]);
                                        r.x             = 30F;
                                        r.y            += 5F;
                                        PopupWindow.Show(r, this.hoverPopup);
                                    }
                                }
                                else
                                {
                                    this.hoverPopup = new HighlightMatchesPopup(this, this.potentialTypes[l]);
                                    r.x             = 30F;
                                    r.y            += 5F;
                                    PopupWindow.Show(r, this.hoverPopup);
                                }
                            }
                            else
                            {
                                if (this.hoverPopup != null && this.hoverPopup.potentialType.type == this.potentialTypes[l].type)
                                {
                                    if (this.hoverPopup.editorWindow != null)
                                    {
                                        this.hoverPopup.editorWindow.Close();
                                    }
                                    this.hoverPopup = null;
                                }
                            }
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                }
            }
            --EditorGUI.indentLevel;
            --EditorGUI.indentLevel;
        }
Example #4
0
        private IEnumerator     RecoveryTask()
        {
            Selection.activeObject = null;

            this.currentGameObject = 0;
            this.isRecovering      = true;
            this.tab = Tab.Recovery;

            StringBuilder buffer = null;
            DateTime      start  = DateTime.Now;

            if (string.IsNullOrEmpty(this.recoveryLogFilePath) == false)
            {
                buffer = Utility.GetBuffer();
                InternalNGDebug.Log("Started recovery at " + start.ToLongTimeString() + ".");
                buffer.AppendLine("Started recovery at " + start.ToLongTimeString() + ".");
            }

            this.PostDiagnostic += this.OnPostDiagnosticAutomaticRecovery;

            while (this.currentGameObject < this.missings.Count && this.isRecovering == true)
            {
                this.Diagnose(this.missings[this.currentGameObject].gameObject);

                if (this.recoveryMode == RecoveryMode.Automatic && this.skipFix == false)
                {
                    EditorUtility.DisplayProgressBar(NGMissingScriptRecoveryWindow.NormalTitle + " (" + this.currentGameObject + " / " + this.missings.Count + ")", "Recovering " + this.missings[this.currentGameObject].path, (float)this.currentGameObject / (float)this.missings.Count);

                    InternalNGDebug.Log("Recovering GameObject " + this.missings[this.currentGameObject].path + ".");
                    if (buffer != null)
                    {
                        buffer.AppendLine("Recovering GameObject " + this.missings[this.currentGameObject].path + ".");
                    }

                    for (int i = 0; i < this.rawComponents.Count && this.isRecovering == true; ++i)
                    {
                        if (this.rawComponents[i].fields.Count == 0)
                        {
                            continue;
                        }

                        if (this.useCache == true)
                        {
                            // Fix from cache.
                            CachedLineFix lineFix = this.cachedComponentFixes.Find(c => c.brokenLine == this.rawComponents[i].line);
                            if (lineFix != null)
                            {
                                this.rawComponents[i].FixLine(this, this.rawComponents[i].componentID, lineFix.type);
                                this.Repaint();

                                InternalNGDebug.Log("Recovered Component \"" + this.rawComponents[i].name + "\" (" + i + ") from cache (Type \"" + lineFix.type + "\").");
                                if (buffer != null)
                                {
                                    buffer.AppendLine("Recovered Component \"" + this.rawComponents[i].name + "\" (" + i + ") from cache (Type \"" + lineFix.type + "\").");
                                }

                                continue;
                            }
                        }

                        int  perfectMatches = 0;
                        Type matchType      = null;

                        for (int j = 0; j < this.rawComponents[i].potentialTypes.Count; j++)
                        {
                            if (this.rawComponents[i].potentialTypes[j].matchingFields == this.rawComponents[i].fields.Count &&
                                this.rawComponents[i].fields.Count == this.rawComponents[i].potentialTypes[j].fields.Length)
                            {
                                ++perfectMatches;
                                matchType = this.rawComponents[i].potentialTypes[j].type;
                            }
                        }

                        if (perfectMatches == 1)
                        {
                            this.rawComponents[i].FixMissingComponent(this, this.rawComponents[i].componentID, matchType);

                            InternalNGDebug.Log("Recovered Component \"" + this.rawComponents[i].name + "\" (" + i + ") with Type \"" + matchType.FullName + "\".");
                            if (buffer != null)
                            {
                                buffer.AppendLine("Recovered Component \"" + this.rawComponents[i].name + "\" (" + i + ") with Type \"" + matchType.FullName + "\".");
                            }
                        }
                        else if (this.rawComponents[i].potentialTypes.Count == 0)
                        {
                            InternalNGDebug.Log("Component \"" + this.rawComponents[i].name + "\" (" + i + ") has no potential type.");
                            if (buffer != null)
                            {
                                buffer.AppendLine("Component \"" + this.rawComponents[i].name + "\" (" + i + ") has no potential type.");
                            }
                        }
                        else                         // Ask the user to fix it.
                        {
                            if (this.promptOnPause == true)
                            {
                                if (EditorUtility.DisplayDialog(NGMissingScriptRecoveryWindow.NormalTitle, "Recovery requires your attention.", "OK", "Skip alert for next cases") == false)
                                {
                                    this.promptOnPause = false;
                                }
                            }

                            this.Diagnose(this.missings[this.currentGameObject].gameObject);
                            this.isPausing = true;
                            EditorUtility.ClearProgressBar();

                            InternalNGDebug.Log("Paused on Component \"" + this.rawComponents[i].name + "\" (" + i + ").");
                            if (buffer != null)
                            {
                                buffer.AppendLine("Paused on Component \"" + this.rawComponents[i].name + "\" (" + i + ").");
                            }

                            break;
                        }
                    }

                    if (this.isPausing == false)
                    {
                        this.currentGameObject++;
                    }
                }
                else
                {
                    this.isPausing = true;
                }

                this.skipFix       = false;
                this.isGUIRendered = false;

                while (this.isPausing == this.isRecovering)
                {
                    yield return(null);
                }

                while (this.supaFast == false && this.isGUIRendered == false)
                {
                    this.Repaint();
                    yield return(null);
                }
            }

            this.PostDiagnostic -= this.OnPostDiagnosticAutomaticRecovery;

            EditorUtility.ClearProgressBar();

            DateTime end      = DateTime.Now;
            TimeSpan duration = end - start;

            InternalNGDebug.Log("Ended recovery at " + end.ToLongTimeString() + " (" + duration.TotalSeconds + "s).");

            if (buffer != null)
            {
                buffer.AppendLine("Ended recovery at " + end.ToLongTimeString() + " (" + duration.TotalSeconds + "s).");
                buffer.AppendLine();
                File.AppendAllText(this.recoveryLogFilePath, Utility.ReturnBuffer(buffer));
                InternalNGDebug.Log("Recovery log saved at \"" + this.recoveryLogFilePath + "\".");
            }

            AssetDatabase.Refresh();

            this.target    = null;
            this.hasResult = false;

            this.Repaint();

            this.isRecovering = false;
        }