Skip to content

Commit

Permalink
Updated for Models.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Jones-HM committed Mar 19, 2023
1 parent e7601e6 commit 8f7cb34
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
56 changes: 36 additions & 20 deletions QVMEditor/QUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using FileSystem = Microsoft.VisualBasic.FileIO.FileSystem;
using System.Collections.Generic;
using System.Drawing;
using Newtonsoft.Json;

namespace QVM_Editor
{
Expand Down Expand Up @@ -118,8 +119,7 @@ internal static bool InitEditorAppData()
appdataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
qvmEditorQEdPath = appdataPath + Path.DirectorySeparatorChar + qEditor;
qCompilerPath = qvmEditorQEdPath + @"\QCompiler";
objectsModelsFile = qvmEditorQEdPath + Path.DirectorySeparatorChar + "IGIModels.txt";
masterobjList = LoadFile(QUtils.objectsModelsFile);
objectsModelsFile = qvmEditorQEdPath + Path.DirectorySeparatorChar + "IGIModels.json";
tempPathFile = qvmEditorQEdPath + "\\" + tempPathFileName;

if (!Directory.Exists(qvmEditorQEdPath)) { initErrReason = "QEditor"; initStatus = false; }
Expand Down Expand Up @@ -274,6 +274,24 @@ internal static DialogResult ShowInputDialog(ref string input)
return result;
}


// Read models from JSON file
internal static dynamic ReadModels()
{
try
{
string jsonModels = LoadFile(QUtils.objectsModelsFile);
dynamic jsonModelsData = JsonConvert.DeserializeObject(jsonModels);
return jsonModelsData;
}
catch (Exception ex)
{
LogException(MethodBase.GetCurrentMethod().Name, ex);
return null;
}
}

// Find model name from ID
internal static string FindModelName(string modelId, bool addLogs = false)
{
string modelName = "UNKNOWN_OBJECT";
Expand All @@ -282,37 +300,35 @@ internal static string FindModelName(string modelId, bool addLogs = false)
if (modelId.Contains("\""))
modelId = modelId.Replace("\"", String.Empty);

if (File.Exists(QUtils.objectsModelsFile))
{
if (String.IsNullOrEmpty(masterobjList)) return String.Empty;

var objList = masterobjList.Split('\n');
dynamic jsonModelsData = ReadModels();

foreach (var obj in objList)
// Loop through the data to find the matching model ID
foreach (var data in jsonModelsData)
{
if (data.ModelId.ToString().Equals(modelId))
{
if (obj.Contains(modelId))
modelName = data.ModelName.ToString();
if (modelName.Length < 3 || String.IsNullOrEmpty(modelName))
{
modelName = obj.Split('=')[0];
if (modelName.Length < 3 || String.IsNullOrEmpty(modelName))
{
if (addLogs)
QUtils.AddLog(MethodBase.GetCurrentMethod().Name, "couldn't find model name for Model id : " + modelId);
return modelName;
}
if (addLogs)
QUtils.AddLog(MethodBase.GetCurrentMethod().Name, "couldn't find model name for Model id : " + modelId);
return modelName;
}
}

if (modelName.Length > 3 && !String.IsNullOrEmpty(modelName) && addLogs)
QUtils.AddLog(MethodBase.GetCurrentMethod().Name, "Found model name '" + modelName + "' for id : " + modelId);
}

if (modelName.Length > 3 && !String.IsNullOrEmpty(modelName) && addLogs)
QUtils.AddLog(MethodBase.GetCurrentMethod().Name, "Found model name '" + modelName + "' for id : " + modelId);
}
catch (Exception ex)
{
QUtils.LogException(MethodBase.GetCurrentMethod().Name, ex);
}
return modelName.Trim();

return modelName;
}


//Execute shell command and get std-output.
internal static string ShellExec(string cmdArgs, bool runAsAdmin = false, bool waitForExit = true, string shell = "cmd.exe")
{
Expand Down
3 changes: 3 additions & 0 deletions QVMEditor/QVMEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualBasic" />
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="ScintillaNET, Version=3.6.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\jacobslusser.ScintillaNET.3.6.3\lib\net40\ScintillaNET.dll</HintPath>
</Reference>
Expand Down
1 change: 1 addition & 0 deletions QVMEditor/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="jacobslusser.ScintillaNET" version="3.6.3" targetFramework="net40" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net40" />
</packages>

0 comments on commit 8f7cb34

Please sign in to comment.