Inheritance: IDisposable
        public static string ExtractArchiveFile(string psarcPath, string entryNamePath, string outputDir)
        {
            if (!File.Exists(psarcPath))
            {
                return("");
            }

            using (PSARC archive = new PSARC(true))
                using (var psarcStream = File.OpenRead(psarcPath))
                {
                    archive.Read(psarcStream, true);
                    var tocEntry = archive.TOC.Where(entry => entry.Name.Contains(entryNamePath)).FirstOrDefault();

                    if (tocEntry != null)
                    {
                        if (!Directory.Exists(outputDir))
                        {
                            Directory.CreateDirectory(outputDir);
                        }

                        archive.InflateEntry(tocEntry, Path.Combine(outputDir, Path.GetFileName(tocEntry.ToString())));

                        return(Path.Combine(outputDir, tocEntry.ToString()));
                    }

                    return("");
                }
        }
 // Loads song archive file to memory.
 public PsarcLoader(string fileName, bool useMemory = true)
 {
     _filePath = fileName;
     _archive = new PSARC(useMemory);
     _fileStream = File.OpenRead(_filePath);
     _archive.Read(_fileStream);
 }
Beispiel #3
0
 // Loads song archive file to memory.
 public PsarcLoader(string fileName, bool useMemory = true)
 {
     _filePath   = fileName;
     _archive    = new PSARC(useMemory);
     _fileStream = File.OpenRead(_filePath);
     _archive.Read(_fileStream);
 }
        public static bool RemoveArchiveEntry(string psarcPath, string entryName)
        {
            if (!File.Exists(psarcPath))
            {
                return(false);
            }

            using (PSARC archive = new PSARC(true))
                using (var psarcStream = File.OpenRead(psarcPath))
                {
                    archive.Read(psarcStream);
                    psarcStream.Dispose(); // CRITICAL

                    var tocEntry = archive.TOC.FirstOrDefault(entry => entry.Name == entryName);

                    if (tocEntry == null)
                    {
                        archive.Dispose(); // CRITICAL
                        return(true);
                    }

                    archive.TOC.Remove(tocEntry);
                    archive.TOC.Insert(0, new Entry()
                    {
                        Name = "NamesBlock.bin"
                    });

                    using (var fs = File.Create(psarcPath))
                        archive.Write(fs, true);

                    archive.Dispose(); // CRITICAL
                    return(true);
                }
        }
        // old slower static method
        public static List<Entry> ExtractAudioEntry(string archiveName, string audioName, string previewName)
        {
            bool result = false;
            if (String.IsNullOrEmpty(audioName))
                return null; // false;

            GlobalExtension.ShowProgress("Extracting Audio ...");
            List<Entry> wems;

            using (var archive = new PSARC(true))
            using (var stream = File.OpenRead(archiveName))
            {
                archive.Read(stream, true);
                wems = archive.TOC.Where(entry => entry.Name.StartsWith("audio/windows") && entry.Name.EndsWith(".wem")).ToList();

                if (wems.Count > 1)
                {
                    wems.Sort((e1, e2) =>
                    {
                        if (e1.Length < e2.Length)
                            return 1;
                        if (e1.Length > e2.Length)
                            return -1;
                        return 0;
                    });
                }

                if (wems.Count > 0)
                {
                    var top = wems[0]; // wem audio with internal TOC path
                    archive.InflateEntry(top);
                    top.Data.Position = 0;
                    using (var FS = File.Create(audioName))
                    {
                        WwiseToOgg w2o = new WwiseToOgg(top.Data, FS);
                        result = w2o.ConvertToOgg();
                    }
                }

                if (!String.IsNullOrEmpty(previewName) && result && wems.Count > 0)
                {
                    var bottom = wems.Last();
                    archive.InflateEntry(bottom);
                    bottom.Data.Position = 0;
                    using (var FS = File.Create(previewName))
                    {
                        WwiseToOgg w2o = new WwiseToOgg(bottom.Data, FS);
                        result = w2o.ConvertToOgg();
                    }
                }
            }

            // confirmed this output is same as old exe converter method both are 44KHz VBR
            if (!result)
                return null;

            return wems;
        }
        public static bool InjectArchiveEntry(string psarcPath, string entryName, string sourcePath)
        {
            if (!File.Exists(psarcPath))
            {
                return(false);
            }

            using (PSARC archive = new PSARC(true))
                using (var psarcStream = File.OpenRead(psarcPath))
                {
                    try
                    {
                        archive.Read(psarcStream);
                        psarcStream.Dispose(); // CRITICAL

                        var entryStream = new MemoryStream();

                        using (var sourceStream = File.OpenRead(sourcePath))
                            sourceStream.CopyTo(entryStream);

                        entryStream.Position = 0;
                        Entry tocEntry = archive.TOC.FirstOrDefault(x => x.Name == entryName);

                        if (tocEntry != null)
                        {
                            tocEntry.Data.Dispose(); // CRITICAL
                            tocEntry.Data = entryStream;
                        }
                        else
                        {
                            archive.AddEntry(entryName, entryStream);

                            // evil genius ... ;) => forces archive update
                            archive.TOC.Insert(0, new Entry()
                            {
                                Name = "NamesBlock.bin"
                            });
                        }
                    }
                    catch
                    {
                        archive.Dispose(); // CRITICAL
                        return(false);
                    }

                    using (var fs = File.Create(psarcPath))
                        archive.Write(fs, true);

                    archive.Dispose(); // CRITICAL
                    return(true);
                }
        }
        public static bool ReplaceData(this PSARC p, Dictionary <Func <Entry, bool>, Stream> newData)
        {
            bool result = true;

            foreach (var d in newData)
            {
                if (!p.ReplaceData(d.Key, d.Value))
                {
                    result = false;
                }
            }
            return(result);
        }
