00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "Core/precomp.h"
00020 #include <API/Core/Resources/resourcetype_raw.h>
00021 #include <API/Core/System/cl_assert.h>
00022
00023
00024 CL_Res_Raw::CL_Res_Raw() : CL_ResourceType("raw")
00025 {
00026 }
00027
00028 CL_Resource *CL_Res_Raw::create_from_location(
00029 std::string name,
00030 std::string location,
00031 CL_ResourceOptions *options,
00032 CL_ResourceManager *parent)
00033 {
00034 bool is_raw_type = false;
00035 if (options->exists("type"))
00036 {
00037 if (options->get_value("type") != "raw") return NULL;
00038 is_raw_type=true;
00039 }
00040
00041 if (is_raw_type || options->exists("raw"))
00042 {
00043 return new CL_RawResource(
00044 name,
00045 location,
00046 options,
00047 parent);
00048 }
00049
00050 return NULL;
00051 }
00052
00053 CL_Resource *CL_Res_Raw::create_from_serialization(
00054 std::string name,
00055 CL_ResourceManager *parent)
00056 {
00057 return new CL_RawResource(name, parent);
00058 }
00059
00060 std::vector<unsigned char> *CL_Res_Raw::load(
00061 std::string name,
00062 CL_ResourceManager *manager)
00063 {
00064 CL_RawResource *res = (CL_RawResource *) manager->get_resource(name);
00065 return res->get_value();
00066 }
00067
00068
00069 CL_RawResource::CL_RawResource(
00070 std::string name,
00071 std::string location,
00072 CL_ResourceOptions *options,
00073 CL_ResourceManager *parent)
00074 : CL_Resource("raw", 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_RawResource::CL_RawResource(
00085 std::string name,
00086 CL_ResourceManager *parent)
00087 : CL_Resource("raw", 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_RawResource::serialize_save(CL_OutputSource *output)
00098 {
00099 load_count++;
00100 if (value != NULL) return;
00101
00102 if (from_datafile) load_from_datafile();
00103 else load_from_file();
00104
00105 for (
00106 std::vector<unsigned char>::iterator it=value->begin();
00107 it != value->end();
00108 it++)
00109 {
00110 output->write_uchar8(*it);
00111 }
00112
00113 unload();
00114 }
00115
00116 void CL_RawResource::load()
00117 {
00118 load_count++;
00119 if (value != NULL) return;
00120
00121 if (from_datafile) load_from_datafile();
00122 else load_from_file();
00123 }
00124
00125 void CL_RawResource::unload()
00126 {
00127 load_count--;
00128 if (load_count == 0)
00129 {
00130 delete value;
00131 value = NULL;
00132 }
00133 }
00134
00135 std::vector<unsigned char> *CL_RawResource::get_value()
00136 {
00137 load();
00138 return value;
00139 }
00140
00141 void CL_RawResource::load_from_datafile()
00142 {
00143 value=new std::vector<unsigned char>;
00144
00145 CL_InputSourceProvider *provider=parent->get_resource_provider();
00146 cl_assert ( provider != NULL );
00147 CL_InputSource *input=provider->open_source(get_name().c_str());
00148 cl_assert ( input != NULL );
00149
00150 unsigned int size=input->size();
00151 for (unsigned int i = 0; i < size; ++i)
00152 value->push_back (input->read_uchar8());
00153
00154 delete input;
00155 }
00156
00157 void CL_RawResource::load_from_file()
00158 {
00159 value=new std::vector<unsigned char>;
00160
00161 CL_InputSourceProvider *provider=CL_InputSourceProvider::create_file_provider(".");
00162 cl_assert ( provider != NULL );
00163
00164 CL_InputSource *input=provider->open_source(location);
00165 cl_assert ( input != NULL );
00166
00167 unsigned int size=input->size();
00168 for (unsigned int i = 0; i < size; ++i)
00169 value->push_back (input->read_uchar8());
00170
00171 delete input;
00172 delete provider;
00173 }
00174
00175
00176
00177