11using System . Collections . Generic ;
22using VisualStudioDiscordRPC . Shared . Variables ;
33using VisualStudioDiscordRPC . Shared . Observers ;
4+ using Newtonsoft . Json ;
5+ using System . Windows ;
6+ using System ;
47
58namespace 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}
0 commit comments