Esempio n. 1
0
        public static bool TryGetDataFromURL(string url, out string output)
        {
            try
            {
                WebClient wc  = new System.Net.WebClient();
                byte[]    raw = wc.DownloadData(url);

                output = Encoding.UTF8.GetString(raw);
                return(true);
            }
            catch (Exception ex)
            {
                output = "";

                TKContext.LogException(ex.ToString());
                return(false);
            }
        }
Esempio n. 2
0
        public static UTexture2D GetTextureFromUAsset(UassetFile uassetFile)
        {
            /*if (uassetFile.UE4Version != UE4Version.UE4__4_14)
             *  throw new NotSupportedException($"Texture extraction from {uassetFile.UE4Version.ToString()} uasset files is not supported.");*/

            TKContext.LogInner("INFO", $"Attempting to get a texture from file {uassetFile.Filename}");

            try
            {
                FObjectExport export       = uassetFile.ExportsTable[0];
                EPixelFormat  ePixelFormat = EPixelFormat.PF_Unknown;

                int pfOffset = 0x0;

                // TODO: Simplify this mess.
                for (int i = 0; i < uassetFile.NamesTable.Length; i++)
                {
                    if (uassetFile.NamesTable[i].Name.ToUpper().StartsWith("PF_"))
                    {
                        if (!Enum.TryParse(uassetFile.NamesTable[i].Name, out ePixelFormat))
                        {
                            TKContext.LogError("Pixel Format is not supported.");
                            return(null);
                        }
                    }
                }

                UTexture2D uTexture2D = new UTexture2D();
                //uTexture2D.USerializedData = uSerializedData;
                uTexture2D.UassetFile = uassetFile;
                TKContext.LogInner("INFO", $"Attempting to find {ePixelFormat.ToString()}");
                pfOffset = ArrayExtensions.IndexOfPattern(export.data, Encoding.ASCII.GetBytes(ePixelFormat.ToString()));
                TKContext.LogInner("INFO", $"Pixel Format offset is 0x{pfOffset:X8}");

                if (pfOffset == int.MaxValue)
                {
                    TKContext.LogError("No texture was found.");
                    return(null);
                }

                uTexture2D.PFOffset = pfOffset;

                uTexture2D.PixelFormat  = ePixelFormat;
                uTexture2D.Height       = BitConverter.ToInt32(ArrayExtensions.GetBytesFromByteArray(export.data, pfOffset - UASSET_RELATIVE_PF_HEIGHT, 4), 0);
                uTexture2D.Width        = BitConverter.ToInt32(ArrayExtensions.GetBytesFromByteArray(export.data, pfOffset - UASSET_RELATIVE_PF_WIDTH, 4), 0);
                uTexture2D.UnknownValue = BitConverter.ToInt32(ArrayExtensions.GetBytesFromByteArray(export.data, pfOffset + 0x8, 4), 0);

                TKContext.LogInner("INFO", $"Texture resolution is {uTexture2D.Width}x{uTexture2D.Height}");

                int[] offsets = ArrayExtensions.Locate(export.data, BitConverter.GetBytes(PF_SEPERATOR));

                uTexture2D.MipMapCount = BitConverter.ToInt32(ArrayExtensions.GetBytesFromByteArray(export.data, offsets[0] - 0x4, 4), 0);
                uTexture2D.Mipmaps     = new List <FTexture2DMipMap>();

                TKContext.LogInner("INFO", $"Texture has {uTexture2D.MipMapCount} mipmaps");

                uTexture2D.FirstSeperatorOffset = offsets[0];

                for (int i = 0; i < uTexture2D.MipMapCount; i++)
                {
                    FTexture2DMipMap fTexture2DMipMap = new FTexture2DMipMap();

                    fTexture2DMipMap.UncompressedSize    = BitConverter.ToInt32(ArrayExtensions.GetBytesFromByteArray(export.data, offsets[i] + SEPERATOR_UNCOMPRESSED_SIZE_OFFSET, 4), 0);
                    fTexture2DMipMap.TextureUassetOffset = BitConverter.ToInt32(ArrayExtensions.GetBytesFromByteArray(export.data, offsets[i] + SEPERATOR_TEXTURE_START_OFFSET_OFFSET, 4), 0);
                    fTexture2DMipMap.UnknownValue        = BitConverter.ToInt32(ArrayExtensions.GetBytesFromByteArray(export.data, offsets[i] + SEPERATOR_UNKNOWN_OFFSET, 4), 0);
                    fTexture2DMipMap.Texture             = ArrayExtensions.GetBytesFromByteArray(export.data, offsets[i] + SEPERATOR_TEXTURE_DATA, fTexture2DMipMap.UncompressedSize);
                    fTexture2DMipMap.Width  = BitConverter.ToInt32(ArrayExtensions.GetBytesFromByteArray(export.data, offsets[i] + SEPERATOR_TEXTURE_DATA + fTexture2DMipMap.UncompressedSize, 4), 0);
                    fTexture2DMipMap.Height = BitConverter.ToInt32(ArrayExtensions.GetBytesFromByteArray(export.data, offsets[i] + SEPERATOR_TEXTURE_DATA + fTexture2DMipMap.UncompressedSize + 0x4, 4), 0);

                    uTexture2D.Mipmaps.Add(fTexture2DMipMap);

                    TKContext.LogInner("INFO", fTexture2DMipMap.ToString());
                }

                return(uTexture2D);
            }
            catch (Exception ex)
            {
                TKContext.LogException(ex.ToString());
                return(null);
            }
        }
