Example #1
0
        private void Start_Update()
        {
            bool Update = false;
            int  Status = 1;

            Update = NeedUpdate();

            if (!Update)
            {
                //MessageBox.Show("Nothing to be patched", "Patch", MessageBoxButtons.OK, MessageBoxIcon.Information);
                UpdateCompleate(Status);
                return;
            }

            string[] Files = FileList.Split('\n');
            string   CRC32;

            FilesToPatch = "";

            // Generate a List of Files that need patched
            foreach (string Lines in Files)
            {
                string[] Data = Lines.Split('\t');

                // Check CRC if file exists
                try
                {
                    uint crc = Crc32.GetFileCRC32(Data[0]);
                    CRC32 = String.Format("{0:X8}", crc);
                }
                catch
                {
                    CRC32 = "";
                    // if its our file name read from seperate file
                    if (Data[0] == Me)
                    {
                        try
                        {
                            StreamReader readcrc = File.OpenText(Me + ".crc");
                            CRC32 = readcrc.ReadLine();
                            readcrc.Close();
                        }
                        catch
                        {
                            CRC32 = "";
                        }
                    }
                }

                // If the CRC's match don't download the file
                if (Data[0] != "" && CRC32.CompareTo(Data[1]) != 0)
                {
                    FilesToPatch += Data[0] + "\t" + Data[1] + "\n";
                }
            }

            // Download/Patch the files
            string[] PatchFiles = FilesToPatch.Split('\n');
            int      LoopCount  = 0;

            foreach (string File in PatchFiles)
            {
                // We are done
                if (File == "")
                {
                    break;
                }

                string[] Data = File.Split('\t');

                // Update File count
                LoopCount++;

                // Display file downloading
                FileProgress.Invoke(new UpdateFileNameCallback(UpdateFileName),
                                    new object[] { Data[0] }
                                    );


                // Download File
                if (!DownloadFile(Data[0], Data[0] == Me ? MeAlt : Data[0]))
                {
                    // if an error exit out of this
                    return;
                }

                // Let the patcher know to restart
                if (Data[0] == Me)
                {
                    Status = 2;
                }

                // Check CRC32 after download
                uint crc = Crc32.GetFileCRC32(Data[0] == Me? MeAlt : Data[0]);
                CRC32 = String.Format("{0:X8}", crc);

                if (Data[0] != "" && CRC32.CompareTo(Data[1]) != 0)
                {
                    MessageBox.Show("CRC32 Error in File " + Data[0] + "\nPlease run the patcher Again", "CRC32 Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // Save our CRC in a file
                if (Data[0] == Me)
                {
                    FileInfo     crcfile  = new FileInfo(Me + ".crc");
                    StreamWriter writecrc = crcfile.CreateText();
                    writecrc.WriteLine(Data[1]);
                    writecrc.Close();
                }

                // Update total progress bar
                FileProgress.Invoke(new UpdateTotalProgressCallback(UpdateTotalProgress),
                                    new object[] { LoopCount, PatchFiles.Length - 1 }
                                    );
            }
            // Download the newest version info
            DownloadFile("Version.txt", "Version.txt");
            MessageBox.Show("All Files are Updated", "Patching Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
            UpdateCompleate(Status);
            return;
        }