Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
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
15
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();
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 //REFACTOR NOTE
66 //This is wrong, should check for either ip address OR invite code. Switche it to the corret function call.
68 {
70 {
71 gameplaySettings.Set("m_sLastIP", sAddr);
72 GetGame().UserSettingsChanged();
73 }
74 }
75
77 {
78 gameplaySettings.Set("m_sLastPort", sPort);
79 GetGame().UserSettingsChanged();
80 }
81
82 // Results
83 if (!sPort.IsEmpty())
84 return sAddr + ":" + sPort;
85 else
86 return sAddr;
87 }
88
89
90
91 //------------------------------------------------------------------------------------------------
93 {
94 // Old OnMenuOpen()
95 super.OnMenuOpen(preset);
96 Widget root = GetRootWidget();
97
98
99 // IP and join code edit
100 m_EditIP = SCR_EditBoxComponent.GetEditBoxComponent(WIDGET_EDIT_IP, root);
101
102 if (m_EditIP)
103 {
104 m_EditIP.m_OnChanged.Insert(CheckValidInput);
105
106 Widget focus = m_EditIP.GetRootWidget();
107 if (focus)
108 GetGame().GetWorkspace().SetFocusedWidget(focus);
109 }
110
111 // Port edit
112 m_EditPort = SCR_EditBoxComponent.GetEditBoxComponent(WIDGET_EDIT_PORT, root);
113
114 if (m_EditPort)
115 {
116 m_EditPort.m_OnChanged.Insert(CheckValidInput);
117 }
118
119 // Checkbox network
120 m_CheckNetwork = SCR_CheckboxComponent.GetCheckboxComponent(WIDGET_CHECK_NETWORK, root);
121
122 //SetDialogType(EDialogType.ACTION);
123 //---
124
125 // Try to load the last valid address
126 string text;
127 BaseContainer gameplaySettings = GetGame().GetGameUserSettings().GetModule("SCR_GameplaySettings");
129 gameplaySettings.Get("m_sLastIP", text);
130
131 if (text != string.Empty && m_EditIP)
132 m_EditIP.SetValue(text);
133
134 // Port
135 string port;
137 gameplaySettings.Get("m_sLastPort", port);
138
139 if (m_EditPort)
140 {
141 if (!port.IsEmpty())
142 m_EditPort.SetValue(port);
143 else
144 m_EditPort.SetValue(DEFAULT_PORT);
145 }
146
147 // Set the confirm button enabled or disabled
148 CheckValidInput(m_EditIP, string.Empty);
149 }
150
151 //------------------------------------------------------------------------------------------------
152 // Checks whether specified ipv4 address matches the 'x.x.x.x' format, where x = [0, 255]
153 // if address is passed along with port, port is ignored
154 bool IsAddressValid(string address)
155 {
156 // ""
157 if (address == string.Empty)
158 return false;
159
160
161 // cut off port or whatever follows the :
162 if (address.Contains(":"))
163 {
164 int colonIndex = address.IndexOf(":");
165 address = address.Substring(0, colonIndex);
166 }
167
168 array<string> values = new array<string>();
169 address.Split(".", values, true);
170 int valuesCount = values.Count();
171
172 // 0.1.2.3 = 4 values
173 if (valuesCount != 4)
174 return false;
175
176 int zeroElements = 0;
177 // check if values are ok
178 for (int i = 0; i < 4; i++)
179 {
180 // Iterate through all tokens parsed between '.'
181 // if all of the characters are digit (ascii 48-57)
182 // it then checks if each value is within 0-255 range
183 // if anything fails, returns false
184 int strLen = values[i].Length();
185 for (int j = 0; j < strLen; j++)
186 {
187 // Check if it's a valid digit
188 string char = values[i].Get(j);
189 int asciiValue = char.ToAscii();
190 if (asciiValue >= 48 && asciiValue <= 57)
191 continue;
192 else
193 return false;
194 }
195
196 int value = values[i].ToInt();
197 if (value == 0)
198 zeroElements++;
199
200 if (value >= 0 && value <= 255)
201 continue;
202 else
203 return false;
204 }
205
206 return true;
207 }
208
209 //------------------------------------------------------------------------------------------------
211 protected bool IsJoinCodeValid(string text)
212 {
213 // Text size
214 if (text.Length() != DIRECT_JOIN_CODE_LENGTH)
215 return false;
216
217 for (int i = 0, len = text.Length(); i < len; i++)
218 {
219 // fail if is not a number
220 if (text[i].ToInt() == 0 && text[i] != "0")
221 return false;
222 }
223
224 return true;
225 }
226
227 //------------------------------------------------------------------------------------------------
228 void CheckValidInput(SCR_EditBoxComponent comp, string text)
229 {
230 if (!m_EditIP)
231 return;
232
233 bool validInput = IsInputValid(m_EditIP) != EDirectJoinFormats.INVALID;
234 bool validAddress = IsInputValid(m_EditIP) == EDirectJoinFormats.IP_PORT;
235
236 // Enable input
237 m_EditPort.SetEnabled(validAddress);
238 //m_Confirm.SetEnabled(validInput);
239 FindButton("confirm").SetEnabled(validInput);
240 }
241
242 //------------------------------------------------------------------------------------------------
244 {
245 // IP:port
246 if (IsAddressValid(m_EditIP.GetValue()))
247 return EDirectJoinFormats.IP_PORT;
248
249 // Join code
250 if (IsJoinCodeValid(m_EditIP.GetValue()))
251 return EDirectJoinFormats.JOIN_CODE;
252
253 return EDirectJoinFormats.INVALID;
254 }
255
256 //------------------------------------------------------------------------------------------------
258};
259
260//------------------------------------------------------------------------------------------------
268
ArmaReforgerScripted GetGame()
Definition game.c:1398
EDirectJoinFormats
SCR_HelicopterControllerComponentClass gameplaySettings
override void OnMenuOpen(SCR_ConfigurableDialogUiPreset preset)
SCR_CheckboxComponent m_CheckNetwork
void SetServerBrowserUI(ServerBrowserMenuUI serverBrowserUI)
SCR_EditBoxComponent m_EditIP
override void OnConfirm()
ServerBrowserMenuUI m_ServerBrowserUI
bool IsAddressValid(string address)
SCR_EditBoxComponent m_EditPort
bool IsJoinCodeValid(string text)
Check it string is in join code format - 10 size, digits are check in editbox.
void CheckValidInput(SCR_EditBoxComponent comp, string text)
EDirectJoinFormats IsInputValid(SCR_EditBoxComponent comp)
This is refactored checkbox preserving its own class and API for compatability purposes.
SCR_InputButtonComponent FindButton(string tag)
Returns a button with given tag.
static SCR_EditBoxComponent GetEditBoxComponent(string name, Widget parent, bool searchAllChildren=true)
override void SetEnabled(bool enabled, bool animate=true)
@ INVALID
Missing components, or obstruction test was not possible.