Example #1
0
        private static List <Section> ReplaceSection(List <Section> sections, TablePatch patch)
        {
            if (patch.offset == null || patch.section == null || patch.data == null)
            {
                Console.WriteLine($"[ERROR] Incomplete patch, skipping...");
                return(sections);
            }
            // Get info from json patch
            int section = (int)patch.section;
            int offset  = (int)patch.offset;

            string[] stringData = patch.data.Split(' ');
            byte[]   data       = new byte[stringData.Length];
            for (int i = 0; i < data.Length; i++)
            {
                try
                {
                    data[i] = Convert.ToByte(stringData[i], 16);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"[ERROR] Couldn't parse hex string ({ex.Message}), skipping...");
                    return(sections);
                }
            }
            if (offset < 0)
            {
                Console.WriteLine($"[ERROR] Offset cannot be negative, skipping...");
                return(sections);
            }
            if (section >= sections.Count)
            {
                Console.WriteLine($"[ERROR] Section chosen is out of bounds, skipping...");
                return(sections);
            }
            if (offset + data.Length >= sections[section].data.Length)
            {
                using (MemoryStream
                       memoryStream = new MemoryStream())
                {
                    using (BinaryWriter bw = new BinaryWriter(memoryStream))
                    {
                        bw.Write(sections[section].data);
                        while (offset >= memoryStream.Length)
                        {
                            bw.Write((byte)0);
                        }
                        bw.BaseStream.Position = offset;
                        bw.Write(data);
                        sections[section].data = memoryStream.ToArray();
                        sections[section].size = sections[section].data.Length;
                    }
                }
            }
            else
            {
                using (MemoryStream
                       memoryStream = new MemoryStream(sections[section].data))
                {
                    using (BinaryWriter bw = new BinaryWriter(memoryStream))
                    {
                        if (offset >= memoryStream.Length)
                        {
                            bw.BaseStream.Position = memoryStream.Length - 1;
                            while (offset >= memoryStream.Length)
                            {
                                bw.Write((byte)0);
                            }
                        }
                        bw.BaseStream.Position = offset;
                        bw.Write(data);
                    }
                }
            }

            return(sections);
        }
Example #2
0
        private static List <NameSection> ReplaceName(List <NameSection> sections, byte[] patch, TablePatch namePatch)
        {
            int section = 0;
            int index   = 0;

            byte[] fileContents = null;
            if (patch != null)
            {
                section = Convert.ToInt32(patch[3]);
                index   = BitConverter.ToInt16(SliceArray(patch, 4, 6).Reverse().ToArray(), 0);
                // Contents is what to replace
                fileContents = SliceArray(patch, 6, patch.Length);
            }
            else if (namePatch != null)
            {
                if (namePatch.section == null || namePatch.index == null || namePatch.name == null)
                {
                    Console.WriteLine($"[ERROR] Incomplete patch, skipping...");
                    return(sections);
                }
                section      = (int)namePatch.section;
                index        = (int)namePatch.index;
                fileContents = ConvertName(namePatch.name);
                if (fileContents == null)
                {
                    return(sections);
                }
            }
            else
            {
                Console.WriteLine($"[ERROR] No patch passed to replace function, skipping...");
                return(sections);
            }

            if (section >= sections.Count)
            {
                Console.WriteLine($"[ERROR] Section chosen is out of bounds, skipping...");
                return(sections);
            }

            if (index < 0)
            {
                Console.WriteLine($"[ERROR] Index cannot be negative, skipping...");
                return(sections);
            }

            if (index >= sections[section].names.Count)
            {
                byte[] dummy = Encoding.ASCII.GetBytes("RESERVE");
                // Add RESERVE names if index is further down
                while (sections[section].names.Count < index)
                {
                    sections[section].pointers.Add((ushort)(sections[section].pointers.Last() + sections[section].names.Last().Length + 1));
                    sections[section].names.Add(dummy);
                    sections[section].pointersSize += 2;
                    sections[section].namesSize    += dummy.Length + 1;
                }
                // Add expanded name
                sections[section].pointers.Add((ushort)(sections[section].pointers.Last() + sections[section].names.Last().Length + 1));
                sections[section].names.Add(fileContents);
                sections[section].pointersSize += 2;
                sections[section].namesSize    += fileContents.Length + 1;
            }
            else
            {
                int delta = fileContents.Length - sections[section].names[index].Length;
                sections[section].names[index] = fileContents;
                sections[section].namesSize   += delta;
                for (int i = index + 1; i < sections[section].pointers.Count; i++)
                {
                    sections[section].pointers[i] += (UInt16)delta;
                }
            }
            return(sections);
        }