NeL Files and Serialisation

Introduction

This is really quite a difficult subject to write about - so this file is an introduction which describes the basic features and principles of our system.

How files work in NeL

The NeL files are NOT designed to be man-readable. Interpretation and generation of file contents is performed by the objects that are to be read and written using a standardised mechanism. This mechanism was inspired by the system provided by Java.

We use the term 'serialisable' to describe a class that can be read from/ written to a NeL data file.
Counter-intuitive as it may, at first, appear, each 'serialisable' class supplies a single method that is used for both reading and writing.

Note that the files are encoded in little-endian and that the NeL library code deals with conversion of endian-ness for big-endian platforms.

Serialisation beyond files

The serialisation system can be used for generating binary data buffers in memory (without writing the result to a file) or for packing and unpacking data for transfer over a LAN.

How it works

Technically, we define a 'serialisable' class as a class that can be passed to IStream::serial().

In order for a class to be serialisable it is sufficient for it to include the following method:

1void serial(IStream&)

The fact that we use a template method definition means that a serialisable class does not have to be derived from any other class.

All standard types are serialisable due to a non-template prototypes shown below.
STL containers of serialisable types are serialisable.
Pointers to non-polymorphic serialisable types are serialisable.

The IStream class definition looks something like this:

 1class IStream
 2{
 3...
 4    void serial (int&);
 5    void serial (float&);
 6...
 7    template <class T> void serial (T&t)
 8    {
 9        t.serial (*this);
10    }
11};

Example:

To make the following class serialisable:

1class CMyFirstClass
2{
3    uint32 a, b;
4};

you would need to extend the class as follows:

 1class CMyFirstClass
 2{
 3    uint23 a, b;
 4    void serial (IStream &istream)
 5    {
 6        istream.serial(a);
 7        istream.serial(b);
 8    }
 9};

The following example shows how to serialise a more complicated data structure:

 1class CMyFirstClass
 2{
 3    void serial (IStream &);
 4};
 5
 6class CMyclass
 7{
 8    uint32                    BaseType;
 9    CMyFirstClass                SerialisableClass
10    std::vector< myFirstClass>        STLContainerOfSerialisableClass;
11    CMyFirstClass                *PointerToSerialisableClass;
12    std::vector< myFirstClass*>        STLContainerOfPointersToSerialisableClass;
13
14    void serial (IStream &istream)
15    {
16        istream.serial(BaseType);
17        istream.serial(SerialisableClass);
18        istream.serialCont(STLContainerOfSerialisableClass);
19        istream.serialPtr(PointerToSerialisableClass);
20        istream.serialContPtr(STLContainerOfPointersToSerialisableClass);
21    }
22};

Dealing with cross referenced or hierarchical data

If an object contains a pointer to another object in memory then the serialPtr() method is used to read/write the referenced object.

The NeL library code writes a value corresponding to the pointer to the serialised data, followed by the data that the pointer points to (In the case of a NULL pointer the value 0 is written without any following data).

The NeL library code automatically deals with the cases where two or more objects reference the same object or there is a circular reference. Each time a pointer is de-referenced, for writing, NeL checks against a table of previous pointers; if the pointer value already exists in the table then no data is written. At read time the data structures are faithfully reconstructed.

Dealing with polymorphism within cross referenced data

In a nut shell, in order to un-serialise a data record that one only has an interface type for, one needs to store an additional identifier with the data record that identifies its real type. The mechanism for doing this is best shown with an example:

 1class IBaseClass : public IStreamable
 2{
 3    // This class is an interface. It is polymorphic.
 4    virtual void foo () = 0;
 5
 6    // It must declare its name
 7    NLMISC_DECLARE_CLASS(MyClass);
 8};
 9
10class CClassToSerialise
11{
12    IBaseClass *PointerToAPolymorphicClass;
13
14    void serial (IStream& s)
15    {
16        s.serialPolyPtr (PointerToAPolymorphicClass);
17    }
18};
19
20void main ()
21{
22    ...
23    // The polymorphic class must be registered in the registry
24    NLMISC_REGISTER_CLASS (MyClass);
25    ...
26}

Dealing with file format evolution

 1void serial (IStream &s)
 2{
 3    // At the beginning of the serial process, read/write the version number of the class implementation
 4
 5    // In the following example - at read time 'version' contains the version read from the stream. At
 6    // write time version code '3' is written to the stream and to the variable 'version'.
 7    uint32 version = s.serialVersion (3);
 8
 9    // Now switch the version
10    switch (version)
11    {
12    case 3:
13        // The last field added in the class
14        s.serial (LastField);
15
16        // do some different stuff at read time and write time
17        if (s.isReading())
18        {
19            // at read time
20            ...
21        }
22        else
23        {
24            // at write time
25            ...
26        }
27
28    case 2:
29        // note that the code provided as of here allows for the reading of old versions of the class
30        s.serial (Toto);
31
32        // in the case where the evolution from my version 1 implementation to my version 2
33        // is not simply an extension of version 1 we need to break execution here
34        break;
35
36    case 1:
37        s.serial (Foo);
38    case 0:
39        s.serial (Truc);
40    }
41}

NeL File Headers

The objective of NeL file headers is to verify that a file is in the right format before attempting to interpret the contents.

 1// The NeL team use the following advise serialise a file this way:
 2void CFileRootClass::serial (IStream &s)
 3{
 4    // First write / read-check the header
 5    s.serialCheck ((uint32)'_LEN');
 6    s.serialCheck ((uint32)'HSEM');
 7
 8    // This code write / read-check the header 'NEL_MESH' at the beginning of the file.
 9    // If the check fails, serialCheck throws the EInvalidDataStream exception.
10}

Good examples to look at:

include/nel/misc/stream.h            // Stream base classes
class CTileBank in src/3d/tile_bank.cpp    // Good example of file format evolution
class CAnimation in src/3d/animation.cpp    // Good example of polymorphism