Skip to content

Commit bb4c4e4

Browse files
authored
Merge pull request #38 from Kromtec/develop
merge 1.1.1 to main
2 parents 0813ce5 + cbfc18f commit bb4c4e4

File tree

7 files changed

+131
-15
lines changed

7 files changed

+131
-15
lines changed

LegendsViewer.Backend/Legends/Enums/SiteType.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,11 @@ public enum SiteType
2929
ImportantLocation,
3030
Fort,
3131
Monastery,
32-
Castle
32+
Castle,
33+
[Description("Mysterious Palace")]
34+
MysteriousPalace,
35+
[Description("Mysterious Dungeon")]
36+
MysteriousDungeon,
37+
[Description("Mysterious Lair")]
38+
MysteriousLair
3339
}

LegendsViewer.Backend/Legends/Events/SiteRetired.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,12 @@ public SiteRetired(List<Property> properties, World world)
2626
}
2727
if (Site != null)
2828
{
29-
Site.OwnerHistory.Last().EndYear = Year;
30-
Site.OwnerHistory.Last().EndCause = "retired";
3129
world.AddPlayerRelatedDwarfObjects(Site);
3230
}
3331
if (SiteEntity != null)
3432
{
35-
SiteEntity.SiteHistory.Last(s => s.Site == Site).EndYear = Year;
36-
SiteEntity.SiteHistory.Last(s => s.Site == Site).EndCause = "retired";
3733
world.AddPlayerRelatedDwarfObjects(SiteEntity);
3834
}
39-
if (Civ != null)
40-
{
41-
Civ.SiteHistory.Last(s => s.Site == Site).EndYear = Year;
42-
Civ.SiteHistory.Last(s => s.Site == Site).EndCause = "retired";
43-
}
4435

4536
Civ.AddEvent(this);
4637
SiteEntity.AddEvent(this);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
namespace LegendsViewer.Backend.Legends.Parser;
2+
3+
public class FilteredStream : Stream
4+
{
5+
private readonly Stream _baseStream;
6+
7+
public FilteredStream(Stream baseStream)
8+
{
9+
_baseStream = baseStream ?? throw new ArgumentNullException(nameof(baseStream));
10+
}
11+
12+
public override bool CanRead => _baseStream.CanRead;
13+
14+
public override bool CanSeek => _baseStream.CanSeek;
15+
16+
public override bool CanWrite => _baseStream.CanWrite;
17+
18+
public override long Length => _baseStream.Length;
19+
20+
public override long Position { get => _baseStream.Position; set => _baseStream.Position = value; }
21+
22+
public override void Flush()
23+
{
24+
_baseStream.Flush();
25+
}
26+
27+
public override int Read(byte[] buffer, int offset, int count)
28+
{
29+
byte[] tempBuffer = new byte[count];
30+
int bytesRead = _baseStream.Read(tempBuffer, 0, count);
31+
32+
if (bytesRead == 0) return 0;
33+
34+
int wrote = 0;
35+
for (int i = 0; i < bytesRead; i++)
36+
{
37+
if (tempBuffer[i] < 32)
38+
{
39+
// Replace non-printable characters with a space (ASCII 32)
40+
buffer[offset + wrote] = (byte)' ';
41+
}
42+
else
43+
{
44+
buffer[offset + wrote] = tempBuffer[i];
45+
}
46+
47+
wrote++;
48+
}
49+
50+
return wrote;
51+
}
52+
53+
public override long Seek(long offset, SeekOrigin origin)
54+
{
55+
return _baseStream.Seek(offset, origin);
56+
}
57+
58+
public override void SetLength(long value)
59+
{
60+
_baseStream.SetLength(value);
61+
}
62+
63+
public override void Write(byte[] buffer, int offset, int count)
64+
{
65+
_baseStream.Write(buffer, offset, count);
66+
}
67+
}

LegendsViewer.Backend/Legends/Parser/XMLParser.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public class XmlParser : IDisposable
2121
protected XmlParser(World world, string xmlFile)
2222
{
2323
World = world;
24-
XmlReader = XmlReader.Create(xmlFile, new XmlReaderSettings { Async = true, IgnoreWhitespace = true, IgnoreComments = true, IgnoreProcessingInstructions = true });
24+
XmlReader = XmlReader.Create(
25+
new FilteredStream(new FileStream(xmlFile, FileMode.Open)),
26+
new XmlReaderSettings { Async = true, IgnoreWhitespace = true, IgnoreComments = true, IgnoreProcessingInstructions = true });
2527
}
2628

2729
public XmlParser(World world, string xmlFile, string? xmlPlusFile) : this(world, xmlFile)

LegendsViewer.Backend/Legends/WorldObjects/Artifact.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace LegendsViewer.Backend.Legends.WorldObjects;
1010

