Skip to content

Commit

Permalink
refactoring, catch error and display warning for bad layout file, rem…
Browse files Browse the repository at this point in the history
…ove WMP reference, correctly add custom watch rooms
  • Loading branch information
TalicZealot committed Jun 24, 2024
1 parent 1b26a6d commit 9234ef4
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net48</TargetFramework>

<IsPackable>false</IsPackable>

</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public void ThrowArgumentNullException_WhenRelicsArgumentIsNull()
//Arrange
var mockedFormGraphics = Substitute.For<IGraphics>();
var mockedToolConfig = Substitute.For<IToolConfig>();
List<TrackerRelic> testRelics = new List<TrackerRelic>();
List<Item> testProgressionItems = new List<Item>();
List<Item> testThrustSwords = new List<Item>();
TrackerRelic[] testRelics = new TrackerRelic[1];
Item[] testProgressionItems = new Item[1];
Item[] testThrustSwords = new Item[1];
//Act
TrackerRendererGDI trackerGraphicsEngine = new TrackerRendererGDI(mockedFormGraphics, mockedToolConfig);
//Assert
Expand All @@ -31,9 +31,9 @@ public void ThrowArgumentNullException_WhenProgressionItemsArgumentIsNull()
//Arrange
var mockedFormGraphics = Substitute.For<IGraphics>();
var mockedToolConfig = Substitute.For<IToolConfig>();
List<TrackerRelic> testRelics = new List<TrackerRelic>();
List<Item> testProgressionItems = new List<Item>();
List<Item> testThrustSwords = new List<Item>();
TrackerRelic[] testRelics = new TrackerRelic[1];
Item[] testProgressionItems = new Item[1];
Item[] testThrustSwords = new Item[1];
//Act
TrackerRendererGDI trackerGraphicsEngine = new TrackerRendererGDI(mockedFormGraphics, mockedToolConfig);
//Assert
Expand All @@ -46,9 +46,9 @@ public void ThrowArgumentNullException_WhenThrustSwordsArgumentIsNull()
//Arrange
var mockedFormGraphics = Substitute.For<IGraphics>();
var mockedToolConfig = Substitute.For<IToolConfig>();
List<TrackerRelic> testRelics = new List<TrackerRelic>();
List<Item> testProgressionItems = new List<Item>();
List<Item> testThrustSwords = new List<Item>();
TrackerRelic[] testRelics = new TrackerRelic[1];
Item[] testProgressionItems = new Item[1];
Item[] testThrustSwords = new Item[1];
//Act
TrackerRendererGDI trackerGraphicsEngine = new TrackerRendererGDI(mockedFormGraphics, mockedToolConfig);
//Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void ReturnsAnInstance_WhenParametersAreNotNull()
mockedToolConfig.Tracker.UseOverlay = false;
mockedToolConfig.Tracker.EnableAutosplitter = false;
mockedGraphicsEngine
.When(g => g.InitializeItems(Arg.Any<List<Models.TrackerRelic>>(), Arg.Any<List<Models.Item>>(), Arg.Any<List<Models.Item>>()))
.When(g => g.InitializeItems(Arg.Any<Models.TrackerRelic[]>(), Arg.Any<Models.Item[]>(), Arg.Any< Models.Item[]>()))
.Do(x => { });
mockedGraphicsEngine
.When(g => g.CalculateGrid(Arg.Any<int>(), Arg.Any<int>()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net48</TargetFramework>

<IsPackable>false</IsPackable>

</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 0 additions & 6 deletions SotnRandoTools/src/Configuration/TrackerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ public override object ReadJson(
else
{
return TrackerConfig.GetDefaultOverlay();
//throw new Exception(
// string.Format(
// "Unexpected token parsing binary. "
// + "Expected StartArray, got {0}.",
// reader.TokenType));
}
}

Expand Down Expand Up @@ -186,7 +181,6 @@ public void LoadOverlayLayout(string path)
if (File.Exists(path))
{
string layoutJson = File.ReadAllText(path);

byte[] layout = JsonConvert.DeserializeObject<byte[]>(layoutJson,
new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace, MissingMemberHandling = MissingMemberHandling.Ignore });

Expand Down
14 changes: 13 additions & 1 deletion SotnRandoTools/src/Panels/AutotrackerSettingsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,19 @@ private void saveLayoutButton_Click(object sender, EventArgs e)

private void openLayoutDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
toolConfig.Tracker.LoadOverlayLayout(openLayoutDialog.FileName);
try
{
toolConfig.Tracker.LoadOverlayLayout(openLayoutDialog.FileName);
}
catch (Exception)
{
string message = "Unsupported or outdated file format!";
string caption = "Unsupported or outdated file format!";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result;

result = MessageBox.Show(message, caption, buttons);
}
}

private void saveLayoutDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
Expand Down
3 changes: 2 additions & 1 deletion SotnRandoTools/src/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"commandName": "Executable",
"executablePath": "D:\\Programming\\SotnRandoTools\\SotnRandoTools\\BizHawk\\EmuHawk.exe",
"commandLineArgs": "--open-ext-tool-dll=SotnRandoTools",
"workingDirectory": "D:\\Programming\\SotnRandoTools\\SotnRandoTools\\BizHawk"
"workingDirectory": "D:\\Programming\\SotnRandoTools\\SotnRandoTools\\BizHawk",
"nativeDebugging": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ internal interface ITrackerRenderer
void CalculateGrid(int width, int height);
void Render();
void ChangeGraphics(IGraphics formGraphics);
void InitializeItems(List<Models.TrackerRelic> relics, List<Item> progressionItems, List<Item> thrustSwords);
void InitializeItems(Models.TrackerRelic[] relics, Item[] progressionItems, Item[] thrustSwords);
}
}
8 changes: 4 additions & 4 deletions SotnRandoTools/src/RandoTracker/Models/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ internal sealed class Location
{
public Location()
{
Locks = new List<string[]>();
OutOfLogicLocks = new List<string[]>();
Locks = new List<int[]>();
OutOfLogicLocks = new List<int[]>();
Rooms = new List<Room>();
WatchIndecies = new List<int>();
AvailabilityColor = MapColor.Unavailable;
Expand All @@ -30,8 +30,8 @@ public Location()
public bool Visited { get; set; }
public int X { get; set; }
public int Y { get; set; }
public List<string[]>? Locks { get; set; }
public List<string[]>? OutOfLogicLocks { get; set; }
public List<int[]>? Locks { get; set; }
public List<int[]>? OutOfLogicLocks { get; set; }
public List<int> WatchIndecies { get; set; }
public List<Room>? Rooms { get; set; }
}
Expand Down
109 changes: 73 additions & 36 deletions SotnRandoTools/src/RandoTracker/Tracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ internal sealed class Tracker
new Room { WatchIndex = 81, Values = new int[] { 0x10 }},
}},
};
private readonly List<TrackerRelic> relics = new List<TrackerRelic>
private readonly TrackerRelic[] relics = new TrackerRelic[]
{
new TrackerRelic { Name = "SoulOfBat", Progression = true},
new TrackerRelic { Name = "FireOfBat", Progression = false},
Expand Down Expand Up @@ -393,14 +393,14 @@ internal sealed class Tracker
new TrackerRelic { Name = "RingOfVlad", Progression = true},
new TrackerRelic { Name = "EyeOfVlad", Progression = true}
};
private readonly List<Item> progressionItems = new List<Item>
private readonly Item[] progressionItems = new Item[]
{
new Item { Name = "GoldRing", Value = 72 },
new Item { Name = "SilverRing", Value = 73 },
new Item { Name = "SpikeBreaker", Value = 14 },
new Item { Name = "HolyGlasses", Value = 34 }
};
private readonly List<Item> thrustSwords = new List<Item>
private readonly Item[] thrustSwords = new Item[]
{
new Item { Name = "Estoc", Value = 95 },
new Item { Name = "Claymore", Value = 98 },
Expand Down Expand Up @@ -448,14 +448,6 @@ internal sealed class Tracker
{ "spikebreaker", 2 },
{ "holyglasses", 3 },
};
private readonly Dictionary<string, int> swordToIndex = new Dictionary<string, int>
{
{ "estoc", 0 },
{ "claymore", 1 },
{ "flamberge", 2 },
{ "zweihander", 3 },
{ "obsidiansword", 4 },
};
private readonly HashSet<string> VanillaPresets = new HashSet<string>
{
"adventure",
Expand Down Expand Up @@ -732,6 +724,7 @@ private bool LoadExtension(string extensionFilePath)
foreach (ExtensionLocation location in extension.Locations)
{
Location? customLocation = locations.Where(x => x.Name == location.Name).FirstOrDefault();
int locationIndex = 0;

if (customLocation is null)
{
Expand All @@ -743,6 +736,8 @@ private bool LoadExtension(string extensionFilePath)
SecondCastle = location.SecondCastle,
CustomExtension = true
};
locations.Add(customLocation);
locationIndex = locations.Count - 1;
}
else
{
Expand All @@ -756,7 +751,7 @@ private bool LoadExtension(string extensionFilePath)
{
customLocation.CustomExtension = true;
}
continue;
locationIndex = locations.IndexOf(customLocation);
}

int roomCounter = 0;
Expand All @@ -774,9 +769,8 @@ private bool LoadExtension(string extensionFilePath)
customLocation.WatchIndecies.Add(watchlistService.SafeLocationWatches.Count - 1);
customLocation.Rooms.Add(customRoom);
watchToRoom[watchlistService.SafeLocationWatches.Count - 1] = roomCounter - 1;
watchToLocation[watchlistService.SafeLocationWatches.Count - 1] = locations.Count;
watchToLocation[watchlistService.SafeLocationWatches.Count - 1] = locationIndex;
}
locations.Add(customLocation);
}
return true;
}
Expand Down Expand Up @@ -860,13 +854,34 @@ private void LoadLocks(string presetFilePath, bool outOfLogic = false, bool over
}
}
}

