public VariableDeclaration(VariableType type, UnrealFlags.EPropertyFlags flags,
                            string name, int arrayLength = 0, string category = null, SourcePosition start = null, SourcePosition end = null)
     : base(ASTNodeType.VariableDeclaration, start, end)
 {
     Flags       = flags;
     Name        = name;
     ArrayLength = arrayLength;
     Category    = category;
     VarType     = IsStaticArray && !(type is StaticArrayType) ? new StaticArrayType(type, ArrayLength) : type;
 }
 public static void Serialize(this SerializingContainer2 sc, ref UnrealFlags.EPropertyFlags flags)
 {
     if (sc.IsLoading)
     {
         flags = (UnrealFlags.EPropertyFlags)sc.ms.ReadUInt64();
     }
     else
     {
         sc.ms.Writer.WriteUInt64((ulong)flags);
     }
 }
Exemple #3
0
        public void Decompile(TextBuilder result, bool createControlStatements)
        {
            result.Indent();
            if (Native)
            {
                result.Append("native");
                if (_nativeIndex > 0)
                {
                    result.Append("(").Append(_nativeIndex).Append(")");
                }
                result.Append(" ");
            }

            _flags.Except("Native", "Event", "Delegate", "Defined", "Public", "HasDefaults", "HasOutParms").Each(f => result.Append(f.ToLower() + " "));

            if (HasFlag("Event"))
            {
                result.Append("event ");
            }
            else if (HasFlag("Delegate"))
            {
                result.Append("delegate ");
            }
            else
            {
                result.Append("function ");
            }
            string type = GetReturnType();

            if (type != null)
            {
                result.Append(type).Append(" ");
            }
            result.Append(_self.ObjectName.Instanced).Append("(");
            int paramCount = 0;
            var locals     = new List <ExportEntry>();

            Tokens     = new List <BytecodeSingularToken>();
            Statements = ReadBytecode(out var bytecodeReader);

            // UI only
            // Man this code is bad
            // sorry
#if !DEBUG && !AZURE
            try
            {
#endif
            var s = bytecodeReader._reader;
            if (Export.ClassName == "State")
            {
                s.BaseStream.Skip(0x10); //Unknown
                var stateFlags       = s.ReadInt32();
                var unknown          = s.ReadInt16();
                var functionMapCount = s.ReadInt32();
                for (int i = 0; i < functionMapCount; i++)
                {
                    int spos  = (int)s.BaseStream.Position;
                    var name  = bytecodeReader.ReadName();
                    var entry = bytecodeReader.ReadEntryRef(out var _);
                    Statements.statements.Add(new Statement(spos, (int)s.BaseStream.Position, new NothingToken(spos, $"  {name} => {entry.FullPath}()"), bytecodeReader));
                }
            }
#if !DEBUG && !AZURE
        }

        catch (Exception e)
        {
            Debug.WriteLine("Exception..");
        }
#endif
            NameReferences  = bytecodeReader.NameReferences;
            EntryReferences = bytecodeReader.EntryReferences;
            //var childIdx = EndianReader.ToInt32(Export.DataReadOnly, 0x18, Export.FileRef.Endian);
            var childIdx = EndianReader.ToInt32(Export.Data, 0x18, Export.FileRef.Endian);
            var children = new List <ExportEntry>();
            while (Export.FileRef.TryGetUExport(childIdx, out var parsingExp))
            {
                children.Add(parsingExp);
                //childIdx = EndianReader.ToInt32(parsingExp.DataReadOnly, 0x10, Export.FileRef.Endian);
                childIdx = EndianReader.ToInt32(parsingExp.Data, 0x10, Export.FileRef.Endian);
            }

            //Get local children of this function
            foreach (ExportEntry export in children)
            {
                //Reading parameters info...
                if (export.ClassName.EndsWith("Property"))
                {
                    UnrealFlags.EPropertyFlags ObjectFlagsMask = (UnrealFlags.EPropertyFlags)EndianReader.ToUInt64(export.Data, 0x18, export.FileRef.Endian);
                    //UnrealFlags.EPropertyFlags ObjectFlagsMask = (UnrealFlags.EPropertyFlags)EndianReader.ToUInt64(export.DataReadOnly, 0x18, export.FileRef.Endian);
                    if (ObjectFlagsMask.HasFlag(UnrealFlags.EPropertyFlags.Parm) && !ObjectFlagsMask.HasFlag(UnrealFlags.EPropertyFlags.ReturnParm))
                    {
                        if (paramCount > 0)
                        {
                            result.Append(", ");
                        }

                        if (export.ClassName == "ObjectProperty" || export.ClassName == "StructProperty")
                        {
                            //var uindexOfOuter = EndianReader.ToInt32(export.DataReadOnly, export.DataSize - 4, export.FileRef.Endian);
                            var    uindexOfOuter = EndianReader.ToInt32(export.Data, export.Data.Length - 4, export.FileRef.Endian);
                            IEntry entry         = export.FileRef.GetEntry(uindexOfOuter);
                            if (entry != null)
                            {
                                result.Append($"{entry.ObjectName.Instanced} ");
                            }
                        }
                        else
                        {
                            result.Append($"{GetPropertyType(export)} ");
                        }

                        result.Append(export.ObjectName.Instanced);
                        paramCount++;

                        if (ObjectFlagsMask.HasFlag(UnrealFlags.EPropertyFlags.OptionalParm) && Statements.Count > 0)
                        {
                            if (Statements[0].Token is NothingToken)
                            {
                                Statements.RemoveRange(0, 1);
                            }
                            else if (Statements[0].Token is DefaultParamValueToken)
                            {
                                result.Append(" = ").Append(Statements[0].Token.ToString());
                                Statements.RemoveRange(0, 1);
                            }
                        }
                    }
                    if (ObjectFlagsMask.HasFlag(UnrealFlags.EPropertyFlags.ReturnParm))
                    {
                        break; //return param
                    }
                }



                //object instance = export.ReadInstance();
                //if (instance is UnClassProperty)
                //{
                //    var prop = (UnClassProperty)instance;
                //    if (prop.Parm)
                //    {
                //        if (!prop.ReturnParm)
                //        {
                //            if (paramCount > 0)
                //                result.Append(", ");

                //            prop.Flags.Except("Parm").Each(f => result.Append(f.ToLower() + " "));
                //            result.Append(prop.GetPropertyType()).Append(" ").Append(export.ObjectName);
                //            if (prop.OptionalParm && statements.Count > 0)
                //            {
                //                if (statements[0].Token is NothingToken)
                //                    statements.RemoveRange(0, 1);
                //                else if (statements[0].Token is DefaultParamValueToken)
                //                {
                //                    result.Append(" = ").Append(statements[0].Token.ToString());
                //                    statements.RemoveRange(0, 1);
                //                }
                //            }
                //            paramCount++;
                //        }
                //    }
                //    else
                //    {
                //        locals.Add(prop);
                //    }
                //}
            }
            result.Append(")");

            FunctionSignature = result.ToString();
            if (HasFlag("Defined"))
            {
                result.NewLine().Indent().Append("{").NewLine();
                result.PushIndent();
                foreach (var local in locals)
                {
                    result.Indent().Append("local ").Append(GetPropertyType(local)).Append(" ").Append(local.ObjectName.Instanced).Append(";").NewLine();
                }
                result.PopIndent();   // will be pushed again in DecompileBytecode()
                DecompileBytecode(Statements, result, createControlStatements);
                result.Indent().Append("}").NewLine().NewLine();
            }
            else
            {
                result.Append(";").NewLine().NewLine();
            }
        }
