-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainFrm.cs
171 lines (148 loc) · 5.43 KB
/
mainFrm.cs
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using FSUIPC;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FSTimeSync
{
public partial class mainFrm : AppFormBase
{
public mainFrm()
{
InitializeComponent();
RegisterMoveableControl(topLabel);
topLabel.Text = "FS Time Sync v" + FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileVersion;
BackColor = Color.FromArgb(133, 187, 92);
btnConnect.Text = "Connect";
forceUtc = false;
tmrMinute = new Timer();
tmrMinute.Interval = 1000;
tmrMinute.Tick += TmrMinute_Tick;
tmrMinute.Start();
}
private void TmrMinute_Tick(object sender, EventArgs e)
{
if (forceUtc)
{
ForceSimToUTC(EnvironmentDateTime);
}
}
private void btnConnect_Click(object sender, EventArgs e)
{
switch (btnConnect.Text)
{
case "Connect":
case "Nop. Connect again":
if (ConnectToSim())
btnConnect.Text = "Impose UTC";
else
btnConnect.Text = "Nop. Try connect again";
break;
case "Impose UTC":
forceUtc = true;
btnConnect.Text = "Stop imposing UTC";
break;
case "Stop imposing UTC":
forceUtc = false;
btnConnect.Text = "Impose UTC";
break;
}
}
#region local variables
Timer tmrMinute;
bool forceUtc;
private Offset<byte[]> environmentDateTime = new Offset<byte[]>(0x0238, 10);
static private Offset<byte[]> environmentDateTimeDayOfYear = new Offset<byte[]>(0x023E, 4);
static private Offset<byte[]> environmentDateTimeHour = new Offset<byte[]>(0x023B, 4);
static private Offset<byte[]> environmentDateTimeMinute = new Offset<byte[]>(0x023C, 4);
static private Offset<byte[]> environmentDateTimeYear = new Offset<byte[]>(0x0240, 4);
private DateTime EnvironmentDateTime
{
get
{
try
{
FSUIPCConnection.Process();
// get year in Simulator
short year = BitConverter.ToInt16(environmentDateTime.Value, 8);
// create a time based on Jan 1 of Simulator year
DateTime result = new DateTime(year, 1, 1, environmentDateTime.Value[0], environmentDateTime.Value[1], environmentDateTime.Value[2]);
// get day of year from Simulator, and add that to the above time
short dayOfYear = BitConverter.ToInt16(environmentDateTime.Value, 6);
// add and return
return result.Add(new TimeSpan(dayOfYear - 1, 0, 0, 0));
}
catch (FSUIPCException crap)
{
switch (crap.FSUIPCErrorCode)
{
case FSUIPCError.FSUIPC_ERR_NOTOPEN:
break;
}
return DateTime.MinValue;
}
}
set
{
environmentDateTimeDayOfYear.Value = BitConverter.GetBytes(value.DayOfYear);
environmentDateTimeHour.Value = BitConverter.GetBytes(value.Hour);
environmentDateTimeMinute.Value = BitConverter.GetBytes(value.Minute);
environmentDateTimeYear.Value = BitConverter.GetBytes(value.Year);
try
{
FSUIPCConnection.Process();
}
catch (FSUIPCException crap)
{
}
}
}
#endregion local variables
private bool ConnectToSim()
{
try
{
FSUIPCConnection.Open();
return true;
}
catch (FSUIPCException crap)
{
switch (crap.FSUIPCErrorCode)
{
case FSUIPCError.FSUIPC_ERR_NOFS:
break;
case FSUIPCError.FSUIPC_ERR_OPEN:
return true;
}
}
return false;
}
private bool ForceSimToUTC(DateTime _sim)
{
// adjust dates, since they don't really matter for us
_sim = _sim.AddDays(Math.Truncate((DateTime.UtcNow - _sim).TotalDays));
// calculate diff
TimeSpan timeDiff = (DateTime.UtcNow > _sim) ? DateTime.UtcNow - _sim : _sim - DateTime.UtcNow;
if (Math.Truncate(timeDiff.TotalMinutes) > 1)
{
EnvironmentDateTime = DateTime.UtcNow;
return true;
}
return false;
}
private void btnClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnMinimize_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}
}
}