Subversion Repositories display

Rev

Rev 251 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2009.
 *
 * This program is 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.
 */

package test.display;

import uk.me.parabola.imgfmt.app.ImgFileReader;

import java.io.PrintStream;

/**
 * Use to display the MDR 1 sub files.  They have a header containing pointers into
 * the sub file.
 */

public class Mdr1SubFileDisplay {
        private final ImgFileReader reader;
        private final PrintStream outStream;
        private final MdrDisplay.MapInfo mapInfo;

        public Mdr1SubFileDisplay(ImgFileReader reader, PrintStream out, MdrDisplay.MapInfo mapInfo) {
                this.reader = reader;
                outStream = out;
                this.mapInfo = mapInfo;
        }

        void print() {
                int suboff = mapInfo.offset;

                Displayer d = new Displayer(reader);

                d.setSectStart(suboff);
                d.setTitle("Sect 1, map " + mapInfo.mapIndex + ": " + mapInfo.mapid);

                reader.position(suboff);

                SectList subSectList = new SectList();

                int hl1 = d.charValue("Header len %d");

                int off = d.intValue("sub1 (mdr11) start 0x%x");
                int len = d.intValue("sub1 len %d");
                subSectList.add(new Section("sub1 (mdr11)", off, len));

                // This seems to have the same length as the previous
                off = d.intValue("sub2 (mdr10) start 0x%x");
                subSectList.add(new Section("sub2 (mdr10)", off, len));

                // Do the rest of the entries, all have start and length
                // ... BUT the length is a number of items
                for (int i = 14; i < hl1; i += 8) {
                        int ind = (i - 14)/8 + 3;
                        off = d.intValue("sub" + ind + "(" + subToMdr(ind) + ") start 0x%x");
                        len = d.intValue("sub" + ind + " len %d");
                        subSectList.add(new Section("sub "+ind + "(" + subToMdr(ind)+")", off, len));
                }

                d.print(outStream);

                subSectList.add(new Section("END OF SECTION", off+len, 0));
                subSectList.analyze(outStream);
                d.setTitle(null);

                int subsectNumber = 1;
                for (Section sect : subSectList) {
                        d.setTitle("Sect 1, map " + mapInfo.mapIndex + ": " + mapInfo.mapid + " sub " + subsectNumber);
                        print1(d, sect, subsectNumber++);
                        d.print(outStream);
                }
        }

        public static String subToMdr(int sub) {
                String[] subs = new String[] {
                                "n/a",
                                "mdr11", "mdr10", "mdr7", "mdr5",
                                "mdr6", "mdr20", "mdr21", "mdr22"
                };
                if (sub >= subs.length)
                        return "unk";
                return subs[sub];
        }

        private void print1(Displayer d, Section subsect, int subNumber) {
                long recsize = subsect.getRecordSize();
                if (recsize == 0)
                        recsize = 1;
                int len = (int) (subsect.getLen() * recsize);
                if (len <= 0) {
                        System.out.println("NO LENGTH!! " + subsect.getLen() + " " + recsize);
                        return;
                }

                long orig = reader.position();

                switch (subNumber) {
                default:
                        // Just dump it out
                        d.rawValue(len, "data for sub" + subNumber + " (" + subToMdr(subNumber) + ")");
                        break;
                }

                reader.position(orig);

                reader.position(orig + len);
        }
}