-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathETCS_RBC.cs
More file actions
156 lines (145 loc) · 5.74 KB
/
ETCS_RBC.cs
File metadata and controls
156 lines (145 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using Orts.MultiPlayer;
using Orts.Simulation.Signalling;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Reflection;
namespace ORTS.Scripting.Script
{
public class EtcsRbcManager
{
public static EtcsRbcManager DefaultRbc;
static Assembly RbcAssembly;
static bool AssemblyLoadTried;
object Rbc;
MethodInfo? RbcUpdate;
public bool Active { get; private set; }
public int NID_C { get; private set; }
public int NID_RBC { get; private set; }
public ulong NID_RADIO { get; private set; } = 0xFFFFFFFFFFFFFFFFUL;
Task<string> ipTask;
public EtcsRbcManager(int nid_c)
{
NID_C = nid_c;
byte[] firstMacAddress = NetworkInterface
.GetAllNetworkInterfaces()
.Where(nic => nic.OperationalStatus == OperationalStatus.Up && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.Select(nic => nic.GetPhysicalAddress().GetAddressBytes())
.FirstOrDefault();
if (firstMacAddress != null)
{
if (firstMacAddress.Length > 1) NID_RBC = ((firstMacAddress[firstMacAddress.Length - 2] << 8) | firstMacAddress[firstMacAddress.Length - 1]) & 16383;
else if (firstMacAddress.Length > 0) NID_RBC = firstMacAddress[0] & 255;
}
ipTask = GetIpAddress();
try
{
if (!AssemblyLoadTried)
{
AssemblyLoadTried = true;
byte[] bytes = System.IO.File.ReadAllBytes(Path.Combine(ETCS.RouteDirectoryPath, "RBC.dll"));
byte[] bytesPdb = null;
var pdbPath = Path.Combine(ETCS.RouteDirectoryPath, "RBC.pdb");
if (File.Exists(pdbPath)) bytesPdb = System.IO.File.ReadAllBytes(pdbPath);
RbcAssembly = bytesPdb != null ? Assembly.Load(bytes, bytesPdb) : Assembly.Load(bytes);
}
if (RbcAssembly != null)
{
var ty = RbcAssembly.GetType("RBC.RBC");
var ctor = ty.GetConstructor(new[] { typeof(int), typeof(int), typeof(int) });
Rbc = ctor.Invoke(new object[] { NID_C, NID_RBC, 0x7911 });
RbcUpdate = ty.GetMethod("Update");
Active = true;
}
else
{
Active = false;
}
}
catch (Exception e)
{
Active = false;
Console.WriteLine(e);
}
}
public void Update()
{
if (ipTask != null && ipTask.IsCompleted)
{
string localIP = ipTask.Result;
Console.WriteLine("IP RBC: " + localIP);
ulong IP = 0;
string[] bytes = localIP.Split('.');
for (int i = 0; i < bytes.Length; i++)
{
IP = IP << 8 | ulong.Parse(bytes[i]);
}
NID_RADIO = ulong.Parse((IP << 16 | 30993).ToString(), System.Globalization.NumberStyles.HexNumber);
ipTask = null;
}
if (Rbc != null)
{
try
{
RbcUpdate.Invoke(Rbc, null);
}
catch (Exception e)
{
Active = false;
Rbc = null;
Console.WriteLine(e);
}
}
}
public static async Task<string> GetIpAddress()
{
string localIP = null;
try
{
/*if (MPManager.IsMultiPlayer())
{
//localIP = (await new HttpClient().GetStringAsync("http://ipv4.icanhazip.com")).Replace("\\r\\n", "").Replace("\\n", "").Trim();
localIP = "127.0.0.1";
}
else*/
foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
{
// Check if the interface is Hamachi (it typically contains "Hamachi" in its name)
if (netInterface.Name.Contains("Hamachi") && netInterface.OperationalStatus == OperationalStatus.Up)
{
// Get the unicast IP addresses for this interface
foreach (UnicastIPAddressInformation ipAddress in netInterface.GetIPProperties().UnicastAddresses)
{
// We are interested in IPv4 addresses (ignoring IPv6 for simplicity)
if (ipAddress.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
localIP = ipAddress.Address.ToString();
break;
}
}
}
}
if (localIP == null)
{
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
{
await socket.ConnectAsync("8.8.8.8", 65530);
IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
localIP = endPoint.Address.ToString();
}
}
}
catch(Exception e)
{
localIP = "127.0.0.1";
}
return localIP;
}
}
}