Skip to content

Commit f3d04f8

Browse files
committed
Handling possible exceptions when initializing variables
1 parent 67f71f5 commit f3d04f8

11 files changed

Lines changed: 169 additions & 30 deletions
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"variables": [
3+
{
4+
"name": "file_name",
5+
"description": "Name of the currently active file",
6+
"type": "FileNameVariable"
7+
},
8+
{
9+
"name": "project_name",
10+
"description": "Name of the currently active project",
11+
"type": "ProjectNameVariable"
12+
},
13+
{
14+
"name": "solution_name",
15+
"description": "Name of the currently active solution",
16+
"type": "SolutionNameVariable"
17+
},
18+
{
19+
"name": "version",
20+
"description": "Current version of Visual Studio",
21+
"type": "VersionVariable"
22+
},
23+
{
24+
"name": "edition",
25+
"description": "Current edition of Visual Studio",
26+
"type": "EditionVariable"
27+
},
28+
{
29+
"name": "debug_mode",
30+
"description": "Current project debugging mode",
31+
"type": "DebugModeVariable"
32+
}
33+
]
34+
}

VisualStudioDiscordRPC.Shared/PackageController.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using VisualStudioDiscordRPC.Shared.Plugs.TimerPlugs;
1616
using VisualStudioDiscordRPC.Shared.Nests;
1717
using VisualStudioDiscordRPC.Shared.Utils;
18+
using Newtonsoft.Json;
1819

