private async Task ProcessPacket(IPacket thisPacket) { if (thisPacket == null) { return; } for (int i = 0; i < PacketWaiters.Count; i++) { PacketWaiter thisWaiter = PacketWaiters[i]; thisWaiter.CheckIfReceived(thisPacket); } try { await Task.Run(() => PacketProcessor(this, thisPacket)); } catch (NotImplementedException e) { Debug.AddErrorMessage(e, "Packet Processing for type " + thisPacket.Type + " is not yet implemented."); //this.SendMessageAsync("Sorry, that feature or packet is not implemented yet! Please try something else..."); } catch (Exception e) { Debug.AddErrorMessage(e, "Packet Processing for type " + thisPacket.Type + " encountered an error."); this.SendMessageAsync("There was an error processing on of your packets. You haven't been disconnected, but you might have problems on the server from here!").ConfigureAwait(false); } }
public bool Load() { try { Contents = System.IO.File.ReadAllLines(Filename); return(true); } catch (ArgumentNullException e) { Debug.AddErrorMessage(e, "path is null."); return(false); } catch (ArgumentException e) { Debug.AddErrorMessage(e, "path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars."); return(false); } catch (PathTooLongException e) { Debug.AddErrorMessage(e, "The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters."); return(false); } catch (DirectoryNotFoundException e) { Debug.AddErrorMessage(e, "The specified path is invalid (for example, it is on an unmapped drive)."); return(false); } catch (FileNotFoundException e) { Debug.AddErrorMessage(e, "The file specified in path was not found."); return(false); } catch (IOException e) { Debug.AddErrorMessage(e, "an I/O error occurred while opening the file."); return(false); } catch (UnauthorizedAccessException e) { Debug.AddErrorMessage(e, "path specified a file that is read-only.\r\n\r\n-or-\r\n\r\nThis operation is not supported on the current platform.\r\n\r\n-or-\r\n\r\npath specified a directory.\r\n\r\n-or-\r\n\r\nThe caller does not have the required permission."); return(false); } catch (NotSupportedException e) { Debug.AddErrorMessage(e, "path is in an invalid format."); return(false); } catch (System.Security.SecurityException e) { Debug.AddErrorMessage(e, "The caller does not have the required permission."); return(false); } catch (Exception e) { Debug.AddErrorMessage(e, "Unknown error, general Exception."); return(false); } }
internal static object ConvertParameterStringToObject(Type desiredType, string parameters, object defaultObject) { try { //DO NOT ADD CUSTOM CLASSES TO CONVERT TYPE! USE A DATABASE PRIMARY KEY INSTEAD! //System Objects #region Boolean if (desiredType == typeof(Boolean)) { Boolean output; return(Boolean.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region Byte if (desiredType == typeof(Byte)) { Byte output; return(Byte.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region SByte if (desiredType == typeof(SByte)) { SByte output; return(SByte.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region Int16 if (desiredType == typeof(Int16)) { Int16 output; return(Int16.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region UInt16 if (desiredType == typeof(UInt16)) { UInt16 output; return(UInt16.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region Int32 if (desiredType == typeof(Int32)) { Int32 output; return(Int32.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region UInt32 if (desiredType == typeof(UInt32)) { UInt32 output; return(UInt32.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region Int64 if (desiredType == typeof(Int64)) { Int64 output; return(Int64.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region UInt64 if (desiredType == typeof(UInt64)) { UInt64 output; return(UInt64.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region Single if (desiredType == typeof(Single)) { Single output; return(Single.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region Double if (desiredType == typeof(Double)) { Double output; return(Double.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region IPAddress if (desiredType == typeof(IHostAddress)) { IHostAddress Default = defaultObject as IHostAddress; try { IHostAddress HostAddress = ObjectFactory.CreateHostAddress(parameters); return((Default.IpAddress.ToString() == HostAddress.IpAddress.ToString()) ? Default : HostAddress); } catch { } return(defaultObject); } #endregion #region String if (desiredType == typeof(String)) { return(parameters); } #endregion //Units Of Measurement #region Angle if (desiredType == typeof(IAngle)) { IAngle nullConversion; ObjectFactory.TryParse("0DEGREES", out nullConversion); IAngle converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Area if (desiredType == typeof(IArea)) { IArea nullConversion; ObjectFactory.TryParse("0SQUAREMETERS", out nullConversion); IArea converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Distance if (desiredType == typeof(IDistance)) { IDistance nullConversion; ObjectFactory.TryParse("0METERS", out nullConversion); IDistance converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Durations if (desiredType == typeof(IDate)) { IDate nullConversion = ObjectFactory.CreateDate(""); IDate converted = ObjectFactory.CreateDate(parameters); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } if (desiredType == typeof(IDateTime)) { IDateTime nullConversion = ObjectFactory.CreateDateTime(""); IDateTime converted = ObjectFactory.CreateDateTime(parameters); return((converted.ToString() == nullConversion.ToSystemString()) ? defaultObject : converted); } if (desiredType == typeof(ITime)) { ITime nullConversion = ObjectFactory.CreateTime(""); ITime converted = ObjectFactory.CreateTime(parameters); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } if (desiredType == typeof(ITimeSpan)) { ITimeSpan nullConversion = ObjectFactory.CreateTimeSpan(""); ITimeSpan converted = ObjectFactory.CreateTimeSpan(parameters); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Energy if (desiredType == typeof(IEnergy)) { IEnergy nullConversion; ObjectFactory.TryParse("0KILOJOULES", out nullConversion); IEnergy converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Mass if (desiredType == typeof(IMass)) { IMass nullConversion; ObjectFactory.TryParse("0KILOGRAMS", out nullConversion); IMass converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Power if (desiredType == typeof(IPower)) { IPower nullConversion; ObjectFactory.TryParse("0KILOWATTS", out nullConversion); IPower converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Pressure if (desiredType == typeof(IPressure)) { IPressure nullConversion; ObjectFactory.TryParse("0PASCALS", out nullConversion); IPressure converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Speed if (desiredType == typeof(ISpeed)) { ISpeed nullConversion; ObjectFactory.TryParse("0M/SEC", out nullConversion); ISpeed converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Temperature if (desiredType == typeof(ITemperature)) { ITemperature nullConversion; ObjectFactory.TryParse("0CELCIUS", out nullConversion); ITemperature converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Volume if (desiredType == typeof(IVolume)) { IVolume nullConversion; ObjectFactory.TryParse("0LITRES", out nullConversion); IVolume converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion //Colors #region XRGBColor if (desiredType == typeof(I24BitColor)) { string[] split = parameters.Split(' '); byte red = 255; byte green = 255; byte blue = 255; I24BitColor Default = defaultObject as I24BitColor; if (split.Length < 3) { return(Default); } try { red = Convert.ToByte(split[0]); green = Convert.ToByte(split[1]); blue = Convert.ToByte(split[2]); } catch (OverflowException) { //Debug return(Default); } catch (FormatException) { //Debug return(Default); } I24BitColor output = ObjectFactory.CreateColor(red, green, blue).Get24BitColor(); return(output); } #endregion } catch (Exception e) { Debug.AddErrorMessage(e, "Error converting setting from string."); } return(defaultObject); }
/// <summary> /// Searches the YSFlightDirectory for the Scenery Folder, and loads all Scenery Lists from it. /// </summary> /// <returns></returns> public static bool LoadAll() { Debug.AddSummaryMessage("Starting MetaData.Scenery.LoadAll()"); Extensions.YSFlight.MetaData.Scenery.List.Clear(); int loadingErrors = 0; #region Load All Scenery MetaData try { string YSFlightSceneryDirectory = SettingsLibrary.Settings.YSFlight.Directory + @"Scenery/"; #region Scenery Directory Not Found on Disk if (!Directory.Exists(YSFlightSceneryDirectory)) { Debug.AddErrorMessage(new DirectoryNotFoundException(), "YSFlight Scenery Directory Not Found."); return(false); } #endregion #region Initialise Variables string[] FullFilenames = Directory.GetFiles(YSFlightSceneryDirectory); string[] Filenames = FullFilenames.Select(Path.GetFileName).ToArray(); string[] SceneryLists = Filenames .Where(x => x.ToUpperInvariant().StartsWith(@"SCE") && x.ToUpperInvariant().EndsWith(@".LST")).ToArray(); #endregion #region Load Each Scenery List foreach (string thisSceneryListFile in SceneryLists) { #region Aircraft List Not Found on Disk if (!File.Exists(YSFlightSceneryDirectory + thisSceneryListFile)) { string message = "Aircraft List defined in directory tree not found."; Debug.AddWarningMessage(message); loadingErrors++; continue; } #endregion #region Get Lines with .FLD Definitions string[] SceneryListContents = File.ReadAllLines(YSFlightSceneryDirectory + thisSceneryListFile); SceneryListContents = SceneryListContents.Where(x => x.ToUpperInvariant().Contains(@".FLD")).ToArray(); #endregion #region Iterate over LST Contents for (int i = 0; i < SceneryListContents.Length; i++) { #region Update Line Number and Contents string thisLine = SceneryListContents[i]; string ProcessedLine = thisLine.Replace("\\", "/"); string[] SplitString = ProcessedLine.ToUpperInvariant().SplitPresevingQuotes(); #endregion if (thisLine == "") { continue; //skip blank lines. } #region Initialise Variables string SceneryPath1Fld = ""; string SceneryPath2Stp = ""; string SceneryPath3Yfs = ""; string Identify = ""; #endregion #region Assign File Paths for This Scenery switch (SplitString.Length - 1) { default: if (SplitString.Length > 3) { goto case 3; } break; case 3: SceneryPath3Yfs = SplitString[3]; goto case 2; case 2: SceneryPath2Stp = SplitString[2]; goto case 1; case 1: SceneryPath1Fld = SplitString[1]; goto case 0; case 0: Identify = SplitString[0]; break; } #endregion #region Create a New MetaScenery Scenery NewMetaScenery = new Scenery(Identify); NewMetaScenery.Path_1_FieldFile = SceneryPath1Fld; NewMetaScenery.Path_2_StartPositionFile = SceneryPath2Stp; NewMetaScenery.Path_3_YFSFile = SceneryPath3Yfs; #endregion Extensions.YSFlight.MetaData.Scenery.List.Add(NewMetaScenery); } #endregion } #endregion } catch (Exception e) { string message = "MetaData.Scenery.LoadAll Crashed!"; Debug.AddErrorMessage(e, message); loadingErrors++; } #endregion Debug.AddSummaryMessage("Finished MetaData.Scenery.LoadAll()" + (loadingErrors > 0 ? (" with " + loadingErrors + " errors.") : ("."))); return(loadingErrors <= 0); }
internal static bool FindAndUpdateSetting(string command, string parameters) { try { #region Split Command string[] commandSplit = command.Split('.'); #endregion #region Find and Modify the Setting. object TargetClass = Settings; PropertyInfo TargetProperty = typeof(SettingsLibrary).GetProperty("Settings", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Static); for (int i = 0; i < commandSplit.Length; i++) { #region Found a Matching Sub-Class, Move into it. if (i < commandSplit.Length - 1) { try { TargetProperty = TargetProperty.PropertyType.GetProperty(commandSplit[i], BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); TargetClass = TargetProperty.GetValue(TargetClass); } catch (Exception e) { Debug.AddErrorMessage(e, "Setting Not Found: \"" + command + "\"."); return(false); } continue; } #endregion #region Convert the Paramters to an Object PropertyInfo thisProperty = TargetProperty.PropertyType.GetProperty(commandSplit[i], BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); object valueToSet = ConvertParameterStringToObject(thisProperty.PropertyType, parameters, thisProperty.GetValue(TargetClass)); #endregion #region Set the Field to the Converted Value thisProperty.SetValue(TargetClass, valueToSet); Debug.AddDetailMessage("Successfully Read Setting: \"" + command + "\"."); return(true); #endregion } #endregion #region Couldn't Find the Correct Setting. return(false); #endregion } catch (Exception e) { Debug.AddErrorMessage(e, "Error in FindAndUpdateSetting"); return(false); } }