Skip to content

Commit

Permalink
Switch expressions, more nullability & other improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankvdStam committed Jan 24, 2025
1 parent f3e1ff7 commit f3bfdbc
Show file tree
Hide file tree
Showing 22 changed files with 176 additions and 601 deletions.
52 changes: 26 additions & 26 deletions src/SoulMemory/DarkSouls1/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,41 +66,41 @@ public int MaxUpgrade
{
get
{
switch (Infusion)
return Infusion switch
{
default: throw new NotSupportedException($"Unknown infusion type: {Infusion}");
case ItemInfusion.Normal: return 15;
case ItemInfusion.Chaos: return 5;
case ItemInfusion.Crystal: return 5;
case ItemInfusion.Divine: return 10;
case ItemInfusion.Enchanted: return 5;
case ItemInfusion.Fire: return 10;
case ItemInfusion.Lightning: return 5;
case ItemInfusion.Magic: return 10;
case ItemInfusion.Occult: return 5;
case ItemInfusion.Raw: return 5;
}
ItemInfusion.Normal => 15,
ItemInfusion.Chaos => 5,
ItemInfusion.Crystal => 5,
ItemInfusion.Divine => 10,
ItemInfusion.Enchanted => 5,
ItemInfusion.Fire => 10,
ItemInfusion.Lightning => 5,
ItemInfusion.Magic => 10,
ItemInfusion.Occult => 5,
ItemInfusion.Raw => 5,
_ => throw new NotSupportedException($"Unknown infusion type: {Infusion}")
};
}
}

public bool RestrictUpgrade
{
get
{
switch (Infusion)
return Infusion switch
{
default: throw new NotSupportedException($"Unknown infusion type: {Infusion}");
case ItemInfusion.Normal: return false;
case ItemInfusion.Chaos: return true;
case ItemInfusion.Crystal: return false;
case ItemInfusion.Divine: return false;
case ItemInfusion.Enchanted: return true;
case ItemInfusion.Fire: return false;
case ItemInfusion.Lightning: return false;
case ItemInfusion.Magic: return false;
case ItemInfusion.Occult: return true;
case ItemInfusion.Raw: return true;
}
ItemInfusion.Normal => false,
ItemInfusion.Chaos => true,
ItemInfusion.Crystal => false,
ItemInfusion.Divine => false,
ItemInfusion.Enchanted => true,
ItemInfusion.Fire => false,
ItemInfusion.Lightning => false,
ItemInfusion.Magic => false,
ItemInfusion.Occult => true,
ItemInfusion.Raw => true,
_ => throw new NotSupportedException($"Unknown infusion type: {Infusion}")
};
}
}

Expand Down
42 changes: 9 additions & 33 deletions src/SoulMemory/DarkSouls1/Remastered.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,27 +223,22 @@ public TreeBuilder GetTreeBuilder()
}
#endregion

public int GetAttribute(Attribute attribute) => _playerGameData?.ReadInt32(0x8 + (long)attribute) ?? 0;
public int GetAttribute(Attribute attribute) => _playerGameData.ReadInt32(0x8 + (long)attribute);

public int GetInGameTimeMilliseconds() => _gameDataMan?.ReadInt32(0xa4) ?? 0;
public int GetInGameTimeMilliseconds() => _gameDataMan.ReadInt32(0xa4);

public int NgCount() => _gameDataMan?.ReadInt32(0x78) ?? 0;
public int NgCount() => _gameDataMan.ReadInt32(0x78);

public int GetCurrentSaveSlot() => _gameMan?.ReadInt32(_currentSaveSlotOffset) ?? 0;
public int GetCurrentSaveSlot() => _gameMan.ReadInt32(_currentSaveSlotOffset);

public Vector3f GetPosition() => _playerPos == null ? new Vector3f(0, 0, 0) : new Vector3f(_playerPos.ReadFloat(0x10), _playerPos.ReadFloat(0x14), _playerPos.ReadFloat(0x18));
public Vector3f GetPosition() => new(_playerPos.ReadFloat(0x10), _playerPos.ReadFloat(0x14), _playerPos.ReadFloat(0x18));

public int GetPlayerHealth() => _playerIns?.ReadInt32(0x3e8) ?? 0;
public int GetPlayerHealth() => _playerIns.ReadInt32(0x3e8);

public bool IsPlayerLoaded() => !_playerIns?.IsNullPtr() ?? false;
public bool IsPlayerLoaded() => !_playerIns.IsNullPtr();

