private int CheckAxisCount(int offset, byte[] allBytes, out List<MapSelector> mapSelectors)
{
int axisCount = 0;
/*if (offset == 0x58504)
{
Console.WriteLine("58504");
}*/
mapSelectors = new List<MapSelector>();
bool axisFound = true;
int t = offset;
while (axisFound)
{
axisFound = false;
int axisid = (Convert.ToInt32(allBytes[t + 1]) * 256) + Convert.ToInt32(allBytes[t]);
if (isAxisID(axisid))
{
int axislen = (Convert.ToInt32(allBytes[t + 3]) * 256) + Convert.ToInt32(allBytes[t + 2]);
if (axislen > 0 && axislen < 32)
{
axisCount++;
axisFound = true;
t += 4 + (axislen * 2);
}
}
}
// search from offset 't' for selectors
// maximum searchrange = 0x1000
int BytesToSearch = 5120 + 16;
if (axisCount > 3)
{
while (BytesToSearch > 0)
{
int axisid = (Convert.ToInt32(allBytes[t + 1]) * 256) + Convert.ToInt32(allBytes[t]);
if (isAxisID(axisid))
{
//Console.WriteLine("Checking address: " + t.ToString("X8"));
int axislen = (Convert.ToInt32(allBytes[t + 3]) * 256) + Convert.ToInt32(allBytes[t + 2]);
if (axislen <= 10) // more is not valid for selectors
{
// read & verify data (00 00 00 01 00 02 00 03 etc)
bool selectorValid = true;
int num = 0;
uint prevSelector = 0;
for (int i = 0; i < (axislen * 2); i += 2)
{
uint selValue = Convert.ToUInt32(allBytes[t + 4 + (axislen * 2) + i]) + Convert.ToUInt32(allBytes[t + 4 + (axislen * 2) + 1 + i]);
//Console.WriteLine("Selval: " + selValue.ToString() + " num: " + num.ToString());
/*if (axislen < 3)
{
selectorValid = false;
break;
}*/
if (allBytes[t + 4 + (axislen * 2) + i] != 0)
{
if(allBytes[t + 4 + (axislen * 2) + i] != 0x40) selectorValid = false;
break;
}
if (allBytes[t + 4 + (axislen * 2) + 1 + i] > 9)
{
selectorValid = false;
break;
}
if (prevSelector > selValue)
{
selectorValid = false;
break;
}
prevSelector = selValue;
/*if (num != selValue)
{
// not a valid selector
selectorValid = false;
break;
}*/
num++;
}
if (selectorValid)
{
// create a new selector
//Console.WriteLine("Selector valid " + t.ToString("X8"));
MapSelector newSel = new MapSelector();
newSel.NumRepeats = axislen;
newSel.StartAddress = t;
// read the data into the mapselector
newSel.MapData = new int[axislen];
int boffset = 0;
for (int ia = 0; ia < axislen; ia++)
{
int axisValue = Convert.ToInt32(allBytes[newSel.StartAddress + 4 + boffset]) + Convert.ToInt32(allBytes[newSel.StartAddress + 4 + boffset + 1]) * 256;
newSel.MapData.SetValue(axisValue, ia);
boffset += 2;
}
mapSelectors.Add(newSel);
if (mapSelectors.Count > 5) break;
BytesToSearch = 5120 + 16;
}
}
}
t += 2;
BytesToSearch -= 2;
}
}
return axisCount;
}