public static short GetPacketOpcode(MainWindow window, string name, bool isServer = true) { short opCode = (from val in isServer ? ServerPacketNames : ClientPacketNames where val.Value.Equals(name) select val.Key).FirstOrDefault(); if(opCode == 0) { while (true) { try { InputValueBox inputBox = new InputValueBox(window, "Need to enter opcode", "Enter "+ name +" opcode: "); if (inputBox.Show() == false) return 0; opCode = BitConverter.ToInt16(inputBox.Result.ToBytes(), 0); break; } // ReSharper disable EmptyGeneralCatchClause catch // ReSharper restore EmptyGeneralCatchClause { MessageBox.Show("WRONG PARAMS!", "Error", 0, MessageBoxImage.Warning); } } } return opCode; }
public InputValueBox(MainWindow mainWindow, string title, string label, string def = "") { MainWindow = mainWindow; Grid grid = new Grid { Margin = new Thickness(15), }; grid.ColumnDefinitions.Add(new ColumnDefinition()); grid.ColumnDefinitions.Add(new ColumnDefinition()); grid.RowDefinitions.Add(new RowDefinition()); grid.RowDefinitions.Add(new RowDefinition()); Label textLabel = new Label { Content = label, Margin = new Thickness(0, 0, 15, 0) }; textLabel.SetValue(Grid.ColumnProperty, 0); textLabel.SetValue(Grid.RowProperty, 0); grid.Children.Add(textLabel); TextBox = new TextBox { Text = def, Width = 160, Height = 26, HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center, }; TextBox.SetValue(Grid.ColumnProperty, 1); TextBox.SetValue(Grid.RowProperty, 0); grid.Children.Add(TextBox); Button cancelButton = new Button { Width = 80, Height = 26, Margin = new Thickness(0, 15, 0, 0), Content = "Cancel", HorizontalAlignment = HorizontalAlignment.Right }; cancelButton.SetValue(Grid.ColumnProperty, 1); cancelButton.SetValue(Grid.RowProperty, 1); cancelButton.Click += Cancel; grid.Children.Add(cancelButton); Button okButton = new Button { Width = 60, Height = 26, Margin = new Thickness(0, 15, 90, 0), Content = "Ok", HorizontalAlignment = HorizontalAlignment.Right }; okButton.SetValue(Grid.ColumnProperty, 1); okButton.SetValue(Grid.RowProperty, 1); okButton.Click += Ok; grid.Children.Add(okButton); Window = new Window { Title = title, Left = mainWindow.Left + Mouse.GetPosition(mainWindow).X, Top = mainWindow.Top + Mouse.GetPosition(mainWindow).Y, SizeToContent = SizeToContent.WidthAndHeight, Content = grid, ResizeMode = ResizeMode.NoResize, }; }