{
// Decode base 64
final byte[] data =
DatatypeConverter.parseBase64Binary(templateCode.trim());
final InputStream stream = new DynaByteInputStream(
new ByteArrayInputStream(data));
try
{
// Read complex settings
final int settings = stream.read();
final boolean hasSector = (settings & 1) == 1;
final int gameNid = (settings >> 1) & 7;
final Game game = GameFactory.getInstance().getGame(gameNid);
final Complex complex = new Complex(game);
// Read sector coordinates or sun power
if (hasSector)
{
final int x = stream.read();
final int y = stream.read();
complex.setSector(game.getSectorFactory().getSector(x, y));
}
else
{
final int percent = stream.read();
complex.setSuns(game.getSunFactory().getSun(percent));
}
int factoryId;
while ((factoryId = stream.read()) != 0)
{
final Factory factory =
game.getFactoryFactory().getFactory(factoryId);
if (factory.isMine())
{
final List<Integer> yields = new ArrayList<Integer>();
int yield;
while ((yield = stream.read()) != 0)
yields.add(yield - 1);
complex
.addFactory(new ComplexFactory(game, factory, yields));
}
else
{
final int quantity = stream.read();
complex.addFactory(new ComplexFactory(game, factory,
quantity, 0));
}
}
return complex;
}
finally
{
stream.close();
}
}
catch (final IOException e)
{
throw new TemplateCodeException(e.toString(), e);