Sometimes, I'm an idiot.
It looks like importing/exporting the files as XML (or JSON, or whatever) might be fairly simple.
Here's the basic plan.
An ESM file is made up of a list of objects, of various types. (e.g. Cell, NPC, Creature, etc.)
The OpenMW code base has a collection of structs that represent each of these objects.
(The structs can be found in
https://github.com/OpenMW/openmw/tree/m ... onents/esm)
Each struct has a function called load() that reads the structure from the ESM file and save() which writes the structure to an ESM file.
Here's an example
Code: Select all
void Creature::save(ESMWriter &esm) const
{
esm.writeHNCString("MODL", mModel);
esm.writeHNOCString("CNAM", mOriginal);
esm.writeHNOCString("FNAM", mName);
esm.writeHNOCString("SCRI", mScript);
esm.writeHNT("NPDT", mData, 96);
esm.writeHNT("FLAG", mFlags);
if (mScale != 1.0) {
esm.writeHNT("XSCL", mScale);
}
mInventory.save(esm);
mSpells.save(esm);
if (mHasAI) {
esm.writeHNT("AIDT", mAiData, sizeof(mAiData));
}
mTransport.save(esm);
mAiPackage.save(esm);
}
Note: the save() function doesn't do the writing of the file itself.
It just tells the passed in ESMWriter what data needs to be saved, and the ESMWriter takes care of writing the data to the file in the correct format.
(The load() function uses a similar ESMReader.)
So, in principle to read/write as XML, you'd just need to modify the ESMWriter and ESMReader to emit XML.
And the ESMReader and ESMWriter are actually quite small.
Of cource, the better solution so that you could read and write both the ESM Native format and XML goes as follows.
Make the ESMReader and ESMWriter classes abstract base classes.
Create new classes ESMReaderNative and ESMWriterNative that derive from the base classes and have the exiting functionality.
Create two new classes ESMReaderXML and ESMWriterXML that also derive from the base class, but they read and write to XML.
Then in the code when you pick export, if you want to read a native ESM, you create a ESMReaderNative and pass it to the structs.
If you want to read an XML ESM, then you create a ESMReaderXML and pass it to the structs instead.
The load() and save() functions in the structs don't need to change.
And yes, to the programmers out there, I realize I've just described polymorphism.
Like I said. Sometimes I'm and idiot.