Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrouind committed Sep 5, 2024
1 parent 52621da commit 752cdb9
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 108 deletions.
4 changes: 4 additions & 0 deletions VarsViewer/VarsViewer/Actors/Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ public class Actor
public long CreationTime;
public long DeletionTime;
public long UpdateTime;

public bool Created;
public bool Updated;
public bool Deleted;
}
}
197 changes: 100 additions & 97 deletions VarsViewer/VarsViewer/Actors/ActorWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ActorWorker : IWorker
{
bool IWorker.UseMouse => false;
readonly Func<int> getAddress;
(int Rows, int Columns) cellConfig;
readonly (int Rows, int Columns) cellConfig;
readonly List<Column> config;

static (int ActorAddress, int ObjectAddress) GameConfig => gameConfigs[Program.GameVersion];
Expand Down Expand Up @@ -52,6 +52,14 @@ public ActorWorker(int view)
}

actors = new Actor[cellConfig.Rows];
for (int i = 0; i < actors.Length; i++)
{
actors[i] = new Actor
{
Id = -1,
Values = new byte[cellConfig.Columns * 2]
};
}

List<Column> LoadConfig(string fileName)
{
Expand Down Expand Up @@ -116,11 +124,6 @@ void Populate()
void IWorker.Render()
{
int rowsCount = 0;
if (!freeze)
{
timeStamp = Stopwatch.GetTimestamp();
}

(int rows, int columns) = cellConfig;

if (TimeSpan.FromTicks(timeStamp - refreshTime) > TimeSpan.FromSeconds(5))
Expand All @@ -129,77 +132,10 @@ void IWorker.Render()
HideColumns();
}

if (ReadActors())
{
WriteCells();
}

WriteCells();
ResizeColumns();
OutputToConsole();

bool ReadActors()
{
int address = getAddress();

if (Enumerable.Range(0, rows)
.All(x => Program.Memory.ReadShort(address + x * columns * 2) == 0)) //are actors initialized ?
{
return false;
}

FieldFormatter.Timer1 = Program.Memory.ReadUnsignedInt(Program.EntryPoint + 0x19D12);
FieldFormatter.Timer2 = Program.Memory.ReadUnsignedShort(Program.EntryPoint + 0x242E0);
FieldFormatter.FullMode = fullMode;

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

if (actor == null)
{
actor = new Actor
{
Id = -1,
Values = new byte[columns * 2]
};
actors[i] = actor;
}

if ((actor.Id == -1 && id != -1) || actor.Id != id) //created
{
actor.CreationTime = timeStamp;
actor.DeletionTime = 0;
actor.UpdateTime = 0;
}

if (actor.Id != -1 && id == -1) //deleted
{
actor.DeletionTime = timeStamp;
actor.CreationTime = 0;
actor.UpdateTime = 0;
}

if (id != -1 && actor.Id == id)
{
for (int j = 0; j < actor.Values.Length; j++)
{
if (Program.Memory[j + startAddress] != actor.Values[j])
{
actor.UpdateTime = timeStamp;
break;
}
}
}

actor.Id = id;
Array.Copy(Program.Memory, startAddress, actor.Values, 0, actor.Values.Length);
}

return true;
}

