88using FileSystem = Microsoft . VisualBasic . FileIO . FileSystem ;
99using System . Collections . Generic ;
1010using System . Drawing ;
11+ using Newtonsoft . Json ;
1112
1213namespace QVM_Editor
1314{
@@ -118,8 +119,7 @@ internal static bool InitEditorAppData()
118119 appdataPath = Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) ;
119120 qvmEditorQEdPath = appdataPath + Path . DirectorySeparatorChar + qEditor ;
120121 qCompilerPath = qvmEditorQEdPath + @"\QCompiler" ;
121- objectsModelsFile = qvmEditorQEdPath + Path . DirectorySeparatorChar + "IGIModels.txt" ;
122- masterobjList = LoadFile ( QUtils . objectsModelsFile ) ;
122+ objectsModelsFile = qvmEditorQEdPath + Path . DirectorySeparatorChar + "IGIModels.json" ;
123123 tempPathFile = qvmEditorQEdPath + "\\ " + tempPathFileName ;
124124
125125 if ( ! Directory . Exists ( qvmEditorQEdPath ) ) { initErrReason = "QEditor" ; initStatus = false ; }
@@ -274,6 +274,24 @@ internal static DialogResult ShowInputDialog(ref string input)
274274 return result ;
275275 }
276276
277+
278+ // Read models from JSON file
279+ internal static dynamic ReadModels ( )
280+ {
281+ try
282+ {
283+ string jsonModels = LoadFile ( QUtils . objectsModelsFile ) ;
284+ dynamic jsonModelsData = JsonConvert . DeserializeObject ( jsonModels ) ;
285+ return jsonModelsData ;
286+ }
287+ catch ( Exception ex )
288+ {
289+ LogException ( MethodBase . GetCurrentMethod ( ) . Name , ex ) ;
290+ return null ;
291+ }
292+ }
293+
294+ // Find model name from ID
277295 internal static string FindModelName ( string modelId , bool addLogs = false )
278296 {
279297 string modelName = "UNKNOWN_OBJECT" ;
@@ -282,37 +300,35 @@ internal static string FindModelName(string modelId, bool addLogs = false)
282300 if ( modelId . Contains ( "\" " ) )
283301 modelId = modelId . Replace ( "\" " , String . Empty ) ;
284302
285- if ( File . Exists ( QUtils . objectsModelsFile ) )
286- {
287- if ( String . IsNullOrEmpty ( masterobjList ) ) return String . Empty ;
288-
289- var objList = masterobjList . Split ( '\n ' ) ;
303+ dynamic jsonModelsData = ReadModels ( ) ;
290304
291- foreach ( var obj in objList )
305+ // Loop through the data to find the matching model ID
306+ foreach ( var data in jsonModelsData )
307+ {
308+ if ( data . ModelId . ToString ( ) . Equals ( modelId ) )
292309 {
293- if ( obj . Contains ( modelId ) )
310+ modelName = data . ModelName . ToString ( ) ;
311+ if ( modelName . Length < 3 || String . IsNullOrEmpty ( modelName ) )
294312 {
295- modelName = obj . Split ( '=' ) [ 0 ] ;
296- if ( modelName . Length < 3 || String . IsNullOrEmpty ( modelName ) )
297- {
298- if ( addLogs )
299- QUtils . AddLog ( MethodBase . GetCurrentMethod ( ) . Name , "couldn't find model name for Model id : " + modelId ) ;
300- return modelName ;
301- }
313+ if ( addLogs )
314+ QUtils . AddLog ( MethodBase . GetCurrentMethod ( ) . Name , "couldn't find model name for Model id : " + modelId ) ;
315+ return modelName ;
302316 }
303317 }
304-
305- if ( modelName . Length > 3 && ! String . IsNullOrEmpty ( modelName ) && addLogs )
306- QUtils . AddLog ( MethodBase . GetCurrentMethod ( ) . Name , "Found model name '" + modelName + "' for id : " + modelId ) ;
307318 }
319+
320+ if ( modelName . Length > 3 && ! String . IsNullOrEmpty ( modelName ) && addLogs )
321+ QUtils . AddLog ( MethodBase . GetCurrentMethod ( ) . Name , "Found model name '" + modelName + "' for id : " + modelId ) ;
308322 }
309323 catch ( Exception ex )
310324 {
311325 QUtils . LogException ( MethodBase . GetCurrentMethod ( ) . Name , ex ) ;
312326 }
313- return modelName . Trim ( ) ;
327+
328+ return modelName ;
314329 }
315330
331+
316332 //Execute shell command and get std-output.
317333 internal static string ShellExec ( string cmdArgs , bool runAsAdmin = false , bool waitForExit = true , string shell = "cmd.exe" )
318334 {
0 commit comments