Skip to content

Commit

Permalink
Show unmanaged files in game folder
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Apr 27, 2023
1 parent e3baf64 commit e1b50cf
Show file tree
Hide file tree
Showing 18 changed files with 775 additions and 124 deletions.
87 changes: 39 additions & 48 deletions Core/GameInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,10 @@ private void LoadCompatibleVersions()
}

private string CompatibleGameVersionsFile()
{
return Path.Combine(CkanDir(), game.CompatibleVersionsFile);
}
=> Path.Combine(CkanDir(), game.CompatibleVersionsFile);

public List<GameVersion> GetCompatibleVersions()
{
return new List<GameVersion>(this._compatibleVersions);
}
=> new List<GameVersion>(this._compatibleVersions);

public HashSet<string> GetSuppressedCompatWarningIdentifiers =>
SuppressedCompatWarningIdentifiers.LoadFrom(Version(), SuppressedCompatWarningIdentifiersFile).Identifiers;
Expand All @@ -204,12 +200,9 @@ public void AddSuppressedCompatWarningIdentifiers(HashSet<string> idents)

public string[] InstallFilters
{
get
{
return File.Exists(InstallFiltersFile)
get => File.Exists(InstallFiltersFile)
? JsonConvert.DeserializeObject<string[]>(File.ReadAllText(InstallFiltersFile))
: new string[] { };
}

set
{
Expand Down Expand Up @@ -322,9 +315,7 @@ private GameVersion DetectVersion(string directory)
#region Things which would be better as Properties

public string GameDir()
{
return gameDir;
}
=> gameDir;

public string CkanDir()
{
Expand All @@ -338,24 +329,13 @@ public string CkanDir()
}

public string DownloadCacheDir()
{
return CKANPathUtils.NormalizePath(
Path.Combine(CkanDir(), "downloads"));
}
=> CKANPathUtils.NormalizePath(Path.Combine(CkanDir(), "downloads"));

public string InstallHistoryDir()
{
return CKANPathUtils.NormalizePath(
Path.Combine(CkanDir(), "history")
);
}
=> CKANPathUtils.NormalizePath(Path.Combine(CkanDir(), "history"));

public string TempDir()
{
return CKANPathUtils.NormalizePath(
Path.Combine(CkanDir(), "temp")
);
}
=> CKANPathUtils.NormalizePath(Path.Combine(CkanDir(), "temp"));

public GameVersion Version()
{
Expand All @@ -367,9 +347,7 @@ public GameVersion Version()
}

public GameVersionCriteria VersionCriteria()
{
return new GameVersionCriteria(Version(), _compatibleVersions);
}
=> new GameVersionCriteria(Version(), _compatibleVersions);

#endregion

Expand Down Expand Up @@ -439,18 +417,14 @@ public bool Scan()
/// Returns path relative to this KSP's GameDir.
/// </summary>
public string ToRelativeGameDir(string path)
{
return CKANPathUtils.ToRelative(path, GameDir());
}
=> CKANPathUtils.ToRelative(path, GameDir());

/// <summary>
/// Given a path relative to this KSP's GameDir, returns the
/// absolute path on the system.
/// </summary>
public string ToAbsoluteGameDir(string path)
{
return CKANPathUtils.ToAbsolute(path, GameDir());
}
=> CKANPathUtils.ToAbsolute(path, GameDir());

/// <summary>
/// https://xkcd.com/208/
Expand Down Expand Up @@ -485,25 +459,42 @@ public string DllPathToIdentifier(string relative_path)
: null;
}

/// <summary>
/// Generate a sequence of files in the game folder that weren't installed by CKAN
/// </summary>
/// <param name="registry">A Registry object that knows which files CKAN installed in this folder</param>
/// <returns>Relative file paths as strings</returns>
public IEnumerable<string> UnmanagedFiles(Registry registry)
=> Directory.EnumerateFiles(gameDir, "*", SearchOption.AllDirectories)
.Select(CKANPathUtils.NormalizePath)
.Where(absPath => !absPath.StartsWith(CkanDir()))
.Select(ToRelativeGameDir)
.Where(relPath =>
!game.StockFolders.Any(f => relPath.StartsWith($"{f}/"))
&& registry.FileOwner(relPath) == null);

/// <summary>
/// Check whether a given path contains any files or folders installed by CKAN
/// </summary>
/// <param name="registry">A Registry object that knows which files CKAN installed in this folder</param>
/// <param name="absPath">Absolute path to a folder to check</param>
/// <returns>true if any descendants of given path were installed by CKAN, false otherwise</returns>
public bool HasManagedFiles(Registry registry, string absPath)
=> registry.FileOwner(ToRelativeGameDir(absPath)) != null
|| Directory.EnumerateFileSystemEntries(absPath, "*", SearchOption.AllDirectories)
.Any(f => registry.FileOwner(ToRelativeGameDir(f)) != null);

public override string ToString()
{
return string.Format(Properties.Resources.GameInstanceToString, game.ShortName, gameDir);
}
=> string.Format(Properties.Resources.GameInstanceToString, game.ShortName, gameDir);

public bool Equals(GameInstance other)
{
return other != null && gameDir.Equals(other.GameDir());
}
=> other != null && gameDir.Equals(other.GameDir());

public override bool Equals(object obj)
{
return Equals(obj as GameInstance);
}
=> Equals(obj as GameInstance);

public override int GetHashCode()
{
return gameDir.GetHashCode();
}
=> gameDir.GetHashCode();
}

}
88 changes: 28 additions & 60 deletions Core/Games/KerbalSpaceProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ public class KerbalSpaceProgram : IGame
public string ShortName => "KSP";

