|
|
@@ -35,153 +35,131 @@ namespace crown
|
|
|
{
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-Compiler::Compiler(const char* root_path, const char* dest_path, const char* resource, uint32_t type_expected, uint32_t seed) :
|
|
|
- m_name_hash(0),
|
|
|
- m_type_hash(0),
|
|
|
- m_seed(seed),
|
|
|
-
|
|
|
+Compiler::Compiler(const char* root_path, const char* dest_path, uint32_t type_expected) :
|
|
|
m_root_fs(root_path),
|
|
|
m_dest_fs(dest_path),
|
|
|
- m_src_file(NULL),
|
|
|
- m_dest_file(NULL),
|
|
|
+ m_type_expected(type_expected)
|
|
|
+{
|
|
|
+ memset(m_resource_name, 0, MAX_RESOURCE_NAME_LENGTH);
|
|
|
+}
|
|
|
|
|
|
- m_prepared(false),
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+Compiler::~Compiler()
|
|
|
+{
|
|
|
+}
|
|
|
|
|
|
- m_verbose(false)
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+size_t Compiler::compile(const char* resource, uint32_t name, uint32_t type)
|
|
|
{
|
|
|
- // Init structures
|
|
|
- memset(m_root_path, 0, MAX_RESOURCE_PATH_LENGTH);
|
|
|
- memset(m_dest_path, 0, MAX_RESOURCE_PATH_LENGTH);
|
|
|
- memset(m_resource, 0, MAX_RESOURCE_PATH_LENGTH);
|
|
|
+ string::strncpy(m_resource_name, resource, MAX_RESOURCE_NAME_LENGTH);
|
|
|
|
|
|
- memset(&m_compiled_header, 0, sizeof(CompiledHeader));
|
|
|
- memset(m_name, 0, MAX_RESOURCE_NAME_LENGTH);
|
|
|
- memset(m_type, 0, MAX_RESOURCE_TYPE_LENGTH);
|
|
|
+ char resource_name[MAX_RESOURCE_NAME_LENGTH];
|
|
|
+ char resource_type[MAX_RESOURCE_TYPE_LENGTH];
|
|
|
|
|
|
- string::strncpy(m_root_path, root_path, MAX_RESOURCE_PATH_LENGTH);
|
|
|
- string::strncpy(m_dest_path, dest_path, MAX_RESOURCE_PATH_LENGTH);
|
|
|
- string::strncpy(m_resource, resource, MAX_RESOURCE_PATH_LENGTH);
|
|
|
+ path::filename_without_extension(resource, resource_name, MAX_RESOURCE_NAME_LENGTH);
|
|
|
+ path::extension(resource, resource_type, MAX_RESOURCE_TYPE_LENGTH);
|
|
|
|
|
|
- // Extract resource name and type
|
|
|
- path::filename_without_extension(m_resource, m_name, MAX_RESOURCE_NAME_LENGTH);
|
|
|
- path::extension(m_resource, m_type, MAX_RESOURCE_TYPE_LENGTH);
|
|
|
+ char output_name[17];
|
|
|
+ snprintf(output_name, 17, "%.8X%.8X", name, type);
|
|
|
|
|
|
- // Compute the resource hashes
|
|
|
- m_name_hash = hash::murmur2_32(m_name, string::strlen(m_name), m_seed);
|
|
|
+ Log::i("%s => %s", resource, output_name);
|
|
|
|
|
|
- // NOTE: The type hash _MUST_ be generated with seed = 0
|
|
|
- m_type_hash = hash::murmur2_32(m_type, string::strlen(m_type), 0);
|
|
|
+ if (type != m_type_expected)
|
|
|
+ {
|
|
|
+ Log::e("'%s': resource type does not match expected type.", resource);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
|
|
|
- if (m_type_hash != type_expected)
|
|
|
+ if (!m_root_fs.exists(resource))
|
|
|
{
|
|
|
- Log::e("Compiler: Trying to compile '%s' with the wrong compiler. Aborting.", resource_path());
|
|
|
- exit(-1);
|
|
|
+ Log::e("'%s': resource does not exist.");
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
- char dest_name[17];
|
|
|
- memset(dest_name, 0, 17);
|
|
|
+ // Read source file
|
|
|
+ FileStream* input_file = m_root_fs.open(resource, SOM_READ);
|
|
|
|
|
|
- snprintf(dest_name, 17, "%.8X%.8X", m_name_hash, m_type_hash);
|
|
|
+ size_t header_size = read_header(input_file);
|
|
|
+ size_t resource_size = read_resource(input_file);
|
|
|
|
|
|
- // Open streams
|
|
|
- m_src_file = (FileStream*)m_root_fs.open(m_resource, SOM_READ);
|
|
|
+ // Write compiled file
|
|
|
+ FileStream* output_file;
|
|
|
|
|
|
- if (!m_dest_fs.exists(dest_name))
|
|
|
+ if (m_dest_fs.exists(output_name))
|
|
|
{
|
|
|
- m_dest_fs.create_file(dest_name);
|
|
|
+ m_dest_fs.delete_file(output_name);
|
|
|
}
|
|
|
|
|
|
- m_dest_file = (FileStream*)m_dest_fs.open(dest_name, SOM_WRITE);
|
|
|
-}
|
|
|
+ m_dest_fs.create_file(output_name);
|
|
|
+ output_file = m_dest_fs.open(output_name, SOM_WRITE);
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-Compiler::~Compiler()
|
|
|
-{
|
|
|
- if (m_src_file != NULL)
|
|
|
- {
|
|
|
- m_root_fs.close(m_src_file);
|
|
|
- }
|
|
|
+ write_header(output_file, name, type, resource_size);
|
|
|
+ write_resource(output_file);
|
|
|
|
|
|
- if (m_dest_file != NULL)
|
|
|
- {
|
|
|
- m_dest_fs.close(m_dest_file);
|
|
|
- }
|
|
|
-}
|
|
|
+ m_root_fs.close(input_file);
|
|
|
+ m_dest_fs.close(output_file);
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-const char* Compiler::root_path() const
|
|
|
-{
|
|
|
- return m_root_path;
|
|
|
+ // Cleanup
|
|
|
+ cleanup();
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-const char* Compiler::dest_path() const
|
|
|
+size_t Compiler::read_header(FileStream* in_file)
|
|
|
{
|
|
|
- return m_dest_path;
|
|
|
+ return read_header_impl(in_file);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-const char* Compiler::resource_path() const
|
|
|
+size_t Compiler::read_resource(FileStream* in_file)
|
|
|
{
|
|
|
- return m_resource;
|
|
|
+ return read_resource_impl(in_file);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-uint32_t Compiler::resource_name_hash() const
|
|
|
+void Compiler::write_header(FileStream* out_file, uint32_t name, uint32_t type, uint32_t resource_size)
|
|
|
{
|
|
|
- return m_name_hash;
|
|
|
-}
|
|
|
+ CompiledHeader header;
|
|
|
+ header.magic = COMPILED_HEADER_MAGIC_NUMBER;
|
|
|
+ header.version = COMPILER_VERSION;
|
|
|
+ header.name = name;
|
|
|
+ header.type = type;
|
|
|
+ header.size = resource_size;
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-uint32_t Compiler::resource_type_hash() const
|
|
|
-{
|
|
|
- return m_type_hash;
|
|
|
-}
|
|
|
+ out_file->write(&header, sizeof(CompiledHeader));
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-uint32_t Compiler::seed() const
|
|
|
-{
|
|
|
- return m_seed;
|
|
|
+ write_header_impl(out_file);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-const char* Compiler::resource_name() const
|
|
|
+void Compiler::write_resource(FileStream* out_file)
|
|
|
{
|
|
|
- return m_name;
|
|
|
+ write_resource_impl(out_file);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-const char* Compiler::resource_type() const
|
|
|
+void Compiler::cleanup()
|
|
|
{
|
|
|
- return m_type;
|
|
|
-}
|
|
|
+ cleanup_impl();
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-FileStream* Compiler::source_file()
|
|
|
-{
|
|
|
- return m_src_file;
|
|
|
+ string::strncpy(m_resource_name, "", MAX_RESOURCE_NAME_LENGTH);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-FileStream* Compiler::destination_file()
|
|
|
+const char* Compiler::root_path() const
|
|
|
{
|
|
|
- return m_dest_file;
|
|
|
+ return m_root_fs.root_path();
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-void Compiler::prepare_header(uint32_t size)
|
|
|
+const char* Compiler::dest_path() const
|
|
|
{
|
|
|
- m_compiled_header.magic = COMPILED_HEADER_MAGIC_NUMBER;
|
|
|
- m_compiled_header.version = COMPILER_VERSION;
|
|
|
- m_compiled_header.name = m_name_hash;
|
|
|
- m_compiled_header.type = m_type_hash;
|
|
|
- m_compiled_header.size = size;
|
|
|
+ return m_dest_fs.root_path();
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-void Compiler::write_header()
|
|
|
+const char* Compiler::resource_name() const
|
|
|
{
|
|
|
- m_dest_file->write(&m_compiled_header, sizeof(CompiledHeader));
|
|
|
+ return m_resource_name;
|
|
|
}
|
|
|
|
|
|
} // namespace crown
|