void WriteCells()
{
cells.Clear();
Expand All @@ -209,24 +145,20 @@ void WriteCells()
int maxRow = 0;

Actor actor = actors[i];
var deleted = Shared.Tools.GetTimeSpan(timeStamp, actor.DeletionTime) < TimeSpan.FromSeconds(2);
var added = Shared.Tools.GetTimeSpan(timeStamp, actor.CreationTime) < TimeSpan.FromSeconds(2);
var updated = Shared.Tools.GetTimeSpan(timeStamp, actor.UpdateTime) < TimeSpan.FromSeconds(1);

if (actor != null && (actor.Id != -1 || showAll || deleted))
if (actor.Id != -1 || showAll || actor.Deleted)
{
(ConsoleColor, ConsoleColor) color;
if (deleted)
if (actor.Deleted)
{
color = (ConsoleColor.DarkGray, ConsoleColor.Black); //removed
color = (ConsoleColor.DarkGray, ConsoleColor.Black);
}
else if (added)
else if (actor.Created)
{
color = (ConsoleColor.DarkGreen, ConsoleColor.Black); //added
color = (ConsoleColor.DarkGreen, ConsoleColor.Black);
}
else if (updated)
else if (actor.Updated)
{
color = (ConsoleColor.Black, ConsoleColor.DarkYellow); //updated
color = (ConsoleColor.Black, ConsoleColor.DarkYellow);
}
else
{
Expand Down Expand Up @@ -397,9 +329,80 @@ void OutputToConsole()

bool IWorker.ReadMemory()
{
if (!freeze && Program.Process.Read(Program.Memory, 0, 640 * 1024) == 0)
if (!freeze)
{
return false;
if (Program.Process.Read(Program.Memory, 0, 640 * 1024) == 0)
{
return false;
}

timeStamp = Stopwatch.GetTimestamp();
ReadActors();
}

void ReadActors()
{
(int rows, int columns) = cellConfig;
int address = getAddress();

if (Enumerable.Range(0, rows)
.All(x => Program.Memory.ReadShort(address + x * columns * 2) == 0)) //are actors initialized ?
{
ClearTab();
foreach (var actor in actors)
{
actor.Id = -1;
actor.Created = false;
actor.Deleted = false;
actor.Updated = false;
Array.Clear(actor.Values, 0, actor.Values.Length);
}
return;
}

FieldFormatter.Timer1 = Program.Memory.ReadUnsignedInt(Program.EntryPoint + 0x19D12);
FieldFormatter.Timer2 = Program.Memory.ReadUnsignedShort(Program.EntryPoint + 0x242E0);
FieldFormatter.FullMode = fullMode;

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

if ((actor.Id == -1 && id != -1) || actor.Id != id) //created
{
actor.CreationTime = timeStamp;
actor.DeletionTime = 0;
actor.UpdateTime = 0;
}

if (actor.Id != -1 && id == -1) //deleted
{
actor.DeletionTime = timeStamp;
actor.CreationTime = 0;
actor.UpdateTime = 0;
}

if (id != -1 && actor.Id == id)
{
for (int j = 0; j < actor.Values.Length; j++)
{
if (Program.Memory[j + startAddress] != actor.Values[j])
{
actor.UpdateTime = timeStamp;
break;
}
}
}

actor.Id = id;
actor.Deleted = Shared.Tools.GetTimeSpan(timeStamp, actor.DeletionTime) < TimeSpan.FromSeconds(2);
actor.Created = Shared.Tools.GetTimeSpan(timeStamp, actor.CreationTime) < TimeSpan.FromSeconds(2);
actor.Updated = Shared.Tools.GetTimeSpan(timeStamp, actor.UpdateTime) < TimeSpan.FromSeconds(1);

Array.Copy(Program.Memory, startAddress, actor.Values, 0, actor.Values.Length);
}
}

return true;
Expand Down Expand Up @@ -438,19 +441,19 @@ void IWorker.KeyDown(ConsoleKeyInfo key)
freeze = !freeze;
break;
}
}

void ClearTab()
void ClearTab()
{
cells.Clear();
foreach (var group in config)
{
cells.Clear();
foreach (var group in config)
group.Width = 0;
group.Visible = false;
foreach (var col in group.Columns[0].Columns)
{
group.Width = 0;
group.Visible = false;
foreach (var col in group.Columns[0].Columns)
{
col.Width = 0;
col.Visible = false;
}
col.Width = 0;
col.Visible = false;
}
}
}
Expand Down
21 changes: 10 additions & 11 deletions VarsViewer/VarsViewer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ class Program
static readonly Stopwatch dosboxTimer = new Stopwatch();

static readonly IWorker[] workers = new IWorker[] { new VarsWorker(), new CacheWorker(), new ActorWorker(0), new ActorWorker(1) };
static int view = -1;
static IWorker Worker => workers[view];
static IWorker worker;

public static void Main(string[] args)
{
Expand Down Expand Up @@ -51,14 +50,14 @@ public static void Main(string[] args)

if (Process != null)
{
if (!Worker.ReadMemory())
if (!worker.ReadMemory())
{
CloseReader();
}
}

Console.Clear();
Worker.Render();
worker.Render();
Console.Flush();

Thread.Sleep(15);
Expand Down Expand Up @@ -96,7 +95,7 @@ void SetupConsole()
break;
default:
Worker.KeyDown(keyInfo);
worker.KeyDown(keyInfo);
break;
}
};
Expand All @@ -106,19 +105,19 @@ void SetupConsole()
switch (keyInfo.Key)
{
case (ConsoleKey)17: //control
Console.MouseInput = Worker.UseMouse;
Console.MouseInput = worker.UseMouse;
break;
}
};

Console.MouseDown += (sender, position) =>
{
Worker.MouseDown(position.x, position.y);
worker.MouseDown(position.x, position.y);
};

Console.MouseMove += (sender, position) =>
{
Worker.MouseMove(position.x, position.y);
worker.MouseMove(position.x, position.y);
};
}
}
Expand Down Expand Up @@ -167,10 +166,10 @@ static void CloseReader()

static void SetView(int view)
{
if (Program.view != view)
if (worker != workers[view])
{
Program.view = view;
Console.MouseInput = Worker.UseMouse;
worker = workers[view];
Console.MouseInput = worker.UseMouse;
System.Console.Title = $"AITD {new string[] { "vars", "cache", "actors", "objects" }[view]} viewer";
}
}
Expand Down

0 comments on commit 752cdb9

Please sign in to comment.