public MainForm() { InitializeComponent(); CollectFileNames(); scanners.Add(new ScannerColoCombat()); scanners.Add(new ScannerColoPurify()); scanners.Add(new ScannerTitleScreen()); scanners.Add(new ScannerMessageBox()); scanners.Add(new ScannerCombat()); scanners.Add(new ScannerPurify()); foreach (var scanner in scanners) { scanner.DebugLevel = ScannerBase.EDebugLevel.Verbose; } MLDataExporter MLexport = new MLDataExporter { LoadScreenshot = (path) => LoadTestScreenshot(path, false), exportPath = jsonFolder }; MLexport.DoTheThing(); }
public virtual void ValidateScan(string configPath, ScreenAnalyzer.EMode mode, MLDataExporter dataExporter) { throw new Exception("Scanner doesn't support tests!"); }
public override void ValidateScan(string configPath, ScreenAnalyzer.EMode mode, MLDataExporter dataExporter) { string testName = Path.GetFileNameWithoutExtension(configPath); GameState validationState = LoadValidationConfig(configPath); if (validationState == null || cachedGameState == null) { string exceptionMsg = string.Format("Test {0} failed! Scan results:{1}, config path: {2}", testName, cachedGameState, configPath); throw new Exception(exceptionMsg); } if (dataExporter != null && cachedCircles != null) { int numPatterns = 0; for (int idx = 0; idx < 9; idx++) { // generate additional sample images by offseting source bounds -5..5 px in each direction // numbers only, empty field doesn't have enough details to care if (validationState.board[idx] > 0) { for (int offsetX = -5; offsetX <= 5; offsetX++) { for (int offsetY = -5; offsetY <= 5; offsetY++) { Rectangle offsetBounds = cachedCircles[idx]; offsetBounds.Offset(offsetX, offsetY); float[] exportValues = ExtractCirclePattern(screenAnalyzer.cachedFastBitmap, offsetBounds); dataExporter.ExportValues(exportValues, validationState.board[idx]); numPatterns++; } } } else { float[] exportValues = ExtractCirclePattern(screenAnalyzer.cachedFastBitmap, cachedCircles[idx]); dataExporter.ExportValues(exportValues, validationState.board[idx]); numPatterns++; } } Logger.WriteLine("Exported ML entries:{0}", numPatterns); } for (int idx = 0; idx < 9; idx++) { if (cachedGameState.board[idx] != validationState.board[idx]) { string exceptionMsg = string.Format("Test {0} failed! Board[{1}] got:{2}, expected:{3}", testName, idx, cachedGameState.board[idx], validationState.board[idx]); throw new Exception(exceptionMsg); //Logger.WriteLine(exceptionMsg); } } }
public static void RunTests(string path, ScreenAnalyzer.EMode mode) { ScreenAnalyzer screenAnalyzer = new ScreenAnalyzer(); MLDataExporter dataExporter = null; ScannerBase scannerOb = null; foreach (var kvp in screenAnalyzer.mapScanners) { if ((kvp.Key & mode) != ScreenAnalyzer.EMode.None) { scannerOb = kvp.Value; break; } } if (scannerOb == null) { throw new Exception("Test failed! Can't find scanner for requested type:" + mode); } if (exportDetectionPatterns) { dataExporter = new MLDataExporter(); dataExporter.exportPath = AssetManager.Get().CreateFilePath("ml/patternMatch/data"); dataExporter.StartDataExport((mode & ScreenAnalyzer.EMode.ScanAll).ToString()); } string testRoot = AssetManager.Get().CreateFilePath(path); IEnumerable <string> configPaths = Directory.EnumerateFiles(testRoot, "*.json"); foreach (var configPath in configPaths) { string imagePath = configPath.Replace(".json", ".jpg"); if (!File.Exists(imagePath)) { imagePath = imagePath.Replace(".jpg", ".png"); } if (File.Exists(imagePath)) { Logger.WriteLine("==> Testing: " + Path.GetFileNameWithoutExtension(configPath)); bool bNeedsDebugRun = false; screenAnalyzer.debugScreenshotPath = imagePath; screenAnalyzer.debugScannerContext = null; try { screenAnalyzer.DoWork(mode); scannerOb.ValidateScan(configPath, mode, dataExporter); } catch (Exception ex) { Logger.WriteLine("Exception:" + ex); bNeedsDebugRun = true; } // retry, don't catch exceptions if (bNeedsDebugRun && !exportDetectionPatterns) { screenAnalyzer.DoWork(mode | ScreenAnalyzer.EMode.Debug); scannerOb.ValidateScan(configPath, mode | ScreenAnalyzer.EMode.Debug, null); } } } if (exportDetectionPatterns) { dataExporter.FinishDataExport("ml-" + Path.GetFileNameWithoutExtension(path) + ".json"); } }