170 lines
6.6 KiB
C#
170 lines
6.6 KiB
C#
using Plane.Communication;
|
|
using Plane.Copters;
|
|
using Plane.FormationCreator.Formation;
|
|
using Plane.Logging;
|
|
using Plane.Windows;
|
|
using Plane.Windows.Messages;
|
|
using GalaSoft.MvvmLight.Ioc;
|
|
using Microsoft.Practices.ServiceLocation;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
|
|
namespace Plane.FormationCreator
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for App.xaml
|
|
/// </summary>
|
|
public partial class App : Application
|
|
{
|
|
public App()
|
|
{
|
|
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
|
|
|
|
ServiceLocatorConfigurer.Instance.Configure();
|
|
|
|
_logger = ServiceLocator.Current.GetInstance<ILogger>();
|
|
_copterManager = ServiceLocator.Current.GetInstance<CopterManager>();
|
|
_formationController = ServiceLocator.Current.GetInstance<FormationController>();
|
|
|
|
AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
|
|
{
|
|
var simpleName = new AssemblyName(e.Name).Name;
|
|
if (simpleName.EndsWith(".resources"))
|
|
{
|
|
return null;
|
|
}
|
|
return LoadAssembly(simpleName);
|
|
};
|
|
this.DispatcherUnhandledException += async (s, e) =>
|
|
{
|
|
_logger.Log(e.Exception);
|
|
await _formationController.AllStop();
|
|
TcpServerConnectionManager.Instance.StopListening();
|
|
UdpServerConnectionManager.Instance.StopReceiving();
|
|
};
|
|
this.Exit += (s, e) =>
|
|
{
|
|
try
|
|
{
|
|
TcpServerConnectionManager.Instance.StopListening();
|
|
UdpServerConnectionManager.Instance.StopReceiving();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// RaiseExceptionThrown(ex);
|
|
}
|
|
|
|
};
|
|
//new Test().Prepare().Run();
|
|
}
|
|
|
|
private ILogger _logger;
|
|
private CopterManager _copterManager;
|
|
private FormationController _formationController;
|
|
|
|
private Assembly LoadAssembly(string simpleName)
|
|
{
|
|
String resourceName = this.GetType().Namespace + ".AssemblyLoadingAndReflection." + simpleName + ".dll";
|
|
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
|
|
{
|
|
Byte[] assemblyData = new Byte[stream.Length];
|
|
stream.Read(assemblyData, 0, assemblyData.Length);
|
|
return Assembly.Load(assemblyData);
|
|
}
|
|
}
|
|
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
{
|
|
base.OnStartup(e);
|
|
|
|
var md = Resources.MergedDictionaries;
|
|
// md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml") });
|
|
md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml") });
|
|
md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml") });
|
|
md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/VS/Colors.xaml") });
|
|
md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/VS/Styles.xaml") });
|
|
|
|
md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/Plane.Windows.Messages;component/Styles.xaml") });
|
|
|
|
md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/Styles/Colors.xaml") });
|
|
md.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/Styles.xaml") });
|
|
/*
|
|
new UpdateChecker("http://dl.Plane.com/tools/ver.php?app=FormationCreator", _logger)
|
|
.CheckAsync(ver =>
|
|
{
|
|
var currentVersion = this.GetType().Assembly.GetName().Version;
|
|
if (currentVersion < ver
|
|
&& Alert.Show("检测到新版本,请下载。", "更新提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
|
{
|
|
Process.Start("http://dl.Plane.com/tools/FormationCreatorSetup.exe");
|
|
this.Shutdown();
|
|
}
|
|
});
|
|
*/
|
|
MainWindow = new MainWindow();
|
|
MainWindow.Show();
|
|
|
|
TcpServerConnectionManager.Instance.ConnectionEstablished += ConnectionManager_ConnectionEstablished;
|
|
if (!TcpServerConnectionManager.Instance.StartListening())
|
|
{
|
|
Alert.Show("网络连接不正常,无法启动监听。");
|
|
return;
|
|
}
|
|
UdpServerConnectionManager.Instance.ExceptionThrown += (sender, e1) =>
|
|
{
|
|
_logger.Log(e1.Exception);
|
|
};
|
|
UdpServerConnectionManager.Instance.ConnectionEstablished += ConnectionManager_ConnectionEstablished;
|
|
UdpServerConnectionManager.Instance.StartReceiving();
|
|
}
|
|
private void Copter_TextReceived(object sender, MessageCreatedEventArgs e)
|
|
{
|
|
_logger.Log(e.Message );
|
|
}
|
|
private async Task AddOrUpdateCopter(string ip, IConnection Connection)
|
|
{
|
|
var copters = _copterManager.Copters;
|
|
var copterStatus = _copterManager.CopterStatus;
|
|
|
|
var copter = copters.FirstOrDefault(c => c.Id == ip);
|
|
if (copter == null)
|
|
{
|
|
await Dispatcher.BeginInvoke(new Action(() =>
|
|
{
|
|
copter = new Copter(Connection, SynchronizationContext.Current)
|
|
{
|
|
Id = ip,
|
|
Name = ip.Substring(ip.LastIndexOf('.') + 1)
|
|
};
|
|
int _index;
|
|
_index=copters.AddCopter(copter);
|
|
copterStatus.Insert(_index,false);
|
|
|
|
copter.TextReceived += Copter_TextReceived;
|
|
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
copter.Connection = Connection;
|
|
}
|
|
await copter.ConnectAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
private async void ConnectionManager_ConnectionEstablished(object sender, ConnectionEstablishedEventArgs e)
|
|
{
|
|
await AddOrUpdateCopter(e.RemoteAddress, e.Connection);
|
|
}
|
|
}
|
|
}
|