Esempio n. 3
0
        public bool Read()
        {
            try
            {
                using (FileStream fs = new FileStream(FullPath, FileMode.Open, FileAccess.Read))
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        // Go to 0x2C from the end of the file.
                        br.BaseStream.Seek(-END_FOOTER_POS, SeekOrigin.End);

                        var magic = br.ReadUInt32();

                        // Read the file's magic number and see if it's valid.
                        if (magic != FOOTER_MAGIC && !IgnoreMagicNumber)
                        {
                            // The + operator is for nice code readiblity so don't get upset.
                            TKContext.LogError($"The package: {FileName} contains an invalid magic number." +
                                               " Set IgnoreMagicNumber to false if you want the stream reader to ignore it.");

                            return(false);
                        }

                        PackageFooter footer = new PackageFooter();
                        footer.magic       = magic;
                        footer.version     = br.ReadUInt32();
                        footer.indexOffset = br.ReadUInt64();
                        footer.indexLength = br.ReadUInt64();
                        footer.indexHash   = br.ReadBytes(20);

                        br.BaseStream.Seek((long)footer.indexOffset, SeekOrigin.Begin);
                        var skipbytes = br.ReadUInt32();
                        br.BaseStream.Seek(skipbytes, SeekOrigin.Current);
                        FileCount = br.ReadUInt32();

                        FileEntries = new PackageFileEntry[FileCount];

                        for (int i = 0; i < FileEntries.Length; i++)
                        {
                            PackageFileEntry packageFileEntry = new PackageFileEntry();

                            packageFileEntry.fileNameLength = br.ReadUInt32();
                            packageFileEntry.fileName       = new string(br.ReadChars((int)(packageFileEntry.fileNameLength - 1)));

                            br.BaseStream.Seek(1, SeekOrigin.Current);

                            packageFileEntry.offset = br.ReadUInt64();
                            packageFileEntry.size1  = br.ReadUInt64();
                            packageFileEntry.size2  = br.ReadUInt64();
                            packageFileEntry.pad    = br.ReadUInt32();
                            packageFileEntry.sha1   = br.ReadBytes(20);
                            packageFileEntry.pad2   = br.ReadBytes(5);

#if DEBUG
                            TKContext.Log("PackFileEntry", i.ToString(), $"File name: {packageFileEntry.fileName} Offset: 0x{packageFileEntry.offset:X8} " +
                                          $"Size1: 0x{packageFileEntry.size1:X8} Size2: 0x{packageFileEntry.size2:X8} SHA1: {packageFileEntry.sha1.ToString()}", TKContext.LOG_TYPE_DEBUG, ConsoleColor.DarkCyan);
#endif

                            FileEntries[i] = packageFileEntry;
                        }

                        Footer = footer;
                    }
                }
                TKContext.LogInner("INFO", $"Loaded package {FileName}", ConsoleColor.Yellow);
                Array.Sort(FileEntries, (x, y) =>
                {
                    var lengthComp = Comparer <int> .Default.Compare((int)y.fileNameLength, (int)x.fileNameLength);
                    if (lengthComp == 0)
                    {
                        return(string.Compare(y.fileName, x.fileName));
                    }
                    return(lengthComp);
                });
                return(true);
            }
            catch (Exception ex)
            {
                TKContext.LogException(ex.ToString());
                return(false);
            }
        }