Beispiel #8
0
        public void Dispose()
        {
            if (_fileStream != null)
            {
                _fileStream.Dispose();
                _fileStream = null;
            }
            if (_archive != null)
            {
                _archive.Dispose();
                _archive = null;
            }

            GC.SuppressFinalize(this);
        }
        public static NoCloseStream ReplaceData(this PSARC p, Func <Entry, bool> dataEntry, String newData)
        {
            NoCloseStream s = new NoCloseStream();

            using (var sr = new StreamWriter(s))
                sr.Write(newData);
            s.Position = 0;
            if (!ReplaceData(p, dataEntry, s))
            {
                s.canClose = true;
                s.Dispose();
                return(null);
            }
            return(s);
        }
        public static Stream GetData(this PSARC p, Func <Entry, bool> dataEntry)
        {
            var de = p.TOC.Where(dataEntry).FirstOrDefault();

            if (de != null)
            {
                if (de.Data == null)
                {
                    p.InflateEntry(de);
                }

                return(de.Data);
            }
            return(null);
        }
        public static bool ReplaceData(this PSARC p, Func <Entry, bool> dataEntry, Stream newData)
        {
            var de = p.TOC.Where(dataEntry).FirstOrDefault();

            if (de != null)
            {
                if (de.Data != null)
                {
                    de.Data.Dispose();
                    de.Data = null;
                }
                else
                {
                    p.InflateEntry(de);
                }

                de.Data = newData;
                return(true);
            }
            return(false);
        }
        public static Stream ExtractArchiveFile(string psarcPath, string entryNamePath)
        {
            if (!File.Exists(psarcPath))
            {
                return(null);
            }

            using (PSARC archive = new PSARC(true))
                using (var psarcStream = File.OpenRead(psarcPath))
                {
                    archive.Read(psarcStream, true);
                    var tocEntry = archive.TOC.FirstOrDefault(x => (x.Name.Equals(entryNamePath)));

                    if (tocEntry != null)
                    {
                        archive.InflateEntry(tocEntry);
                        return(tocEntry.Data);
                    }
                }

            return(null);
        }
Beispiel #13
0
        public static Stream ExtractPSARCData(this Stream stream, Func <Entry, bool> dataEntry)
        {
            using (PSARC p = new PSARC(true))
            {
                p.Read(stream, true);

                var de = p.TOC.Where(dataEntry).FirstOrDefault();
                if (de != null)
                {
                    MemoryStream ms = new MemoryStream();
                    p.InflateEntry(de);
                    if (de.Data == null)
                    {
                        return(null);
                    }
                    de.Data.Position = 0;
                    de.Data.CopyTo(ms);
                    ms.Position = 0;
                    return(ms);
                }
                return(null);
            }
        }
        public void Dispose()
        {
            if (_fileStream != null)
            {
                _fileStream.Dispose();
                _fileStream = null;
            }
            if (_archive != null)
            {
                _archive.Dispose();
                _archive = null;
            }

            GC.SuppressFinalize(this);
        }
