Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing EntityKeyValues #615

Open
wants to merge 39 commits into
base: main
Choose a base branch
from

Conversation

KillStr3aK
Copy link
Contributor

Mostly based on #440 by @Yarukon

Implemented CEntityKeyValues, can be used like:

CDynamicProp? prop = Utilities.CreateEntityByName<CDynamicProp>("prop_dynamic");

if (prop != null)
{
    CEntityKeyValues keyValues = new CEntityKeyValues();

    keyValues.SetBool("my_bool", true);
    keyValues.SetEHandle("player_handle", player.EntityHandle);
    keyValues.SetVector("vector", new Vector(15.0f, 250.0f, 300.0f));
        
    prop.DispatchSpawn(keyValues);
}

This is also possible:

CDynamicProp? prop = Utilities.CreateEntityByName<CDynamicProp>("prop_dynamic");

if (prop != null)
{
    CEntityKeyValues keyValues = new CEntityKeyValues();

    keyValues["my_bool", KeyValuesType.TYPE_BOOL] = true;
        
    prop.DispatchSpawn(keyValues);
}

Also implemented some of the math related classes minimally, however: we need #613 in order to prevent memory leaks related to them. (changes needed later on if this one gets merged earlier, or I can make the adjustments on this if 613 is merged.)

Comment on lines +452 to +456
if (type == typeof(Color))
{
var pointer = (IntPtr)GetResult(typeof(IntPtr), ptr);
return Marshaling.ColorMarshaler.NativeToManaged(pointer);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should be there since the beginning I believe haha

Comment on lines +460 to +463
if (type == typeof(CEntityHandle))
{
return new CEntityHandle((uint)GetResult(typeof(uint), ptr));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope this one fits?


case counterstrikesharp::TYPE_COLOR:
{
script_context.SetResult(new Color(keyValues->GetColor(key)));
Copy link
Contributor Author

@KillStr3aK KillStr3aK Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one sadly leaks even if #613 is merged, however could be fixed by using the MemAlloc from #613 to release the ptr either in the ScriptContext or in the ColorMarshaler once the values are converted

Comment on lines +506 to +536
script_context.SetResult(new Vector(keyValues->GetVector(key)));
break;
}

case counterstrikesharp::TYPE_VECTOR2D:
{
script_context.SetResult(new Vector2D(keyValues->GetVector2D(key)));
break;
}

case counterstrikesharp::TYPE_VECTOR4D:
{
script_context.SetResult(new Vector4D(keyValues->GetVector4D(key)));
break;
}

case counterstrikesharp::TYPE_QUATERNION:
{
script_context.SetResult(new Quaternion(keyValues->GetQuaternion(key)));
break;
}

case counterstrikesharp::TYPE_QANGLE:
{
script_context.SetResult(new QAngle(keyValues->GetQAngle(key)));
break;
}

case counterstrikesharp::TYPE_MATRIX3X4:
{
script_context.SetResult(new matrix3x4_t(keyValues->GetMatrix3x4(key)));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should not leak with #613 because the wrapper classes will be DisposableMemory and when its no longer used on the managed side these pointers are released

Comment on lines +48 to +66
Vector2D* Vector2DNew(ScriptContext& script_context)
{
return new Vector2D();
}

Vector4D* Vector4DNew(ScriptContext& script_context)
{
return new Vector4D();
}

matrix3x4_t* Matrix3x4New(ScriptContext& script_context)
{
return new matrix3x4_t();
}

Quaternion* QuaternionNew(ScriptContext& script_context)
{
return new Quaternion();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above!

@KillStr3aK KillStr3aK marked this pull request as ready for review October 8, 2024 06:02
@KillStr3aK KillStr3aK requested a review from roflmuffin as a code owner October 8, 2024 06:02
@KillStr3aK KillStr3aK changed the title [DRAFT] Implementing EntityKeyValues Implementing EntityKeyValues Oct 8, 2024

CEntityKeyValues* EntityKeyValuesNew(ScriptContext& script_context)
{
return new CEntityKeyValues();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgot to mention that this one is also will be released with #613

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant