diff options
author | Your Name <you@example.com> | 2019-01-16 20:35:21 +0900 |
---|---|---|
committer | Your Name <you@example.com> | 2019-01-16 20:35:21 +0900 |
commit | 26477a691fdaa382aa9337dc225a3251ed4e863f (patch) | |
tree | d12449675cd6e9a4190343c13d0e117602742f00 /WarframeClock | |
parent | 6eb17bebf7aeef6e936069e73539f074c5aa6c3d (diff) | |
download | wf-clock-26477a691fdaa382aa9337dc225a3251ed4e863f.tar.gz |
get rid of Newtonsoft.Json
Diffstat (limited to 'WarframeClock')
-rw-r--r-- | WarframeClock/Clock.cs | 50 | ||||
-rw-r--r-- | WarframeClock/ClockOverlay.cs | 34 | ||||
-rw-r--r-- | WarframeClock/Program.cs | 115 | ||||
-rw-r--r-- | WarframeClock/WarframeClock.csproj | 6 | ||||
-rw-r--r-- | WarframeClock/WorldState.cs | 51 |
5 files changed, 156 insertions, 100 deletions
diff --git a/WarframeClock/Clock.cs b/WarframeClock/Clock.cs new file mode 100644 index 0000000..9d1ffa7 --- /dev/null +++ b/WarframeClock/Clock.cs @@ -0,0 +1,50 @@ +using System; + +namespace WarframeClock +{ + public class Clock + { + public long CetusExpiry { get; set; } = -1; + + public string CetusExpiryString() + { + double currentTime = GetCurrentTime(); + double cetusLen = 8998.8748; + double cetusDayLen = 8248.9686 - 2249.7187; + double cetusBegin = 1509371722 + 2249.7187; + + if (CetusExpiry >= 0) + { + cetusBegin = CetusExpiry / 1000 - cetusLen; + } + cetusBegin += Math.Floor((currentTime - cetusBegin) / cetusLen) * cetusLen; + + if (currentTime < cetusBegin + cetusDayLen) + return $"Plains: ☀ {GetReadableTimeSpan(cetusBegin + cetusDayLen - currentTime)}"; + else + return $"Plains: 🌙 {GetReadableTimeSpan(cetusBegin + cetusLen - currentTime)}"; + } + + public string VallisExpiryString() + { + double currentTime = GetCurrentTime(); + double dayStart = 1542131224; + dayStart += Math.Floor((currentTime - dayStart) / 1600) * 1600; + + if (currentTime - dayStart < 400) + return $"Vallis: ❄️[→☁] {GetReadableTimeSpan(dayStart + 400 - currentTime)}"; + else if (currentTime - dayStart < 800) + return $"Vallis: ☁[→🌣] {GetReadableTimeSpan(dayStart + 800 - currentTime)}"; + else if (currentTime - dayStart < 1200) + return $"Vallis: 🌣[→☁] {GetReadableTimeSpan(dayStart + 1200 - currentTime)}"; + else + return $"Vallis: ☁[→❄️] {GetReadableTimeSpan(dayStart + 1600 - currentTime)}"; + } + + private double GetCurrentTime() => + DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds / 1000; + + private string GetReadableTimeSpan(double span) => + $"{Math.Floor(span / 60)}m{Math.Floor(span % 60)}s"; + } +} diff --git a/WarframeClock/ClockOverlay.cs b/WarframeClock/ClockOverlay.cs index 49fa4b6..1bb587b 100644 --- a/WarframeClock/ClockOverlay.cs +++ b/WarframeClock/ClockOverlay.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Threading; using SharpDX; using SharpDX.Direct2D1; using SharpDX.DirectWrite; @@ -13,7 +14,7 @@ namespace WarframeClock class ClockOverlay : IDisposable { private readonly string targetProcessName; - private Program.WorldStateDataStruct worldStateData; + private Clock clock; private IntPtr targetWindowHandle; private IntPtr handle; private bool isVisible; @@ -29,9 +30,9 @@ namespace WarframeClock private TextFormat textFont; private SolidColorBrush textBrush; - public ClockOverlay(Program.WorldStateDataStruct worldStateData, string targetProcessName) + public ClockOverlay(Clock clock, string targetProcessName) { - this.worldStateData = worldStateData; + this.clock = clock; this.targetProcessName = targetProcessName; SetupOverlayWindow(); @@ -111,7 +112,16 @@ namespace WarframeClock Native.DestroyWindow(handle); } - public void Update() + public void Start() + { + while (true) + { + Update(); + Thread.Sleep(100); + } + } + + private void Update() { GetTargetWindow(); if (targetWindowHandle == IntPtr.Zero) @@ -168,7 +178,7 @@ namespace WarframeClock device.BeginDraw(); device.Clear(transparent); - var infoText = $"{worldStateData.CetusExpiryString()} {worldStateData.VallisExpiryString()}"; + var infoText = $"{clock.CetusExpiryString()} {clock.VallisExpiryString()}"; device.DrawText(infoText, textFont, new RawRectangleF(30, parentBounds.Bottom - parentBounds.Top - 50, float.MaxValue, float.MaxValue), textBrush); @@ -185,13 +195,23 @@ namespace WarframeClock var process = System.Diagnostics.Process.GetProcessesByName(targetProcessName).FirstOrDefault(); if (process == null) { - Console.WriteLine($"ClockOverlay: Process with name={targetProcessName} not found; skipping"); return; } try { - targetWindowHandle = process.MainWindowHandle; + var handle = process.MainWindowHandle; + if (handle != IntPtr.Zero) + { + targetWindowHandle = handle; + Console.WriteLine($"ClockOverlay: Found a process with name={targetProcessName}; " + + $"MainWindowHandle={targetWindowHandle}"); + } + else + { + Console.WriteLine($"ClockOverlay: Found a process with name={targetProcessName}; " + + "but without a main window"); + } } catch (Exception) { diff --git a/WarframeClock/Program.cs b/WarframeClock/Program.cs index e4998f4..71514cb 100644 --- a/WarframeClock/Program.cs +++ b/WarframeClock/Program.cs @@ -1,75 +1,26 @@ using System; -using System.Net.Http; +using System.Linq; using System.Threading; using System.Windows.Forms; -using Newtonsoft.Json; namespace WarframeClock { class Program { - private static bool UseWorldStatePhp = false; - private static string VersionString = "Warframe Clock Overlay 2019-01-16"; - - public class WorldStateDataStruct - { - public long CetusExpiry { get; set; } = -1; - - public string CetusExpiryString() - { - double currentTime = GetCurrentTime(); - double cetusLen = 8998.8748; - double cetusDayLen = 8248.9686 - 2249.7187; - double cetusBegin = 1509371722 + 2249.7187; - - if (UseWorldStatePhp) - { - if (CetusExpiry < 0) - return "Plains: <NOT READY>"; - cetusBegin = CetusExpiry / 1000 - cetusLen; - } - cetusBegin += Math.Floor((currentTime - cetusBegin) / cetusLen) * cetusLen; - - if (currentTime < cetusBegin + cetusDayLen) - return $"Plains: ☀ {GetReadableTimeSpan(cetusBegin + cetusDayLen - currentTime)}"; - else - return $"Plains: 🌙 {GetReadableTimeSpan(cetusBegin + cetusLen - currentTime)}"; - } - - public string VallisExpiryString() - { - double currentTime = GetCurrentTime(); - double dayStart = 1542131224; - dayStart += Math.Floor((currentTime - dayStart) / 1600) * 1600; - - if (currentTime - dayStart < 400) - return $"Vallis: ❄️[→☁] {GetReadableTimeSpan(dayStart + 400 - currentTime)}"; - else if (currentTime - dayStart < 800) - return $"Vallis: ☁[→🌣] {GetReadableTimeSpan(dayStart + 800 - currentTime)}"; - else if (currentTime - dayStart < 1200) - return $"Vallis: 🌣[→☁] {GetReadableTimeSpan(dayStart + 1200 - currentTime)}"; - else - return $"Vallis: ☁[→❄️] {GetReadableTimeSpan(dayStart + 1600 - currentTime)}"; - } - - private double GetCurrentTime() => DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds / 1000; - - private string GetReadableTimeSpan(double span) => - $"{Math.Floor(span / 60)}m{Math.Floor(span % 60)}s"; - } - - public static WorldStateDataStruct WorldStateData = new WorldStateDataStruct(); + public static bool UseWorldStatePhp = true; + public static string VersionString = "Warframe Clock Overlay 2019-01-16"; + private static Clock clock; + private static System.Threading.Timer worldStateFetchTimer; [STAThread] static void Main(string[] args) { - StartNotifyIcon(); - StartOverlay(); + clock = new Clock(); + StartNotifyIcon(); if (UseWorldStatePhp) - { StartWorldStateFetch(); - } + StartOverlay(); } private static void StartNotifyIcon() @@ -99,51 +50,35 @@ namespace WarframeClock private static void StartWorldStateFetch() { - var httpClient = new HttpClient(); - - var period = 10 * 60 * 1000; // 10 Minutes - var timer = new System.Threading.Timer(state => + worldStateFetchTimer = new System.Threading.Timer(state => { lock (state) { - var res = httpClient.GetAsync("http://content.warframe.com/dynamic/worldState.php").Result; - if (!res.IsSuccessStatusCode) + try { - Console.WriteLine("WorldState: Could not fetch worldState.php"); - return; + var worldState = WorldState.Fetch(); + var cetusSyndicate = worldState.SyndicateMissions.FirstOrDefault(mi => mi.Tag == "CetusSyndicate"); + if (cetusSyndicate == null) + { + Console.WriteLine("WorldState: CetusSyndicate missions not found"); + return; + } + var cetusExpiry = cetusSyndicate.Expiry.Date.NumberLong; + Console.WriteLine($"WorldState: CetusSyndicate missions expire at {cetusExpiry}"); + clock.CetusExpiry = cetusExpiry; } - - var json = res.Content.ReadAsStringAsync().Result; - dynamic obj = JsonConvert.DeserializeObject(json); - - dynamic cetusSyndicate = null; - foreach (var syndicateMission in obj.SyndicateMissions) - { - if (syndicateMission.Tag == "CetusSyndicate") - cetusSyndicate = syndicateMission; - } - if (cetusSyndicate == null) + catch (Exception ex) { - Console.WriteLine("WorldState: CetusSyndicate missions not found"); - return; + Console.WriteLine($"WorldState: Failed to fetch or parse worldState.php: {ex.ToString()}"); + Console.WriteLine(ex.StackTrace); } - - dynamic cetusExpiry = cetusSyndicate.Expiry["$date"]["$numberLong"]; - Console.WriteLine($"WorldState: CetusSyndicate missions expire at {cetusExpiry}"); - - WorldStateData.CetusExpiry = cetusExpiry; } - }, new object(), 0, period); + }, new object(), 0, 10 * 60 * 1000 /* 10 minutes */); } private static void StartOverlay() { - var plugin = new ClockOverlay(WorldStateData, "Warframe.x64"); - while (true) - { - plugin.Update(); - Thread.Sleep(100); - } + new ClockOverlay(clock, "Warframe.x64").Start(); } } } diff --git a/WarframeClock/WarframeClock.csproj b/WarframeClock/WarframeClock.csproj index 2a95d33..88e8198 100644 --- a/WarframeClock/WarframeClock.csproj +++ b/WarframeClock/WarframeClock.csproj @@ -62,9 +62,6 @@ <Reference Include="Costura, Version=3.3.0.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL"> <HintPath>..\packages\Costura.Fody.3.3.0\lib\net40\Costura.dll</HintPath> </Reference> - <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> - <HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath> - </Reference> <Reference Include="SharpDX, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL"> <HintPath>..\packages\SharpDX.4.2.0\lib\net45\SharpDX.dll</HintPath> </Reference> @@ -77,6 +74,7 @@ <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Drawing" /> + <Reference Include="System.Runtime.Serialization" /> <Reference Include="System.Windows.Forms" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> @@ -86,6 +84,7 @@ <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> + <Compile Include="Clock.cs" /> <Compile Include="ClockOverlay.cs" /> <Compile Include="Native.cs" /> <Compile Include="Program.cs" /> @@ -95,6 +94,7 @@ <DesignTime>True</DesignTime> <DependentUpon>Resources.resx</DependentUpon> </Compile> + <Compile Include="WorldState.cs" /> </ItemGroup> <ItemGroup> <None Include="App.config" /> diff --git a/WarframeClock/WorldState.cs b/WarframeClock/WorldState.cs new file mode 100644 index 0000000..b364660 --- /dev/null +++ b/WarframeClock/WorldState.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Json; + +namespace WarframeClock +{ + [DataContract] + public class WorldState + { + [DataMember(Name = "SyndicateMissions")] + public List<WorldStateSyndicateMission> SyndicateMissions; + + [DataContract] + public class WorldStateSyndicateMission + { + [DataMember(Name = "Tag")] + public string Tag { get; set; } + [DataMember(Name = "Expiry")] + public WorldStateDate Expiry { get; set; } + } + + [DataContract] + public class WorldStateDate + { + [DataMember(Name = "$date")] + public WorldStateNumberLong Date { get; set; } + } + + [DataContract] + public class WorldStateNumberLong + { + [DataMember(Name = "$numberLong")] + public long NumberLong { get; set; } + } + + public static WorldState Fetch() + { + var httpClient = new HttpClient(); + var res = httpClient.GetAsync("http://content.warframe.com/dynamic/worldState.php").Result; + if (!res.IsSuccessStatusCode) + { + throw new Exception("WorldState: Could not fetch worldState.php"); + } + + var serializer = new DataContractJsonSerializer(typeof(WorldState)); + return (WorldState)serializer.ReadObject(res.Content.ReadAsStreamAsync().Result); + } + } +} |