| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package uk.me.parabola.imgfmt.app.net; |
| 18 |
|
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.List; |
| 21 |
|
| 22 |
import uk.me.parabola.imgfmt.app.BufferedImgFileReader; |
| 23 |
import uk.me.parabola.imgfmt.app.BufferedImgFileWriter; |
| 24 |
import uk.me.parabola.imgfmt.app.ImgFile; |
| 25 |
import uk.me.parabola.imgfmt.app.ImgFileWriter; |
| 26 |
import uk.me.parabola.imgfmt.app.Section; |
| 27 |
import uk.me.parabola.imgfmt.app.SectionWriter; |
| 28 |
import uk.me.parabola.imgfmt.fs.ImgChannel; |
| 29 |
import uk.me.parabola.log.Logger; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class NODFile extends ImgFile { |
| 51 |
private static final Logger log = Logger.getLogger(NODFile.class); |
| 52 |
|
| 53 |
private final NODHeader nodHeader = new NODHeader(); |
| 54 |
|
| 55 |
private List<RouteCenter> centers = new ArrayList<>(); |
| 56 |
private List<RoadDef> roads = new ArrayList<>(); |
| 57 |
private List<RouteNode> boundaryNodes = new ArrayList<>(); |
| 58 |
|
| 59 |
public NODFile(ImgChannel chan, boolean write) { |
| 60 |
setHeader(nodHeader); |
| 61 |
if (write) { |
| 62 |
setWriter(new BufferedImgFileWriter(chan, "NOD")); |
| 63 |
position(NODHeader.HEADER_LEN); |
| 64 |
} else { |
| 65 |
setReader(new BufferedImgFileReader(chan)); |
| 66 |
nodHeader.readHeader(getReader()); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public void write() { |
| 71 |
writeNodes(); |
| 72 |
writeRoadData(); |
| 73 |
writeBoundary(); |
| 74 |
writeHighClassBoundary(); |
| 75 |
} |
| 76 |
|
| 77 |
public void writePost() { |
| 78 |
ImgFileWriter writer = new SectionWriter(getWriter(), nodHeader.getNodeSection()); |
| 79 |
|
| 80 |
for (RouteCenter rc : centers) { |
| 81 |
rc.writePost(writer); |
| 82 |
} |
| 83 |
|
| 84 |
position(0); |
| 85 |
getHeader().writeHeader(getWriter()); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private void writeNodes() { |
| 93 |
ImgFileWriter writer = getWriter(); |
| 94 |
nodHeader.setNodeStart(writer.position()); |
| 95 |
|
| 96 |
Section section = nodHeader.getNodeSection(); |
| 97 |
writer = new SectionWriter(writer, section); |
| 98 |
|
| 99 |
int[] classBoundaries = nodHeader.getClassBoundaries(); |
| 100 |
for (RouteCenter cp : centers){ |
| 101 |
cp.write(writer, classBoundaries); |
| 102 |
} |
| 103 |
for (int i = 4; i >= 0; --i){ |
| 104 |
if (classBoundaries[i] > writer.position()) |
| 105 |
classBoundaries[i] = writer.position(); |
| 106 |
} |
| 107 |
nodHeader.setNodeSize(writer.position()); |
| 108 |
log.debug("the nod offset", Integer.toHexString(getWriter().position())); |
| 109 |
Section.close(writer); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
private void writeRoadData() { |
| 116 |
log.info("writeRoadData"); |
| 117 |
|
| 118 |
ImgFileWriter writer = new SectionWriter(getWriter(), nodHeader.getRoadSection()); |
| 119 |
|
| 120 |
boolean debug = log.isDebugEnabled(); |
| 121 |
for (RoadDef rd : roads) { |
| 122 |
if(debug) |
| 123 |
log.debug("wrting nod2", writer.position()); |
| 124 |
rd.writeNod2(writer); |
| 125 |
} |
| 126 |
if(debug) |
| 127 |
log.debug("ending nod2", writer.position()); |
| 128 |
nodHeader.setRoadSize(writer.position()); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
private void writeBoundary() { |
| 135 |
log.info("writeBoundary"); |
| 136 |
|
| 137 |
ImgFileWriter writer = new SectionWriter(getWriter(), nodHeader.getBoundarySection()); |
| 138 |
|
| 139 |
boolean debug = log.isDebugEnabled(); |
| 140 |
for (RouteNode node : boundaryNodes) { |
| 141 |
if(debug) |
| 142 |
log.debug("wrting nod3", writer.position()); |
| 143 |
node.writeNod3OrNod4(writer); |
| 144 |
} |
| 145 |
if(debug) |
| 146 |
log.debug("ending nod3", writer.position()); |
| 147 |
nodHeader.setBoundarySize(writer.position()); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
private void writeHighClassBoundary() { |
| 155 |
log.info("writeBoundary"); |
| 156 |
|
| 157 |
Section section = nodHeader.getHighClassBoundary(); |
| 158 |
int pos = section.getPosition(); |
| 159 |
pos = (pos + 0x200) & ~0x1ff; |
| 160 |
int numBytesToWrite = pos - section.getPosition(); |
| 161 |
for (int i = 0; i < numBytesToWrite; i++) |
| 162 |
getWriter().put1u(0); |
| 163 |
section.setPosition(pos); |
| 164 |
ImgFileWriter writer = new SectionWriter(getWriter(), section); |
| 165 |
|
| 166 |
boolean debug = log.isDebugEnabled(); |
| 167 |
for (RouteNode node : boundaryNodes) { |
| 168 |
if (node.getNodeClass() == 0) |
| 169 |
continue; |
| 170 |
if(debug) |
| 171 |
log.debug("wrting nod4", writer.position()); |
| 172 |
node.writeNod3OrNod4(writer); |
| 173 |
} |
| 174 |
if(debug) |
| 175 |
log.debug("ending nod4", writer.position()); |
| 176 |
nodHeader.setHighClassBoundarySize(writer.position()); |
| 177 |
} |
| 178 |
|
| 179 |
public void setNetwork(List<RouteCenter> centers, List<RoadDef> roads, List<RouteNode> boundary) { |
| 180 |
this.centers = centers; |
| 181 |
this.roads = roads; |
| 182 |
this.boundaryNodes = new ArrayList<>(boundary); |
| 183 |
this.boundaryNodes.sort(null); |
| 184 |
} |
| 185 |
|
| 186 |
public void setDriveOnLeft(boolean dol) { |
| 187 |
nodHeader.setDriveOnLeft(dol); |
| 188 |
} |
| 189 |
} |