Skip to content

Commit

Permalink
Add GetGameframeTime to NativeAPI (#627)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Wilson <[email protected]>
  • Loading branch information
Interesting-exe and roflmuffin authored Oct 17, 2024
1 parent c2f212d commit 71ae253
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
10 changes: 10 additions & 0 deletions managed/CounterStrikeSharp.API/Core/API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ public static int GetMaxClients(){
}
}

public static float GetGameFrameTime(){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.SetIdentifier(0x97E331CA);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
return (float)ScriptContext.GlobalScriptContext.GetResult(typeof(float));
}
}

public static void IssueServerCommand(string command){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
Expand Down
23 changes: 14 additions & 9 deletions managed/CounterStrikeSharp.API/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,33 @@ public class Server
/// </summary>
/// <remarks>Does not increment when server is hibernating</remarks>
public static double TickedTime => NativeAPI.GetTickedTime();

/// <summary>
/// Returns the current map time in seconds, as an interval of the server's tick interval.
/// e.g. 70.046875 would represent 70 seconds of map time and the 4483rd tick of the server (70.046875 / 0.015625).
/// </summary>
/// <remarks>Increments even when server is hibernating</remarks>
public static float CurrentTime => NativeAPI.GetCurrentTime();

/// <summary>
/// Returns the current map tick count.
/// CS2 is a 64 tick server, so the value will increment by 64 every second.
/// </summary>
public static int TickCount => NativeAPI.GetTickCount();

/// <summary>
/// Returns the total time the server has been running in seconds.
/// </summary>
/// <remarks>Increments even when server is hibernating</remarks>
public static double EngineTime => NativeAPI.GetEngineTime();


/// <summary>
/// Returns the time spent on last server or client frame
/// </summary>
public static float FrameTime => NativeAPI.GetGameFrameTime();

public static void PrecacheModel(string name) => NativeAPI.PrecacheModel(name);

/// <summary>
/// <inheritdoc cref="RunOnTick"/>
/// Returns Task that completes once the synchronous task has been completed.
Expand All @@ -78,7 +83,7 @@ public static Task RunOnTickAsync(int tick, Action task)
NativeAPI.QueueTaskForFrame(tick, functionReference);
return functionReference.CompletionTask;
}

/// <summary>
/// Queue a task to be executed on the specified tick.
/// See <see cref="TickCount"/> to retrieve the current tick.
Expand Down Expand Up @@ -108,7 +113,7 @@ public static void NextFrame(Action task)
{
NextFrameAsync(task);
}

/// <summary>
/// <inheritdoc cref="NextWorldUpdate"/>
/// Returns Task that completes once the synchronous task has been completed.
Expand All @@ -119,7 +124,7 @@ public static Task NextWorldUpdateAsync(Action task)
NativeAPI.QueueTaskForNextWorldUpdate(functionReference);
return functionReference.CompletionTask;
}

/// <summary>
/// Queue a task to be executed on the next pre world update.
/// <remarks>Executes if the server is hibernating.</remarks>
Expand Down Expand Up @@ -157,4 +162,4 @@ public static string[] GetMapList()

public static void PrintToConsole(string s) => NativeAPI.PrintToServerConsole($"{s}\n\0");
}
}
}
2 changes: 1 addition & 1 deletion src/scripting/natives/natives_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ REGISTER_NATIVES(engine, {
ScriptEngine::RegisterNativeHandler("GET_TICK_INTERVAL", GetTickInterval);
ScriptEngine::RegisterNativeHandler("GET_TICK_COUNT", GetTickCount);
ScriptEngine::RegisterNativeHandler("GET_CURRENT_TIME", GetCurrentTime);
ScriptEngine::RegisterNativeHandler("GET_GAMEFRAME_TIME", GetGameFrameTime);
ScriptEngine::RegisterNativeHandler("GET_GAME_FRAME_TIME", GetGameFrameTime);
ScriptEngine::RegisterNativeHandler("GET_ENGINE_TIME", GetEngineTime);
ScriptEngine::RegisterNativeHandler("GET_MAX_CLIENTS", GetMaxClients);
ScriptEngine::RegisterNativeHandler("ISSUE_SERVER_COMMAND", ServerCommand);
Expand Down
1 change: 1 addition & 0 deletions src/scripting/natives/natives_engine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ GET_CURRENT_TIME: -> float
GET_TICK_COUNT: -> int
GET_ENGINE_TIME: -> double
GET_MAX_CLIENTS: -> int
GET_GAME_FRAME_TIME: -> float
ISSUE_SERVER_COMMAND: command:string -> void
PRECACHE_MODEL: name:string -> void
PRECACHE_SOUND: name:string, preload:bool -> bool
Expand Down

0 comments on commit 71ae253

Please sign in to comment.