-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProjectSerializer.cs
174 lines (168 loc) · 5.76 KB
/
ProjectSerializer.cs
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using GS_PatEditor.Editor;
using GS_PatEditor.Editor.Editable;
using GS_PatEditor.Editor.Exporters;
using GS_PatEditor.Localization;
using GS_PatEditor.Pat;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace GS_PatEditor
{
class SerializationBaseClassAttribute : Attribute
{
}
class ProjectSerializer
{
private static XmlSerializer _ProjSerializer;
private static XmlSerializer ProjSerializer
{
get
{
if (_ProjSerializer == null)
{
var allTypes = System.Reflection.Assembly.GetExecutingAssembly().GetTypes();
var baseClasses = allTypes
.Where(t => t.GetCustomAttribute<SerializationBaseClassAttribute>() != null)
.ToArray();
var types = allTypes
.Where(t => baseClasses.Any(tt => tt.IsAssignableFrom(t)))
.Where(t => !t.IsAbstract)
.ToArray();
_ProjSerializer = new XmlSerializer(typeof(Project), types);
}
return _ProjSerializer;
}
}
private static XmlSerializer _LocalSerializer;
private static XmlSerializer LocalSerializer
{
get
{
if (_LocalSerializer == null)
{
_LocalSerializer = new XmlSerializer(typeof(ProjectLocalInfo));
}
return _LocalSerializer;
}
}
public static void SaveProject(Project proj, string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (var file = File.Open(filename, FileMode.CreateNew))
{
ProjSerializer.Serialize(file, proj);
}
var localFilename = filename + ".local";
if (File.Exists(localFilename))
{
File.Delete(localFilename);
}
using (var file = File.Open(localFilename, FileMode.CreateNew))
{
SaveLocalSettings(proj, file);
}
}
public static Project OpenProject(string filename)
{
Project proj;
using (var file = File.OpenRead(filename))
{
proj = (Project)ProjSerializer.Deserialize(file);
proj.FilePath = filename;
}
if (File.Exists(filename + ".local"))
{
using (var file = File.OpenRead(filename + ".local"))
{
if (!LoadLocalSettings(proj, file))
{
return null;
}
}
}
else
{
MessageBox.Show(ProgramRes.LocalNotFound,
EditorFormCodeRes.MsgBoxTitle,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
if (!ShowDirectoryDialog(proj))
{
return null;
}
proj.ImageList.ReloadPaletteList();
}
proj.ImageList.SelectedPalette = 0;
return proj;
}
private static bool LoadLocalSettings(Project proj, FileStream file)
{
var local = (ProjectLocalInfo)LocalSerializer.Deserialize(file);
proj.LastExportDirectory = local.LastExportDirectory;
proj.LastExportFileName = local.LastExportFileName;
proj.PostExportScript = local.PostExportScript;
int notFound1 = 0;
foreach (var dir in local.Directories)
{
var dp = proj.Settings.Directories.FirstOrDefault(d => d.Name == dir.Name);
if (dp == null)
{
++notFound1;
continue;
}
dp.Path = dir.Path;
}
var notFound2 = proj.Settings.Directories.Where(d => d.Path == null);
var notFound2c = notFound2.Count();
if (notFound1 > 0)
{
MessageBox.Show(String.Format(ProgramRes.DirNotUsedFormat, notFound1),
EditorFormCodeRes.MsgBoxTitle,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (notFound2c > 0)
{
MessageBox.Show(String.Format(ProgramRes.DirNotFoundFormat, notFound2c),
EditorFormCodeRes.MsgBoxTitle,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
if (!ShowDirectoryDialog(proj))
{
return false;
}
}
return true;
}
private static void SaveLocalSettings(Project proj, FileStream file)
{
var local = new ProjectLocalInfo();
local.LoadFromProject(proj);
LocalSerializer.Serialize(file, local);
}
private static bool ShowDirectoryDialog(Project proj)
{
var dialog = new ProjectDirectoryEditForm(proj.Settings, false);
if (dialog.ShowDialog() == DialogResult.OK)
{
return true;
}
return false;
}
public static Project CopyProject(Project proj)
{
using (var ms = new MemoryStream())
{
ProjSerializer.Serialize(ms, proj);
ms.Position = 0;
return (Project)ProjSerializer.Deserialize(ms);
}
}
}
}