Skip to content

Commit

Permalink
Merge branch 'mr-155'
Browse files Browse the repository at this point in the history
  • Loading branch information
nihonium-cfx committed Dec 6, 2023
2 parents 6f0f05b + 5031bb7 commit ca429f2
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 29 deletions.
Expand Up @@ -140,7 +140,7 @@ static InitFunction initFunction([] ()
if (FX_SUCCEEDED(fx::GetCurrentScriptRuntime(&runtime)))
{
fx::Resource* resource = reinterpret_cast<fx::Resource*>(runtime->GetParentObject());
int overlayIdx = sf::AddMinimapOverlay(resource->GetPath() + "/" + context.CheckArgument<const char*>(0));
int overlayIdx = sf::AddMinimapOverlay(resource->GetPath() + "/" + context.CheckArgument<const char*>(0), -1);

resource->OnStop.Connect([=]()
{
Expand All @@ -154,6 +154,31 @@ static InitFunction initFunction([] ()
context.SetResult(-1);
});

fx::ScriptEngine::RegisterNativeHandler("ADD_MINIMAP_OVERLAY_WITH_DEPTH", [](fx::ScriptContext& context)
{
fx::OMPtr<IScriptRuntime> runtime;

if (FX_FAILED(fx::GetCurrentScriptRuntime(&runtime)))
{
context.SetResult(-1);
return;
}

fx::Resource* resource = reinterpret_cast<fx::Resource*>(runtime->GetParentObject());

auto gfxFileName = context.CheckArgument<const char*>(0);
auto depth = context.GetArgument<int>(1);

int overlayIdx = sf::AddMinimapOverlay(resource->GetPath() + "/" + gfxFileName, depth);

resource->OnStop.Connect([=]()
{
sf::RemoveMinimapOverlay(overlayIdx);
});

context.SetResult(overlayIdx);
});

fx::ScriptEngine::RegisterNativeHandler("HAS_MINIMAP_OVERLAY_LOADED", [](fx::ScriptContext& context)
{
context.SetResult(sf::HasMinimapLoaded(context.GetArgument<int>(0)));
Expand Down
2 changes: 1 addition & 1 deletion code/components/gta-streaming-five/include/sfFontStuff.h
Expand Up @@ -14,7 +14,7 @@ namespace sf

void GTA_STREAMING_EXPORT RegisterFontLib(const std::string& swfName);

int GTA_STREAMING_EXPORT AddMinimapOverlay(const std::string& swfName);
int GTA_STREAMING_EXPORT AddMinimapOverlay(const std::string& swfName, int depth);

void GTA_STREAMING_EXPORT RemoveMinimapOverlay(int swfId);

Expand Down
68 changes: 41 additions & 27 deletions code/components/gta-streaming-five/src/ScaleformHacks.cpp
Expand Up @@ -15,7 +15,7 @@ struct GFxObjectInterface

struct GFxValue;

static hook::cdecl_stub<bool(GFxObjectInterface*, void*, GFxValue*, const char*)> __GFxObjectInterface_CreateEmptyMovieClip([]()
static hook::cdecl_stub<bool(GFxObjectInterface*, void*, GFxValue*, const char*, int)> __GFxObjectInterface_CreateEmptyMovieClip([]()
{
return hook::get_pattern("4D 8B E0 4C 8B F9 48 85 DB 75 18 48", -0x25);
});
Expand Down Expand Up @@ -156,9 +156,15 @@ struct GFxValue
pObjectInterface = nullptr;
}

inline bool CreateEmptyMovieClip(GFxValue* movieClip, const char* instanceName)
inline bool CreateEmptyMovieClip(GFxValue* movieClip, const char* instanceName, int depth)
{
return __GFxObjectInterface_CreateEmptyMovieClip(pObjectInterface, mValue.pData, movieClip, instanceName);
// CreateEmptyMovieClip will fail if depth >0x7EFFFFFD
if (depth > 0x7EFFFFFD)
{
depth = 0x7EFFFFFD;
}

return __GFxObjectInterface_CreateEmptyMovieClip(pObjectInterface, mValue.pData, movieClip, instanceName, depth);
}

inline bool GetDisplayInfo(GFxDisplayInfo* info)
Expand Down Expand Up @@ -319,7 +325,7 @@ static void SetupTerritories()

overlayRootClip = {};

g_foregroundOverlay3D->CreateEmptyMovieClip(&overlayRootClip, "asTestClip3D");
g_foregroundOverlay3D->CreateEmptyMovieClip(&overlayRootClip, "asTestClip3D", -1);

auto movie = _getScaleformMovie(*g_gfxId);

Expand All @@ -331,9 +337,15 @@ static void SetupTerritories()
movie->SetVariable("TIMELINE.OVERLAY_METHOD", overlayMethodValue, 1);
}

static std::map<std::string, int> g_swfLoadQueue;
static std::set<int> g_swfRemoveQueue;
static int g_swfId;
struct MinimapOverlayLoadRequest
{
int sfwId;
int depth;
};

static std::map<std::string, MinimapOverlayLoadRequest> g_minimapOverlayLoadQueue;
static std::set<int> g_minimapOverlayRemoveQueue;
static int g_minimapOverlaySwfId;

static hook::cdecl_stub<void(const char*, bool)> _gfxPushString([]()
{
Expand All @@ -352,17 +364,18 @@ static hook::cdecl_stub<bool(uint32_t, int, const char*, int, int)> _setupGfxCal

namespace sf
{
int AddMinimapOverlay(const std::string& swfName)
int AddMinimapOverlay(const std::string& swfName, int depth)
{
auto id = ++g_swfId;
g_swfLoadQueue.insert({ swfName, id });
auto id = ++g_minimapOverlaySwfId;

g_minimapOverlayLoadQueue.insert({ swfName, { id, depth } });

return id;
}

void RemoveMinimapOverlay(int swfId)
{
g_swfRemoveQueue.insert(swfId);
g_minimapOverlayRemoveQueue.insert(swfId);
}

bool HasMinimapLoaded(int swfId)
Expand Down Expand Up @@ -445,41 +458,42 @@ static HookFunction hookFunction([]()
{
OnMainGameFrame.Connect([]()
{
std::set<std::string> toRemove;
std::set<std::string> toRemoveFromMinimapOverlayLoadQueue;

auto cstreaming = streaming::Manager::GetInstance();

for (const auto& swf : g_swfLoadQueue)
for (const auto& [gfxFileName, request] : g_minimapOverlayLoadQueue)
{
auto val = std::make_shared<GFxValue>();
overlayRootClip.CreateEmptyMovieClip(val.get(), va("id%d", swf.second));
auto swf = std::make_shared<GFxValue>();

auto instanceName = va("id%d", request.sfwId);

GFxValue rv;
overlayRootClip.CreateEmptyMovieClip(swf.get(), instanceName, request.depth);

GFxValue args(swf.first.c_str());
val->Invoke("loadMovie", &rv, &args, 1);
GFxValue result;
GFxValue args(gfxFileName.c_str());

g_overlayClips[swf.second] = val;
swf->Invoke("loadMovie", &result, &args, 1);

toRemove.insert(swf.first);
g_overlayClips[request.sfwId] = swf;

toRemoveFromMinimapOverlayLoadQueue.insert(gfxFileName);
}

for (const auto& swf : toRemove)
for (const auto& gfxFileName : toRemoveFromMinimapOverlayLoadQueue)
{
g_swfLoadQueue.erase(swf);
g_minimapOverlayLoadQueue.erase(gfxFileName);
}

for (int swfKey : g_swfRemoveQueue)
for (int swfId : g_minimapOverlayRemoveQueue)
{
auto swf = g_overlayClips[swfKey];

if (swf)
if (auto swf = g_overlayClips[swfId]; swf)
{
swf->Invoke("removeMovieClip", nullptr, nullptr, 0);
}
}

g_swfRemoveQueue.clear();
g_minimapOverlayRemoveQueue.clear();
});

{
Expand Down
2 changes: 2 additions & 0 deletions ext/native-decls/AddMinimapOverlay.md
Expand Up @@ -10,6 +10,8 @@ int ADD_MINIMAP_OVERLAY(char* name);
Loads a minimap overlay from a GFx file in the current resource.
If you need to control the depth of overlay use [`ADD_MINIMAP_OVERLAY_WITH_DEPTH`](#_0xED0935B5).
## Parameters
* **name**: The path to a `.gfx` file in the current resource. It has to be specified as a `file`.
Expand Down
18 changes: 18 additions & 0 deletions ext/native-decls/AddMinimapOverlayWithDepth.md
@@ -0,0 +1,18 @@
---
ns: CFX
apiset: client
---
## ADD_MINIMAP_OVERLAY_WITH_DEPTH

```c
int ADD_MINIMAP_OVERLAY_WITH_DEPTH(char* name, int depth);
```
Loads a minimap overlay from a GFx file in the current resource.
## Parameters
* **name**: The path to a `.gfx` file in the current resource. It has to be specified as a `file`.
* **depth**: The depth of new overlay on the minimap. Pass `-1` for game to figure out the highest depth itself. Should not be greater than `0x7EFFFFFD`.
## Return value
A minimap overlay ID.

0 comments on commit ca429f2

Please sign in to comment.