[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")] // this usage is perfectly correct public static ModuleInfoReader FromFile(string filename = null) { ModuleInfoReader info = new ModuleInfoReader(); string data = Files.FromSavedGames("ModulesInfo.json"); if (data != null) { info = JsonConvert.DeserializeObject <ModuleInfoReader>(data); } return(info); }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")] // this usage is perfectly correct public static ModuleInfoReader FromFile(string filename = null) { ModuleInfoReader info = new ModuleInfoReader(); Regex Filter = new Regex(@"^ModulesInfo\.json$"); string directory = GetSavedGamesDir(); if (directory == null || directory.Trim() == "") { return(null); } FileInfo fileInfo = null; try { fileInfo = FindModuleInfoFile(directory, Filter); } catch (NotSupportedException nsex) { Logging.Error("Directory " + directory + " not supported: ", nsex); } if (fileInfo != null) { int maxTries = 6; while (IsFileLocked(fileInfo)) { Thread.Sleep(100); maxTries--; if (maxTries == 0) { Logging.Info("Unable to open Elite Dangerous ModulesInfo.json file"); return(null); } } using (FileStream fs = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (StreamReader reader = new StreamReader(fs, Encoding.UTF8)) { string data = string.Empty; fs.Seek(0, SeekOrigin.Begin); data = reader.ReadToEnd() ?? string.Empty; info = JsonConvert.DeserializeObject <ModuleInfoReader>(data); } } return(info); }
public static ModuleInfoReader FromFile(string filename = null) { Regex Filter = new Regex(@"^ModulesInfo\.json$"); string directory = GetSavedGamesDir(); if (directory == null || directory.Trim() == "") { return(null); } if (filename == null) { filename = directory + @"\ModulesInfo.json"; } ModuleInfoReader info = new ModuleInfoReader(); if (File.Exists(filename)) { try { string data = Files.Read(filename); if (data != null) { info = JsonConvert.DeserializeObject <ModuleInfoReader>(data); } } catch (Exception ex) { Logging.Debug("Failed to read modules info", ex); } } if (info == null) { info = new ModuleInfoReader(); } info.dataPath = filename; return(info); }
private void handleModuleInfoEvent(ModuleInfoEvent @event) { Ship ship = GetCurrentShip(); ModuleInfoReader info = ModuleInfoReader.FromFile(); if (info != null) { for (int i = 0; i < info.Modules.Count(); i++) { int position = i + 1; int priority = info.Modules[i].priority + 1; decimal power = info.Modules[i].power; string slot = info.Modules[i].slot; switch (slot) { case "CargoHatch": { ship.cargohatch.position = position; ship.cargohatch.priority = priority; ship.cargohatch.power = power; } break; case "FrameShiftDrive": { ship.frameshiftdrive.position = position; ship.frameshiftdrive.priority = priority; ship.frameshiftdrive.power = power; } break; case "LifeSupport": { ship.lifesupport.position = position; ship.lifesupport.priority = priority; ship.lifesupport.power = power; } break; case "MainEngines": { ship.thrusters.position = position; ship.thrusters.priority = priority; ship.thrusters.power = power; } break; case "PowerDistributor": { ship.powerdistributor.position = position; ship.powerdistributor.priority = priority; ship.powerdistributor.power = power; } break; case "PowerPlant": { ship.powerplant.position = position; ship.powerplant.priority = priority; ship.powerplant.power = power; } break; case "Radar": { ship.sensors.position = position; ship.sensors.priority = priority; ship.sensors.power = power; } break; case "ShipCockpit": { ship.canopy.position = position; ship.canopy.priority = priority; ship.canopy.power = power; } break; } if (slot.Contains("Slot")) { Compartment compartment = ship.compartments.FirstOrDefault(c => c.name == slot); if (compartment != null) { compartment.module.position = position; compartment.module.priority = priority; compartment.module.power = power; } } else if (slot.Contains("Hardpoint")) { Hardpoint hardpoint = ship.hardpoints.FirstOrDefault(h => h.name == slot); if (hardpoint != null) { hardpoint.module.position = position; hardpoint.module.priority = priority; hardpoint.module.power = power; } } } writeShips(); } }