2015-05-16 01:15:44 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using xServer.Core.Helper;
|
|
|
|
|
using xServer.Core.Packets.ClientPackets;
|
|
|
|
|
|
|
|
|
|
namespace xServer.Core.Commands
|
|
|
|
|
{
|
|
|
|
|
/* THIS PARTIAL CLASS SHOULD CONTAIN MISCELLANEOUS METHODS. */
|
|
|
|
|
public static partial class CommandHandler
|
|
|
|
|
{
|
|
|
|
|
public static void HandleShellCommandResponse(Client client, ShellCommandResponse packet)
|
|
|
|
|
{
|
|
|
|
|
if (client.Value.FrmRs == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2015-05-29 21:34:57 +00:00
|
|
|
|
if (packet.IsError)
|
|
|
|
|
client.Value.FrmRs.PrintError(packet.Output);
|
|
|
|
|
else
|
|
|
|
|
client.Value.FrmRs.PrintMessage(packet.Output);
|
2015-05-16 01:15:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void HandleDownloadFileResponse(Client client, DownloadFileResponse packet)
|
|
|
|
|
{
|
|
|
|
|
string downloadPath = Path.Combine(Application.StartupPath, "Clients\\" + client.EndPoint.Address.ToString());
|
|
|
|
|
if (!Directory.Exists(downloadPath))
|
|
|
|
|
Directory.CreateDirectory(downloadPath);
|
|
|
|
|
|
|
|
|
|
downloadPath = Path.Combine(downloadPath, packet.Filename);
|
|
|
|
|
|
|
|
|
|
bool Continue = true;
|
|
|
|
|
if (packet.CurrentBlock == 0 && File.Exists(downloadPath))
|
|
|
|
|
if (
|
|
|
|
|
MessageBox.Show(string.Format("The file '{0}' already exists!\nOverwrite it?", packet.Filename),
|
|
|
|
|
"Overwrite Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
|
|
|
|
|
Continue = false;
|
|
|
|
|
|
|
|
|
|
if (client.Value.FrmFm == null)
|
|
|
|
|
{
|
|
|
|
|
new Packets.ServerPackets.DownloadFileCanceled(packet.ID).Execute(client);
|
|
|
|
|
MessageBox.Show("Please keep the File Manager open.\n\nWarning: Download aborted", "Download aborted",
|
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-29 21:34:57 +00:00
|
|
|
|
int index = client.Value.FrmFm.GetTransferIndex(packet.ID.ToString());
|
|
|
|
|
if (index < 0)
|
2015-05-16 01:15:44 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (Continue)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(packet.CustomMessage))
|
|
|
|
|
{
|
2015-05-29 21:34:57 +00:00
|
|
|
|
if (client.Value.FrmFm == null) // abort download when form is closed
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
client.Value.FrmFm.UpdateTransferStatus(index, packet.CustomMessage, 0);
|
2015-05-16 01:15:44 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileSplit destFile = new FileSplit(downloadPath);
|
|
|
|
|
if (!destFile.AppendBlock(packet.Block, packet.CurrentBlock))
|
|
|
|
|
{
|
2015-05-29 21:34:57 +00:00
|
|
|
|
if (client.Value.FrmFm == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
client.Value.FrmFm.UpdateTransferStatus(index, destFile.LastError, 0);
|
2015-05-16 01:15:44 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decimal progress =
|
|
|
|
|
Math.Round((decimal)((double)(packet.CurrentBlock + 1) / (double)packet.MaxBlocks * 100.0), 2);
|
|
|
|
|
|
2015-05-29 21:34:57 +00:00
|
|
|
|
if (client.Value.FrmFm == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
client.Value.FrmFm.UpdateTransferStatus(index, string.Format("Downloading...({0}%)", progress), -1);
|
2015-05-16 01:15:44 +00:00
|
|
|
|
|
|
|
|
|
if ((packet.CurrentBlock + 1) == packet.MaxBlocks)
|
|
|
|
|
{
|
2015-05-29 21:34:57 +00:00
|
|
|
|
if (client.Value.FrmFm == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
client.Value.FrmFm.UpdateTransferStatus(index, "Completed", 1);
|
2015-05-16 01:15:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-05-29 21:34:57 +00:00
|
|
|
|
if (client.Value.FrmFm == null)
|
|
|
|
|
return;
|
2015-05-16 01:15:44 +00:00
|
|
|
|
|
2015-05-29 21:34:57 +00:00
|
|
|
|
client.Value.FrmFm.UpdateTransferStatus(index, "Canceled", 0);
|
|
|
|
|
}
|
2015-05-16 01:15:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|