Skip to content

Commit

Permalink
[joyrider3774#40] early implementation for symlink creation
Browse files Browse the repository at this point in the history
  • Loading branch information
cnapolit committed Jul 29, 2023
1 parent a19ee91 commit c3c85e5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions Interfaces/IFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public interface IFileManager
void SelectMusicForGames(IEnumerable<Game> games);
string SelectStartSoundForGame(Game game);
void DeleteMusicDirectories(IEnumerable<Game> games);
void CreateSymLinks();
}
}
42 changes: 42 additions & 0 deletions Services/Files/FileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,48 @@ public void OpenGameDirectories(IEnumerable<Game> games)

#endregion

[DllImport("kernel32.dll")]
static extern bool CreateSymbolicLinkA(string lpSymlinkFileName, string lpTargetFileName, SymbolicLink dwFlags);

enum SymbolicLink
{
File = 0,
Directory = 1,
UnPrivileged = 2
}

public void CreateSymLinks()
{
foreach (var gameDir in Directory.GetDirectories(_pathingService.GameMusicFilePath))
{
CreateSymLink(gameDir);
}
}

public void CreateSymLink(string dir)
{
var musicDir = Path.Combine(dir, SoundDirectory.Music);
if (Directory.Exists(musicDir))
{
var targetFiles = Directory.GetFiles(musicDir);
if (targetFiles.Any())
{
var targetFile = targetFiles.First();
var destination = Path.Combine(dir, "Music.mp3");

if (Directory.GetFiles(dir).Contains("Music.mp3"))
/* Then */
Logger.Info($"Symlink in directory '{dir}' already exists");
else if (!CreateSymbolicLinkA(destination, targetFile, SymbolicLink.UnPrivileged))
{
Logger.Warn($"Failed to create symbolic link from '{targetFile}' to '{destination}'");
}
}
else /* Then */ Logger.Info($"Directory '{musicDir}' does not contain music files");
}
else /* Then */ Logger.Info($"Directory '{musicDir}' does not exist");
}

#region Helpers

private static IEnumerable<string> SelectMusicForDirectory(string directory, IEnumerable<string> files)
Expand Down
1 change: 1 addition & 0 deletions Services/UI/MainMenuFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class MainMenuFactory : BaseMenuFactory, IMainMenuFactory
ConstructMainMenuItem(Resource.ActionsOpenMusicFolder, _soundManager.OpenMusicFolder),
ConstructMainMenuItem(Resource.ActionsOpenSoundsFolder, _soundManager.OpenSoundsFolder),
ConstructMainMenuItem(Resource.ActionsHelp, _soundManager.HelpMenu),
ConstructMainMenuItem("Create Symbolic Links", _fileManager.CreateSymLinks),
new MainMenuItem { Description = "-", MenuSection = App.MainMenuName },
ConstructMainMenuItem(Resource.ActionsCopySelectMusicFile, _fileManager.SelectMusicForDefault, "|" + Resource.ActionsDefault),
});
Expand Down

0 comments on commit c3c85e5

Please sign in to comment.