Skip to content

Commit 213338e

Browse files
committed
Fix errors when reading Rules file, some other bugfixes
1 parent 28daad3 commit 213338e

File tree

10 files changed

+73
-62
lines changed

10 files changed

+73
-62
lines changed

Civ2/ImageLoader/TerrainLoader.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static class TerrainLoader
1717
{
1818
public static void LoadTerrain(Ruleset ruleset, IUserInterface active)
1919
{
20+
active.TileSets.Clear();
2021
for (var i = 0; i < active.ExpectedMaps; i++)
2122
{
2223
active.TileSets.Add(LoadTerrain(ruleset, i, active));
@@ -30,11 +31,11 @@ private static TerrainSet LoadTerrain(Ruleset ruleset, int index, IUserInterface
3031

3132
// Get dither tile before making it transparent
3233
var ditherTile = Images.ExtractBitmap(MapIndexChange((BitmapStorage)active.PicSources["dither"][0], index, active));
33-
34+
3435
Color gray;
3536
unsafe
3637
{
37-
// Get the gray colour (it's not always the same in MGE/TOT, unlike pink)
38+
// Get the gray colour (it's not always the same in MGE/TOT, unlike magenta)
3839
var imageColours = ditherTile.LoadColors();
3940
gray = imageColours[0];
4041
Image.UnloadColors(imageColours);

Civ2/Rules/Initialization.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Civ2engine.IO;
66
using Model;
77
using Model.Core;
8+
using RaylibUtils;
89

910
namespace Civ2.Rules;
1011

@@ -34,8 +35,8 @@ public static void LoadGraphicsAssets(Civ2Interface civ2Interface)
3435
{
3536
var ruleSet = civ2Interface.MainApp.ActiveRuleSet;
3637
ConfigObject.Rules = RulesParser.ParseRules(ruleSet);
37-
38-
//TODO: Check is interface already hase initialized images and unload them
38+
39+
Images.ClearCache();
3940
TerrainLoader.LoadTerrain(ruleSet, civ2Interface);
4041
UnitLoader.LoadUnits(ruleSet, civ2Interface);
4142
CityLoader.LoadCities(ruleSet, civ2Interface.CityImages, civ2Interface);

Civ2TOT/TestOfTimeInterface.cs

+3
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ public override void Initialize()
223223
{ "globalWarming", Enumerable.Range(0, 4).Select(col =>
224224
new BitmapStorage("ICONS", new Rectangle(49 + 15 * col, 305, 14, 14), true)).ToArray() },
225225
{ "backgroundImage", new[]{ new BinaryStorage("Tiles.dll", 0x100740, 0x12702) } },
226+
{ "backgroundImageSmall1", new[]{ new BinaryStorage("Tiles.dll", 0xF5C44, 0xAAFC, new Rectangle(332, 134, 64, 64)) } },
226227
{ "backgroundImageSmall2", new[]{ new BinaryStorage("Tiles.dll", 0xF5C44, 0xAAFC, new Rectangle(398, 134, 64, 64)) } },
228+
{ "cityBuiltAncient", new[]{ new BinaryStorage("Tiles.dll", 0xE3D1C, 0x5A34) } },
229+
{ "cityBuiltModern", new[]{ new BinaryStorage("Tiles.dll", 0xE9750, 0x5C2D) } },
227230
{ "observatoryPic", new[]{ new BinaryStorage("Intro.dll", 0x1E630, 0xACDC) } },
228231
{ "horzionPic", new[]{ new BinaryStorage("Intro.dll", 0x1E630, 0xACDC) } },
229232
{ "creaturePic", new[]{ new BinaryStorage("Intro.dll", 0x2EBB4, 0x14600) } },

Engine/src/IO/RulesParser.cs

+45-47
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,12 @@ private void ProcessLeaders(string[] values)
207207
Rules.Leaders = values.Select((value,id) =>
208208
{
209209
var line = value.Split(',', StringSplitOptions.TrimEntries);
210-
var titles = new List<LeaderTitle>(
211-
);
210+
var titles = new List<LeaderTitle>();
212211
for(var i = 12; i < line.Length;i+=3)
213212
{
214213
titles.Add(new LeaderTitle
215214
{
216-
Gov = int.Parse(line[i-2]),
215+
Gov = int.TryParse(line[i-2], out int val0) ? val0 : 0,
217216
TitleMale = line[i-1],
218217
TitleFemale = line[i],
219218
});
@@ -224,13 +223,13 @@ private void ProcessLeaders(string[] values)
224223
NameMale = line[0],
225224
NameFemale = line[1],
226225
Female = int.Parse(line[2]) == 1,
227-
Color = int.Parse(line[3]),
228-
CityStyle = int.Parse(line[4]),
226+
Color = int.TryParse(line[3], out int val) ? val : 0,
227+
CityStyle = int.TryParse(line[4], out val) ? val : 0,
229228
Plural = line[5],
230229
Adjective = line[6],
231-
Attack = int.Parse(line[7]),
232-
Expand = int.Parse(line[8]),
233-
Civilize = int.Parse(line[9]),
230+
Attack = int.TryParse(line[7], out val) ? val : 0,
231+
Expand = int.TryParse(line[8], out val) ? val : 0,
232+
Civilize = int.TryParse(line[9], out val) ? val : 0,
234233
Titles = titles.ToArray()
235234
};
236235
}).ToArray();
@@ -293,7 +292,7 @@ private void ProcessTerrain(IEnumerable<string>? values)
293292
if (row < 11)
294293
{
295294
if (!mappings.ContainsKey(parts[1]))
296-
mappings.Add(parts[1].Substring(0, 3), terrains.Count);
295+
mappings.Add(parts[1].Length > 3 ? parts[1][..3] : parts[1], terrains.Count);
297296
terrains.Add(parts[0]);
298297
}
299298
else
@@ -326,19 +325,19 @@ private void ProcessTerrain(IEnumerable<string>? values)
326325
{
327326
Type = (TerrainType) type,
328327
Name = line[0],
329-
MoveCost = int.Parse(line[1]),
330-
Defense = int.Parse(line[2]),
331-
Food = int.Parse(line[3]),
332-
Shields = int.Parse(line[4]),
333-
Trade = int.Parse(line[5]),
328+
MoveCost = int.TryParse(line[1], out int val) ? val : 1,
329+
Defense = int.TryParse(line[2], out val) ? val : 1,
330+
Food = int.TryParse(line[3], out val) ? val : 0,
331+
Shields = int.TryParse(line[4], out val) ? val : 0,
332+
Trade = int.TryParse(line[5], out val) ? val : 0,
334333
CanIrrigate = mappings[line[6]],
335-
IrrigationBonus = int.Parse(line[7]),
336-
TurnsToIrrigate = int.Parse(line[8]),
337-
MinGovrnLevelAItoPerformIrrigation = int.Parse(line[9]),
334+
IrrigationBonus = int.TryParse(line[7], out val) ? val : 0,
335+
TurnsToIrrigate = int.TryParse(line[8], out val) ? val : 0,
336+
MinGovrnLevelAItoPerformIrrigation = int.TryParse(line[9], out val) ? val : 0,
338337
CanMine = mappings[line[10]],
339-
MiningBonus = int.Parse(line[11]),
340-
TurnsToMine = int.Parse(line[12]),
341-
MinGovrnLevelAItoPerformMining = int.Parse(line[13]),
338+
MiningBonus = int.TryParse(line[11], out val) ? val : 0,
339+
TurnsToMine = int.TryParse(line[12], out val) ? val : 0,
340+
MinGovrnLevelAItoPerformMining = int.TryParse(line[13], out val) ? val : 0,
342341
Transform = mappings[line[14]],
343342
Impassable = line[15] == "yes",
344343
RoadBonus = type <= (int)TerrainType.Grassland ? 1:0,
@@ -370,17 +369,17 @@ private void ProcessUnits(string[] values)
370369
{
371370
Type = type,
372371
Name = text[0],
373-
Until = Rules.AdvanceMappings.ContainsKey(text[1]) ? Rules.AdvanceMappings[text[1]] : -1, // temp
374-
Domain = (UnitGas) int.Parse(text[2]),
375-
Move = Rules.Cosmic.MovementMultiplier * int.Parse(text[3].Replace(".", string.Empty)),
376-
Range = int.Parse(text[4]),
377-
Attack = int.Parse(text[5].Replace("a", string.Empty)),
378-
Defense = int.Parse(text[6].Replace("d", string.Empty)),
379-
Hitp = 10 * int.Parse(text[7].Replace("h", string.Empty)),
380-
Firepwr = int.Parse(text[8].Replace("f", string.Empty)),
381-
Cost = int.Parse(text[9]),
382-
Hold = int.Parse(text[10]),
383-
AIrole = (AIroleType)int.Parse(text[11]),
372+
Until = Rules.AdvanceMappings.TryGetValue(text[1], out int value) ? value : -1,
373+
Domain = (UnitGas) (int.TryParse(text[2], out value) ? value : 0),
374+
Move = Rules.Cosmic.MovementMultiplier * (int.TryParse(text[3].Replace(".", string.Empty), out value) ? value : 0),
375+
Range = int.TryParse(text[4], out value) ? value : 0,
376+
Attack = int.TryParse(text[5].Replace("a", string.Empty), out value) ? value : 0,
377+
Defense = int.TryParse(text[6].Replace("d", string.Empty), out value) ? value : 0,
378+
Hitp = 10 * (int.TryParse(text[7].Replace("h", string.Empty), out value) ? value : 0),
379+
Firepwr = int.TryParse(text[8].Replace("f", string.Empty), out value) ? value : 0,
380+
Cost = int.TryParse(text[9], out value) ? value : 0,
381+
Hold = int.TryParse(text[10], out value) ? value : 0,
382+
AIrole = (AIroleType)(int.TryParse(text[11], out value) ? value : 0),
384383
Prereq = Rules.AdvanceMappings.ContainsKey(text[12]) ? Rules.AdvanceMappings[text[12]] : -1, // temp
385384
Flags = text[13],
386385
AttackSound = defaultAttackSounds.FirstOrDefault(s=>s.Item1 == type)?.Item2
@@ -405,15 +404,14 @@ private void ProcessUnits(string[] values)
405404

406405
private void ProcessAdvancedUnitFlags(string[] values)
407406
{
408-
409407
var limit = values.Length < Rules.UnitTypes.Length ? values.Length : Rules.UnitTypes.Length;
410408
for (int i = 0; i < limit; i++)
411409
{
412410
var line = values[i].Split(new []{ ',',';'}, StringSplitOptions.TrimEntries);
413411
var unit = Rules.UnitTypes[i];
414412
unit.CivCanBuild = ReadBitsReversed(line[0]);
415413
unit.CanBeOnMap = ReadBitsReversed(line[1]);
416-
unit.MinBribe = int.Parse(line[2]);
414+
unit.MinBribe = int.TryParse(line[2], out int val) ? val : 0;
417415
var extraFlags = ReadBitsReversed(line[6]);
418416
unit.Invisible = extraFlags[0];
419417
unit.NonDispandable = extraFlags[1];
@@ -456,9 +454,9 @@ private void ProcessImprovements(string[] values)
456454
{
457455
Type = type,
458456
Name = parts[0],
459-
Cost = int.Parse(parts[1]),
460-
Upkeep = int.Parse(parts[2]),
461-
Prerequisite = Rules.AdvanceMappings.ContainsKey(parts[3]) ? Rules.AdvanceMappings[parts[3]] : -1 // temp
457+
Cost = int.TryParse(parts[1], out int val) ? val : 1,
458+
Upkeep = int.TryParse(parts[2], out val) ? val : 0,
459+
Prerequisite = Rules.AdvanceMappings.TryGetValue(parts[3], out val) ? val : -1
462460
};
463461
}).ToArray();
464462
}
@@ -542,12 +540,12 @@ private void ProcessTech(IReadOnlyList<string> values)
542540
{
543541
Index = index,
544542
Name = text[0],
545-
AIvalue = int.Parse(text[1]),
546-
Modifier = int.Parse(text[2]),
547-
Prereq1 = Rules.AdvanceMappings.ContainsKey(text[3]) ? Rules.AdvanceMappings[text[3]] : -1, // temp
548-
Prereq2 = Rules.AdvanceMappings.ContainsKey(text[4]) ? Rules.AdvanceMappings[text[4]] : -1, // temp
549-
Epoch = int.Parse(text[5]),
550-
KnowledgeCategory = int.Parse(text[6])
543+
AIvalue = int.TryParse(text[1], out int val) ? val : 1,
544+
Modifier = int.TryParse(text[2], out val) ? val : 0,
545+
Prereq1 = Rules.AdvanceMappings.TryGetValue(text[3], out val) ? val : -1,
546+
Prereq2 = Rules.AdvanceMappings.TryGetValue(text[4], out val) ? val : -1,
547+
Epoch = int.TryParse(text[5], out val) ? val : 0,
548+
KnowledgeCategory = int.TryParse(text[6], out val) ? val : 0
551549
};
552550
}).ToArray();
553551
}
@@ -571,11 +569,11 @@ private Special MakeSpecial(string source)
571569
return new Special
572570
{
573571
Name = line[0],
574-
MoveCost = int.Parse(line[1]),
575-
Defense = int.Parse(line[2]),
576-
Food = int.Parse(line[3]),
577-
Shields = int.Parse(line[4]),
578-
Trade = int.Parse(line[5]),
572+
MoveCost = int.TryParse(line[1], out int val) ? val : 1,
573+
Defense = int.TryParse(line[2], out val) ? val : 1,
574+
Food = int.TryParse(line[3], out val) ? val : 0,
575+
Shields = int.TryParse(line[4], out val) ? val : 0,
576+
Trade = int.TryParse(line[5], out val) ? val : 0,
579577
};
580578
}
581579
}

Engine/src/MapObjects/Islands.cs

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public static void RenumberOceans(this Map map, List<Tile> oceanTiles)
109109

110110
public static void RenumberOceans(this Map map, IEnumerable<IslandDetails> oceans)
111111
{
112+
if (!oceans.Any()) return;
113+
112114
var orderedOceans = oceans.OrderByDescending(i => i.Tiles.Count).ToList();
113115

114116
orderedOceans[0].Id = 0;

RaylibUI/BasicTypes/Controls/ImageBox.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
using Model.Images;
44
using RaylibUtils;
55
using Model.Dialog;
6+
using Model;
67

78
namespace RaylibUI.BasicTypes.Controls;
89

910
public class ImageBox : BaseControl
1011
{
12+
private readonly IUserInterface _active;
1113
private readonly IImageSource[] _image;
1214
private readonly float _scale;
1315
private readonly int[,] _coords;
@@ -17,16 +19,17 @@ public ImageBox(IControlLayout controller, DialogImageElements image) : base(con
1719
_image = image.Image;
1820
_scale = image.Scale;
1921
_coords = image.Coords;
22+
_active = controller.MainWindow.ActiveInterface;
2023
}
2124

2225
public override int GetPreferredWidth()
2326
{
24-
return _image.Select(img => Images.GetImageWidth(img, _scale)).Max();
27+
return _image.Select(img => Images.GetImageWidth(img, _active, _scale)).Max();
2528
}
2629

2730
public override int GetPreferredHeight()
2831
{
29-
return _image.Select(img => Images.GetImageHeight(img, _scale)).Max();
32+
return _image.Select(img => Images.GetImageHeight(img, _active, _scale)).Max();
3033
}
3134

3235
public override void Draw(bool pulse)

RaylibUI/BasicTypes/Controls/OptionControl.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal class OptionControl : LabelControl
1717
public override bool CanFocus => true;
1818

1919
public OptionControl(IControlLayout controller, string text, int index, bool isChecked, IImageSource[] images) : base(
20-
controller, text, eventTransparent: false, offset: Images.GetImageWidth(images[0]),
20+
controller, text, eventTransparent: false, offset: Images.GetImageWidth(images[0], controller.MainWindow.ActiveInterface),
2121
font: controller.MainWindow.ActiveInterface.Look.LabelFont,
2222
fontSize: controller.MainWindow.ActiveInterface.Look.LabelFontSize,
2323
colorFront: controller.MainWindow.ActiveInterface.Look.LabelColour,
@@ -26,8 +26,8 @@ public OptionControl(IControlLayout controller, string text, int index, bool isC
2626
Index = index;
2727
Checked = isChecked;
2828
_images = images;
29-
_imageWidth = Images.GetImageWidth(images[0]);
30-
_imageHeight = Images.GetImageHeight(images[0]);
29+
_imageWidth = Images.GetImageWidth(images[0], controller.MainWindow.ActiveInterface);
30+
_imageHeight = Images.GetImageHeight(images[0], controller.MainWindow.ActiveInterface);
3131
}
3232

3333
public int Index { get; }

RaylibUI/RunGame/Commands/Orders/BuildCity.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ public override bool Update()
7272

7373
if (activeTile.CityHere != null)
7474
{
75-
return SetCommandState(activeTile.CityHere.Size < GameScreen.Game.Rules.Cosmic.ToExceedCitySizeAqueductNeeded ? CommandStatus.Normal : CommandStatus.Disabled, Labels.For(LabelIndex.JoinCity), errorPopupKeyword: "ONLY10", errorPopupImage: new(new[] { cityImage.Image, flagImage.Image }, 2, coords: new int[,] { { 0, 0 }, { (int)cityImage.FlagLoc.X, (int)cityImage.FlagLoc.Y - Images.GetImageHeight(flagImage.Image) - 5 } }));
75+
return SetCommandState(activeTile.CityHere.Size < GameScreen.Game.Rules.Cosmic.ToExceedCitySizeAqueductNeeded ? CommandStatus.Normal : CommandStatus.Disabled, Labels.For(LabelIndex.JoinCity), errorPopupKeyword: "ONLY10", errorPopupImage: new(new[] { cityImage.Image, flagImage.Image }, 2, coords: new int[,] { { 0, 0 }, { (int)cityImage.FlagLoc.X, (int)cityImage.FlagLoc.Y - Images.GetImageHeight(flagImage.Image, _active) - 5 } }));
7676
}
7777
else
7878
{
79-
return SetCommandState(errorPopupKeyword: "ADJACENTCITY", errorPopupImage: new(new[] { cityImage.Image, flagImage.Image }, 2, coords: new int[,] { { 0, 0 }, { (int)cityImage.FlagLoc.X, (int)cityImage.FlagLoc.Y - Images.GetImageHeight(flagImage.Image) - 5 } }));
79+
return SetCommandState(errorPopupKeyword: "ADJACENTCITY", errorPopupImage: new(new[] { cityImage.Image, flagImage.Image }, 2, coords: new int[,] { { 0, 0 }, { (int)cityImage.FlagLoc.X, (int)cityImage.FlagLoc.Y - Images.GetImageHeight(flagImage.Image, _active) - 5 } }));
8080
}
8181
}
8282

RaylibUtils/Images.ImportBitmaps.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ public static Image ExtractBitmap(IImageSource imageSource)
8787
return ExtractBitmapData(imageSource, active: null).Image;
8888
}
8989

90-
public static int GetImageWidth(IImageSource? imageSource, float scale = 1f)
90+
public static int GetImageWidth(IImageSource? imageSource, IUserInterface active, float scale = 1f)
9191
{
92-
return imageSource == null ? 0 : (int)(ExtractBitmap(imageSource).Width * scale);
92+
return imageSource == null ? 0 : (int)(ExtractBitmap(imageSource, active).Width * scale);
9393
}
9494

95-
public static int GetImageHeight(IImageSource? imageSource, float scale = 1f)
95+
public static int GetImageHeight(IImageSource? imageSource, IUserInterface active, float scale = 1f)
9696
{
97-
return imageSource == null ? 0 : (int)(ExtractBitmap(imageSource).Height * scale);
97+
return imageSource == null ? 0 : (int)(ExtractBitmap(imageSource, active).Height * scale);
9898
}
9999

100100
public static ImageProps ExtractBitmapData(IImageSource imageSource, IUserInterface? active, int owner = -1, string[]? searchPaths = null)
@@ -140,6 +140,7 @@ public static ImageProps ExtractBitmapData(IImageSource imageSource, IUserInterf
140140
string[] _paths = active != null ?
141141
active.MainApp.ActiveRuleSet.Paths : searchPaths ?? Settings.SearchPaths;
142142
var path = Utils.GetFilePath(bitmapStorage.Filename, _paths, bitmapStorage.Extension);
143+
path ??= Utils.GetFilePath(bitmapStorage.Filename, Settings.SearchPaths, bitmapStorage.Extension);
143144
var source_img_bpp = Images.LoadImageFromFile(path);
144145
_imageCache[sourceKey] = source_img_bpp.Image;
145146
_sourceBpp[sourceKey] = source_img_bpp.ColourDepth;

RaylibUtils/Images.cs

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public static Image_and_bpp LoadImageFromFile(string filename, int dataStart = 0
6767
var height = BitConverter.ToInt32(bytes, 22);
6868
bpp = BitConverter.ToInt16(bytes, 28);
6969
var size = BitConverter.ToInt32(bytes, 34);
70+
if (size == 0)
71+
size = bytes.Length - dataOffset;
7072
var extraBits = size / height - bpp / 8 * width;
7173
var imgData = new byte[4 * width * height];
7274
int flag1Color = Convert.ToInt32("0x0000FFFF", 16);

0 commit comments

Comments
 (0)