:wave: Here it is my finished WDT java class, it's perfect for my needs!
Usage:
!!!Obviously include in your project and add to namespace to reflect directory structure
WDT wdt = new WDT("absolutefilepath",true);//auto opens wdt
WDT wdt = new WDT("absolutefilepath",false);//needs wdt.read() to open
wdt.debugMode() // toggles tracing of info
wdt.setActive(row,col); // sets an adt tile to become active
wdt.setInactive(row,col); // sets an adt tile to become inactive
wdt.write(filename); // writes to file (try not to overwrite it's untested and may break the file)
NOTE: Always keep a backup of the file!
Code:
import java.io.*;
import java.lang.String;
public class WDT
{
private boolean[] fileperms = {false,false};
private String filename = "";
private boolean debug = true;
private boolean[] data = new boolean[4096];//because there are 64*64 possibilities
public WDT(String _filename, boolean autoOpen)
{
filename = _filename;
File fil = new File(_filename);
if(fil.canRead())//check read capability
fileperms[0] = true;
if(fil.canWrite())//check write capability
fileperms[1] = true;
if(autoOpen)
read();
}
private char[] flip(char[] target)
{
char[] tmp = new char[target.length];
for(int i=0;i<target.length;i++)
{
tmp[(tmp.length-i)-1] = target[i];
}
return tmp;
}
private void trace()
{
for(int row = 1; row<=64 ;row++)
{
for(int col = 1;col<=64;col++)
{
System.out.print( ((data[((row-1)*64)+(col-1)]) ? ("x") : ("0")) + ((col == 64)?("\n"):("")) );// = ( ( (fourcc[0]|fourcc[1]|fourcc[2]|fourcc[3]) & 1 ) == 1) ? (true) : (false);
}
}
}
public boolean read()
{
if(!fileperms[0])
return false;
try
{
FileReader f = new FileReader(filename);
char[] fourcc = new char[4];
boolean eof = false;
while (!eof)
{
int tmp = f.read(fourcc);
if(tmp == -1)
eof = true;
fourcc = flip(fourcc);
if((fourcc[0] == 'M') && (fourcc[1] == 'A') && (fourcc[2] == 'I') && (fourcc[3] == 'N'))
{
f.read(fourcc);
f.read(fourcc);
for(int row = 1; row<=64 ;row++)
{
for(int col = 1;col<=64;col++)
{
f.read(fourcc);
f.read(fourcc);
data[((row-1)*64)+(col-1)] = ( ( (fourcc[0]|fourcc[1]|fourcc[2]|fourcc[3]) & 1 ) == 1) ? (true) : (false);
}
}
}
}
if(debug)
trace();
}
catch(IOException ioexc)
{
}
return true;
}
public void debugMode()
{
debug = !debug;
}
public void setActive(int row, int col)
{
(data[((row-1)*64)+(col-1)]) = true;
}
public void setInactive(int row, int col)
{
(data[((row-1)*64)+(col-1)]) = false;
}
public boolean write(String _filename)
{
if(!fileperms[0])
return false;
try
{
File file = new File(filename);
long bytes = file.length();
FileReader fr = new FileReader(filename);
char[] fourcc = new char[4];
FileWriter fw = new FileWriter(_filename);
fw.flush();
int test = 0;
while(bytes > 0)
{
if(bytes > 4)
{
fr.read(fourcc);
if(String.copyValueOf(fourcc) != "NIAM")
{
fw.append(fourcc[0]);
fw.append(fourcc[1]);
fw.append(fourcc[2]);
fw.append(fourcc[3]);
bytes -= 4;
}
else
{
fw.append(fourcc[0]);
fw.append(fourcc[1]);
fw.append(fourcc[2]);
fw.append(fourcc[3]);
bytes -= 4;
fr.read(fourcc);
fw.append(fourcc[0]);
fw.append(fourcc[1]);
fw.append(fourcc[2]);
fw.append(fourcc[3]);
bytes -= 4;
fr.read(fourcc);
fw.append(fourcc[0]);
fw.append(fourcc[1]);
fw.append(fourcc[2]);
fw.append(fourcc[3]);
bytes -= 4;
for(int row = 1; row<=64 ;row++)
{
for(int col = 1;col<=64;col++)
{
fw.append((char)(0));
fw.append((char)(0));
fw.append((char)(0));
fw.append((char)(0));
fw.append((char)(0));
fw.append((char)(0));
fw.append((char)(0));
fw.append((char)((data[((row-1)*64)+(col-1)]) ? (1) : (0)));
}
}
}
}
else
{
fw.append((char)fr.read());
bytes --;
}
}
//EOF (fr.read() returned -1)
fw.close();
fr.close();
}
catch(IOException ioexc)
{
return false;
}
return true;
}
}