Ejemplo n.º 1
0
        public void Unserialize(UndertaleReader reader)
        {
            uint instructionStartAddress = reader.Position;

            reader.ReadByte(); reader.ReadByte(); reader.ReadByte(); // skip for now, we'll read them later
            Kind            = (Opcode)reader.ReadByte();
            reader.Position = instructionStartAddress;
            switch (GetInstructionType(Kind))
            {
            case InstructionType.SingleTypeInstruction:
            case InstructionType.DoubleTypeInstruction:
            case InstructionType.ComparisonInstruction:
            {
                DupExtra = reader.ReadByte();
                if (DupExtra != 0 && Kind != Opcode.Dup)
                {
                    throw new IOException("Invalid padding in " + Kind.ToString().ToUpper());
                }
                ComparisonKind = (ComparisonType)reader.ReadByte();
                if ((Kind == Opcode.Cmp) != ((byte)ComparisonKind != 0))
                {
                    throw new IOException("Got unexpected comparison type in " + Kind.ToString().ToUpper() + " (should be only in CMP)");
                }
                byte TypePair = reader.ReadByte();
                Type1 = (DataType)(TypePair & 0xf);
                Type2 = (DataType)(TypePair >> 4);
                if (GetInstructionType(Kind) == InstructionType.SingleTypeInstruction && Type2 != (byte)0)
                {
                    throw new IOException("Second type should be 0 in " + Kind.ToString().ToUpper());
                }
                if (reader.ReadByte() != (byte)Kind)
                {
                    throw new Exception("really shouldn't happen");
                }
            }
            break;

            case InstructionType.GotoInstruction:
            {
                uint v = reader.ReadUInt24();

                // TODO: This is SO WRONG that I don't even believe it. Is that Int24 or Int23?!?!
                uint r = v & 0x003FFFFF;

                if ((v & 0x00C00000) != 0)
                {
                    r |= 0xFFC00000;
                }

                JumpOffset        = (int)r;
                JumpOffsetIsWeird = (v & 0x00800000) != 0;

                if (reader.ReadByte() != (byte)Kind)
                {
                    throw new Exception("really shouldn't happen");
                }
            }
            break;

            case InstructionType.PopInstruction:
            {
                TypeInst = (InstanceType)reader.ReadInt16();
                byte TypePair = reader.ReadByte();
                Type1 = (DataType)(TypePair & 0xf);
                Type2 = (DataType)(TypePair >> 4);
                if (reader.ReadByte() != (byte)Kind)
                {
                    throw new Exception("really shouldn't happen");
                }
                Destination = reader.ReadUndertaleObject <Reference <UndertaleVariable> >();
            }
            break;

            case InstructionType.PushInstruction:
            {
                short val = reader.ReadInt16();
                Type1 = (DataType)reader.ReadByte();
                if (reader.ReadByte() != (byte)Kind)
                {
                    throw new Exception("really shouldn't happen");
                }
                switch (Type1)
                {
                case DataType.Double:
                    Value = reader.ReadDouble();
                    break;

                case DataType.Float:
                    Value = reader.ReadSingle();
                    break;

                case DataType.Int32:
                    Value = reader.ReadInt32();
                    break;

                case DataType.Int64:
                    Value = reader.ReadInt64();
                    break;

                case DataType.Boolean:
                    Value = (reader.ReadUInt32() == 1);             // TODO: double check
                    break;

                case DataType.Variable:
                    TypeInst = (InstanceType)val;
                    Value    = reader.ReadUndertaleObject <Reference <UndertaleVariable> >();
                    break;

                case DataType.String:
                    UndertaleResourceById <UndertaleString> str = new UndertaleResourceById <UndertaleString>("STRG");
                    str.Unserialize(reader, reader.ReadInt32());
                    Value = str;
                    break;

                case DataType.Int16:
                    Value = val;
                    break;
                }
            }
            break;

            case InstructionType.CallInstruction:
            {
                ArgumentsCount = reader.ReadUInt16();
                Type1          = (DataType)reader.ReadByte();
                if (reader.ReadByte() != (byte)Kind)
                {
                    throw new Exception("really shouldn't happen");
                }
                Function = reader.ReadUndertaleObject <Reference <UndertaleFunction> >();
            }
            break;

            case InstructionType.BreakInstruction:
            {
                Value = reader.ReadInt16();
                Type1 = (DataType)reader.ReadByte();
                if (reader.ReadByte() != (byte)Kind)
                {
                    throw new Exception("really shouldn't happen");
                }
            }
            break;

            default:
                throw new IOException("Unknown opcode " + Kind.ToString().ToUpper());
            }
        }