1920
namespace VisualStudioDiscordRPC.Shared
2021
{
@@ -96,7 +97,9 @@ private void RegisterServices()
9697
ServiceRepository.Default.AddService(localizationService);
9798

9899
// Registering variable service.
99-
ServiceRepository.Default.AddService(new VariableService(_vsObserver));
100+
string variablesConfigJson = File.ReadAllText(PathHelper.GetPackageInstallationPath("Configs/variables_config.json"));
101+
VariableServiceConfig variablesConfig = JsonConvert.DeserializeObject<VariableServiceConfig>(variablesConfigJson);
102+
ServiceRepository.Default.AddService(new VariableService(variablesConfig, _vsObserver));
100103

101104
// Registering plug service.
102105
_plugService = new PlugService();
Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
using System.Collections.Generic;
22
using VisualStudioDiscordRPC.Shared.Variables;
33
using VisualStudioDiscordRPC.Shared.Observers;
4+
using Newtonsoft.Json;
5+
using System.Windows;
6+
using System;
47

58
namespace VisualStudioDiscordRPC.Shared.Services
69
{
10+
public class VariableServiceConfig
11+
{
12+
[JsonProperty("variables")]
13+
public List<VariableInfo> Variables;
14+
}
15+
16+
public class VariableInfo
17+
{
18+
[JsonProperty("name")]
19+
public string Name;
20+
21+
[JsonProperty("description")]
22+
public string Description;
23+
24+
[JsonProperty("type")]
25+
public string Type;
26+
}
27+
728
public class VariableDescriptor
829
{
930
public string Name { get; private set; }
@@ -21,33 +42,76 @@ public VariableDescriptor(string name, string description, Variable variable)
2142
public class VariableService
2243
{
2344
private readonly VsObserver _vsObserver;
24-
private readonly Dictionary<string, VariableDescriptor> _variables = new Dictionary<string, VariableDescriptor>();
45+
private readonly Dictionary<string, Variable> _variablesByType = new Dictionary<string, Variable>();
46+
private readonly Dictionary<string, VariableDescriptor> _variablesDescriptors = new Dictionary<string, VariableDescriptor>();
2547

26-
public VariableService(VsObserver vsObserver)
48+
public VariableService(VariableServiceConfig config, VsObserver vsObserver)
2749
{
2850
_vsObserver = vsObserver;
2951

30-
RegisterVariable("file_name", "Name of the currently active file", new FileNameVariable(_vsObserver));
31-
RegisterVariable("project_name", "Name of the currently active project", new ProjectNameVariable(_vsObserver));
32-
RegisterVariable("solution_name", "Name of the currently active solution", new SolutionNameVariable(_vsObserver));
33-
RegisterVariable("version", "Current version of Visual Studio", new VersionVariable(_vsObserver.DTE));
34-
RegisterVariable("edition", "Current edition of Visual Studio", new EditionVariable(_vsObserver.DTE));
35-
RegisterVariable("debug_mode", "Current project debugging mode", new DebugModeVariable(_vsObserver.DTE));
52+
RegisterVariable(new FileNameVariable(_vsObserver));
53+
RegisterVariable(new ProjectNameVariable(_vsObserver));
54+
RegisterVariable(new SolutionNameVariable(_vsObserver));
55+
RegisterVariable(new VersionVariable(_vsObserver.DTE));
56+
RegisterVariable(new EditionVariable(_vsObserver.DTE));
57+
RegisterVariable(new DebugModeVariable(_vsObserver.DTE));
58+
59+
foreach (VariableInfo variableInfo in config.Variables)
60+
{
61+
if (!_variablesByType.TryGetValue(variableInfo.Type, out Variable variable))
62+
{
63+
MessageBox.Show(
64+
$"Can't initialize variable '{variableInfo.Name}': Type '{variableInfo.Type}' was not found or not registered",
65+
"VisualStudioDiscordRPC",
66+
MessageBoxButton.OK,
67+
MessageBoxImage.Warning);
68+
69+
continue;
70+
}
71+
72+
if (!TryInitVariable(variable, out Exception exception))
73+
{
74+
MessageBox.Show(
75+
$"Can't initialize variable '{variableInfo.Name}': An exception occurred during initialization.\n{exception}",
76+
"VisualStudioDiscordRPC",
77+
MessageBoxButton.OK,
78+
MessageBoxImage.Warning);
79+
80+
continue;
81+
}
82+
83+
_variablesDescriptors[variableInfo.Name] = new VariableDescriptor(variableInfo.Name, variableInfo.Description, variable);
84+
}
3685
}
3786

3887
public Variable GetVariableByName(string name)
3988
{
40-
if (_variables.TryGetValue(name, out var variable))
89+
if (_variablesDescriptors.TryGetValue(name, out var variable))
4190
return variable.Variable;
4291

4392
return null;
4493
}
4594

46-
public IReadOnlyCollection<VariableDescriptor> GetVariables() => _variables.Values;
95+
public IReadOnlyCollection<VariableDescriptor> GetVariablesDescriptors() => _variablesDescriptors.Values;
96+
97+
private void RegisterVariable(Variable variable)
98+
{
99+
_variablesByType[variable.GetType().Name] = variable;
100+
}
47101

48-
private void RegisterVariable(string name, string description, Variable variable)
102+
private bool TryInitVariable(Variable variable, out Exception exception)
49103
{
50-
_variables.Add(name, new VariableDescriptor(name, description, variable));
104+
exception = null;
105+
try
106+
{
107+
variable.Initialize();
108+
return true;
109+
}
110+
catch (Exception e)
111+
{
112+
exception = e;
113+
return false;
114+
}
51115
}
52116
}
53117
}

VisualStudioDiscordRPC.Shared/Variables/DebugModeVariable.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@ namespace VisualStudioDiscordRPC.Shared.Variables
66
{
77
public class DebugModeVariable : Variable
88
{
9-
private readonly Debugger _debugger;
10-
private readonly DebuggerEvents _debuggerEvents;
9+
private readonly DTE2 _dte;
10+
11+
private Debugger _debugger;
12+
private DebuggerEvents _debuggerEvents;
1113

1214
public DebugModeVariable(DTE2 dte)
15+
{
16+
_dte = dte;
17+
}
18+
19+
public override void Initialize()
1320
{
1421
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
1522

16-
_debugger = dte.Debugger;
17-
_debuggerEvents = dte.Events.DebuggerEvents;
23+
_debugger = _dte.Debugger;
24+
_debuggerEvents = _dte.Events.DebuggerEvents;
1825

1926
_debuggerEvents.OnEnterBreakMode += OnEnterBreakMode;
2027
_debuggerEvents.OnEnterDesignMode += OnEnterDesignMode;

VisualStudioDiscordRPC.Shared/Variables/EditionVariable.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ namespace VisualStudioDiscordRPC.Shared.Variables
44
{
55
public class EditionVariable : Variable
66
{
7-
private readonly string _edition;
7+
private readonly DTE2 _dte;
8+
private string _edition;
89

910
public EditionVariable(DTE2 dte)
1011
{
11-
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
12-
_edition = dte.Edition;
12+
_dte = dte;
13+
}
14+
15+
public override void Initialize()
16+
{
17+
_edition = _dte.Edition;
1318
}
1419

1520
public override string GetData()

VisualStudioDiscordRPC.Shared/Variables/FileNameVariable.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ namespace VisualStudioDiscordRPC.Shared.Variables
66
public class FileNameVariable : Variable
77
{
88
private Document _document;
9+
private VsObserver _vsObserver;
910

1011
public FileNameVariable(VsObserver vsObserver)
1112
{
12-
_document = vsObserver.DTE.ActiveDocument;
13-
vsObserver.DocumentChanged += OnDocumentChanged;
13+
_vsObserver = vsObserver;
14+
}
15+
16+
public override void Initialize()
17+
{
18+
_document = _vsObserver.DTE.ActiveDocument;
19+
_vsObserver.DocumentChanged += OnDocumentChanged;
1420
}
1521

1622
public override string GetData()

VisualStudioDiscordRPC.Shared/Variables/ProjectNameVariable.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ namespace VisualStudioDiscordRPC.Shared.Variables
55
{
66
public class ProjectNameVariable : Variable
77
{
8+
private readonly VsObserver _vsObserver;
9+
810
private Project _project;
911

1012
public ProjectNameVariable(VsObserver vsObserver)
1113
{
12-
_project = vsObserver.DTE.ActiveWindow?.Project;
13-
vsObserver.ProjectChanged += OnProjectChanged;
14+
_vsObserver = vsObserver;
15+
}
16+
17+
public override void Initialize()
18+
{
19+
_project = _vsObserver.DTE.ActiveWindow?.Project;
20+
_vsObserver.ProjectChanged += OnProjectChanged;
1421
}
1522

1623
public override string GetData()

VisualStudioDiscordRPC.Shared/Variables/SolutionNameVariable.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ namespace VisualStudioDiscordRPC.Shared.Variables
66
{
77
public class SolutionNameVariable : Variable
88
{
9+
private readonly VsObserver _vsObserver;
910
private Solution _solution;
1011

1112
public SolutionNameVariable(VsObserver vsObserver)
1213
{
13-
vsObserver.SolutionChanged += OnSolutionChanged;
14-
_solution = vsObserver.DTE.Solution;
14+
_vsObserver = vsObserver;
15+
}
16+
17+
public override void Initialize()
18+
{
19+
_vsObserver.SolutionChanged += OnSolutionChanged;
20+
_solution = _vsObserver.DTE.Solution;
1521
}
1622

1723
public override string GetData()

VisualStudioDiscordRPC.Shared/Variables/Variable.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public abstract class Variable
66
{
77
public event EventHandler<string> Changed;
88

9+
public abstract void Initialize();
10+
911
public abstract string GetData();
1012

1113
protected void RaiseChangedEvent()

VisualStudioDiscordRPC.Shared/Variables/VersionVariable.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ namespace VisualStudioDiscordRPC.Shared.Variables
55
{
66
public class VersionVariable : Variable
77
{
8-
private readonly string _version;
8+
private readonly DTE2 _dte;
9+
private string _version;
910

1011
public VersionVariable(DTE2 dte)
1112
{
12-
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
13-
_version = dte.Version;
13+
_dte = dte;
14+
}
15+
16+
public override void Initialize()
17+
{
18+
_version = _dte.Version;
1419
}
1520

1621
public override string GetData()

0 commit comments

Comments
 (0)