public AddItemEditor(ShortcutData editorItem) : base(editorItem) { var types = TypeUtils.GetShortcutTypes(); this.typeNames = new string[types.Keys.Count]; types.Keys.CopyTo(this.typeNames, 0); }
/// <summary> /// Validates whether all the objects exists. /// </summary> /// <param name="shortcutData">Shortcut data to be validated.</param> /// <returns>Validated shortcut data.</returns> private static ShortcutData ValidateObjects(ShortcutData shortcutData) { foreach (var shortcutType in shortcutData.types) { for (var index = 0; index < shortcutType.guids.Count; index++) { var assetPath = AssetDatabase.GUIDToAssetPath(shortcutType.guids[index]); if (string.IsNullOrEmpty(assetPath) || !File.Exists(String.Concat(Application.dataPath, assetPath.Replace("Assets", "")))) { shortcutType.guids.RemoveAt(index--); } } } return(shortcutData); }
public void SuckInCreature(Creature creature, Room room, ShortcutData shortCut) { room.PlaySound((!(creature is Player)) ? this.NPCShortcutSound(creature, 0) : SoundID.Player_Enter_Shortcut, creature.mainBodyChunk.pos); if (creature is Player && shortCut.shortCutType == ShortcutData.Type.RoomExit) { int num = room.abstractRoom.connections[shortCut.destNode]; if (num > -1 && !(creature.abstractPhysicalObject as patch_AbstractPhysicalObject).networkObject) { room.world.ActivateRoom(room.world.GetAbstractRoom(num)); } } if (shortCut.shortCutType == ShortcutData.Type.NPCTransportation && Array.IndexOf <IntVector2>(room.shortcutsIndex, creature.NPCTransportationDestination.Tile) > -1) { this.transportVessels.Add(new ShortcutHandler.ShortCutVessel(creature.NPCTransportationDestination.Tile, creature, room.abstractRoom, (int)Vector2.Distance(IntVector2.ToVector2(shortCut.DestTile), IntVector2.ToVector2(creature.NPCTransportationDestination.Tile)))); } else { this.transportVessels.Add(new ShortcutHandler.ShortCutVessel(shortCut.StartTile, creature, room.abstractRoom, 0)); } }
/// <summary> /// Loads shortcuts data /// </summary> /// <returns>The shortcut data.</returns> public static ShortcutData LoadShorcutData() { //Checks whether editor default resources path exists. var resourcesPath = Path.Combine(Application.dataPath, EDITOR_DEFAULT_RESOURCES); if (!Directory.Exists(resourcesPath)) { Directory.CreateDirectory(resourcesPath); } //Checks whether the shourtcuts data path exists. var dataPath = Path.Combine(Application.dataPath, SHORTCUTS_DATA_PATH); if (!File.Exists(dataPath)) { return(CreateShortcutData()); } else { ShortcutData shortcutData = (ShortcutData)EditorGUIUtility.Load("Shortcuts.asset"); return(ValidateObjects(shortcutData)); } }
/// <summary> /// Constructor /// </summary> /// <param name="msg">Exception message</param> /// <param name="shortcut">The <see cref="InvalidShortcut"/></param> /// <param name="e">Parent exception</param> public InvalidShortcutException(string msg, ShortcutData shortcut, Exception e) : base(msg, e) { InvalidShortcut = shortcut; }
/// <summary> /// Find the id of a shortcut. /// </summary> /// <param name="shortcut">The shortcut to find</param> /// <returns>The identfier of the shortcut (i.e. its place in the current shortcut list). /// If the item is not found in the current shortcut list the value is equal to the size of the list.</returns> // TODO: I think the id member is not needed any more. It can be deleted. private int findShortcutId(ShortcutData shortcut) { int s = 0; while (++s <= mCurrentShortcutsList.Count) if (shortcut.Equals(mCurrentShortcutsList[s - 1])) break; return s; }
public static void updateConfig(string oldPath, ShortcutData newData) { currentConfig.RemoveAll(s => s.path == oldPath); currentConfig.Add(newData); writeCurrentConfig(); }
/// <summary> /// Unregister a shortcut. /// </summary> /// <para> /// This method finds the identifier of the shortcut, /// takes it from the current shortcut list /// and unloads it form the <see cref="HookHandler"/>. /// </para> /// <param name="shortcut">The shortcut to unregister</param> /// <seealso cref="registerShortcut"/> /// <seealso cref="replaceShortcut"/> /// <seealso cref="deactivateShortcut"/> public void unregisterShortcut(ShortcutData shortcut) { int s = findShortcutId(shortcut); if (s > mCurrentShortcutsList.Count) throw new InvalidShortcutException("Shortcut is not currently loaded.", shortcut); mHookHandler.unloadShortcut(shortcut.Modifier, shortcut.Key); mCurrentShortcutsList.RemoveAt(s - 1); }
/// <summary> /// Activate a shortcut. /// </summary> /// <para> /// This method finds the identifier of the shortcut /// and loads it in the <see cref="HookHandler"/>. /// </para> /// <param name="shortcut">The shortcut to activate</param> /// <seealso cref="deactivateShortcut"/> /// <seealso cref="registerShortcut"/> public void activateShortcut(ShortcutData shortcut) { int s = findShortcutId(shortcut); if (s > mCurrentShortcutsList.Count) throw new InvalidShortcutException("Shortcut is not currently loaded.", shortcut); loadShortcut(mCurrentShortcutsList[s - 1], s, true); }
private static void SuckInCreatureHK(On.ShortcutHandler.orig_SuckInCreature orig, ShortcutHandler self, Creature creature, Room room, ShortcutData shortCut) { if (creature is Player) { room.PlaySound(SoundID.Player_Enter_Shortcut, creature.mainBodyChunk.pos); if (shortCut.shortCutType == ShortcutData.Type.RoomExit) { int cnt = room.abstractRoom.connections[shortCut.destNode]; if (cnt > -1 && !AbstractPhysicalObjectHK.GetField(creature.abstractPhysicalObject).networkObject) { room.world.ActivateRoom(room.world.GetAbstractRoom(cnt)); } } if (shortCut.shortCutType == ShortcutData.Type.NPCTransportation && Array.IndexOf <IntVector2>(room.shortcutsIndex, creature.NPCTransportationDestination.Tile) > -1) { self.transportVessels.Add(new ShortcutHandler.ShortCutVessel(creature.NPCTransportationDestination.Tile, creature, room.abstractRoom, (int)Vector2.Distance(IntVector2.ToVector2(shortCut.DestTile), IntVector2.ToVector2(creature.NPCTransportationDestination.Tile)))); } else { self.transportVessels.Add(new ShortcutHandler.ShortCutVessel(shortCut.StartTile, creature, room.abstractRoom, 0)); } return; } orig(self, creature, room, shortCut); }
/// <summary> /// Call the method assiciated with a shortcut. /// </summary> /// <para> /// The shortcut is first checked using <see cref="checkShortcut"/>. /// </para> /// <para> /// First try to retrieve an instance of the given class and invoke the desired member. /// If it fails, try to invoke a static method of the given class. /// </para> /// <param name="shortcut">The shortcut whose associated method should be invoked</param> private void callShortcut(ShortcutData shortcut) { checkShortcut(shortcut); log.InfoFormat("Calling {0}", shortcut.action()); // Try to get the singleton: try { Type providerClass = Type.GetType("GlobalHotKeys." + shortcut.Class, true); Object singleton = providerClass.InvokeMember( "getInstance", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, null ); providerClass.InvokeMember( shortcut.Method, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.ExactBinding, null, singleton, new Object[] { shortcut.Params } ); return; } catch (TargetInvocationException e) { log.Error("Shortcut method failed with exception", e.GetBaseException()); } catch (Exception) { // Ignore excepetion. } // Try to invoke a static method (there seems to be no singleton): try { Type providerClass = Type.GetType("GlobalHotKeys." + shortcut.Class, true); providerClass.InvokeMember( shortcut.Method, BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.ExactBinding, null, null, new Object[] { shortcut.Params } ); } catch (TargetInvocationException e) { log.Error("Shortcut method failed with exception.", e.GetBaseException()); } catch (Exception e) { log.Error("Couldn't find the specified method.", e); } }
public static List<ShortcutData> getShortcutData() { Debug.Log("Reading xml"); XmlDocument xml = new XmlDocument(); try{ xml.Load(configPath+shortcutConfig); } catch(Exception) { createConfig(); return getShortcutData(); } List<ShortcutData> data = new List<ShortcutData>(); ShortcutData d; Debug.Log(xml.ToString()); foreach(XmlNode n1 in xml.FirstChild.ChildNodes) { //Debug.Log(n1.Name+"/"+n1.InnerXml); d = new ShortcutData(); int x = -1; int y = -1; int wall = -1; foreach(XmlNode n2 in n1.ChildNodes) { switch(n2.Name) { case "name": d.name = n2.InnerText; break; case "path": d.path = n2.InnerText; break; case "icon": try { d.iconId = System.Convert.ToInt32(n2.InnerText); } catch(Exception) { d.loadIcon(n2.InnerText); n2.InnerText = d.iconId.ToString (); } break; case "model": //TODO break; case "model_enable": if(n2.InnerText == "true") d.modelEnabled = true; else d.modelEnabled = false; break; case "extension_standard": if(n2.InnerText == "true") d.modelEnabled = true; else d.extensionStandard = false; break; case "x": x = Convert.ToInt32(n2.InnerText); break; case "y": y = Convert.ToInt32(n2.InnerText); break; case "wall": wall = Convert.ToInt32(n2.InnerText); break; } } if(x != -1 && y != -1 && wall != -1) d.setPosition(wall,x,y,false); data.Add(d); } xml.Save(configPath+shortcutConfig); currentConfig = data; return data; }
public ListItemsEditor(ShortcutData editorItem) : base(editorItem) { }
/// <summary> /// Initializes a new instance of the <see cref="Intentor.Shortcuter.Partials.PartialEditor"/> class. /// </summary> /// <param name="editorItem">Object to be edited.</param> public PartialEditor(ShortcutData editorItem) { this.editorItem = editorItem; }
public override void Update(bool eu) { base.Update(eu); lastScreenblockAlpha = screenblockAlpha; hideAllSprites = false; if (room.game.IsArenaSession) { if (!room.game.GetArenaGameSession.playersSpawned) { hideAllSprites = true; } } Player ply = null; if (room.game.Players.Count > 0) { ply = room.game.Players[0].realizedCreature as Player; } // Map edges to display quads if (state != MappingState.Done) { UpdateMapper(300); } // Do not try to access shortcuts when the room is not ready for AI if (!room.readyForAI) { screenblockAlpha = 1f; return; } // Find the player's shortcut vessel ShortcutHandler.ShortCutVessel plyVessel = null; foreach (ShortcutHandler.ShortCutVessel vessel in room.game.shortcuts.transportVessels) { if (vessel.creature == ply) { plyVessel = vessel; break; } } if (ply == null || ply.room != room || (plyVessel != null && plyVessel.entranceNode != -1)) { screenblockAlpha = Mathf.Clamp01(screenblockAlpha + 0.1f); } else { screenblockAlpha = Mathf.Clamp01(screenblockAlpha - 0.1f); } if (ply != null) { // Search for the closest shortcut entrance and display a sprite at the end location // Disabled in classic mode if (!LineOfSightMod.classic) { IntVector2 scPos = new IntVector2(); bool found = false; if (ply.room != null && !ply.inShortcut) { for (int chunk = 0; chunk < ply.bodyChunks.Length; chunk++) { IntVector2 chunkPos = room.GetTilePosition(ply.bodyChunks[chunk].pos); for (int i = 0; i < _peekSearchOffsets.Length; i++) { IntVector2 testPos = chunkPos + _peekSearchOffsets[i]; if (testPos.x < 0 || testPos.y < 0 || testPos.x >= room.TileWidth || testPos.y >= room.TileHeight) { continue; } if (room.GetTile(testPos).Terrain == Room.Tile.TerrainType.ShortcutEntrance) { int ind = Array.IndexOf(room.shortcutsIndex, testPos); if (ind > -1 && ind < (room.shortcuts?.Length ?? 0)) { if (room.shortcuts[ind].shortCutType == ShortcutData.Type.Normal) { found = true; scPos = testPos; break; } } } } } } ShortcutData sc = default(ShortcutData); int scInd = Array.IndexOf(room.shortcutsIndex, scPos); if (scInd > -1 && scInd < (room.shortcuts?.Length ?? 0)) { sc = room.shortcuts[scInd]; } else { found = false; } if (found) { IntVector2 dest = sc.DestTile; Vector2 newPeekPos = ply.room.MiddleOfTile(dest); if (_peekPos != newPeekPos) { _peekAlpha = 0f; _peekPos = newPeekPos; _peekAngle = 0f; for (int i = 0; i < 4; i++) { if (!ply.room.GetTile(dest + Custom.fourDirections[i]).Solid) { _peekAngle = 180f - 90f * i; break; } } } } _lastPeekAlpha = _peekAlpha; _peekAlpha = Custom.LerpAndTick(_peekAlpha, found ? Mathf.Sin(room.game.clock / 40f * Mathf.PI * 4f) * 0.25f + 0.75f : 0f, 0.1f, 0.075f); } // Allow vision when going through shortcuts if (plyVessel != null) { int updateShortCut = (int)_RainWorldGame_updateShortCut.GetValue(room.game); bool first = !_overrideEyePos.HasValue; if (!first) { _lastOverrideEyePos = _overrideEyePos.Value; } _overrideEyePos = Vector2.Lerp(plyVessel.lastPos.ToVector2(), plyVessel.pos.ToVector2(), (updateShortCut + 1) / 3f) * 20f + new Vector2(10f, 10f); if (first) { _lastOverrideEyePos = _overrideEyePos.Value; } if (plyVessel.room.realizedRoom != null) { screenblockAlpha = plyVessel.room.realizedRoom.GetTile(_overrideEyePos.Value).Solid ? 1f : 0f; } } else { _overrideEyePos = null; } } else { _peekAlpha = 0f; } // Don't display in arena while multiple players are present // This doesn't happen in story so that Monkland still works if (room.game.IsArenaSession && room.game.Players.Count > 1) { hideAllSprites = true; } }
private void OnFocus() { this.shortcuts = AssetUtils.LoadShorcutData(); }
private async Task RunShortcutCreator(CancellationToken token) { var shortcutData = new ShortcutData(); var frcHomePath = configurationProvider.InstallDirectory; var frcYear = configurationProvider.UpgradeConfig.FrcYear; shortcutData.IconLocation = Path.Join(frcHomePath, configurationProvider.UpgradeConfig.PathFolder, "wpilib-256.ico"); shortcutData.IsAdmin = toInstallProvider.Model.InstallAsAdmin; shortcutData.DesktopShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "vscode", "Code.exe"), $"FRC VS Code {frcYear}", $"FRC VS Code {frcYear}")); var serializedData = JsonConvert.SerializeObject(shortcutData); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { // Run windows shortcut creater var tempFile = Path.GetTempFileName(); await File.WriteAllTextAsync(tempFile, serializedData, token); var shortcutCreatorPath = Path.Combine(configurationProvider.InstallDirectory, "installUtils", "WPILibShortcutCreator.exe"); var startInfo = new ProcessStartInfo(shortcutCreatorPath, $"\"{tempFile}\""); startInfo.UseShellExecute = false; startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.CreateNoWindow = true; startInfo.RedirectStandardOutput = true; startInfo.WorkingDirectory = Environment.CurrentDirectory; if (shortcutData.IsAdmin) { startInfo.Verb = "runsas"; } var exitCode = await Task.Run(() => { var proc = Process.Start(startInfo); proc !.WaitForExit(); return(proc.ExitCode); }); if (exitCode != 0) { // Print a message saying not all shortcuts were successful } } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { // Create Linux desktop shortcut var desktopFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Desktop", $@"FRC VS Code {frcYear}.desktop"); var launcherFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".local/share/applications", $@"FRC VS Code {frcYear}.desktop"); string contents = $@"#!/usr/bin/env xdg-open [Desktop Entry] Version=1.0 Type=Application Name=FRC VS Code {frcYear} Comment=Official C++/Java IDE for the FIRST Robotics Competition Exec={configurationProvider.InstallDirectory}/frccode/frccode{frcYear} Icon={configurationProvider.InstallDirectory}/frccode/wpilib-256.ico Terminal=false StartupNotify=true "; await File.WriteAllTextAsync(desktopFile, contents); await File.WriteAllTextAsync(launcherFile, contents); } }
protected void OnEnable() { this.editorItem = (ShortcutData)this.target; this.addItemEditor = new AddItemEditor(this.editorItem); this.listItemsEditor = new ListItemsEditor(this.editorItem); }
public void addShortCut(ShortcutData data) { GameObject sc = (GameObject)GameObject.Instantiate(shortc); data.nameObject = (GameObject)GameObject.Instantiate(scName); sc.GetComponent<ShortcutScript>().data = data; sc.renderer.material = data.getMaterial(); addShortCut(sc,data.wall,data.x,data.y); }
/// <summary> /// Find a shortcut in the shortcut lists by modifiers and key. /// </summary> /// <param name="shortcutModifiers">The modifier of the shortcut</param> /// <param name="shortcutKey">The key of the shortcut</param> /// <param name="where">The list of shortcuts where it should be searched</param> /// <returns>The matching shortcut data or <c>null</c> if it is not found</returns> private ShortcutData findShortcut(ShortcutData.Modifiers shortcutModifiers, ShortcutData.Keys shortcutKey, List<ShortcutData> where) { return where.Find((ShortcutData shortcut) => { return ((shortcut.Modifier == shortcutModifiers) && (shortcut.Key == shortcutKey)); }).Clone(); }
public void loadFromDir(int wall, string dir) { ReadConfig.setLocked(true); List<string> content = new List<string>(Directory.GetFiles(dir)); List<string> files = new List<string>(); List<ShortcutData> config = ReadConfig.getShortcutData(); foreach(string file in content) if(new FileInfo(file).Extension == ".lnk") { try { files.Add(FileHelper.GetShortcutTargetFile(file)); } catch(Exception) { Debug.Log("file to big: " + file); } } else files.Add(file); foreach(string file in files) { if(containsShortcut(wall,file)) { Debug.Log("already contained"); continue; } if(file == "") { Debug.Log("empty file"); continue; } ShortcutData data = config.Find(d => d.path == file); if(data == null) data = new ShortcutData(new FileInfo(file).Name,file); if(!data.hasTexture()) { try { ByteArrayImg img = FileHelper.GetFileIcon(file); if(img.GetHash() != noicon) { Texture2D tex = new Texture2D(img.width, img.height); tex.LoadImage(img.bytes); data.setIcon(tex); } } catch (Exception){} } if(data.wall == -1) { Vector2 v = getFreeSpot(wall); data.setPosition(wall,(int)v.x,(int)v.y,false); } addShortCut(data); } ReadConfig.setLocked(false); }
public void show(ShortcutData d) { this.enabled = true; data = d; newName = d.name; }
private async Task RunShortcutCreator(CancellationToken token) { var shortcutData = new ShortcutData(); var frcHomePath = configurationProvider.InstallDirectory; var frcYear = configurationProvider.UpgradeConfig.FrcYear; shortcutData.IconLocation = Path.Join(frcHomePath, configurationProvider.UpgradeConfig.PathFolder, "wpilib-256.ico"); shortcutData.IsAdmin = toInstallProvider.Model.InstallAsAdmin; if (toInstallProvider.Model.InstallVsCode) { // Add VS Code Shortcuts shortcutData.DesktopShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "vscode", "Code.exe"), $"{frcYear} WPILib VS Code", $"{frcYear} WPILib VS Code")); shortcutData.StartMenuShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "vscode", "Code.exe"), $"Programs/{frcYear} WPILib VS Code", $"{frcYear} WPILib VS Code")); } if (toInstallProvider.Model.InstallTools) { // Add Tool Shortcuts shortcutData.DesktopShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "Glass.vbs"), $"{frcYear} WPILib Tools/Glass {frcYear}", $"Glass {frcYear}")); shortcutData.DesktopShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "OutlineViewer.vbs"), $"{frcYear} WPILib Tools/OutlineViewer {frcYear}", $"OutlineViewer {frcYear}")); shortcutData.DesktopShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "PathWeaver.vbs"), $"{frcYear} WPILib Tools/PathWeaver {frcYear}", $"PathWeaver {frcYear}")); shortcutData.DesktopShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "RobotBuilder.vbs"), $"{frcYear} WPILib Tools/RobotBuilder {frcYear}", $"RobotBuilder {frcYear}")); shortcutData.DesktopShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "RobotBuilder-Old.vbs"), $"{frcYear} WPILib Tools/RobotBuilder-Old {frcYear}", $"RobotBuilder for Old Command Framework {frcYear}")); shortcutData.DesktopShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "shuffleboard.vbs"), $"{frcYear} WPILib Tools/Shuffleboard {frcYear}", $"Shuffleboard {frcYear}")); shortcutData.DesktopShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "SmartDashboard.vbs"), $"{frcYear} WPILib Tools/SmartDashboard {frcYear}", $"SmartDashboard {frcYear}")); shortcutData.DesktopShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "SysId.vbs"), $"{frcYear} WPILib Tools/SysId {frcYear}", $"SysId {frcYear}")); shortcutData.StartMenuShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "Glass.vbs"), $"Programs/{frcYear} WPILib Tools/Glass {frcYear}", $"Glass {frcYear}")); shortcutData.StartMenuShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "OutlineViewer.vbs"), $"Programs/{frcYear} WPILib Tools/OutlineViewer {frcYear}", $"OutlineViewer {frcYear}")); shortcutData.StartMenuShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "PathWeaver.vbs"), $"Programs/{frcYear} WPILib Tools/PathWeaver {frcYear}", $"PathWeaver {frcYear}")); shortcutData.StartMenuShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "RobotBuilder.vbs"), $"Programs/{frcYear} WPILib Tools/RobotBuilder {frcYear}", $"RobotBuilder {frcYear}")); shortcutData.StartMenuShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "RobotBuilder-Old.vbs"), $"Programs/{frcYear} WPILib Tools/RobotBuilder-Old {frcYear}", $"RobotBuilder for Old Command Framework {frcYear}")); shortcutData.StartMenuShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "shuffleboard.vbs"), $"Programs/{frcYear} WPILib Tools/Shuffleboard {frcYear}", $"Shuffleboard {frcYear}")); shortcutData.StartMenuShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "SmartDashboard.vbs"), $"Programs/{frcYear} WPILib Tools/SmartDashboard {frcYear}", $"SmartDashboard {frcYear}")); shortcutData.StartMenuShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "tools", "SysId.vbs"), $"Programs/{frcYear} WPILib Tools/SysId {frcYear}", $"SysId {frcYear}")); } // Add Documentation Shortcuts shortcutData.DesktopShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "documentation", "rtd", "frc-docs-latest", "index.html"), $"{frcYear} WPILib Documentation", $"{frcYear} WPILib Documentation")); shortcutData.StartMenuShortcuts.Add(new ShortcutInfo(Path.Join(frcHomePath, "documentation", "rtd", "frc-docs-latest", "index.html"), $"Programs/{frcYear} WPILib Documentation", $"{frcYear} WPILib Documentation")); var serializedData = JsonConvert.SerializeObject(shortcutData); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { // Run windows shortcut creater var tempFile = Path.GetTempFileName(); await File.WriteAllTextAsync(tempFile, serializedData, token); var shortcutCreatorPath = Path.Combine(configurationProvider.InstallDirectory, "installUtils", "WPILibShortcutCreator.exe"); var startInfo = new ProcessStartInfo(shortcutCreatorPath, $"\"{tempFile}\"") { WorkingDirectory = Environment.CurrentDirectory }; if (shortcutData.IsAdmin) { startInfo.UseShellExecute = true; startInfo.Verb = "runas"; } else { startInfo.UseShellExecute = false; startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.CreateNoWindow = true; startInfo.RedirectStandardOutput = true; } var exitCode = await Task.Run(() => { var proc = Process.Start(startInfo); proc !.WaitForExit(); return(proc.ExitCode); }); if (exitCode != 0) { var result = await MessageBox.Avalonia.MessageBoxManager.GetMessageBoxStandardWindow("Warning", "Shortcut creation failed. Error Code: " + exitCode, icon : MessageBox.Avalonia.Enums.Icon.Warning, @enum : MessageBox.Avalonia.Enums.ButtonEnum.Ok).ShowDialog(programWindow.Window); } } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && toInstallProvider.Model.InstallVsCode) { // Create Linux desktop shortcut var desktopFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Desktop", $@"FRC VS Code {frcYear}.desktop"); var launcherFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".local/share/applications", $@"FRC VS Code {frcYear}.desktop"); string contents = $@"#!/usr/bin/env xdg-open [Desktop Entry] Version=1.0 Type=Application Categories=Development Name=FRC VS Code {frcYear} Comment=Official C++/Java IDE for the FIRST Robotics Competition Exec={configurationProvider.InstallDirectory}/frccode/frccode{frcYear} Icon={configurationProvider.InstallDirectory}/frccode/wpilib-256.ico Terminal=false StartupNotify=true "; var desktopPath = Path.GetDirectoryName(desktopFile); if (desktopPath != null) { Directory.CreateDirectory(desktopPath); } var launcherPath = Path.GetDirectoryName(launcherFile); if (launcherPath != null) { Directory.CreateDirectory(launcherPath); } await File.WriteAllTextAsync(desktopFile, contents, token); await File.WriteAllTextAsync(launcherFile, contents, token); await Task.Run(() => { var startInfo = new ProcessStartInfo("chmod", $"+x \"{desktopFile}\"") { UseShellExecute = false, WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true }; var proc = Process.Start(startInfo); proc !.WaitForExit(); }, token); } }
/// <summary> /// Load a shortcut in the hash table of registed shortcuts /// </summary> /// <param name="modifier">The modifier of the shortcut</param> /// <param name="key">The key code of the shortcut</param> /// <param name="id">The id of the shortcut</param> /// <seealso cref="unloadShortcut"/> public void loadShortcut(ShortcutData.Modifiers modifier, ShortcutData.Keys key, int id) { uint index = hash(modifier, key); mKeyCombinationsMutex.WaitOne(); if (mKeyCombinations[index] != 0) { mKeyCombinationsMutex.ReleaseMutex(); throw new InvalidShortcutException("A shortcut with the same modifier (" + modifier + ") and the same key (" + key + ") already exists"); } mKeyCombinations[index] = id; mKeyCombinationsMutex.ReleaseMutex(); }
/// <summary> /// Find a shortcut in the shortcut lists by modifiers and key. /// </summary> /// <param name="shortcutModifiers">The modifier of the shortcut</param> /// <param name="shortcutKey">The key of the shortcut</param> /// <param name="where">A flag to tell where the shortcut should be searched</param> /// <returns>The matching shortcut data or <c>null</c> if it is not found</returns> /// <seealso cref="findAllShortcuts"/> public ShortcutData findShortcut(ShortcutData.Modifiers shortcutModifiers, ShortcutData.Keys shortcutKey, SearchScope where = SearchScope.All) { if (mCurrentShortcutsList == mDefaultShortcutsList) return findShortcut(shortcutModifiers, shortcutKey, mCurrentShortcutsList); switch (where) { case SearchScope.AllCurrentFirst: return findShortcut(shortcutModifiers, shortcutKey, mCurrentShortcutsList.Concat(mDefaultShortcutsList).ToList()); case SearchScope.AllDefaultFirst: return findShortcut(shortcutModifiers, shortcutKey, mDefaultShortcutsList.Concat(mCurrentShortcutsList).ToList()); case SearchScope.Current: return findShortcut(shortcutModifiers, shortcutKey, mCurrentShortcutsList); case SearchScope.Default: return findShortcut(shortcutModifiers, shortcutKey, mDefaultShortcutsList); default: throw new ArgumentException("Bad value for search scope: " + where); } }
/// <summary> /// Unload a shortcut from the hash table of registed shortcuts /// </summary> /// <param name="modifier">The modifier of the shortcut</param> /// <param name="key">The key code of the shortcut</param> /// <seealso cref="loadShortcut"/> public void unloadShortcut(ShortcutData.Modifiers modifier, ShortcutData.Keys key) { uint index = hash(modifier, key); mKeyCombinationsMutex.WaitOne(); if (mKeyCombinations[index] == 0) { mKeyCombinationsMutex.ReleaseMutex(); throw new InvalidShortcutException("The shortcut with modifier (" + modifier + ") and key (" + key + ") does not exist"); } mKeyCombinations[index] = 0; mKeyCombinationsMutex.ReleaseMutex(); }
/// <summary> /// Check the given shortcut. /// </summary> /// <para> /// Check that /// <list type="number"> /// <item>The shortcut is not a special one</item> /// <item>The class exists</item> /// <item>The method is in the list of authorized methods for this class</item> /// </list> /// </para> /// <param name="shortcut">The shortcut to check</param> private void checkShortcut(ShortcutData shortcut) { // Check that shortcut is valid: if (!shortcut.isValid()) throw new InvalidShortcutException("Loading an invalid shortcut is forbidden", shortcut); // Gets the method class : Type providerClass = Type.GetType("GlobalHotKeys." + shortcut.Class); if (providerClass == null) throw new InvalidShortcutException("Couldn't find the specified class: " + shortcut.Class, shortcut); // Gets the list of authorized methods: List<string> authorizedMethods; try { authorizedMethods = providerClass.InvokeMember( "AuthorizedMethods", BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.Public, null, null, null ) as List<string>; log.Debug("Authorized methods are: " + String.Join<string>(",", authorizedMethods)); } catch (Exception e) { throw new UnauthorizedShortcutException("Could not find the static public property AuthorizedMethods in specified class: " + shortcut.Class, shortcut, e); } // Checks that the called method is in the list of authorized methods: if ((authorizedMethods == null) || (authorizedMethods.Find((method) => { return (method == shortcut.Method); }) == null)) throw new UnauthorizedShortcutException("Not authorized method: " + shortcut.Method, shortcut); }
/// <summary> /// Computes the hash code for a shortcut. /// </summary> /// <param name="modifier">The modifier of the shortcut</param> /// <param name="key">The key code of the shortcut</param> /// <returns>The hash code of the shorctut</returns> private uint hash(ShortcutData.Modifiers modifier, ShortcutData.Keys key) { if (ShortcutData.getKeyHashCode(key) == 0) throw new InvalidShortcutException("Cannot register a shortcut with no key"); return (uint)modifier + (ShortcutData.getKeyHashCode(key) - 1) * 256; }
/// <summary> /// Register a shortcut and assign its identifier /// </summary> /// <para> /// This method searches for a valid identifier for the given shortcut, /// appends it at the right place in the current shortcut list /// and loads it in the <see cref="HookHandler"/>. /// </para> /// <param name="shortcut">The shortcut to register</param> /// <seealso cref="unregisterShortcut"/> /// <seealso cref="replaceShortcut"/> /// <seealso cref="activateShortcut"/> public void registerShortcut(ShortcutData shortcut) { int s = 0; while (++s <= mCurrentShortcutsList.Count) if ((mCurrentShortcutsList[s - 1].Id != 0) && (mCurrentShortcutsList[s - 1].Id != s)) break; loadShortcut(shortcut, s, true); mCurrentShortcutsList.Insert(s - 1, shortcut); }
/// <summary> /// Constructor. /// </summary> /// <param name="shortcut">The <see cref="UnauthorizedShortcut"/></param> public UnauthorizedShortcutException(ShortcutData shortcut) : base() { UnauthorizedShortcut = shortcut; }
/// <summary> /// Replaces a shortcut by another. /// </summary> /// <para> /// This function is equivalent to calling successively /// <see cref="unregisterShortcut"/> with the first shortcut /// and <see cref="registerShortcut"/> with the second shortcut. /// </para> /// <param name="oldShortcut">The shortcut to unregister</param> /// <param name="newShortcut">The shortcut to register</param> /// <seealso cref="registerShortcut"/> /// <seealso cref="unregisterShortcut"/> public void replaceShortcut(ShortcutData oldShortcut, ShortcutData newShortcut) { int s = findShortcutId(oldShortcut); if (s > mCurrentShortcutsList.Count) throw new InvalidShortcutException("Shortcut is not currently loaded.", oldShortcut); mHookHandler.unloadShortcut(oldShortcut.Modifier, oldShortcut.Key); mCurrentShortcutsList.RemoveAt(s - 1); if (newShortcut.Id == 0) { loadShortcut(newShortcut, s, true); mCurrentShortcutsList.Insert(s - 1, newShortcut); } }
/// <summary> /// Constructor. /// </summary> /// <param name="msg">Exception message</param> /// <param name="shortcut">The <see cref="UnauthorizedShortcut"/></param> /// <param name="e">Parent exception</param> public UnauthorizedShortcutException(string msg, ShortcutData shortcut, Exception e) : base(msg, e) { UnauthorizedShortcut = shortcut; }
/// <summary> /// Deactivate a shortcut. /// </summary> /// <para> /// This method finds the identifier of the shortcut /// and unloads it from the <see cref="HookHandler"/>. /// </para> /// <param name="shortcut">The shortcut to deactivate</param> /// <seealso cref="activateShortcut"/> /// <seealso cref="unregisterShortcut"/> public void deactivateShortcut(ShortcutData shortcut) { int s = findShortcutId(shortcut); if (s > mCurrentShortcutsList.Count) throw new InvalidShortcutException("Shortcut is not currently loaded.", shortcut); mHookHandler.unloadShortcut(shortcut.Modifier, shortcut.Key); mCurrentShortcutsList[s - 1].Id = 0; }
/// <summary> /// Constructor /// </summary> /// <param name="shortcut">The <see cref="InvalidShortcut"/></param> public InvalidShortcutException(ShortcutData shortcut) : base() { InvalidShortcut = shortcut; }
/// <summary> /// Load a shortcut in the <see cref="HookHandler"/> /// </summary> /// <param name="shortcut">The shortcut to load</param> /// <param name="id">The identifier of the shortcut</param> /// <param name="userDefined">Whether the shortcut is user-defined, in which case it is checked that tha shortcut is not special</param> /// <seealso cref="loadShortcuts"/> private void loadShortcut(ShortcutData shortcut, int id, bool userDefined = true) { if (userDefined && shortcut.isSpecial()) throw new InvalidShortcutException(shortcut.keyCombination() + " is a reserved shortcut", shortcut); checkShortcut(shortcut); if (shortcut.Id != 0) return; mHookHandler.loadShortcut(shortcut.Modifier, shortcut.Key, id); log.Info("Sucessfully registered shortcut (id=" + id + "): " + shortcut); shortcut.Id = id; }
public static void updateConfig(ShortcutData data) { updateConfig(data.path,data); }