int[] mappedLockSet = new int[newLockSet.Length];

for (int i = 0; i < newLockSet.Length; i++)
{
if (relicToIndex.ContainsKey(newLockSet[i]))
{
mappedLockSet[i] = relicToIndex[newLockSet[i]];

}
else if (itemToIndex.ContainsKey(newLockSet[i]))
{
mappedLockSet[i] = 100 + itemToIndex[newLockSet[i]];

}
else if (newLockSet[i] == "thrustsword")
{
mappedLockSet[i] = 200;
}
}

if (outOfLogic)
{
trackerLocation.OutOfLogicLocks.Add(newLockSet);
trackerLocation.OutOfLogicLocks.Add(mappedLockSet);
}
else
{
trackerLocation.Locks.Add(newLockSet);
trackerLocation.Locks.Add(mappedLockSet);
}
}
}
Expand Down Expand Up @@ -896,7 +911,27 @@ private void LoadLocks(string presetFilePath, bool outOfLogic = false, bool over
}
}

trackerLocation.OutOfLogicLocks.Add(newLockSet);
int[] mappedLockSet = new int[newLockSet.Length];

for (int i = 0; i < newLockSet.Length; i++)
{
if (relicToIndex.ContainsKey(newLockSet[i]))
{
mappedLockSet[i] = relicToIndex[newLockSet[i]];

}
else if (itemToIndex.ContainsKey(newLockSet[i]))
{
mappedLockSet[i] = 100 + itemToIndex[newLockSet[i]];

}
else if (newLockSet[i] == "thrustsword")
{
mappedLockSet[i] = 200;
}
}

