Skip to content

Commit

Permalink
- Restore the PlaceAnywhere flag to its original value after applying…
Browse files Browse the repository at this point in the history
… a layout, instead of setting it to false
  • Loading branch information
jawslouis committed Nov 3, 2024
1 parent 2619bbd commit 129b53b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
27 changes: 13 additions & 14 deletions MakePlacePlugin/MakePlacePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class MakePlacePlugin : IDalamudPlugin

public static bool CurrentlyPlacingItems = false;

public static bool OriginalPlaceAnywhere = false;

public static bool ApplyChange = false;

public static SaveLayoutManager LayoutManager;
Expand Down Expand Up @@ -187,14 +189,6 @@ public unsafe void RecursivelyPlaceItems()

try
{

if (Memory.Instance.GetCurrentTerritory() == Memory.HousingArea.Outdoors)
{
GetPlotLocation();
}

Memory.Instance.SetPlaceAnywhere(true);

while (ItemsToPlace.Count > 0)
{
var item = ItemsToPlace.First();
Expand All @@ -215,22 +209,19 @@ public unsafe void RecursivelyPlaceItems()
return;
}

if (ItemsToPlace.Count == 0)
{
Log("Finished applying layout");
}

}
catch (Exception e)
{
LogError($"Error: {e.Message}", e.StackTrace);
}

Cleanup();

void Cleanup()
{
Memory.Instance.SetPlaceAnywhere(false);
Memory.Instance.SetPlaceAnywhere(OriginalPlaceAnywhere);
CurrentlyPlacingItems = false;
Log("Finished applying layout");
}
}

Expand Down Expand Up @@ -324,6 +315,14 @@ public void ApplyLayout()
ItemsToPlace.AddRange(placedLast);


if (Memory.Instance.GetCurrentTerritory() == Memory.HousingArea.Outdoors)
{
GetPlotLocation();
}

OriginalPlaceAnywhere = Memory.Instance.GetPlaceAnywhere();
Memory.Instance.SetPlaceAnywhere(true);

RecursivelyPlaceItems();
}

Expand Down
26 changes: 26 additions & 0 deletions MakePlacePlugin/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,32 @@ private static void WriteProtectedBytes(IntPtr addr, byte b)
WriteProtectedBytes(addr, [b]);
}

private static byte ReadProtectedByte(IntPtr addr)
{
byte value = 0;

if (addr == IntPtr.Zero) return value;

VirtualProtect(addr, 1, Protection.PAGE_EXECUTE_READWRITE, out var oldProtection);
value = Marshal.ReadByte(addr);
VirtualProtect(addr, 1, oldProtection, out _);

return value;
}

public bool GetPlaceAnywhere()
{
if (placeAnywhere == IntPtr.Zero)
{
LogError("Could not setup memory for placing anywhere");
return false;
}

var value = ReadProtectedByte(placeAnywhere);
return value != 0;

}

/// <summary>
/// Sets the flag for place anywhere in memory.
/// </summary>
Expand Down

0 comments on commit 129b53b

Please sign in to comment.