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

94 lines
3.1 KiB
C#

using System;
using ResourceViewer;
namespace ResourceViewer
{
/// <summary>
/// Class to format resource information into UI
/// </summary>
public class ResourceUtils
{
// string pattern to display 3 infos on the same control
private const string _infoPattern = "{0}\r\n\r\n{1}\r\n\r\n{2}";
/// <summary>
/// Returns formatted memory value string
/// </summary>
/// <param name="memory">Memory value in bytes</param>
/// <returns>Memory as formatted string</returns>
public string MemoryInKb(uint memory)
{
return $"{memory / 1024.0f:0.00}";
}
/// <summary>
/// Returns formatted memory value string
/// </summary>
/// <param name="timeInSec">Time value in microsecond</param>
/// <returns>Time as formatted string</returns>
public string TimeInMs(float timeInSec)
{
return $"{timeInSec * 1000.0f:0.00}";
}
#region nested classes
/// <summary>
/// Class to handle engine resource types to UI types
/// </summary>
public static class TypeHelper
{
private readonly static string _resourceTypePrefix = "ResourceType";
private readonly static string _resourceAllSuffix = "All";
public static string ResourceAllSuffix
{
get
{
return _resourceAllSuffix;
}
}
private readonly static string _resourceTypeUnknown = "UNKNOW";
/// <summary>
/// Convert an engine resource type name into an UI (readable) type name
/// </summary>
/// <param name="typeName">engine type name</param>
/// <returns>UI type name</returns>
public static string ConvertEngineTypeToUIType(string typeName)
{
if (string.Compare(typeName, _resourceTypeUnknown) != 0)
{
string[] typesSplits = typeName.Split('_');
if (typesSplits.Length <= 1 || typesSplits.Length > 2 || string.Compare(typesSplits[0], _resourceTypePrefix) != 0)
{
throw new Exception("Bad name for resource type, should be ResourceType_XXXXXXXXX type string");
}
return typesSplits[1];
}
else
{
return typeName;
}
}
/// <summary>
/// Convert an UI resource type name into an engine type name
/// </summary>
/// <param name="typeName">UI type name</param>
/// <returns>engine type name</returns>
public static string ConvertUITypeToEngineType(string typeName)
{
if (string.Compare(typeName, _resourceTypeUnknown) != 0)
{
return _resourceTypePrefix + '_' + typeName;
}
else
{
return typeName;
}
}
}
#endregion
}
}