public bool GameInFolder(DirectoryInfo where)
{
return Directory.Exists(Path.Combine(where.FullName, "GameData"));
}
=> Directory.Exists(Path.Combine(where.FullName, "GameData"));

/// <summary>
/// Finds the Steam KSP path. Returns null if the folder cannot be located.
Expand Down Expand Up @@ -96,15 +94,19 @@ public string MacPath()
public string PrimaryModDirectoryRelative => "GameData";

public string PrimaryModDirectory(GameInstance inst)
{
return CKANPathUtils.NormalizePath(
=> CKANPathUtils.NormalizePath(
Path.Combine(inst.GameDir(), PrimaryModDirectoryRelative));
}

public string[] StockFolders => new string[]
{
"GameData/Squad",
"GameData/SquadExpansion"
"GameData/SquadExpansion",
"KSP_Data",
"KSP_x64_Data",
"KSPLauncher_Data",
"Launcher_Data",
"MonoBleedingEdge",
"PDLauncher",
};

public string[] ReservedPaths => new string[]
Expand All @@ -123,21 +125,17 @@ public string PrimaryModDirectory(GameInstance inst)
/// <param name="path"></param>
/// <returns></returns>
public bool IsReservedDirectory(GameInstance inst, string path)
{
return path == inst.GameDir() || path == inst.CkanDir()
|| path == PrimaryModDirectory(inst)
|| path == Missions(inst)
|| path == Scenarios(inst) || path == Tutorial(inst)
|| path == Ships(inst) || path == ShipsThumbs(inst)
|| path == ShipsVab(inst) || path == ShipsThumbsVAB(inst)
|| path == ShipsSph(inst) || path == ShipsThumbsSPH(inst)
|| path == ShipsScript(inst);
}
=> path == inst.GameDir() || path == inst.CkanDir()
|| path == PrimaryModDirectory(inst)
|| path == Missions(inst)
|| path == Scenarios(inst) || path == Tutorial(inst)
|| path == Ships(inst) || path == ShipsThumbs(inst)
|| path == ShipsVab(inst) || path == ShipsThumbsVAB(inst)
|| path == ShipsSph(inst) || path == ShipsThumbsSPH(inst)
|| path == ShipsScript(inst);

public bool AllowInstallationIn(string name, out string path)
{
return allowedFolders.TryGetValue(name, out path);
}
=> allowedFolders.TryGetValue(name, out path);

public void RebuildSubdirectories(string absGameRoot)
{
Expand Down Expand Up @@ -218,64 +216,34 @@ public GameVersion DetectVersion(DirectoryInfo where)
public Uri RepositoryListURL => new Uri("https://raw.githubusercontent.com/KSP-CKAN/CKAN-meta/master/repositories.json");

private string Missions(GameInstance inst)
{
return CKANPathUtils.NormalizePath(
Path.Combine(inst.GameDir(), "Missions"));
}
=> CKANPathUtils.NormalizePath(Path.Combine(inst.GameDir(), "Missions"));

private string Ships(GameInstance inst)
{
return CKANPathUtils.NormalizePath(
Path.Combine(inst.GameDir(), "Ships"));
}
=> CKANPathUtils.NormalizePath(Path.Combine(inst.GameDir(), "Ships"));

private string ShipsVab(GameInstance inst)
{
return CKANPathUtils.NormalizePath(
Path.Combine(Ships(inst), "VAB"));
}
=> CKANPathUtils.NormalizePath(Path.Combine(Ships(inst), "VAB"));

private string ShipsSph(GameInstance inst)
{
return CKANPathUtils.NormalizePath(
Path.Combine(Ships(inst), "SPH"));
}
=> CKANPathUtils.NormalizePath(Path.Combine(Ships(inst), "SPH"));

private string ShipsThumbs(GameInstance inst)
{
return CKANPathUtils.NormalizePath(
Path.Combine(Ships(inst), "@thumbs"));
}
=> CKANPathUtils.NormalizePath(Path.Combine(Ships(inst), "@thumbs"));

private string ShipsThumbsSPH(GameInstance inst)
{
return CKANPathUtils.NormalizePath(
Path.Combine(ShipsThumbs(inst), "SPH"));
}
=> CKANPathUtils.NormalizePath(Path.Combine(ShipsThumbs(inst), "SPH"));

private string ShipsThumbsVAB(GameInstance inst)
{
return CKANPathUtils.NormalizePath(
Path.Combine(ShipsThumbs(inst), "VAB"));
}
=> CKANPathUtils.NormalizePath(Path.Combine(ShipsThumbs(inst), "VAB"));

private string ShipsScript(GameInstance inst)
{
return CKANPathUtils.NormalizePath(
Path.Combine(Ships(inst), "Script"));
}
=> CKANPathUtils.NormalizePath(Path.Combine(Ships(inst), "Script"));

private string Tutorial(GameInstance inst)
{
return CKANPathUtils.NormalizePath(
Path.Combine(inst.GameDir(), "saves", "training"));
}
=> CKANPathUtils.NormalizePath(Path.Combine(inst.GameDir(), "saves", "training"));

private string Scenarios(GameInstance inst)
{
return CKANPathUtils.NormalizePath(
Path.Combine(inst.GameDir(), "saves", "scenarios"));
}
=> CKANPathUtils.NormalizePath(Path.Combine(inst.GameDir(), "saves", "scenarios"));

private readonly Dictionary<string, string> allowedFolders = new Dictionary<string, string>
{
Expand Down
12 changes: 12 additions & 0 deletions GUI/CKAN-GUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@
<Compile Include="Controls\ModInfo.Designer.cs">
<DependentUpon>ModInfo.cs</DependentUpon>
</Compile>
<Compile Include="Controls\UnmanagedFiles.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="Controls\UnmanagedFiles.Designer.cs">
<DependentUpon>UnmanagedFiles.cs</DependentUpon>
</Compile>
<Compile Include="Controls\EditModSearches.cs">
<SubType>UserControl</SubType>
</Compile>
Expand Down Expand Up @@ -331,6 +337,9 @@
<Compile Include="Main\MainTrayIcon.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Main\MainUnmanaged.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Main\MainWait.cs">
<SubType>Form</SubType>
</Compile>
Expand Down Expand Up @@ -868,6 +877,9 @@
<EmbeddedResource Include="Controls\ModInfo.resx">
<DependentUpon>ModInfo.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Controls\UnmanagedFiles.resx">
<DependentUpon>UnmanagedFiles.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Controls\PlayTime.resx">
<DependentUpon>PlayTime.cs</DependentUpon>
</EmbeddedResource>
Expand Down
16 changes: 7 additions & 9 deletions GUI/Controls/ManageMods.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e1b50cf

Please sign in to comment.