private void downloader_TorrentRemoved(object sender, ShortTorrentInfo e)
		{
			ProcessTorrentRemoved(e);
		}
		private void ProcessTorrentRemoved(ShortTorrentInfo e)
		{
			if (e.Type == "MOD") {
				bool found = false;
				foreach (var mi in modList.Values) {
					if (mi.Checksum == e.Hash) {
						found = true;
						break;
					}
				}
				if (found) modList.Remove(e.Name);
				if (Downloader.IsTorrentListDone && NotifyModsChanged != null) NotifyModsChanged(this, EventArgs.Empty);
			} else if (e.Type == "MAP") {
				bool found = false;
				foreach (var mi in mapList.Values) {
					if (mi.Checksum == e.Hash) {
						found = true;
						break;
					}
				}
				if (found) mapList.Remove(e.Name);
			}
		}
Пример #3
0
        private void ProcessCommandRecieved(ServerConnectionEventArgs e)
        {
            try {
                if (e.Result == ServerConnectionEventArgs.ResultTypes.Success)
                {
                    switch (e.Command)
                    {
                    case "T+":
                    {
                        int    hash = int.Parse(e.Parameters[0]);
                        string type = e.Parameters[2];
                        string name = e.Parameters[1];
                        if (!serverTorrentList.ContainsKey(hash))
                        {
                            var ti = new ShortTorrentInfo(name, hash, type);
                            serverTorrentList.Add(hash, ti);
                            if (TorrentAdded != null)
                            {
                                TorrentAdded(this, ti);
                            }
                        }
                    }
                    break;

                    case "T-":
                    {
                        int hash = int.Parse(e.Parameters[0]);
                        ShortTorrentInfo ti;
                        if (serverTorrentList.TryGetValue(hash, out ti))
                        {
                            serverTorrentList.Remove(hash);
                            if (TorrentRemoved != null)
                            {
                                TorrentRemoved(this, ti);
                            }
                        }
                    }
                    break;


                    case "M+":
                    {
                        int           hash    = int.Parse(e.Parameters[0]);
                        List <string> mirrors = new List <string>();
                        for (int i = 1; i < e.Parameters.Length; ++i)
                        {
                            if (e.Parameters[i] != "")
                            {
                                mirrors.Add(e.Parameters[i]);
                            }
                        }
                        if (LinksRecieved != null)
                        {
                            LinksRecieved(this, new ResourceLinks(hash, mirrors));
                        }
                    }
                    break;



                    case "TLISTDONE":
                        IsTorrentListDone = true;
                        if (TorrentListDone != null)
                        {
                            TorrentListDone(this, EventArgs.Empty);
                        }
                        break;

                    case "PING":
                        lastPing = DateTime.Now;
                        con.SendCommand("PING");
                        break;
                    }
                }
            } catch (Exception ex) {
                ErrorHandling.HandleException(ex, "error recieving data from P2P server");
            }
        }
		private void ProcessTorrentAdded(ShortTorrentInfo e)
		{
			if (e.Type == "MOD") {
				foreach (var mi in modList.Values) if (mi.Checksum == e.Hash) return;

				double version;
				string baseName;
				ModInfo.ExtractNameAndVersion(e.Name, out baseName, out version);
				ModInfo closestMi = null;
				double difference = double.MaxValue;
				foreach (var mi in loadedModList.Values) {
					if (mi.ShortBaseName == baseName)
					{
						var nd = Math.Abs(mi.Version - version);
						if (nd < difference) {
							difference = nd;
							closestMi = mi;
						}
					}
				}
				
				if (closestMi != null) {
					var newMi = (ModInfo)closestMi.Clone();
					newMi.Checksum = e.Hash;
					newMi.Name = e.Name;
					modList[newMi.Name] = newMi;
					if (Downloader.IsTorrentListDone && NotifyModsChanged != null) NotifyModsChanged(this, EventArgs.Empty);
				}

			} else if (e.Type == "MAP") {
				foreach (var mi in mapList.Values) if (mi.Checksum == e.Hash) return;
				mapList[e.Name] = new MapInfo(e.Name) {Checksum = e.Hash};
			}
		}
		private void ProcessCommandRecieved(ServerConnectionEventArgs e)
		{
			try {
				if (e.Result == ServerConnectionEventArgs.ResultTypes.Success) {
					switch (e.Command) {
						case "T+":
						{
							int hash = int.Parse(e.Parameters[0]);
							string type = e.Parameters[2];
							string name = e.Parameters[1];
							if (!serverTorrentList.ContainsKey(hash)) {
								var ti = new ShortTorrentInfo(name, hash, type);
								serverTorrentList.Add(hash, ti);
								if (TorrentAdded != null) TorrentAdded(this, ti);
							}
						}
							break;

						case "T-":
						{
							int hash = int.Parse(e.Parameters[0]);
							ShortTorrentInfo ti;
							if (serverTorrentList.TryGetValue(hash, out ti)) {
								serverTorrentList.Remove(hash);
								if (TorrentRemoved != null) TorrentRemoved(this, ti);
							}
						}
							break;


						case "M+":
							{
								int hash = int.Parse(e.Parameters[0]);
								List<string> mirrors = new List<string>();
								for (int i = 1; i < e.Parameters.Length; ++i) if (e.Parameters[i] != "") mirrors.Add(e.Parameters[i]);
								if (LinksRecieved != null) LinksRecieved(this, new ResourceLinks(hash, mirrors));
							}
							break;



						case "TLISTDONE":
							IsTorrentListDone = true;
							if (TorrentListDone != null) TorrentListDone(this, EventArgs.Empty);
							break;

						case "PING":
							lastPing = DateTime.Now;
							con.SendCommand("PING");
							break;
					}
				}
			} catch (Exception ex) {
				ErrorHandling.HandleException(ex, "error recieving data from P2P server");
			}
		}