trackerLocation.OutOfLogicLocks.Add(mappedLockSet);
}
}

Expand Down Expand Up @@ -1040,7 +1075,7 @@ private bool UpdateProgressionItems()
}
watchlistService.ProgressionItemWatches.ClearChangeCounts();

for (int i = 0; i < progressionItems.Count; i++)
for (int i = 0; i < progressionItems.Length; i++)
{
switch (i)
{
Expand Down Expand Up @@ -1107,7 +1142,7 @@ private bool UpdateThrustSwords()
}
watchlistService.ThrustSwordWatches.ClearChangeCounts();

for (int i = 0; i < thrustSwords.Count; i++)
for (int i = 0; i < thrustSwords.Length; i++)
{
thrustSwords[i].Equipped = (sotnApi.AlucardApi.RightHand == thrustSwords[i].Value);

Expand Down Expand Up @@ -1410,26 +1445,28 @@ private void CheckReachability()
}
}

private bool TrackedObjectStatus(string name)
private bool TrackedObjectStatus(int index)
{
if (relicToIndex.ContainsKey(name))
if (index == 200)
{
TrackerRelic relic = relics[relicToIndex[name]];
return relic.Collected;
for (int i = 0; i < thrustSwords.Length; i++)
{
if (thrustSwords[i].Collected)
{
return true;
}
}
}
if (name == "holyglasses" && secondCastle)
else if (index < 100)
{
return true;
TrackerRelic relic = relics[index];
return relic.Collected;
}
if (itemToIndex.ContainsKey(name))
else
{
Item progressionItem = progressionItems[itemToIndex[name]];
Item progressionItem = progressionItems[index - 100];
return progressionItem.Status;
}
if (name == "thrustsword" && swordToIndex.ContainsKey(name))
{
return true;
}

return false;
}
Expand All @@ -1445,13 +1482,13 @@ private void UpdateOverlay()
private int EncodeItems()
{
int itemsNumber = 0;
for (int i = 0; i < progressionItems.Count + 1; i++)
for (int i = 0; i < progressionItems.Length + 1; i++)
{
if (i < progressionItems.Count && progressionItems[i].Status)
if (i < progressionItems.Length && progressionItems[i].Status)
{
itemsNumber |= (int) Math.Pow(2, i);
}
else if (i == progressionItems.Count)
else if (i == progressionItems.Length)
{
foreach (var sword in thrustSwords)
{
Expand All @@ -1470,7 +1507,7 @@ private int EncodeItems()
private int EncodeRelics()
{
int relicsNumber = 0;
for (int i = 0; i < relics.Count; i++)
for (int i = 0; i < relics.Length; i++)
{
if (relics[i].Collected)
{
Expand Down Expand Up @@ -1557,7 +1594,7 @@ public void SaveReplay()
}
replayPath = Paths.ReplaysPath + replayPath + ".sotnr";

byte[] replayBytes = new byte[2 + (replayLenght * 4) + ((relics.Count + progressionItems.Count + 1) * 4)];
byte[] replayBytes = new byte[2 + (replayLenght * 4) + ((relics.Length + progressionItems.Length + 1) * 4)];

int replayIndex = 0;
byte[] finalTimeSecondsBytes = BitConverter.GetBytes((ushort) finalTime.TotalSeconds);
Expand Down
Loading

0 comments on commit 9234ef4

Please sign in to comment.