Skip to content

Commit

Permalink
feat: add GetSchemaOffset method
Browse files Browse the repository at this point in the history
  • Loading branch information
roflmuffin committed Oct 18, 2023
1 parent 76a78c6 commit ed0fdbb
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions managed/CounterStrikeSharp.API/Core/API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,18 @@ public static IntPtr FindSignature(string modulepath, string signature){
}
}

public static short GetSchemaOffset(string classname, string propname){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(classname);
ScriptContext.GlobalScriptContext.Push(propname);
ScriptContext.GlobalScriptContext.SetIdentifier(0x57B77D8F);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
return (short)ScriptContext.GlobalScriptContext.GetResult(typeof(short));
}
}

public static T GetSchemaValueByName<T>(IntPtr instance, int returntype, string classname, string propname){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
Expand Down
14 changes: 14 additions & 0 deletions managed/CounterStrikeSharp.API/Modules/Memory/Schema.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using CounterStrikeSharp.API.Core;

namespace CounterStrikeSharp.API.Modules.Memory;

public class Schema
{
private static Dictionary<Tuple<string, string>, short> _schemaOffsets = new();
public static short GetSchemaOffset(string className, string propertyName)
{
if (_schemaOffsets.TryGetValue(new Tuple<string, string>(className, propertyName), out var offset))
{
return offset;
}

var foundOffset = NativeAPI.GetSchemaOffset(className, propertyName);
_schemaOffsets.Add(new Tuple<string, string>(className, propertyName), foundOffset);
return foundOffset;
}

public static T GetSchemaValue<T>(IntPtr handle, string className, string propertyName)
{
return NativeAPI.GetSchemaValueByName<T>(handle, (int)typeof(T).ToDataType(), className, propertyName);
Expand Down
12 changes: 12 additions & 0 deletions src/scripting/natives/natives_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@

namespace counterstrikesharp {

int16 GetSchemaOffset(ScriptContext &script_context) {
auto className = script_context.GetArgument<const char *>(0);
auto memberName = script_context.GetArgument<const char *>(1);
auto classKey = hash_32_fnv1a_const(className);
auto memberKey = hash_32_fnv1a_const(memberName);

const auto m_key = schema::GetOffset(className, classKey, memberName, memberKey);

return m_key.offset;
}

void GetSchemaValueByName(ScriptContext &script_context) {
auto instancePointer = script_context.GetArgument<void *>(0);
auto returnType = script_context.GetArgument<DataType_t>(1);
Expand Down Expand Up @@ -200,6 +211,7 @@ void SetSchemaValueByName(ScriptContext &script_context) {
}

REGISTER_NATIVES(schema, {
ScriptEngine::RegisterNativeHandler("GET_SCHEMA_OFFSET", GetSchemaOffset);
ScriptEngine::RegisterNativeHandler("GET_SCHEMA_VALUE_BY_NAME", GetSchemaValueByName);
ScriptEngine::RegisterNativeHandler("SET_SCHEMA_VALUE_BY_NAME", SetSchemaValueByName);
})
Expand Down
1 change: 1 addition & 0 deletions src/scripting/natives/natives_schema.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
GET_SCHEMA_OFFSET: className:string, propName:string -> short
GET_SCHEMA_VALUE_BY_NAME: instance:pointer, returnType:int, className:string, propName:string -> any
SET_SCHEMA_VALUE_BY_NAME: instance:pointer, returnType:int, className:string, propName:string, value:any -> void
3 changes: 3 additions & 0 deletions tooling/CodeGen.Natives/Mapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static string GetPushType(string argType)
case "pointer":
case "bool":
case "double":
case "short":
return "Push(";
case "func":
case "callback":
Expand All @@ -33,6 +34,8 @@ public static string GetCSharpType(string type)
{
switch (type)
{
case "short":
return "short";
case "int":
return "int";
case "uint":
Expand Down

0 comments on commit ed0fdbb

Please sign in to comment.