1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_MemorySoundArchive.cpp
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: $
16  *---------------------------------------------------------------------------*/
17 
18 #include "precompiled.h"
19 
20 #include <nw/snd/snd_MemorySoundArchive.h>
21 
22 namespace nw {
23 namespace snd {
24 
25 /*---------------------------------------------------------------------------*
26   Name:         MemorySoundArchive
27 
28   Description:  コンストラクタ
29 
30   Arguments:    None.
31 
32   Returns:      None.
33  *---------------------------------------------------------------------------*/
MemorySoundArchive()34 MemorySoundArchive::MemorySoundArchive()
35 : m_pData( NULL )
36 {
37 }
38 
39 /*---------------------------------------------------------------------------*
40   Name:         ~MemorySoundArchive
41 
42   Description:  デストラクタ
43 
44   Arguments:    None.
45 
46   Returns:      None.
47  *---------------------------------------------------------------------------*/
~MemorySoundArchive()48 MemorySoundArchive::~MemorySoundArchive()
49 {
50 }
51 
52 /*---------------------------------------------------------------------------*
53   Name:         Initialize
54 
55   Description:  サウンドアーカイブを初期化する
56 
57   Arguments:    soundArchiveData - サウンドアーカイブデータ
58 
59   Returns:      成功したかどうか
60  *---------------------------------------------------------------------------*/
Initialize(const void * soundArchiveData)61 bool MemorySoundArchive::Initialize( const void* soundArchiveData )
62 {
63     NW_NULL_ASSERT( soundArchiveData );
64     NW_ALIGN4_ASSERT( soundArchiveData );
65 
66     m_FileReader.Initialize( soundArchiveData );
67     SoundArchive::Initialize( &m_FileReader );
68 
69     const unsigned long infoBlockOffset = m_FileReader.GetInfoBlockOffset();
70     const unsigned long infoBlockSize = m_FileReader.GetInfoBlockSize();
71 
72     m_FileReader.SetInfoBlock(
73         ut::AddOffsetToPtr( soundArchiveData, infoBlockOffset )
74     );
75 
76     const unsigned long stringBlockOffset = m_FileReader.GetStringBlockOffset();
77     const unsigned long stringBlockSize = m_FileReader.GetStringBlockSize();
78 
79     // 文字列ブロックへのリファレンスは必ず含まれるが、
80     // ブロック自体が含まれない場合、stringBlockOffset/stringBlockSize の両方共、
81     // 0xffffffff が入っている。
82     if ( stringBlockOffset == 0xffffffff || stringBlockSize == 0xffffffff )
83     {
84         // なにもしない
85     }
86     else
87     {
88         m_FileReader.SetStringBlock(
89                 ut::AddOffsetToPtr( soundArchiveData, stringBlockOffset ));
90     }
91 
92     m_pData = soundArchiveData;
93 
94     return true;
95 }
96 
97 /*---------------------------------------------------------------------------*
98   Name:         Finalize
99 
100   Description:  サウンドアーカイブを閉じる
101 
102   Arguments:    None.
103 
104   Returns:      None.
105  *---------------------------------------------------------------------------*/
Finalize()106 void MemorySoundArchive::Finalize()
107 {
108     m_pData = NULL;
109     m_FileReader.Finalize();
110     SoundArchive::Finalize();
111 }
112 
detail_GetFileAddress(FileId fileId) const113 const void* MemorySoundArchive::detail_GetFileAddress( FileId fileId ) const
114 {
115 #if 0
116     // ファイル位置情報取得
117     FilePos filePos;
118     if ( ! detail_ReadFilePos( fileId, 0, &filePos ) )
119     {
120         return NULL;
121     }
122 
123     // グループ情報取得
124     GroupInfo groupInfo;
125     if ( ! detail_ReadGroupInfo( filePos.groupId, &groupInfo ) )
126     {
127         return NULL;
128     }
129     GroupItemInfo itemInfo;
130     if ( ! detail_ReadGroupItemInfo( filePos.groupId, filePos.index, &itemInfo ) )
131     {
132         return NULL;
133     }
134     if ( groupInfo.extFilePath != NULL )
135     {
136         // 外部参照グループ
137         return NULL;
138     }
139 
140     const u32 itemOffset = groupInfo.offset + itemInfo.offset;
141 
142     return ut::AddOffsetToPtr( m_pData, itemOffset );
143 #else
144     FileInfo fileInfo;
145     if ( ! detail_ReadFileInfo( fileId, &fileInfo ) )
146     {
147         return NULL;
148     }
149 
150     // 外部参照ファイルは対応しない (?)
151 
152     return ut::AddOffsetToPtr( m_pData,
153             fileInfo.offsetFromFileBlockHead +
154             m_FileReader.GetFileBlockOffset() );
155 #endif
156 }
157 
OpenStream(void * buffer,int size,u32 begin,u32 length)158 io::FileStream* MemorySoundArchive::OpenStream( void* buffer, int size, u32 begin, u32 length )
159 {
160     if ( m_pData == NULL ) return NULL;
161     if ( size < sizeof( MemoryFileStream ) ) return NULL;
162     MemoryFileStream* stream = new( buffer ) MemoryFileStream(
163         ut::AddOffsetToPtr( m_pData, begin ), length );
164     return stream;
165 }
166 
OpenExtStream(void * buffer,int size,const char * extFilePath,u32 begin,u32 length) const167 io::FileStream* MemorySoundArchive::OpenExtStream(
168         void* buffer,
169         int size,
170         const char* extFilePath,
171         // const wchar_t* extFilePath,
172         u32 begin,
173         u32 length ) const
174 {
175     (void)buffer;
176     (void)size;
177     (void)extFilePath;
178     (void)begin;
179     (void)length;
180 
181     NW_WARNING( false, "Cannot OpenExtStream for MemorySoundArchive\n");
182 
183     return NULL;
184 }
185 
detail_GetRequiredStreamBufferSize() const186 size_t MemorySoundArchive::detail_GetRequiredStreamBufferSize() const
187 {
188     return sizeof( MemoryFileStream );
189 }
190 
MemoryFileStream(const void * buffer,u32 size)191 MemorySoundArchive::MemoryFileStream::MemoryFileStream( const void* buffer, u32 size )
192 : m_pBuffer( buffer ),
193   m_Size( size ),
194   m_Position( 0 )
195 {
196 }
197 
Close()198 void MemorySoundArchive::MemoryFileStream::Close()
199 {
200     m_pBuffer = NULL;
201     m_Size = 0;
202     m_Position = 0;
203 }
204 
Read(void * buf,u32 length)205 s32 MemorySoundArchive::MemoryFileStream::Read( void* buf, u32 length )
206 {
207     u32 readLen = ut::Min( length, m_Size - m_Position );
208     std::memcpy( buf, ut::AddOffsetToPtr( m_pBuffer, m_Position ), readLen );
209     return static_cast<s32>(readLen);
210 }
211 
Seek(s32 offset,u32 origin)212 void MemorySoundArchive::MemoryFileStream::Seek( s32 offset, u32 origin )
213 {
214     switch( origin ) {
215     case io::FILE_STREAM_SEEK_BEGIN:
216         m_Position = static_cast<u32>(offset);
217         break;
218 
219     case io::FILE_STREAM_SEEK_CURRENT:
220         m_Position += offset;
221         break;
222 
223     case io::FILE_STREAM_SEEK_END:
224         m_Position = m_Size - offset;
225         break;
226 
227     default:
228         NW_ASSERTMSG( false, "Unsupported Seek origin" );
229         return;
230     }
231 }
232 
233 } // namespace nw::snd
234 } // namespace nw
235 
236