public void WriteStrings(StreamFunctionAdd sfa)
 {
     sfa.PositionStream = (long)this.offsetStrings;
     sfa.LengthStream   = (long)this.offsetStrings;
     for (int index = 0; index < this.str.Count; ++index)
     {
         this.str[index].WriteData(sfa);
         if (index > 0)
         {
             uint num1 = this.str[index].offset - this.offsetStrings;
             sfa.PositionStream = (long)(this.str[index].myOffset - 1U);
             if (num1 < 0x10)
             {
                 byte num2 = (byte)(0x10 + num1);
                 sfa.WriteByte(num2);
                 sfa.WriteByte((byte)0);
             }
             else if (num1 < 0x1000)
             {
                 byte num2 = (byte)(0x50 + (num1 >> 8));
                 sfa.WriteByte(num2);
                 byte num3 = (byte)(num1 & (uint)byte.MaxValue);
                 sfa.WriteUInt16((ushort)num3);
             }
             else if (num1 > 0xFFF)
             {
                 sfa.WriteByte(0x90);
                 sfa.WriteUInt16((ushort)num1);
             }
             sfa.PositionStream = sfa.LengthStream;
         }
     }
     sfa.SeekValueWrite(2U);
 }
 public sceHeader(StreamFunctionAdd sfa)
 {
     this.offsetScript  = !(sfa.ReadAnsiStringSize(8) != "TOD1RSCE") ? sfa.ReadUInt32() : throw new Exception("Error #1: Bad Magic");
     this.offsetStrings = sfa.ReadUInt32();
     this.str           = new List <sceStrings>();
     this.sizes         = new List <int>();
     sfa.PositionStream = (long)this.offsetScript;
     while (sfa.PositionStream < (long)this.offsetStrings)
     {
         if (sfa.ReadByte() == 0x47)
         {
             byte num = sfa.ReadByte();
             if ((int)num >> 4 == 1 && sfa.ReadByte() == (byte)0)
             {
                 this.str.Add(new sceStrings((uint)sfa.PositionStream - 1, this.offsetStrings)
                 {
                     offset     = ((uint)num & 0xF) + this.offsetStrings,
                     typeOffset = (byte)1
                 });
             }
             if ((int)num >> 4 == 5)
             {
                 sceStrings sceStrings = new sceStrings((uint)sfa.PositionStream, this.offsetStrings);
                 sceStrings.offset     = (uint)(((int)num & 0xF) << 8) + (uint)sfa.ReadByte() + this.offsetStrings;
                 sceStrings.typeOffset = (byte)2;
                 if (sfa.ReadByte() == (byte)0)
                 {
                     this.str.Add(sceStrings);
                 }
             }
             if (num == 0x90)
             {
                 sceStrings sceStrings = new sceStrings((uint)sfa.PositionStream, this.offsetStrings);
                 sceStrings.offset     = (uint)sfa.ReadUInt16() + this.offsetStrings;
                 sceStrings.typeOffset = (byte)3;
                 if (sfa.ReadByte() == (byte)0)
                 {
                     this.str.Add(sceStrings);
                 }
             }
         }
     }
     this.str.Sort((Comparison <sceStrings>)((s1, s2) => s1.offset.CompareTo(s2.offset)));
     for (int index = 0; index < this.str.Count; ++index)
     {
         if (index < this.str.Count - 1)
         {
             this.sizes.Add((int)this.str[index + 1].offset - (int)this.str[index].offset);
         }
         else
         {
             this.sizes.Add((int)(uint)sfa.LengthStream - (int)this.str[index].offset);
         }
     }
     for (int index = 0; index < this.str.Count; ++index)
     {
         this.str[index].ReadData(sfa, this.sizes[index]);
     }
 }
Example #3
0
 public void WriteData(StreamFunctionAdd sfa)
 {
     if (typeOffset == OffsetType.LargeOffset && sfa.PositionStream - baseOffset < 4095L)
     {
         while (sfa.PositionStream - baseOffset < 4095L)
         {
             sfa.WriteByte(0);
         }
     }
     offset = (uint)sfa.PositionStream;
     sfa.WriteBytes(data.ToArray());
 }
Example #4
0
 public void ReadData(StreamFunctionAdd sfa, int size = -1)
 {
     sfa.PositionStream = offset;
     if (size <= 0)
     {
         return;
     }
     for (int index = 0; index < size; ++index)
     {
         data.Add(sfa.ReadByte());
     }
 }
Example #5
0
 public void WriteData(StreamFunctionAdd sfa)
 {
     if (this.typeOffset == (byte)3 && sfa.PositionStream - (long)this.baseOffset < 4095L)
     {
         while (sfa.PositionStream - (long)this.baseOffset < 4095L)
         {
             sfa.WriteByte((byte)0);
         }
     }
     this.offset = (uint)sfa.PositionStream;
     sfa.WriteBytes(this.data.ToArray());
 }