Beispiel #15
0
        // old slower static method
        public static List <Entry> ExtractAudioEntry(string archiveName, string audioName, string previewName)
        {
            bool result = false;

            if (String.IsNullOrEmpty(audioName))
            {
                return(null); // false;
            }
            GlobalExtension.ShowProgress("Extracting Audio ...");
            List <Entry> wems;

            using (var archive = new PSARC(true))
                using (var stream = File.OpenRead(archiveName))
                {
                    archive.Read(stream, true);
                    wems = archive.TOC.Where(entry => entry.Name.StartsWith("audio/windows") && entry.Name.EndsWith(".wem")).ToList();

                    if (wems.Count > 1)
                    {
                        wems.Sort((e1, e2) =>
                        {
                            if (e1.Length < e2.Length)
                            {
                                return(1);
                            }
                            if (e1.Length > e2.Length)
                            {
                                return(-1);
                            }
                            return(0);
                        });
                    }

                    if (wems.Count > 0)
                    {
                        var top = wems[0]; // wem audio with internal TOC path
                        archive.InflateEntry(top);
                        top.Data.Position = 0;
                        using (var FS = File.Create(audioName))
                        {
                            WwiseToOgg w2o = new WwiseToOgg(top.Data, FS);
                            result = w2o.ConvertToOgg();
                        }
                    }

                    if (!String.IsNullOrEmpty(previewName) && result && wems.Count > 0)
                    {
                        var bottom = wems.Last();
                        archive.InflateEntry(bottom);
                        bottom.Data.Position = 0;
                        using (var FS = File.Create(previewName))
                        {
                            WwiseToOgg w2o = new WwiseToOgg(bottom.Data, FS);
                            result = w2o.ConvertToOgg();
                        }
                    }
                }

            // confirmed this output is same as old exe converter method both are 44KHz VBR
            if (!result)
            {
                return(null);
            }

            return(wems);
        }
Beispiel #16
0
        public static bool InjectArchiveEntry(string psarcPath, string entryName,
                                              string sourcePath, bool updateToolkitVersion = false,
                                              string packageAuthor = "", string packageVersion = "1", string packageComment = "")
        {
            if (!File.Exists(psarcPath))
            {
                return(false);
            }

            int injectionCount = 2;

            if (!updateToolkitVersion)
            {
                injectionCount = 1;
            }
            else
            {
                if (String.IsNullOrEmpty(packageVersion))
                {
                    packageVersion = "1";
                }
                if (String.IsNullOrEmpty(packageAuthor))
                {
                    packageAuthor = Assembly.GetExecutingAssembly().GetName().ToString();
                }
            }

            using (PSARC archive = new PSARC(true))
                using (var psarcStream = File.OpenRead(psarcPath))
                {
                    try
                    {
                        archive.Read(psarcStream);
                        psarcStream.Dispose();

                        for (int i = 0; i < injectionCount; i++)
                        {
                            var entryStream = new MemoryStream();

                            switch (i)
                            {
                            case 0:
                                using (var sourceStream = File.OpenRead(sourcePath))
                                    sourceStream.CopyTo(entryStream);
                                break;

                            case 1:
                                DLCPackageCreator.GenerateToolkitVersion(entryStream, packageAuthor, packageVersion, packageComment);
                                entryName = "toolkit.version";
                                break;
                            }

                            entryStream.Position = 0;
                            Entry tocEntry = archive.TOC.FirstOrDefault(x => x.Name == entryName);

                            if (tocEntry != null)
                            {
                                tocEntry.Data.Dispose();
                                tocEntry.Data = null;
                                tocEntry.Data = entryStream;
                            }
                            else
                            {
                                archive.AddEntry(entryName, entryStream);

                                // evil genius ... ;) => forces archive update
                                archive.TOC.Insert(0, new Entry()
                                {
                                    Name = "NamesBlock.bin"
                                });
                            }
                        }
                    }
                    catch
                    {
                        return(false);
                    }

                    using (var fs = File.Create(psarcPath))
                        archive.Write(fs, true);

                    return(true);
                }
        }