Skip to content

Commit

Permalink
Refactor savegame detection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianOzelRose committed Mar 30, 2024
1 parent dd6c5b7 commit 2e768e3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
15 changes: 12 additions & 3 deletions TombExtract/ManageSlotsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public partial class ManageSlotsForm : Form
private string savegamePath;

// Offsets
private const int slotStatusOffset = 0x004;
private const int gameModeOffset = 0x008;
private const int saveNumberOffset = 0x00C;
private int levelIndexOffset;
Expand Down Expand Up @@ -162,6 +163,11 @@ private Int32 ReadInt32(int offset)
return (Int32)(byte1 + (byte2 << 8) + (byte3 << 16) + (byte4 << 24));
}

private bool IsSavegamePresent(int savegameOffset)
{
return ReadByte(savegameOffset + slotStatusOffset) != 0;
}

private byte GetLevelIndex(int savegameOffset)
{
return ReadByte(savegameOffset + levelIndexOffset);
Expand Down Expand Up @@ -206,8 +212,9 @@ private void PopulateSavegamesTR1()
int currentSavegameOffset = BASE_SAVEGAME_OFFSET_TR1 + (i * SAVEGAME_ITERATOR);

byte levelIndex = GetLevelIndex(currentSavegameOffset);
bool savegamePresent = IsSavegamePresent(currentSavegameOffset);

if (levelIndex >= 1 && levelIndex <= 19)
if (savegamePresent && levelNamesTR1.ContainsKey(levelIndex))
{
Int32 saveNumber = GetSaveNumber(currentSavegameOffset);
string levelName = levelNamesTR1[levelIndex];
Expand Down Expand Up @@ -248,8 +255,9 @@ private void PopulateSavegamesTR2()
int currentSavegameOffset = BASE_SAVEGAME_OFFSET_TR2 + (i * SAVEGAME_ITERATOR);

byte levelIndex = GetLevelIndex(currentSavegameOffset);
bool savegamePresent = IsSavegamePresent(currentSavegameOffset);

if (levelIndex >= 1 && levelIndex <= 23)
if (savegamePresent && levelNamesTR2.ContainsKey(levelIndex))
{
Int32 saveNumber = GetSaveNumber(currentSavegameOffset);
string levelName = levelNamesTR2[levelIndex];
Expand Down Expand Up @@ -290,8 +298,9 @@ private void PopulateSavegamesTR3()
int currentSavegameOffset = BASE_SAVEGAME_OFFSET_TR3 + (i * SAVEGAME_ITERATOR);

byte levelIndex = GetLevelIndex(currentSavegameOffset);
bool savegamePresent = IsSavegamePresent(currentSavegameOffset);

if (levelIndex >= 1 && levelIndex <= 26)
if (savegamePresent && levelNamesTR3.ContainsKey(levelIndex))
{
Int32 saveNumber = GetSaveNumber(currentSavegameOffset);
string levelName = levelNamesTR3[levelIndex];
Expand Down
16 changes: 13 additions & 3 deletions TombExtract/TR1Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TR1Utilities
private string savegameDestinationPath;

// Offsets
private const int slotStatusOffset = 0x004;
private const int gameModeOffset = 0x008;
private const int saveNumberOffset = 0x00C;
private const int levelIndexOffset = 0x62C;
Expand Down Expand Up @@ -65,6 +66,11 @@ private Int32 ReadInt32(string path, int offset)
return (Int32)(byte1 + (byte2 << 8) + (byte3 << 16) + (byte4 << 24));
}

private bool IsSavegamePresent(string path, int savegameOffset)
{
return ReadByte(path, savegameOffset + slotStatusOffset) != 0;
}

private GameMode GetGameMode(string path, int savegameOffset)
{
int gameMode = ReadByte(path, savegameOffset + gameModeOffset);
Expand Down Expand Up @@ -115,8 +121,9 @@ public void PopulateSourceSavegames(CheckedListBox cklSavegames)
int currentSavegameOffset = BASE_SAVEGAME_OFFSET_TR1 + (i * SAVEGAME_ITERATOR);

byte levelIndex = GetLevelIndex(savegameSourcePath, currentSavegameOffset);
bool savegamePresent = IsSavegamePresent(savegameSourcePath, currentSavegameOffset);

if (levelIndex >= 1 && levelIndex <= 19)
if (savegamePresent && levelNames.ContainsKey(levelIndex))
{
Int32 saveNumber = GetSaveNumber(savegameSourcePath, currentSavegameOffset);
string levelName = levelNames[levelIndex];
Expand Down Expand Up @@ -144,8 +151,9 @@ public void PopulateDestinationSavegames(ListBox lstSavegames)
int currentSavegameOffset = BASE_SAVEGAME_OFFSET_TR1 + (i * SAVEGAME_ITERATOR);

byte levelIndex = GetLevelIndex(savegameDestinationPath, currentSavegameOffset);
bool savegamePresent = IsSavegamePresent(savegameDestinationPath, currentSavegameOffset);

if (levelIndex >= 1 && levelIndex <= 19)
if (savegamePresent && levelNames.ContainsKey(levelIndex))
{
Int32 saveNumber = GetSaveNumber(savegameDestinationPath, currentSavegameOffset);
string levelName = levelNames[levelIndex];
Expand Down Expand Up @@ -173,9 +181,11 @@ public int GetNumOverwrites(List<Savegame> savegames)
for (int i = 0; i < savegames.Count; i++)
{
int currentSavegameOffset = savegames[i].Offset;

byte levelIndex = GetLevelIndex(savegameDestinationPath, currentSavegameOffset);
bool savegamePresent = IsSavegamePresent(savegameDestinationPath, currentSavegameOffset);

if (levelIndex >= 1 && levelIndex <= 19)
if (savegamePresent && levelNames.ContainsKey(levelIndex))
{
numOverwrites++;
}
Expand Down
15 changes: 12 additions & 3 deletions TombExtract/TR2Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TR2Utilities
private string savegameDestinationPath;

// Offsets
private const int slotStatusOffset = 0x004;
private const int gameModeOffset = 0x008;
private const int saveNumberOffset = 0x00C;
private const int levelIndexOffset = 0x628;
Expand Down Expand Up @@ -65,6 +66,11 @@ private Int32 ReadInt32(string path, int offset)
return (Int32)(byte1 + (byte2 << 8) + (byte3 << 16) + (byte4 << 24));
}

private bool IsSavegamePresent(string path, int savegameOffset)
{
return ReadByte(path, savegameOffset + slotStatusOffset) != 0;
}

private GameMode GetGameMode(string path, int savegameOffset)
{
int gameMode = ReadByte(path, savegameOffset + gameModeOffset);
Expand Down Expand Up @@ -119,8 +125,9 @@ public void PopulateSourceSavegames(CheckedListBox cklSavegames)
int currentSavegameOffset = BASE_SAVEGAME_OFFSET_TR2 + (i * SAVEGAME_ITERATOR);

byte levelIndex = GetLevelIndex(savegameSourcePath, currentSavegameOffset);
bool savegamePresent = IsSavegamePresent(savegameSourcePath, currentSavegameOffset);

if (levelIndex >= 1 && levelIndex <= 23)
if (savegamePresent && levelNames.ContainsKey(levelIndex))
{
Int32 saveNumber = GetSaveNumber(savegameSourcePath, currentSavegameOffset);
string levelName = levelNames[levelIndex];
Expand Down Expand Up @@ -148,8 +155,9 @@ public void PopulateDestinationSavegames(ListBox lstSavegames)
int currentSavegameOffset = BASE_SAVEGAME_OFFSET_TR2 + (i * SAVEGAME_ITERATOR);

byte levelIndex = GetLevelIndex(savegameDestinationPath, currentSavegameOffset);
bool savegamePresent = IsSavegamePresent(savegameDestinationPath, currentSavegameOffset);

if (levelIndex >= 1 && levelIndex <= 23)
if (savegamePresent && levelNames.ContainsKey(levelIndex))
{
Int32 saveNumber = GetSaveNumber(savegameDestinationPath, currentSavegameOffset);
string levelName = levelNames[levelIndex];
Expand Down Expand Up @@ -178,8 +186,9 @@ public int GetNumOverwrites(List<Savegame> savegames)
{
int currentSavegameOffset = savegames[i].Offset;
byte levelIndex = GetLevelIndex(savegameDestinationPath, currentSavegameOffset);
bool savegamePresent = IsSavegamePresent(savegameDestinationPath, currentSavegameOffset);

if (levelIndex >= 1 && levelIndex <= 23)
if (savegamePresent && levelNames.ContainsKey(levelIndex))
{
numOverwrites++;
}
Expand Down
15 changes: 12 additions & 3 deletions TombExtract/TR3Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TR3Utilities
private string savegameDestinationPath;

// Offsets
private const int slotStatusOffset = 0x004;
private const int gameModeOffset = 0x008;
private const int saveNumberOffset = 0x00C;
private const int levelIndexOffset = 0x8D6;
Expand Down Expand Up @@ -65,6 +66,11 @@ private Int32 ReadInt32(string path, int offset)
return (Int32)(byte1 + (byte2 << 8) + (byte3 << 16) + (byte4 << 24));
}

private bool IsSavegamePresent(string path, int savegameOffset)
{
return ReadByte(path, savegameOffset + slotStatusOffset) != 0;
}

private GameMode GetGameMode(string path, int savegameOffset)
{
int gameMode = ReadByte(path, savegameOffset + gameModeOffset);
Expand Down Expand Up @@ -122,8 +128,9 @@ public void PopulateSourceSavegames(CheckedListBox cklSavegames)
int currentSavegameOffset = BASE_SAVEGAME_OFFSET_TR3 + (i * SAVEGAME_ITERATOR);

byte levelIndex = GetLevelIndex(savegameSourcePath, currentSavegameOffset);
bool savegamePresent = IsSavegamePresent(savegameSourcePath, currentSavegameOffset);

if (levelIndex >= 1 && levelIndex <= 26)
if (savegamePresent && levelNames.ContainsKey(levelIndex))
{
Int32 saveNumber = GetSaveNumber(savegameSourcePath, currentSavegameOffset);
string levelName = levelNames[levelIndex];
Expand Down Expand Up @@ -151,8 +158,9 @@ public void PopulateDestinationSavegames(ListBox lstSavegames)
int currentSavegameOffset = BASE_SAVEGAME_OFFSET_TR3 + (i * SAVEGAME_ITERATOR);

byte levelIndex = GetLevelIndex(savegameDestinationPath, currentSavegameOffset);
bool savegamePresent = IsSavegamePresent(savegameDestinationPath, currentSavegameOffset);

if (levelIndex >= 1 && levelIndex <= 26)
if (savegamePresent && levelNames.ContainsKey(levelIndex))
{
Int32 saveNumber = GetSaveNumber(savegameDestinationPath, currentSavegameOffset);
string levelName = levelNames[levelIndex];
Expand Down Expand Up @@ -181,8 +189,9 @@ public int GetNumOverwrites(List<Savegame> savegames)
{
int currentSavegameOffset = savegames[i].Offset;
byte levelIndex = GetLevelIndex(savegameDestinationPath, currentSavegameOffset);
bool savegamePresent = IsSavegamePresent(savegameDestinationPath, currentSavegameOffset);

if (levelIndex >= 1 && levelIndex <= 26)
if (savegamePresent && levelNames.ContainsKey(levelIndex))
{
numOverwrites++;
}
Expand Down
Binary file modified TombExtract/bin/x64/Release/TombExtract.exe
Binary file not shown.

0 comments on commit 2e768e3

Please sign in to comment.