Terraria.ModLoader.ModNet.ReceiveMod C# (CSharp) Method

ReceiveMod() static private method

static private ReceiveMod ( BinaryReader reader ) : void
reader BinaryReader
return void
		internal static void ReceiveMod(BinaryReader reader) {
			if (downloadingMod == null)
				return;

			try
			{
				if (downloadingFile == null)
				{
					Interface.downloadMod.SetDownloading(reader.ReadString());
					Interface.downloadMod.SetCancel(() => {
						downloadingFile?.Close();
						downloadingMod = null;
						Netplay.disconnect = true;
						Main.menuMode = 0;
					});
					Main.menuMode = Interface.downloadModID;

					downloadingLength = reader.ReadInt64();
					downloadingFile = new FileStream(downloadingMod.path, FileMode.Create);
					return;
				}

				var bytes = reader.ReadBytes((int) Math.Min(downloadingLength - downloadingFile.Position, CHUNK_SIZE));
				downloadingFile.Write(bytes, 0, bytes.Length);
				Interface.downloadMod.SetProgress(downloadingFile.Position, downloadingLength);

				if (downloadingFile.Position == downloadingLength)
				{
					downloadingFile.Close();
					var mod = new TmodFile(downloadingMod.path);
					mod.Read();
					var ex = mod.ValidMod();
					if (ex != null)
						throw ex;

					if (!downloadingMod.Matches(mod))
						throw new Exception("Hash mismatch");

					if (downloadingMod.signed && !mod.ValidModBrowserSignature)
						throw new Exception("Mod was not signed by the Mod Browser");

					ModLoader.EnableMod(mod);

					if (downloadQueue.Count > 0)
						DownloadNextMod();
					else
						OnModsDownloaded(true);
				}
			}
			catch (Exception e)
			{
				try
				{
					downloadingFile?.Close();
				} catch {}

				File.Delete(downloadingMod.path);
				ErrorLogger.LogException(e, "An error occured while downloading "+downloadingMod.name);
				downloadingMod = null;
			}
		}