Exemple #1
0
        //Edits a byte array in memory
        public static byte[] EditAsset(byte[] file, api.Asset asset, bool Converting, out bool Compress)
        {
            Compress = false;
            using (MemoryStream stream = new MemoryStream(file))
            {
                int NumberOfReplaces = asset.Search.Length;
                for (int i = 0; i < NumberOfReplaces; i++)
                {
                    byte[] searchB  = ParseByteArray(asset.Search[i]);
                    byte[] replaceB = ParseByteArray(asset.Replace[i]);
                    byte[] RealReplace;
                    int    ReplaceIndex = 0, SearchIndex = 0;


                    if (Converting)
                    {
                        //Search is in the byte array
                        RealReplace = FillEnd(replaceB, searchB.Length);
                        SearchIndex = IndexOfSequence(file, searchB);
                        if (SearchIndex == -1)    //replace cannot be found so that means it is already swapped so skip it.
                        {
                            continue;
                        }
                    }
                    else
                    {
                        RealReplace  = FillEnd(searchB, replaceB.Length);
                        ReplaceIndex = IndexOfSequence(file, replaceB);
                        if (ReplaceIndex == -1)//search cannot be found so that means it is already swapped so skip it.
                        {
                            continue;
                        }
                    }
                    Compress = true;    //Change has been made so compress it

                    stream.Position = Math.Max(SearchIndex, ReplaceIndex);
                    stream.Write(RealReplace, 0, RealReplace.Length);
                }
                return(stream.ToArray());
            }
        }
Exemple #2
0
        public static bool EditAsset(ref byte[] file, api.Asset Asset, bool Converting)
        {
            using (MemoryStream stream = new MemoryStream(file))
            {
                bool Ready        = false;
                int  ReplaceCount = Asset.Search.Length;
                for (int i = 0; i < ReplaceCount; i++)
                {
                    byte[] searchB  = ParseByteArray(Asset.Search[i]);
                    byte[] replaceB = ParseByteArray(Asset.Replace[i]);

                    int SearchOffset  = IndexOfSequence(file, searchB);
                    int ReplaceOffset = IndexOfSequence(file, replaceB);

                    if (SearchOffset != -1 || ReplaceOffset != -1)
                    {
                        Ready = true;

                        if (searchB.Length >= replaceB.Length)
                        {
                            replaceB = FillEnd(replaceB, searchB.Length);

                            if (Converting)
                            {
                                stream.Position = SearchOffset;
                                stream.Write(replaceB, 0, replaceB.Length);
                                continue;
                            }
                            else
                            {
                                stream.Position = ReplaceOffset;
                                stream.Write(searchB, 0, searchB.Length);
                                continue;
                            }
                        }
                        else
                        {
                            if (Converting)
                            {
                                stream.Position = SearchOffset;
                                byte[] ConvertCheck = stream.ToArray();
                                if (ConvertCheck[SearchOffset + 2] == Convert.ToByte(0))
                                {
                                    int SearchLengthWeHave = SearchOffset;
                                    while (true)
                                    {
                                        if (Convert.ToByte(stream.ReadByte()) == 0)
                                        {
                                            SearchLengthWeHave++;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                    SearchLengthWeHave--;//Last char is to represent end of string in uasset
                                    if (SearchLengthWeHave > replaceB.Length)
                                    {
                                        stream.Position = SearchOffset;
                                        replaceB        = FillEnd(replaceB, SearchLengthWeHave);
                                        stream.Write(replaceB, 0, replaceB.Length);
                                        continue;
                                    }
                                    else
                                    {
                                        searchB = FillEnd(searchB, SearchLengthWeHave);
                                        byte[] tmp = ReplaceAnyLength(stream.ToArray(), searchB, replaceB);
                                        stream.Position = 0;
                                        stream.Write(tmp, 0, tmp.Length);
                                        continue;
                                    }
                                }
                                else
                                {
                                    byte[] tmp = ReplaceAnyLength(stream.ToArray(), searchB, replaceB);
                                    stream.Position = 0;
                                    stream.Write(tmp, 0, tmp.Length);
                                    continue;
                                }
                            }
                            else
                            {
                                //Just paste in the replace
                                if (ReplaceOffset != -1)
                                {
                                    stream.Position = ReplaceOffset;
                                    stream.Write(FillEnd(searchB, replaceB.Length), 0, replaceB.Length);
                                }
                                continue;
                            }
                        }
                    }
                }
                file = stream.ToArray();
                return(Ready);
            }
        }