Brought back editorconfig

- Fully commented CSTNet code :D
This commit is contained in:
Tony Bark 2023-01-07 10:45:54 -05:00
parent ec7c191d90
commit 63b4410d65
7 changed files with 196 additions and 10 deletions

View file

@ -34,10 +34,14 @@ public class CST
/// Replaces the document's line endings with the native system line endings.
/// </summary>
/// <remarks>This stage ensures there are no crashes during parsing.</remarks>
/// <param name="content">The content of the document.</param>
/// <returns>The document's content with native system line endings.</returns>
static IEnumerable<string> NormalizeEntries(string content)
{
// Check if the document already uses native system line endings.
if (!content.Contains(Environment.NewLine))
{
// If not, check for and replace other line ending types.
if (content.Contains(LF))
content = content.Replace(LF, Environment.NewLine);
@ -51,9 +55,11 @@ public class CST
content = content.Replace(LS, Environment.NewLine);
}
// Split the content by the caret and newline characters.
var lines = content.Split(new[] { $"{CARET}{Environment.NewLine}" },
StringSplitOptions.RemoveEmptyEntries);
// Filter out any lines that start with "//", "#", "/*", or end with "*/".
return lines.Where(line =>
!line.StartsWith("//") &&
!line.StartsWith("#") &&
@ -62,24 +68,34 @@ public class CST
.AsEnumerable();
}
/// <summary>
/// Retrieves the value for the specified key from the given entries.
/// </summary>
/// <param name="entries">The entries to search through.</param>
/// <param name="key">The key to search for.</param>
/// <returns>The value for the specified key, or a default string if not found.</returns>
static string GetEntry(IEnumerable<string> entries, string key)
{
// Search through list
// Iterate through the entries.
foreach (var entry in entries)
{
// If the line doesn't start with the key, keep searching.
if (!entry.StartsWith(key))
continue;
// Locate index, trim carets and return translation.
// Locate the index of the caret character.
var startIndex = entry.IndexOf(CARET);
var line = entry.Substring(startIndex);
// Get the line from the caret character to the end of the string.
var line = entry[startIndex..];
// Return the line with the caret characters trimmed.
return line.TrimStart(CARET).TrimEnd(CARET);
}
// If no entry is found, return a default string.
return "***MISSING***";
}
}

View file

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<Version>2.0.101-beta1</Version>
<Version>2.0.102</Version>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>

View file

@ -4,6 +4,16 @@ namespace CSTNet;
public interface IUIText
{
/// <summary>
/// The base directory for the language files.
/// </summary>
string[] BasePath { get; set; }
/// <summary>
/// Get the text for the given id and key.
/// </summary>
/// <param name="id">The id of the text.</param>
/// <param name="key">The key of the text.</param>
/// <returns>The text for the given id and key.</returns>
string GetText(int id, int key);
}
}

View file

@ -4,14 +4,24 @@ namespace CSTNet;
public class UIText : IUIText
{
/// <summary>
/// The language of the text.
/// </summary>
string Language { get; set; } = "english";
/// <summary>
/// The base directory for the language files.
/// </summary>
public string[] BasePath { get; set; } = { AppContext.BaseDirectory, "uitext" };
/// <summary>
/// Constructor for the UIText class.
/// </summary>
public UIText() { }
/// <summary>
/// Loads the language file.
/// Constructor for the UIText class.
/// Loads the language file for the specified language.
/// </summary>
/// <param name="language">Language to load</param>
public UIText(string language)
@ -19,9 +29,9 @@ public class UIText : IUIText
Language = language;
}
/// <summary>
/// Loads the language file.
/// Constructor for the UIText class.
/// Loads the language file for the specified language and base directory.
/// </summary>
/// <param name="language">Language to load</param>
/// <param name="basePath">Base directory for the language files.</param>
@ -47,15 +57,21 @@ public class UIText : IUIText
/// <returns>The text for the given id and key.</returns>
public string GetText(int id, string key)
{
// Combine the base path and language path to get the full path of the language file.
var basePath = Path.Combine(BasePath);
var langPath = Path.Combine(basePath, $"{Language}.dir");
// Get all the files in the language directory.
var files = Directory.GetFiles(langPath);
// Iterate through the files in the language directory.
foreach (var file in files)
{
// Skip files that do not have the ".cst" extension.
if (!file.Contains(".cst"))
continue;
// Get the id of the current file.
var ids = Path.GetFileName(file);
var second = ids.IndexOf("_", 1, StringComparison.InvariantCultureIgnoreCase);
@ -64,14 +80,21 @@ public class UIText : IUIText
ids = ids.Substring(1, second - 1);
// If the id of the current file does not match the id passed to the function,
// skip to the next file.
if (ids != id.ToString())
continue;
// Read the content of the current file.
var content = File.ReadAllText(file);
// Return the text for the specified key.
return CST.Parse(content, key);
}
// If no text is found, return a default string.
return "***MISSING***";
}
}