1 // Copyright 2001-2019 Crytek GmbH / Crytek Group. All rights reserved.
4 using System
.Collections
.Generic
;
5 using System
.ComponentModel
;
10 using System
.Windows
.Forms
;
11 using LuaRemoteDebugger
.Properties
;
12 using System
.Runtime
.InteropServices
;
15 namespace LuaRemoteDebugger
17 public partial class ConnectMessageBox
: Form
19 private class TargetItem
21 public string Name { get; set; }
22 public string IpAddress { get; set; }
23 public int Port { get; set; }
24 public TargetItem(string name
, string ipAddress
, int port
) { Name = name; IpAddress = ipAddress; Port = port; }
26 public override string ToString()
33 bool suppressChangeEvents
= false; // Used to prevent feedback loop
34 static ConnectMessageBox currentConnectMessageBox
;
36 public string IpAddress { get { return textBoxIpAddress.Text; }
}
41 int result
= CryNotificationNetworkClient
.NN_PORT
;
42 int.TryParse(textBoxPort
.Text
, out result
);
47 #region Kernel32Functions
49 [DllImport("kernel32.dll")]
50 private static extern IntPtr
LoadLibrary(string dllFilePath
);
52 [DllImport("kernel32.dll")]
53 private static extern IntPtr
GetProcAddress(IntPtr hModule
, String procname
);
55 [DllImport("kernel32.dll")]
56 private static extern Int32
GetLastError();
58 [DllImport("kernel32.dll")]
59 private static extern Int32
SetDllDirectory(string directory
);
65 // Functions and structs defined in xbdm.h
66 [DllImport("xbdm.dll")]
67 static extern Int32
DmGetAltAddress(out uint address
);
69 [DllImport("xbdm.dll")]
70 static extern Int32
DmGetNameOfXbox(StringBuilder name
, out uint length
, bool resolvable
);
74 public ConnectMessageBox(MainForm parent
)
76 InitializeComponent();
79 string currentIp
= Settings
.Default
.IpAddress
;
80 int currentPort
= Settings
.Default
.Port
;
81 textBoxIpAddress
.Text
= currentIp
;
82 textBoxPort
.Text
= currentPort
.ToString();
85 FindMatchingTarget(currentIp
, currentPort
);
88 private void buttonConnect_Click(object sender
, EventArgs e
)
90 Settings
.Default
.IpAddress
= textBoxIpAddress
.Text
;
91 Settings
.Default
.Port
= Port
;
94 private void PopulateTargets()
97 comboBoxTarget
.Items
.Add(new TargetItem("PC (Game)", "127.0.0.1", CryNotificationNetworkClient
.NN_PORT
));
98 comboBoxTarget
.Items
.Add(new TargetItem("PC (Editor)", "127.0.0.1", CryNotificationNetworkClient
.NN_PORT_EDITOR
));
100 /* No more support for Xenon and PS3, let's not look for them anymore
101 // Get Xbox IP address
102 string xekd = Environment.GetEnvironmentVariable("XEDK");
106 if (IntPtr.Size == 4)
108 dllPath = Path.Combine(xekd, "bin\\win32");
112 dllPath = Path.Combine(xekd, "bin\\x64");
115 // Set the DLL search directory so that it is able to find xbdm.dll
116 SetDllDirectory(dllPath);
118 uint xboxAddress = 0;
119 StringBuilder xboxName = new StringBuilder(1024);
123 DmGetAltAddress(out xboxAddress);
124 uint xboxNameLength = (uint)xboxName.Capacity;
125 DmGetNameOfXbox(xboxName, out xboxNameLength, true);
127 catch (System.Exception ex)
129 parent.LogMessage("Failed to get Xbox IP address: {0}", ex.Message);
134 byte p1 = (byte)(xboxAddress >> 24 & 0xFF);
135 byte p2 = (byte)(xboxAddress >> 16 & 0xFF);
136 byte p3 = (byte)(xboxAddress >> 8 & 0xFF);
137 byte p4 = (byte)(xboxAddress >> 0 & 0xFF);
138 string xboxIp = string.Format("{0}.{1}.{2}.{3}", p1, p2, p3, p4);
140 comboBoxTarget.Items.Add(new TargetItem(string.Format("[Xbox 360] {0}", xboxName), xboxIp, CryNotificationNetworkClient.NN_PORT));
143 // Restore the default DLL search order
144 SetDllDirectory(null);
148 parent.LogMessage("Failed to find Xbox XDK");
151 // Get the PS3 IP addresses
152 if (Ps3TmApi.Available)
154 int result = Ps3TmApi.SNPS3InitTargetComms();
155 currentConnectMessageBox = this;
156 result = Ps3TmApi.SNPS3EnumerateTargetsEx(GetPS3TargetInfoCallback, IntPtr.Zero);
157 currentConnectMessageBox = null;
158 result = Ps3TmApi.SNPS3CloseTargetComms();
162 parent.LogMessage("Failed to load PS3 Target Manager API");
167 private static int GetPS3TargetInfoCallback(int target
, IntPtr userData
)
169 Ps3TmApi
.SNPS3TargetInfo targetInfo
= new Ps3TmApi
.SNPS3TargetInfo();
170 targetInfo
.hTarget
= target
;
171 targetInfo
.nFlags
= Ps3TmApi
.SN_TI_TARGETID
| Ps3TmApi
.SN_TI_NAME
| Ps3TmApi
.SN_TI_INFO
;
172 int result
= Ps3TmApi
.SNPS3GetTargetInfo(ref targetInfo
);
173 string type
= Marshal
.PtrToStringAnsi(targetInfo
.pszType
);
174 string name
= Marshal
.PtrToStringAnsi(targetInfo
.pszName
);
175 string info
= Marshal
.PtrToStringAnsi(targetInfo
.pszInfo
);
176 string ipAddress
= string.Empty
;
177 Ps3TmApi
.SNPS3GamePortIPAddressData gameIpAddress
;
178 // Note: We can only get the game port IP address if we are connected to the kit and it is running a game
179 result
= Ps3TmApi
.SNPS3GetGamePortIPAddrData(target
, IntPtr
.Zero
, out gameIpAddress
);
180 if (result
== 0 && gameIpAddress
.uReturnValue
== 0)
182 byte p1
= (byte)(gameIpAddress
.uIPAddress
>> 24 & 0xFF);
183 byte p2
= (byte)(gameIpAddress
.uIPAddress
>> 16 & 0xFF);
184 byte p3
= (byte)(gameIpAddress
.uIPAddress
>> 8 & 0xFF);
185 byte p4
= (byte)(gameIpAddress
.uIPAddress
>> 0 & 0xFF);
186 ipAddress
= string.Format("{0}.{1}.{2}.{3}", p1
, p2
, p3
, p4
);
187 name
+= " (connected)";
189 else if (type
== "PS3_DBG_DEX")
191 // Test kits only have 1 IP address, and we can get it from the info string
192 string[] parts
= info
.Split(',');
193 string lastPart
= parts
[parts
.Length
- 1].Trim();
194 ipAddress
= lastPart
.Split(':')[0];
196 if (!string.IsNullOrEmpty(ipAddress
))
198 currentConnectMessageBox
.comboBoxTarget
.Items
.Add(new TargetItem(string.Format("[PS3] {0}", name
), ipAddress
, CryNotificationNetworkClient
.NN_PORT
));
203 private void FindMatchingTarget(string ipAddress
, int port
)
205 foreach (TargetItem item
in comboBoxTarget
.Items
)
207 if (item
.IpAddress
== ipAddress
&& item
.Port
== port
)
209 comboBoxTarget
.SelectedItem
= item
;
214 comboBoxTarget
.SelectedItem
= null;
217 private void comboBoxTarget_SelectedIndexChanged(object sender
, EventArgs e
)
219 if (!suppressChangeEvents
)
221 suppressChangeEvents
= true;
222 TargetItem selectedItem
= comboBoxTarget
.SelectedItem
as TargetItem
;
223 if (selectedItem
!= null)
225 textBoxIpAddress
.Text
= selectedItem
.IpAddress
;
226 textBoxPort
.Text
= selectedItem
.Port
.ToString();
230 textBoxIpAddress
.Text
= string.Empty
;
231 textBoxPort
.Text
= CryNotificationNetworkClient
.NN_PORT
.ToString();
233 suppressChangeEvents
= false;
237 private void textBoxIpAddress_TextChanged(object sender
, EventArgs e
)
239 if (!suppressChangeEvents
)
241 suppressChangeEvents
= true;
242 FindMatchingTarget(textBoxIpAddress
.Text
, Port
);
243 suppressChangeEvents
= false;
247 private void textBoxPort_TextChanged(object sender
, EventArgs e
)
249 if (!suppressChangeEvents
)
251 suppressChangeEvents
= true;
252 FindMatchingTarget(textBoxIpAddress
.Text
, Port
);
253 suppressChangeEvents
= false;