Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion LegendsViewer.Backend/Legends/Enums/SiteType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@ public enum SiteType
ImportantLocation,
Fort,
Monastery,
Castle
Castle,
[Description("Mysterious Palace")]
MysteriousPalace,
[Description("Mysterious Dungeon")]
MysteriousDungeon,
[Description("Mysterious Lair")]
MysteriousLair
}
9 changes: 0 additions & 9 deletions LegendsViewer.Backend/Legends/Events/SiteRetired.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,12 @@ public SiteRetired(List<Property> properties, World world)
}
if (Site != null)
{
Site.OwnerHistory.Last().EndYear = Year;
Site.OwnerHistory.Last().EndCause = "retired";
world.AddPlayerRelatedDwarfObjects(Site);
}
if (SiteEntity != null)
{
SiteEntity.SiteHistory.Last(s => s.Site == Site).EndYear = Year;
SiteEntity.SiteHistory.Last(s => s.Site == Site).EndCause = "retired";
world.AddPlayerRelatedDwarfObjects(SiteEntity);
}
if (Civ != null)
{
Civ.SiteHistory.Last(s => s.Site == Site).EndYear = Year;
Civ.SiteHistory.Last(s => s.Site == Site).EndCause = "retired";
}

Civ.AddEvent(this);
SiteEntity.AddEvent(this);
Expand Down
67 changes: 67 additions & 0 deletions LegendsViewer.Backend/Legends/Parser/FilteredStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace LegendsViewer.Backend.Legends.Parser;

public class FilteredStream : Stream
{
private readonly Stream _baseStream;

public FilteredStream(Stream baseStream)
{
_baseStream = baseStream ?? throw new ArgumentNullException(nameof(baseStream));
}

public override bool CanRead => _baseStream.CanRead;

public override bool CanSeek => _baseStream.CanSeek;

public override bool CanWrite => _baseStream.CanWrite;

public override long Length => _baseStream.Length;

public override long Position { get => _baseStream.Position; set => _baseStream.Position = value; }

public override void Flush()
{
_baseStream.Flush();
}

public override int Read(byte[] buffer, int offset, int count)
{
byte[] tempBuffer = new byte[count];
int bytesRead = _baseStream.Read(tempBuffer, 0, count);

if (bytesRead == 0) return 0;

int wrote = 0;
for (int i = 0; i < bytesRead; i++)
{
if (tempBuffer[i] < 32)
{
// Replace non-printable characters with a space (ASCII 32)
buffer[offset + wrote] = (byte)' ';
}
else
{
buffer[offset + wrote] = tempBuffer[i];
}

wrote++;
}

return wrote;
}

public override long Seek(long offset, SeekOrigin origin)
{
return _baseStream.Seek(offset, origin);
}

public override void SetLength(long value)
{
_baseStream.SetLength(value);
}

public override void Write(byte[] buffer, int offset, int count)
{
_baseStream.Write(buffer, offset, count);
}
}
4 changes: 3 additions & 1 deletion LegendsViewer.Backend/Legends/Parser/XMLParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public class XmlParser : IDisposable
protected XmlParser(World world, string xmlFile)
{
World = world;
XmlReader = XmlReader.Create(xmlFile, new XmlReaderSettings { Async = true, IgnoreWhitespace = true, IgnoreComments = true, IgnoreProcessingInstructions = true });
XmlReader = XmlReader.Create(
new FilteredStream(new FileStream(xmlFile, FileMode.Open)),
new XmlReaderSettings { Async = true, IgnoreWhitespace = true, IgnoreComments = true, IgnoreProcessingInstructions = true });
}

public XmlParser(World world, string xmlFile, string? xmlPlusFile) : this(world, xmlFile)
Expand Down
44 changes: 41 additions & 3 deletions LegendsViewer.Backend/Legends/WorldObjects/Artifact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace LegendsViewer.Backend.Legends.WorldObjects;

