/* OpenMW - The completely unofficial reimplementation of Morrowind Copyright (C) 2009 Nicolay Korslund WWW: http://openmw.sourceforge.net/ This file (cachewriter.d) is part of the OpenMW package. OpenMW is distributed as free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License version 3 along with this program. If not, see http://www.gnu.org/licenses/ . */ module terrain.cachewriter; import terrain.archive; import terrain.outbuffer; import std.stdio, std.stream, std.string; import terrain.myfile; import std.math2; import monster.util.string; import monster.vm.dbg; // Helper structs struct AlphaHolder { AlphaInfo info; // Actual pixel buffer ubyte[] buffer; } struct MeshHolder { MeshInfo info; // Actual buffers byte[] vertexBuffer; // Alpha maps (if any) AlphaHolder alphas[]; } // A struct that gathers all the relevant quad data in one place. struct QuadHolder { QuadInfo info; MeshHolder meshes[]; } struct CacheWriter { // Opens the main archive file for output void openFile(char[] fname) { mainFile = new File(fname, FileMode.OutNew); iname = fname ~ ".index"; buf = new OutBuffer; } void setParams(int mxLev, int alphSize) { maxLevel = mxLev; alphaSize = alphSize; vertBuf.length = maxLevel; } // Closes the main archive file and writes the index. void finish() { mainFile.close(); // Write the index file scope MyFile ofile = new MyFile(iname, FileMode.OutNew); // Header first ArchiveHeader head; head.magic = CACHE_MAGIC; head.quads = quadList.length; head.rootLevel = maxLevel; head.alphaSize = alphaSize; head.stringNum = stringList.length; head.stringSize = totalStringLength; ofile.dump(head); // Write the quads ofile.dumpArray(quadList); // String table next. We need to sort it in order of the indices // first. char[][] strVector; strVector.length = head.stringNum; foreach(char[] key, int value; stringList) strVector[value] = key; // Next, write the strings to file while we fill in the offset // list int[] offsets = new int[head.stringNum]; size_t curOffs = 0; for(int i=0; i