diff --git a/Source/SkyPortal/Private/SkyPortalIO.cpp b/Source/SkyPortal/Private/SkyPortalIO.cpp index 19821ed..32152e0 100644 --- a/Source/SkyPortal/Private/SkyPortalIO.cpp +++ b/Source/SkyPortal/Private/SkyPortalIO.cpp @@ -181,11 +181,25 @@ uint8* USkyPortalIO::QueryBlock(uint8 FigureIndex, uint8 BlockIndex) command.data[2] = FigureIndex; // Figure index (0x00-0x0F) command.data[3] = BlockIndex; unsigned char* output; + int attempt=0; do { - Write(&command); + attempt++; + UE_LOG(LogSkyportalIO, Verbose, TEXT("Trying to write... attempt:%d"), attempt); + if (Write(&command)) { output = Read(); - } while (output[0] != 'Q' || (output[1] % 0x10 != FigureIndex && output[1] != 0x01) || output[2] != BlockIndex); + if (output == 0) { + UE_LOG(LogSkyportalIO, Error, TEXT("Something went wrong"), attempt); + return 0; + } + } + else { + UE_LOG(LogSkyportalIO, Error, TEXT("Something went wrong"), attempt); + return 0; + } + } while (output[0] != 'Q' || (output[1] % 0x10 != FigureIndex && output[1] != 0x01) || output[2] != BlockIndex|| attempt <10); + UE_LOG(LogSkyportalIO, Verbose, TEXT("Querying block %d - success"), BlockIndex); + UE_LOG(LogSkyportalIO, VeryVerbose, TEXT("Data block %d = \n %s"), BlockIndex,*OutputToString(output)); return output; } @@ -330,11 +344,19 @@ FigureData USkyPortalIO::ReadFigureBlocks(uint8 FigureIndex) // Loop over all 64 blocks for (uint8 BlockIndex = 0; BlockIndex < FIGURE_TOTAL_BLOCKS; ++BlockIndex) { + UE_LOG(LogSkyportalIO, VeryVerbose, TEXT("Reading block %d"), BlockIndex); + // Query the block from the portal uint8* output = QueryBlock(FigureIndex, BlockIndex); + if (output == 0) { + --BlockIndex; + UE_LOG(LogSkyportalIO, Error, TEXT("Query error, reprocessing...")); + continue; + } // Copy 16 bytes from the output, starting at the third byte - FMemory::Memcpy(TempFigureData.data, output + 3, FIGURE_BLOCK_SIZE); + FMemory::Memcpy(TempFigureData.data[BlockIndex], output + 3, FIGURE_BLOCK_SIZE); + // Block 1 is sometimes a duplicate of block 0 if (BlockIndex == 1)