📄 customemotionmanager.cs
字号:
namespace Imps.Client.Core.CustomEmotion
{
using Imps.Client.Base;
using Imps.Client.Core;
using Imps.Client.Resource;
using Imps.Client.Utils;
using Imps.Client.Utils.Cryptography;
using Imps.Utils;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Xml;
public class CustomEmotionManager : ICustomEmotionManager, IDisposable
{
private Imps.Client.Core.User _currentUser;
private string _customEmotionConfigPath;
private string _customEmotionRecivedPath;
private string _customEmotionSavePath;
private Dictionary<string, List<AsyncBizOperation>> _downloadingEmotions;
private Dictionary<Imps.Client.Core.CustomEmotion.CustomEmotion, Image> _emotionthumbImages = new Dictionary<Imps.Client.Core.CustomEmotion.CustomEmotion, Image>();
private string[] _imageAccpetFormt = new string[] { "JPG", "JPEG", "PNG", "GIF", "BMP" };
private Size? _maxSize = null;
private CustomEmotionDictionary _ownerEmotions;
private object _syncDownloadingObject;
internal CustomEmotionManager(Imps.Client.Core.User owner)
{
this._currentUser = owner;
this.Init();
}
public void AddClicks(Imps.Client.Core.CustomEmotion.CustomEmotion emotion)
{
emotion.Clicks++;
this.OwnerEmotions.Sort();
this.OwnerEmotions.SortShortCuts();
this.UpdateEmotionConfig(emotion);
}
private void AddEmotionConfig(Imps.Client.Core.CustomEmotion.CustomEmotion emotion)
{
XmlDocument doc = this.LoadEmotionConfigXmlDocument();
XmlNode newChild = doc.CreateElement("object");
XmlAttribute node = doc.CreateAttribute("shortcut");
node.Value = emotion.ShortCut;
newChild.Attributes.Append(node);
node = doc.CreateAttribute("name");
node.Value = emotion.Name;
newChild.Attributes.Append(node);
node = doc.CreateAttribute("animated");
node.Value = emotion.Animated ? "1" : "0";
newChild.Attributes.Append(node);
node = doc.CreateAttribute("id");
node.Value = emotion.Id;
newChild.Attributes.Append(node);
node = doc.CreateAttribute("clicks");
node.Value = emotion.Clicks.ToString();
newChild.Attributes.Append(node);
node = doc.CreateAttribute("expiredate");
node.Value = !emotion.ExpireDate.get_HasValue() ? "" : emotion.ExpireDate.get_Value().ToString("yyyy-MM-dd HH:mm:ss");
newChild.Attributes.Append(node);
doc.DocumentElement.AppendChild(newChild);
this.SaveEmotionConfigXmlDocument(doc);
}
public Imps.Client.Core.CustomEmotion.CustomEmotion AddOwnerEmotion(string shortcut, string name, string filepath, out CustomEmotionStatus status)
{
Imps.Client.Core.CustomEmotion.CustomEmotion emotion2;
if (shortcut.Length > this.MaxShortCutLength)
{
shortcut = shortcut.Substring(0, this.MaxShortCutLength);
}
if (name.Length > this.MaxShortCutLength)
{
name = name.Substring(0, this.MaxShortCutLength);
}
if (this.OwnerEmotions.Count >= this.MaxCustomEmotionLimit)
{
status = CustomEmotionStatus.LimitExceeded;
return null;
}
if (ImpsEmoticons.Instance.ContainsKey(shortcut))
{
status = CustomEmotionStatus.ShortCutExistSystemEmotion;
return null;
}
if (this.OwnerEmotions.GetEmotionByShortCut(shortcut) != null)
{
status = CustomEmotionStatus.ShortCutExist;
return null;
}
if (this.ExistSameStartShortCutInSystemEmotions(shortcut))
{
status = CustomEmotionStatus.ExistSameStartShortInSystemEmotions;
return null;
}
if (this.ExistSameStartShortCut(shortcut))
{
status = CustomEmotionStatus.ExistSameStartShort;
return null;
}
if (!File.Exists(filepath))
{
status = CustomEmotionStatus.FileNotFound;
return null;
}
FileInfo info = new FileInfo(filepath);
if (info.Length > this.MaxCustomEmotionLength)
{
status = CustomEmotionStatus.FileLengthError;
return null;
}
Image image = null;
try
{
image = Image.FromFile(filepath);
}
catch
{
if (image != null)
{
image.Dispose();
}
status = CustomEmotionStatus.FileFormatError;
return null;
}
ImageCodecInfo imageCodecInfo = ImageHelper.GetImageCodecInfo(image.RawFormat);
if ((imageCodecInfo == null) || (Array.IndexOf<string>(this._imageAccpetFormt, imageCodecInfo.FormatDescription) == -1))
{
status = CustomEmotionStatus.FileFormatError;
if (image != null)
{
image.Dispose();
}
return null;
}
if ((image.Size.Width > this.MaxCustomEmotionSize.Width) || (image.Size.Height > this.MaxCustomEmotionSize.Height))
{
status = CustomEmotionStatus.FileSizeError;
if (image != null)
{
image.Dispose();
}
return null;
}
string key = MD5Helper.GetFileMD5(filepath);
if (this.OwnerEmotions.ContainsKey(key))
{
status = CustomEmotionStatus.FileExist;
if (image != null)
{
image.Dispose();
}
return null;
}
string path = Path.Combine(this._customEmotionSavePath, string.Format("{0}.{1}", key, "fce"));
string text3 = Path.Combine(this._customEmotionSavePath, string.Format("{0}.{1}", key, "thumb"));
try
{
if (!File.Exists(path))
{
File.Copy(filepath, path);
}
if (!File.Exists(text3))
{
ImageHelper.GetSquareThumbnailImage(image, this.ThumbImageSize).Save(text3);
}
Imps.Client.Core.CustomEmotion.CustomEmotion emotion = new Imps.Client.Core.CustomEmotion.CustomEmotion();
emotion.Id = key;
emotion.ShortCut = shortcut;
emotion.Name = name;
emotion.Animated = ImageHelper.GetImageFrameCount(image) > 1;
this.AddEmotionConfig(emotion);
this.AddOwnerEmotionToMemory(emotion, true);
status = CustomEmotionStatus.OK;
emotion2 = emotion;
}
catch (IOException)
{
status = CustomEmotionStatus.SaveError;
emotion2 = null;
}
catch (Exception)
{
status = CustomEmotionStatus.UnKonwError;
emotion2 = null;
}
finally
{
if (image != null)
{
image.Dispose();
}
}
return emotion2;
}
private void AddOwnerEmotionToMemory(Imps.Client.Core.CustomEmotion.CustomEmotion emotion, bool sort)
{
try
{
this.OwnerEmotions.Add(emotion.Id.ToUpper(), emotion);
if (sort)
{
this.OwnerEmotions.Sort();
this.OwnerEmotions.SortShortCuts();
}
}
catch (Exception exception)
{
ClientLogger.WriteException(exception);
}
}
public void AsyncDownLoadEmotionImage(Imps.Client.Core.CustomEmotion.CustomEmotion emotion, AsyncBizOperation op)
{
op.ContextForBiz = emotion;
if (this._downloadingEmotions == null)
{
this._downloadingEmotions = new Dictionary<string, List<AsyncBizOperation>>();
}
if (this._syncDownloadingObject == null)
{
this._syncDownloadingObject = new object();
}
lock (this._syncDownloadingObject)
{
if (this._downloadingEmotions.ContainsKey(emotion.Id))
{
this._downloadingEmotions.get_Item(emotion.Id).Add(op);
return;
}
List<AsyncBizOperation> list = new List<AsyncBizOperation>();
list.Add(op);
this._downloadingEmotions.Add(emotion.Id, list);
}
ThreadPool.QueueUserWorkItem(new WaitCallback(this.DownLoadCustomEmotionInThread), op);
}
public void AsyncUploadEmotionImages(List<Imps.Client.Core.CustomEmotion.CustomEmotion> emotions)
{
if ((emotions != null) && (emotions.get_Count() != 0))
{
List<Imps.Client.Core.CustomEmotion.CustomEmotion> state = new List<Imps.Client.Core.CustomEmotion.CustomEmotion>();
for (int i = emotions.get_Count() - 1; i >= 0; i--)
{
if (!state.Contains(emotions.get_Item(i)) && (!emotions.get_Item(i).ExpireDate.get_HasValue() || (emotions.get_Item(i).ExpireDate.get_Value() < DateTime.Now)))
{
state.Add(emotions.get_Item(i));
}
}
if (state.get_Count() > 0)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(this.UploadAfterCheckInThread), state);
}
}
}
private void CreateNewEmotionConfig()
{
using (XmlWriter writer = XmlWriter.Create(this._customEmotionConfigPath))
{
writer.WriteStartElement("CustomEmotions");
writer.WriteAttributeString("version", "1");
writer.WriteEndElement();
}
}
public void DeleteOwnerEmotion(Imps.Client.Core.CustomEmotion.CustomEmotion emotion)
{
this.OwnerEmotions.Remove(emotion.Id);
if (!string.IsNullOrEmpty(emotion.ShortCut))
{
this.OwnerEmotions.SortShortCuts();
}
this.RemoveEmotionConfig(emotion);
string path = Path.Combine(this._customEmotionSavePath, string.Format("{0}.{1}", emotion.Id, "fce"));
string text2 = Path.Combine(this._customEmotionSavePath, string.Format("{0}.{1}", emotion.Id, "thumb"));
try
{
if (File.Exists(path))
{
File.Delete(path);
}
if (File.Exists(text2))
{
File.Delete(text2);
}
}
catch (IOException)
{
ClientLogger.WriteGeneral("自定义表情图片删除失败,图片正在被使用!" + emotion.Id);
}
catch (Exception exception)
{
ClientLogger.WriteException(exception);
}
}
public void Dispose()
{
this.OwnerEmotions.Clear();
Dictionary<Imps.Client.Core.CustomEmotion.CustomEmotion, Image>.ValueCollection.Enumerator enumerator = this._emotionthumbImages.get_Values().GetEnumerator();
try
{
while (enumerator.MoveNext())
{
enumerator.get_Current().Dispose();
}
}
finally
{
enumerator.Dispose();
}
this._emotionthumbImages.Clear();
}
private void DownLoadCustomEmotionInThread(object data)
{
Thread.Sleep(500);
AsyncBizOperation operation = data as AsyncBizOperation;
Imps.Client.Core.CustomEmotion.CustomEmotion context = operation.ContextForBiz as Imps.Client.Core.CustomEmotion.CustomEmotion;
try
{
HttpWebRequest request;
string uriGetEmotion = this.CurrentUser.Configuration.SystemSetting.SysConversationSetting.UriGetEmotion;
if (string.IsNullOrEmpty(uriGetEmotion))
{
ClientLogger.WriteGeneral("自定义表情下载失败,下载地址为null");
this.FireEventAfterDownLoadEmotion(context.Id, false);
return;
}
uriGetEmotion = string.Format("{0}?id={1}", uriGetEmotion, context.Id);
int num = 0;
Label_0070:
request = ConnectionFactory.CreateHttpWebRequest(uriGetEmotion, context, false, true);
request.Accept = "image/*";
try
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -