11using System . Reflection ;
2+ using System . Security . Cryptography ;
23using System . Text ;
34using SonicAudioLib . CriMw ;
45using VGAudio . Containers . Wave ;
@@ -29,8 +30,11 @@ public static byte[] SetAcbPreview(string acbPath, TimeSpan loopStart, TimeSpan
2930 return criTable . Save ( ) ;
3031 }
3132
32- public static byte [ ] CreateAcbWithPreview ( string wavPath , byte [ ] sha1HashOfAwb , TimeSpan loopStart , TimeSpan loopEnd )
33+ public static async Task < byte [ ] > CreateAcbWithPreview ( string wavPath , byte [ ] awbBytes , TimeSpan loopStart , TimeSpan loopEnd )
3334 {
35+ using var sha1 = SHA1 . Create ( ) ;
36+ var sha1HashOfAwb = await sha1 . ComputeHashAsync ( new MemoryStream ( awbBytes ) ) ;
37+
3438 var waveReader = new WaveReader ( ) ;
3539 var audioData = waveReader . Read ( File . ReadAllBytes ( wavPath ) ) ;
3640 var format = audioData . GetFormat < Pcm16Format > ( ) ;
@@ -56,6 +60,16 @@ public static byte[] CreateAcbWithPreview(string wavPath, byte[] sha1HashOfAwb,
5660 waveFormTable . Rows [ 0 ] [ "NumSamples" ] = ( byte ) format . SampleCount ;
5761 criTable . Rows [ 0 ] [ "WaveformTable" ] = waveFormTable . Save ( ) ;
5862
63+ // 修复歌曲持续时间
64+ var count = BitConverter . ToInt32 ( awbBytes , 8 ) + 1 ;
65+ var headSize = 16 + awbBytes [ 5 ] * count + awbBytes [ 6 ] * count + awbBytes [ 7 ] * count ;
66+ var header = new byte [ headSize ] ;
67+ Array . Copy ( awbBytes , header , headSize ) ;
68+ var streamAwbAfs2Header = new CriTable ( ) ;
69+ streamAwbAfs2Header . Load ( criTable . Rows [ 0 ] [ "StreamAwbAfs2Header" ] as byte [ ] ) ;
70+ streamAwbAfs2Header . Rows [ 0 ] [ "Header" ] = header ;
71+ criTable . Rows [ 0 ] [ "StreamAwbAfs2Header" ] = streamAwbAfs2Header . Save ( ) ;
72+
5973 criTable . WriterSettings = CriTableWriterSettings . Adx2Settings ;
6074 return criTable . Save ( ) ;
6175 }
@@ -105,4 +119,4 @@ private static CriTable GetTable(this CriRow row, string name)
105119 table . Load ( row [ name ] as byte [ ] ) ;
106120 return table ;
107121 }
108- }
122+ }
0 commit comments