using System.Collections.Generic;using System.Globalization;using System.Net.Sockets; public static readonly bool On = true; public static readonly bool On = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("OCR_BRIDGE")); public const bool On = false; static NetworkStream net; static StreamReader reader; static readonly object gate = new object(); public struct Item { public string Text; public double X, Y, W, H; } return int.TryParse(Environment.GetEnvironmentVariable("OCR_BRIDGE"), out p) ? p : 8765; client = new TcpClient("127.0.0.1", Port) { NoDelay = true }; client.ReceiveTimeout = 120000; net = client.GetStream(); reader = new StreamReader(net, new UTF8Encoding(false)); try { if (client != null) client.Close(); } catch (Exception) { } public static List<Item> Ask(byte[] bgra, int w, int h) try { return Once(bgra, w, h); } catch (Exception) { Close(); } try { return Once(bgra, w, h); } catch (Exception) { Close(); throw; } var line = reader.ReadLine(); if (line == null) throw new IOException("the reader closed the connection"); static List<Item> Once(byte[] bgra, int w, int h) if (client == null) Connect(); BitConverter.GetBytes(w).CopyTo(head, 0); BitConverter.GetBytes(h).CopyTo(head, 4); net.Write(bgra, 0, w * 4 * h); int n = int.Parse(Line(), CultureInfo.InvariantCulture); var list = new List<Item>(); for (int i = 0; i < n; i++) int tab = line.IndexOf('\t'); var p = line.Substring(0, tab).Split(' '); X = double.Parse(p[0], CultureInfo.InvariantCulture), Y = double.Parse(p[1], CultureInfo.InvariantCulture), W = double.Parse(p[2], CultureInfo.InvariantCulture), H = double.Parse(p[3], CultureInfo.InvariantCulture), Text = line.Substring(tab + 1), public static string Check() try { Ask(new byte[16 * 16 * 4], 16, 16); return null; } catch (Exception e) { return "The text reader doesn't answer on port " + Port + " (" + e.Message + "), so the bot can't read the game."; }