Exemple #4
0
        public string GetSignature()
        {
            string result = "";

            if (export.ClassName == "Function")
            {
                if (Native)
                {
                    result += "native";
                    if (GetNatIdx() > 0)
                    {
                        result += $"({GetNatIdx()})";
                    }
                    result += " ";
                }

                flags.Except("Native", "Event", "Delegate", "Defined", "Public", "HasDefaults", "HasOutParms").Each(f => result += f.ToLower() + " ");
            }

            if (HasFlag("Event"))
            {
                result += "event ";
            }
            else if (HasFlag("Delegate"))
            {
                result += "delegate ";
            }
            else
            {
                result += export.ClassName.ToLower() + " ";
            }
            string type = GetReturnType();

            if (type != null)
            {
                result += type + " ";
            }
            result += export.ObjectName.Instanced + "(";
            int paramCount = 0;
            var locals     = new List <ExportEntry>();

            //Tokens = new List<BytecodeSingularToken>();
            //Statements = ReadBytecode();

            // OLD CODE
            //List<ExportEntry> childrenReversed = export.FileRef.Exports.Where(x => x.idxLink == export.UIndex).ToList();
            //childrenReversed.Reverse();

            // NEW CODE
            List <ExportEntry> childrenReversed = new List <ExportEntry>();
            var childIdx = EndianReader.ToInt32(export.Data, 0x14, export.FileRef.Endian);

            while (export.FileRef.TryGetUExport(childIdx, out var parsingExp))
            {
                childrenReversed.Add(parsingExp);
                childIdx = EndianReader.ToInt32(parsingExp.Data, 0x10, export.FileRef.Endian);
            }

            //Get local children of this function
            foreach (ExportEntry export in childrenReversed)
            {
                //Reading parameters info...
                if (export.ClassName.EndsWith("Property"))
                {
                    UnrealFlags.EPropertyFlags ObjectFlagsMask = (UnrealFlags.EPropertyFlags)EndianReader.ToUInt64(export.Data, 0x18, export.FileRef.Endian);
                    if (ObjectFlagsMask.HasFlag(UnrealFlags.EPropertyFlags.Parm) && !ObjectFlagsMask.HasFlag(UnrealFlags.EPropertyFlags.ReturnParm))
                    {
                        if (paramCount > 0)
                        {
                            result += ", ";
                        }

                        if (export.ClassName == "ObjectProperty" || export.ClassName == "StructProperty")
                        {
                            var    uindexOfOuter = EndianReader.ToInt32(export.Data, export.Data.Length - 4, export.FileRef.Endian);
                            IEntry entry         = export.FileRef.GetEntry(uindexOfOuter);
                            if (entry != null)
                            {
                                result += entry.ObjectName.Instanced + " ";
                            }
                        }
                        else
                        {
                            result += UnFunction.GetPropertyType(export) + " ";
                        }

                        result += export.ObjectName.Instanced;
                        paramCount++;

                        //if (ObjectFlagsMask.HasFlag(UnrealFlags.EPropertyFlags.OptionalParm) && Statements.Count > 0)
                        //{
                        //    if (Statements[0].Token is NothingToken)
                        //        Statements.RemoveRange(0, 1);
                        //    else if (Statements[0].Token is DefaultParamValueToken)
                        //    {
                        //        result += " = ").Append(Statements[0].Token.ToString());
                        //        Statements.RemoveRange(0, 1);
                        //    }
                        //}
                    }
                    if (ObjectFlagsMask.HasFlag(UnrealFlags.EPropertyFlags.ReturnParm))
                    {
                        break; //return param
                    }
                }
            }
            result += ")";
            return(result);
        }
        public void Decompile(TextBuilder result, bool createControlStatements)
        {
            result.Indent();
            if (Native)
            {
                result.Append("native");
                if (_nativeIndex > 0)
                {
                    result.Append("(").Append(_nativeIndex).Append(")");
                }
                result.Append(" ");
            }

            _flags.Except("Native", "Event", "Delegate", "Defined", "Public", "HasDefaults", "HasOutParms").Each(f => result.Append(f.ToLower() + " "));

            if (HasFlag("Event"))
            {
                result.Append("event ");
            }
            else if (HasFlag("Delegate"))
            {
                result.Append("delegate ");
            }
            else
            {
                result.Append("function ");
            }
            string type = GetReturnType();

            if (type != null)
            {
                result.Append(type).Append(" ");
            }
            result.Append(_self.ObjectName.Instanced).Append("(");
            int paramCount = 0;
            var locals     = new List <ExportEntry>();

            Tokens     = new List <BytecodeSingularToken>();
            Statements = ReadBytecode();
            List <ExportEntry> childrenReversed = _self.FileRef.Exports.Where(x => x.idxLink == _self.UIndex).ToList();

            childrenReversed.Reverse();

            //Get local children of this function
            foreach (ExportEntry export in childrenReversed)
            {
                //Reading parameters info...
                if (export.ClassName.EndsWith("Property"))
                {
                    UnrealFlags.EPropertyFlags ObjectFlagsMask = (UnrealFlags.EPropertyFlags)BitConverter.ToUInt64(export.Data, 0x18);
                    if (ObjectFlagsMask.HasFlag(UnrealFlags.EPropertyFlags.Parm) && !ObjectFlagsMask.HasFlag(UnrealFlags.EPropertyFlags.ReturnParm))
                    {
                        if (paramCount > 0)
                        {
                            result.Append(", ");
                        }

                        if (export.ClassName == "ObjectProperty" || export.ClassName == "StructProperty")
                        {
                            var    uindexOfOuter = BitConverter.ToInt32(export.Data, export.Data.Length - 4);
                            IEntry entry         = export.FileRef.GetEntry(uindexOfOuter);
                            if (entry != null)
                            {
                                result.Append($"{entry.ObjectName.Instanced} ");
                            }
                        }
                        else
                        {
                            result.Append($"{GetPropertyType(export)} ");
                        }

                        result.Append(export.ObjectName.Instanced);
                        paramCount++;

                        if (ObjectFlagsMask.HasFlag(UnrealFlags.EPropertyFlags.OptionalParm) && Statements.Count > 0)
                        {
                            if (Statements[0].Token is NothingToken)
                            {
                                Statements.RemoveRange(0, 1);
                            }
                            else if (Statements[0].Token is DefaultParamValueToken)
                            {
                                result.Append(" = ").Append(Statements[0].Token.ToString());
                                Statements.RemoveRange(0, 1);
                            }
                        }
                    }
                    if (ObjectFlagsMask.HasFlag(UnrealFlags.EPropertyFlags.ReturnParm))
                    {
                        break; //return param
                    }
                }

                //object instance = export.ReadInstance();
                //if (instance is UnClassProperty)
                //{
                //    var prop = (UnClassProperty)instance;
                //    if (prop.Parm)
                //    {
                //        if (!prop.ReturnParm)
                //        {
                //            if (paramCount > 0)
                //                result.Append(", ");

                //            prop.Flags.Except("Parm").Each(f => result.Append(f.ToLower() + " "));
                //            result.Append(prop.GetPropertyType()).Append(" ").Append(export.ObjectName);
                //            if (prop.OptionalParm && statements.Count > 0)
                //            {
                //                if (statements[0].Token is NothingToken)
                //                    statements.RemoveRange(0, 1);
                //                else if (statements[0].Token is DefaultParamValueToken)
                //                {
                //                    result.Append(" = ").Append(statements[0].Token.ToString());
                //                    statements.RemoveRange(0, 1);
                //                }
                //            }
                //            paramCount++;
                //        }
                //    }
                //    else
                //    {
                //        locals.Add(prop);
                //    }
                //}
            }
            result.Append(")");

            FunctionSignature = result.ToString();
            if (HasFlag("Defined"))
            {
                result.NewLine().Indent().Append("{").NewLine();
                result.PushIndent();
                foreach (var local in locals)
                {
                    result.Indent().Append("local ").Append(GetPropertyType(local)).Append(" ").Append(local.ObjectName.Instanced).Append(";").NewLine();
                }
                result.PopIndent();   // will be pushed again in DecompileBytecode()
                DecompileBytecode(Statements, result, createControlStatements);
                result.Indent().Append("}").NewLine().NewLine();
            }
            else
            {
                result.Append(";").NewLine().NewLine();
            }
        }