Exemple #1
0
        public void InstallRedist(
            string gamepath,
            string source,
            RedistFile.UpdateDelegate upd,
            bool uninstall,
            List <string> ignoreList)
        {
            StreamReader streamReader = new StreamReader(source, Encoding.Default);
            string       path1        = !(streamReader.ReadLine() != "BFI") ? streamReader.ReadLine() : throw new Exception("Not a valid install script");
            string       str1         = string.Format("{0}\\PakFiles\\{1}", (object)gamepath, (object)path1);
            string       str2         = string.Format("{0}\\PakFiles\\{1}_Uninstall.bfi", (object)gamepath, (object)path1.Substring(0, path1.LastIndexOf(".") - 4));

            if (File.Exists(str1) && File.Exists(str2))
            {
                if (!uninstall)
                {
                    throw new PackageAlreadyInstalledException();
                }
                this.UnInstall(gamepath, str2, str1, upd);
            }
            else
            {
                if (upd != null)
                {
                    upd("Preparing install. Please wait...");
                }
                else
                {
                    Console.WriteLine("Preparing install. Please wait...");
                }
                FileStream fileStream1 = new FileStream(path1, FileMode.Open, FileAccess.Read);
                Dictionary <string, int>             dictionary1 = new Dictionary <string, int>();
                Dictionary <string, RedistFileEntry> dictionary2 = new Dictionary <string, RedistFileEntry>();
                byte[] numArray1 = new byte[4];
                this.Assert(fileStream1.Read(numArray1, 0, 4), 4);
                if (Encoding.Default.GetString(numArray1) == "BFFZ")
                {
                    fileStream1.Seek(8L, SeekOrigin.Begin);
                    this.Assert(fileStream1.Read(numArray1, 0, 4), 4);
                    int int32_1 = BitConverter.ToInt32(numArray1, 0);
                    this.Assert(fileStream1.Read(numArray1, 0, 4), 4);
                    int    int32_2   = BitConverter.ToInt32(numArray1, 0);
                    byte[] numArray2 = new byte[256];
                    fileStream1.Seek(16L, SeekOrigin.Begin);
                    for (int index = 0; index < int32_2; ++index)
                    {
                        this.Assert(fileStream1.Read(numArray2, 0, 256), 256);
                        string str3 = Encoding.Default.GetString(numArray2);
                        this.Assert(fileStream1.Read(numArray1, 0, 4), 4);
                        int int32_3 = BitConverter.ToInt32(numArray1, 0);
                        this.Assert(fileStream1.Read(numArray1, 0, 4), 4);
                        int int32_4 = BitConverter.ToInt32(numArray1, 0);
                        dictionary1.Add(str3.Replace(char.MinValue, '?').Replace("?", ""), int32_3);
                        fileStream1.Seek((long)int32_4, SeekOrigin.Begin);
                    }
                    fileStream1.Seek((long)int32_1, SeekOrigin.Begin);
                    this.Assert(fileStream1.Read(numArray1, 0, 4), 4);
                    int           result      = BitConverter.ToInt32(numArray1, 0);
                    List <string> stringList1 = new List <string>();
                    for (int i = 0; i < result; ++i)
                    {
                        this.Assert(fileStream1.Read(numArray1, 0, 4), 4);
                        int int32_3 = BitConverter.ToInt32(numArray1, 0);
                        this.Assert(fileStream1.Read(numArray1, 0, 4), 4);
                        int int32_4 = BitConverter.ToInt32(numArray1, 0);
                        this.Assert(fileStream1.Read(numArray1, 0, 4), 4);
                        int    int32_5 = BitConverter.ToInt32(numArray1, 0);
                        byte[] buffer  = new byte[1];
                        this.Assert(fileStream1.Read(buffer, 0, 1), 1);
                        int    length    = (int)buffer[0];
                        byte[] numArray3 = new byte[length];
                        this.Assert(fileStream1.Read(numArray3, 0, length), length);
                        int             position        = (int)fileStream1.Position;
                        RedistFileEntry redistFileEntry = new RedistFileEntry(i, Encoding.Default.GetString(numArray3), int32_4, int32_5, position);
                        dictionary2.Add(redistFileEntry.Name, redistFileEntry);
                        stringList1.Add(redistFileEntry.Name);
                        fileStream1.Seek((long)int32_3, SeekOrigin.Begin);
                    }
                    byte[]       buffer1      = new byte[4];
                    RedistHeader redistHeader = new RedistHeader(str1.Substring(0, str1.LastIndexOf("\\")));
                    File.Copy(new FileInfo(source).FullName, str2);
                    FileStream fileStream2 = new FileStream(str1, FileMode.Create, FileAccess.ReadWrite);
                    fileStream2.Write(redistHeader.Magic, 0, 4);
                    int position1 = (int)fileStream2.Position;
                    fileStream2.Write(BitConverter.GetBytes(redistHeader.FileSize), 0, 4);
                    int position2 = (int)fileStream2.Position;
                    fileStream2.Write(BitConverter.GetBytes(0), 0, 4);
                    fileStream2.Write(BitConverter.GetBytes(int32_2), 0, 4);
                    fileStream2.Seek(16L, SeekOrigin.Begin);
                    foreach (string key in dictionary1.Keys)
                    {
                        int num1 = dictionary1[key];
                        for (int index = 0; index < numArray2.Length; ++index)
                        {
                            numArray2[index] = (byte)0;
                        }
                        Encoding.Default.GetBytes(key).CopyTo((Array)numArray2, 0);
                        fileStream2.Write(numArray2, 0, numArray2.Length);
                        fileStream2.Write(BitConverter.GetBytes(num1), 0, 4);
                        int position3 = (int)fileStream2.Position;
                        fileStream2.Write(buffer1, 0, 4);
                        foreach (string str3 in stringList1)
                        {
                            str3.Replace("\\-", "\\").Replace("\\+", "\\");
                            fileStream2.Write(BitConverter.GetBytes(stringList1.IndexOf(str3)), 0, 4);
                        }
                        int position4 = (int)fileStream2.Position;
                        int num2      = position4 + (16 - position4 % 16);
                        fileStream2.Seek((long)position3, SeekOrigin.Begin);
                        fileStream2.Write(BitConverter.GetBytes(num2), 0, 4);
                        fileStream2.Seek((long)num2, SeekOrigin.Begin);
                    }
                    int position5 = (int)fileStream2.Position;
                    int num3      = position5 + (16 - position5 % 16);
                    fileStream2.Seek((long)position2, SeekOrigin.Begin);
                    fileStream2.Write(BitConverter.GetBytes(num3), 0, 4);
                    fileStream2.Seek((long)num3, SeekOrigin.Begin);
                    int num4 = num3;
                    fileStream2.Write(buffer1, 0, 4);
                    Dictionary <string, RedistFileEntry> dictionary3 = new Dictionary <string, RedistFileEntry>();
                    List <string> stringList2 = new List <string>();
                    BFF           bff         = new BFF();
label_30:
                    string str4 = streamReader.ReadLine();
                    if (str4 != null)
                    {
                        string[] strArray = str4.Split('=');
                        string   str3     = strArray[0];
                        if (!int.TryParse(strArray[1], out result))
                        {
                            throw new Exception("BFI script is corrupt. Not valid number of files");
                        }
                        string str5 = (gamepath + "\\" + str3).Replace("\\\\", "\\");
                        if (!File.Exists(str5))
                        {
                            for (int index = 0; index < result; ++index)
                            {
                                streamReader.ReadLine();
                            }
                            goto label_30;
                        }
                        else
                        {
                            string str6 = str5 + ".bkp";
                            if (!File.Exists(str6))
                            {
                                if (!stringList2.Contains(str6))
                                {
                                    stringList2.Add(str6);
                                }
                                File.Copy(str5, str6);
                            }
                            string msg = string.Format("Updating {0} file(s) in {1}...", (object)result, (object)str3);
                            if (upd != null)
                            {
                                upd(msg);
                            }
                            else
                            {
                                Console.WriteLine(msg);
                            }
                            List <string> fileList = bff.GetFileList(str5);
                            FileStream    fs       = new FileStream(str5, FileMode.Open);
                            try
                            {
                                for (int index = 0; index < result; ++index)
                                {
                                    string str7 = streamReader.ReadLine();
                                    string str8 = str7.Replace("\\-", "\\").Replace("\\+", "\\");
                                    if (dictionary2.ContainsKey(str7))
                                    {
                                        RedistFileEntry redistFileEntry1 = dictionary2[str7];
                                        if (!dictionary3.ContainsKey(str8))
                                        {
                                            int num1 = 304 + 42 * fileList.IndexOf(str8);
                                            fs.Seek((long)(num1 + 8), SeekOrigin.Begin);
                                            fs.Read(numArray1, 0, 4);
                                            int int32_3 = BitConverter.ToInt32(numArray1, 0);
                                            fs.Read(numArray1, 0, 4);
                                            fs.Read(numArray1, 0, 4);
                                            int int32_4 = BitConverter.ToInt32(numArray1, 0);
                                            fs.Read(numArray1, 0, 4);
                                            int             int32_5          = BitConverter.ToInt32(numArray1, 0);
                                            RedistFileEntry redistFileEntry2 = new RedistFileEntry(redistFileEntry1.Index, redistFileEntry1.Name, int32_4, int32_5, int32_3);
                                            dictionary3.Add(str7, redistFileEntry2);
                                            byte[] buffer2 = new byte[int32_4];
                                            fs.Seek((long)int32_3, SeekOrigin.Begin);
                                            fs.Read(buffer2, 0, int32_4);
                                            int position3 = (int)fileStream2.Position;
                                            fileStream2.Write(buffer1, 0, 4);
                                            fileStream2.Write(BitConverter.GetBytes(int32_4), 0, 4);
                                            fileStream2.Write(BitConverter.GetBytes(int32_5), 0, 4);
                                            byte[] buffer3 = new byte[1]
                                            {
                                                (byte)str8.Length
                                            };
                                            fileStream2.Write(buffer3, 0, 1);
                                            fileStream2.Write(Encoding.Default.GetBytes(str8), 0, str8.Length);
                                            fileStream2.Write(buffer2, 0, int32_4);
                                            int position4 = (int)fileStream2.Position;
                                            int num2      = position4 + (16 - position4 % 16);
                                            fileStream2.Seek((long)position3, SeekOrigin.Begin);
                                            fileStream2.Write(BitConverter.GetBytes(num2), 0, 4);
                                            fileStream2.Seek((long)num2, SeekOrigin.Begin);
                                        }
                                        if (ignoreList == null || ignoreList != null && !ignoreList.Contains(str8))
                                        {
                                            byte[] numArray3 = new byte[redistFileEntry1.Size];
                                            fileStream1.Seek((long)redistFileEntry1.Offset, SeekOrigin.Begin);
                                            this.Assert(fileStream1.Read(numArray3, 0, redistFileEntry1.Size), redistFileEntry1.Size);
                                            if (!str3.StartsWith("PakFiles\\"))
                                            {
                                                str3 = "PakFiles\\" + str3;
                                            }
                                            bff.Inject(str5, str7, numArray3, redistFileEntry1.Size, redistFileEntry1.USize, fs, fileList, (RedistFile.UpdateDelegate)null);
                                            if (upd != null && !bff.Result.StartsWith("OK"))
                                            {
                                                upd(bff.Result);
                                            }
                                        }
                                        else if (upd != null)
                                        {
                                            upd(string.Format("Ignoring file {0}", (object)str8));
                                        }
                                    }
                                }
                                goto label_30;
                            }
                            finally
                            {
                                fs.Close();
                            }
                        }
                    }
                    else
                    {
                        int position3 = (int)fileStream2.Position;
                        fileStream2.Seek((long)num4, SeekOrigin.Begin);
                        fileStream2.Write(BitConverter.GetBytes(dictionary3.Count), 0, 4);
                        fileStream2.Seek((long)position1, SeekOrigin.Begin);
                        fileStream2.Write(BitConverter.GetBytes(position3), 0, 4);
                        fileStream2.Close();
                        foreach (string path2 in stringList2)
                        {
                            File.Delete(path2);
                        }
                    }
                }
                fileStream1.Close();
                streamReader.Close();
                if (upd != null)
                {
                    upd("Install finished");
                }
                else
                {
                    Console.WriteLine("Install finished");
                }
            }
        }