public bool IsWarpRequested()
{
if (_gameMan == null)
{
return false;
}

if (GetPlayerHealth() == 0)
{
return false;
Expand All @@ -255,11 +250,6 @@ public bool IsWarpRequested()

public bool AreCreditsRolling()
{
if(_menuMan == null)
{
return false;
}

var first = _menuMan.ReadInt32(0xc8);
var second = _menuMan.ReadInt32(0xd4);
var third = _menuMan.ReadInt32(0x80); //This address seems like it turns into a 1 only when you are on the main menu
Expand All @@ -270,11 +260,6 @@ public bool AreCreditsRolling()

public List<Item> GetInventory()
{
if (_playerGameData == null)
{
return [];
}

//Path: GameDataMan->hostPlayerGameData->equipGameData.equipInventoryData.equipInventoryDataSub
const long equipInventoryDataSubOffset = 0x3b0;

Expand All @@ -294,11 +279,6 @@ public List<Item> GetInventory()

public BonfireState GetBonfireState(Bonfire bonfire)
{
if (_netBonfireDb == null)
{
return BonfireState.Unknown;
}

var element = _netBonfireDb.CreatePointerFromAddress(0x28);
element = element.CreatePointerFromAddress(0x0);
var netBonfireDbItem = element.CreatePointerFromAddress(0x10);
Expand Down Expand Up @@ -402,15 +382,11 @@ public bool ReadEventFlag(uint eventFlagId)
#endregion

//Imported from CapitaineToinon. Thanks!

public void ResetInventoryIndices()
{
if (_inventoryIndices != null)
for (int i = 0; i < 20; i++)
{
for (int i = 0; i < 20; i++)
{
_inventoryIndices.WriteUint32(0x4 * i, uint.MaxValue);
}
_inventoryIndices.WriteUint32(0x4 * i, uint.MaxValue);
}
}

Expand Down
24 changes: 0 additions & 24 deletions src/SoulMemory/DarkSouls2/scholar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ private void ResetPointers()

public Vector3f GetPosition()
{
if (_position == null)
{
return new Vector3f(0, 0, 0);
}

return new Vector3f(
_position.ReadFloat(0x88),
_position.ReadFloat(0x80),
Expand All @@ -95,30 +90,16 @@ public Vector3f GetPosition()

public int GetBossKillCount(BossType bossType)
{
if (_bossCounters == null)
{
return 0;
}
return _bossCounters.ReadInt32((long)bossType);
}

public bool IsLoading()
{
if (_loadState == null)
{
return false;
}

return _loadState.ReadInt32(0x11c) == 1;
}

public int GetAttribute(Attribute attribute)
{
if (_attributes == null)
{
return 0;
}

var offset = _attributeOffsets[attribute];
if (attribute == Attribute.SoulLevel)
{
Expand All @@ -136,11 +117,6 @@ public int GetAttribute(Attribute attribute)

public bool ReadEventFlag(uint eventFlagId)
{
if (_eventFlagManager == null)
{
return false;
}

var eventCategory = eventFlagId / 10000 * 0x89;
var uVar1 = ((eventCategory - eventCategory / 0x1f >> 1) + eventCategory / 0x1f >> 4) * 31;
var r8d = eventCategory - uVar1;
Expand Down
24 changes: 0 additions & 24 deletions src/SoulMemory/DarkSouls2/vanilla.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ private ResultErr<RefreshError> InitPointers()

public Vector3f GetPosition()
{
if (_position == null)
{
return new Vector3f();
}

return new Vector3f(
_position.ReadFloat(0x88),
_position.ReadFloat(0x80),
Expand All @@ -91,30 +86,16 @@ public Vector3f GetPosition()

public int GetBossKillCount(BossType bossType)
{
if (_bossCounters == null)
{
return 0;
}
return _bossCounters.ReadInt32((long)bossType);
}

public bool IsLoading()
{
if (_loadState == null)
{
return false;
}

return _loadState.ReadUInt32(0x1D4) == 1;
}

public int GetAttribute(Attribute attribute)
{
if (_attributes == null)
{
return 0;
}

var offset = _attributeOffsets[attribute];
if (attribute == Attribute.SoulLevel)
{
Expand All @@ -129,11 +110,6 @@ public int GetAttribute(Attribute attribute)

public bool ReadEventFlag(uint eventFlagId)
{
if (_eventFlagManager == null)
{
return false;
}

var eventCategory = eventFlagId / 10000 * 0x89;
var offset = eventCategory % 0x1f * 4 + 0x10;

Expand Down
42 changes: 11 additions & 31 deletions src/SoulMemory/DarkSouls3/DarkSouls3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,12 @@ private ResultErr<RefreshError> InitPointers()
}

//Clear count: 0x78 -> likely subject to the same shift that happens to IGT offset
switch (GetVersion(v))
_igtOffset = GetVersion(v) switch
{
default:
_igtOffset = 0xa4;
break;

case DarkSouls3Version.Earlier:
case DarkSouls3Version.V104:
case DarkSouls3Version.V105:
_igtOffset = 0x9c;
break;

case DarkSouls3Version.Later:
_igtOffset = 0xa4;
break;
}
DarkSouls3Version.Earlier or DarkSouls3Version.V104 or DarkSouls3Version.V105 => 0x9c,
DarkSouls3Version.Later => 0xa4,
_ => 0xa4
};

var treeBuilder = GetTreeBuilder();
return MemoryScanner.TryResolvePointers(treeBuilder, _process);
Expand Down Expand Up @@ -137,23 +127,13 @@ public enum DarkSouls3Version

public static DarkSouls3Version GetVersion(Version v)
{
switch (v.Minor)
return v.Minor switch
{
case 0:
case 1:
case 2:
case 3:
return DarkSouls3Version.Earlier;

case 4:
return DarkSouls3Version.V104;

case 5:
return DarkSouls3Version.V105;

default:
return DarkSouls3Version.Later;
}
0 or 1 or 2 or 3 => DarkSouls3Version.Earlier,
4 => DarkSouls3Version.V104,
5 => DarkSouls3Version.V105,
_ => DarkSouls3Version.Later
};
}

public bool IsLoading()
Expand Down
18 changes: 6 additions & 12 deletions src/SoulMemory/EldenRing/EldenRing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,9 @@ public EldenRingVersion GetVersion(Version v)

public void EnableHud()
{
if (_hud != null)
{
var b = _hud.ReadByte();
b |= 0x1; //Not sure if this whole byte is reserved for the HUD setting. Just going to write a 1 to the first bit and preserve the other bits.
_hud.WriteByte(null, b);
}
var b = _hud.ReadByte();
b |= 0x1; //Not sure if this whole byte is reserved for the HUD setting. Just going to write a 1 to the first bit and preserve the other bits.
_hud.WriteByte(null, b);
}

public Position GetPosition()
Expand All @@ -320,7 +317,7 @@ public bool IsPlayerLoaded()

public ScreenState GetScreenState()
{
var screenState = _menuManImp?.ReadInt32(_screenStateOffset) ?? (int)ScreenState.Unknown;
var screenState = _menuManImp.ReadInt32(_screenStateOffset);
if (screenState.TryParseEnum(out ScreenState s))
{
return s;
Expand Down Expand Up @@ -410,11 +407,8 @@ public List<Item> ReadInventory()
}

var item = Item.FromLookupTable(category, (uint)itemId);
if (item != null)
{
Console.WriteLine($"{item.GroupName} {item.Name}");
items.Add(item);
}
//Console.WriteLine($"{item.GroupName} {item.Name}");

Check warning on line 410 in src/SoulMemory/EldenRing/EldenRing.cs

View workflow job for this annotation

GitHub Actions / build

Remove this commented out code. (https://rules.sonarsource.com/csharp/RSPEC-125)
items.Add(item);
}

return items;
Expand Down
8 changes: 4 additions & 4 deletions src/SoulMemory/MemoryV2/Process/ProcessHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ProcessHook(string name, IProcessWrapper? processWrapper = null) :
/// <returns></returns>
public ResultErr<RefreshError> TryRefresh()
{
var processRefreshResult = ProcessWrapper.TryRefresh(_name, out Exception? e);
var processRefreshResult = ProcessWrapper.TryRefresh(_name, out var e);
switch (processRefreshResult)
{
case ProcessRefreshResult.ProcessNotRunning:
Expand All @@ -64,7 +64,7 @@ public ResultErr<RefreshError> TryRefresh()
{
return pointerScanResult;
}
return Hooked?.Invoke() ?? Result.Ok();
return Hooked.Invoke() ?? Result.Ok();


//Standard refresh
Expand All @@ -73,11 +73,11 @@ public ResultErr<RefreshError> TryRefresh()


case ProcessRefreshResult.Exited:
Exited?.Invoke(null!);
Exited.Invoke(null!);
return Result.Err(new RefreshError(RefreshErrorReason.ProcessExited));

case ProcessRefreshResult.Error:
Exited?.Invoke(e!);
Exited.Invoke(e!);
if (e!.Message == "Access is denied")
{
return Result.Err(new RefreshError(RefreshErrorReason.AccessDenied, "Access is denied. Make sure you disable easy anti cheat and try running livesplit as admin."));
Expand Down
Loading

0 comments on commit f3bfdbc

Please sign in to comment.