00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "pent_include.h"
00020
00021 #include "FlexWriter.h"
00022 #include "FlexFile.h"
00023 #include "IDataSource.h"
00024 #include "ODataSource.h"
00025
00026 DEFINE_RUNTIME_CLASSTYPE_CODE_BASE_CLASS(FlexWriter);
00027
00028 FlexWriter::FlexWriter(FlexFile * f)
00029 {
00030 if (f)
00031 {
00032 uint32 i;
00033 uint32 count = f->getIndexCount();
00034 for (i = 0; i < count; ++i)
00035 {
00036 uint32 size;
00037 uint8* data = f->getObject(i, &size);
00038 if (!data) size = 0;
00039 FlexObject o;
00040 o.size = size;
00041 o.obj = data;
00042 objects.push_back(o);
00043 }
00044 delete f;
00045 }
00046 }
00047
00048 FlexWriter::~FlexWriter()
00049 {
00050 std::vector<FlexObject>::iterator it;
00051 for (it = objects.begin(); it != objects.end(); ++it)
00052 {
00053 delete [] it->obj;
00054 }
00055 objects.clear();
00056 }
00057
00058 void FlexWriter::add_object(const uint8* obj, uint32 size)
00059 {
00060 FlexObject o;
00061 o.obj = new uint8[size];
00062 o.size = size;
00063 std::memcpy (o.obj, obj, size);
00064 objects.push_back(o);
00065 }
00066
00067 void FlexWriter::add_datasource(IDataSource* ds)
00068 {
00069 FlexObject o;
00070 o.size = ds->getSize();
00071 o.obj = new uint8[o.size];
00072 ds->seek(0);
00073 ds->read(o.obj, o.size);
00074 objects.push_back(o);
00075 delete ds;
00076 }
00077
00078 void FlexWriter::set_object(uint32 index, const uint8* obj, uint32 size)
00079 {
00080 if (index >= objects.size())
00081 {
00082 add_object(obj, size);
00083 }
00084 FlexObject * o = &objects.at(index);
00085
00086 delete o->obj;
00087 o->obj = new uint8[size];
00088 o->size = size;
00089 std::memcpy (o->obj, obj, size);
00090 }
00091
00092 void FlexWriter::set_datasource(uint32 index, IDataSource* ds)
00093 {
00094 if (index >= objects.size())
00095 {
00096 add_datasource(ds);
00097 }
00098 FlexObject * o = &objects.at(index);
00099
00100 delete o->obj;
00101 o->size = ds->getSize();
00102 o->obj = new uint8[o->size];
00103 ds->seek(0);
00104 ds->read(o->obj, o->size);
00105 delete ds;
00106 }
00107
00108 void FlexWriter::write(ODataSource* ds)
00109 {
00110 writeHead(ds);
00111 std::vector<FlexObject>::iterator it;
00112 for (it = objects.begin(); it != objects.end(); ++it)
00113 {
00114 if (it->obj && it->size != 0)
00115 ds->write(it->obj, it->size);
00116 }
00117 }
00118
00119 void FlexWriter::writeHead(ODataSource* ds)
00120 {
00121 uint32 i;
00122 ds->seek(0);
00123 for (i = 0; i < (0x50 / 4); i++)
00124 {
00125 ds->write4(0x1A1A1A1A);
00126 }
00127 ds->write4(0x00001A1A);
00128 ds->write4(objects.size());
00129
00130 ds->write4(0x00000001);
00131
00132 for (i = ds->getPos(); i < 0x80; i += 4)
00133 {
00134 ds->write4(0);
00135 }
00136
00137 ds->seek(0x80);
00138 i = objects.size() * 8 + 0x80;
00139 std::vector<FlexObject>::iterator it;
00140 for (it = objects.begin(); it != objects.end(); ++it)
00141 {
00142 if (!it->obj || it->size == 0)
00143 {
00144 ds->write4(0);
00145 }
00146 else
00147 {
00148 ds->write4(i);
00149 }
00150 ds->write4(it->size);
00151 i += it->size;
00152 }
00153
00154
00155 ds->seek(0x5c);
00156 ds->write4(i);
00157
00158 i = objects.size() * 8 + 0x80;
00159 ds->seek(i);
00160 }