Example #6
0
        private static void AddBytes(string fileName, string[] args)
        {
            int result = 0;

            Console.ForegroundColor = ConsoleColor.Red;
            for (int index = 0; index < args.Length; ++index)
            {
                if (args[index] == "-as")
                {
                    Console.WriteLine(" -Activate add bytes to start.");
                    int.TryParse(args[index + 1], out result);
                }
            }
            Console.ForegroundColor = ConsoleColor.White;
            StreamFunctionAdd streamFunctionAdd1 = new StreamFunctionAdd();
            StreamFunctionAdd streamFunctionAdd2 = new StreamFunctionAdd(fileName);

            if (flag2)
            {
                streamFunctionAdd2.PositionStream = streamFunctionAdd2.LengthStream;
                streamFunctionAdd2.SeekValueWrite(16U);
            }
            if (!flag1)
            {
                return;
            }
            StreamFunctionAdd destination = new StreamFunctionAdd();

            for (int index = 0; index < result; ++index)
            {
                destination.WriteByte((byte)0);
            }
            streamFunctionAdd2.PositionStream = 0L;
            streamFunctionAdd2.CopyPartTo(destination, (int)streamFunctionAdd2.LengthStream);
            streamFunctionAdd2.Dispose();
            destination.SaveToFile(fileName);
        }
Example #7
0
 public sceModule(string fileName)
 {
     this.mainSfa = new StreamFunctionAdd(fileName);
     this.header  = new sceHeader(this.mainSfa);
 }
Example #8
0
        public void WriteStrings(StreamFunctionAdd sfa, bool dedup = false)
        {
            sfa.PositionStream = offsetStrings;
            sfa.LengthStream   = offsetStrings;
            List <int> failedMediumStrings = new List <int>();
            List <int> failedShortStrings  = new List <int>();
            uint       lastShortLength     = 0;
            uint       lastMediumLength    = 0;
            long       realPos             = 0;
            bool       matched             = false;

            for (int index = 0; index < fileStrings.Count; ++index)
            {
                if (dedup)
                {
                    for (int index1 = index - 1; index1 >= 0; --index1)
                    {
                        if (index == 0)
                        {
                            break;
                        }
                        if (fileStrings[index].data.Count == fileStrings[index1].data.Count)
                        {
                            if (System.Linq.Enumerable.SequenceEqual(fileStrings[index].data, fileStrings[index1].data))
                            {
                                realPos = sfa.PositionStream;
                                fileStrings[index].offset = fileStrings[index1].offset;
                                matched = true;
                                break;
                            }
                        }
                    }

                    if (!matched)
                    {
                        fileStrings[index].WriteData(sfa);
                    }
                }
                else
                {
                    fileStrings[index].WriteData(sfa);
                }

                if (index > 0)
                {
                    uint num1 = fileStrings[index].offset - offsetStrings;
                    sfa.PositionStream = fileStrings[index].myOffset - 1U;
                    if (num1 < 0x10 && fileStrings[index].typeOffset == sceStrings.OffsetType.ShortOffset)
                    {
                        byte num2 = (byte)(0x10 + num1);
                        sfa.WriteByte(num2);
                        sfa.WriteByte(0);
                    }
                    else if (num1 < 0x1000 && fileStrings[index].typeOffset == sceStrings.OffsetType.MediumOffset)
                    {
                        byte num2 = (byte)(0x50 + (num1 >> 8));
                        sfa.WriteByte(num2);
                        byte num3 = (byte)(num1 & byte.MaxValue);
                        sfa.WriteUInt16(num3);
                    }
                    else if (num1 < 0x10000 && fileStrings[index].typeOffset == sceStrings.OffsetType.LargeOffset)
                    {
                        sfa.WriteByte(0x90);
                        sfa.WriteUInt16((ushort)num1);
                    }
                    else
                    {
                        Console.OutputEncoding = System.Text.Encoding.UTF8;
                        if (Program.verbose)
                        {
                            MiscUtils.Warn("Can't fit desired pointer in the available space...");
                            MiscUtils.Warn("- Failed at block: " + index + ", around line " + lineNumberList[index]);
                            MiscUtils.Warn("- String: " + plainStringList[index]);
                            //MiscUtils.Warn("- Intended offset: " + num1);
                            MiscUtils.Warn(string.Format("- Position: 0x{0:X6}", fileStrings[index].offset));
                            MiscUtils.Warn("- Pointer type: " + fileStrings[index].typeOffset);
                            MiscUtils.Warn("Continuing insertion, but leaving pointer unchanged, expect text errors!");
                            Console.WriteLine();
                            //Console.ReadKey();
                            //throw new InvalidOperationException();
                        }

                        if (fileStrings[index].typeOffset == sceStrings.OffsetType.MediumOffset)
                        {
                            failedMediumStrings.Add(index);
                            lastMediumLength = num1;
                        }
                        else
                        {
                            failedShortStrings.Add(index);
                            lastShortLength = num1;
                        }
                    }

                    if (matched)
                    {
                        sfa.PositionStream = realPos;
                        matched            = false;
                        continue;
                    }
                    sfa.PositionStream = sfa.LengthStream;
                }
            }

            if (failedShortStrings.Count != 0 || failedMediumStrings.Count != 0)
            {
                MiscUtils.Warn("Couldn't insert all lines due to pointer size issues");
                MiscUtils.Warn("Inserted the other lines, but left the faulty ones untouched, expect text errors!");
                Console.WriteLine();

                if (failedShortStrings.Count != 0)
                {
                    MiscUtils.Info("Found " + failedShortStrings.Count + " strings over the 16 line");
                    MiscUtils.Info("Remove " + (lastShortLength - 16) + " bytes before line "
                                   + lineNumberList[failedShortStrings[0]] + " to get them back to a valid position");
                    MiscUtils.Info("Line " + lineNumberList[failedShortStrings[0]] + " for reference:");
                    MiscUtils.Info("\"" + plainStringList[failedShortStrings[0]] + "\"");
                    Console.WriteLine();
                }

                if (failedMediumStrings.Count != 0)
                {
                    MiscUtils.Info("Found " + failedMediumStrings.Count + " strings over the 4096 line");
                    MiscUtils.Info("Remove " + (lastMediumLength - 4096) + " bytes before line "
                                   + lineNumberList[failedMediumStrings[0]] + " to get them back to a valid position");
                    MiscUtils.Info("Line " + lineNumberList[failedMediumStrings[0]] + " for reference:");
                    MiscUtils.Info("\"" + plainStringList[failedMediumStrings[0]] + "\"");
                    Console.WriteLine();
                }
            }

            sfa.SeekValueWrite(2U);
        }
