]> git.localhorst.tv Git - l2e.git/blobdiff - src/loader/Compiler.cpp
ref and load images in l2o files
[l2e.git] / src / loader / Compiler.cpp
index b583aabb298a46017fb87d23d1772e1e79fed12e..96c0cc1425b643a8068a7b5ff6c8e2ff3697069e 100644 (file)
@@ -27,8 +27,7 @@ using std::vector;
 namespace loader {
 
 Compiler::Compiler(const Interpreter &intp)
-: intp(intp)
-, cursor(0) {
+: intp(intp) {
        int headerSize(sizeof(ObjectFileHeader));
 
        fileHeader.exportsBegin = headerSize;
@@ -47,19 +46,23 @@ void Compiler::Write(iostream &out) {
        ReserveHeader(out);
        WriteObjects(out);
        WriteOwnStrings(out);
-       fileHeader.objectsEnd = cursor;
+       fileHeader.objectsEnd = out.tellp();
        Pad(out, 16);
-       fileHeader.arraysBegin = cursor;
+       fileHeader.arraysBegin = out.tellp();
        WriteArrays(out);
-       fileHeader.arraysEnd = cursor;
+       fileHeader.arraysEnd = out.tellp();
        out.seekp(0);
-       cursor = 0;
        WriteHeader(out);
        WriteExports(out);
        WriteExternals(out);
        out.seekg(fileHeader.objectsBegin);
        out.clear();
        Relocate(out);
+       fileHeader.imagesBegin = out.tellp();
+       WriteImages(out);
+       fileHeader.imagesEnd = out.tellp();
+       out.seekp(0);
+       WriteHeader(out);
 }
 
 void Compiler::ReserveHeader(ostream &out) {
@@ -79,21 +82,34 @@ void Compiler::WriteOwnStrings(ostream &out) {
                object.typeId = Interpreter::STRING_ID;
                object.size = i->size() + 1;
                Write(out, &object, sizeof(Object));
-               addressMap.insert(make_pair(i->c_str(), cursor));
+               addressMap.insert(make_pair(i->c_str(), out.tellp()));
                Write(out, i->c_str(), object.size);
        }
        for(vector<Interpreter::PostponedDefinition>::const_iterator
                        i(intp.PostponedDefinitions().begin()),
                        end(intp.PostponedDefinitions().end());
                        i != end; ++i) {
-               addressMap.insert(make_pair(
-                               i->identifier.c_str(), cursor));
                Object object;
                object.typeId = Interpreter::STRING_ID;
                object.size = i->identifier.size() + 1;
                Write(out, &object, sizeof(Object));
+               addressMap.insert(make_pair(
+                               i->identifier.c_str(), out.tellp()));
                Write(out, i->identifier.c_str(), object.size);
        }
+       for (std::map<std::string, SDL_Surface *>::const_iterator
+                       i(intp.Images().begin()), end(intp.Images().end());
+                       i != end; ++i) {
+               addressMap.insert(make_pair(
+                               i->second, 0));
+               Object object;
+               object.typeId = Interpreter::STRING_ID;
+               object.size = i->first.size() + 1;
+               Write(out, &object, sizeof(Object));
+               addressMap.insert(make_pair(
+                               i->first.c_str(), out.tellp()));
+               Write(out, i->first.c_str(), object.size);
+       }
 }
 
 void Compiler::WriteExports(ostream &out) {
@@ -130,6 +146,7 @@ void Compiler::WriteObjects(ostream &out) {
                        Object object;
                        PrepareObject(object, td, *j);
                        Write(out, &object, sizeof(Object));
+                       addressMap.insert(make_pair(*j, out.tellp()));
                        Write(out, *j, object.size);
                }
        }
@@ -145,11 +162,24 @@ void Compiler::WriteArrays(ostream &out) {
                array.size = i->size;
                array.ref = i->ref;
                Write(out, &array, sizeof(Array));
-               addressMap.insert(make_pair(i->data, cursor));
+               addressMap.insert(make_pair(i->data, out.tellp()));
                Write(out, i->data, array.size);
        }
 }
 
+void Compiler::WriteImages(ostream &out) {
+       for (std::map<unsigned int, void *>::const_iterator
+                       i(images.begin()), end(images.end());
+                       i != end; ++i) {
+               const string &path = intp.FindImage(
+                               reinterpret_cast<SDL_Surface *>(i->second));
+               Image img;
+               img.pathOffset = addressMap.at(path.c_str());
+               img.destOffset = i->first;
+               Write(out, &img, sizeof(Image));
+       }
+}
+
 
 void Compiler::PrepareExport(Export &exp, const string &str) {
        const Interpreter::ParsedDefinition &dfn
@@ -173,7 +203,6 @@ void Compiler::PrepareObject(
                Object &object,
                const TypeDescription &td,
                void *data) {
-       addressMap.insert(make_pair(data, cursor + sizeof(Object)));
        object.typeId = td.TypeId();
        switch (td.TypeId()) {
                case Interpreter::STRING_ID:
@@ -190,18 +219,13 @@ void Compiler::Relocate(iostream &out) {
        int bufferSize = TypeDescription::GetMaxSize();
        char *buffer = new char[bufferSize];
        for (;out && out.tellg() < fileHeader.objectsEnd;) {
-               // 20785
                Object object;
                out.read(reinterpret_cast<char *>(&object), sizeof(Object));
                const TypeDescription &td = TypeDescription::Get(object.typeId);
-               if (!td.NeedsLinking()) {
-                       out.seekg(object.size, iostream::cur);
-                       continue;
-               }
                unsigned int pos = out.tellg();
-               out.seekg(pos);
+
                out.read(buffer, object.size);
-               Relocate(buffer, td);
+               Relocate(pos, buffer, td);
                out.seekp(pos);
                out.write(buffer, object.size);
                out.seekg(out.tellp());
@@ -211,23 +235,22 @@ void Compiler::Relocate(iostream &out) {
        Array array;
        for (; out && out.tellg() < fileHeader.arraysEnd;) {
                out.read(reinterpret_cast<char *>(&array), sizeof(Array));
+               buffer = new char[array.size];
+               unsigned int pos = out.tellg();
+               out.read(buffer, array.size);
                if (array.ref) {
-                       buffer = new char[array.size];
-                       unsigned int pos = out.tellg();
-                       out.seekg(pos);
-                       out.read(buffer, array.size);
                        RelocateArray(buffer, array.size);
-                       out.seekp(pos);
-                       out.write(buffer, array.size);
-                       out.seekg(out.tellp());
-                       delete[] buffer;
                } else {
                        const TypeDescription &td = TypeDescription::Get(array.typeId);
-                       for (char *i = array.Data(), *end = array.Data() + array.size;
+                       for (char *i = buffer, *end = buffer + array.size;
                                        i < end; i += td.Size()) {
-                               Relocate(i, td);
+                               Relocate(pos + (i - buffer), i, td);
                        }
                }
+               out.seekp(pos);
+               out.write(buffer, array.size);
+               out.seekg(out.tellp());
+               delete[] buffer;
        }
 }
 
@@ -245,44 +268,52 @@ void Compiler::RelocateArray(char *array, int size) {
        }
 }
 
-void Compiler::Relocate(char *object, const TypeDescription &td) {
+void Compiler::Relocate(
+               unsigned int pos,
+               char *object,
+               const TypeDescription &td) {
        for (TypeDescription::FieldIterator
                        i(td.FieldsBegin()), end(td.FieldsEnd());
                        i != end; ++i) {
                const FieldDescription &fd = i->second;
-               if (!fd.IsAggregate() && !fd.IsReferenced()) {
-                       continue;
-               }
-               char **dest = reinterpret_cast<char **>(
-                               object + fd.Offset());
-               if (!(*dest)) {
-                       continue;
-               }
-               map<const void *, unsigned int>::const_iterator
-                               entry(addressMap.find(*dest));
-               if (entry == addressMap.end()) {
-                       throw runtime_error(string("unable to relocate field ")
-                                       + i->first + " in object of type " + td.TypeName());
+               if (fd.IsAggregate() || fd.IsReferenced()) {
+                       char **dest = reinterpret_cast<char **>(
+                                       object + fd.Offset());
+                       if (!(*dest)) {
+                               continue;
+                       }
+                       if (fd.TypeId() == Interpreter::IMAGE_ID) {
+                               images.insert(make_pair(
+                                               pos + fd.Offset(), *dest));
+                       }
+                       map<const void *, unsigned int>::const_iterator
+                                       entry(addressMap.find(*dest));
+                       if (entry == addressMap.end()) {
+                               throw runtime_error(string("unable to relocate field ")
+                                               + i->first + " in object of type " + td.TypeName());
+                       }
+                       unsigned int destOffset = entry->second;
+                       *dest = reinterpret_cast<char *>(destOffset);
+               } else {
+                       const TypeDescription &nestedType
+                                       = TypeDescription::Get(fd.TypeId());
+                       Relocate(pos + fd.Offset(), object + fd.Offset(), nestedType);
                }
-               unsigned int destOffset = entry->second;
-               *dest = reinterpret_cast<char *>(destOffset);
        }
 }
 
 
 void Compiler::Write(ostream &out, const void *data, int amount) {
        out.write(reinterpret_cast<const char *>(data), amount);
-       cursor += amount;
 }
 
 void Compiler::Pad(ostream &out, int to) {
-       Fill(out, Remaining(cursor, to));
+       Fill(out, Remaining(out.tellp(), to));
 }
 
 void Compiler::Fill(ostream &out, int count, char c) {
        for (int remaining(count); remaining > 0; --remaining) {
                out.put(c);
-               ++cursor;
        }
 }