protected static void setCargo(CargoMonitor cargoMonitor, ref dynamic vaProxy) { try { vaProxy.SetInt("Ship cargo carried", cargoMonitor?.cargoCarried ?? 0); vaProxy.SetInt("Ship limpets carried", cargoMonitor?.GetCargoWithEDName("Drones")?.total ?? 0); } catch (Exception ex) { Logging.Error("Failed to set ship cargo values", ex); } }
public static void VA_Init1(dynamic vaProxy) { Logging.Info("Initialising EDDI VoiceAttack plugin"); EDDI.FromVA = true; try { GetEddiInstance(ref vaProxy); Logging.incrementLogs(); App.StartRollbar(); App.ApplyAnyOverrideCulture(); EDDI.Instance.Start(); // Set up our event responder VoiceAttackResponder.RaiseEvent += (s, theEvent) => { try { eventQueue.Enqueue(theEvent); Thread eventHandler = new Thread(() => dequeueEvent(ref vaProxy)) { Name = "VoiceAttackEventHandler", IsBackground = true }; eventHandler.Start(); eventHandler.Join(); } catch (ThreadAbortException tax) { Thread.ResetAbort(); Logging.Debug("Thread aborted", tax); } catch (Exception ex) { Dictionary <string, object> data = new Dictionary <string, object> { { "event", JsonConvert.SerializeObject(theEvent) }, { "exception", ex.Message }, { "stacktrace", ex.StackTrace } }; Logging.Error("VoiceAttack failed to handle event.", data); } }; // Add notifiers for changes in variables we want to react to // (we can only use event handlers with classes which are always constructed - nullable objects will be updated via responder events) EDDI.Instance.State.CollectionChanged += (s, e) => setDictionaryValues(EDDI.Instance.State, "state", ref vaProxy); SpeechService.Instance.PropertyChanged += (s, e) => setSpeaking(SpeechService.Instance.eddiSpeaking, ref vaProxy); CargoMonitor cargoMonitor = (CargoMonitor)EDDI.Instance.ObtainMonitor("Cargo monitor"); cargoMonitor.InventoryUpdatedEvent += (s, e) => { lock (vaProxyLock) { setCargo(cargoMonitor, ref vaProxy); } }; ShipMonitor shipMonitor = (ShipMonitor)EDDI.Instance.ObtainMonitor("Ship monitor"); shipMonitor.ShipyardUpdatedEvent += (s, e) => { lock (vaProxyLock) { setShipValues(shipMonitor?.GetCurrentShip(), "Ship", ref vaProxy); Task.Run(() => setShipyardValues(shipMonitor?.shipyard?.ToList(), ref vaProxy)); } }; StatusMonitor statusMonitor = (StatusMonitor)EDDI.Instance.ObtainMonitor("Status monitor"); statusMonitor.StatusUpdatedEvent += (s, e) => { lock (vaProxyLock) { setStatusValues(statusMonitor?.currentStatus, "Status", ref vaProxy); } }; // Display instance information if available if (EDDI.Instance.UpgradeRequired) { vaProxy.WriteToLog("Please shut down VoiceAttack and run EDDI standalone to upgrade", "red"); string msg = Properties.VoiceAttack.run_eddi_standalone; SpeechService.Instance.Say(((ShipMonitor)EDDI.Instance.ObtainMonitor("Ship monitor")).GetCurrentShip(), msg, 0); } else if (EDDI.Instance.UpgradeAvailable) { vaProxy.WriteToLog("Please shut down VoiceAttack and run EDDI standalone to upgrade", "orange"); string msg = Properties.VoiceAttack.run_eddi_standalone; SpeechService.Instance.Say(((ShipMonitor)EDDI.Instance.ObtainMonitor("Ship monitor")).GetCurrentShip(), msg, 0); } if (EDDI.Instance.Motd != null) { vaProxy.WriteToLog("Message from EDDI: " + EDDI.Instance.Motd, "black"); string msg = String.Format(Eddi.Properties.EddiResources.msg_from_eddi, EDDI.Instance.Motd); SpeechService.Instance.Say(((ShipMonitor)EDDI.Instance.ObtainMonitor("Ship monitor")).GetCurrentShip(), msg, 0); } // Set the initial values from the main EDDI objects setStandardValues(ref vaProxy); vaProxy.WriteToLog("The EDDI plugin is fully operational.", "green"); setStatus(ref vaProxy, "Operational"); // Fire an event once the VA plugin is initialized Event @event = new VAInitializedEvent(DateTime.UtcNow); if (initEventEnabled(@event.type)) { EDDI.Instance.enqueueEvent(@event); } // Set a variable indicating the version of VoiceAttack in use System.Version v = vaProxy.VAVersion; EDDI.Instance.vaVersion = v.ToString(); // Set a variable indicating whether EDDI is speaking try { setSpeaking(SpeechService.Instance.eddiSpeaking, ref vaProxy); } catch (Exception ex) { Logging.Error("Failed to set initial speaking status", ex); } Logging.Info("EDDI VoiceAttack plugin initialization complete"); } catch (Exception e) { Logging.Error("Failed to initialize VoiceAttack plugin", e); vaProxy.WriteToLog("Unable to fully initialize EDDI. Some functions may not work.", "red"); } }
public static void VA_Init1(dynamic vaProxy) { // Initialize and launch an EDDI instance without opening the main window // VoiceAttack commands will be used to manipulate the window state. App.vaProxy = vaProxy; if (App.AlreadyRunning()) { return; } Thread appThread = new Thread(App.Main); appThread.SetApartmentState(ApartmentState.STA); appThread.Start(); try { int timeout = 0; while (Application.Current == null) { if (timeout < 200) { Thread.Sleep(50); timeout++; } else { throw new TimeoutException("EDDI VoiceAttack plugin initialisation has timed out"); } } Logging.Info("Initialising EDDI VoiceAttack plugin"); // Set up our event responder. VoiceAttackResponder.RaiseEvent += (s, theEvent) => { if (theEvent is null) { return; } if (eventQueues.ContainsKey(theEvent.type)) { // Add our event to an existing blocking collection for that event type. eventQueues[theEvent.type].Add(theEvent); } else { // Add our event to a new blocking collection for that event type and start a consumer task for that collection eventQueues[theEvent.type] = new BlockingCollection <Event> { theEvent }; var consumerTask = Task.Run(() => { // ReSharper disable once AccessToModifiedClosure - OK to use vaProxy in this context. dequeueEvents(eventQueues[theEvent.type], ref vaProxy); }); consumerTasks.Add(consumerTask); } }; // Add notifiers for changes in variables we want to react to // (we can only use event handlers with classes which are always constructed - nullable objects will be updated via responder events) EDDI.Instance.PropertyChanged += (s, e) => updateStandardValues(e); EDDI.Instance.State.CollectionChanged += (s, e) => setDictionaryValues(EDDI.Instance.State, "state", ref vaProxy); SpeechService.Instance.PropertyChanged += (s, e) => setSpeechState(e); CompanionAppService.Instance.StateChanged += (oldState, newState) => setCAPIState(newState == CompanionAppService.State.Authorized, ref vaProxy); CargoMonitor cargoMonitor = (CargoMonitor)EDDI.Instance.ObtainMonitor("Cargo monitor"); cargoMonitor.InventoryUpdatedEvent += (s, e) => { lock (vaProxyLock) { setCargo(cargoMonitor, ref vaProxy); } }; ShipMonitor shipMonitor = (ShipMonitor)EDDI.Instance.ObtainMonitor("Ship monitor"); if (shipMonitor != null) { shipMonitor.ShipyardUpdatedEvent += (s, e) => { lock (vaProxyLock) { setShipValues(shipMonitor.GetCurrentShip(), "Ship", ref vaProxy); Task.Run(() => setShipyardValues(shipMonitor.shipyard?.Copy().ToList(), ref vaProxy)); } }; } StatusMonitor statusMonitor = (StatusMonitor)EDDI.Instance.ObtainMonitor("Status monitor"); if (statusMonitor != null) { statusMonitor.StatusUpdatedEvent += (s, e) => { lock (vaProxyLock) { setStatusValues(statusMonitor.currentStatus, "Status", ref vaProxy); } }; } // Set initial values for standard variables initializeStandardValues(); // Display instance information if available if (EddiUpgrader.UpgradeRequired) { vaProxy.WriteToLog("Please shut down VoiceAttack and run EDDI standalone to upgrade", "red"); string msg = Properties.VoiceAttack.run_eddi_standalone; SpeechService.Instance.Say(null, msg, 0); } else if (EddiUpgrader.UpgradeAvailable) { vaProxy.WriteToLog("Please shut down VoiceAttack and run EDDI standalone to upgrade", "orange"); string msg = Properties.VoiceAttack.run_eddi_standalone; SpeechService.Instance.Say(null, msg, 0); } if (EddiUpgrader.Motd != null) { vaProxy.WriteToLog("Message from EDDI: " + EddiUpgrader.Motd, "black"); string msg = String.Format(Eddi.Properties.EddiResources.msg_from_eddi, EddiUpgrader.Motd); SpeechService.Instance.Say(null, msg, 0); } vaProxy.WriteToLog("The EDDI plugin is fully operational.", "green"); setStatus(ref vaProxy, "Operational"); // Fire an event once the VA plugin is initialized EDDI.Instance.enqueueEvent(new VAInitializedEvent(DateTime.UtcNow)); // Set a variable indicating the version of VoiceAttack in use System.Version v = vaProxy.VAVersion; EDDI.Instance.vaVersion = v.ToString(); Logging.Info("EDDI VoiceAttack plugin initialization complete"); } catch (Exception e) { Logging.Error("Failed to initialize VoiceAttack plugin", e); vaProxy.WriteToLog("Unable to fully initialize EDDI. Some functions may not work.", "red"); } }
/// <summary>Set all values</summary> public static void setStandardValues(ref dynamic vaProxy) { // Update our primary objects only if they don't match the state of the EDDI instance. try { if (EDDI.Instance.CurrentStarSystem != CurrentStarSystem) { setStarSystemValues(EDDI.Instance.CurrentStarSystem, "System", ref vaProxy); CurrentStarSystem = EDDI.Instance.CurrentStarSystem; } } catch (Exception ex) { Logging.Error("Failed to set current system", ex); } try { if (EDDI.Instance.LastStarSystem != LastStarSystem) { setStarSystemValues(EDDI.Instance.LastStarSystem, "Last system", ref vaProxy); LastStarSystem = EDDI.Instance.LastStarSystem; } } catch (Exception ex) { Logging.Error("Failed to set last system", ex); } try { if (EDDI.Instance.SquadronStarSystem != SquadronStarSystem) { setStarSystemValues(EDDI.Instance.SquadronStarSystem, "Squadron system", ref vaProxy); SquadronStarSystem = EDDI.Instance.SquadronStarSystem; } } catch (Exception ex) { Logging.Error("Failed to set last system", ex); } try { if (EDDI.Instance.CurrentStellarBody != CurrentStellarBody) { setDetailedBodyValues(EDDI.Instance.CurrentStellarBody, "Body", ref vaProxy); CurrentStellarBody = EDDI.Instance.CurrentStellarBody; } } catch (Exception ex) { Logging.Error("Failed to set stellar body", ex); } try { if (EDDI.Instance.CurrentStation != CurrentStation) { setStationValues(EDDI.Instance.CurrentStation, "Last station", ref vaProxy); CurrentStation = EDDI.Instance.CurrentStation; } } catch (Exception ex) { Logging.Error("Failed to set last station", ex); } try { CargoMonitor cargoMonitor = ((CargoMonitor)EDDI.Instance.ObtainMonitor("Cargo monitor")); vaProxy.SetInt("Ship cargo carried", cargoMonitor?.cargoCarried ?? 0); vaProxy.SetInt("Ship limpets carried", cargoMonitor?.GetCargoWithEDName("Drones")?.total ?? 0); } catch (Exception ex) { Logging.Error("Failed to set ship cargo values", ex); } try { ShipMonitor shipMonitor = ((ShipMonitor)EDDI.Instance.ObtainMonitor("Ship monitor")); try { if (shipMonitor?.GetCurrentShip() != Ship) { setShipValues(shipMonitor?.GetCurrentShip(), "Ship", ref vaProxy); Ship = shipMonitor.GetCurrentShip(); } } catch (Exception ex) { Logging.Error("Failed to set current ship values", ex); } try { if (shipMonitor?.shipyard != Shipyard) { List <Ship> shipyard = new List <Ship>(shipMonitor?.shipyard); if (shipyard != null) { int currentStoredShip = 1; foreach (Ship StoredShip in shipyard) { setShipValues(StoredShip, "Stored ship " + currentStoredShip, ref vaProxy); currentStoredShip++; } vaProxy.SetInt("Stored ship entries", shipMonitor?.shipyard.Count); } Shipyard = shipMonitor.shipyard; } } catch (Exception ex) { Logging.Error("Failed to set shipyard", ex); } } catch (Exception ex) { Logging.Error("Failed to obtain ship monitor & set VoiceAttack values", ex); } try { if (EDDI.Instance.HomeStarSystem != HomeStarSystem) { setStarSystemValues(EDDI.Instance.HomeStarSystem, "Home system", ref vaProxy); HomeStarSystem = EDDI.Instance.HomeStarSystem; // Backwards-compatibility with 1.x try { if (EDDI.Instance.HomeStarSystem != null) { vaProxy.SetText("Home system", EDDI.Instance.HomeStarSystem.name); vaProxy.SetText("Home system (spoken)", Translations.StarSystem(EDDI.Instance.HomeStarSystem.name)); } if (EDDI.Instance.HomeStation != null) { vaProxy.SetText("Home station", EDDI.Instance.HomeStation.name); } } catch (Exception ex) { Logging.Error("Failed to set 1.x home system values", ex); } } } catch (Exception ex) { Logging.Error("Failed to set home system", ex); } try { Status currentStatus = ((StatusMonitor)EDDI.Instance.ObtainMonitor("Status monitor"))?.GetStatus(); if (currentStatus != Status) { setStatusValues(StatusMonitor.currentStatus, "Status", ref vaProxy); Status = currentStatus; } } catch (Exception ex) { Logging.Error("Failed to set current status", ex); } try { // Set SetState values if (EDDI.Instance.State != State) { setDictionaryValues(EDDI.Instance.State, "state", ref vaProxy); State = EDDI.Instance.State; } } catch (Exception ex) { Logging.Error("Failed to set state", ex); } try { if (EDDI.Instance.Cmdr != Commander) { setCommanderValues(EDDI.Instance.Cmdr, ref vaProxy); Commander = EDDI.Instance.Cmdr; } } catch (Exception ex) { Logging.Error("Failed to set commander values", ex); } // On every event... // Set miscellaneous values try { vaProxy.SetText("Environment", EDDI.Instance.Environment); vaProxy.SetText("Vehicle", EDDI.Instance.Vehicle); vaProxy.SetText("EDDI version", Constants.EDDI_VERSION.ToString()); } catch (Exception ex) { Logging.Error("Failed to set misc values", ex); } }