Exemple #2
0
        public void UnInstall(
            string gamepath,
            string source,
            string unpath,
            RedistFile.UpdateDelegate upd)
        {
            StreamReader streamReader = new StreamReader(source, Encoding.Default);

            if (upd != null)
            {
                upd("Starting MOD uninstall, please wait...");
            }
            if (streamReader.ReadLine() != "BFI")
            {
                throw new Exception("Not a valid install script");
            }
            streamReader.ReadLine();
            FileStream fileStream = new FileStream(unpath, FileMode.Open, FileAccess.Read);
            Dictionary <string, int>             dictionary1 = new Dictionary <string, int>();
            Dictionary <string, RedistFileEntry> dictionary2 = new Dictionary <string, RedistFileEntry>();

            byte[] numArray1 = new byte[4];
            this.Assert(fileStream.Read(numArray1, 0, 4), 4);
            if (Encoding.Default.GetString(numArray1) == "BFFZ")
            {
                fileStream.Seek(8L, SeekOrigin.Begin);
                this.Assert(fileStream.Read(numArray1, 0, 4), 4);
                int int32_1 = BitConverter.ToInt32(numArray1, 0);
                this.Assert(fileStream.Read(numArray1, 0, 4), 4);
                int int32_2 = BitConverter.ToInt32(numArray1, 0);
                fileStream.Seek(16L, SeekOrigin.Begin);
                for (int index = 0; index < int32_2; ++index)
                {
                    byte[] numArray2 = new byte[256];
                    this.Assert(fileStream.Read(numArray2, 0, 256), 256);
                    string key = Encoding.Default.GetString(numArray2);
                    this.Assert(fileStream.Read(numArray1, 0, 4), 4);
                    int int32_3 = BitConverter.ToInt32(numArray1, 0);
                    this.Assert(fileStream.Read(numArray1, 0, 4), 4);
                    int int32_4 = BitConverter.ToInt32(numArray1, 0);
                    dictionary1.Add(key, int32_3);
                    fileStream.Seek((long)int32_4, SeekOrigin.Begin);
                }
                fileStream.Seek((long)int32_1, SeekOrigin.Begin);
                this.Assert(fileStream.Read(numArray1, 0, 4), 4);
                int result = BitConverter.ToInt32(numArray1, 0);
                for (int i = 0; i < result; ++i)
                {
                    this.Assert(fileStream.Read(numArray1, 0, 4), 4);
                    int int32_3 = BitConverter.ToInt32(numArray1, 0);
                    this.Assert(fileStream.Read(numArray1, 0, 4), 4);
                    int int32_4 = BitConverter.ToInt32(numArray1, 0);
                    this.Assert(fileStream.Read(numArray1, 0, 4), 4);
                    int    int32_5 = BitConverter.ToInt32(numArray1, 0);
                    byte[] buffer  = new byte[1];
                    this.Assert(fileStream.Read(buffer, 0, 1), 1);
                    int    length    = (int)buffer[0];
                    byte[] numArray2 = new byte[length];
                    this.Assert(fileStream.Read(numArray2, 0, length), length);
                    int             position        = (int)fileStream.Position;
                    RedistFileEntry redistFileEntry = new RedistFileEntry(i, Encoding.Default.GetString(numArray2), int32_4, int32_5, position);
                    dictionary2.Add(redistFileEntry.Name, redistFileEntry);
                    fileStream.Seek((long)int32_3, SeekOrigin.Begin);
                }
                List <string> stringList = new List <string>();
                BFF           bff        = new BFF();
label_12:
                string str1 = streamReader.ReadLine();
                if (str1 != null)
                {
                    string[] strArray = str1.Split('=');
                    string   str2     = strArray[0];
                    if (!int.TryParse(strArray[1], out result))
                    {
                        throw new Exception("BFI script is corrupt. Not valid number of files");
                    }
                    string str3 = (gamepath + "\\" + str2).Replace("\\\\", "\\");
                    if (!File.Exists(str3))
                    {
                        for (int index = 0; index < result; ++index)
                        {
                            streamReader.ReadLine();
                        }
                        goto label_12;
                    }
                    else
                    {
                        string path = str3 + ".bkp";
                        if (!File.Exists(path))
                        {
                            if (!stringList.Contains(path))
                            {
                                stringList.Add(path);
                            }
                            File.Copy(str3, str3 + ".bkp");
                        }
                        string msg = string.Format("Restoring {0} file(s) in {1}...", (object)result, (object)str2);
                        if (upd != null)
                        {
                            upd(msg);
                        }
                        else
                        {
                            Console.WriteLine(msg);
                        }
                        List <string> fileList = bff.GetFileList(str3);
                        FileStream    fs       = new FileStream(str3, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
                        try
                        {
                            for (int index = 0; index < result; ++index)
                            {
                                string str4 = streamReader.ReadLine().Replace("\\-", "\\").Replace("\\+", "\\");
                                if (dictionary2.ContainsKey(str4))
                                {
                                    RedistFileEntry redistFileEntry = dictionary2[str4];
                                    byte[]          numArray2       = new byte[redistFileEntry.Size];
                                    fileStream.Seek((long)redistFileEntry.Offset, SeekOrigin.Begin);
                                    this.Assert(fileStream.Read(numArray2, 0, redistFileEntry.Size), redistFileEntry.Size);
                                    if (!str2.StartsWith("PakFiles\\"))
                                    {
                                        str2 = "PakFiles\\" + str2;
                                    }
                                    bff.Inject(str3, str4, numArray2, redistFileEntry.Size, redistFileEntry.USize, fs, fileList, (RedistFile.UpdateDelegate)null);
                                    if (upd != null && !bff.Result.StartsWith("OK"))
                                    {
                                        upd(bff.Result);
                                    }
                                }
                            }
                            goto label_12;
                        }
                        catch (Exception ex)
                        {
                            if (upd != null)
                            {
                                return;
                            }
                            Console.WriteLine(ex.Message + "\r\nPress ENTER: ");
                            Console.ReadLine();
                            return;
                        }
                        finally
                        {
                            fs.Close();
                        }
                    }
                }
                else
                {
                    foreach (string path in stringList)
                    {
                        File.Delete(path);
                    }
                }
            }
            fileStream.Close();
            streamReader.Close();
            if (upd != null)
            {
                upd("Deleting uninstall files");
            }
            File.Delete(source);
            File.Delete(unpath);
            if (upd == null)
            {
                return;
            }
            upd("Uninstall finished");
        }