Skip to content

Commit

Permalink
Prevent crash when game crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrouind committed Sep 9, 2024
1 parent 8f8aa13 commit 8384a06
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
17 changes: 11 additions & 6 deletions VarsViewer/VarsViewer/Actors/ActorWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace VarsViewer
public class ActorWorker : IWorker
{
bool IWorker.UseMouse => false;
readonly Func<int> getAddress;
readonly Func<int> getActorAddress;
readonly (int Rows, int Columns) cellConfig;
readonly List<Column> config;

Expand All @@ -39,13 +39,13 @@ public ActorWorker(int view)
{
case 0:
config = LoadConfig("Actor.json");
getAddress = () => GameConfig.ActorAddress + Program.EntryPoint;
getActorAddress = () => GameConfig.ActorAddress + Program.EntryPoint;
cellConfig = (50, 80);
break;

case 1:
config = LoadConfig("Object.json");
getAddress = () => Program.Memory.ReadFarPointer(GameConfig.ObjectAddress + Program.EntryPoint);
getActorAddress = () => Program.Memory.ReadFarPointer(GameConfig.ObjectAddress + Program.EntryPoint);
cellConfig = (Program.GameVersion == GameVersion.AITD1_DEMO ? 18 : 292, 26);
break;
}
Expand Down Expand Up @@ -343,10 +343,15 @@ void IWorker.ReadMemory()
void ReadActors()
{
(int rows, int columns) = cellConfig;
int address = getAddress();
int actorPointer = getActorAddress();

if ((actorPointer + rows * columns * 2) >= Program.Memory.Length) //should never happen, unless game crashes
{
return;
}

if (Enumerable.Range(0, rows)
.All(x => Program.Memory.ReadShort(address + x * columns * 2) == 0)) //are actors initialized ?
.All(x => Program.Memory.ReadShort(actorPointer + x * columns * 2) == 0)) //are actors initialized ?
{
ClearTab();
foreach (var actor in actors)
Expand All @@ -365,7 +370,7 @@ void ReadActors()

for (int i = 0; i < rows; i++)
{
int startAddress = address + i * columns * 2;
int startAddress = actorPointer + i * columns * 2;
int id = Program.Memory.ReadShort(startAddress);
var actor = actors[i];

Expand Down
4 changes: 2 additions & 2 deletions VarsViewer/VarsViewer/Cache/CacheWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void IWorker.ReadMemory()
foreach (var ch in Cache)
{
int cachePointer = Program.Memory.ReadFarPointer(Program.EntryPoint + GameConfig[ch.Index]);
if (cachePointer != 0)
if (cachePointer != 0 && cachePointer < Program.Memory.Length) //should never happen, unless game crashes
{
DosMCB block = DosBox.ReadMCB(Program.Memory, cachePointer - 16);
if ((block.Tag == 0x4D || block.Tag == 0x5A) && block.Owner != 0 && block.Size < 4096) //block is still allocated
Expand Down Expand Up @@ -237,7 +237,7 @@ void RenderCache(Cache ch, int column)
Console.Write(ch.Name);

Console.SetCursorPosition(column * 19 + 14, 0);
(Console.BackgroundColor, Console.ForegroundColor) = (ConsoleColor.Black, ConsoleColor.Gray);
(Console.BackgroundColor, Console.ForegroundColor) = (ConsoleColor.Black, ConsoleColor.Gray);
switch (Sort.SortMode)
{
case SortMode.Memory:
Expand Down
2 changes: 1 addition & 1 deletion VarsViewer/VarsViewer/Vars/VarsWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void IWorker.ReadMemory()
{
vars.Count = 0;
}
else
else if ((varsPointer + vars.Count * 2) < Program.Memory.Length) //should never happen, unless game crashes
{
vars.Count = Program.GameVersion == GameVersion.AITD1_DEMO ? 22 : 207;
CheckDifferences(varsPointer, vars);
Expand Down

0 comments on commit 8384a06

Please sign in to comment.