old_flsystem/类库/Chat.Framework/QQSdk/PCQQ/QQClient.cs

850 lines
34 KiB
C#
Raw Normal View History

2022-09-20 03:10:29 +00:00
using Chat.Framework.QQSdk.Events;
using Grant.Framework;
using QQLite;
using QQLite.Framework;
using QQLite.Framework.Browser;
using QQLite.Framework.Dapper;
using QQLite.Framework.Entity;
using QQLite.Framework.SDK;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Chat.Framework.QQSdk.PCQQ
{
public class QQClient : QQBase
{
private QQUser User;
public QQLite.Framework.Client Client;
private QQLitePlugin Plugin;
/// <summary>
/// 通过窗体登陆
/// </summary>
public void Login(uint QQ)
{
this.QQ = QQ;
new LoginLiteQQ(this).ShowDialog();
}
private void SDK_LoginStatusChange(object sender, QQLite.Framework.Event.LoginStatusChangeEventArgs e)
{
try
{
var x = new QQLoginChangeEvents(this, e.LoginStatus == LoginStatus.Login, e.LoginStatusMessage);
ChatClient.QQPool.OnEvent(x);
this.OnLog(e.LoginStatusMessage);
}
catch (Exception ex)
{
this.OnLog($"{ex.Message} ~ {ex.StackTrace}");
}
}
public void SendVerifyCode(string code)
{
this.Client.SendVerifyCode(new QQLite.Framework.Event.VerifyCodeEventArgs() { Termination = true }, code);
}
///// <summary>
///// 通过账号密码登陆
///// </summary>
///// <param name="username">账号</param>
///// <param name="userpass">密码</param>
//public void Login(string username, string userpass, bool pcqq = true)
//{
// try
// {
// if (this.User != null && this.Client != null) this.Client.Logout();
// this.QQ = uint.Parse(username);
// this.User = new QQUser(QQ, userpass);
// base.Password = userpass;
// base.IsEQQ = !pcqq;
// this.Client = new Client(this.User);
// this.Plugin = GetLitePlugin(this.Client);//装载QQ,数据库相关信息
// this.Plugin.SDK.LoginStatusChange += SDK_LoginStatusChange;
// this.Plugin.EndSdk = new QQClientSDK();
// this.Plugin.EndSdk.SlipVerifyCode += EndSdk_SlipVerifyCode;
// this.User.LoginMode = QQLite.Framework.QQEnum.QQStatus.const_3;//QQ登录状态(在线|Q我吧|离开...)
// this.Client.SetTXProtocol(pcqq ? QQLite.Framework.QQEnum.TXProtocolType.QQ_9_1 : QQLite.Framework.QQEnum.TXProtocolType.QQEIM, true);
// this.Client.Login();
// }
// catch (Exception ex)
// {
// throw ex;
// }
//}
/// <summary>
/// 通过账号密码登陆
/// </summary>
/// <param name="username">账号</param>
/// <param name="userpass">密码</param>
public void Login(QQLitePlugin plugin, Client client, string pass)
{
try
{
this.Client = client;
this.User = client.QQUser;
this.QQ = User.QQ;
this.Password = pass;
if (!this.IsLogin || !isRegisterLog)
{
isRegisterLog = true;
this.Plugin = GetLitePlugin(this.Client);//装载QQ,数据库相关信息
this.Plugin.SDK.LoginStatusChange -= SDK_LoginStatusChange;
this.Plugin.SDK.LoginStatusChange += SDK_LoginStatusChange;
}
this.Client.Login();
}
catch (Exception ex)
{ }
}
bool isRegisterLog = false;
bool isRegisterEvent = false;
internal override void RegisterEvent()
{
if (this.Client != null)
{
this.Nickname = this.Client.QQUser.NickName;
SendMessageQueue.InitQueueTimer(true, 1);//队列
this.Client.CanSendTime = DateTime.Now.AddSeconds(20);
this.Client.AllowSendCluster = true;
this.Client.AllowSendTempIM = true;
this.Client.AllowSendQQ = true;
this.Client.AllowSendDiscuss = true;
this.Client.ClusterUseXml = false;
this.Client.FriendUseXml = false;
if (!isRegisterLog)
{
isRegisterLog = true;
Plugin = GetLitePlugin(this.Client);
Plugin.SDK.LoginStatusChange += SDK_LoginStatusChange;
}
IsRegisterEvent = true;
if (!isRegisterEvent)
{
isRegisterEvent = true;
Plugin.SDK.SlipVerifyCode += SDK_SlipVerifyCode;
Plugin.SDK.ReceiveClusterIM += SDK_ReceiveClusterIM; //群消息
Plugin.SDK.ReceiveNormalIM += SDK_ReceiveNormalIM; //私人消息
Plugin.SDK.ReceiveTempSessionIM += SDK_ReceiveTempSessionIM; //临时会话
Plugin.SDK.ClusterMemberJoin += SDK_ClusterMemberJoin; //新用户加入群
Plugin.SDK.ClusterRequestJoin += SDK_ClusterRequestJoin; //用户请求加入群
Plugin.SDK.FriendRequestAddMeAuth += SDK_FriendRequestAddMeAuth;//请求加我为好友
Plugin.SDK.FriendAddSuccess += SDK_FriendAddSuccess;
Plugin.SDK.FriendRequestAddMe += SDK_FriendRequestAddMe; //加我好友
}
}
}
private void QQClient_GrantEvent(customer customer, string message)
{
if (!string.IsNullOrEmpty(message) && this.Client != null && this.Client.QQUser.QQ.ToString() == customer.cardname && "1002" == customer.softId.ToString())
{
var x = new QQLoginChangeEvents(this, false, $"授权异常!{(string.IsNullOrEmpty(message) ? "" : message)}");
ChatClient.QQPool.OnEvent(x);
}
}
private void SDK_SlipVerifyCode(object sender, QQLite.Framework.Event.VerifyCodeEventArgs e)
{
}
//private void SDK_VerifyCode(object sender, QQLite.Framework.Event.VerifyCodeEventArgs e)
//{
// try
// {
// this.OnLog($"验证码图片:{e.ImageFile}");
// //this.OnLog($"QQ({(Client == null ? 0 : Client.QQUser.QQ)})->【{e.QQ}】需要验证码");
// var evt = new QQVerifyCodeEvents(this) { ImageFile = e.ImageFile, QQ = e.QQ, Session = e.Session, Type = (VerifyCodeEventType)((int)e.Type) };
// ChatClient.QQPool.OnEvent(evt);
// }
// catch (Exception ex)
// {
// this.OnLog($"VerifyCode Error:{ex.Message}");
// }
//}
internal override void UnRegisterEvent()
{
if (this.Client != null && isRegisterEvent)
{
try
{
isRegisterEvent = false;
IsRegisterEvent = false;
Plugin.SDK.SlipVerifyCode -= SDK_SlipVerifyCode;
Plugin.SDK.ReceiveClusterIM -= SDK_ReceiveClusterIM; //群消息
Plugin.SDK.ReceiveNormalIM -= SDK_ReceiveNormalIM; //私人消息
Plugin.SDK.ReceiveTempSessionIM -= SDK_ReceiveTempSessionIM; //临时会话
Plugin.SDK.ClusterMemberJoin -= SDK_ClusterMemberJoin; //新用户加入群
Plugin.SDK.ClusterRequestJoin -= SDK_ClusterRequestJoin; //用户请求加入群
Plugin.SDK.FriendAddSuccess -= SDK_FriendAddSuccess; //
Plugin.SDK.FriendRequestAddMeAuth -= SDK_FriendRequestAddMeAuth;//请求加我为好友
Plugin.SDK.FriendRequestAddMe -= SDK_FriendRequestAddMe; //加我好友
Plugin.SDK.LoginStatusChange -= SDK_LoginStatusChange;
if (ChatClient.QQPool.QQClients.ContainsKey(this.QQ))
{
QQBase b;
ChatClient.QQPool.QQClients.TryRemove(this.QQ, out b);
}
}
catch (Exception)
{ }
finally { RemovePlugin(Plugin); }
}
}
/// <summary>
/// 用户申请添加好友
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SDK_FriendRequestAddMeAuth(object sender, QQLite.Framework.Event.FriendRequestAddMeEventArgs e)
{
try
{
this.OnLog($"QQ({(this.Client == null ? 0 : this.Client.QQUser.QQ)})->用户【{e.QQ}】申请添加好友!");
var evt = new QQAddMeNeedAuthEvents(this) { QQ = e.QQ, Message = e.Message };
ChatClient.QQPool.OnEvent(evt);
}
catch (Exception ex)
{
this.OnLog($"FRAddMeAuth Error:{ex.Message}");
}
}
private void SDK_FriendRequestAddMe(object sender, QQLite.Framework.Event.FriendRequestAddMeEventArgs e)
{
try
{
}
catch (Exception ex)
{
this.OnLog($"FRAddMe Error:{ex.Message}");
}
}
private void SDK_FriendAddSuccess(object sender, QQLite.Framework.Event.FriendAddSuccessEventArgs e)
{
try
{
//this.OnLog($"QQ({(Client == null ? 0 : Client.QQUser.QQ)})->用户【{e.QQ}】申请添加好友!");
}
catch (Exception ex)
{
this.OnLog($"FAddSuccess Error:{ex.Message}");
}
}
/// <summary>
/// 新人申请入QQ群
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SDK_ClusterRequestJoin(object sender, QQLite.Framework.Event.ClusterRequestJoinEventArgs e)
{
try
{
var evt = new QQAddToClusterNeedAuthEvents(this) { ClusterId = e.ClusterInfo.ClusterId, QQ = e.MemberQQ, Nickname = e.MemberName, Message = e.Message, GroupId = e.ClusterInfo.ExternalId, GroupName = e.ClusterInfo.Name, Creator = e.ClusterInfo.Creator };
ChatClient.QQPool.OnEvent(evt);
}
catch (Exception ex)
{
this.OnLog($"CRJoin Error:{ex.Message}");
}
}
/// <summary>
/// 群加入新用户
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SDK_ClusterMemberJoin(object sender, QQLite.Framework.Event.ClusterMemberJoinEventArgs e)
{
try
{
var evt = new QQNewGroupMemberEvents(this) { GroupId = e.ClusterInfo.ExternalId, GroupName = e.ClusterInfo.Name, Creator = e.ClusterInfo.Creator, NewFriendQQ = e.MemberQQ, NewFriendNick = e.MemberName, InviteQQ = e.InviteQQ, Operator = e.Operator, OperatorName = e.OperatorName };
ChatClient.QQPool.OnEvent(evt);
}
catch (Exception ex)
{
this.OnLog($"CMJoin Error:{ex.Message}");
}
}
/// <summary>
/// 收到临时会话
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SDK_ReceiveTempSessionIM(object sender, QQLite.Framework.Event.TempSessionIMEventArgs e)
{
try
{
Client = sender as Client;
this.OnLog($"QQ({(Client == null ? 0 : Client.QQUser.QQ)})->临时消息->{e.SenderName}({e.Sender})说:{e.Message}");
var temp = new QQReceiveFriendMsgEvents(this) { Token = e.Token, TempMsgID = e.Sender, QQ = e.Sender, Nickname = e.SenderName, Message = e.Message };
ChatClient.QQPool.OnEvent(temp);
}
catch (Exception ex)
{
this.OnLog($"RTSIM Error:{ex.Message}");
}
}
/// <summary>
/// 收到私人消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SDK_ReceiveNormalIM(object sender, QQLite.Framework.Event.NormalIMEventArgs e)
{
try
{
if (e.Sender == this.QQ) return;
Client = sender as Client;
this.OnLog($"QQ({(Client == null ? 0 : Client.QQUser.QQ)})->私消息->{e.SenderName}({e.Sender})说:{e.Message}");
var temp = new QQReceiveFriendMsgEvents(this) { QQ = e.Sender, Message = e.Message, Nickname = e.SenderName };
ChatClient.QQPool.OnEvent(temp);
}
catch (Exception ex)
{
this.OnLog($"RNIM Error:{ex.Message}");
}
}
/// <summary>
/// 收到群消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SDK_ReceiveClusterIM(object sender, QQLite.Framework.Event.ClusterIMEventArgs e)
{
try
{
if (e.Sender == this.QQ) return;//忽略自己的消息
this.OnLog($"QQ({(this.Client == null ? 0 : this.Client.QQUser.QQ)})->群消息->{e.SenderName}({e.Sender})在[{e.ClusterInfo.Name}({e.ClusterInfo.ExternalId})]群说:{e.Message}");
var temp = new QQReceiveGroupMsgEvents(this) { QQ = e.Sender, Message = e.Message, Nickname = e.SenderName, GroupName = e.ClusterInfo.Name, GroupId = e.ClusterInfo.ExternalId };
ChatClient.QQPool.OnEvent(temp);
}
catch (Exception ex)
{
this.OnLog($"RCIM Error:{ex.Message}");
}
}
public override void SendFriendImage(uint QQ, string ImageSrc)
{
try
{
if (this.Client != null) this.Client.SendIM(QQ, $"[image]{ImageSrc}[/image]");
}
catch (Exception)
{ }
}
public override void SendFriendMessage(uint QQ, string Message)
{
if (IsLogin)
{
try
{
QQMessagePack pack = new QQMessagePack(Message);
do
{
var msg = pack.NextMessage();
msg = Regex.Replace(msg, @"(\[@\d+\])", "");
if (string.IsNullOrEmpty(msg)) break;
var result = this.Client.SendIM(QQ, msg);
} while (true);
}
catch (Exception)
{ }
}
}
public override void SendGroupImage(uint GroupId, string ImageSrc)
{
try
{
if (this.Client != null) this.Client.SendClusterIM(GroupId, $"[image]{ImageSrc}[/image]");
}
catch (Exception)
{ }
}
//public override void SendFriendVideo(uint toUsername, string videoSrc)
//{
// try
// {
// if (this.Client != null) this.Client.SendIM(toUsername, $"[Video]{videoSrc}[/Video]");
// }
// catch (Exception ex)
// { }
//}
//public override void SendGroupVideo(uint GroupId, string ImageSrc)
//{
// try
// {
// if (this.Client != null) this.Client.SendClusterIM(GroupId, $"[Video]{ImageSrc}[/Video]");
// }
// catch (Exception)
// { }
//}
/// <summary>
/// 显示登录窗口
/// </summary>
/// <param name="Username"></param>
public override void ShowLogin(string Username)
{
if (!string.IsNullOrEmpty(Username)) this.QQ = uint.Parse(Username);
var form = new LoginLiteQQ(this);
form.ShowDialog();
}
public override void SendGroupMessage(uint GroupId, string Message)
{
if (IsLogin)
{
try
{
QQMessagePack pack = new QQMessagePack(Message);
do
{
var msg = pack.NextMessage();
if (string.IsNullOrEmpty(msg)) break;
var result = this.Client.SendClusterIM(GroupId, msg);
} while (true);
}
catch (Exception)
{ }
}
}
public override void SendTempMessage(uint QQ, string Message, byte[] Token)
{
if (IsLogin)
{
try
{
QQMessagePack pack = new QQMessagePack(Message);
do
{
var msg = pack.NextMessage();
msg = Regex.Replace(msg, @"\[image.+image\]", "【临时消息不支持图片】");
if (string.IsNullOrEmpty(msg)) break;
var result = this.Client.SendTempIM(QQ, msg, Token);
} while (true);
}
catch (Exception)
{ }
}
}
public override void SendTempMessage(uint QQ, string Message)
{
if (IsLogin)
{
try
{
QQMessagePack pack = new QQMessagePack(Message);
do
{
var msg = pack.NextMessage();
msg = Regex.Replace(msg, @"\[image.+image\]", "【临时消息不支持图片】");
if (string.IsNullOrEmpty(msg)) break;
var result = this.Client.SendTempIM(QQ, msg);
} while (true);
}
catch (Exception)
{ }
}
}
public override void AerifyAddMe(uint QQ, bool Agree)
{
try
{
if (IsLogin) this.Client.FriendApprovedAddMe(QQ);
}
catch (Exception ex)
{
this.OnLog($"AAddMe Error:{ex.Message}");
}
}
public override void AerifyAddGroup(uint groupId, uint QQ)
{
try
{
if (IsLogin)
{
var result = this.Client.ClusterApproveInviteJoin(groupId, QQ);
}
}
catch (Exception ex)
{
this.OnLog($"AerifyAddGroup Error:{ex.Message}");
}
}
public override void Logout()
{
try
{
if (this.Client != null && isRegisterEvent)
{
try
{
isRegisterEvent = false;
IsRegisterEvent = false;
Plugin.SDK.SlipVerifyCode -= SDK_SlipVerifyCode;
Plugin.SDK.ReceiveClusterIM -= SDK_ReceiveClusterIM; //群消息
Plugin.SDK.ReceiveNormalIM -= SDK_ReceiveNormalIM; //私人消息
Plugin.SDK.ReceiveTempSessionIM -= SDK_ReceiveTempSessionIM; //临时会话
Plugin.SDK.ClusterMemberJoin -= SDK_ClusterMemberJoin; //新用户加入群
Plugin.SDK.ClusterRequestJoin -= SDK_ClusterRequestJoin; //用户请求加入群
Plugin.SDK.FriendAddSuccess -= SDK_FriendAddSuccess; //
Plugin.SDK.FriendRequestAddMeAuth -= SDK_FriendRequestAddMeAuth;//请求加我为好友
Plugin.SDK.FriendRequestAddMe -= SDK_FriendRequestAddMe; //加我好友
Plugin.SDK.LoginStatusChange -= SDK_LoginStatusChange;
}
catch (Exception)
{ }
}
RemovePlugin(Plugin);
}
catch (Exception ex)
{
this.OnLog($"Logout Error:{ex.Message}");
}
}
public override void AutoLogin()
{
try
{
this.Client.Login();
}
catch (Exception ex)
{
this.OnLog($"algn Error:{ex.Message}");
}
}
#region
class QQMessagePack
{
private string content;
List<string> needMessage;
public QQMessagePack(string Message)
{
this.content = Message;
this.needMessage = GetNeedMessage();
}
#region
private static string Expression = "[微笑],[憨笑],[呲牙],[爱心],[啤酒],[太阳],[礼物],[抱拳],[咖啡],[调皮],[激动],[回头],[色],[愉快],[爱情],[飞吻],[跳跳],[发抖],[转圈],[献吻],[耶],[红包],[嘿哈]";
private static List<string> ExpressionList = null;
private static Random random = new Random();
public static string FindRandomExpression()
{
if (ExpressionList == null)
ExpressionList = Expression.Split(',').ToList();
return ExpressionList[random.Next(ExpressionList.Count)];
}
#endregion
private List<string> GetNeedMessage()
{
List<string> _msgs = new List<string>();
if (string.IsNullOrEmpty(content)) return _msgs;
content = content.Replace("\r\n", "\n").Trim().Replace("[时间]", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
var msgs = content.Split(new string[] { "[分段]" }, StringSplitOptions.RemoveEmptyEntries);
try
{
foreach (var msg in msgs)
{
var message = msg.Trim();
if (string.IsNullOrEmpty(message)) continue;
message = message.Contains("[随机表情]") ? message.Replace("[随机表情]", FindRandomExpression()) : message;
if (message.StartsWith("[延迟="))
{
var reg = Regex.Match(message, @"\[延迟=(\d+)\]");
if (reg.Success)
{
message = Regex.Replace(message, @"\[延迟=(\d+)\]", "").Trim();
// this.WriteLog($"需要延迟{reg.Groups[1].Value}秒,再发送:{message}");
//_msgs.Add(reg.Groups[1].Value);
}
}
//发送XML消息
if (message.StartsWith("<appmsg appid=") && message.EndsWith("</appmsg>"))
{
_msgs.Add("暂不支持QQ卡片消息!");
}
//发送图片消息
var matchs = Regex.Matches(message, @"\[图片\=([^\]]+)\]", RegexOptions.IgnoreCase);
foreach (Match match in matchs)
{
_msgs.Add($"[image]{ match.Groups[1].Value}[/image]");
}
message = Regex.Replace(message, @"\[图片\=([^\]]+)\]", "");
//发送语音消息
matchs = Regex.Matches(message, @"\[语音\=([^\]]+)\]", RegexOptions.IgnoreCase);
foreach (Match item in matchs)
{
//暂不支持语音消息
}
message = Regex.Replace(message, @"\[语音\=([^\]]+)\]", "");
matchs = Regex.Matches(message, @"\[视频\=([^\]]+)\]", RegexOptions.IgnoreCase);
//foreach (Match item in matchs)
//{
// try
// {
// _msgs.Add($"[Video]{ item.Groups[1].Value}[/Video]");
// //var video = Util.StrToHex(temp);
// //if (video != null)
// //{
// // var video_json = Encoding.UTF8.GetString(video);
// // var video_data = CsharpHttpHelper.HttpExtend.JsonToDictionary(video_json);
// // if (video_data != null)
// // {
// // //暂不支持视频消息
// // }
// //}
// }
// catch (Exception ex)
// { }
//}
message = Regex.Replace(message, @"\[视频\=([^\]]+)\]", "");
matchs = Regex.Matches(message, @"\[卡片\=([^\]]+)\]", RegexOptions.IgnoreCase);
foreach (Match item in matchs)
{
//暂不支持卡片
//SendCard(username, item.Groups[1].Value == "自己" ? User.Username : item.Groups[1].Value);
}
message = Regex.Replace(message, @"\[卡片\=([^\]]+)\]", "");
if (string.IsNullOrEmpty(message)) continue;
if (message.StartsWith("\r\n")) message = message.Substring("\r\n".Length);
if (message.StartsWith("\n")) message = message.Substring("\n".Length);
_msgs.Add(message);
}
}
catch (Exception ex)
{
}
return _msgs;
}
private int index = 0;
public string NextMessage()
{
if (needMessage.Count == index) return string.Empty;
string msg = needMessage[index];
var reg = Regex.Match(msg, @"\[延迟=(\d+)\]");
if (reg.Success)
{
Thread.Sleep(int.Parse(reg.Groups[1].Value) * 1000);
}
index++;
return msg;
}
}
#endregion
#region
private static bool i5MxESGqfP(string string_0, string string_1)
{
bool result;
try
{
if (string_1[string_1.Length - 1] != Path.DirectorySeparatorChar)
{
string_1 += Path.DirectorySeparatorChar.ToString();
}
if (!Directory.Exists(string_1))
{
Directory.CreateDirectory(string_1);
}
string[] fileSystemEntries = Directory.GetFileSystemEntries(string_0);
foreach (string text in fileSystemEntries)
{
if (Directory.Exists(text))
{
i5MxESGqfP(text, string_1 + Path.GetFileName(text));
}
else
{
File.Copy(text, string_1 + Path.GetFileName(text), true);
}
}
result = true;
}
catch
{
result = false;
}
return result;
}
/// <summary>
/// 初始化QQ
/// </summary>
/// <param name="Client"></param>
/// <returns></returns>
public static QQLite.QQLitePlugin GetLitePlugin(Client Client)
{
QQLite.QQLitePlugin plugin = null;
DbBase.RobotQQ = Client.QQ;
string text = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "QQ\\" + Client.QQ);
if (!Directory.Exists(text))
{
string text2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "QQ\\0");
if (Directory.Exists(text2))
{
i5MxESGqfP(text2, text);
}
else
{
Directory.CreateDirectory(text);
}
}
text = Path.Combine(text, "QQLite.db");
if (!File.Exists(text))
{
using (DbBase dbBase = new DbBase("Data Source=" + text + ";Pooling=true;FailIfMissing=false"))
{
dbBase.BeginTransation();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("DROP TABLE IF EXISTS `QQLite_Config`;");
stringBuilder.AppendLine("CREATE TABLE IF NOT EXISTS `QQLite_Config` (");
stringBuilder.AppendLine("`ConfigId` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,");
stringBuilder.AppendLine("`PluginId` TEXT(255) NOT NULL DEFAULT ('qqrobot'),");
stringBuilder.AppendLine("`ConfigName` TEXT(20) NOT NULL COLLATE NOCASE ,");
stringBuilder.AppendLine("`ConfigInfo` TEXT(100) NOT NULL COLLATE NOCASE ,");
stringBuilder.AppendLine("`ConfigType` TEXT(10) NOT NULL COLLATE NOCASE DEFAULT ('string'),");
stringBuilder.AppendLine("`ConfigValue` TEXT COLLATE NOCASE ,");
stringBuilder.AppendLine("CONSTRAINT `ConfigId` UNIQUE (`ConfigId` ASC)");
stringBuilder.AppendLine(");");
stringBuilder.AppendLine("DROP TABLE IF EXISTS `QQLite_Plugin`;");
stringBuilder.AppendLine("CREATE TABLE IF NOT EXISTS `QQLite_Plugin` (");
stringBuilder.AppendLine("`Pid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,");
stringBuilder.AppendLine("`PluginId` TEXT(255) NOT NULL ,");
stringBuilder.AppendLine("`IsInstall` INTEGER NOT NULL DEFAULT (0),");
stringBuilder.AppendLine("`IsEnabled` INTEGER NOT NULL DEFAULT (0),");
stringBuilder.AppendLine("`Priority` INTEGER NOT NULL DEFAULT (0),");
stringBuilder.AppendLine("`Version` TEXT(20) NOT NULL COLLATE NOCASE ,");
stringBuilder.AppendLine("CONSTRAINT `Pid` UNIQUE (`Pid` ASC)");
stringBuilder.AppendLine(");");
stringBuilder.AppendLine("DROP TABLE IF EXISTS `DBVersion`;");
stringBuilder.AppendLine("CREATE TABLE `DBVersion`(");
stringBuilder.AppendLine("`Version` TEXT");
stringBuilder.AppendLine(");");
stringBuilder.AppendLine("CREATE UNIQUE INDEX IF NOT EXISTS `index_config_plugin` ON `QQLite_Config` (`PluginId` ASC, `ConfigName` ASC);");
stringBuilder.AppendLine("CREATE UNIQUE INDEX IF NOT EXISTS `index_qqlite_plugin` ON `QQLite_Plugin` (`PluginId` ASC);");
dbBase.Conn.Execute(stringBuilder.ToString(), null, dbBase.Tran, null, null);
dbBase.Conn.Execute("INSERT INTO `DBVersion` (Version) VALUES (@Version)", new
{
Version = "1.0.0.1"
}, dbBase.Tran, null, null);
dbBase.EndTransaction();
((SQLiteConnection)dbBase.Conn).ChangePassword("WomccServices.Desc");
}
}
else
{
using (DbBase dbBase2 = new DbBase("----------"))
{
dbBase2.BeginTransation();
//通过表名检测是否存在
if (dbBase2.Conn.Query<Int64>("SELECT COUNT(*) as CNT FROM sqlite_master where type='table' and name='DBVersion'").FirstOrDefault() == 0)
{
StringBuilder stringBuilder2 = new StringBuilder();
stringBuilder2.AppendLine("DROP TABLE IF EXISTS `DBVersion`;");
stringBuilder2.AppendLine("CREATE TABLE `DBVersion`(");
stringBuilder2.AppendLine("`Version` TEXT");
stringBuilder2.AppendLine(");");
dbBase2.Conn.Execute(stringBuilder2.ToString(), null, dbBase2.Tran, null, null);
dbBase2.Conn.Execute("INSERT INTO `DBVersion` (Version) VALUES (@Version)", new
{
Version = "1.0.0.0"
}, dbBase2.Tran, null, null);
}
string a = dbBase2.Conn.Query<String>("SELECT Version FROM `DBVersion`").SingleOrDefault();
if (a == "1.0.0.0")
{
dbBase2.Conn.Execute("CREATE UNIQUE INDEX IF NOT EXISTS `index_config_plugin` ON `QQLite_Config` (`PluginId` ASC, `ConfigName` ASC);", dbBase2.Tran, null, null, null);
dbBase2.Conn.Execute("ALTER TABLE `QQLite_Plugin` ADD COLUMN `IsInstall` INTEGER NOT NULL DEFAULT (0);", dbBase2.Tran, null, null, null);
dbBase2.Conn.Execute("CREATE UNIQUE INDEX IF NOT EXISTS `index_qqlite_plugin` ON `QQLite_Plugin` (`PluginId` ASC);", dbBase2.Tran, null, null, null);
dbBase2.Conn.Execute("UPDATE `DBVersion` SET Version=@Version", new
{
Version = "1.0.0.1"
}, dbBase2.Tran, null, null);
}
dbBase2.EndTransaction();
}
}
plugin.PluginAction_StartAll(Client);
plugin = Client.GetPlugin("QQLite.QQLitePlugin") as QQLitePlugin;
plugin.SDK = new QQClientSDK();
return plugin;
}
public static void RemovePlugin(QQLitePlugin plugin)
{
try
{
if (plugin != null)
{
plugin.PluginAction_StopAll();
plugin.Stop();
}
}
catch (Exception ex)
{ }
finally
{
plugin = null;
}
}
#endregion
}
}