Ejemplo n.º 2
0
 public void Unserialize(UndertaleReader reader)
 {
     DisableDebugger = reader.ReadByte() != 0 ? true : false;
     BytecodeVersion = reader.ReadByte();
     Unknown         = reader.ReadUInt16();
     Filename        = reader.ReadUndertaleString();
     Config          = reader.ReadUndertaleString();
     LastObj         = reader.ReadUInt32();
     LastTile        = reader.ReadUInt32();
     GameID          = reader.ReadUInt32();
     byte[] GuidData = reader.ReadBytes(16);
     DirectPlayGuid          = new Guid(GuidData);
     Name                    = reader.ReadUndertaleString();
     Major                   = reader.ReadUInt32();
     Minor                   = reader.ReadUInt32();
     Release                 = reader.ReadUInt32();
     Build                   = reader.ReadUInt32();
     DefaultWindowWidth      = reader.ReadUInt32();
     DefaultWindowHeight     = reader.ReadUInt32();
     Info                    = (InfoFlags)reader.ReadUInt32();
     LicenseMD5              = reader.ReadBytes(16);
     LicenseCRC32            = reader.ReadUInt32();
     Timestamp               = reader.ReadUInt64();
     DisplayName             = reader.ReadUndertaleString();
     ActiveTargets           = reader.ReadUInt64();
     FunctionClassifications = (FunctionClassification)reader.ReadUInt64();
     SteamAppID              = reader.ReadInt32();
     DebuggerPort            = reader.ReadUInt32();
     RoomOrder               = reader.ReadUndertaleObject <UndertaleSimpleResourcesList <UndertaleRoom, UndertaleChunkROOM> >();
     if (Major >= 2)
     {
         // Begin parsing random UID, and verify it based on original algorithm
         GMS2RandomUID = new List <long>();
         Random random      = new Random((int)(Timestamp & 4294967295L));
         long   firstRandom = (long)random.Next() << 32 | (long)random.Next();
         if (reader.ReadInt64() != firstRandom)
         {
             //throw new IOException("Unexpected random UID");
         }
         long  infoNumber = (long)(Timestamp - 1000);
         ulong temp       = (ulong)infoNumber;
         temp = ((temp << 56 & 18374686479671623680UL) | (temp >> 8 & 71776119061217280UL) |
                 (temp << 32 & 280375465082880UL) | (temp >> 16 & 1095216660480UL) | (temp << 8 & 4278190080UL) |
                 (temp >> 24 & 16711680UL) | (temp >> 16 & 65280UL) | (temp >> 32 & 255UL));
         infoNumber  = (long)temp;
         infoNumber ^= firstRandom;
         infoNumber  = ~infoNumber;
         infoNumber ^= ((long)GameID << 32 | (long)GameID);
         infoNumber ^= ((long)(DefaultWindowWidth + (int)Info) << 48 |
                        (long)(DefaultWindowHeight + (int)Info) << 32 |
                        (long)(DefaultWindowHeight + (int)Info) << 16 |
                        (long)(DefaultWindowWidth + (int)Info));
         infoNumber ^= BytecodeVersion;
         int infoLocation = (int)(Math.Abs((int)(Timestamp & 65535L) / 7 + (GameID - DefaultWindowWidth) + RoomOrder.Count) % 4);
         for (int i = 0; i < 4; i++)
         {
             if (i == infoLocation)
             {
                 long curr = reader.ReadInt64();
                 curr = infoNumber;
                 GMS2RandomUID.Add(curr);
             }
             else
             {
                 int first  = reader.ReadInt32();
                 int second = reader.ReadInt32();
                 first  = random.Next();
                 second = random.Next();
                 GMS2RandomUID.Add(((long)first << 32) | (long)second);
             }
         }
         GMS2FPS             = reader.ReadSingle();
         GMS2AllowStatistics = reader.ReadBoolean();
         GMS2GameGUID        = reader.ReadBytes(16);
     }
     reader.undertaleData.UnsupportedBytecodeVersion = BytecodeVersion < 14 || BytecodeVersion > 17;
     reader.Bytecode14OrLower = BytecodeVersion <= 14;
 }
        public void Unserialize(UndertaleReader reader)
        {
            uint instructionStartAddress = reader.Position;

            reader.ReadByte(); reader.ReadByte(); reader.ReadByte(); // skip for now, we'll read them later
            Kind            = (Opcode)reader.ReadByte();
            reader.Position = instructionStartAddress;
            switch (GetInstructionType(Kind))
            {
            case InstructionType.SingleTypeInstruction:
            case InstructionType.DoubleTypeInstruction:
            case InstructionType.ComparisonInstruction:
            {
                DupExtra = reader.ReadByte();
                if (DupExtra != 0 && Kind != Opcode.Dup)
                {
                    throw new IOException("Invalid padding in " + Kind.ToString().ToUpper());
                }
                ComparisonKind = (ComparisonType)reader.ReadByte();
                if ((Kind == Opcode.Cmp) != ((byte)ComparisonKind != 0))
                {
                    throw new IOException("Got unexpected comparison type in " + Kind.ToString().ToUpper() + " (should be only in CMP)");
                }
                byte TypePair = reader.ReadByte();
                Type1 = (DataType)(TypePair & 0xf);
                Type2 = (DataType)(TypePair >> 4);
                if (GetInstructionType(Kind) == InstructionType.SingleTypeInstruction && Type2 != (byte)0)
                {
                    throw new IOException("Second type should be 0 in " + Kind.ToString().ToUpper());
                }
                if (reader.ReadByte() != (byte)Kind)
                {
                    throw new Exception("really shouldn't happen");
                }
            }
            break;

            case InstructionType.GotoInstruction:
            {
                uint v = reader.ReadUInt24();

                JumpOffsetPopenvExitMagic = (v & 0x800000) != 0;

                // The rest is int23 signed value, so make sure
                uint r = v & 0x003FFFFF;
                if (JumpOffsetPopenvExitMagic && v != 0xF00000)
                {
                    throw new Exception("Popenv magic doesn't work, call issue #90 again");
                }
                else
                {
                    if ((v & 0x00C00000) != 0)
                    {
                        r |= 0xFFC00000;
                    }
                    JumpOffset = (int)r;
                }

                if (reader.ReadByte() != (byte)Kind)
                {
                    throw new Exception("really shouldn't happen");
                }
            }
            break;

            case InstructionType.PopInstruction:
            {
                TypeInst = (InstanceType)reader.ReadInt16();
                byte TypePair = reader.ReadByte();
                Type1 = (DataType)(TypePair & 0xf);
                Type2 = (DataType)(TypePair >> 4);
                if (reader.ReadByte() != (byte)Kind)
                {
                    throw new Exception("really shouldn't happen");
                }
                if (Type1 == DataType.Int16)
                {
                    // Special scenario - the swap instruction
                    // TODO: Figure out the proper syntax, see #129
                    SwapExtra = (ushort)TypeInst;
                    TypeInst  = 0;
                }
                else
                {
                    Destination = reader.ReadUndertaleObject <Reference <UndertaleVariable> >();
                }
            }
            break;

            case InstructionType.PushInstruction:
            {
                short val = reader.ReadInt16();
                Type1 = (DataType)reader.ReadByte();
                if (reader.ReadByte() != (byte)Kind)
                {
                    throw new Exception("really shouldn't happen");
                }
                switch (Type1)
                {
                case DataType.Double:
                    Value = reader.ReadDouble();
                    break;

                case DataType.Float:
                    Value = reader.ReadSingle();
                    break;

                case DataType.Int32:
                    Value = reader.ReadInt32();
                    break;

                case DataType.Int64:
                    Value = reader.ReadInt64();
                    break;

                case DataType.Boolean:
                    Value = (reader.ReadUInt32() == 1);             // TODO: double check
                    break;

                case DataType.Variable:
                    TypeInst = (InstanceType)val;
                    Value    = reader.ReadUndertaleObject <Reference <UndertaleVariable> >();
                    break;

                case DataType.String:
                    Value = reader.ReadUndertaleObject <UndertaleResourceById <UndertaleString, UndertaleChunkSTRG> >();
                    break;

                case DataType.Int16:
                    Value = val;
                    break;
                }
            }
            break;

            case InstructionType.CallInstruction:
            {
                ArgumentsCount = reader.ReadUInt16();
                Type1          = (DataType)reader.ReadByte();
                if (reader.ReadByte() != (byte)Kind)
                {
                    throw new Exception("really shouldn't happen");
                }
                Function = reader.ReadUndertaleObject <Reference <UndertaleFunction> >();
            }
            break;

            case InstructionType.BreakInstruction:
            {
                Value = reader.ReadInt16();
                Type1 = (DataType)reader.ReadByte();
                if (reader.ReadByte() != (byte)Kind)
                {
                    throw new Exception("really shouldn't happen");
                }
            }
            break;

            default:
                throw new IOException("Unknown opcode " + Kind.ToString().ToUpper());
            }
        }