private static void PatchBND(string sourceLangDir, string refLangDir, string[] destFilePath)
        {
            foreach (var file in destFilePath)//Patch each file in files Array
            {
                if (BND3.IsRead(file, out BND3 destBND3))
                {
                    //Set source BND files
                    string sourceLangFiles = $@"{ sourceLangDir }\{ Path.GetFileName(file) }";
                    BND3   sourceBND       = BND3.Read(sourceLangFiles);
                    string refLangFiles    = $@"{refLangDir}\{Path.GetFileName(file)}";
                    //Make null ref BND, and make an actual BND read if it exists
                    BND3 refBND = null;
                    if (File.Exists(refLangFiles))
                    {
                        refBND = BND3.Read(refLangFiles);
                    }
                    #region Debug Stuff
                    //ConsoleLog(sourceBND.Files.Count);
                    //Debug.WriteLine(Path.GetFileName(sourceBND.Files[0].Name));
                    //ConsoleLog(destFMG.Entries.Count + " & " + sourceFMG.Entries.Count);
                    #endregion

                    //Patch BND file
                    PatchFMG(sourceBND, destBND3, refBND, file);

                    //Write new BND
                    destBND3.Write(file);
                }
                else if (BND4.IsRead(file, out BND4 destBND4))
                {
                    //Set source BND files
                    string sourceLangFiles = $@"{sourceLangDir}\{Path.GetFileName(file)}";
                    BND4   sourceBND       = BND4.Read(sourceLangFiles);
                    string refLangFiles    = $@"{refLangDir}\{Path.GetFileName(file)}";
                    //Make null ref BND, and make an actual BND read if it exists
                    BND4 refBND = null;
                    if (File.Exists(refLangFiles))
                    {
                        refBND = BND4.Read(refLangFiles);
                    }

                    #region Debug Stuff
                    //ConsoleLog(sourceBND.Files.Count);
                    //Debug.WriteLine(Path.GetFileName(sourceBND.Files[0].Name));
                    //ConsoleLog(destFMG.Entries.Count + " & " + sourceFMG.Entries.Count);
                    #endregion
                    //Patch BND file
                    PatchFMG(sourceBND, destBND4, refBND, file);

                    //Write new BND
                    destBND4.Write(file);
                }
            }
        }
