/// <summary> /// 指定したファイルパスから設定をロードします。 /// </summary> /// <param name="path"></param> /// <param name="mode"></param> /// <param name="content"></param> /// <param name="fromAutomation"></param> public void LoadSetting( string path, SettingFileReadWriteModes mode, SettingFileReadContent content = SettingFileReadContent.All, bool fromAutomation = false ) { if (!File.Exists(path)) { LogOutput.Instance.Write($"Setting file load requested (mode={mode}, but file does not exist at: {path}"); return; } try { //NOTE: ファイルロードではメッセージが凄い量になるので、 //コンポジットして「1つの大きいメッセージ」として書き込むためにこうしてます _sender.StartCommandComposite(); LoadSettingSub(path, mode, content, fromAutomation); _sender.EndCommandComposite(); } catch (Exception ex) { MessageBox.Show($"Failed to load setting file {path} : {ex.Message}"); } }
private void SaveSettingSub(SettingFileReadWriteModes mode, Action <SaveData> act) { var autoLoadEnabled = _model.AutoLoadLastLoadedVrm.Value; var saveData = new SaveData() { //ここ若干名前がややこしいが、歴史的経緯によるものです IsInternalSaveFile = (mode == SettingFileReadWriteModes.AutoSave), LastLoadedVrmFilePath = mode switch { SettingFileReadWriteModes.AutoSave => autoLoadEnabled ? _model.LastVrmLoadFilePath : "", SettingFileReadWriteModes.Internal => _model.LastVrmLoadFilePath, _ => "", },
public void SaveSetting(string path, SettingFileReadWriteModes mode) { if (File.Exists(path)) { File.Delete(path); } //データを作ったらXMLで保存してるだけ using (var sw = new StreamWriter(path)) { SaveSettingSub( mode, saveData => new XmlSerializer(typeof(SaveData)).Serialize(sw, saveData) ); } }
/// <summary> /// いま指定したファイルにセーブを行ったらそのファイルの中身が書き換わるかどうかをチェックします。 /// ファイルがないばあいtrueを返します。 /// よくあるDirty判定と違い、「設定ファイルの文字列を試しに作って書き込み対象ファイルの中身と比べる」 /// という非常に強引な実装であることに注意して下さい。 /// </summary> /// <param name="path"></param> /// <param name="mode"></param> /// <returns></returns> public bool CheckSettingIsDifferent(string path, SettingFileReadWriteModes mode) { if (!File.Exists(path)) { return(true); } var settingInFile = File.ReadAllText(path); var sb = new StringBuilder(); using (var sw = new StringWriter(sb)) { SaveSettingSub( mode, saveData => new XmlSerializer(typeof(SaveData)).Serialize(sw, saveData) ); } var settingToSave = sb.ToString(); return(settingInFile != settingToSave); }