Imports System.Net
Imports System.IO
Imports System.Text

Public Class Form1
    Dim ServerIP As String = "127.0.0.1"
    Dim ClientExe As String = "Odin.exe"
    Dim WebsiteAddress As String = "http://google.com/"
    Dim CashShopAddress As String = "http://google.com/"
    Dim ForumAddress As String = "http://google.com/"
    Dim Top100Address As String = "http://google.com/"
    Dim SignUpAddress As String = "http://google.com/"
    'DON'T EDIT AFTER THIS LINE IF YOU DON'T KNOW WHAT YOUR DOING!!
    '--------------------------------------------------------------
    Dim cpx, cpy As Int32
    Friend WithEvents downloader As New Net.WebClient
    Dim wc As New WebClient
    Dim MyFunction As New MyFunction
    Dim ClientConfigFile As String() = Nothing
    Dim PatchList As String() = Nothing
    Private Sub downloadPatches()
        If ServerVersionlabel.Text = ClientVersionLabel.Text Then
            LaunchBtn.Enabled = True
            StatusLabel.Text = "No new Updates avaiable!"
            PatchProgressLabel.Hide()
        ElseIf Val(ClientVersionLabel.Text) > Val(ServerVersionlabel.Text) Then
            MessageBox.Show("Overpatched. I'll restart now to repatch.", "Patcher :: Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Directory.Delete("Launcher", True)
            Application.Restart()
        Else
            StatusLabel.Text = "Getting Patch: " & ClientVersionLabel.Text
            ProgressBar1.Value = 0
            downloader.DownloadFileAsync(New Uri(PatchList(Val(ClientVersionLabel.Text) + Val(1)).Split(vbTab)(1)), "patch.rar")
        End If
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not File.Exists("UnRAR.exe") Then
            My.Computer.Network.DownloadFile(WebsiteAddress & "UnRAR.exe", "UnRAR.exe")
        End If
        If Not File.Exists("Launcher/config.lul") Then
            Directory.CreateDirectory("Launcher")
            File.WriteAllText("Launcher/config.lul", MyFunction.Encrypt("Version=0"))
        End If
        ClientConfigFile = IO.File.ReadAllLines("Launcher/config.lul")
        ClientConfigFile(0) = MyFunction.Decrypt(ClientConfigFile(0))

        PatchList = wc.DownloadString(WebsiteAddress & "list.txt").Split(vbNewLine)
        ClientVersionLabel.Text = ClientConfigFile(0).Split("=")(1)
        ServerVersionlabel.Text = MyFunction.CountPatches(WebsiteAddress & "list.txt")
        downloadPatches()
    End Sub
    Private Sub downloader_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles downloader.DownloadFileCompleted
        ClientVersionLabel.Text = Val(ClientVersionLabel.Text) + Val(1)
        Dim oProcess As Process
        oProcess = System.Diagnostics.Process.Start("UnRAR.exe", "x -y -ac patch.rar")
        oProcess.WaitForExit()
        File.Delete("patch.rar")
        IO.File.WriteAllText("Launcher/config.lul", MyFunction.Encrypt("LauncherVersion=" & ClientVersionLabel.Text))
        downloadPatches()
    End Sub
    Private Sub downloader_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles downloader.DownloadProgressChanged
        PatchProgressLabel.Text = e.ProgressPercentage & "%"
        ProgressBar1.Value = e.ProgressPercentage
    End Sub
    Private Sub PictureBox3_Click_1(sender As System.Object, e As System.EventArgs) Handles LaunchBtn.Click
        Dim MD5Pass As String = MyFunction.MD5StringHash(passwordtxtbx.Text)
        Dim wc As WebClient = New WebClient()

        Dim Login As String = wc.DownloadString(WebsiteAddress & "Login.php?Username=" & usernametxtbx.Text & "&Password=" & MD5Pass.ToLower())

        If Login.Contains("OK") Then
            Process.Start(ClientExe, "-osk_server " & ServerIP & " -osk_token " & Login.Split("#")(1) & " -osk_store " & CashShopAddress)
            Environment.Exit(0)
        Else
            MessageBox.Show("Please check your Password!", "AccountLogin :: Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub
    Private Sub PictureBox4_Click_1(sender As System.Object, e As System.EventArgs) Handles Forum.Click
        Process.Start(ForumAddress)
    End Sub

    Private Sub PictureBox7_Click_1(sender As System.Object, e As System.EventArgs) Handles Signup.Click
        Process.Start(SignUpAddress)
    End Sub

    Private Sub PictureBox6_Click(sender As System.Object, e As System.EventArgs) Handles Homepage.Click
        Process.Start(WebsiteAddress)
    End Sub

    Private Sub PictureBox2_Click_2(sender As System.Object, e As System.EventArgs) Handles Top100.Click
        Process.Start(Top100Address)
    End Sub

    Private Sub PictureBox8_Click(sender As System.Object, e As System.EventArgs) Handles Closebtn.Click
        Environment.Exit(0)
    End Sub
    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        cpx = e.X
        cpy = e.Y
    End Sub
    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Me.Top = Windows.Forms.Cursor.Position.Y() - cpy
            Me.Left = Windows.Forms.Cursor.Position.X() - cpx
        End If
    End Sub
    Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles usernametxtbx.KeyUp, passwordtxtbx.KeyUp
        If e.KeyCode = Keys.Enter Then PictureBox3_Click_1(sender, e)
    End Sub
End Class