Exemplo n.º 1
0
            private Token DeserializeNext( byte tokenCode = Byte.MaxValue )
            {
                var tokenPosition = CodePosition;
                if( tokenCode == Byte.MaxValue )
                {
                    tokenCode = FixToken( Buffer.ReadByte() );
                    AlignSize( sizeof(byte) );
                }

                Token tokenItem = null;
                if( tokenCode >= (byte)ExprToken.FirstNative )
                {
                    tokenItem = FindNativeTable( tokenCode );
                }
                else if( tokenCode >= (byte)ExprToken.ExtendedNative )
                {
                    tokenItem = FindNativeTable( (tokenCode - (byte)ExprToken.ExtendedNative) << 8 | Buffer.ReadByte() );
                    AlignSize( sizeof(byte) );
                }
                else switch( tokenCode )
                {
                    #region Cast
                    case (byte)ExprToken.DynamicCast:
                        tokenItem = new DynamicCastToken();
                        break;

                    case (byte)ExprToken.MetaCast:
                        tokenItem = new MetaCastToken();
                        break;

                    case (byte)ExprToken.InterfaceCast:
                        if( Buffer.Version < PrimitveCastVersion )      // UE1
                        {
                            tokenItem = new IntToStringToken();
                        }
                        else
                        {
                            tokenItem = new InterfaceCastToken();
                        }
                        break;

                    // Redefined, can be RotatorToVector!(UE1)
                    case (byte)ExprToken.PrimitiveCast:
                        if( Buffer.Version < PrimitveCastVersion )      // UE1
                        {
                            tokenItem = new RotatorToVectorToken();
                        }
                        else                                            // UE2+
                        {
                            // Next byte represents the CastToken!
                            tokenCode = Buffer.ReadByte();
                            AlignSize( sizeof(byte) );

                            tokenItem = DeserializeCastToken( tokenCode );
                            //tokenitem = new PrimitiveCastToken();
                        }
                        break;
                    #endregion

                    #region Context
                    case (byte)ExprToken.ClassContext:
                        tokenItem = new ClassContextToken();
                        break;

                    case (byte)ExprToken.InterfaceContext:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new ByteToStringToken();
                        }
                        else
                        {
                            tokenItem = new InterfaceContextToken();
                        }
                        break;

                    case (byte)ExprToken.Context:
                        tokenItem = new ContextToken();
                        break;

                    case (byte)ExprToken.StructMember:
                        tokenItem = new StructMemberToken();
                        break;
                    #endregion

                    #region Assigns
                    case (byte)ExprToken.Let:
                        tokenItem = new LetToken();
                        break;

                    case (byte)ExprToken.LetBool:
                        tokenItem = new LetBoolToken();
                        break;

                    case (byte)ExprToken.EndParmValue:
                        tokenItem = new EndParmValueToken();
                        break;

                    // Redefined, can be FloatToBool!(UE1)
                    case (byte)ExprToken.LetDelegate:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new FloatToBoolToken();
                        }
                        else
                        {
                            tokenItem = new LetDelegateToken();
                        }
                        break;

                    // Redefined, can be NameToBool!(UE1)
                    case (byte)ExprToken.Conditional:
                        tokenItem = new ConditionalToken();
                        break;

                    case (byte)ExprToken.Eval: // case (byte)ExprToken.DynArrayFindStruct: case (byte)ExprToken.Conditional:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new NameToBoolToken();
                        }
                        else if( Buffer.Version >= 300 )
                        {
                            tokenItem = new DynamicArrayFindStructToken();
                        }
                        else
                        {
                            tokenItem = new ConditionalToken();
                        }
                        break;
                    #endregion

                    #region Jumps
                    case (byte)ExprToken.Return:
                        tokenItem = new ReturnToken();
                        break;

                    case (byte)ExprToken.ReturnNothing:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new ByteToIntToken();
                        }
                            // Definitely existed since GoW(490)
                        else if( Buffer.Version > 420 && (DeserializedTokens.Count > 0 && !(DeserializedTokens[DeserializedTokens.Count - 1] is ReturnToken)) ) // Should only be done if the last token wasn't Return
                        {
                            tokenItem = new DynamicArrayInsertToken();
                        }
                        else
                        {
                            tokenItem = new ReturnNothingToken();
                        }
                        break;

                    case (byte)ExprToken.GotoLabel:
                        tokenItem = new GoToLabelToken();
                        break;

                    case (byte)ExprToken.Jump:
                        tokenItem = new JumpToken();
                        break;

                    case (byte)ExprToken.JumpIfNot:
                        tokenItem = new JumpIfNotToken();
                        break;

                    case (byte)ExprToken.Switch:
                        tokenItem = new SwitchToken();
                        break;

                    case (byte)ExprToken.Case:
                        tokenItem = new CaseToken();
                        break;

                    case (byte)ExprToken.DynArrayIterator:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new RotatorToStringToken();
                        }
                        else
                        {
                            tokenItem = new ArrayIteratorToken();
                        }
                        break;

                    case (byte)ExprToken.Iterator:
                        tokenItem = new IteratorToken();
                        break;

                    case (byte)ExprToken.IteratorNext:
                        tokenItem = new IteratorNextToken();
                        break;

                    case (byte)ExprToken.IteratorPop:
                        tokenItem = new IteratorPopToken();
                        break;

                    case (byte)ExprToken.FilterEditorOnly:
                        tokenItem = new FilterEditorOnlyToken();
                        break;

                    #endregion

                    #region Variables
                    case (byte)ExprToken.NativeParm:
                        tokenItem = new NativeParameterToken();
                        break;

                    // Referenced variables that are from this function e.g. Local and params
                    case (byte)ExprToken.InstanceVariable:
                        tokenItem = new InstanceVariableToken();
                        break;

                    case (byte)ExprToken.LocalVariable:
                        tokenItem = new LocalVariableToken();
                        break;

                    case (byte)ExprToken.StateVariable:
                        tokenItem = new StateVariableToken();
                        break;

                    // Referenced variables that are default
                    case (byte)ExprToken.UndefinedVariable:
                        #if BORDERLANDS2
                            if( _Container.Package.Build == UnrealPackage.GameBuild.BuildName.Borderlands2 )
                            {
                                tokenItem = new DynamicVariableToken();
                                break;
                            }
                        #endif
                        tokenItem = new UndefinedVariableToken();
                        break;

                    case (byte)ExprToken.DefaultVariable:
                        tokenItem = new DefaultVariableToken();
                        break;

                    // UE3+
                    case (byte)ExprToken.OutVariable:
                        tokenItem = new OutVariableToken();
                        break;

                    case (byte)ExprToken.BoolVariable:
                        tokenItem = new BoolVariableToken();
                        break;

                    // Redefined, can be FloatToInt!(UE1)
                    case (byte)ExprToken.DelegateProperty:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new FloatToIntToken();
                        }
                        else
                        {
                            tokenItem = new DelegatePropertyToken();
                        }
                        break;

                    case (byte)ExprToken.DefaultParmValue:
                        if( Buffer.Version < PrimitveCastVersion )   // StringToInt
                        {
                            tokenItem = new StringToIntToken();
                        }
                        else
                        {
                            tokenItem = new DefaultParameterToken();
                        }
                        break;
                    #endregion

                    #region Misc
                    // Redefined, can be BoolToFloat!(UE1)
                    case (byte)ExprToken.DebugInfo:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new BoolToFloatToken();
                        }
                        else
                        {
                            tokenItem = new DebugInfoToken();
                        }
                        break;

                    case (byte)ExprToken.Nothing:
                        tokenItem = new NothingToken();
                        break;

                    case (byte)ExprToken.EndFunctionParms:
                        tokenItem = new EndFunctionParmsToken();
                        break;

                    case (byte)ExprToken.IntZero:
                        tokenItem = new IntZeroToken();
                        break;

                    case (byte)ExprToken.IntOne:
                        tokenItem = new IntOneToken();
                        break;

                    case (byte)ExprToken.True:
                        tokenItem = new TrueToken();
                        break;

                    case (byte)ExprToken.False:
                        tokenItem = new FalseToken();
                        break;

                    case (byte)ExprToken.NoDelegate:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new IntToFloatToken();
                        }
                        else
                        {
                            tokenItem = new NoDelegateToken();
                        }
                        break;

                        // No value passed to an optional parameter.
                    case (byte)ExprToken.NoParm:
                        tokenItem = new NoParmToken();
                        break;

                    case (byte)ExprToken.NoObject:
                        tokenItem = new NoObjectToken();
                        break;

                    case (byte)ExprToken.Self:
                        tokenItem = new SelfToken();
                        break;

                    // End of state code.
                    case (byte)ExprToken.Stop:
                        tokenItem = new StopToken();
                        break;

                    case (byte)ExprToken.Assert:
                        tokenItem = new AssertToken();
                        break;

                    case (byte)ExprToken.LabelTable:
                        tokenItem = new LabelTableToken();
                        break;

                    case (byte)ExprToken.EndOfScript:   //CastToken.BoolToString:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new BoolToStringToken();
                        }
                        else
                        {
                            tokenItem = new EndOfScriptToken();
                        }
                        break;

                    case (byte)ExprToken.Skip:
                        tokenItem = new SkipToken();
                        break;

                    case (byte)ExprToken.StructCmpEq:
                        tokenItem = new StructCmpEqToken();
                        break;

                    case (byte)ExprToken.StructCmpNE:
                        tokenItem = new StructCmpNeToken();
                        break;

                    case (byte)ExprToken.DelegateCmpEq:
                        tokenItem = new DelegateCmpEqToken();
                        break;

                    case (byte)ExprToken.DelegateFunctionCmpEq:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new IntToBoolToken();
                        }
                        else
                        {
                            tokenItem = new DelegateFunctionCmpEqToken();
                        }
                        break;

                    case (byte)ExprToken.DelegateCmpNE:
                        tokenItem = new DelegateCmpNEToken();
                        break;

                    case (byte)ExprToken.DelegateFunctionCmpNE:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new IntToBoolToken();
                        }
                        else
                        {
                            tokenItem = new DelegateFunctionCmpNEToken();
                        }
                        break;

                    case (byte)ExprToken.InstanceDelegate:
                        tokenItem = new InstanceDelegateToken();
                        break;

                    case (byte)ExprToken.EatString:
                        tokenItem = new EatStringToken();
                        break;

                    case (byte)ExprToken.New:
                        tokenItem = new NewToken();
                        break;

                    case (byte)ExprToken.FunctionEnd: // case (byte)ExprToken.DynArrayFind:
                        if( Buffer.Version < 300 )
                        {
                            tokenItem = new EndOfScriptToken();
                        }
                        else
                        {
                            tokenItem = new DynamicArrayFindToken();
                        }
                        break;

                    case (byte)ExprToken.VarInt:
                    case (byte)ExprToken.VarFloat:
                    case (byte)ExprToken.VarByte:
                    case (byte)ExprToken.VarBool:
                    //case (byte)ExprToken.VarObject:   // See UndefinedVariable
                        if (_Container.Package.Build == UnrealPackage.GameBuild.BuildName.Mass_Effect)
                        {
                            // Don't know what this op code is for.... just know it takes two bytes.
                            tokenItem = new MassEffectUnknownToken();
                        }
                        else
                        {
                            tokenItem = new DynamicVariableToken();
                        }
                        break;
                    #endregion

                    #region Constants
                    case (byte)ExprToken.IntConst:
                        tokenItem = new IntConstToken();
                        break;

                    case (byte)ExprToken.ByteConst:
                        tokenItem = new ByteConstToken();
                        break;

                    case (byte)ExprToken.IntConstByte:
                        tokenItem = new IntConstByteToken();
                        break;

                    case (byte)ExprToken.FloatConst:
                        tokenItem = new FloatConstToken();
                        break;

                    // ClassConst?
                    case (byte)ExprToken.ObjectConst:
                        tokenItem = new ObjectConstToken();
                        break;

                    case (byte)ExprToken.NameConst:
                        tokenItem = new NameConstToken();
                        break;

                    case (byte)ExprToken.StringConst:
                        tokenItem = new StringConstToken();
                        break;

                    case (byte)ExprToken.UniStringConst:
                        tokenItem = new UniStringConstToken();
                        break;

                    case (byte)ExprToken.RotatorConst:
                        tokenItem = new RotatorConstToken();
                        break;

                    case (byte)ExprToken.VectorConst:
                        tokenItem = new VectorConstToken();
                        break;
                    #endregion

                    #region Functions
                    case (byte)ExprToken.FinalFunction:
                        tokenItem = new FinalFunctionToken();
                        break;

                    case (byte)ExprToken.VirtualFunction:
                        tokenItem = new VirtualFunctionToken();
                        break;

                    case (byte)ExprToken.GlobalFunction:
                        tokenItem = new GlobalFunctionToken();
                        break;

                    // Redefined, can be FloatToByte!(UE1)
                    case (byte)ExprToken.DelegateFunction:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new FloatToByteToken();
                        }
                        else
                        {
                            tokenItem = new DelegateFunctionToken();
                        }
                        break;
                    #endregion

                    #region Arrays
                    case (byte)ExprToken.ArrayElement:
                        tokenItem = new ArrayElementToken();
                        break;

                    case (byte)ExprToken.DynArrayElement:
                        tokenItem = new DynamicArrayElementToken();
                        break;

                    case (byte)ExprToken.DynArrayLength:
                        tokenItem = new DynamicArrayLengthToken();
                        break;

                    case (byte)ExprToken.DynArrayInsert:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new BoolToByteToken();
                        }
                        else
                        {
                            tokenItem = new DynamicArrayInsertToken();
                        }
                        break;

                    case (byte)ExprToken.DynArrayInsertItem:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new VectorToStringToken();
                        }
                        else
                        {
                            tokenItem = new DynamicArrayInsertItemToken();
                        }
                        break;

                    // Redefined, can be BoolToInt!(UE1)
                    case (byte)ExprToken.DynArrayRemove:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new BoolToIntToken();
                        }
                        else
                        {
                            tokenItem = new DynamicArrayRemoveToken();
                        }
                        break;

                    case (byte)ExprToken.DynArrayRemoveItem:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new NameToStringToken();
                        }
                        else
                        {
                            tokenItem = new DynamicArrayRemoveItemToken();
                        }
                        break;

                    case (byte)ExprToken.DynArrayAdd:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new FloatToStringToken();
                        }
                        else
                        {
                            tokenItem = new DynamicArrayAddToken();
                        }
                        break;

                    case (byte)ExprToken.DynArrayAddItem:
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            tokenItem = new ObjectToStringToken();
                        }
                        else
                        {
                            tokenItem = new DynamicArrayAddItemToken();
                        }
                        break;

                    case (byte)ExprToken.DynArraySort:
                        tokenItem = new DynamicArraySortToken();
                        break;

                    // See FunctionEnd and Eval
                    /*case (byte)ExprToken.DynArrayFind:
                        break;

                    case (byte)ExprToken.DynArrayFindStruct:
                        break;*/

                    #endregion

                    default:
                    {
                        #region Casts
                        if( Buffer.Version < PrimitveCastVersion )
                        {
                            // No other token was matched. Check if it matches any of the CastTokens
                            // We don't just use PrimitiveCast detection due compatible with UE1 games
                            tokenItem = DeserializeCastToken( tokenCode );
                        }
                        break;
                        #endregion
                    }
                }

                if( tokenItem == null )
                {
                    tokenItem = new UnknownExprToken();
                }

                Console.Out.WriteLine("Token is: "+ tokenCode.ToString("X") + " type: " + tokenItem.GetType().Name + " pos: " + Buffer.Position);

                tokenItem.Decompiler = this;
                tokenItem.RepresentToken = tokenCode;
                tokenItem.Position = tokenPosition;// + (uint)Owner._ScriptOffset;
                tokenItem.StoragePosition = (uint)Buffer.Position - (uint)_Container.ScriptOffset - 1;
                // IMPORTANT:Add before deserialize, due the possibility that the tokenitem might deserialize other tokens as well.
                DeserializedTokens.Add( tokenItem );
                tokenItem.Deserialize( Buffer );
                // Includes all sizes of followed tokens as well! e.g. i = i + 1; is summed here but not i = i +1; (not>>)i ++;
                tokenItem.Size = (ushort)(CodePosition - tokenPosition);
                tokenItem.StorageSize = (ushort)((uint)Buffer.Position - (uint)_Container.ScriptOffset - tokenItem.StoragePosition);
                tokenItem.PostDeserialized();
                return tokenItem;
            }
Exemplo n.º 2
0
 public virtual T applyToTrueToken(TrueToken operand)
 {
     return(applyToParseTreeNode(operand));
 }
Exemplo n.º 3
0
 public override AbstractSyntaxTreeNode applyToTrueToken(TrueToken operand)
 {
     return(Context.newReducedNode(trueConstant));
 }
Exemplo n.º 4
0
 public Literal(TrueToken token) : base(token)
 {
 }