public class Artifact : WorldObject, IHasCoordinates
{
private const string DefaultName = "Untitled";

[JsonIgnore]
public HistoricalFigure? Creator { get; set; }
public string? CreatorLink => Creator?.ToLink(true, this);
Expand Down Expand Up @@ -53,15 +55,15 @@ public Artifact(List<Property> properties, World world)
: base(properties, world)
{
Icon = HtmlStyleUtil.GetIconString("diamond-stone");
Name = "Untitled";
Name = DefaultName;
Type = "Unknown";
Subtype = "";

foreach (Property property in properties)
{
switch (property.Name)
{
case "name": Name = Formatting.InitCaps(property.Value); break;
case "name": Name = Formatting.InitCaps(CheckArtifactName(property.Value)); break;
case "item":
if (property.SubProperties != null)
{
Expand All @@ -71,7 +73,7 @@ public Artifact(List<Property> properties, World world)
switch (subProperty.Name)
{
case "name_string":
Item = Formatting.InitCaps(subProperty.Value);
Item = Formatting.InitCaps(CheckArtifactName(subProperty.Value));
break;
case "page_number":
PageCount = Convert.ToInt32(subProperty.Value);
Expand Down Expand Up @@ -119,6 +121,42 @@ public Artifact(List<Property> properties, World world)
{
Coordinates.AddRange(Site.Coordinates);
}
if (Name == DefaultName && !string.IsNullOrEmpty(Item))
{
Name = Item;
}
}

private static string CheckArtifactName(ReadOnlySpan<char> text)
{
// Determine the start and end characters to replace if needed
char firstChar = text.Length > 0 ? text[0] : '\0';
char lastChar = text.Length > 1 ? text[^1] : '\0';

// If no replacements are necessary, return the original string
if (firstChar != ' ' && lastChar != ' ')
{
return text.ToString();
}

// Allocate a new array to modify the content if changes are needed
Span<char> result = stackalloc char[text.Length];
text.CopyTo(result);

// Replace the first character if it's a space
if (firstChar == ' ')
{
result[0] = '‹';
}

// Replace the last character if it's a space
if (lastChar == ' ')
{
result[^1] = '›';
}

// Convert the modified span back to a string
return new string(result);
}

public void Resolve(World world)
Expand Down
12 changes: 12 additions & 0 deletions LegendsViewer.Backend/Legends/WorldObjects/Site.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ public Site(List<Property> properties, World world)
case "fort": SiteType = SiteType.Fort; break;
case "monastery": SiteType = SiteType.Monastery; break;
case "castle": SiteType = SiteType.Castle; break;
case "mysterious palace": SiteType = SiteType.MysteriousPalace; break;
case "mysterious dungeon": SiteType = SiteType.MysteriousDungeon; break;
case "mysterious lair": SiteType = SiteType.MysteriousLair; break;
default:
property.Known = false;
break;
Expand Down Expand Up @@ -357,6 +360,15 @@ private void SetIconByType(SiteType siteType)
case SiteType.Castle:
Icon = HtmlStyleUtil.GetIconString("castle");
break;
case SiteType.MysteriousPalace:
Icon = HtmlStyleUtil.GetIconString("shield-crown");
break;
case SiteType.MysteriousDungeon:
Icon = HtmlStyleUtil.GetIconString("checkbox-multiple-blank");
break;
case SiteType.MysteriousLair:
Icon = HtmlStyleUtil.GetIconString("checkbox-multiple-blank-circle");
break;
default:
Icon = HtmlStyleUtil.GetIconString("home-modern");
break;
Expand Down
2 changes: 1 addition & 1 deletion LegendsViewer.Backend/LegendsViewer.Backend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AssemblyName>LegendsViewer</AssemblyName>
<Version>1.1.0.0</Version>
<Version>1.1.1.0</Version>
<!-- Set the icon only for Windows builds -->
<ApplicationIcon Condition="'$(RuntimeIdentifier)' == 'win-x64'">Resources/AppIcon.ico</ApplicationIcon>
</PropertyGroup>
Expand Down
Loading