add game stats helper

This commit is contained in:
Emmanuel Hansen 2023-07-02 22:12:47 +00:00
parent c8d6f786c5
commit c5bcdc06f5
2 changed files with 59 additions and 0 deletions

View File

@ -94,6 +94,43 @@ namespace LibRyujinx
return InitializeDevice(isHostMapped); return InitializeDevice(isHostMapped);
} }
[UnmanagedCallersOnly(EntryPoint = "Java_org_ryujinx_android_RyujinxNative_deviceGetGameStats")]
public static JObjectLocalRef JniGetGameStats(JEnvRef jEnv, JObjectLocalRef jObj)
{
var stats = GetGameStats();
var javaClassName = GetCCharSequence("org/ryujinx/android/viewmodels/GameStats");
JEnvValue value = jEnv.Environment;
ref JNativeInterface jInterface = ref value.Functions;
IntPtr findClassPtr = jInterface.FindClassPointer;
IntPtr newGlobalRefPtr = jInterface.NewGlobalRefPointer;
IntPtr getFieldIdPtr = jInterface.GetFieldIdPointer;
IntPtr getMethodPtr = jInterface.GetMethodIdPointer;
IntPtr newObjectPtr = jInterface.NewObjectPointer;
IntPtr setDoubleFieldPtr = jInterface.SetDoubleFieldPointer;
var findClass = findClassPtr.GetUnsafeDelegate<FindClassDelegate>();
var newGlobalRef = newGlobalRefPtr.GetUnsafeDelegate<NewGlobalRefDelegate>();
var getFieldId = getFieldIdPtr.GetUnsafeDelegate<GetFieldIdDelegate>();
var getMethod = getMethodPtr.GetUnsafeDelegate<GetMethodIdDelegate>();
var newObject = newObjectPtr.GetUnsafeDelegate<NewObjectDelegate>();
var setDoubleField = setDoubleFieldPtr.GetUnsafeDelegate<SetDoubleFieldDelegate>();
var javaClass = findClass(jEnv, javaClassName);
var newGlobal = newGlobalRef(jEnv, javaClass._value);
var constructor = getMethod(jEnv, javaClass, GetCCharSequence("<init>"), GetCCharSequence("()V"));
var newObj = newObject(jEnv, javaClass, constructor, 0);
setDoubleField(jEnv, newObj, getFieldId(jEnv, javaClass, GetCCharSequence("Fifo"), GetCCharSequence("D")), stats.Fifo);
setDoubleField(jEnv, newObj, getFieldId(jEnv, javaClass, GetCCharSequence("GameFps"), GetCCharSequence("D")), stats.GameFps);
setDoubleField(jEnv, newObj, getFieldId(jEnv, javaClass, GetCCharSequence("GameTime"), GetCCharSequence("D")), stats.GameTime);
return newObj;
}
[UnmanagedCallersOnly(EntryPoint = "Java_org_ryujinx_android_RyujinxNative_deviceLoad")] [UnmanagedCallersOnly(EntryPoint = "Java_org_ryujinx_android_RyujinxNative_deviceLoad")]
public static JBoolean JniLoadApplicationNative(JEnvRef jEnv, JObjectLocalRef jObj, JStringLocalRef pathPtr) public static JBoolean JniLoadApplicationNative(JEnvRef jEnv, JObjectLocalRef jObj, JStringLocalRef pathPtr)
{ {

View File

@ -100,6 +100,21 @@ namespace LibRyujinx
AudioDriver = new SDL2HardwareDeviceDriver(); AudioDriver = new SDL2HardwareDeviceDriver();
} }
public static GameStats GetGameStats()
{
if (SwitchDevice?.EmulationContext == null)
return new GameStats();
var context = SwitchDevice.EmulationContext;
return new GameStats()
{
Fifo = context.Statistics.GetFifoPercent(),
GameFps = context.Statistics.GetGameFrameRate(),
GameTime = context.Statistics.GetGameFrameTime()
};
}
public static GameInfo GetGameInfo(Stream gameStream, bool isXci) public static GameInfo GetGameInfo(Stream gameStream, bool isXci)
{ {
var gameInfo = new GameInfo(); var gameInfo = new GameInfo();
@ -579,4 +594,11 @@ namespace LibRyujinx
public string Version; public string Version;
public byte[] Icon; public byte[] Icon;
} }
public class GameStats
{
public double Fifo;
public double GameFps;
public double GameTime;
}
} }