Parcourir la source

Move class declaration and implementation to .cpp

Daniele Bartolini il y a 12 ans
Parent
commit
420f4d5dcc
3 fichiers modifiés avec 160 ajouts et 91 suppressions
  1. 99 56
      engine/resource/ArchiveBundle.cpp
  2. 6 3
      engine/resource/Bundle.h
  3. 55 32
      engine/resource/FileBundle.cpp

+ 99 - 56
engine/resource/ArchiveBundle.cpp

@@ -24,84 +24,127 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#include "ArchiveBundle.h"
+#include "Bundle.h"
 #include "Filesystem.h"
-#include "Resource.h"
-#include "DiskFile.h"
+#include "HeapAllocator.h"
 #include "Log.h"
 #include "Memory.h"
+#include "Resource.h"
+#include "Types.h"
 
 namespace crown
 {
 
-//-----------------------------------------------------------------------------
-ArchiveBundle::ArchiveBundle(Filesystem& fs) :
-	m_filesystem(fs),
-	m_archive_file(NULL),
-	m_entries_count(0),
-	m_entries(NULL)
-{
-	// FIXME Default archive name
-	m_archive_file = m_filesystem.open("disk", "archive.bin", FOM_READ);
-	
-	ArchiveHeader header;
-	
-	// Read the header of the archive
-	m_archive_file->read(&header, sizeof(ArchiveHeader));
-	
-	Log::d("Version: %d", header.version);
-	Log::d("Entries: %d", header.entries_count);
-	Log::d("Checksum: %d", header.checksum);
-	
-	// No need to initialize memory
-	m_entries = (ArchiveEntry*)m_allocator.allocate(header.entries_count * sizeof(ArchiveEntry));
-
-	m_entries_count = header.entries_count;
-
-	// Read the entries
-	m_archive_file->read(m_entries, m_entries_count * sizeof(ArchiveEntry));
-}
+/// Structure of the archive
+///
+/// [ArchiveHeader]
+/// [ArchiveEntry]
+/// [ArchiveEntry]
+/// ...
+/// [ArchiveEntry]
+/// [ResourceData]
+/// [ResourceData]
+/// ...
+/// [ResourceData]
+///
+/// A valid archive must always have at least the archive header,
+/// starting at byte 0 of the archive file.
+///
+/// Newer archive versions must be totally backward compatible
+/// across minor engine releases, in order to be able to use
+/// recent version of the engine with older game archives.
 
-//-----------------------------------------------------------------------------
-ArchiveBundle::~ArchiveBundle()
+/// Source of resources
+class ArchiveBundle : public Bundle
 {
-	if (m_archive_file != NULL)
+public:
+
+	//-----------------------------------------------------------------------------
+	ArchiveBundle(Filesystem& fs) :
+		m_filesystem(fs), m_archive_file(NULL), m_entries_count(0), m_entries(NULL)
 	{
-		m_filesystem.close(m_archive_file);
+		// FIXME Default archive name
+		m_archive_file = m_filesystem.open( "archive.bin", FOM_READ);
+		
+		ArchiveHeader header;
+		
+		// Read the header of the archive
+		m_archive_file->read(&header, sizeof(ArchiveHeader));
+		
+		Log::d("Version: %d", header.version);
+		Log::d("Entries: %d", header.entries_count);
+		Log::d("Checksum: %d", header.checksum);
+		
+		// No need to initialize memory
+		m_entries = (ArchiveEntry*)m_allocator.allocate(header.entries_count * sizeof(ArchiveEntry));
+
+		m_entries_count = header.entries_count;
+
+		// Read the entries
+		m_archive_file->read(m_entries, m_entries_count * sizeof(ArchiveEntry));
 	}
-	
-	if (m_entries != NULL)
+
+	//-----------------------------------------------------------------------------
+	~ArchiveBundle()
 	{
-		m_allocator.deallocate(m_entries);
+		if (m_archive_file != NULL)
+		{
+			m_filesystem.close(m_archive_file);
+		}
+		
+		if (m_entries != NULL)
+		{
+			m_allocator.deallocate(m_entries);
+		}
+		
+		m_entries = NULL;
+		m_entries_count = 0;
 	}
-	
-	m_entries = NULL;
-	m_entries_count = 0;
-}
 
-//-----------------------------------------------------------------------------
-File* ArchiveBundle::open(ResourceId name)
-{
-	// Search the resource in the archive
-	for (uint32_t i = 0; i < m_entries_count; i++)
-	{		
-		if (m_entries[i].name == name.name && m_entries[i].type == name.type)
-		{
-			// If found, seek to the first byte of the resource data
-			m_archive_file->seek(m_entries[i].offset);
+	//-----------------------------------------------------------------------------
+	File* open(ResourceId name)
+	{
+		// Search the resource in the archive
+		for (uint32_t i = 0; i < m_entries_count; i++)
+		{		
+			if (m_entries[i].name == name.name && m_entries[i].type == name.type)
+			{
+				// If found, seek to the first byte of the resource data
+				m_archive_file->seek(m_entries[i].offset);
 
-			return m_archive_file;
+				return m_archive_file;
+			}
 		}
+
+		return NULL;
 	}
 
-	return NULL;
+	//-----------------------------------------------------------------------------
+	void close(File* resource)
+	{
+		// Does nothing, the stream is automatically closed at exit.
+		(void)resource;
+	}
+
+private:
+
+	HeapAllocator	m_allocator;
+	Filesystem&		m_filesystem;
+	File*			m_archive_file;
+	uint32_t		m_entries_count;
+	ArchiveEntry*	m_entries;
+};
+
+//-----------------------------------------------------------------------------
+Bundle* create(Allocator& a, Filesystem& fs)
+{
+	return CE_NEW(a, ArchiveBundle)(fs);
 }
 
 //-----------------------------------------------------------------------------
-void ArchiveBundle::close(File* resource)
+void destroy(Allocator& a, Bundle* bundle)
 {
-	// Does nothing, the stream is automatically closed at exit.
-	(void)resource;
+	CE_DELETE(a, bundle);
 }
 
 } // namespace crown

+ 6 - 3
engine/resource/Bundle.h

@@ -56,7 +56,10 @@ class Bundle
 {
 public:
 
-	virtual					~Bundle() {}
+	static Bundle* create(Allocator& a, Filesystem& fs);
+	static void destroy(Allocator& a, Bundle* bundle);
+
+	virtual ~Bundle() {}
 
 	/// Opens the resource file containing @a name resource
 	/// and returns a stream from which read the data from.
@@ -64,10 +67,10 @@ public:
 	/// The resource stream points exactly at the start
 	/// of the useful resource data, so you do not have to
 	/// care about skipping headers, metadatas and so on.
-	virtual File*		open(ResourceId name) = 0;
+	virtual File* open(ResourceId name) = 0;
 
 	/// Closes the resource file.
-	virtual void		close(File* resource) = 0;
+	virtual void close(File* resource) = 0;
 };
 
 } // namespace crown

+ 55 - 32
engine/resource/FileBundle.cpp

@@ -25,58 +25,81 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include <stdio.h>
-#include "FileBundle.h"
+
+#include "Allocator.h"
+#include "Bundle.h"
 #include "Filesystem.h"
 #include "Resource.h"
-#include "DiskFile.h"
-#include "Log.h"
 #include "StringUtils.h" 
-#include "OS.h"
-#include "StringSetting.h"
+#include "Types.h"
 
 namespace crown
 {
 
-extern StringSetting g_default_mountpoint;
+const uint32_t	RESOURCE_MAGIC_NUMBER		= 0xCE010101;
+const uint32_t	RESOURCE_VERSION			= 2;
 
-//-----------------------------------------------------------------------------
-FileBundle::FileBundle(Filesystem& fs) :
-	m_filesystem(fs)
+/// Contains the header data common to all
+/// types of resources passing through the
+/// standard Compiler mechanics.
+struct ResourceHeader
 {
-}
+	uint32_t	magic;		// Magic number used to identify the file
+	uint32_t	version;	// Version of the compiler used to compile the resource
+	uint32_t	size;		// Size of the resource data _not_ including this header in bytes
+};
 
-//-----------------------------------------------------------------------------
-FileBundle::~FileBundle()
+class FileBundle : public Bundle
 {
-}
+public:
 
-//-----------------------------------------------------------------------------
-File* FileBundle::open(ResourceId name)
-{
-	// Convert name/type into strings
-	char resource_name[512];
-	snprintf(resource_name, 512, "%.8X%.8X", name.name, name.type);
-	
-	// Search the resource in the filesystem
-	bool exists = m_filesystem.exists(g_default_mountpoint.value(), resource_name);
-	CE_ASSERT(exists == true, "Resource does not exist: %s", resource_name);
+	//-----------------------------------------------------------------------------
+	FileBundle(Filesystem& fs) : m_filesystem(fs) {}
 
-	// Open the resource and check magic number/version
-	File* file = m_filesystem.open(g_default_mountpoint.value(), resource_name, FOM_READ);
+	//-----------------------------------------------------------------------------
+	File* open(ResourceId name)
+	{
+		// Convert name/type into strings
+		char resource_name[512];
+		snprintf(resource_name, 512, "%.8X%.8X", name.name, name.type);
+		
+		// Search the resource in the filesystem
+		// bool exists = m_filesystem.exists(resource_name);
+		// CE_ASSERT(exists == true, "Resource does not exist: %s", resource_name);
 
-	ResourceHeader header;
-	file->read(&header, sizeof(ResourceHeader));
+		// Open the resource and check magic number/version
+		File* file = m_filesystem.open(resource_name, FOM_READ);
 
-	CE_ASSERT(header.magic == RESOURCE_MAGIC_NUMBER, "Resource is not valid: %s", resource_name);
-	CE_ASSERT(header.version == RESOURCE_VERSION, "Resource version mismatch: %s", resource_name);
+		ResourceHeader header;
+		file->read(&header, sizeof(ResourceHeader));
 
-	return file;
+		CE_ASSERT(header.magic == RESOURCE_MAGIC_NUMBER, "Resource is not valid: %s", resource_name);
+		CE_ASSERT(header.version == RESOURCE_VERSION, "Resource version mismatch: %s", resource_name);
+
+		return file;
+	}
+
+	//-----------------------------------------------------------------------------
+	void close(File* resource)
+	{
+		m_filesystem.close(resource);
+	}
+
+private:
+
+	Filesystem& m_filesystem;
+};
+
+//-----------------------------------------------------------------------------
+Bundle* create(Allocator& a, Filesystem& fs)
+{
+	return CE_NEW(a, FileBundle)(fs);
 }
 
 //-----------------------------------------------------------------------------
-void FileBundle::close(File* resource)
+void destroy(Allocator& a, Bundle* bundle)
 {
-	m_filesystem.close(resource);
+	CE_DELETE(a, bundle);
 }
 
 } // namespace crown