Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
MultiplayerDialogUI.c
Go to the documentation of this file.
1 //------------------------------------------------------------------------------------------------
3 {
4  const string WIDGET_EDIT_IP = "EditIP";
5  const string WIDGET_EDIT_PORT = "EditPort";
6  const string WIDGET_CHECK_NETWORK = "CheckboxNetwork";
7 
8  const string DEFAULT_ADDRESS = "127.0.0.1";
9  const string DEFAULT_PORT = "2001";
10  const int DIRECT_JOIN_CODE_LENGTH = 10;
11 
12  protected SCR_EditBoxComponent m_EditIP;
13  protected SCR_EditBoxComponent m_EditPort;
14  protected SCR_CheckboxComponent m_CheckNetwork;
15 
16  protected ServerBrowserMenuUI m_ServerBrowserUI;
17 
18  /*ref ScriptInvoker m_OnJoinCodeEntered = new ScriptInvoker;
19  ref ScriptInvoker m_OnIpEntered = new ScriptInvoker;*/
20 
21  //------------------------------------------------------------------------------------------------
22  override void OnConfirm()
23  {
24  string address = GetAddress();
25  EDirectJoinFormats format = IsInputValid(m_EditIP);
26  bool publicNetwork = !m_CheckNetwork.IsChecked();
27 
28  m_OnConfirm.Invoke(address, format, publicNetwork);
29 
30  Close();
31  }
32 
33  //------------------------------------------------------------------------------------------------
34  string GetAddress()
35  {
36  // Theres no other way to recover the default values other than setting "UseDefaultValues".
37  // This prevents the user from setting the value directly. Instead it forces him to delete
38  // the default value glyph by glyph which is tedious.
39 
40  if (!m_EditIP || !m_EditPort)
41  return string.Empty;
42 
43  // Address
44  string sAddr = m_EditIP.GetValue();
45  if (sAddr.Length() == 0)
46  return string.Empty;
47 
48  // Set client address as fallback - todo: this will be removed once lan detection is available
49  if (sAddr == DEFAULT_ADDRESS)
50  sAddr = GetGame().GetBackendApi().GetClientLobby().GetMyIP();
51 
52  // Port
53  string sPort = string.Empty;
54 
55  if (IsAddressValid(sAddr))
56  {
57  sPort = m_EditPort.GetValue();
58  if (sPort.Length() == 0)
59  return string.Empty;
60  }
61 
62  BaseContainer gameplaySettings = GetGame().GetGameUserSettings().GetModule("SCR_GameplaySettings");
63 
64  // Check whether address matches ipv4 format of x.x.x.x, or x.x.x.x:port where x = [0, 255]
65  if (IsAddressValid(sAddr))
66  {
67  if (gameplaySettings)
68  {
69  gameplaySettings.Set("m_sLastIP", sAddr);
70  GetGame().UserSettingsChanged();
71  }
72  }
73 
74  if (gameplaySettings)
75  {
76  gameplaySettings.Set("m_sLastPort", sPort);
77  GetGame().UserSettingsChanged();
78  }
79 
80  // Results
81  if (!sPort.IsEmpty())
82  return sAddr + ":" + sPort;
83  else
84  return sAddr;
85  }
86 
87 
88 
89  //------------------------------------------------------------------------------------------------
90  override void OnMenuOpen(SCR_ConfigurableDialogUiPreset preset)
91  {
92  // Old OnMenuOpen()
93  super.OnMenuOpen(preset);
94  Widget root = GetRootWidget();
95 
96 
97  // IP and join code edit
98  m_EditIP = SCR_EditBoxComponent.GetEditBoxComponent(WIDGET_EDIT_IP, root);
99 
100  if (m_EditIP)
101  {
102  m_EditIP.m_OnChanged.Insert(CheckValidInput);
103 
104  Widget focus = m_EditIP.GetRootWidget();
105  if (focus)
106  GetGame().GetWorkspace().SetFocusedWidget(focus);
107  }
108 
109  // Port edit
110  m_EditPort = SCR_EditBoxComponent.GetEditBoxComponent(WIDGET_EDIT_PORT, root);
111 
112  if (m_EditPort)
113  {
114  m_EditPort.m_OnChanged.Insert(CheckValidInput);
115  }
116 
117  // Checkbox network
118  m_CheckNetwork = SCR_CheckboxComponent.GetCheckboxComponent(WIDGET_CHECK_NETWORK, root);
119 
120  //SetDialogType(EDialogType.ACTION);
121  //---
122 
123  // Try to load the last valid address
124  string text;
125  BaseContainer gameplaySettings = GetGame().GetGameUserSettings().GetModule("SCR_GameplaySettings");
126  if (gameplaySettings)
127  gameplaySettings.Get("m_sLastIP", text);
128 
129  if (text != string.Empty && m_EditIP)
130  m_EditIP.SetValue(text);
131 
132  // Port
133  string port;
134  if (gameplaySettings)
135  gameplaySettings.Get("m_sLastPort", port);
136 
137  if (m_EditPort)
138  {
139  if (!port.IsEmpty())
140  m_EditPort.SetValue(port);
141  else
142  m_EditPort.SetValue(DEFAULT_PORT);
143  }
144 
145  // Set the confirm button enabled or disabled
146  CheckValidInput(m_EditIP, string.Empty);
147  }
148 
149  //------------------------------------------------------------------------------------------------
150  // Checks whether specified ipv4 address matches the 'x.x.x.x' format, where x = [0, 255]
151  // if address is passed along with port, port is ignored
152  bool IsAddressValid(string address)
153  {
154  // ""
155  if (address == string.Empty)
156  return false;
157 
158 
159  // cut off port or whatever follows the :
160  if (address.Contains(":"))
161  {
162  int colonIndex = address.IndexOf(":");
163  address = address.Substring(0, colonIndex);
164  }
165 
166  array<string> values = new array<string>();
167  address.Split(".", values, true);
168  int valuesCount = values.Count();
169 
170  // 0.1.2.3 = 4 values
171  if (valuesCount != 4)
172  return false;
173 
174  int zeroElements = 0;
175  // check if values are ok
176  for (int i = 0; i < 4; i++)
177  {
178  // Iterate through all tokens parsed between '.'
179  // if all of the characters are digit (ascii 48-57)
180  // it then checks if each value is within 0-255 range
181  // if anything fails, returns false
182  int strLen = values[i].Length();
183  for (int j = 0; j < strLen; j++)
184  {
185  // Check if it's a valid digit
186  string char = values[i].Get(j);
187  int asciiValue = char.ToAscii();
188  if (asciiValue >= 48 && asciiValue <= 57)
189  continue;
190  else
191  return false;
192  }
193 
194  int value = values[i].ToInt();
195  if (value == 0)
196  zeroElements++;
197 
198  if (value >= 0 && value <= 255)
199  continue;
200  else
201  return false;
202  }
203 
204  return true;
205  }
206 
207  //------------------------------------------------------------------------------------------------
209  protected bool IsJoinCodeValid(string text)
210  {
211  // Text size
212  if (text.Length() != DIRECT_JOIN_CODE_LENGTH)
213  return false;
214 
215  for (int i = 0, len = text.Length(); i < len; i++)
216  {
217  // fail if is not a number
218  if (text[i].ToInt() == 0 && text[i] != "0")
219  return false;
220  }
221 
222  return true;
223  }
224 
225  //------------------------------------------------------------------------------------------------
226  void CheckValidInput(SCR_EditBoxComponent comp, string text)
227  {
228  if (!m_EditIP)
229  return;
230 
231  bool validInput = IsInputValid(m_EditIP) != EDirectJoinFormats.INVALID;
232  bool validAddress = IsInputValid(m_EditIP) == EDirectJoinFormats.IP_PORT;
233 
234  // Enable input
235  m_EditPort.SetEnabled(validAddress);
236  //m_Confirm.SetEnabled(validInput);
237  FindButton("confirm").SetEnabled(validInput);
238  }
239 
240  //------------------------------------------------------------------------------------------------
241  protected EDirectJoinFormats IsInputValid(SCR_EditBoxComponent comp)
242  {
243  // IP:port
244  if (IsAddressValid(m_EditIP.GetValue()))
245  return EDirectJoinFormats.IP_PORT;
246 
247  // Join code
248  if (IsJoinCodeValid(m_EditIP.GetValue()))
249  return EDirectJoinFormats.JOIN_CODE;
250 
251  return EDirectJoinFormats.INVALID;
252  }
253 
254  //------------------------------------------------------------------------------------------------
255  void SetServerBrowserUI(ServerBrowserMenuUI serverBrowserUI) { m_ServerBrowserUI = m_ServerBrowserUI; }
256 };
257 
258 //------------------------------------------------------------------------------------------------
260 {
265 };
266 
SCR_CheckboxComponent
This is refactored checkbox preserving its own class and API for compatability purposes.
Definition: SCR_CheckboxComponent.c:3
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
EDirectJoinFormats
EDirectJoinFormats
Definition: MultiplayerDialogUI.c:259
GetRootWidget
Widget GetRootWidget()
Definition: SCR_UITaskManagerComponent.c:160
JOIN_CODE
@ JOIN_CODE
Definition: MultiplayerDialogUI.c:263
IP_PORT
@ IP_PORT
Definition: MultiplayerDialogUI.c:262
MultiplayerDialogUI
Definition: MultiplayerDialogUI.c:2
SCR_ConfigurableDialogUiPreset
Configuration for a dialog.
Definition: SCR_ConfigurableDialogUI.c:809
INVALID
@ INVALID
Definition: MultiplayerDialogUI.c:261
SCR_ConfigurableDialogUi
Definition: SCR_ConfigurableDialogUI.c:13
SCR_EditBoxComponent
Definition: SCR_EditBoxComponent.c:8
FindButton
SCR_InputButtonComponent FindButton(string tag)
Returns a button with given tag.
Definition: SCR_BrowserHoverTooltipComponent.c:116
ROOM_ID
@ ROOM_ID
Definition: MultiplayerDialogUI.c:264
ServerBrowserMenuUI
Definition: ServerBrowserMenuUI.c:10