Skip to content

Instantly share code, notes, and snippets.

@arilotter
Created August 11, 2012 19:49
Show Gist options
  • Save arilotter/3326688 to your computer and use it in GitHub Desktop.
Save arilotter/3326688 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using ForScience;
using System.Net;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public string username;
public string password;
public delegate void DelegateClose();
public delegate void DelegateSetStatus(string arg1);
public delegate void DelegateEnableLoginButton();
public Form1() {
InitializeComponent();
rememberCheckBox.Checked = Properties.Settings.Default.saveSettings;
if (rememberCheckBox.Checked)
{
userField.Text = Properties.Settings.Default.username;
passField.Text = Properties.Settings.Default.password;
}
}
private void loginButton_Click(object sender, EventArgs e) {
loginButton.Enabled = false;
SetStatusText("Logging in. . .");
username = userField.Text;
password = passField.Text;
if (requiresUpdate())
{
DialogResult dialogResult = MessageBox.Show("Update available! Do you want to update?", "Update", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
update();
return;
}
}
login();
}
public void login()
{
Thread loginThread = new Thread(loginThreaded);
loginThread.Start();
}
public void loginThreaded()
{
SetStatusText("Logging in. . .");
try
{
MyWebRequest request = new MyWebRequest("https://login.minecraft.net/", "POST", "user=" + username + "&password=" + password + "&version=1000");
string[] strArray = request.GetResponse().Split(new char[] { ':' });
string appData = Environment.GetEnvironmentVariable("APPDATA");
startGame(strArray[2], strArray[3]);
DoClose();
}
catch
{
SetStatusText("");
DialogResult dialogResult = MessageBox.Show("Error logging in. Do you wish to play in offline mode?", "Login", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
startGame("MrFaggot", "meow");
DoClose();
return;
}
else
{
EnableLoginButton();
return;
}
}
}
private void startGame(string username, string sessionid)
{
string appData = Environment.GetEnvironmentVariable("APPDATA");
Process mc = new Process();
// Stop the process from opening a new window
mc.StartInfo.RedirectStandardOutput = true;
mc.StartInfo.UseShellExecute = false;
mc.StartInfo.CreateNoWindow = true;
// Setup executable and parameters
mc.StartInfo.FileName = "java";
mc.StartInfo.Arguments = "-Xincgc -Xmx1024m -cp \""+appData + @"\.minecraft\bin\ForScience.jar;" + appData + @"\.minecraft\bin\lwjgl.jar;" + appData + @"\.minecraft\bin\lwjgl_util.jar;" + appData + "\\.minecraft\\bin\\jinput.jar\" -Djava.library.path=\"" + appData + "\\.minecraft\\bin\\natives\" net.minecraft.client.Minecraft " + username + " " + sessionid;
mc.Start();
}
public void update()
{
Thread updateThread = new Thread(updateThreaded);
updateThread.Start();
}
public void updateThreaded()
{
SetStatusText("Downloading update. . .");
String appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string remoteUri = "https://dl.dropbox.com/u/5332200/ForScience/ForScience.jar";
string fileName = appData + @"\.minecraft\bin\ForScience.jar";
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
myWebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadStatusChange);
myWebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadFileCompleted);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFileAsync(new Uri(remoteUri), fileName);
}
void downloadStatusChange(object sender, DownloadProgressChangedEventArgs e)
{
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;
SetStatusText(@"Download Progress: " + Math.Truncate(percentage).ToString() + @"%");
}
void downloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
String appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
File.Delete(appData + @"\.minecraft\bin\ForScience.md5");
File.Move(appData + @"\.minecraft\bin\update.md5", appData + @"\.minecraft\bin\ForScience.md5");
SetStatusText("Update Finished.");
login();
}
public bool requiresUpdate()
{
SetStatusText("Checking for updates. . .");
String appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (!File.Exists(appData + @"\.minecraft\bin\ForScience.md5"))
{
File.Create(appData + @"\.minecraft\bin\ForScience.md5");
return true;
}
if (!File.Exists(appData + @"\.minecraft\bin\ForScience.jar"))
{
return true;
}
string remoteUri = "https://dl.dropbox.com/u/5332200/ForScience/update.md5";
try
{
File.Delete(appData + @"\.minecraft\bin\update.md5");
}
catch {}
try
{
WebClient myWebClient = new WebClient();
string fileName = "update.md5";
myWebClient.DownloadFile(remoteUri, fileName);
File.Move("update.md5", appData + @"\.minecraft\bin\update.md5");
System.Collections.Generic.IEnumerable<string> localVersion = File.ReadLines(appData + @"\.minecraft\bin\ForScience.md5");
System.Collections.Generic.IEnumerable<string> remoteVersion = File.ReadLines(appData + @"\.minecraft\bin\update.md5");
if (localVersion.SequenceEqual(remoteVersion))
{
return false; // Does not need update
}
else
{
return true; // Needs update
}
}
catch
{
SetStatusText("Error checking for updates!");
return false;
}
}
public void DoClose()
{
if (this.InvokeRequired)
{
DelegateClose theDelegateMethod = new DelegateClose(this.DoClose);
this.Invoke(theDelegateMethod);
}
else
{
this.SaveSettings();
this.Close();
}
}
public void SetStatusText(string arg1)
{
if (this.InvokeRequired)
{
DelegateSetStatus theDelegateMethod = new DelegateSetStatus(this.SetStatusText);
this.Invoke(theDelegateMethod,arg1);
}
else
{
this.statusText.Text = arg1;
}
}
public void EnableLoginButton()
{
if (this.InvokeRequired)
{
DelegateEnableLoginButton theDelegateMethod = new DelegateEnableLoginButton(this.EnableLoginButton);
this.Invoke(theDelegateMethod);
}
else
{
loginButton.Enabled = true;
}
}
public void SaveSettings()
{
if (rememberCheckBox.Checked)
{
Properties.Settings.Default.username = username;
Properties.Settings.Default.password = password;
Properties.Settings.Default.saveSettings = rememberCheckBox.Checked;
}
else
{
Properties.Settings.Default.username = "";
Properties.Settings.Default.password = "";
Properties.Settings.Default.saveSettings = false;
}
Properties.Settings.Default.Save();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment