/// <summary> /// Return a stream writer for the passed path name /// </summary> /// <param name="path">The path name</param> /// <returns>An existing or new stream writer</returns> public StreamWriter GetStreamWriter(string path) { CachedStream cs = null; using(Locker.AcquireReaderLock(_lock)) { if (!_streamCache.TryGetValue(path, out cs)) { using (Locker.UpgradeToWriterLock(_lock)) { Stream stream = GetStream(path); cs = new CachedStream() { StreamWriter = new StreamWriter(stream) }; _streamCache.Add(path, cs); } } } cs.LastAccessTime = DateTime.Now; return cs.StreamWriter; }
public DdrPs2FileDataTableChunk GetUnbound(Stream stream) { var cache = new CachedStream(stream); var cacheReader = new BinaryReader(cache); using var snapshot = new SnapshotStream(cache); var snapshotReader = new BinaryReader(snapshot); var firstFile = snapshotReader.ReadInt32(); var tableSize = firstFile / 4 * 4; var table = Enumerable.Range(0, firstFile / 4).Select(_ => snapshotReader.ReadInt32()).ToArray(); var skip = firstFile - tableSize; int index; if (skip > 0) { snapshotReader.ReadBytes(skip); } for (index = 1; index < table.Length; index++) { if (table[index] < table[index - 1]) { break; } } // Warning: big heckin' hack. // We're looking for a TCB table, which puts headers on all the file entries. // There's no way to tell easily outside of this function, so... var prevPosition = cache.Position; cache.Position = table[0]; var headTest0 = cacheReader.ReadInt32(); var headTest1 = cacheReader.ReadInt32(); var headTest2 = cacheReader.ReadInt32(); var hasHeaders = headTest0 > 0 && headTest1 > 0 && headTest2 > 0 && headTest2 > headTest1 && headTest1 > headTest0 && (headTest0 & 0x3) == 0 && ((headTest0 | headTest1 | headTest2) >> 24) == 0; cache.Position = prevPosition; var desiredPosition = table[index - 1]; snapshotReader.ReadBytes((int)(desiredPosition - prevPosition)); if (hasHeaders) { snapshotReader.ReadBytes(0xC); } try { _bemaniLzDecoder.Decode(snapshot); } catch (Exception) { return(null); } return(new DdrPs2FileDataTableChunk { Data = snapshot.ToArray(), HasHeaders = hasHeaders }); }
public void Test_Export_SN_WAV() { using var mdSource = new FileStream(ExecutablePath, FileMode.Open, FileAccess.Read); var metadataDecoder = Resolve <IDdrPs2MetadataTableStreamReader>(); var dbDecoder = Resolve <IDdrPs2DatabaseDecoder>(); var rawMetaDatas = metadataDecoder.Get(mdSource, mdSource.Length).Select(dbDecoder.Decode) .OrderBy(x => x.AudioTrack).ToList(); foreach (var md in rawMetaDatas) { TestContext.WriteLine($"{md}"); } var firstSongIndex = rawMetaDatas.Where(x => x.AudioTrack > 0).Select(x => x.AudioTrack).Min(); using var source = new FileStream(FileDataPath, FileMode.Open, FileAccess.Read); var vagDecoder = Resolve <VagDecoder>(); // var metadataDecorator = Resolve<IDdrMetadataDecorator>(); // var ssqDecoder = Resolve<ISsqDecoder>(); // var smEncoder = Resolve<ISmEncoder>(); // var smWriter = Resolve<ISmStreamWriter>(); var soundIndex = 0; // var chartIndex = 0; var max = source.Length - 0x800; // var ssqHeuristic = Resolve<SsqHeuristic>(); var bgmHeuristic = Resolve <BeatmaniaPs2NewBgmHeuristic>(); var cache = new CachedStream(source); for (var offset = 0L; offset < max; offset += 0x800) { source.Position = offset; cache.Reset(); // if (ssqHeuristic.Match(cache) is HeuristicResult ssqResult) // { // cache.Rewind(); // var charts = ssqDecoder.Decode(ssqHeuristic.Read(ssqResult, cache)); // // var idMd = rawMetaDatas.FirstOrDefault(md => md.InternalId == chartIndex + 1); // // var chartSet = new ChartSet // { // Charts = charts, // Metadata = new Metadata() // }; // // // metadataDecorator.Decorate(chartSet, idMd, new MetadataDecoratorFileExtensions()); // var commands = smEncoder.Encode(chartSet); // using var stream = this.OpenWrite(Path.Combine(OutPath, $"{chartIndex:D4}.sm")); // smWriter.Write(stream, commands); // stream.Flush(); // chartIndex++; // // continue; // } // cache.Rewind(); if (bgmHeuristic.Match(cache) is HeuristicResult bgmResult) { cache.Rewind(); //var idMd = rawMetaDatas.FirstOrDefault(md => md.AudioTrack == soundIndex + firstSongIndex); //var idMd = soundIndex >= rawMetaDatas.Count ? null : rawMetaDatas[soundIndex]; var fileName = $"{soundIndex:D4}"; //idMd?.Id ?? $"{soundIndex:D4}"; var vag = bgmHeuristic.Read(bgmResult, cache); var decoded = vagDecoder.Decode(vag); this.WriteSound(decoded, Path.Combine(OutPath, $"{fileName}.wav")); soundIndex++; } } }