Updates Angelscript addons, adds angelscript dictionary addon.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2021-03-27 22:39:25 +01:00
parent e5b2ff5c59
commit e748f6e96f
10 changed files with 1921 additions and 376 deletions

View File

@@ -4,6 +4,7 @@
#include <assert.h>
#include <stdio.h> // sprintf
#include <string>
#include <algorithm> // std::sort
#include "scriptarray.h"
@@ -998,7 +999,7 @@ void CScriptArray::Destruct(SArrayBuffer *buf, asUINT start, asUINT end)
// internal
bool CScriptArray::Less(const void *a, const void *b, bool asc, asIScriptContext *ctx, SArrayCache *cache)
bool CScriptArray::Less(const void *a, const void *b, bool asc)
{
if( !asc )
{
@@ -1027,42 +1028,6 @@ bool CScriptArray::Less(const void *a, const void *b, bool asc, asIScriptContext
#undef COMPARE
}
}
else
{
int r = 0;
if( subTypeId & asTYPEID_OBJHANDLE )
{
// Allow sort to work even if the array contains null handles
if( *(void**)a == 0 ) return true;
if( *(void**)b == 0 ) return false;
}
// Execute object opCmp
if( cache && cache->cmpFunc )
{
// TODO: Add proper error handling
r = ctx->Prepare(cache->cmpFunc); assert(r >= 0);
if( subTypeId & asTYPEID_OBJHANDLE )
{
r = ctx->SetObject(*((void**)a)); assert(r >= 0);
r = ctx->SetArgObject(0, *((void**)b)); assert(r >= 0);
}
else
{
r = ctx->SetObject((void*)a); assert(r >= 0);
r = ctx->SetArgObject(0, (void*)b); assert(r >= 0);
}
r = ctx->Execute();
if( r == asEXECUTION_FINISHED )
{
return (int)ctx->GetReturnDWord() < 0;
}
}
}
return false;
}
@@ -1475,12 +1440,11 @@ void CScriptArray::Sort(asUINT startAt, asUINT count, bool asc)
return;
}
asBYTE tmp[16];
asIScriptContext *cmpContext = 0;
bool isNested = false;
if( subTypeId & ~asTYPEID_MASK_SEQNBR )
{
asIScriptContext *cmpContext = 0;
bool isNested = false;
// Try to reuse the active context
cmpContext = asGetActiveContext();
if( cmpContext )
@@ -1491,38 +1455,83 @@ void CScriptArray::Sort(asUINT startAt, asUINT count, bool asc)
cmpContext = 0;
}
if( cmpContext == 0 )
{
cmpContext = objType->GetEngine()->RequestContext();
// Do the sorting
struct {
bool asc;
asIScriptContext *cmpContext;
asIScriptFunction *cmpFunc;
bool operator()(void *a, void *b) const
{
if( !asc )
{
// Swap items
void *TEMP = a;
a = b;
b = TEMP;
}
int r = 0;
// Allow sort to work even if the array contains null handles
if( a == 0 ) return true;
if( b == 0 ) return false;
// Execute object opCmp
if( cmpFunc )
{
// TODO: Add proper error handling
r = cmpContext->Prepare(cmpFunc); assert(r >= 0);
r = cmpContext->SetObject(a); assert(r >= 0);
r = cmpContext->SetArgObject(0, b); assert(r >= 0);
r = cmpContext->Execute();
if( r == asEXECUTION_FINISHED )
{
return (int)cmpContext->GetReturnDWord() < 0;
}
}
return false;
}
} customLess = {asc, cmpContext, cache ? cache->cmpFunc : 0};
std::sort((void**)GetArrayItemPointer(start), (void**)GetArrayItemPointer(end), customLess);
// Clean up
if( cmpContext )
{
if( isNested )
{
asEContextState state = cmpContext->GetState();
cmpContext->PopState();
if( state == asEXECUTION_ABORTED )
cmpContext->Abort();
}
else
objType->GetEngine()->ReturnContext(cmpContext);
}
}
// Insertion sort
for( int i = start + 1; i < end; i++ )
else
{
Copy(tmp, GetArrayItemPointer(i));
int j = i - 1;
while( j >= start && Less(GetDataPointer(tmp), At(j), asc, cmpContext, cache) )
// TODO: Use std::sort for primitive types too
// Insertion sort
asBYTE tmp[16];
for( int i = start + 1; i < end; i++ )
{
Copy(GetArrayItemPointer(j + 1), GetArrayItemPointer(j));
j--;
}
Copy(tmp, GetArrayItemPointer(i));
Copy(GetArrayItemPointer(j + 1), tmp);
}
int j = i - 1;
if( cmpContext )
{
if( isNested )
{
asEContextState state = cmpContext->GetState();
cmpContext->PopState();
if( state == asEXECUTION_ABORTED )
cmpContext->Abort();
while( j >= start && Less(GetDataPointer(tmp), At(j), asc) )
{
Copy(GetArrayItemPointer(j + 1), GetArrayItemPointer(j));
j--;
}
Copy(GetArrayItemPointer(j + 1), tmp);
}
else
objType->GetEngine()->ReturnContext(cmpContext);
}
}
@@ -1550,7 +1559,6 @@ void CScriptArray::Sort(asIScriptFunction *func, asUINT startAt, asUINT count)
return;
}
asBYTE tmp[16];
asIScriptContext *cmpContext = 0;
bool isNested = false;
@@ -1567,6 +1575,7 @@ void CScriptArray::Sort(asIScriptFunction *func, asUINT startAt, asUINT count)
cmpContext = objType->GetEngine()->RequestContext();
// Insertion sort
asBYTE tmp[16];
for (asUINT i = start + 1; i < end; i++)
{
Copy(tmp, GetArrayItemPointer(i));

View File

@@ -120,7 +120,7 @@ protected:
CScriptArray(const CScriptArray &other);
virtual ~CScriptArray();
bool Less(const void *a, const void *b, bool asc, asIScriptContext *ctx, SArrayCache *cache);
bool Less(const void *a, const void *b, bool asc);
void *GetArrayItemPointer(int index);
void *GetDataPointer(void *buffer);
void Copy(void *dst, void *src);