mirror of
https://github.com/kwsch/pkNX.git
synced 2026-03-22 10:14:22 -05:00
File scoped namespaces for all lib projects netstandard2.0 => net6; now uniform. bye netframework!
47 lines
1.8 KiB
C#
47 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
|
|
namespace pkNX.Structures;
|
|
|
|
/// <summary>
|
|
/// Version specific text parsing configuration object to interact with text variable codes.
|
|
/// </summary>
|
|
public class TextConfig
|
|
{
|
|
internal static readonly TextConfig Default = new(GameVersion.Any);
|
|
private static readonly char[] _trim_hex = { '0', 'x' };
|
|
private readonly TextVariableCode[] Variables;
|
|
|
|
public IEnumerable<string> GetVariableList() => Variables.Select(z => $"{z.Code:X4}={z.Name}");
|
|
public TextConfig(GameVersion game) => Variables = TextVariableCode.GetVariables(game);
|
|
|
|
private TextVariableCode GetCode(string name) => Array.Find(Variables, v => v.Name == name);
|
|
private TextVariableCode GetName(int value) => Array.Find(Variables, v => v.Code == value);
|
|
|
|
/// <summary>
|
|
/// Gets the machine-friendly variable instruction code to be written to the data.
|
|
/// </summary>
|
|
/// <param name="variable">Variable name</param>
|
|
public ushort GetVariableNumber(string variable)
|
|
{
|
|
var v = GetCode(variable);
|
|
if (v != null)
|
|
return (ushort)v.Code;
|
|
if (ushort.TryParse(variable.TrimStart(_trim_hex), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var result))
|
|
return result;
|
|
throw new ArgumentException($"Variable parse error: {variable}. Expected a hexadecimal value or standard variable code.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the human-friendly variable instruction name to be written to the output text line.
|
|
/// </summary>
|
|
/// <param name="variable">Variable code</param>
|
|
public string GetVariableString(ushort variable)
|
|
{
|
|
var v = GetName(variable);
|
|
return v?.Name ?? variable.ToString("X4");
|
|
}
|
|
}
|