1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 // (c) 2008-2011 Moz (@syo68k)
4 // (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 // (c) 2011 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
10 // This file is part of OpenTween.
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
30 using System.Collections.Generic;
34 using System.Threading;
35 using System.Windows.Forms;
37 using System.Xml.Serialization;
41 public abstract class SettingBase<T>
42 where T : class, new()
45 /// XML に含まれる追加データを保持するフィールド
48 /// 他バージョンから設定をコピーした場合など、クラスに存在しないフィールドが
49 /// XML に含まれていた場合に破棄せず保持するため必要となる。
52 public XmlElement[] ExtraElements = Array.Empty<XmlElement>();
54 private static readonly object LockObj = new();
56 protected static T LoadSettings(string settingsPath, string fileId)
60 var settingFilePath = GetSettingFilePath(settingsPath, fileId);
61 if (!File.Exists(settingFilePath))
68 using var fs = new FileStream(settingFilePath, FileMode.Open, FileAccess.Read);
70 var xs = new XmlSerializer(typeof(T));
71 var instance = (T)xs.Deserialize(fs);
75 catch (FileNotFoundException)
81 var backupFile = Path.Combine(
83 Application.StartupPath,
84 ApplicationSettings.AssemblyName + "Backup1st"),
85 typeof(T).Name + fileId + ".xml");
86 if (File.Exists(backupFile))
92 using var fs = new FileStream(backupFile, FileMode.Open, FileAccess.Read);
94 var xs = new XmlSerializer(typeof(T));
95 var instance = (T)xs.Deserialize(fs);
96 MessageBox.Show("File: " + GetSettingFilePath(settingsPath, fileId) + Environment.NewLine + "Use old setting file, because application can't read this setting file.");
104 MessageBox.Show("File: " + GetSettingFilePath(settingsPath, fileId) + Environment.NewLine + "Use default setting, because application can't read this setting file.");
109 protected static T LoadSettings(string settingsPath)
110 => LoadSettings(settingsPath, "");
112 protected static void SaveSettings(T instance, string settingsPath, string fileId)
114 const int SaveRetryMax = 3;
116 if (instance == null)
120 Exception? lastException = null;
122 var filePath = GetSettingFilePath(settingsPath, fileId);
125 var tmpfilePath = GetSettingFilePath(settingsPath, "_" + Path.GetRandomFileName());
130 using (var stream = new FileStream(tmpfilePath, FileMode.Create, FileAccess.Write))
132 var serializer = new XmlSerializer(typeof(T));
133 serializer.Serialize(stream, instance);
137 var fileInfo = new FileInfo(tmpfilePath);
138 if (fileInfo.Length != 0)
141 File.Copy(tmpfilePath, filePath, true);
154 if (File.Exists(tmpfilePath))
155 File.Delete(tmpfilePath);
166 while (retryCount <= SaveRetryMax);
169 if (lastException != null)
170 MyCommon.ExceptionOut(lastException);
172 MessageBox.Show("Can't write setting XML.(" + filePath + ")", "Save Settings", MessageBoxButtons.OK);
175 protected static void SaveSettings(T instance, string settingsPath)
176 => SaveSettings(instance, settingsPath, "");
178 public static string GetSettingFilePath(string settingsPath, string fileId)
179 => Path.Combine(settingsPath, typeof(T).Name + fileId + ".xml");