Add dictionaries, materials.

This commit is contained in:
Daniel Kraus
2015-06-28 09:44:33 +02:00
parent 24c58a5867
commit 39f63835be
12 changed files with 501 additions and 58 deletions
+140 -5
View File
@@ -20,6 +20,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace zaaReloaded2.Dictionaries
{
@@ -32,16 +33,42 @@ namespace zaaReloaded2.Dictionaries
{
#region Public properties
/// <summary>
/// Gets a sorted dictionary of records. The records are sorted by the
/// first field, and each record contains the entire set of fields,
/// including the first field.
/// </summary>
public SortedDictionary<string, string[]> Records { get; protected set; }
#endregion
#region Constructor
/// <summary>
/// Constructs a new dictionary, loading default data from a built-in
/// resource, and user data from a custom file.
/// </summary>
public DictionaryBase()
{
Records = new SortedDictionary<string, string[]>();
LoadDefaultValues();
LoadUserValues();
}
#endregion
#region Public methods
/// <summary>
/// Returns true if the dictionary contains a record for
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">Dictionary key to look up.</param>
/// <returns>True if a record for <paramref name="key"/>
/// exist, false if not.</returns>
public bool HasRecord(string key)
{
return Records.ContainsKey(key);
}
#endregion
@@ -53,6 +80,13 @@ namespace zaaReloaded2.Dictionaries
/// </summary>
abstract protected Stream GetDefaultStream();
/// <summary>
/// Returns the file name of the text file that contains user-defined
/// values.
/// </summary>
/// <returns>File name.</returns>
abstract protected string GetUserFileName();
#endregion
#region Protected methods
@@ -96,19 +130,120 @@ namespace zaaReloaded2.Dictionaries
ReadFile(sr);
}
#endregion
#region Private methods
/// <summary>
/// Loads default dictionary values from a text file that is built
/// into the assembly as an embbedded resource.
/// </summary>
private void LoadDefaultValues()
protected virtual void LoadDefaultValues()
{
ReadFile(GetDefaultStream());
}
/// <summary>
/// Attempts to load additional values from a user file whose path
/// is provided by <see cref="GetUserFileName()"/>.
/// </summary>
protected virtual void LoadUserValues()
{
try
{
FileStream fs = new FileStream(GetUserFileName(), FileMode.Open, FileAccess.Read);
ReadFile(fs);
}
catch { }
}
/// <summary>
/// Looks up a dictionary key and returns the value from the requested field,
/// or a default value if the field is empty or contains only dashes.
/// </summary>
/// <param name="key">Key to look up.</param>
/// <param name="fieldNum">Zero-based index of the field to look up
/// (note that field 0 is the key itself).</param>
/// <param name="defaultValue">Default value that will be returned
/// if the field is empty or contains only dashes.</param>
/// <returns>Value of the requested field, or <paramref name="defaultValue"/>
/// if the field is empty or contains only dashes.</returns>
/// <exception cref="System.IndexOutOfRangeException">if fieldNum is negative.</exception>
protected string LookUpValue(string key, int fieldNum, string defaultValue)
{
string[] record;
if (Records.TryGetValue(key, out record))
{
if (fieldNum >= record.Length) return defaultValue;
string value = record[fieldNum];
if (string.IsNullOrWhiteSpace(value) || _dashes.IsMatch(value))
{
return defaultValue;
}
else
{
return value;
}
}
else
{
return defaultValue;
}
}
/// <summary>
/// Looks up a dictionary key and returns the value from the requested field,
/// or an empty string if the field is empty or contains only dashes.
/// </summary>
/// <param name="key">Key to look up.</param>
/// <param name="fieldNum">Zero-based index of the field to look up
/// (note that field 0 is the key itself).</param>
/// <returns>Value of the requested field, or <see cref="String.Empty"/>
/// if the field is empty or contains only dashes.</returns>
/// <exception cref="System.IndexOutOfRangeException">if fieldNum is negative.</exception>
protected string LookUpValue(string key, int fieldNum)
{
return LookUpValue(key, fieldNum, String.Empty);
}
/// <summary>
/// Looks up a dictionary key and returns a boolean value from the requested field,
/// or a default value if the field is empty or contains only dashes.
/// </summary>
/// <remarks>
/// The following case-insensitive text values are evaluated as "true": X, Y, J, +,
/// 1, JA, TRUE, WAHR.
/// </remarks>
/// <param name="key">Key to look up.</param>
/// <param name="fieldNum">Zero-based index of the field to look up
/// (note that field 0 is the key itself).</param>
/// <param name="defaultValue">Default value that will be returned
/// if the field is empty or contains only dashes.</param>
/// <returns>Value of the requested field, or <paramref name="defaultValue"/>
/// if the field is empty or contains only dashes.</returns>
/// <exception cref="System.IndexOutOfRangeException">if fieldNum is negative.</exception>
protected bool LookUpValue(string key, int fieldNum, bool defaultValue)
{
string textValue = LookUpValue(key, fieldNum, String.Empty);
switch (textValue.ToUpper())
{
case "X":
case "Y":
case "J":
case "+":
case "1":
case "JA":
case "YES":
case "TRUE":
case "WAHR":
return true;
default:
return defaultValue;
}
}
#endregion
#region Fields
private static readonly Regex _dashes = new Regex("-+");
#endregion
}
}
@@ -20,6 +20,8 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.IO;
using zaaReloaded2.Models;
namespace zaaReloaded2.Dictionaries
{
@@ -30,9 +32,74 @@ namespace zaaReloaded2.Dictionaries
/// </summary>
class ParameterDictionary : DictionaryBase
{
#region Public methods
/// <summary>
/// Looks up the canonical name for a given <paramref name="laurisName"/>.
/// </summary>
/// <param name="laurisName">Lauris item name to look up.</param>
/// <returns>Canonical name, or original Lauris name if no canonical name
/// is defined for this item.</returns>
public string GetCanonicalName(string laurisName)
{
return LookUpValue(laurisName, 1, laurisName);
}
/// <summary>
/// Looks up the material for a given <paramref name="laurisName"/>.
/// </summary>
/// <param name="laurisName">Lauris item name to look up.</param>
/// <returns><see cref="zaaReloaded2.Models.Materials"/> enum; if no material is
/// found in the dictionary, the default material "S" (serum) is returned.</returns>
public Materials GetMaterial(string laurisName)
{
string textValue = LookUpValue(laurisName, 2);
try
{
Materials m = (Materials)Enum.Parse(typeof(Materials), textValue, true);
return m;
}
catch
{
return Materials.S;
}
}
/// <summary>
/// Returns whether or not reference limits shall always
/// be displayed for a given item, regardless whether it
/// is normal or not.
/// </summary>
/// <param name="key">Laboratory item to look up; this must
/// be an original Lauris string.</param>
/// <returns></returns>
public bool GetForceReferenceDisplay(string laurisName)
{
return LookUpValue(laurisName, 3, false);
}
#endregion
#region Overrides
/// <summary>
/// Returns the resource stream for Defaults\parameters.txt.
/// </summary>
protected override System.IO.Stream GetDefaultStream()
{
return Assembly.GetExecutingAssembly().GetManifestResourceStream("");
return Assembly.GetExecutingAssembly().GetManifestResourceStream(
"zaaReloaded2.Defaults.parameters.txt");
}
/// <summary>
/// Returns a path to a file parameters.txt in the addin directory
/// as provided by <see cref="ThisAddin.Subdir"/>.
/// </summary>
protected override string GetUserFileName()
{
return Path.Combine(ThisAddIn.Subdir, "parameters.txt");
}
#endregion
}
}
+37 -1
View File
@@ -19,6 +19,8 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
namespace zaaReloaded2.Dictionaries
{
@@ -28,9 +30,43 @@ namespace zaaReloaded2.Dictionaries
/// </summary>
class UnitDictionary : DictionaryBase
{
#region Public methods
/// <summary>
/// Translates a Lauris unit into a zaaReloaded2 canonical unit.
/// If there is no canonical unit, this function returns the
/// Lauris unit as-is.
/// </summary>
/// <param name="laurisUnit">Lauris unit to translate.</param>
/// <returns>Canonical unit, or Lauris unit if no canonical
/// unit was found.</returns>
public string TranslateLaurisUnit(string laurisUnit)
{
return LookUpValue(laurisUnit, 1, laurisUnit);
}
#endregion
#region Overrides
/// <summary>
/// Returns the resource stream for Defaults\parameters.txt.
/// </summary>
protected override System.IO.Stream GetDefaultStream()
{
throw new NotImplementedException();
return Assembly.GetExecutingAssembly().GetManifestResourceStream(
"zaaReloaded2.Defaults.units.txt");
}
/// <summary>
/// Returns a path to a file parameters.txt in the addin directory
/// as provided by <see cref="ThisAddin.Subdir"/>.
/// </summary>
protected override string GetUserFileName()
{
return Path.Combine(ThisAddIn.Subdir, "units.txt");
}
#endregion
}
}