예제 #2
0
        private int PackBinder(IBinder binder, string relOutputDir, CancellationToken cancelToken)
        {
            int textureCount = 0;

            foreach (BinderFile file in binder.Files)
            {
                if (cancelToken.IsCancellationRequested)
                {
                    return(textureCount);
                }

                if (TPUtil.HasValidExtension(file.Name))
                {
                    try
                    {
                        byte[]   bytes   = file.Bytes;
                        DCX.Type dcxType = DCX.Type.None;
                        if (DCX.Is(bytes))
                        {
                            bytes = DCX.Decompress(bytes, out dcxType);
                        }

                        if (TPF.IsRead(bytes, out TPF tpf))
                        {
                            int thisTextureCount = PackTPF(tpf, relOutputDir);
                            if (thisTextureCount > 0)
                            {
                                file.Bytes = tpf.Write(dcxType);
                            }
                            textureCount += thisTextureCount;
                        }
                        else if (BND4.IsRead(bytes, out BND4 bnd))
                        {
                            int thisTextureCount = PackBinder(bnd, relOutputDir, cancelToken);
                            if (thisTextureCount > 0)
                            {
                                file.Bytes = bnd.Write(dcxType);
                            }
                            textureCount += thisTextureCount;
                        }
                        else
                        {
                            throw new NotSupportedException("Unknown file type.");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception($"Error in binder file \"{file.Name}\"", ex);
                    }
                }
            }
            return(textureCount);
        }
예제 #3
0
        private int PackVirtualFile(VirtualFile vf, CancellationToken cancelToken)
        {
            byte[] bytes;
            long   baseMemory;

            lock (this)
            {
                while (BaseMemoryCommitted > TPUtil.MAX_BASE_MEMORY)
                {
                    Thread.Sleep(10);
                }

                if (cancelToken.IsCancellationRequested)
                {
                    return(0);
                }

                bytes      = vf.Load();
                baseMemory = bytes.Length;
                Interlocked.Add(ref BaseMemoryCommitted, baseMemory);
            }

            try
            {
                string   relOutputDir = TPUtil.GetRelativeOutputDir(vf.Path);
                DCX.Type dcxType      = DCX.Type.None;
                if (DCX.Is(bytes))
                {
                    bytes = DCX.Decompress(bytes, out dcxType);
                }

                int textureCount;
                if (TPF.IsRead(bytes, out TPF tpf))
                {
                    textureCount = PackTPF(tpf, relOutputDir);
                    if (textureCount > 0)
                    {
                        tpf.Write($@"{OutputDirectory}\{vf.Path}", dcxType);
                    }
                }
                else if (BND4.IsRead(bytes, out BND4 bnd))
                {
                    textureCount = PackBinder(bnd, relOutputDir, cancelToken);
                    if (textureCount > 0)
                    {
                        bnd.Write($@"{OutputDirectory}\{vf.Path}", dcxType);
                    }
                }
                else if (BXF4.IsBDT(bytes))
                {
                    string      ext      = Path.GetExtension(vf.Path).Replace("bdt", "bhd");
                    string      bhdPath  = Path.ChangeExtension(vf.Path, ext);
                    VirtualFile vfHeader = VirtualFS.Files[bhdPath];
                    byte[]      bhdBytes = vfHeader.Load();

                    var bxf = BXF4.Read(bhdBytes, bytes);
                    textureCount = PackBinder(bxf, relOutputDir, cancelToken);
                    if (textureCount > 0)
                    {
                        bxf.Write($@"{OutputDirectory}\{vfHeader.Path}", $@"{OutputDirectory}\{vf.Path}");
                    }
                }
                else
                {
                    throw new NotSupportedException("Unknown file type.");
                }
                return(textureCount);
            }
            finally
            {
                Interlocked.Add(ref BaseMemoryCommitted, -baseMemory);
            }
        }
예제 #4
0
        private int UnpackVirtualFile(VirtualFile vf, CancellationToken cancelToken)
        {
            byte[] bytes;
            long   baseMemory;

            lock (this)
            {
                while (BaseMemoryCommitted > TPUtil.MAX_BASE_MEMORY)
                {
                    Thread.Sleep(10);
                }

                if (cancelToken.IsCancellationRequested)
                {
                    return(0);
                }

                bytes      = vf.Load();
                baseMemory = bytes.Length;
                Interlocked.Add(ref BaseMemoryCommitted, baseMemory);
            }

            try
            {
                string relOutputDir = TPUtil.GetRelativeOutputDir(vf.Path);
                if (DCX.Is(bytes))
                {
                    bytes = DCX.Decompress(bytes);
                }

                int textureCount;
                var report = new UnpackReport();
                if (TPF.IsRead(bytes, out TPF tpf))
                {
                    textureCount = UnpackTPF(tpf, relOutputDir, report);
                }
                else if (BND4.IsRead(bytes, out BND4 bnd))
                {
                    textureCount = UnpackBinder(bnd, relOutputDir, report, cancelToken);
                }
                else if (BXF4.IsBDT(bytes))
                {
                    string      ext      = Path.GetExtension(vf.Path).Replace("bdt", "bhd");
                    string      bhdPath  = Path.ChangeExtension(vf.Path, ext);
                    VirtualFile vfHeader = VirtualFS.Files[bhdPath];
                    byte[]      bhdBytes = vfHeader.Load();
                    var         bxf      = BXF4.Read(bhdBytes, bytes);
                    textureCount = UnpackBinder(bxf, relOutputDir, report, cancelToken);
                }
                else
                {
                    throw new NotSupportedException("Unknown file type.");
                }

                if (report.Files.Count > 0)
                {
                    File.WriteAllText($@"{Game.Settings.UnpackDirectory.TrimEnd('\\')}\{relOutputDir}\_report.txt", report.Write());
                }
                return(textureCount);
            }
            finally
            {
                Interlocked.Add(ref BaseMemoryCommitted, -baseMemory);
            }
        }