dmolony-DiskBrowser/src/com/bytezone/diskbrowser/wizardry/Wizardry4BootDisk.java

346 lines
12 KiB
Java
Raw Normal View History

2016-08-08 04:53:29 +00:00
package com.bytezone.diskbrowser.wizardry;
import java.util.ArrayList;
import java.util.List;
2016-08-09 04:15:44 +00:00
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
2016-08-14 08:41:19 +00:00
import com.bytezone.diskbrowser.applefile.AbstractFile;
2016-08-08 04:53:29 +00:00
import com.bytezone.diskbrowser.disk.AppleDisk;
2016-08-14 08:41:19 +00:00
import com.bytezone.diskbrowser.disk.DefaultAppleFileSource;
2016-08-08 04:53:29 +00:00
import com.bytezone.diskbrowser.disk.Disk;
2016-08-14 08:41:19 +00:00
import com.bytezone.diskbrowser.disk.DiskAddress;
2016-08-09 04:15:44 +00:00
import com.bytezone.diskbrowser.pascal.FileEntry;
2016-08-08 04:53:29 +00:00
import com.bytezone.diskbrowser.pascal.PascalDisk;
2016-09-01 04:01:47 +00:00
import com.bytezone.diskbrowser.utilities.HexFormatter;
2016-08-08 04:53:29 +00:00
import com.bytezone.diskbrowser.utilities.Utility;
2016-08-16 06:34:23 +00:00
import com.bytezone.diskbrowser.wizardry.Header.ScenarioData;
2016-08-08 04:53:29 +00:00
public class Wizardry4BootDisk extends PascalDisk
{
2016-08-16 06:34:23 +00:00
public Header scenarioHeader;
2016-09-19 05:18:10 +00:00
// private final List<AppleDisk> disks = new ArrayList<AppleDisk> ();
2016-08-09 04:15:44 +00:00
private Relocator relocator;
2016-08-16 06:34:23 +00:00
private MessageBlock messageBlock;
2016-08-18 06:35:54 +00:00
private Huffman huffman;
2016-08-19 09:57:29 +00:00
private final int version;
2016-08-08 04:53:29 +00:00
public Wizardry4BootDisk (AppleDisk[] dataDisks)
{
2016-08-09 04:15:44 +00:00
super (dataDisks[0]);
2016-08-19 09:57:29 +00:00
version = dataDisks.length == 6 ? 4 : dataDisks.length == 10 ? 5 : 0;
2016-08-09 04:15:44 +00:00
DefaultTreeModel model = (DefaultTreeModel) catalogTree.getModel ();
DefaultMutableTreeNode currentRoot = (DefaultMutableTreeNode) model.getRoot ();
2016-08-14 08:41:19 +00:00
// get the relocation table
2016-08-09 04:15:44 +00:00
DefaultMutableTreeNode relocNode = findNode (currentRoot, "SYSTEM.RELOC");
FileEntry fileEntry = (FileEntry) relocNode.getUserObject ();
if (fileEntry != null)
{
relocator =
new Relocator (fileEntry.getUniqueName (), fileEntry.getDataSource ().buffer);
2016-08-14 08:41:19 +00:00
relocator.createNewBuffer (dataDisks); // create new data buffer
2016-08-09 04:15:44 +00:00
fileEntry.setFile (relocator);
}
2016-08-09 09:09:11 +00:00
// reset the code segment so that it rebuilds itself from the new data
DefaultMutableTreeNode pascalNode = findNode (currentRoot, "SYSTEM.PASCAL");
fileEntry = (FileEntry) pascalNode.getUserObject ();
2016-08-14 08:41:19 +00:00
if (fileEntry != null)
{
2016-08-14 08:50:41 +00:00
fileEntry.setFile (null);
fileEntry.getDataSource ();
2016-08-14 08:41:19 +00:00
}
2016-08-23 14:14:04 +00:00
DefaultMutableTreeNode huffNode = findNode (currentRoot, "ASCII.HUFF");
fileEntry = (FileEntry) huffNode.getUserObject ();
if (fileEntry != null)
{
byte[] buffer = fileEntry.getDataSource ().buffer;
huffman = new Huffman ("Huffman tree", buffer);
fileEntry.setFile (huffman);
}
DefaultMutableTreeNode messagesNode = findNode (currentRoot, "ASCII.KRN");
fileEntry = (FileEntry) messagesNode.getUserObject ();
if (fileEntry != null)
{
messageBlock = new MessageBlock (fileEntry.getDataSource ().buffer, huffman);
fileEntry.setFile (messageBlock);
messagesNode.setAllowsChildren (true);
List<DiskAddress> blocks = fileEntry.getSectors ();
int count = 0;
for (MessageDataBlock mdb : messageBlock)
{
List<DiskAddress> messageBlocks = new ArrayList<DiskAddress> ();
messageBlocks.add (blocks.get (count++));
addToNode (mdb, messagesNode, messageBlocks);
}
}
2016-08-19 09:57:29 +00:00
if (version == 4)
2016-08-14 08:41:19 +00:00
{
2016-08-19 09:57:29 +00:00
DefaultMutableTreeNode scenarioNode = findNode (currentRoot, "SCENARIO.DATA");
2016-08-22 03:41:43 +00:00
fileEntry = (FileEntry) scenarioNode.getUserObject ();
if (fileEntry != null)
2016-08-19 09:57:29 +00:00
{
2016-08-22 03:41:43 +00:00
fileEntry.setFile (null);
scenarioNode.setAllowsChildren (true);
scenarioHeader = new Header (scenarioNode, this);
linkMazeLevels4 (scenarioNode, fileEntry);
2016-08-19 09:57:29 +00:00
}
2016-08-16 06:34:23 +00:00
}
2016-08-19 09:57:29 +00:00
else if (version == 5)
2016-08-16 06:34:23 +00:00
{
2016-08-19 09:57:29 +00:00
DefaultMutableTreeNode scenarioNode = findNode (currentRoot, "DRAGON.DATA");
2016-08-22 03:41:43 +00:00
fileEntry = (FileEntry) scenarioNode.getUserObject ();
if (fileEntry != null)
{
fileEntry.setFile (null);
scenarioNode.setAllowsChildren (true);
linkMazeLevels5 (scenarioNode, fileEntry);
2016-09-01 04:01:47 +00:00
linkBlock1 (scenarioNode, fileEntry);
linkOracle (scenarioNode, fileEntry);
linkBlock2 (scenarioNode, fileEntry);
2016-08-22 03:41:43 +00:00
}
2016-08-19 09:57:29 +00:00
}
2016-08-16 06:34:23 +00:00
2016-08-19 09:57:29 +00:00
if (version == 4)
{
DefaultMutableTreeNode monstersNode = findNode (currentRoot, "200.MONSTERS");
fileEntry = (FileEntry) monstersNode.getUserObject ();
if (fileEntry != null)
{
monstersNode.setAllowsChildren (true);
linkMonsterImages4 (monstersNode, fileEntry);
}
2016-08-16 06:34:23 +00:00
}
2016-08-22 03:41:43 +00:00
else if (version == 5)
{
DefaultMutableTreeNode monstersNode = findNode (currentRoot, "200.MONSTERS");
fileEntry = (FileEntry) monstersNode.getUserObject ();
if (fileEntry != null)
{
monstersNode.setAllowsChildren (true);
2016-09-18 10:06:26 +00:00
linkMonsterImages5 (monstersNode, fileEntry);
2016-08-22 03:41:43 +00:00
}
}
2016-08-16 06:34:23 +00:00
}
2016-08-14 08:41:19 +00:00
2016-09-18 10:06:26 +00:00
private void linkMonsterImages4 (DefaultMutableTreeNode monstersNode,
FileEntry fileEntry)
{
List<DiskAddress> pictureBlocks = fileEntry.getSectors ();
Wiz4Monsters w4monsters =
new Wiz4Monsters ("monsters", fileEntry.getDataSource ().buffer);
fileEntry.setFile (w4monsters);
2016-09-19 10:37:50 +00:00
2016-09-18 10:06:26 +00:00
int count = 0;
for (Wiz4Image image : w4monsters.images)
{
List<DiskAddress> monsterBlocks = new ArrayList<DiskAddress> ();
monsterBlocks.add (pictureBlocks.get (w4monsters.blocks.get (count++)));
addToNode (image, monstersNode, monsterBlocks);
}
}
private void linkMonsterImages5 (DefaultMutableTreeNode monstersNode,
FileEntry fileEntry)
{
2016-09-19 10:37:50 +00:00
List<DiskAddress> pictureBlocks = fileEntry.getSectors ();
2016-09-18 10:06:26 +00:00
Wiz5Monsters w5monsters =
new Wiz5Monsters ("monsters", fileEntry.getDataSource ().buffer);
fileEntry.setFile (w5monsters);
2016-09-19 10:37:50 +00:00
2016-09-19 09:09:41 +00:00
for (Wiz5Monsters.Monster monster : w5monsters)
2016-09-18 10:06:26 +00:00
{
List<DiskAddress> monsterBlocks = new ArrayList<DiskAddress> ();
2016-09-19 10:37:50 +00:00
for (Integer blockId : monster.getBlocks ())
monsterBlocks.add (pictureBlocks.get (blockId));
2016-09-19 05:18:10 +00:00
addToNode (monster.getImage (), monstersNode, monsterBlocks);
2016-09-18 10:06:26 +00:00
}
}
2016-08-19 09:57:29 +00:00
private void linkMazeLevels4 (DefaultMutableTreeNode scenarioNode, FileEntry fileEntry)
2016-08-16 06:34:23 +00:00
{
ScenarioData mazeData = scenarioHeader.data.get (Header.MAZE_AREA);
2016-08-14 08:41:19 +00:00
2016-08-16 06:34:23 +00:00
byte[] buffer = fileEntry.getDataSource ().buffer;
List<DiskAddress> blocks = fileEntry.getSectors ();
DefaultMutableTreeNode mazeNode = linkNode ("Maze", "Levels string", scenarioNode);
for (int i = 0; i < 15; i++)
2016-08-14 08:41:19 +00:00
{
2016-08-22 03:41:43 +00:00
byte[] level = new byte[0x380]; // 896
2016-08-19 09:57:29 +00:00
int offset = mazeData.dataOffset * 512 + i * 1024;
2016-08-22 03:41:43 +00:00
System.arraycopy (buffer, offset, level, 0, level.length);
2016-08-19 09:57:29 +00:00
2016-08-22 03:41:43 +00:00
List<DiskAddress> mazeBlocks = new ArrayList<DiskAddress> ();
int ptr = mazeData.dataOffset + i * 2;
mazeBlocks.add (blocks.get (ptr));
mazeBlocks.add (blocks.get (ptr + 1));
addToNode (new MazeLevel (level, i), mazeNode, mazeBlocks);
}
}
private void linkMazeLevels5 (DefaultMutableTreeNode scenarioNode, FileEntry fileEntry)
{
byte[] buffer = fileEntry.getDataSource ().buffer;
List<DiskAddress> blocks = fileEntry.getSectors ();
DefaultMutableTreeNode mazeNode = linkNode ("Maze", "Level 5 mazes", scenarioNode);
2016-09-01 04:01:47 +00:00
List<DiskAddress> allMazeBlocks = new ArrayList<DiskAddress> ();
2016-08-22 03:41:43 +00:00
2016-09-01 04:01:47 +00:00
int dataSize = 0x39A;
2016-08-23 05:20:04 +00:00
int base = 0x1800;
2016-08-22 03:41:43 +00:00
for (int i = 0; i < 8; i++)
{
2016-08-23 14:14:04 +00:00
int offset = base + i * 0x400;
2016-08-24 10:17:27 +00:00
byte[] data = new byte[0x800];
2016-09-01 04:01:47 +00:00
System.arraycopy (buffer, offset, data, 0, dataSize);
System.arraycopy (buffer, offset + 0x2000, data, 0x400, dataSize);
2016-08-23 14:14:04 +00:00
MazeGridV5 grid = new MazeGridV5 ("Maze level " + (i + 1), data, messageBlock);
2016-08-22 03:41:43 +00:00
List<DiskAddress> mazeBlocks = new ArrayList<DiskAddress> ();
2016-09-01 04:01:47 +00:00
for (int j = 0; j < 4; j++)
mazeBlocks.add (blocks.get (12 + i * 4 + j));
allMazeBlocks.addAll (mazeBlocks);
2016-08-22 03:41:43 +00:00
addToNode (grid, mazeNode, mazeBlocks);
2016-08-16 06:34:23 +00:00
}
2016-09-01 04:01:47 +00:00
DefaultAppleFileSource afs = (DefaultAppleFileSource) mazeNode.getUserObject ();
afs.setSectors (allMazeBlocks);
}
private void linkBlock1 (DefaultMutableTreeNode scenarioNode, FileEntry fileEntry)
{
byte[] buffer = fileEntry.getDataSource ().buffer;
List<DiskAddress> blocks = fileEntry.getSectors ();
StringBuilder text = new StringBuilder ();
List<DiskAddress> allBlocks = new ArrayList<DiskAddress> ();
for (int i = 0; i < 23; i++)
{
allBlocks.add (blocks.get (44 + i));
}
int offset = 0x5800;
int length = 66;
for (int i = 0; i < 179; i++)
{
text.append (String.format ("%04X : %s%n", (offset + i * length),
HexFormatter.getHexString (buffer, offset + i * length, length)));
}
DefaultMutableTreeNode oracleNode =
linkNode ("Block1", text.toString (), scenarioNode);
oracleNode.setAllowsChildren (false);
DefaultAppleFileSource afs = (DefaultAppleFileSource) oracleNode.getUserObject ();
afs.setSectors (allBlocks);
}
private void linkBlock2 (DefaultMutableTreeNode scenarioNode, FileEntry fileEntry)
{
byte[] buffer = fileEntry.getDataSource ().buffer;
List<DiskAddress> blocks = fileEntry.getSectors ();
StringBuilder text = new StringBuilder ();
List<DiskAddress> allBlocks = new ArrayList<DiskAddress> ();
for (int i = 0; i < 19; i++)
{
allBlocks.add (blocks.get (87 + i));
}
int offset = 0xAE00;
int length = 60;
for (int i = 0; i < 150; i++)
{
text.append (String.format ("%04X : %s%n", (offset + i * length),
HexFormatter.getHexString (buffer, offset + i * length, length)));
}
DefaultMutableTreeNode oracleNode =
linkNode ("Block2", text.toString (), scenarioNode);
oracleNode.setAllowsChildren (false);
DefaultAppleFileSource afs = (DefaultAppleFileSource) oracleNode.getUserObject ();
afs.setSectors (allBlocks);
}
private void linkOracle (DefaultMutableTreeNode scenarioNode, FileEntry fileEntry)
{
byte[] buffer = fileEntry.getDataSource ().buffer;
List<DiskAddress> blocks = fileEntry.getSectors ();
StringBuilder text = new StringBuilder ();
for (int i = 0; i < 320; i++)
{
// System.out.println (HexFormatter.format (buffer, 0x08600 + i * 32, 32));
int offset = 0x08600 + i * 32 + 18;
int key = HexFormatter.getWord (buffer, offset);
if (key > 0)
text.append (String.format ("%04X %04X * %s%n", offset, key,
messageBlock.getMessageText (key)));
key = HexFormatter.getWord (buffer, offset + 8);
if (key > 0)
text.append (String.format ("%04X %04X %s%n", offset + 8, key,
messageBlock.getMessageText (key)));
}
List<DiskAddress> allOracleBlocks = new ArrayList<DiskAddress> ();
for (int i = 0; i < 20; i++)
{
allOracleBlocks.add (blocks.get (67 + i));
}
DefaultMutableTreeNode oracleNode =
linkNode ("Oracle", text.toString (), scenarioNode);
oracleNode.setAllowsChildren (false);
DefaultAppleFileSource afs = (DefaultAppleFileSource) oracleNode.getUserObject ();
afs.setSectors (allOracleBlocks);
2016-08-16 06:34:23 +00:00
}
2016-08-14 08:41:19 +00:00
private void addToNode (AbstractFile af, DefaultMutableTreeNode node,
2016-08-16 06:34:23 +00:00
List<DiskAddress> blocks)
2016-08-14 08:41:19 +00:00
{
DefaultAppleFileSource dafs =
new DefaultAppleFileSource (af.getName (), af, this, blocks);
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode (dafs);
childNode.setAllowsChildren (false);
2016-08-19 09:57:29 +00:00
node.add (childNode);
2016-08-08 04:53:29 +00:00
}
2016-08-16 06:34:23 +00:00
private DefaultMutableTreeNode linkNode (String name, String text,
DefaultMutableTreeNode parent)
{
DefaultAppleFileSource afs = new DefaultAppleFileSource (name, text, this);
DefaultMutableTreeNode node = new DefaultMutableTreeNode (afs);
parent.add (node);
return node;
}
2016-08-14 08:41:19 +00:00
public static boolean isWizardryIVorV (Disk disk, boolean debug)
2016-08-08 04:53:29 +00:00
{
2016-08-14 08:41:19 +00:00
// Wizardry IV or V boot code
2016-08-08 04:53:29 +00:00
byte[] header = { 0x00, (byte) 0xEA, (byte) 0xA9, 0x60, (byte) 0x8D, 0x01, 0x08 };
byte[] buffer = disk.readSector (0);
if (!Utility.matches (buffer, 0, header))
return false;
2016-08-14 08:41:19 +00:00
buffer = disk.readSector (1);
if (buffer[510] != 1 || buffer[511] != 0) // disk #1
2016-08-08 04:53:29 +00:00
return false;
return true;
}
}