📄 persistentmanager.cs
字号:
namespace Imps.Client.Pc
{
using Imps.Client;
using Imps.Client.Core;
using Imps.Client.Resource;
using Imps.Client.Utils;
using Imps.Common;
using Imps.Utils;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;
public class PersistentManager : IPersistentManager
{
private string _dirHistory;
private string _dirImps;
private string _dirMyContactPortrait;
private string _dirMyUsedPortrait;
private string _dirWin;
private IFrameworkWindow _host;
private string _pathCfgImps;
private string _pathCfgWnd;
private string _pathContacts;
private string _pathPermissions;
private string _pathPlugins;
private string _pathUserInfo;
private object _syncObjCfgImps = new object();
private object _syncObjCfgWnd = new object();
private object _syncObjContacts = new object();
private object _syncObjPermissions = new object();
private object _syncObjPlugins = new object();
private object _syncObjPortraits = new object();
private object _syncObjUserInfo = new object();
private long _userId;
private const string XA_StoreMode = "StoreMode";
private const string XN_RootConfig = "ImpsConfiguration";
private const string XN_SystemConfig = "SystemConfiguration";
private const string XN_UserConfig = "UserConfiguration";
public PersistentManager(IFrameworkWindow host)
{
this._host = host;
this._dirWin = FilePathHelper.GetPathWithEnvVar(@"%AppData%\" + AppDictionary.ShortEnglishName + @"\");
this._pathCfgWnd = this._dirWin + "configuration.dat";
}
private static string DecodeMode1(string text)
{
string str = string.Empty;
try
{
str = GZipHelper.DecompressString(text);
}
catch
{
str = string.Empty;
}
if (!string.IsNullOrEmpty(str))
{
return str;
}
return text;
}
public void DelById(string userId)
{
this.InnerDelSingleDir(Path.Combine(this._dirWin, userId));
}
public void DelChatHistoryById(string userId)
{
if (string.IsNullOrEmpty(this._dirHistory))
{
this.InnerDelSingleDir(this._dirWin + userId + @"\History");
}
else
{
this.InnerDelSingleDir(this._dirHistory);
}
}
public void DelContactsInfoById(string userId)
{
this.InnerDelSingleFileById(userId, "contacts.dat");
}
public void DelPermissionById(string userId)
{
this.InnerDelSingleFileById(userId, "permissions.dat");
}
public void DelPluginsCfgById(string userId)
{
this.InnerDelSingleDir(this._dirWin + userId + @"\Plugins");
}
public void DelPortraitsById(string userId)
{
this.InnerDelSingleDir(this._dirWin + userId + @"\Portraits");
}
public void DelUserCfgById(string userId)
{
this.InnerDelSingleFileById(userId, "configuration.dat");
}
public void DelUserInfoById(string userId)
{
this.InnerDelSingleFileById(userId, "userinfo.dat");
}
private void DoLoadData(PersistentDelegate load, string filename, string xpath, object syncObj)
{
this.DoLoadData(load, filename, xpath, syncObj, true);
}
private void DoLoadData(PersistentDelegate load, string filename, string xpath, object syncObj, bool verify)
{
try
{
if (!string.IsNullOrEmpty(filename))
{
XmlNode section = this.GetXmlNodeFromFile(filename, xpath, syncObj);
if (verify)
{
XmlNode node = section.SelectSingleNode("verify/info");
if ((node == null) || (XmlHelper.ReadXmlAttributeDateTime(node, "datetime", DateTime.MinValue).AddDays(30.0).CompareTo(Imps.Client.Core.User.ServerTime) < 0))
{
return;
}
}
load(section);
}
}
catch (Exception exception)
{
ClientLogger.WriteGeneral("加载本地缓存失败", string.Format("{0}\r\n{1}", filename, exception.Message), 10);
}
}
private void DoSaveData(PersistentDelegate save, string filename, string xpath, object syncObj)
{
try
{
if (!string.IsNullOrEmpty(filename))
{
XmlNode section = this.GetXmlNodeFromFile(filename, xpath, syncObj);
save(section);
XmlNode newChild = section.SelectSingleNode("verify/info");
if (newChild != null)
{
newChild.Attributes["datetime"].Value = Imps.Client.Core.User.ServerTime.ToString("yyyy-MM-dd");
}
else
{
newChild = section.OwnerDocument.CreateNode(XmlNodeType.Element, "verify", null);
newChild.InnerXml = "<info datetime='" + Imps.Client.Core.User.ServerTime.ToString("yyyy-MM-dd") + "' />";
section.AppendChild(newChild);
}
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.CloseOutput = true;
settings.Indent = true;
settings.OmitXmlDeclaration = false;
XmlNode documentElement = section.OwnerDocument.DocumentElement;
string innerXml = documentElement.InnerXml;
documentElement.InnerXml = EncodeMode1(innerXml);
XmlHelper.SetNodeAttributeInt32(documentElement, "StoreMode", 1);
if (!File.Exists(filename))
{
FilePathHelper.MakeSureFilePathExists(filename);
}
lock (syncObj)
{
using (XmlWriter writer = XmlWriter.Create(filename, settings))
{
section.OwnerDocument.Save(writer);
writer.Flush();
writer.Close();
}
}
}
}
catch (Exception exception)
{
ClientLogger.WriteGeneral("保存本地缓存失败", string.Format("{0}\r\n{1}", filename, exception.Message), 10);
}
}
private static string EncodeMode1(string text)
{
try
{
return GZipHelper.CompressString(text);
}
catch
{
}
return string.Empty;
}
private XmlNode GetXmlNodeFromFile(string filename, string xpath, object syncObj)
{
XmlDocument document = new XmlDocument();
XmlNode node = null;
try
{
if (!File.Exists(filename))
{
return node;
}
lock (syncObj)
{
document.Load(filename);
}
XmlNode documentElement = document.DocumentElement;
if (documentElement == null)
{
return node;
}
XmlAttribute attribute = documentElement.Attributes["StoreMode"];
if (attribute == null)
{
return node;
}
if (attribute.Value == "1")
{
string innerXml = documentElement.InnerXml;
documentElement.InnerXml = DecodeMode1(innerXml);
if (documentElement.FirstChild is XmlText)
{
documentElement.RemoveChild(documentElement.FirstChild);
}
return node;
}
documentElement.InnerXml = string.Empty;
}
catch
{
}
finally
{
if (string.IsNullOrEmpty(xpath))
{
node = document;
}
else
{
node = XmlHelper.MakeSureChildPathExists(document, xpath);
}
}
return node;
}
private void InnerDelSingleDir(string dir)
{
if (Directory.Exists(dir))
{
try
{
Directory.Delete(dir, true);
}
catch
{
}
}
}
private void InnerDelSingleFileById(string userId, string filename)
{
string path = this._dirWin + userId + @"\" + filename;
if (File.Exists(path))
{
try
{
File.Delete(path);
}
catch
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -