AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   Dynamic Objects and Properties - v.0.0.32 - [2018.05.08] (https://forums.alliedmods.net/showthread.php?t=270519)

Neuro Toxin 08-22-2015 20:34

Dynamic Objects and Properties - v.0.0.32 - [2018.05.08]
 
Dynamic Wiki | Download | Examples

Dynamic allows developers to use truly dynamic data structures in SourcePawn.
This means you can define a Methodmap and use Dynamic to back it's member data at run-time.
All dynamic members are typed which allows automatic type conversion between base types.

You can then extend and inherit Dynamic Methodmaps to build complex namespaces.
Namespaces simplify the sharing of complex structures which third party plugins can easily access with a minimal amount of code.


Features
- Fully methodmap based to try and give an Object Orientated feel
- Inherit Dynamic to make your own simple methodmaps
- Easily name and retrieve dynamic objects between multiple plugins (no natives required)!!!!
- Supports String, int, float, bool, Handle, Vector and Dynamic types
- Automatic type conversion!!
- Static objects for Server and Player Settings
- Hook member changes
- Member iteration / looping
- Member sorting
- Load and Save flat configs
- Load and Save KeyValue configs


Dynamic Methodmaps
- Extend Dynamic to create your own Dynamic methodmaps
- A Class Builder to quickly generate basic Dynamic methodmaps
- You dont have to muck around with Dynamic Member Offsets to improve performance
- All of the same features that come with Dynamic Objects

Planned Changes
- Add serialisation support using mysql

Download (v0.0.32)
Example Use + Features
Example Class Use Example
Class Example
Class Builder
Include File

SM9 08-23-2015 07:03

Re: Dynamic Objects and Properties
 
Very cool, Will save me a lot of time instead of using natives all the time :P

Neuro Toxin 08-26-2015 23:38

Re: Dynamic Objects and Properties
 
I've done some profiling of the current version attached to the OP.

Here is the profiling code:

Spoiler
The outputs are below for 3 runs:

Code:

[SM] Starting Benchmark Tests...
Created 1000 dynamic object(s) in 0.003906 second(s)
Created 99000 dynamic member(s) in 1.492187 second(s)
Read 99000 dynamic member(s) in 0.527343 second(s)
Verified 99000 dynamic member(s) in 2.871093 second(s)
Updated 99000 dynamic member(s) in 0.542968 second(s)
Disposed 1000 dynamic object(s) in 0.011718 second(s)

[SM] Starting Benchmark Tests...
Created 1000 dynamic object(s) in 0.003906 second(s)
Created 99000 dynamic member(s) in 1.460937 second(s)
Read 99000 dynamic member(s) in 0.531250 second(s)
Verified 99000 dynamic member(s) in 2.839843 second(s)
Updated 99000 dynamic member(s) in 0.546875 second(s)
Disposed 1000 dynamic object(s) in 0.011718 second(s)

[SM] Starting Benchmark Tests...
Created 1000 dynamic object(s) in 0.000000 second(s)
Created 99000 dynamic member(s) in 1.492187 second(s)
Read 99000 dynamic member(s) in 0.527343 second(s)
Verified 99000 dynamic member(s) in 2.863281 second(s)
Updated 99000 dynamic member(s) in 0.546875 second(s)
Disposed 1000 dynamic object(s) in 0.011718 second(s)


Neuro Toxin 08-26-2015 23:57

Re: Dynamic Objects and Properties
 
1 Attachment(s)
I've made a second version.

The main idea was to try and speed up Create/Update member operations.

The internal data for each object is one array, rather than 3 arrays (one for each type (int, float, string)).

The member name is also contained in the data, where as the first version has a lookup array that points to the members position in the data array (1 per type again).

Code:

[SM] Starting Benchmark Tests...
Created 1000 dynamic object(s) in 0.000000 second(s)
Created 99000 dynamic member(s) in 2.285156 second(s)
CRASHED FOR TAKING TO LONG LOL

The benchmarks are considerably slower in this version. I think this is because the lookups per type in the first version are not looping the whole member set on each request.

I also believe lots of the internal functions are passing around arguments that could be static.

Version 002 is attached to this post.

If anyone has any idea's or suggestions on how to go about making this as optimised as possible, I'm all ears!

zipcore 08-27-2015 23:00

Re: Dynamic Objects and Properties
 
Cool stuff

headline 08-28-2015 00:06

Re: Dynamic Objects and Properties
 
Quote:

Originally Posted by zipcore (Post 2337583)
cool stuff

he is back boys

Neuro Toxin 08-28-2015 00:12

Re: Dynamic Objects and Properties
 
Quickly. Get him before he leaves lol

Edit: on topic. I done another version which is heaps faster!

Neuro Toxin 08-28-2015 04:39

Re: Dynamic Objects and Properties
 
1 Attachment(s)
Version 003.

I'm using a Trie for member lookups and arrays for data storage. It's up to 10 times faster.

The Trie holds the offset for where the members data is held in the internal array.

Code:

[SM] Starting Benchmark Tests...
Created 1000 dynamic object(s) in 0.000000 second(s)
Created 99000 dynamic member(s) in 0.164062 second(s)
Read 99000 dynamic member(s) in 0.113281 second(s)
Verified 99000 dynamic member(s) in 0.277343 second(s)
Updated 99000 dynamic member(s) in 0.125000 second(s)
Disposed 1000 dynamic object(s) in 0.007812 second(s)

[SM] Starting Benchmark Tests...
Created 1000 dynamic object(s) in 0.000000 second(s)
Created 99000 dynamic member(s) in 0.164062 second(s)
Read 99000 dynamic member(s) in 0.109375 second(s)
Verified 99000 dynamic member(s) in 0.265625 second(s)
Updated 99000 dynamic member(s) in 0.128906 second(s)
Disposed 1000 dynamic object(s) in 0.007812 second(s)

[SM] Starting Benchmark Tests...
Created 1000 dynamic object(s) in 0.000000 second(s)
Created 99000 dynamic member(s) in 0.164062 second(s)
Read 99000 dynamic member(s) in 0.109375 second(s)
Verified 99000 dynamic member(s) in 0.273437 second(s)
Updated 99000 dynamic member(s) in 0.125000 second(s)
Disposed 1000 dynamic object(s) in 0.011718 second(s)


aeleos 09-03-2015 00:52

Re: Dynamic Objects and Properties
 
Wow thank you for this, this is exactly what I have been needing for my plugin. I am correct in saying it works just like any other data type in that I can have an array of them like
Code:

Dynamic object[32]

Neuro Toxin 09-03-2015 01:44

Re: Dynamic Objects and Properties
 
You sure can :)

Edit: remember to initialise them as per the first post

zipcore 09-06-2015 10:16

Re: Dynamic Objects and Properties
 
Quote:

Originally Posted by Neuro Toxin (Post 2337629)
It's up to 10 times faster.[/CODE]

Using a trie was a very good idea

headline 09-07-2015 04:18

Re: Dynamic Objects and Properties
 
Okay, so I get that you can store integers, floats, and strings in this thing. For someone who's first "language" is SourcePawn: When and Why should I use this?

Neuro Toxin 09-07-2015 05:14

Re: Dynamic Objects and Properties
 
Hi headline :)

Great question.

The main purpose of use is when you require irregular data sets to be stored and shared between plugins. An example maybe storing times for a timer plugin. You would not be required to store arrays for level / player times and could simply use a dynamic object for storage.

You could also hook member data changes which will call forwards to each plugin that has hooked the dynamic object. With this your plugins can be aware of changes in real time without constantly checking for updated data via natives or global forwards.

You wouldn't use dynamic objects for plain arrays that dont need to be shared via other plugins.

If you've ever had a moment where you wish you could make your own netprop. This is a viable solution that would allow such functionality without the need for you to write natives.

I'm personally using this to create member sets for each player. Where one plugin can store members like a playerid and others can read them easily and or hook member changes. I also hook changes and use them to update a mysql databases to synchronise changes automatically to and from plugins with no effort at all.

I have some planned changes where <dynamic>(1-maxplayers) will always relate to data for the corresponding client index that auto initialises and disposes with client connect and disconnect events.

DabuDos 09-14-2015 12:35

Re: Dynamic Objects and Properties
 
Got something weird.

PHP Code:

L 09/14/2015 18:11:29SourceMod error session started
L 09
/14/2015 18:11:29Info (map "de_dust2") (file "errors_20150914.log")
L 09/14/2015 18:11:29: [SMNative "ThrowNativeError" reportedUnable to access dynamic handle 0
L 09
/14/2015 18:11:29: [SMDisplaying call stack trace for plugin "dynamic.smx":
L 09/14/2015 18:11:29: [SM]   [0]  Line 149E:\CSGO\Sourcemod\tracker\addons\sourcemod\scripting\dynamic.sp::Native_Dynamic_IsValidCollectionIndex()
L 09/14/2015 18:11:29: [SMNative "Dynamic_IsValidCollectionIndex" reportedError encountered while processing a dynamic native
L 09
/14/2015 18:11:29: [SMDisplaying call stack trace for plugin "dynamic.smx":
L 09/14/2015 18:11:29: [SM]   [0]  Line 401E:\CSGO\Sourcemod\tracker\addons\sourcemod\scripting\dynamic.sp::Native_Dynamic_SetInt()
L 09/14/2015 18:11:29: [SMNative "Dynamic_SetInt" reportedError encountered while processing a dynamic native
L 09
/14/2015 18:11:29: [SMDisplaying call stack trace for plugin "DynamicTest1.smx":
L 09/14/2015 18:11:29: [SM]   [0]  Line 73D:\SourcemodPlugins\include\dynamic.inc::Dynamic.SetInt()
L 09/14/2015 18:11:29: [SM]   [1]  Line 9D:\SourcemodPlugins\DynamicTest1.sp::OnPluginStart()
L 09/14/2015 18:13:08: [SMNative "ThrowNativeError" reportedUnable to access dynamic handle 0
L 09
/14/2015 18:13:08: [SMDisplaying call stack trace for plugin "dynamic.smx":
L 09/14/2015 18:13:08: [SM]   [0]  Line 149E:\CSGO\Sourcemod\tracker\addons\sourcemod\scripting\dynamic.sp::Native_Dynamic_IsValidCollectionIndex()
L 09/14/2015 18:13:08: [SMNative "Dynamic_IsValidCollectionIndex" reportedError encountered while processing a dynamic native
L 09
/14/2015 18:13:08: [SMDisplaying call stack trace for plugin "dynamic.smx":
L 09/14/2015 18:13:08: [SM]   [0]  Line 401E:\CSGO\Sourcemod\tracker\addons\sourcemod\scripting\dynamic.sp::Native_Dynamic_SetInt()
L 09/14/2015 18:13:08: [SMNative "Dynamic_SetInt" reportedError encountered while processing a dynamic native
L 09
/14/2015 18:13:08: [SMDisplaying call stack trace for plugin "DynamicTest1.smx":
L 09/14/2015 18:13:08: [SM]   [0]  Line 73D:\SourcemodPlugins\include\dynamic.inc::Dynamic.SetInt()
L 09/14/2015 18:13:08: [SM]   [1]  Line 9D:\SourcemodPlugins\DynamicTest1.sp::OnPluginStart() 

if you need the code, tell me (well nothing special, I just made a basic SetInt Plugin and another that should receive the value)

Neuro Toxin 09-14-2015 19:41

Re: Dynamic Objects and Properties
 
Quote:

L 09/14/2015 - 18:11:29: [SM] Native "ThrowNativeError" reported: Unable to access dynamic handle 0
Looks like it's not initialised.

Code:

Dynamic someobject = Dynamic();
You then need to share someobject. Internally someobject is an integer and represents the internal index for the collection array. The best idea is to pass it through a native.

Code:

Dynamic someobject = view_as<Dynamic>(Native_GetDynamicObject());
You would return someobject in the native like so.

Code:

return view_as<int>(someobject);
If you still encounter problems with this advise feel free to post code for both plugins.

DabuDos 09-16-2015 10:29

Re: Dynamic Objects and Properties
 
Quote:

Originally Posted by Neuro Toxin (Post 2343037)
Looks like it's not initialised.

Code:

Dynamic someobject = Dynamic();
You then need to share someobject. Internally someobject is an integer and represents the internal index for the collection array. The best idea is to pass it through a native.

Code:

Dynamic someobject = view_as<Dynamic>(Native_GetDynamicObject());
You would return someobject in the native like so.

Code:

return view_as<int>(someobject);
If you still encounter problems with this advise feel free to post code for both plugins.

Well, I get many errors with "undefined symbol" now, I don't really understand why.
Can you maybe post a basic code for one plugin that sends the value and another plugin that receives the value? Maybe thats easier to understand for me & others.

Neuro Toxin 09-16-2015 19:17

Re: Dynamic Objects and Properties
 
Ok. I'll do something up on the weekend hopefully.

Dr. Api 09-18-2015 01:44

Re: Dynamic Objects and Properties
 
Nice, i'm learning more and more from you, keep going.
I will propably use for my next mod.

Nerus 12-26-2015 11:10

Re: Dynamic Objects and Properties
 
Nice to see similar objectives like in C# and Java in SM :) I head problems with methodsmaps, now I think that u solve my problem, THX !

I always create same question in any topic of plugins, but this is my first time in lib thread: will you add dynamic to github please ? :)

P.S. One more thing:

Quote:

someobject.GetInt("m_iSomeField");
Please add second 'default' parameter if value not found.

I had some errors i try to create dynamic player like:
Quote:

Dynamic player = Dynamic();
Next I added some values like:
Quote:

player.SetString("steam_id3", steam_id3, sizeof(steam_id3));
get working correct after set.

Next I added dynamic player to array based on client id:
Quote:

Players[client] = player;
Last code check player is INVALID_HANDLE and result: is invalid.
Quote:

if(player == INVALID_HANDLE)
What is wrong ?


Regards,
Nerus

Neuro Toxin 12-27-2015 17:56

Re: Dynamic Objects and Properties
 
To check if a object is valid use...

Code:

if (player.IsValid)


You also need to Dispose() your object OnClientDisconnect. These objects are not managed and will cause memory leaks if not disposed of correctly.

Code:

player.Dispose();


GetInt() already supports default values as the second parameter.

Code:

int defaultvalue = player.GetInt ("notset", 123);

Neuro Toxin 02-20-2016 09:58

Re: [2016.02.21] Dynamic Objects and Properties - v004
 
I just moved this to Github and released version 004.

The main post has been updated.

A new example is included on how this is used that covers almost all features of Dynamic.

Neuro Toxin 02-26-2016 20:51

Re: [2016.02.27] Dynamic Objects and Properties - v005
 
As we know, methodmaps require data backing the properties which steers people away from using them.

You can create methodmaps that inherit the Dynamic class and write properties that use the dynamic object to back the properties.

dynamic-example.inc

Quote:

In the linked example, I use static offsets in each property of the methodmap to cache member positions in the dynamic object to increase performance.

This then requires all the dynamic members to be in the same positions.

To get around this, you can see I set all the members in the initialiser for MyClass, this means over multiple instances of MyClass, the member offsets will always correct.
The key points to following when making your own dynamic classes are:

1. All members must be set to their default values in the constructer
2. Use static offsets in each property to increase performance

I'm about to make a webpage where you can define your class. It will then spit out the methodmap to make that class using Dynamic.

Neuro Toxin 02-27-2016 01:17

Re: [2016.02.27] Dynamic Objects and Properties - v005
 
Dynamic Classes
- Extend Dynamic to create your own Dynamic Classes
- A Class Builder to quickly generate basic Dynamic Classes
- You dont have to muck around with Dynamic Member Offsets to improve performance
- All of the same features that come with Dynamic Objects

I just finished a basic web based class builder.

I used these settings to...

Generate this methodmap...

Which can be used like this in a plugin!

zipcore 02-27-2016 03:23

Re: [2016.02.27] Dynamic Objects and Properties - v005
 
not bad... not bad

Neuro Toxin 02-27-2016 05:14

Re: [2016.02.27] Dynamic Objects and Properties - v005
 
The latest profile stats...


I'm really curious around why the reading of members using a string membername is quicker than using integer offsets.

Quote:

Read 99000 dynamic member(s) in 0.139648 second(s)
Read 99000 dynamic member(s) using offsets in 0.141601 second(s)

Read 99000 dynamic member(s) in 0.139648 second(s)
Read 99000 dynamic member(s) using offsets in 0.141601 second(s)

Read 99000 dynamic member(s) in 0.139648 second(s)
Read 99000 dynamic member(s) using offsets in 0.141601 second(s)

Neuro Toxin 03-15-2016 07:05

Re: [2016.02.27] Dynamic Objects and Properties - v005
 
Just updated Dynamic to version 0.0.6

Changes...

The technical answer as to why offsets are now faster...

The latest profile stats...

Member name lookups vs Offset lookups...


Github Changes
Download

Edit:

I've completed a code generate tweak for the Dynamic Class Generator.

- Member offsets are stored when a member is set for the first time

Neuro Toxin 03-17-2016 04:30

Re: Dynamic Objects and Properties - v006 - [2016.03.15]
 
Just realised I need to add support for boolean types.

I'll do this soon by wrapping Get/SetBool around integer support. This will be similar to how Get/SetObject works if your familiar with how the Dynamic methodmap works.

In the mean time booleans can be used with Get/SetInt.

kossolax 03-18-2016 04:35

Re: Dynamic Objects and Properties - v006 - [2016.03.15]
 
Quote:

Originally Posted by Neuro Toxin (Post 2403212)
Just realised I need to add support for boolean types.

What about handle types ?

Neuro Toxin 03-18-2016 05:05

Re: Dynamic Objects and Properties - v006 - [2016.03.15]
 
Great question.

Handles can currently be used via Get/SetInt. You might need view_as casting to avoid type mismatch warnings.

I will however add support for Handles after completing bool support.

ImACow 03-18-2016 08:14

Re: Dynamic Objects and Properties - v006 - [2016.03.15]
 
This is amazing.

Thanks for this! this saved me hours of writing natives to cross communicate with plugins

ImACow 03-18-2016 08:37

Re: Dynamic Objects and Properties - v006 - [2016.03.15]
 
Dynamic someobj = Dynamic();
someobj.SetBool("somebool", true);


-> error 105: cannot find method or property Dynamic.SetBool

Powerlord 03-18-2016 09:18

Re: Dynamic Objects and Properties - v006 - [2016.03.15]
 
Quote:

Originally Posted by ImACow (Post 2403529)
Dynamic someobj = Dynamic();
someobj.SetBool("somebool", true);


-> error 105: cannot find method or property Dynamic.SetBool

Quote:

Originally Posted by Neuro Toxin (Post 2403212)
Just realised I need to add support for boolean types.

I'll do this soon...

(relevant part bolded)

ImACow 03-18-2016 09:44

Re: Dynamic Objects and Properties - v006 - [2016.03.15]
 
My bad, did not read through pages.

Btw I want to make data persistent when a plugin reloads, is this the correct way ?

Offload.smx -> getDyn & setDyn.

Mainplugin.smx
-> OnPluginStart
-> Create Dynamic object.

-> OnPluginEnd
-> Use SetDyn from Offload.smx to set the reference of the dynamic.



then in main plugin on a plugin start check if the getDyn is a valid dynamic?

http://i.imgur.com/N6dxQis.png

Neuro Toxin 03-18-2016 20:23

Re: Dynamic Objects and Properties - v006 - [2016.03.15]
 
After some head scratching this is what im thinking.

Code:

Dynamic someobj = Dynamic.GetByName("pluginname_settings");
Code:

If (!someobj.IsValid)
Code:

someobj = Dynamic();
someobj.SetName("pluginname_settings");

This would enable you to set a unique name for any dynamic object. Any other plugins can then Dynamic.GetByName without having to parse the obj index through natives.

In the sanerio above. Moomoo could try find a dynamic object by name on plugin start.

Neuro Toxin 03-19-2016 00:31

Re: Dynamic Objects and Properties - v007 - [2016.03.19]
 
Version 0.0.7 released
- Added support for boolean datatypes
- Updated example plugin to include bool usage
- Updated test plugin to verify bool usage and conversion between datatypes

Neuro Toxin 03-19-2016 02:39

Re: Dynamic Objects and Properties - v007 - [2016.03.19]
 
Version 0.0.8 released
- Added support for naming dynamic objects
- Updated example plugin to show usage of naming objects
- Dispose now supports automatic disposing of disposable members

Code:

public bool SetName(const char[] objectname, bool replace=false)
public static Dynamic FindByName(const char[] objectname)

Code:

// You can name a dynamic object
someobj.SetName("someobj");

// So another plugin can access it like so
someobj = Dynamic.FindByName("someobj");

Github Changes

ImACow 03-19-2016 10:37

Re: Dynamic Objects and Properties - v008 - [2016.03.19]
 
Awesome dude!

ImACow 03-19-2016 10:38

Re: Dynamic Objects and Properties - v008 - [2016.03.19]
 
I'm using this now to offload all my plugin data on PluginEnd, and recover it on PluginStart

Neuro Toxin 03-19-2016 20:28

Re: Dynamic Objects and Properties - v008 - [2016.03.19]
 
An example of the changes around .Dispose.

Code:

Dynamic someobj = Dynamic();
Dynamic anotherobj = Dynamic();
someobj.SetObject("anotherobj", anotherobj);

// This dispose call will also call dispose on anotherobj
someobj.Dispose();

// If you dont want anotherobj to be disposed you can use this instead
someobj.Dispose(false);

// And then you can dispose anotherobj later
anotherobj.Dispose();


ImACow 03-19-2016 21:37

Re: Dynamic Objects and Properties - v008 - [2016.03.19]
 
Some ideas:

can FindByName be used to search children?

.FindParent()
.FindChild()
.DisposeParent()
.DisposeChildren()
.CountChildren()

Cow.


All times are GMT -4. The time now is 09:38.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.