using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
namespace DarkWynter.App
{
public partial class ContentCompilerControl : UserControl
{
private static string contentFileName = "Content.contentproj";
private static string zipFileName = "G2L.zip";
private static string zipPassWord = "darkwynter";
private static string server_Zip = "http://darkwynter.com/_Episodes/G2L/";
private static string server_Files = "http://www.darkwynter.com/Beta2/Trunk/";
private static string userName = "darkwynter";
private static string password = "wynter1";
private static string localPath = ""; // Calced in CTOR
private static ProgressBar progress;
public ContentCompilerControl()
{
InitializeComponent();
progress = this.progressBarZipDownload;
progress.Minimum = 0;
progress.Maximum = 100;
progress.BackColor = Color.LightBlue;
}
#region Content Compiler - Content Project Management
///
/// Manually download a Content Project based on Content Project File
///
///
///
private void buttonDownloadContentProject_Click_1(object sender, EventArgs e)
{
DownloadContentProject();
}
private void buttonDownloadContent_Click(object sender, EventArgs e)
{
DownloadContentFiles();
}
///
/// Download the Content Project File from the Web.
///
public static void DownloadContentProject()
{
// Open the web client and get the content file.
WebClient client = new WebClient();
try
{
client.Credentials = new NetworkCredential(userName, password);
client.DownloadFile(server_Files + contentFileName, localPath + contentFileName);
}
catch (WebException we)
{
// If not connecting to server should init alert box, not fail...
Console.WriteLine(we.ToString());
}
client.Dispose();
//DownLoadFileInBackground(
// ftpServer + assetFilePath + assetFileName,
// localPath + assetFilePath + assetFileName
// );
}
///
/// Open content.project file and parse for all content filenames.
/// Download each file to local client.
///
/// Content Project xml is generated by VSEC# and XNA.
///
public static void DownloadContentFiles()
{
// Read the Xml File
try
{
XmlDocument reader = new XmlDocument();
reader.Load(localPath + contentFileName);
// Unpack the Project Node
foreach (XmlNode projectNode in reader.ChildNodes)
{
if (projectNode.Name == "Project")
{
// Unpack the Item Node
foreach (XmlNode itemNode in projectNode.ChildNodes)
{
if (itemNode.Name == "ItemGroup")
{
// Unpack the Compile Node
foreach (XmlNode compileNode in itemNode.ChildNodes)
{
if (compileNode.Name == "Compile")
{
// Get the "relative path + filename" of the asset file
string assetFileName = compileNode.Attributes["Include"].Value;
string assetFilePath = "";
// Parse folder path from file name
int finger = assetFileName.IndexOf("\\");
int oldfinger = finger;
while (finger != -1)
{
// Keep track of the last "\\"
oldfinger = finger;
finger = assetFileName.IndexOf("\\", oldfinger + 1);
}
if (finger == -1)
{
// When last "\\" is found save path and file indepentently
assetFilePath = assetFileName.Substring(0, oldfinger + 1);
assetFileName = assetFileName.Substring(
oldfinger + 1,
(assetFileName.Length) - (oldfinger + 1));
}
// Determine whether the directory exists.
if (!Directory.Exists(localPath + assetFilePath))
{
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(localPath + assetFilePath);
Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(localPath + assetFilePath));
}
DownLoadFileInBackground(
server_Files + assetFilePath + assetFileName,
localPath + assetFilePath + assetFileName
);
}
}
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void DownLoadFileInBackground(string serverAddress, string localAddress)
{
WebClient client = new WebClient();
Uri uri = new Uri(serverAddress);
// Specify that the DownloadFileCallback method gets called
// when the download completes.
client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCallback);
// Specify a progress notification handler.
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
client.DownloadFileAsync(uri, localAddress);
}
private static void DownloadFileCallback(object sender, AsyncCompletedEventArgs e)
{
progress.BackColor = Color.LightGreen;
// Displays the operation identifier, and the transfer progress.
if (e.Error != null)
{
throw new Exception("Error Downloading File");
}
Console.WriteLine("Download Complete");
// Unzip the content
DarkWynter.Engine.Utilities.ZipUtility.UnZipFiles(
localPath + zipFileName,
localPath + "/Demo",
zipPassWord,
true
);
}
private static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
progress.Value = e.ProgressPercentage;
// Displays the operation identifier, and the transfer progress.
//Console.WriteLine("{0} downloaded {1} of {2} bytes. {3} % complete...",
// (string)e.UserState,
// e.BytesReceived,
// e.TotalBytesToReceive,
// e.ProgressPercentage);
}
private static void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
{
// Displays the operation identifier, and the transfer progress.
Console.WriteLine("{0} uploaded {1} of {2} bytes. {3} % complete...",
(string)e.UserState,
e.BytesSent,
e.TotalBytesToSend,
e.ProgressPercentage);
}
#endregion
}
}