JD2022-TU1/main/tools/apps/ResourceViewer/ResourceInfo.cs

270 lines
7.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Windows.Media.Imaging;
using System.Windows;
using JD.Core;
namespace ResourceViewer
{
/// <summary>
/// Class to store resource iinformation retrieved from engine
/// Symetric behavior with engine wrapping
/// </summary>
public class ResourceInfo
{
#region members
private string _pathName = string.Empty;
private string _typeName = string.Empty;
private string _shortName = string.Empty;
private string _customInfo = string.Empty;
private float _timeToLoad = 0.0f;
private float _memoryUsage = 0.0f;
private bool _cooked = false;
private bool _loaded = false;
private Dictionary<PlatformId, TextureConfiguration> _textureConfigurations = null;
private Size _fileImageSize = new Size(0, 0);
private Size _customImageSize = new Size(0, 0);
public ResourceInfo()
{
BitmapSource = null;
}
#endregion
#region properties
public static ResourceStatistics OverallStats
{
get; set;
}
public Dictionary<PlatformId, TextureConfiguration> TextureConfigurations
{
get { return _textureConfigurations; }
}
public float MemoryRatio
{
get { return _memoryUsage / OverallStats.MemoryUsage; }
}
public float LoadTimeRatio
{
get
{
if (OverallStats != null && Math.Abs(OverallStats.LoadingTime*OverallStats.Count - 0) > 0.0001)
{
float value = LoadingTime/((2.0f*OverallStats.LoadingTime)/OverallStats.Count);
return value > 1.0f ? 1.0f : value;
}
return 0.0f;
}
}
public bool IsTexture
{
get
{
return Utils.Instance.ContainsNoCase(TypeName, "_texture");
}
}
public bool IsSound
{
get
{
return Utils.Instance.ContainsNoCase(TypeName, "_sound") || Utils.Instance.ContainsNoCase(TypeName, "music");
}
}
public bool IsAnim
{
get
{
return Utils.Instance.ContainsNoCase(TypeName, "Anim");
}
}
public bool IsMove
{
get
{
return Utils.Instance.ContainsNoCase(TypeName, "MoveGesture");
}
}
public BitmapSource BitmapSource { get; set; }
public Size CustomImageSize
{
get { return this._customImageSize; }
}
public Size FileImageSize
{
get { return this._fileImageSize; }
set { this._fileImageSize = value; }
}
#endregion
#region properties binding
[Browsable(true)]
[ReadOnly(true)]
[DisplayName(@"Resource name")]
[Description(@"Resource file name")]
public string ShortName
{
get { return _shortName; }
set { _shortName = value; }
}
public bool Cooked
{
get { return _cooked; }
set { _cooked = value; }
}
[Browsable(true)]
[ReadOnly(true)]
[DisplayName(@"Cooked ?")]
[Description(@"Is the resource cooked in the game?")]
public string CookedValue
{
get
{
return _cooked ? "yes" : "no";
}
}
public bool Loaded
{
get { return _loaded; }
set { _loaded = value; }
}
[Browsable(true)]
[ReadOnly(true)]
[DisplayName(@"Loaded ?")]
[Description(@"Is the resource loaded in the game?")]
public string LoadedValue
{
get
{
return _loaded ? "yes" : "no";
}
}
public string TypeName
{
get { return _typeName; }
set { _typeName = value; }
}
[Browsable(true)]
[ReadOnly(true)]
[DisplayName(@"Type")]
[Description(@"Resource type")]
public string TypeNameValue
{
get { return ResourceUtils.TypeHelper.ConvertEngineTypeToUIType(_typeName); }
}
[Browsable(true)]
[ReadOnly(true)]
[DisplayName(@"Custom")]
[Description(@"Info type")]
public string CustomInfo
{
get { return _customInfo; }
set
{
_customInfo = value;
if (string.IsNullOrEmpty(_customInfo) == false)
{
MatchCollection matches = Utils.Instance.MatchNameWithKey(_customInfo, @"([\d]+)x([\d]+)(.*)", false);
if (matches.Count == 1)
{
_customImageSize.Width = int.Parse(matches[0].Groups[1].ToString());
_customImageSize.Height = int.Parse(matches[0].Groups[2].ToString());
}
}
}
}
[Browsable(true)]
[ReadOnly(true)]
[DisplayName(@"Loading time (ms)")]
[Description(@"Time the game had to load the resource")]
public float LoadingTime
{
get { return _timeToLoad; }
set { _timeToLoad = (float)(Math.Ceiling(value * 100000.0) / 100.0); }
}
[Browsable(true)]
[ReadOnly(true)]
[DisplayName(@"Memory use (kb)")]
[Description(@"Memory print for this resource")]
public float MemoryUsage
{
get { return _memoryUsage; }
set
{
_memoryUsage = (float)Math.Floor((float)value / 1024f);
}
}
[Browsable(true)]
[DisplayName(@"File path")]
[Description(@"Complete file path")]
public string PathName
{
get { return _pathName; }
set { _pathName = value; }
}
#endregion
#region functions
public void UpdateConfigurations(IEnumerable<TextureConfiguration> configurations, IEnumerable<PlatformId> targetList)
{
if (_textureConfigurations == null)
{
_textureConfigurations = new Dictionary<PlatformId, TextureConfiguration>();
}
if (configurations != null)
{
foreach (var textureConfiguration in configurations)
{
_textureConfigurations[textureConfiguration.Target] = textureConfiguration;
}
}
if (targetList != null)
{
foreach (var target in targetList)
{
if (_textureConfigurations.ContainsKey(target) == false)
_textureConfigurations[target] = TextureConfiguration.Create(target, From.Memory);
}
}
}
public IEnumerable<TextureConfiguration> EnumerateConfigurations(IEnumerable<PlatformId> targetList)
{
if (targetList != null)
{
return ( from target
in targetList
where _textureConfigurations.ContainsKey(target)
select _textureConfigurations[target]).ToList();
}
return null;
}
#endregion
}
}