Skip to content

Commit

Permalink
Use file name of icons as item id's
Browse files Browse the repository at this point in the history
This replaces the old system of providing a dedicated static correlation data json file
  • Loading branch information
Blightbuster committed Jan 24, 2024
1 parent c9d9d33 commit 36a3ddf
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 28 deletions.
20 changes: 5 additions & 15 deletions RatEye/Config/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public class Path

/// <summary>
/// Path of the folder containing static icons
/// The icons must have their item-id as file name and be of the .png format
/// For example the correct file name for the RAM item icon would be:
/// <code>
/// 57347baf24597738002c6178.png
/// </code>
/// </summary>
public string StaticIcons = Combine(DataDir, "name");

Expand All @@ -46,20 +51,6 @@ public class Path
/// </summary>
public string DynamicIcons = Combine(GetEfTTempPath(), "Icon Cache");

/// <summary>
/// Path of the file containing correlation data for icons and uid's.
/// The file must be a json file with the following content:
/// <code>
/// [
/// {
/// "icon": "item_ram_module.png",
/// "uid": "57347baf24597738002c6178"
/// }, ...
/// ]
/// </code>
/// </summary>
public string StaticCorrelationData = Combine(DataDir, "correlation.json");

/// <summary>
/// Path of the file containing correlation data for icons and uid's.
/// The file must be a json file and be able to be parsed by <see cref="RatStash"/>.
Expand Down Expand Up @@ -134,7 +125,6 @@ internal string GetHash()
CacheDir,
StaticIcons,
DynamicIcons,
StaticCorrelationData,
DynamicCorrelationData,
UnknownIcon,
TrainedData,
Expand Down
20 changes: 9 additions & 11 deletions RatEye/IconManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using RatEye.Properties;
Expand Down Expand Up @@ -349,24 +348,23 @@ private Mat GetWeaponModIconForeground(Item item)

private void LoadStaticCorrelationData()
{
var path = _config.PathConfig.StaticCorrelationData;
var correlations = JArray.Parse(ReadFileNonBlocking(path));

var correlationData = new Dictionary<string, Item>();
foreach (var jToken in correlations)

var iconPathArray = Directory.GetFiles(_config.PathConfig.StaticIcons, "*.png");
foreach (var iconPath in iconPathArray)
{
var correlation = (JObject)jToken;
var iconPath = correlation.GetValue("icon")?.ToString();
var uid = correlation.GetValue("uid")?.ToString();
var itemId = System.IO.Path.GetFileNameWithoutExtension(iconPath);
var item = _config.RatStashDB.GetItem(itemId);

// Filter out items which are not in the item database
if (_config.RatStashDB.GetItem(uid) == null) continue;
if (item == null) continue;

// Add the item to the correlation data
var iconKey = GetIconKey(iconPath, IconType.Static);
correlationData[iconKey] = _config.RatStashDB.GetItem(uid);
correlationData[iconKey] = item;
}


_staticCorrelationDataLock.EnterWriteLock();
try { _staticCorrelationData = correlationData; }
finally { _staticCorrelationDataLock.ExitWriteLock(); }
Expand All @@ -390,7 +388,7 @@ private string GetIconKey(string iconPath, IconType iconType)
IconType.Static => _config.PathConfig.StaticIcons,
IconType.Dynamic => _config.PathConfig.DynamicIcons,
};
return Path.Combine(basePath, Path.GetFileName(iconPath));
return System.IO.Path.Combine(basePath, System.IO.Path.GetFileName(iconPath));
}

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion RatEyeTest/StaticIconTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ private RatEyeEngine GetRatEyeEngine(float scale = 1f, RatStash.Database itemDat
PathConfig = new Config.Path()
{
StaticIcons = "Data/Icons",
StaticCorrelationData = "Data/Icons/correlation.json",
TrainedData = "Data/traineddata/best",
},
ProcessingConfig = new Config.Processing()
Expand Down
1 change: 0 additions & 1 deletion RatEyeTest/TestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public RatEyeEngine GetDefaultRatEyeEngine(bool optimizeHighlighted = false)
PathConfig = new Config.Path()
{
StaticIcons = "Data/Icons",
StaticCorrelationData = "Data/Icons/correlation.json",
},
ProcessingConfig = new Config.Processing()
{
Expand Down

0 comments on commit 36a3ddf

Please sign in to comment.