void TryConnect(string wid, bool useToken, int retries, int timeout) { re.Reset(); if (retries < 0) { this.InvokeX(() => { MessageBox.Show(this, "Timed out, max retry limit reached. Wrong world id?", "Timeout", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); EnableMainPanel(); }); return; } //if (cachedToken == null) // if (useToken) cli = new EEWClient(File.ReadAllText(EEWClient.TokenPath)); // else // { // var creds = File.ReadAllLines(EEWClient.TokenPath); // cli = new EEWClient(creds[0], creds[1], out cachedToken); // } //else cli = new EEWClient(cachedToken); //cli.OnMessage += (o, e) => //{ // if (e.Type == MessageType.SelfInfo) username = e.GetString(0); //}; //string token; try { string token = null; this.InvokeX(() => token = Clipboard.GetText()); cli = new EEUClient(token); cli.OnDisconnect += OnDisconnect; cli.Connect(); } catch (Exception ex) { this.InvokeX(() => { MessageBox.Show(this, "Make sure you copied your EEU token to clipboard.\n" + ex.ToString(), "Error connecting", MessageBoxButtons.OK, MessageBoxIcon.Error); EnableMainPanel(); }); return; } con = cli.CreateWorldConnection(wid); //con.UseAsync = true; //con.UseLocking = false; con.OnMessage += OnMessage; con.Init(); if (!re.Wait(timeout)) { cli?.Dispose(); TryConnect(wid, useToken, retries - 1, timeout + 750); } }
private void button1_Click(object sender, EventArgs e) { if (button1.Text == "disconnect") { button1.Enabled = false; players.Clear(); zones.Clear(); listBox1.Items.Clear(); listBox2.Items.Clear(); cli?.Dispose(); b?.Dispose(); b2?.Dispose(); g?.Dispose(); g2?.Dispose(); pictureBox1.Image = null; pictureBoxWithInterpolationMode1.Image = null; button1.Text = "connect"; button1.Enabled = true; return; } button1.Enabled = false; var wid = textBox1.Text; File.WriteAllText(WIDPATH, wid); try { cli = new EEUClient(Clipboard.GetText()); cli.Connect(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); button1.Enabled = true; return; } data = new RoomData(); cli.OnDisconnect += delegate(object o, CloseEventArgs ee) { MessageBox.Show(ee.ToString(), "OnDisconnect", MessageBoxButtons.OK, MessageBoxIcon.Error); }; con = cli.CreateWorldConnection(wid); con.OnMessage += OnMessage; con.Init(); button1.Text = "disconnect"; button1.Enabled = true; }
//CountdownEvent ce = new CountdownEvent(1); private void OnMessage(object sender, EEUniverse.Library.Message m) { void die() { cli.Dispose(); panelMain.InvokeX(() => EnableMainPanel()); } switch (m.Type) { case MessageType.Init: re.Set(); if (m.GetString(7) != m.GetString(1)) { die(); MessageBox.Show("not world owner", ".w.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } world = new RoomData(parsePlayerData: false); world.Parse(m); int ww = world.Width, wh = world.Height; Stopwatch s = Stopwatch.StartNew(); //ce.Reset(); Task.Run(() => { //void PlaceBlock(int layer, int x, int y, int id) //{ // ce.AddCount(); // con.SendAsync(MessageType.PlaceBlock, layer, x, y, id).ContinueWith((_) => ce.Signal()); //} for (int j = 0; j < imgh; j++) //y { int yy = j + y; if (yy >= wh) { break; //out of bounds } for (int i = 0; i < imgw; i++) //x { int xx = i + x; if (xx >= ww) { break; //out of bounds } int b = idgrid[i, j]; if (skiptransparent && b == (int)BlockId.Black) { continue; } int l = ((BlockId)b).IsBackground() ? 0 : 1; if (world[l, xx, yy].Id != b) { con.PlaceBlock(l, xx, yy, b); } if (l == 0) { if (glass) { if (world[1, xx, yy].Id != (int)BlockId.Clear) { con.PlaceBlock(1, xx, yy, (int)BlockId.Clear); } } else if (world[1, xx, yy].Id != 0) { con.PlaceBlock(1, xx, yy, 0); } } //else if (i == 1) //{ // PlaceBlock(0, xx, yy, 0);//get rid of bg behind fg //} } } //Console.WriteLine(s.Elapsed); //ce.Signal(); //ce.Wait(); s.Stop(); Console.WriteLine(s.Elapsed); die(); }); break; } }