Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Fixed #53 Crash when analyzing solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Malware committed Oct 7, 2018
1 parent 3729669 commit 3952958
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<TextBlock Grid.Row="0" TextWrapping="Wrap">The following MDK projects must be upgraded in order to function correctly. Do you wish to do this now?</TextBlock>
<TextBlock Grid.Row="0" TextWrapping="Wrap" Text="{Binding Message}" />

<ScrollViewer Grid.Row="1" MaxHeight="128" Margin="0,8,0,0">
<ItemsControl ItemsSource="{Binding Projects}" MaxHeight="128" />
Expand Down
32 changes: 31 additions & 1 deletion Source/MDK/Views/ProjectIntegrity/RequestUpgradeDialogModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace MDK.Views.ProjectIntegrity
/// </summary>
public class RequestUpgradeDialogModel : DialogViewModel
{
string _message = "The following MDK projects must be upgraded in order to function correctly. Do you wish to do this now?";

/// <summary>
/// Creates a new instance of this view model.
/// </summary>
Expand All @@ -26,7 +28,33 @@ public RequestUpgradeDialogModel([NotNull] MDKPackage package, [NotNull] ScriptS
if (analysisResults.BadProjects.IsDefaultOrEmpty)
Projects = new ReadOnlyCollection<ProjectScriptInfo>(new List<ProjectScriptInfo>());
else
Projects = new ReadOnlyCollection<ProjectScriptInfo>(analysisResults.BadProjects.Select(p => p.ProjectInfo).ToArray());
{
if (analysisResults.BadProjects.Any(p => !p.HasValidGamePath))
{
Projects = new ReadOnlyCollection<ProjectScriptInfo>(analysisResults.BadProjects.Where(p => !p.HasValidGamePath) .Select(p => p.ProjectInfo).ToArray());
Message = "The Space Engineers game folder could not be determined. Automatic upgrades cannot be completed. Please verify that the game is installed and that the MDK configuration is correct, and then reload the projects. This affects the following MDK projects:";
SaveAndCloseCommand.IsEnabled = false;
}
else
{
Projects = new ReadOnlyCollection<ProjectScriptInfo>(analysisResults.BadProjects.Select(p => p.ProjectInfo).ToArray());
}
}
}

/// <summary>
/// Contains the message to display in the dialog.
/// </summary>
public string Message
{
get => _message;
set
{
if (value == _message)
return;
_message = value;
OnPropertyChanged();
}
}

/// <summary>
Expand All @@ -49,6 +77,8 @@ public RequestUpgradeDialogModel([NotNull] MDKPackage package, [NotNull] ScriptS
/// </summary>
protected override bool OnSave()
{
if (!SaveAndCloseCommand.IsEnabled)
return false;
try
{
Package.ScriptUpgrades.Repair(AnalysisResults);
Expand Down
51 changes: 51 additions & 0 deletions Source/MDKServices/GamePathUnavailableException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Runtime.Serialization;

namespace Malware.MDKServices
{
/// <summary>
/// Exception which occurs when trying to access game information, and the game path cannot be reached.
/// </summary>
[Serializable]
public class GamePathUnavailableException : ProjectScriptInfoException
{
//
// For guidelines regarding the creation of new exception types, see
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp
// and
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp
//

/// <summary>
/// Creates an instance of <see cref="GamePathUnavailableException"/>
/// </summary>
public GamePathUnavailableException()
: this("The game folder could not be reached. Please make sure the game is installed, and / or check the folder settings of MDK.")
{ }

/// <summary>
/// Creates an instance of <see cref="GamePathUnavailableException"/>
/// </summary>
/// <param name="message"></param>
public GamePathUnavailableException(string message) : base(message)
{ }

/// <summary>
/// Creates an instance of <see cref="GamePathUnavailableException"/>
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
public GamePathUnavailableException(string message, Exception inner) : base(message, inner)
{ }

/// <summary>
/// Creates an instance of <see cref="GamePathUnavailableException"/>
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
protected GamePathUnavailableException(
SerializationInfo info,
StreamingContext context) : base(info, context)
{ }
}
}
1 change: 1 addition & 0 deletions Source/MDKServices/MDKServices.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
<Compile Include="BadReference.cs" />
<Compile Include="BadReferenceType.cs" />
<Compile Include="DTEExtensions.cs" />
<Compile Include="GamePathUnavailableException.cs" />
<Compile Include="GitHub.cs" />
<Compile Include="GitHubRelease.cs" />
<Compile Include="ProjectScriptInfo.cs" />
Expand Down
6 changes: 4 additions & 2 deletions Source/MDKServices/ProjectScriptInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,10 @@ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName
/// <returns></returns>
public string GetActualGameBinPath(string defaultPath)
{
if (UseManualGameBinPath)
return Path.GetFullPath(string.IsNullOrEmpty(GameBinPath) ? defaultPath : GameBinPath);
if (UseManualGameBinPath && !string.IsNullOrEmpty(GameBinPath))
return Path.GetFullPath(GameBinPath);
if (defaultPath == null)
throw new GamePathUnavailableException();
return Path.GetFullPath(defaultPath);
}

Expand Down
12 changes: 9 additions & 3 deletions Source/MDKServices/ScriptProjectAnalysisResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ScriptProjectAnalysisResult
/// <summary>
/// Represents the results of an analysis which was ignored and should be disregarded.
/// </summary>
public static readonly ScriptProjectAnalysisResult NonScriptProjectResult = new ScriptProjectAnalysisResult(null, null, null, default(WhitelistReference), ImmutableArray<BadReference>.Empty);
public static readonly ScriptProjectAnalysisResult NonScriptProjectResult = new ScriptProjectAnalysisResult(null, null, null, default(WhitelistReference), ImmutableArray<BadReference>.Empty, true);

/// <summary>
/// Creates a new instance of <see cref="ScriptProjectAnalysisResult"/>
Expand All @@ -21,15 +21,16 @@ public class ScriptProjectAnalysisResult
/// <param name="projectDocument">The source XML document of the project file</param>
/// <param name="whitelist">Whitelist verification results</param>
/// <param name="badReferences">A list of bad file- or assembly references</param>
public ScriptProjectAnalysisResult(EnvDTE.Project project, ProjectScriptInfo projectInfo, XDocument projectDocument, WhitelistReference whitelist, ImmutableArray<BadReference> badReferences)
public ScriptProjectAnalysisResult(EnvDTE.Project project, ProjectScriptInfo projectInfo, XDocument projectDocument, WhitelistReference whitelist, ImmutableArray<BadReference> badReferences, bool hasValidGamePath)
{
Project = project;
ProjectInfo = projectInfo;
ProjectDocument = projectDocument;
BadReferences = badReferences;
Whitelist = whitelist;
IsScriptProject = projectInfo != null;
IsValid = BadReferences.Length == 0 && whitelist.IsValid;
HasValidGamePath = hasValidGamePath;
IsValid = BadReferences.Length == 0 && whitelist.IsValid && hasValidGamePath;
}

/// <summary>
Expand Down Expand Up @@ -57,6 +58,11 @@ public ScriptProjectAnalysisResult(EnvDTE.Project project, ProjectScriptInfo pro
/// </summary>
public ImmutableArray<BadReference> BadReferences { get; }

/// <summary>
/// Determines whether the registered game path is valid.
/// </summary>
public bool HasValidGamePath { get; }

/// <summary>
/// Whitelist verification result
/// </summary>
Expand Down
19 changes: 15 additions & 4 deletions Source/MDKServices/ScriptUpgrades.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,18 @@ ScriptProjectAnalysisResult AnalyzeProject(Project project, ScriptUpgradeAnalysi
var projectInfo = ProjectScriptInfo.Load(project.FullName, project.Name);
if (!projectInfo.IsValid)
return ScriptProjectAnalysisResult.NonScriptProjectResult;
var expectedGamePath = projectInfo.GetActualGameBinPath(options.DefaultGameBinPath).TrimEnd('\\');
var hasValidGamePath = true;
string expectedGamePath;
try
{
expectedGamePath = projectInfo.GetActualGameBinPath(options.DefaultGameBinPath)?.TrimEnd('\\');
}
catch (GamePathUnavailableException)
{
hasValidGamePath = false;
expectedGamePath = null;
}

var expectedInstallPath = options.InstallPath.TrimEnd('\\');

var badReferences = ImmutableArray.CreateBuilder<BadReference>();
Expand All @@ -157,7 +168,7 @@ ScriptProjectAnalysisResult AnalyzeProject(Project project, ScriptUpgradeAnalysi
AnalyzeFiles(options, document, xmlns, projectDir, expectedGamePath, expectedInstallPath, badReferences);
var whitelist = VerifyWhitelist(document, projectDir, expectedInstallPath);

return new ScriptProjectAnalysisResult(project, projectInfo, document, whitelist, badReferences.ToImmutable());
return new ScriptProjectAnalysisResult(project, projectInfo, document, whitelist, badReferences.ToImmutable(), hasValidGamePath);
}

WhitelistReference VerifyWhitelist(XDocument document, DirectoryInfo projectDir, string expectedInstallPath)
Expand All @@ -182,7 +193,7 @@ void AnalyzeFiles(ScriptUpgradeAnalysisOptions options, XDocument document, XmlN
var include = (string)element.Attribute("Include");
var file = ResolvePath(projectDir, include);
var gameFile = options.GameFiles.FirstOrDefault(fileName => file.EndsWith(fileName, StringComparison.CurrentCultureIgnoreCase));
if (gameFile != null)
if (gameFile != null && expectedGamePath != null)
CheckFileReference(element, expectedGamePath, file, gameFile, badReferences);
var utilityFile = options.UtilityFiles.FirstOrDefault(fileName => file.EndsWith(fileName, StringComparison.CurrentCultureIgnoreCase));
if (utilityFile != null)
Expand All @@ -204,7 +215,7 @@ void AnalyzeReferences(ScriptUpgradeAnalysisOptions options, XDocument document,
var include = (string)element.Attribute("Include");
var hintPath = (string)element.Element(XName.Get("HintPath", Xmlns));
var gameAssemblyName = options.GameAssemblyNames.FirstOrDefault(dll => dll == include);
if (gameAssemblyName != null)
if (gameAssemblyName != null && expectedGamePath != null)
CheckAssemblyReference(projectDir, element, expectedGamePath, hintPath, gameAssemblyName, badReferences);
var utilityAssemblyName = options.UtilityAssemblyNames.FirstOrDefault(dll => dll == include);
if (utilityAssemblyName != null)
Expand Down

0 comments on commit 3952958

Please sign in to comment.