1
1
using OpenLoco . Common . Logging ;
2
2
using OpenLoco . Dat ;
3
3
using OpenLoco . Dat . FileParsing ;
4
- using OpenLoco . Dat . Objects ;
5
4
using OpenLoco . Dat . Objects . Sound ;
6
5
using OpenLoco . Dat . Types ;
7
6
using OpenLoco . Gui . Models ;
12
11
using System . Collections . ObjectModel ;
13
12
using System . IO ;
14
13
using System . Linq ;
14
+ using System . Reflection ;
15
15
using System . Threading . Tasks ;
16
16
17
17
namespace OpenLoco . Gui . ViewModels
@@ -64,6 +64,23 @@ public void UpdateHexDumpView()
64
64
? GetDumpLines ( currentByteList , positionValues . Start , positionValues . End ) . ToArray ( )
65
65
: GetDumpLines ( currentByteList , null , null ) . ToArray ( ) ;
66
66
67
+ public static IObjectViewModel < ILocoStruct > GetViewModelFromStruct ( ILocoStruct locoStruct )
68
+ {
69
+ var asm = Assembly
70
+ . GetExecutingAssembly ( )
71
+ . GetTypes ( )
72
+ . SingleOrDefault ( type
73
+ => type . IsClass
74
+ && ! type . IsAbstract
75
+ && type . BaseType ? . IsGenericType == true
76
+ && type . BaseType . GetGenericTypeDefinition ( ) == typeof ( LocoObjectViewModel < > )
77
+ && type . BaseType . GenericTypeArguments . Single ( ) == locoStruct . GetType ( ) ) ;
78
+
79
+ return asm == null
80
+ ? new GenericObjectViewModel ( ) { Object = locoStruct }
81
+ : ( Activator . CreateInstance ( asm , locoStruct ) as IObjectViewModel < ILocoStruct > ) ! ;
82
+ }
83
+
67
84
public override void Load ( )
68
85
{
69
86
// this stops any currently-playing sounds
@@ -80,57 +97,13 @@ public override void Load()
80
97
81
98
if ( CurrentObject ? . LocoObject != null )
82
99
{
83
- if ( CurrentObject . LocoObject . Object is VehicleObject veh )
84
- {
85
- CurrentObjectViewModel = new VehicleViewModel ( veh ) ;
86
- }
87
- else if ( CurrentObject . LocoObject . Object is TownNamesObject tn )
88
- {
89
- CurrentObjectViewModel = new TownNamesViewModel ( tn ) ;
90
- }
91
- else if ( CurrentObject . LocoObject . Object is AirportObject ao )
92
- {
93
- CurrentObjectViewModel = new AirportViewModel ( ao ) ;
94
- }
95
- else if ( CurrentObject . LocoObject . Object is IndustryObject io )
96
- {
97
- CurrentObjectViewModel = new IndustryViewModel ( io ) ;
98
- }
99
- else if ( CurrentObject . LocoObject . Object is BuildingObject bo )
100
- {
101
- CurrentObjectViewModel = new BuildingViewModel ( bo ) ;
102
- }
103
- else if ( CurrentObject . LocoObject . Object is TrainStationObject ts )
104
- {
105
- CurrentObjectViewModel = new TrainStationViewModel ( ts ) ;
106
- }
107
- else if ( CurrentObject . LocoObject . Object is TrackObject to )
108
- {
109
- CurrentObjectViewModel = new TrackViewModel ( to ) ;
110
- }
111
- else if ( CurrentObject . LocoObject . Object is RegionObject ro )
112
- {
113
- CurrentObjectViewModel = new RegionViewModel ( ro ) ;
114
- }
115
- else if ( CurrentObject . LocoObject . Object is TrainSignalObject tso )
116
- {
117
- CurrentObjectViewModel = new TrainSignalViewModel ( tso ) ;
118
- }
119
- else if ( CurrentObject . LocoObject . Object is StreetLightObject sl )
120
- {
121
- CurrentObjectViewModel = new StreetLightViewModel ( sl ) ;
122
- }
123
- else if ( CurrentObject . LocoObject . Object is ClimateObject cl )
124
- {
125
- CurrentObjectViewModel = new ClimateViewModel ( cl ) ;
126
- }
127
- else
128
- {
129
- CurrentObjectViewModel = new GenericObjectViewModel ( ) { Object = CurrentObject . LocoObject . Object } ;
130
- }
131
-
132
- var imageNameProvider = ( CurrentObject . LocoObject . Object is IImageTableNameProvider itnp ) ? itnp : new DefaultImageTableNameProvider ( ) ;
100
+ CurrentObjectViewModel = GetViewModelFromStruct ( CurrentObject . LocoObject . Object ) ;
133
101
StringTableViewModel = new ( CurrentObject . LocoObject . StringTable ) ;
102
+
103
+ var imageNameProvider = ( CurrentObject . LocoObject . Object is IImageTableNameProvider itnp )
104
+ ? itnp
105
+ : new DefaultImageTableNameProvider ( ) ;
106
+
134
107
ExtraContentViewModel = CurrentObject . LocoObject . Object is SoundObject soundObject
135
108
? new SoundViewModel ( CurrentObject . DatFileInfo . S5Header . Name , soundObject . SoundObjectData . PcmHeader , soundObject . PcmData )
136
109
: new ImageTableViewModel ( CurrentObject . LocoObject , imageNameProvider , Model . PaletteMap , CurrentObject . Images , Model . Logger ) ;
0 commit comments