1111
public class Artifact : WorldObject, IHasCoordinates
1212
{
13+
private const string DefaultName = "Untitled";
14+
1315
[JsonIgnore]
1416
public HistoricalFigure? Creator { get; set; }
1517
public string? CreatorLink => Creator?.ToLink(true, this);
@@ -53,15 +55,15 @@ public Artifact(List<Property> properties, World world)
5355
: base(properties, world)
5456
{
5557
Icon = HtmlStyleUtil.GetIconString("diamond-stone");
56-
Name = "Untitled";
58+
Name = DefaultName;
5759
Type = "Unknown";
5860
Subtype = "";
5961

6062
foreach (Property property in properties)
6163
{
6264
switch (property.Name)
6365
{
64-
case "name": Name = Formatting.InitCaps(property.Value); break;
66+
case "name": Name = Formatting.InitCaps(CheckArtifactName(property.Value)); break;
6567
case "item":
6668
if (property.SubProperties != null)
6769
{
@@ -71,7 +73,7 @@ public Artifact(List<Property> properties, World world)
7173
switch (subProperty.Name)
7274
{
7375
case "name_string":
74-
Item = Formatting.InitCaps(subProperty.Value);
76+
Item = Formatting.InitCaps(CheckArtifactName(subProperty.Value));
7577
break;
7678
case "page_number":
7779
PageCount = Convert.ToInt32(subProperty.Value);
@@ -119,6 +121,42 @@ public Artifact(List<Property> properties, World world)
119121
{
120122
Coordinates.AddRange(Site.Coordinates);
121123
}
124+
if (Name == DefaultName && !string.IsNullOrEmpty(Item))
125+
{
126+
Name = Item;
127+
}
128+
}
129+
130+
private static string CheckArtifactName(ReadOnlySpan<char> text)
131+
{
132+
// Determine the start and end characters to replace if needed
133+
char firstChar = text.Length > 0 ? text[0] : '\0';
134+
char lastChar = text.Length > 1 ? text[^1] : '\0';
135+
136+
// If no replacements are necessary, return the original string
137+
if (firstChar != ' ' && lastChar != ' ')
138+
{
139+
return text.ToString();
140+
}
141+
142+
// Allocate a new array to modify the content if changes are needed
143+
Span<char> result = stackalloc char[text.Length];
144+
text.CopyTo(result);
145+
146+
// Replace the first character if it's a space
147+
if (firstChar == ' ')
148+
{
149+
result[0] = '‹';
150+
}
151+
152+
// Replace the last character if it's a space
153+
if (lastChar == ' ')
154+
{
155+
result[^1] = '›';
156+
}
157+
158+
// Convert the modified span back to a string
159+
return new string(result);
122160
}
123161

124162
public void Resolve(World world)

LegendsViewer.Backend/Legends/WorldObjects/Site.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ public Site(List<Property> properties, World world)
225225
case "fort": SiteType = SiteType.Fort; break;
226226
case "monastery": SiteType = SiteType.Monastery; break;
227227
case "castle": SiteType = SiteType.Castle; break;
228+
case "mysterious palace": SiteType = SiteType.MysteriousPalace; break;
229+
case "mysterious dungeon": SiteType = SiteType.MysteriousDungeon; break;
230+
case "mysterious lair": SiteType = SiteType.MysteriousLair; break;
228231
default:
229232
property.Known = false;
230233
break;
@@ -357,6 +360,15 @@ private void SetIconByType(SiteType siteType)
357360
case SiteType.Castle:
358361
Icon = HtmlStyleUtil.GetIconString("castle");
359362
break;
363+
case SiteType.MysteriousPalace:
364+
Icon = HtmlStyleUtil.GetIconString("shield-crown");
365+
break;
366+
case SiteType.MysteriousDungeon:
367+
Icon = HtmlStyleUtil.GetIconString("checkbox-multiple-blank");
368+
break;
369+
case SiteType.MysteriousLair:
370+
Icon = HtmlStyleUtil.GetIconString("checkbox-multiple-blank-circle");
371+
break;
360372
default:
361373
Icon = HtmlStyleUtil.GetIconString("home-modern");
362374
break;

LegendsViewer.Backend/LegendsViewer.Backend.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<AssemblyName>LegendsViewer</AssemblyName>
8-
<Version>1.1.0.0</Version>
8+
<Version>1.1.1.0</Version>
99
<!-- Set the icon only for Windows builds -->
1010
<ApplicationIcon Condition="'$(RuntimeIdentifier)' == 'win-x64'">Resources/AppIcon.ico</ApplicationIcon>
1111
</PropertyGroup>

0 commit comments

Comments
 (0)