Skip to content

Commit

Permalink
[Sonic Frontiers] Player: remove Lua dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperbx committed Nov 10, 2023
1 parent 0789199 commit 6bdea3c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 34 deletions.
23 changes: 13 additions & 10 deletions Source/Sonic Frontiers/Libraries/Lua.hmm
Original file line number Diff line number Diff line change
Expand Up @@ -475,26 +475,29 @@ Library "Lua" by "Hyper & ĐeäTh"
}
}

public long GetState()
public ScriptSequenceManager.ScriptSequence* GetScriptSequence()
{
var pScriptSequenceManager = GameManager.GetService<ScriptSequenceManager>();

if (pScriptSequenceManager == null)
return 0;
return null;

var scriptSequences = pScriptSequenceManager->ScriptSequences;

if (scriptSequences.Length > 0)
{
var pScriptSequence = scriptSequences[0].pData;
if (scriptSequences.Length <= 0)
return null;

if (pScriptSequence == null)
return 0;
return scriptSequences[0].pData;
}

return pScriptSequence->pScripts[0]->pLuaState;
}
public long GetState()
{
var pScriptSequence = GetScriptSequence();

return 0;
if (pScriptSequence == null)
return 0;

return pScriptSequence->pScripts[0]->pLuaState;
}

public (string ScriptName, long pResourceData) GetCurrentScriptResource()
Expand Down
8 changes: 6 additions & 2 deletions Source/Sonic Frontiers/Libraries/MessageManager.hmm
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
Library "MessageManager" by "Sajid"
Library "MessageManager" by "Sajid & Hyper"
{
#lib "Memory"
#lib "HandleManagerBase"

#import "Collections"
#import "GameTypes"
#import "Messages"

using System.Runtime.InteropServices;

Expand All @@ -13,7 +15,9 @@ Library "MessageManager" by "Sajid"
[StructLayout(LayoutKind.Explicit, Size = 0x50)]
public struct Data
{
[FieldOffset(0)] public ReferencedObject Base;
[FieldOffset(0)] public ReferencedObject Base;

[FieldOffset(0x20)] public MoveArray<Memory.Pointer<Message>> Messages;
}

/* 0x1525C0FDF */
Expand Down
40 changes: 36 additions & 4 deletions Source/Sonic Frontiers/Libraries/Messages.hmm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Library "Messages" by "Hyper, Sajid & ĐeäTh"
private static long _sigSendMessageToPlayer = ScanSignature("\x48\x89\x5C\x24\x18\x56\x57\x41\x56\x48\x83\xEC\x20\x48\x8B\x59", "xxxxxxxxxxxxxxxx");
FUNCTION_PTR(long, fpSendMessageToPlayer, _sigSendMessageToPlayer, Player.Data* in_pPlayer, void* in_pMessage)

/* 0x150880130 */
private static long _sigSendMessageToGameManager = ScanSignature("\x48\x89\x5C\x24\x08\x48\x89\x74\x24\x10\x57\x48\x83\xEC\x20\x31\xFF\x48\x89\xD6\x48\x89\xCB\x48\x39\xB9\xB8\x01\x00\x00\x76\x28\x48\x8B\x83\xB0\x01\x00\x00\x48\x8B\x0C\xF8\x48\x85\xC9\x74\x0C\x48\x8B\x01\x49\x89\xF0\x48\x89\xDA\xFF\x50\xCC\x48\xFF\xC7\x48\x3B\xBB\xB8\x01\x00\x00\x72\xD8\x83\xBB\xD0\x02\x00\x00\x00\x48", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx?xxxxxxxxxxxxxxxxxxxx");
FUNCTION_PTR(void, fpSendMessageToGameManager, _sigSendMessageToGameManager, GameManager.Data* in_pGameManager, void* in_pMessage)

/// <summary>
/// A struct representation of `hh::fnd::Message`.
/// </summary>
Expand All @@ -37,17 +41,17 @@ Library "Messages" by "Hyper, Sajid & ĐeäTh"
{
[FieldOffset(0)] public long pVftable;

[FieldOffset(0x08)] public uint ID;
[FieldOffset(0x08)] public int ID;

[FieldOffset(0x0C)] public uint Sender;
[FieldOffset(0x0C)] public int Sender;

[FieldOffset(0x10)] public uint Receiver;
[FieldOffset(0x10)] public int Receiver;

[FieldOffset(0x14)] public bool IsBroadcasted;

[FieldOffset(0x18)] public uint Mask = 0xFFFFFFFF;

public Message(uint in_id)
public Message(int in_id)
{
ID = in_id;
}
Expand All @@ -63,6 +67,24 @@ Library "Messages" by "Hyper, Sajid & ĐeäTh"
[StructLayout(LayoutKind.Explicit, Size = 0x40)]
public struct MessageQueue { }

/// <summary>
/// A struct representation of `app::game::MsgChangePlayerCharacter`.
/// </summary>
[StructLayout(LayoutKind.Explicit, Size = 0x50)]
public struct MsgChangePlayerCharacter
{
[FieldOffset(0)] public Message Base = new Message(0x20C8);

[FieldOffset(0x20)] public Player.PlayerType PlayerType;

public MsgChangePlayerCharacter() { }

public MsgChangePlayerCharacter(Player.PlayerType in_playerType) : this()
{
PlayerType = in_playerType;
}
}

/// <summary>
/// A struct representation of `app::game::MsgResumeWorld`.
/// </summary>
Expand Down Expand Up @@ -220,6 +242,16 @@ Library "Messages" by "Hyper, Sajid & ĐeäTh"
GET_FUNCTION_PTR(fpSendMessageToGameService)(in_pGameService, in_pGameService->pGameManager, &in_message);
}

public void SendMessageToGameManager<T>(T in_message) where T : unmanaged
{
var pGameManager = GameManager.Get();

if (pGameManager == null)
return;

GET_FUNCTION_PTR(fpSendMessageToGameManager)(pGameManager, &in_message);
}

public void SendMessageToPlayer<T>(T in_message) where T : unmanaged
{
var pPlayer = Player.GetPlayerData();
Expand Down
40 changes: 23 additions & 17 deletions Source/Sonic Frontiers/Libraries/Player.hmm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Library "Player" by "Hyper, NM, Ahremic, Skyth & Sajid"
#lib "Collections"
#lib "GameManager"
#lib "GameObject"
#lib "Lua"
#lib "MessageManager"
#lib "Reflection"
#lib "StringMapOperation"
Expand All @@ -26,6 +25,7 @@ Library "Player" by "Hyper, NM, Ahremic, Skyth & Sajid"
#lib "TailsParameters"

#import "GOComponents"
#import "Messages"
#import "Plugins"
#import "Postures"
#import "Services"
Expand Down Expand Up @@ -101,20 +101,6 @@ Library "Player" by "Hyper, NM, Ahremic, Skyth & Sajid"
{
if (_isInitialised)
return;

// Allow character switching outside of Another Story.
Lua.CreateLineHook
(
@"""sonic"", ""tails"", ""knuckles"", ""amy"", ",

"script/resident/stagedata.lua",

@"attrs\s*=\s*\{",

HookBehavior.After,

true
);

// Always construct app::player::AmyInfo.
WriteNop
Expand Down Expand Up @@ -188,6 +174,26 @@ Library "Player" by "Hyper, NM, Ahremic, Skyth & Sajid"
_isInitialised = true;
}

[LibraryUpdate]
public void Update()
{
var pLevelInfo = GameManager.GetService<LevelInfo>();

if (pLevelInfo == null)
return;

var pStageData = pLevelInfo->pStageData;

if (pStageData == null)
return;

// Update current stage attributes to include other characters.
pStageData->AttributeFlags |= (1 << (int)StageInfo.Attributes.Sonic);
pStageData->AttributeFlags |= (1 << (int)StageInfo.Attributes.Amy);
pStageData->AttributeFlags |= (1 << (int)StageInfo.Attributes.Knuckles);
pStageData->AttributeFlags |= (1 << (int)StageInfo.Attributes.Tails);
}

public PlayerType GetPlayerType()
{
var pLevelInfo = GameManager.GetService<LevelInfo>();
Expand Down Expand Up @@ -223,10 +229,10 @@ Library "Player" by "Hyper, NM, Ahremic, Skyth & Sajid"
if (in_playerType == PlayerType.Unknown)
return;

Lua.Call("ChangePlayerCharacter", in_playerType.ToString());
Messages.SendMessageToGameManager(new MsgChangePlayerCharacter(in_playerType));
}

public enum PlayerType
public enum PlayerType : sbyte
{
Unknown = -1,
Sonic,
Expand Down
2 changes: 1 addition & 1 deletion Source/Sonic Frontiers/Libraries/Services.hmm
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ Library "Services" by "Hyper"
/// <summary>
/// An instance of `hh::game::GameService`, the base class for `app::game::TimeService`.
/// </summary>
[FieldOffset(0)] public GameService.Data GameService;
[FieldOffset(0)] public GameService.Data GameService;

[FieldOffset(0x88)] public Time CurrentTime;

Expand Down

0 comments on commit 6bdea3c

Please sign in to comment.