Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

resourcetype_module.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: resourcetype_module.cpp,v 1.1 2001/01/30 23:34:05 mbn Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 
00014         File purpose:
00015                 Module resource type
00016 
00017 */
00018 
00019 #include "Core/precomp.h"
00020 #include "resourcetype_module.h"
00021 #include "module_reader.h"
00022 #include <API/Core/System/cl_assert.h>
00023 
00024 CL_Res_Module::CL_Res_Module() : CL_ResourceType("module")
00025 {
00026 }
00027 
00028 CL_Resource *CL_Res_Module::create_from_location(
00029         std::string name,
00030         std::string location,
00031         CL_ResourceOptions *options,
00032         CL_ResourceManager *parent)
00033 {
00034         bool is_module_type = false;
00035         if (options->exists("type"))
00036         {
00037                 if (options->get_value("type") != "module") return NULL;
00038                 is_module_type=true;
00039         }
00040 
00041         if (is_module_type || options->exists("module"))
00042         {
00043                 return new CL_ModuleResource(
00044                         name,
00045                         location,
00046                         options,
00047                         parent);
00048         }
00049 
00050         return NULL;
00051 }
00052 
00053 CL_Resource *CL_Res_Module::create_from_serialization(
00054         std::string name,
00055         CL_ResourceManager *parent)
00056 {
00057         return new CL_ModuleResource(name, parent);
00058 }
00059 
00060 MODULE *CL_Res_Module::load(
00061         std::string name,
00062         CL_ResourceManager *manager)
00063 {
00064         CL_ModuleResource *res = (CL_ModuleResource *) manager->get_resource(name);
00065         return res->get_value();
00066 }
00067 
00068                                                        
00069 CL_ModuleResource::CL_ModuleResource(
00070         std::string name,
00071         std::string location,
00072         CL_ResourceOptions *options,
00073         CL_ResourceManager *parent)
00074         : CL_Resource("module", name)
00075 {
00076         this->location = location;
00077         this->parent = parent;
00078         from_datafile = false;
00079         load_count = 0;
00080 
00081         value = NULL;
00082 }
00083 
00084 CL_ModuleResource::CL_ModuleResource(
00085         std::string name,
00086         CL_ResourceManager *parent)
00087         : CL_Resource("module", name)
00088 {
00089         location = "";
00090         this->parent = parent;
00091         from_datafile = true;
00092         load_count = 0;
00093 
00094         value = NULL;
00095 }
00096 
00097 void CL_ModuleResource::serialize_save(CL_OutputSource *output)
00098 {
00099         load_count++;
00100         if (value != NULL)  return; // test if already loaded.
00101         
00102         /*
00103          * When we want to serialize the file, we do not load it in memory
00104          * with the standard loader. Instead we just do a plain copy from
00105          * the input to the input. This way we can garantee that the file
00106          * is correctly stored in the datafile for instance. And when we
00107          * want to load the file for good, we call the real loader.
00108          */
00109         if (from_datafile) 
00110         {
00111                 CL_InputSourceProvider *provider=parent->get_resource_provider();
00112                 cl_assert ( provider != NULL );
00113                 CL_InputSource *input=provider->open_source(get_name().c_str());
00114                 cl_assert ( input != NULL );
00115                 
00116                 unsigned int size=input->size();
00117                 for (unsigned int i = 0; i < size; ++i)
00118                         output->write_uchar8 (input->read_uchar8());
00119 
00120                 delete input;
00121         }
00122         else
00123         {
00124                 CL_InputSourceProvider *provider=CL_InputSourceProvider::create_file_provider(".");
00125                 cl_assert ( provider != NULL );
00126           
00127                 CL_InputSource *input=provider->open_source(location.c_str());
00128                 cl_assert ( input != NULL );
00129 
00130                 unsigned int size=input->size();
00131                 for (unsigned int i = 0; i < size; ++i)
00132                         output->write_uchar8 (input->read_uchar8());
00133 
00134                 delete input;
00135                 delete provider;
00136         }
00137 
00138         unload();
00139 }
00140 
00141 void CL_ModuleResource::load()
00142 {
00143         load_count++;
00144         if (value != NULL) return; // already loaded.
00145 
00146         if (from_datafile) load_from_datafile();
00147         else load_from_file();
00148 }
00149 
00150 void CL_ModuleResource::unload()
00151 {
00152         load_count--;
00153         if (load_count == 0)
00154         {
00155                 delete value;
00156                 value = NULL;
00157         }
00158 }
00159 
00160 MODULE *CL_ModuleResource::get_value()
00161 {
00162         load();
00163         return value;
00164 }
00165 
00166 void CL_ModuleResource::load_from_datafile()
00167 {
00168 
00169         CL_InputSourceProvider *provider=parent->get_resource_provider();
00170         cl_assert ( provider != NULL );
00171         CL_InputSource *input=provider->open_source(get_name().c_str());
00172         cl_assert ( input != NULL );
00173 
00174         MREADER *reader=new_clanlib_reader((void *) input);
00175         cl_assert ( reader != NULL );
00176 
00177         value = Player_LoadGeneric(reader,CLANLIB_READER_CHANNELS,0);
00178 
00179         if (value == NULL)
00180         {
00181                 throw CL_Error(MikMod_strerror(MikMod_errno));
00182         }
00183 
00184         delete_clanlib_reader(reader);
00185         delete input;
00186 }
00187 
00188 void CL_ModuleResource::load_from_file()
00189 {
00190         CL_InputSourceProvider *provider=CL_InputSourceProvider::create_file_provider(".");
00191         cl_assert ( provider != NULL );
00192         CL_InputSource *input=provider->open_source(location.c_str());
00193         cl_assert ( input != NULL );
00194 
00195         MREADER *reader=new_clanlib_reader((void *) input);
00196         cl_assert ( reader != NULL );
00197 
00198         value = Player_LoadGeneric(reader,CLANLIB_READER_CHANNELS,0);
00199 
00200         if (value == NULL)
00201         {
00202                 throw CL_Error(MikMod_strerror(MikMod_errno));
00203         }
00204 
00205         delete_clanlib_reader(reader);
00206         delete input;
00207         delete provider;         
00208 }
00209 
00210 
00211 

Generated at Wed Apr 4 19:54:03 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001