chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System.Diagnostics;
using System.IO;
using System.Windows.Threading;
 
namespace OpenTap.Tui.Views
{
    public class Recovery
    {
        public class RecFile
        {
            public string FilePath { get; set; }
            public string TestPlan { get; set; }
        }
        public RecFile file = new RecFile();
        public string FilePath { get => file.FilePath; set => file.FilePath = value; }
        public string TestPlan { get => file.TestPlan; set => file.TestPlan = value; }
 
        private TestPlan plan = new TestPlan();
        public TestPlan Plan
        {
            get
            {
                return plan;
            }
            set
            {
                plan = value;
                FilePath = plan.Path;
                //Save();
                TestPlanChanged?.Invoke(plan);
            }
        }
 
        //private Stream recStream;
 
        public event Action<TestPlan> TestPlanChanged;
 
        public Recovery()
        {
            //recStream = File.OpenWrite($".{Process.GetCurrentProcess().Id}.TuiRecovery");
            //MainWindow.UnsavedChangesCreated += Save;
            //Dispatcher.CurrentDispatcher.Invoke(() =>
            //{
            //    if (string.IsNullOrEmpty(plan.Path))
            //    {
            //        if (!Load())
            //        {
            //            Plan = new TestPlan();
            //        }
            //    }
            //});
        }
 
        private static TapSerializer TapSerializer = new TapSerializer();
 
        public void Save()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                Plan.Save(ms);
                ms.Seek(0, SeekOrigin.Begin);
                using (StreamReader sr = new StreamReader(ms))
                {
                    TestPlan = sr.ReadToEnd();
                }
            }
 
            //recStream.Seek(0, SeekOrigin.Begin);
            //TapSerializer.Serialize(recStream, file);
        }
 
        public bool Load()
        {
            string[] files = Directory.GetFiles("./", ".*.TuiRecovery");
            if (files.Length == 0)
                return false;
 
            foreach (var file in files)
            {
                RecFile recfile = null;
                try
                {
                    recfile = TapSerializer.DeserializeFromFile(file, type: TypeData.FromType(typeof(Recovery))) as RecFile;
                    if (recfile == null)
                        continue;
                }
                catch
                {
                    continue;
                }
                File.Delete(file);
                //MainWindow.ContainsUnsavedChanges = true;
 
                using (MemoryStream ms = new MemoryStream(recfile.TestPlan.Length * 2))
                {
                    StreamWriter sw = new StreamWriter(ms);
                    sw.Write(recfile.TestPlan);
                    sw.Flush();
                    ms.Seek(0, SeekOrigin.Begin);
                    Plan = OpenTap.TestPlan.Load(ms, recfile.FilePath);
                    return true;
                }
            }
            return false;
        }
 
        public void RemoveRecoveryfile()
        {
            //recStream.Dispose();
            File.Delete($".{Process.GetCurrentProcess().Id}.TuiRecovery");
        }
    }
}