Example #9
0
        public sceHeader(StreamFunctionAdd sfa)
        {
            if (sfa.ReadAnsiStringSize(8) != "TOD1RSCE")
            {
                throw new Exception("Error #1: Bad Magic");
            }
            offsetScript       = sfa.ReadUInt32();
            offsetStrings      = sfa.ReadUInt32();
            fileStrings        = new List <sceStrings>();
            sizes              = new List <int>();
            sfa.PositionStream = offsetScript;
            while (sfa.PositionStream < offsetStrings)
            {
                //The file is composed by some bytecode above and strings below
                //then the game gets the offset to a string whith the opcode 0x47
                //so we loop until then, it can get some false positives though
                if (sfa.ReadByte() == 0x47)
                {
                    byte num = sfa.ReadByte();
                    if (num >> 4 == 1)
                    {
                        if (sfa.ReadByte() == 0x0)
                        {
                            fileStrings.Add(new sceStrings((uint)sfa.PositionStream - 1, offsetStrings)
                            {
                                offset     = ((uint)num & 0xF) + offsetStrings,
                                typeOffset = sceStrings.OffsetType.ShortOffset
                            });
                        }
                        else
                        {
                            sfa.PositionStream--;
                        }
                    }
                    if (num >> 4 == 5)
                    {
                        sceStrings sceStrings = new sceStrings((uint)sfa.PositionStream, offsetStrings);
                        sceStrings.offset     = (uint)((num & 0xF) << 8) + sfa.ReadByte() + offsetStrings;
                        sceStrings.typeOffset = sceStrings.OffsetType.MediumOffset;
                        if (sfa.ReadByte() == 0)
                        {
                            fileStrings.Add(sceStrings);
                        }
                    }
                    if (num == 0x90)
                    {
                        sceStrings sceStrings = new sceStrings((uint)sfa.PositionStream, offsetStrings);
                        sceStrings.offset     = sfa.ReadUInt16() + offsetStrings;
                        sceStrings.typeOffset = sceStrings.OffsetType.LargeOffset;
                        if (sfa.ReadByte() == 0)
                        {
                            fileStrings.Add(sceStrings);
                        }
                    }
                }
            }

            //order the offsets by string position
            fileStrings.Sort((s1, s2) => s1.offset.CompareTo(s2.offset));

            //Get sizes
            for (int index = 0; index < fileStrings.Count; ++index)
            {
                if (index < fileStrings.Count - 1)
                {
                    sizes.Add((int)fileStrings[index + 1].offset - (int)fileStrings[index].offset);
                }
                else
                {
                    sizes.Add((int)(uint)sfa.LengthStream - (int)fileStrings[index].offset);
                }
            }
            for (int index = 0; index < fileStrings.Count; ++index)
            {
                fileStrings[index].ReadData(sfa, sizes[index]);
            }
        }