private int CheckAxisCount(int offset, byte[] allBytes, out List<MapSelector> mapSelectors)
{
int axisCount = 0;
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 (allBytes[t + 4 + (axislen * 2) + i] != 0)
{
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;
mapSelectors.Add(newSel);
BytesToSearch = 5120 + 16;
}
}
}
t += 2;
BytesToSearch -= 2;
}
}
return axisCount;
}