Updates Angelscript addons, adds angelscript dictionary addon.
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
This commit is contained in:
parent
e5b2ff5c59
commit
e748f6e96f
|
@ -4,6 +4,7 @@
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdio.h> // sprintf
|
#include <stdio.h> // sprintf
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <algorithm> // std::sort
|
||||||
|
|
||||||
#include "scriptarray.h"
|
#include "scriptarray.h"
|
||||||
|
|
||||||
|
@ -998,7 +999,7 @@ void CScriptArray::Destruct(SArrayBuffer *buf, asUINT start, asUINT end)
|
||||||
|
|
||||||
|
|
||||||
// internal
|
// 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 )
|
if( !asc )
|
||||||
{
|
{
|
||||||
|
@ -1027,42 +1028,6 @@ bool CScriptArray::Less(const void *a, const void *b, bool asc, asIScriptContext
|
||||||
#undef COMPARE
|
#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;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -1475,12 +1440,11 @@ void CScriptArray::Sort(asUINT startAt, asUINT count, bool asc)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
asBYTE tmp[16];
|
if( subTypeId & ~asTYPEID_MASK_SEQNBR )
|
||||||
|
{
|
||||||
asIScriptContext *cmpContext = 0;
|
asIScriptContext *cmpContext = 0;
|
||||||
bool isNested = false;
|
bool isNested = false;
|
||||||
|
|
||||||
if( subTypeId & ~asTYPEID_MASK_SEQNBR )
|
|
||||||
{
|
|
||||||
// Try to reuse the active context
|
// Try to reuse the active context
|
||||||
cmpContext = asGetActiveContext();
|
cmpContext = asGetActiveContext();
|
||||||
if( cmpContext )
|
if( cmpContext )
|
||||||
|
@ -1491,27 +1455,50 @@ void CScriptArray::Sort(asUINT startAt, asUINT count, bool asc)
|
||||||
cmpContext = 0;
|
cmpContext = 0;
|
||||||
}
|
}
|
||||||
if( cmpContext == 0 )
|
if( cmpContext == 0 )
|
||||||
{
|
|
||||||
cmpContext = objType->GetEngine()->RequestContext();
|
cmpContext = objType->GetEngine()->RequestContext();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insertion sort
|
// Do the sorting
|
||||||
for( int i = start + 1; i < end; i++ )
|
struct {
|
||||||
|
bool asc;
|
||||||
|
asIScriptContext *cmpContext;
|
||||||
|
asIScriptFunction *cmpFunc;
|
||||||
|
bool operator()(void *a, void *b) const
|
||||||
{
|
{
|
||||||
Copy(tmp, GetArrayItemPointer(i));
|
if( !asc )
|
||||||
|
|
||||||
int j = i - 1;
|
|
||||||
|
|
||||||
while( j >= start && Less(GetDataPointer(tmp), At(j), asc, cmpContext, cache) )
|
|
||||||
{
|
{
|
||||||
Copy(GetArrayItemPointer(j + 1), GetArrayItemPointer(j));
|
// Swap items
|
||||||
j--;
|
void *TEMP = a;
|
||||||
|
a = b;
|
||||||
|
b = TEMP;
|
||||||
}
|
}
|
||||||
|
|
||||||
Copy(GetArrayItemPointer(j + 1), tmp);
|
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( cmpContext )
|
||||||
{
|
{
|
||||||
if( isNested )
|
if( isNested )
|
||||||
|
@ -1525,6 +1512,28 @@ void CScriptArray::Sort(asUINT startAt, asUINT count, bool asc)
|
||||||
objType->GetEngine()->ReturnContext(cmpContext);
|
objType->GetEngine()->ReturnContext(cmpContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// TODO: Use std::sort for primitive types too
|
||||||
|
|
||||||
|
// Insertion sort
|
||||||
|
asBYTE tmp[16];
|
||||||
|
for( int i = start + 1; i < end; i++ )
|
||||||
|
{
|
||||||
|
Copy(tmp, GetArrayItemPointer(i));
|
||||||
|
|
||||||
|
int j = i - 1;
|
||||||
|
|
||||||
|
while( j >= start && Less(GetDataPointer(tmp), At(j), asc) )
|
||||||
|
{
|
||||||
|
Copy(GetArrayItemPointer(j + 1), GetArrayItemPointer(j));
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
|
||||||
|
Copy(GetArrayItemPointer(j + 1), tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Sort with script callback for comparing elements
|
// Sort with script callback for comparing elements
|
||||||
void CScriptArray::Sort(asIScriptFunction *func, asUINT startAt, asUINT count)
|
void CScriptArray::Sort(asIScriptFunction *func, asUINT startAt, asUINT count)
|
||||||
|
@ -1550,7 +1559,6 @@ void CScriptArray::Sort(asIScriptFunction *func, asUINT startAt, asUINT count)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
asBYTE tmp[16];
|
|
||||||
asIScriptContext *cmpContext = 0;
|
asIScriptContext *cmpContext = 0;
|
||||||
bool isNested = false;
|
bool isNested = false;
|
||||||
|
|
||||||
|
@ -1567,6 +1575,7 @@ void CScriptArray::Sort(asIScriptFunction *func, asUINT startAt, asUINT count)
|
||||||
cmpContext = objType->GetEngine()->RequestContext();
|
cmpContext = objType->GetEngine()->RequestContext();
|
||||||
|
|
||||||
// Insertion sort
|
// Insertion sort
|
||||||
|
asBYTE tmp[16];
|
||||||
for (asUINT i = start + 1; i < end; i++)
|
for (asUINT i = start + 1; i < end; i++)
|
||||||
{
|
{
|
||||||
Copy(tmp, GetArrayItemPointer(i));
|
Copy(tmp, GetArrayItemPointer(i));
|
||||||
|
|
|
@ -120,7 +120,7 @@ protected:
|
||||||
CScriptArray(const CScriptArray &other);
|
CScriptArray(const CScriptArray &other);
|
||||||
virtual ~CScriptArray();
|
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 *GetArrayItemPointer(int index);
|
||||||
void *GetDataPointer(void *buffer);
|
void *GetDataPointer(void *buffer);
|
||||||
void Copy(void *dst, void *src);
|
void Copy(void *dst, void *src);
|
||||||
|
|
|
@ -14,6 +14,8 @@
|
||||||
|
|
||||||
// TODO: Implement flags for turning on/off include directives and conditional programming
|
// TODO: Implement flags for turning on/off include directives and conditional programming
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//---------------------------
|
//---------------------------
|
||||||
// Declaration
|
// Declaration
|
||||||
//
|
//
|
||||||
|
@ -23,6 +25,7 @@
|
||||||
#include <angelscript.h>
|
#include <angelscript.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if defined(_MSC_VER) && _MSC_VER <= 1200
|
#if defined(_MSC_VER) && _MSC_VER <= 1200
|
||||||
// disable the annoying warnings on MSVC 6
|
// disable the annoying warnings on MSVC 6
|
||||||
#pragma warning (disable:4786)
|
#pragma warning (disable:4786)
|
||||||
|
@ -197,7 +200,7 @@ protected:
|
||||||
{
|
{
|
||||||
bool operator()(const std::string &a, const std::string &b) const
|
bool operator()(const std::string &a, const std::string &b) const
|
||||||
{
|
{
|
||||||
return _strcmpi(a.c_str(), b.c_str()) < 0;
|
return _stricmp(a.c_str(), b.c_str()) < 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
std::set<std::string, ci_less> includedScripts;
|
std::set<std::string, ci_less> includedScripts;
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,240 @@
|
||||||
|
#ifndef SCRIPTDICTIONARY_H
|
||||||
|
#define SCRIPTDICTIONARY_H
|
||||||
|
|
||||||
|
// The dictionary class relies on the script string object, thus the script
|
||||||
|
// string type must be registered with the engine before registering the
|
||||||
|
// dictionary type
|
||||||
|
|
||||||
|
#ifndef ANGELSCRIPT_H
|
||||||
|
// Avoid having to inform include path if header is already include before
|
||||||
|
#include <angelscript.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// By default the CScriptDictionary use the std::string for the keys.
|
||||||
|
// If the application uses a custom string type, then this typedef
|
||||||
|
// can be changed accordingly.
|
||||||
|
#include <string>
|
||||||
|
typedef std::string dictKey_t;
|
||||||
|
|
||||||
|
// Forward declare CScriptDictValue so we can typedef the internal map type
|
||||||
|
BEGIN_AS_NAMESPACE
|
||||||
|
class CScriptDictValue;
|
||||||
|
END_AS_NAMESPACE
|
||||||
|
|
||||||
|
// C++11 introduced the std::unordered_map which is a hash map which is
|
||||||
|
// is generally more performatic for lookups than the std::map which is a
|
||||||
|
// binary tree.
|
||||||
|
// TODO: memory: The map allocator should use the asAllocMem and asFreeMem
|
||||||
|
#if AS_CAN_USE_CPP11
|
||||||
|
#include <unordered_map>
|
||||||
|
typedef std::unordered_map<dictKey_t, AS_NAMESPACE_QUALIFIER CScriptDictValue> dictMap_t;
|
||||||
|
#else
|
||||||
|
#include <map>
|
||||||
|
typedef std::map<dictKey_t, AS_NAMESPACE_QUALIFIER CScriptDictValue> dictMap_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
// Turn off annoying warnings about truncated symbol names
|
||||||
|
#pragma warning (disable:4786)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Sometimes it may be desired to use the same method names as used by C++ STL.
|
||||||
|
// This may for example reduce time when converting code from script to C++ or
|
||||||
|
// back.
|
||||||
|
//
|
||||||
|
// 0 = off
|
||||||
|
// 1 = on
|
||||||
|
|
||||||
|
#ifndef AS_USE_STLNAMES
|
||||||
|
#define AS_USE_STLNAMES 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
BEGIN_AS_NAMESPACE
|
||||||
|
|
||||||
|
class CScriptArray;
|
||||||
|
class CScriptDictionary;
|
||||||
|
|
||||||
|
class CScriptDictValue
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// This class must not be declared as local variable in C++, because it needs
|
||||||
|
// to receive the script engine pointer in all operations. The engine pointer
|
||||||
|
// is not kept as member in order to keep the size down
|
||||||
|
CScriptDictValue();
|
||||||
|
CScriptDictValue(asIScriptEngine *engine, void *value, int typeId);
|
||||||
|
|
||||||
|
// Destructor must not be called without first calling FreeValue, otherwise a memory leak will occur
|
||||||
|
~CScriptDictValue();
|
||||||
|
|
||||||
|
// Replace the stored value
|
||||||
|
void Set(asIScriptEngine *engine, void *value, int typeId);
|
||||||
|
void Set(asIScriptEngine *engine, const asINT64 &value);
|
||||||
|
void Set(asIScriptEngine *engine, const double &value);
|
||||||
|
void Set(asIScriptEngine *engine, CScriptDictValue &value);
|
||||||
|
|
||||||
|
// Gets the stored value. Returns false if the value isn't compatible with the informed typeId
|
||||||
|
bool Get(asIScriptEngine *engine, void *value, int typeId) const;
|
||||||
|
bool Get(asIScriptEngine *engine, asINT64 &value) const;
|
||||||
|
bool Get(asIScriptEngine *engine, double &value) const;
|
||||||
|
|
||||||
|
// Returns the address of the stored value for inspection
|
||||||
|
const void *GetAddressOfValue() const;
|
||||||
|
|
||||||
|
// Returns the type id of the stored value
|
||||||
|
int GetTypeId() const;
|
||||||
|
|
||||||
|
// Free the stored value
|
||||||
|
void FreeValue(asIScriptEngine *engine);
|
||||||
|
|
||||||
|
// GC callback
|
||||||
|
void EnumReferences(asIScriptEngine *engine);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
friend class CScriptDictionary;
|
||||||
|
|
||||||
|
union
|
||||||
|
{
|
||||||
|
asINT64 m_valueInt;
|
||||||
|
double m_valueFlt;
|
||||||
|
void *m_valueObj;
|
||||||
|
};
|
||||||
|
int m_typeId;
|
||||||
|
};
|
||||||
|
|
||||||
|
class CScriptDictionary
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// Factory functions
|
||||||
|
static CScriptDictionary *Create(asIScriptEngine *engine);
|
||||||
|
|
||||||
|
// Called from the script to instantiate a dictionary from an initialization list
|
||||||
|
static CScriptDictionary *Create(asBYTE *buffer);
|
||||||
|
|
||||||
|
// Reference counting
|
||||||
|
void AddRef() const;
|
||||||
|
void Release() const;
|
||||||
|
|
||||||
|
// Reassign the dictionary
|
||||||
|
CScriptDictionary &operator =(const CScriptDictionary &other);
|
||||||
|
|
||||||
|
// Sets a key/value pair
|
||||||
|
void Set(const dictKey_t &key, void *value, int typeId);
|
||||||
|
void Set(const dictKey_t &key, const asINT64 &value);
|
||||||
|
void Set(const dictKey_t &key, const double &value);
|
||||||
|
|
||||||
|
// Gets the stored value. Returns false if the value isn't compatible with the informed typeId
|
||||||
|
bool Get(const dictKey_t &key, void *value, int typeId) const;
|
||||||
|
bool Get(const dictKey_t &key, asINT64 &value) const;
|
||||||
|
bool Get(const dictKey_t &key, double &value) const;
|
||||||
|
|
||||||
|
// Index accessors. If the dictionary is not const it inserts the value if it doesn't already exist
|
||||||
|
// If the dictionary is const then a script exception is set if it doesn't exist and a null pointer is returned
|
||||||
|
CScriptDictValue *operator[](const dictKey_t &key);
|
||||||
|
const CScriptDictValue *operator[](const dictKey_t &key) const;
|
||||||
|
|
||||||
|
// Returns the type id of the stored value, or negative if it doesn't exist
|
||||||
|
int GetTypeId(const dictKey_t &key) const;
|
||||||
|
|
||||||
|
// Returns true if the key is set
|
||||||
|
bool Exists(const dictKey_t &key) const;
|
||||||
|
|
||||||
|
// Returns true if there are no key/value pairs in the dictionary
|
||||||
|
bool IsEmpty() const;
|
||||||
|
|
||||||
|
// Returns the number of key/value pairs in the dictionary
|
||||||
|
asUINT GetSize() const;
|
||||||
|
|
||||||
|
// Deletes the key
|
||||||
|
bool Delete(const dictKey_t &key);
|
||||||
|
|
||||||
|
// Deletes all keys
|
||||||
|
void DeleteAll();
|
||||||
|
|
||||||
|
// Get an array of all keys
|
||||||
|
CScriptArray *GetKeys() const;
|
||||||
|
|
||||||
|
// STL style iterator
|
||||||
|
class CIterator
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void operator++(); // Pre-increment
|
||||||
|
void operator++(int); // Post-increment
|
||||||
|
|
||||||
|
// This is needed to support C++11 range-for
|
||||||
|
CIterator &operator*();
|
||||||
|
|
||||||
|
bool operator==(const CIterator &other) const;
|
||||||
|
bool operator!=(const CIterator &other) const;
|
||||||
|
|
||||||
|
// Accessors
|
||||||
|
const dictKey_t &GetKey() const;
|
||||||
|
int GetTypeId() const;
|
||||||
|
bool GetValue(asINT64 &value) const;
|
||||||
|
bool GetValue(double &value) const;
|
||||||
|
bool GetValue(void *value, int typeId) const;
|
||||||
|
const void * GetAddressOfValue() const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
friend class CScriptDictionary;
|
||||||
|
|
||||||
|
CIterator();
|
||||||
|
CIterator(const CScriptDictionary &dict,
|
||||||
|
dictMap_t::const_iterator it);
|
||||||
|
|
||||||
|
CIterator &operator=(const CIterator &) {return *this;} // Not used
|
||||||
|
|
||||||
|
dictMap_t::const_iterator m_it;
|
||||||
|
const CScriptDictionary &m_dict;
|
||||||
|
};
|
||||||
|
|
||||||
|
CIterator begin() const;
|
||||||
|
CIterator end() const;
|
||||||
|
CIterator find(const dictKey_t &key) const;
|
||||||
|
|
||||||
|
// Garbage collections behaviours
|
||||||
|
int GetRefCount();
|
||||||
|
void SetGCFlag();
|
||||||
|
bool GetGCFlag();
|
||||||
|
void EnumReferences(asIScriptEngine *engine);
|
||||||
|
void ReleaseAllReferences(asIScriptEngine *engine);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// Since the dictionary uses the asAllocMem and asFreeMem functions to allocate memory
|
||||||
|
// the constructors are made protected so that the application cannot allocate it
|
||||||
|
// manually in a different way
|
||||||
|
CScriptDictionary(asIScriptEngine *engine);
|
||||||
|
CScriptDictionary(asBYTE *buffer);
|
||||||
|
|
||||||
|
// We don't want anyone to call the destructor directly, it should be called through the Release method
|
||||||
|
virtual ~CScriptDictionary();
|
||||||
|
|
||||||
|
// Cache the object types needed
|
||||||
|
void Init(asIScriptEngine *engine);
|
||||||
|
|
||||||
|
// Our properties
|
||||||
|
asIScriptEngine *engine;
|
||||||
|
mutable int refCount;
|
||||||
|
mutable bool gcFlag;
|
||||||
|
dictMap_t dict;
|
||||||
|
};
|
||||||
|
|
||||||
|
// This function will determine the configuration of the engine
|
||||||
|
// and use one of the two functions below to register the dictionary object
|
||||||
|
void RegisterScriptDictionary(asIScriptEngine *engine);
|
||||||
|
|
||||||
|
// Call this function to register the math functions
|
||||||
|
// using native calling conventions
|
||||||
|
void RegisterScriptDictionary_Native(asIScriptEngine *engine);
|
||||||
|
|
||||||
|
// Use this one instead if native calling conventions
|
||||||
|
// are not supported on the target platform
|
||||||
|
void RegisterScriptDictionary_Generic(asIScriptEngine *engine);
|
||||||
|
|
||||||
|
END_AS_NAMESPACE
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,6 +1,6 @@
|
||||||
#include "scripthandle.h"
|
#include "scripthandle.h"
|
||||||
#include <assert.h>
|
|
||||||
#include <new>
|
#include <new>
|
||||||
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
BEGIN_AS_NAMESPACE
|
BEGIN_AS_NAMESPACE
|
||||||
|
@ -11,19 +11,22 @@ static void Construct(CScriptHandle* self, const CScriptHandle& o) { new (self)
|
||||||
void Construct(CScriptHandle *self, void *ref, int typeId) { new(self) CScriptHandle(ref, typeId); }
|
void Construct(CScriptHandle *self, void *ref, int typeId) { new(self) CScriptHandle(ref, typeId); }
|
||||||
static void Destruct(CScriptHandle *self) { self->~CScriptHandle(); }
|
static void Destruct(CScriptHandle *self) { self->~CScriptHandle(); }
|
||||||
|
|
||||||
CScriptHandle::CScriptHandle() {
|
CScriptHandle::CScriptHandle()
|
||||||
|
{
|
||||||
m_ref = 0;
|
m_ref = 0;
|
||||||
m_type = 0;
|
m_type = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CScriptHandle::CScriptHandle(const CScriptHandle& other) {
|
CScriptHandle::CScriptHandle(const CScriptHandle &other)
|
||||||
|
{
|
||||||
m_ref = other.m_ref;
|
m_ref = other.m_ref;
|
||||||
m_type = other.m_type;
|
m_type = other.m_type;
|
||||||
|
|
||||||
AddRefHandle();
|
AddRefHandle();
|
||||||
}
|
}
|
||||||
|
|
||||||
CScriptHandle::CScriptHandle(void* ref, asITypeInfo* type) {
|
CScriptHandle::CScriptHandle(void *ref, asITypeInfo *type)
|
||||||
|
{
|
||||||
m_ref = ref;
|
m_ref = ref;
|
||||||
m_type = type;
|
m_type = type;
|
||||||
|
|
||||||
|
@ -32,17 +35,23 @@ CScriptHandle::CScriptHandle(void* ref, asITypeInfo* type) {
|
||||||
|
|
||||||
// This constructor shouldn't be called from the application
|
// This constructor shouldn't be called from the application
|
||||||
// directly as it requires an active script context
|
// directly as it requires an active script context
|
||||||
CScriptHandle::CScriptHandle(void* ref, int typeId) {
|
CScriptHandle::CScriptHandle(void *ref, int typeId)
|
||||||
|
{
|
||||||
m_ref = 0;
|
m_ref = 0;
|
||||||
m_type = 0;
|
m_type = 0;
|
||||||
|
|
||||||
Assign(ref, typeId);
|
Assign(ref, typeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
CScriptHandle::~CScriptHandle() { ReleaseHandle(); }
|
CScriptHandle::~CScriptHandle()
|
||||||
|
{
|
||||||
|
ReleaseHandle();
|
||||||
|
}
|
||||||
|
|
||||||
void CScriptHandle::ReleaseHandle() {
|
void CScriptHandle::ReleaseHandle()
|
||||||
if (m_ref && m_type) {
|
{
|
||||||
|
if( m_ref && m_type )
|
||||||
|
{
|
||||||
asIScriptEngine *engine = m_type->GetEngine();
|
asIScriptEngine *engine = m_type->GetEngine();
|
||||||
engine->ReleaseScriptObject(m_ref, m_type);
|
engine->ReleaseScriptObject(m_ref, m_type);
|
||||||
|
|
||||||
|
@ -53,8 +62,10 @@ void CScriptHandle::ReleaseHandle() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CScriptHandle::AddRefHandle() {
|
void CScriptHandle::AddRefHandle()
|
||||||
if (m_ref && m_type) {
|
{
|
||||||
|
if( m_ref && m_type )
|
||||||
|
{
|
||||||
asIScriptEngine *engine = m_type->GetEngine();
|
asIScriptEngine *engine = m_type->GetEngine();
|
||||||
engine->AddRefScriptObject(m_ref, m_type);
|
engine->AddRefScriptObject(m_ref, m_type);
|
||||||
|
|
||||||
|
@ -64,15 +75,16 @@ void CScriptHandle::AddRefHandle() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CScriptHandle& CScriptHandle::operator=(const CScriptHandle& other) {
|
CScriptHandle &CScriptHandle::operator =(const CScriptHandle &other)
|
||||||
|
{
|
||||||
Set(other.m_ref, other.m_type);
|
Set(other.m_ref, other.m_type);
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CScriptHandle::Set(void* ref, asITypeInfo* type) {
|
void CScriptHandle::Set(void *ref, asITypeInfo *type)
|
||||||
if (m_ref == ref)
|
{
|
||||||
return;
|
if( m_ref == ref ) return;
|
||||||
|
|
||||||
ReleaseHandle();
|
ReleaseHandle();
|
||||||
|
|
||||||
|
@ -82,28 +94,37 @@ void CScriptHandle::Set(void* ref, asITypeInfo* type) {
|
||||||
AddRefHandle();
|
AddRefHandle();
|
||||||
}
|
}
|
||||||
|
|
||||||
void* CScriptHandle::GetRef() { return m_ref; }
|
void *CScriptHandle::GetRef()
|
||||||
|
{
|
||||||
|
return m_ref;
|
||||||
|
}
|
||||||
|
|
||||||
asITypeInfo* CScriptHandle::GetType() const { return m_type; }
|
asITypeInfo *CScriptHandle::GetType() const
|
||||||
|
{
|
||||||
|
return m_type;
|
||||||
|
}
|
||||||
|
|
||||||
int CScriptHandle::GetTypeId() const {
|
int CScriptHandle::GetTypeId() const
|
||||||
if (m_type == 0)
|
{
|
||||||
return 0;
|
if( m_type == 0 ) return 0;
|
||||||
|
|
||||||
return m_type->GetTypeId() | asTYPEID_OBJHANDLE;
|
return m_type->GetTypeId() | asTYPEID_OBJHANDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method shouldn't be called from the application
|
// This method shouldn't be called from the application
|
||||||
// directly as it requires an active script context
|
// directly as it requires an active script context
|
||||||
CScriptHandle& CScriptHandle::Assign(void* ref, int typeId) {
|
CScriptHandle &CScriptHandle::Assign(void *ref, int typeId)
|
||||||
|
{
|
||||||
// When receiving a null handle we just clear our memory
|
// When receiving a null handle we just clear our memory
|
||||||
if (typeId == 0) {
|
if( typeId == 0 )
|
||||||
|
{
|
||||||
Set(0, 0);
|
Set(0, 0);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dereference received handles to get the object
|
// Dereference received handles to get the object
|
||||||
if (typeId & asTYPEID_OBJHANDLE) {
|
if( typeId & asTYPEID_OBJHANDLE )
|
||||||
|
{
|
||||||
// Store the actual reference
|
// Store the actual reference
|
||||||
ref = *(void**)ref;
|
ref = *(void**)ref;
|
||||||
typeId &= ~asTYPEID_OBJHANDLE;
|
typeId &= ~asTYPEID_OBJHANDLE;
|
||||||
|
@ -115,7 +136,8 @@ CScriptHandle& CScriptHandle::Assign(void* ref, int typeId) {
|
||||||
asITypeInfo *type = engine->GetTypeInfoById(typeId);
|
asITypeInfo *type = engine->GetTypeInfoById(typeId);
|
||||||
|
|
||||||
// If the argument is another CScriptHandle, we should copy the content instead
|
// If the argument is another CScriptHandle, we should copy the content instead
|
||||||
if (type && strcmp(type->GetName(), "ref") == 0) {
|
if( type && strcmp(type->GetName(), "ref") == 0 )
|
||||||
|
{
|
||||||
CScriptHandle *r = (CScriptHandle*)ref;
|
CScriptHandle *r = (CScriptHandle*)ref;
|
||||||
ref = r->m_ref;
|
ref = r->m_ref;
|
||||||
type = r->m_type;
|
type = r->m_type;
|
||||||
|
@ -126,8 +148,10 @@ CScriptHandle& CScriptHandle::Assign(void* ref, int typeId) {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CScriptHandle::operator==(const CScriptHandle& o) const {
|
bool CScriptHandle::operator==(const CScriptHandle &o) const
|
||||||
if (m_ref == o.m_ref && m_type == o.m_type)
|
{
|
||||||
|
if( m_ref == o.m_ref &&
|
||||||
|
m_type == o.m_type )
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// TODO: If type is not the same, we should attempt to do a dynamic cast,
|
// TODO: If type is not the same, we should attempt to do a dynamic cast,
|
||||||
|
@ -136,15 +160,20 @@ bool CScriptHandle::operator==(const CScriptHandle& o) const {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CScriptHandle::operator!=(const CScriptHandle& o) const { return !(*this == o); }
|
bool CScriptHandle::operator!=(const CScriptHandle &o) const
|
||||||
|
{
|
||||||
|
return !(*this == o);
|
||||||
|
}
|
||||||
|
|
||||||
bool CScriptHandle::Equals(void* ref, int typeId) const {
|
bool CScriptHandle::Equals(void *ref, int typeId) const
|
||||||
|
{
|
||||||
// Null handles are received as reference to a null handle
|
// Null handles are received as reference to a null handle
|
||||||
if( typeId == 0 )
|
if( typeId == 0 )
|
||||||
ref = 0;
|
ref = 0;
|
||||||
|
|
||||||
// Dereference handles to get the object
|
// Dereference handles to get the object
|
||||||
if (typeId & asTYPEID_OBJHANDLE) {
|
if( typeId & asTYPEID_OBJHANDLE )
|
||||||
|
{
|
||||||
// Compare the actual reference
|
// Compare the actual reference
|
||||||
ref = *(void**)ref;
|
ref = *(void**)ref;
|
||||||
typeId &= ~asTYPEID_OBJHANDLE;
|
typeId &= ~asTYPEID_OBJHANDLE;
|
||||||
|
@ -153,16 +182,17 @@ bool CScriptHandle::Equals(void* ref, int typeId) const {
|
||||||
// TODO: If typeId is not the same, we should attempt to do a dynamic cast,
|
// TODO: If typeId is not the same, we should attempt to do a dynamic cast,
|
||||||
// which may change the pointer for application registered classes
|
// which may change the pointer for application registered classes
|
||||||
|
|
||||||
if (ref == m_ref)
|
if( ref == m_ref ) return true;
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// AngelScript: used as '@obj = cast<obj>(ref);'
|
// AngelScript: used as '@obj = cast<obj>(ref);'
|
||||||
void CScriptHandle::Cast(void** outRef, int typeId) {
|
void CScriptHandle::Cast(void **outRef, int typeId)
|
||||||
|
{
|
||||||
// If we hold a null handle, then just return null
|
// If we hold a null handle, then just return null
|
||||||
if (m_type == 0) {
|
if( m_type == 0 )
|
||||||
|
{
|
||||||
*outRef = 0;
|
*outRef = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -181,7 +211,8 @@ void CScriptHandle::Cast(void** outRef, int typeId) {
|
||||||
engine->RefCastObject(m_ref, m_type, type, outRef);
|
engine->RefCastObject(m_ref, m_type, type, outRef);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CScriptHandle::EnumReferences(asIScriptEngine* inEngine) {
|
void CScriptHandle::EnumReferences(asIScriptEngine *inEngine)
|
||||||
|
{
|
||||||
// If we're holding a reference, we'll notify the garbage collector of it
|
// If we're holding a reference, we'll notify the garbage collector of it
|
||||||
if (m_ref)
|
if (m_ref)
|
||||||
inEngine->GCEnumCallback(m_ref);
|
inEngine->GCEnumCallback(m_ref);
|
||||||
|
@ -191,101 +222,80 @@ void CScriptHandle::EnumReferences(asIScriptEngine* inEngine) {
|
||||||
inEngine->GCEnumCallback(m_type);
|
inEngine->GCEnumCallback(m_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CScriptHandle::ReleaseReferences(asIScriptEngine*) {
|
void CScriptHandle::ReleaseReferences(asIScriptEngine *inEngine)
|
||||||
|
{
|
||||||
// Simply clear the content to release the references
|
// Simply clear the content to release the references
|
||||||
Set(0, 0);
|
Set(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RegisterScriptHandle_Native(asIScriptEngine* engine) {
|
void RegisterScriptHandle_Native(asIScriptEngine *engine)
|
||||||
[[maybe_unused]] int r;
|
{
|
||||||
|
int r;
|
||||||
|
|
||||||
#if AS_CAN_USE_CPP11
|
#if AS_CAN_USE_CPP11
|
||||||
// With C++11 it is possible to use asGetTypeTraits to automatically determine the flags that represent the C++
|
// With C++11 it is possible to use asGetTypeTraits to automatically determine the flags that represent the C++ class
|
||||||
// class
|
r = engine->RegisterObjectType("ref", sizeof(CScriptHandle), asOBJ_VALUE | asOBJ_ASHANDLE | asOBJ_GC | asGetTypeTraits<CScriptHandle>()); assert( r >= 0 );
|
||||||
r = engine->RegisterObjectType("ref", sizeof(CScriptHandle),
|
|
||||||
asOBJ_VALUE | asOBJ_ASHANDLE | asOBJ_GC | asGetTypeTraits<CScriptHandle>());
|
|
||||||
assert(r >= 0);
|
|
||||||
#else
|
#else
|
||||||
r = engine->RegisterObjectType("ref", sizeof(CScriptHandle),
|
r = engine->RegisterObjectType("ref", sizeof(CScriptHandle), asOBJ_VALUE | asOBJ_ASHANDLE | asOBJ_GC | asOBJ_APP_CLASS_CDAK); assert( r >= 0 );
|
||||||
asOBJ_VALUE | asOBJ_ASHANDLE | asOBJ_GC | asOBJ_APP_CLASS_CDAK);
|
|
||||||
assert(r >= 0);
|
|
||||||
#endif
|
#endif
|
||||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f()",
|
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f()", asFUNCTIONPR(Construct, (CScriptHandle *), void), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
|
||||||
asFUNCTIONPR(Construct, (CScriptHandle*), void), asCALL_CDECL_OBJFIRST);
|
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ref &in)", asFUNCTIONPR(Construct, (CScriptHandle *, const CScriptHandle &), void), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
|
||||||
assert(r >= 0);
|
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ?&in)", asFUNCTIONPR(Construct, (CScriptHandle *, void *, int), void), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
|
||||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ref &in)",
|
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_DESTRUCT, "void f()", asFUNCTIONPR(Destruct, (CScriptHandle *), void), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
|
||||||
asFUNCTIONPR(Construct, (CScriptHandle*, const CScriptHandle&), void),
|
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(CScriptHandle,EnumReferences), asCALL_THISCALL); assert(r >= 0);
|
||||||
asCALL_CDECL_OBJFIRST);
|
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(CScriptHandle, ReleaseReferences), asCALL_THISCALL); assert(r >= 0);
|
||||||
assert(r >= 0);
|
r = engine->RegisterObjectMethod("ref", "void opCast(?&out)", asMETHODPR(CScriptHandle, Cast, (void **, int), void), asCALL_THISCALL); assert( r >= 0 );
|
||||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ?&in)",
|
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ref &in)", asMETHOD(CScriptHandle, operator=), asCALL_THISCALL); assert( r >= 0 );
|
||||||
asFUNCTIONPR(Construct, (CScriptHandle*, void*, int), void),
|
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ?&in)", asMETHOD(CScriptHandle, Assign), asCALL_THISCALL); assert( r >= 0 );
|
||||||
asCALL_CDECL_OBJFIRST);
|
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ref &in) const", asMETHODPR(CScriptHandle, operator==, (const CScriptHandle &) const, bool), asCALL_THISCALL); assert( r >= 0 );
|
||||||
assert(r >= 0);
|
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ?&in) const", asMETHODPR(CScriptHandle, Equals, (void*, int) const, bool), asCALL_THISCALL); assert( r >= 0 );
|
||||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_DESTRUCT, "void f()",
|
|
||||||
asFUNCTIONPR(Destruct, (CScriptHandle*), void), asCALL_CDECL_OBJFIRST);
|
|
||||||
assert(r >= 0);
|
|
||||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_ENUMREFS, "void f(int&in)",
|
|
||||||
asMETHOD(CScriptHandle, EnumReferences), asCALL_THISCALL);
|
|
||||||
assert(r >= 0);
|
|
||||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_RELEASEREFS, "void f(int&in)",
|
|
||||||
asMETHOD(CScriptHandle, ReleaseReferences), asCALL_THISCALL);
|
|
||||||
assert(r >= 0);
|
|
||||||
r = engine->RegisterObjectMethod("ref", "void opCast(?&out)", asMETHODPR(CScriptHandle, Cast, (void**, int), void),
|
|
||||||
asCALL_THISCALL);
|
|
||||||
assert(r >= 0);
|
|
||||||
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ref &in)", asMETHOD(CScriptHandle, operator=),
|
|
||||||
asCALL_THISCALL);
|
|
||||||
assert(r >= 0);
|
|
||||||
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ?&in)", asMETHOD(CScriptHandle, Assign),
|
|
||||||
asCALL_THISCALL);
|
|
||||||
assert(r >= 0);
|
|
||||||
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ref &in) const",
|
|
||||||
asMETHODPR(CScriptHandle, operator==,(const CScriptHandle&) const, bool),
|
|
||||||
asCALL_THISCALL);
|
|
||||||
assert(r >= 0);
|
|
||||||
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ?&in) const",
|
|
||||||
asMETHODPR(CScriptHandle, Equals, (void*, int)const, bool), asCALL_THISCALL);
|
|
||||||
assert(r >= 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CScriptHandle_Construct_Generic(asIScriptGeneric* gen) {
|
void CScriptHandle_Construct_Generic(asIScriptGeneric *gen)
|
||||||
|
{
|
||||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||||
new(self) CScriptHandle();
|
new(self) CScriptHandle();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CScriptHandle_ConstructCopy_Generic(asIScriptGeneric* gen) {
|
void CScriptHandle_ConstructCopy_Generic(asIScriptGeneric *gen)
|
||||||
|
{
|
||||||
CScriptHandle *other = reinterpret_cast<CScriptHandle*>(gen->GetArgAddress(0));
|
CScriptHandle *other = reinterpret_cast<CScriptHandle*>(gen->GetArgAddress(0));
|
||||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||||
new(self) CScriptHandle(*other);
|
new(self) CScriptHandle(*other);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CScriptHandle_ConstructVar_Generic(asIScriptGeneric* gen) {
|
void CScriptHandle_ConstructVar_Generic(asIScriptGeneric *gen)
|
||||||
|
{
|
||||||
void *ref = gen->GetArgAddress(0);
|
void *ref = gen->GetArgAddress(0);
|
||||||
int typeId = gen->GetArgTypeId(0);
|
int typeId = gen->GetArgTypeId(0);
|
||||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||||
Construct(self, ref, typeId);
|
Construct(self, ref, typeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CScriptHandle_Destruct_Generic(asIScriptGeneric* gen) {
|
void CScriptHandle_Destruct_Generic(asIScriptGeneric *gen)
|
||||||
|
{
|
||||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||||
self->~CScriptHandle();
|
self->~CScriptHandle();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CScriptHandle_Cast_Generic(asIScriptGeneric* gen) {
|
void CScriptHandle_Cast_Generic(asIScriptGeneric *gen)
|
||||||
|
{
|
||||||
void **ref = reinterpret_cast<void**>(gen->GetArgAddress(0));
|
void **ref = reinterpret_cast<void**>(gen->GetArgAddress(0));
|
||||||
int typeId = gen->GetArgTypeId(0);
|
int typeId = gen->GetArgTypeId(0);
|
||||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||||
self->Cast(ref, typeId);
|
self->Cast(ref, typeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CScriptHandle_Assign_Generic(asIScriptGeneric* gen) {
|
void CScriptHandle_Assign_Generic(asIScriptGeneric *gen)
|
||||||
|
{
|
||||||
CScriptHandle *other = reinterpret_cast<CScriptHandle*>(gen->GetArgAddress(0));
|
CScriptHandle *other = reinterpret_cast<CScriptHandle*>(gen->GetArgAddress(0));
|
||||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||||
*self = *other;
|
*self = *other;
|
||||||
gen->SetReturnAddress(self);
|
gen->SetReturnAddress(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CScriptHandle_AssignVar_Generic(asIScriptGeneric* gen) {
|
void CScriptHandle_AssignVar_Generic(asIScriptGeneric *gen)
|
||||||
|
{
|
||||||
void *ref = gen->GetArgAddress(0);
|
void *ref = gen->GetArgAddress(0);
|
||||||
int typeId = gen->GetArgTypeId(0);
|
int typeId = gen->GetArgTypeId(0);
|
||||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||||
|
@ -293,75 +303,58 @@ void CScriptHandle_AssignVar_Generic(asIScriptGeneric* gen) {
|
||||||
gen->SetReturnAddress(self);
|
gen->SetReturnAddress(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CScriptHandle_Equals_Generic(asIScriptGeneric* gen) {
|
void CScriptHandle_Equals_Generic(asIScriptGeneric *gen)
|
||||||
|
{
|
||||||
CScriptHandle *other = reinterpret_cast<CScriptHandle*>(gen->GetArgAddress(0));
|
CScriptHandle *other = reinterpret_cast<CScriptHandle*>(gen->GetArgAddress(0));
|
||||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||||
gen->SetReturnByte(*self == *other);
|
gen->SetReturnByte(*self == *other);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CScriptHandle_EqualsVar_Generic(asIScriptGeneric* gen) {
|
void CScriptHandle_EqualsVar_Generic(asIScriptGeneric *gen)
|
||||||
|
{
|
||||||
void *ref = gen->GetArgAddress(0);
|
void *ref = gen->GetArgAddress(0);
|
||||||
int typeId = gen->GetArgTypeId(0);
|
int typeId = gen->GetArgTypeId(0);
|
||||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||||
gen->SetReturnByte(self->Equals(ref, typeId));
|
gen->SetReturnByte(self->Equals(ref, typeId));
|
||||||
}
|
}
|
||||||
|
|
||||||
void CScriptHandle_EnumReferences_Generic(asIScriptGeneric* gen) {
|
void CScriptHandle_EnumReferences_Generic(asIScriptGeneric *gen)
|
||||||
|
{
|
||||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||||
self->EnumReferences(gen->GetEngine());
|
self->EnumReferences(gen->GetEngine());
|
||||||
}
|
}
|
||||||
|
|
||||||
void CScriptHandle_ReleaseReferences_Generic(asIScriptGeneric* gen) {
|
void CScriptHandle_ReleaseReferences_Generic(asIScriptGeneric *gen)
|
||||||
|
{
|
||||||
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
CScriptHandle *self = reinterpret_cast<CScriptHandle*>(gen->GetObject());
|
||||||
self->ReleaseReferences(gen->GetEngine());
|
self->ReleaseReferences(gen->GetEngine());
|
||||||
}
|
}
|
||||||
|
|
||||||
void RegisterScriptHandle_Generic(asIScriptEngine* engine) {
|
void RegisterScriptHandle_Generic(asIScriptEngine *engine)
|
||||||
[[maybe_unused]] int r;
|
{
|
||||||
|
int r;
|
||||||
|
|
||||||
r = engine->RegisterObjectType("ref", sizeof(CScriptHandle),
|
r = engine->RegisterObjectType("ref", sizeof(CScriptHandle), asOBJ_VALUE | asOBJ_ASHANDLE | asOBJ_GC | asOBJ_APP_CLASS_CDAK); assert( r >= 0 );
|
||||||
asOBJ_VALUE | asOBJ_ASHANDLE | asOBJ_GC | asOBJ_APP_CLASS_CDAK);
|
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(CScriptHandle_Construct_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||||
assert(r >= 0);
|
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ref &in)", asFUNCTION(CScriptHandle_ConstructCopy_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f()",
|
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ?&in)", asFUNCTION(CScriptHandle_ConstructVar_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||||
asFUNCTION(CScriptHandle_Construct_Generic), asCALL_GENERIC);
|
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(CScriptHandle_Destruct_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||||
assert(r >= 0);
|
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_ENUMREFS, "void f(int&in)", asFUNCTION(CScriptHandle_EnumReferences_Generic), asCALL_GENERIC); assert(r >= 0);
|
||||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ref &in)",
|
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_RELEASEREFS, "void f(int&in)", asFUNCTION(CScriptHandle_ReleaseReferences_Generic), asCALL_GENERIC); assert(r >= 0);
|
||||||
asFUNCTION(CScriptHandle_ConstructCopy_Generic), asCALL_GENERIC);
|
r = engine->RegisterObjectMethod("ref", "void opCast(?&out)", asFUNCTION(CScriptHandle_Cast_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||||
assert(r >= 0);
|
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ref &in)", asFUNCTION(CScriptHandle_Assign_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_CONSTRUCT, "void f(const ?&in)",
|
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ?&in)", asFUNCTION(CScriptHandle_AssignVar_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||||
asFUNCTION(CScriptHandle_ConstructVar_Generic), asCALL_GENERIC);
|
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ref &in) const", asFUNCTION(CScriptHandle_Equals_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||||
assert(r >= 0);
|
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ?&in) const", asFUNCTION(CScriptHandle_EqualsVar_Generic), asCALL_GENERIC); assert( r >= 0 );
|
||||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_DESTRUCT, "void f()",
|
|
||||||
asFUNCTION(CScriptHandle_Destruct_Generic), asCALL_GENERIC);
|
|
||||||
assert(r >= 0);
|
|
||||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_ENUMREFS, "void f(int&in)",
|
|
||||||
asFUNCTION(CScriptHandle_EnumReferences_Generic), asCALL_GENERIC);
|
|
||||||
assert(r >= 0);
|
|
||||||
r = engine->RegisterObjectBehaviour("ref", asBEHAVE_RELEASEREFS, "void f(int&in)",
|
|
||||||
asFUNCTION(CScriptHandle_ReleaseReferences_Generic), asCALL_GENERIC);
|
|
||||||
assert(r >= 0);
|
|
||||||
r = engine->RegisterObjectMethod("ref", "void opCast(?&out)", asFUNCTION(CScriptHandle_Cast_Generic),
|
|
||||||
asCALL_GENERIC);
|
|
||||||
assert(r >= 0);
|
|
||||||
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ref &in)",
|
|
||||||
asFUNCTION(CScriptHandle_Assign_Generic), asCALL_GENERIC);
|
|
||||||
assert(r >= 0);
|
|
||||||
r = engine->RegisterObjectMethod("ref", "ref &opHndlAssign(const ?&in)",
|
|
||||||
asFUNCTION(CScriptHandle_AssignVar_Generic), asCALL_GENERIC);
|
|
||||||
assert(r >= 0);
|
|
||||||
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ref &in) const",
|
|
||||||
asFUNCTION(CScriptHandle_Equals_Generic), asCALL_GENERIC);
|
|
||||||
assert(r >= 0);
|
|
||||||
r = engine->RegisterObjectMethod("ref", "bool opEquals(const ?&in) const",
|
|
||||||
asFUNCTION(CScriptHandle_EqualsVar_Generic), asCALL_GENERIC);
|
|
||||||
assert(r >= 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RegisterScriptHandle(asIScriptEngine* engine) {
|
void RegisterScriptHandle(asIScriptEngine *engine)
|
||||||
|
{
|
||||||
if( strstr(asGetLibraryOptions(), "AS_MAX_PORTABILITY") )
|
if( strstr(asGetLibraryOptions(), "AS_MAX_PORTABILITY") )
|
||||||
RegisterScriptHandle_Generic(engine);
|
RegisterScriptHandle_Generic(engine);
|
||||||
else
|
else
|
||||||
RegisterScriptHandle_Native(engine);
|
RegisterScriptHandle_Native(engine);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
END_AS_NAMESPACE
|
END_AS_NAMESPACE
|
||||||
|
|
|
@ -975,7 +975,6 @@ string ScriptGetExceptionInfo()
|
||||||
|
|
||||||
void RegisterExceptionRoutines(asIScriptEngine *engine)
|
void RegisterExceptionRoutines(asIScriptEngine *engine)
|
||||||
{
|
{
|
||||||
[[maybe_unused]]
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
// The string type must be available
|
// The string type must be available
|
||||||
|
|
|
@ -28,25 +28,26 @@ typedef map<string, int> map_t;
|
||||||
END_AS_NAMESPACE
|
END_AS_NAMESPACE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
BEGIN_AS_NAMESPACE
|
||||||
class CStdStringFactory : public asIStringFactory
|
class CStdStringFactory : public asIStringFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CStdStringFactory() = default;
|
CStdStringFactory() {}
|
||||||
~CStdStringFactory() override
|
~CStdStringFactory()
|
||||||
{
|
{
|
||||||
// The script engine must release each string
|
// The script engine must release each string
|
||||||
// constant that it has requested
|
// constant that it has requested
|
||||||
assert(stringCache.empty());
|
assert(stringCache.size() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
const void *GetStringConstant(const char *data, asUINT length) override
|
const void *GetStringConstant(const char *data, asUINT length)
|
||||||
{
|
{
|
||||||
// The string factory might be modified from multiple
|
// The string factory might be modified from multiple
|
||||||
// threads, so it is necessary to use a mutex.
|
// threads, so it is necessary to use a mutex.
|
||||||
asAcquireExclusiveLock();
|
asAcquireExclusiveLock();
|
||||||
|
|
||||||
string str(data, length);
|
string str(data, length);
|
||||||
auto it = stringCache.find(str);
|
map_t::iterator it = stringCache.find(str);
|
||||||
if (it != stringCache.end())
|
if (it != stringCache.end())
|
||||||
it->second++;
|
it->second++;
|
||||||
else
|
else
|
||||||
|
@ -57,9 +58,9 @@ public:
|
||||||
return reinterpret_cast<const void*>(&it->first);
|
return reinterpret_cast<const void*>(&it->first);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ReleaseStringConstant(const void *str) override
|
int ReleaseStringConstant(const void *str)
|
||||||
{
|
{
|
||||||
if (str == nullptr)
|
if (str == 0)
|
||||||
return asERROR;
|
return asERROR;
|
||||||
|
|
||||||
int ret = asSUCCESS;
|
int ret = asSUCCESS;
|
||||||
|
@ -68,7 +69,7 @@ public:
|
||||||
// threads, so it is necessary to use a mutex.
|
// threads, so it is necessary to use a mutex.
|
||||||
asAcquireExclusiveLock();
|
asAcquireExclusiveLock();
|
||||||
|
|
||||||
auto it = stringCache.find(*reinterpret_cast<const string*>(str));
|
map_t::iterator it = stringCache.find(*reinterpret_cast<const string*>(str));
|
||||||
if (it == stringCache.end())
|
if (it == stringCache.end())
|
||||||
ret = asERROR;
|
ret = asERROR;
|
||||||
else
|
else
|
||||||
|
@ -83,9 +84,9 @@ public:
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int GetRawStringData(const void *str, char *data, asUINT *length) const override
|
int GetRawStringData(const void *str, char *data, asUINT *length) const
|
||||||
{
|
{
|
||||||
if (str == nullptr)
|
if (str == 0)
|
||||||
return asERROR;
|
return asERROR;
|
||||||
|
|
||||||
if (length)
|
if (length)
|
||||||
|
@ -98,17 +99,17 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
// THe access to the string cache is protected with the common mutex provided by AngelScript
|
// THe access to the string cache is protected with the common mutex provided by AngelScript
|
||||||
map_t stringCache = {};
|
map_t stringCache;
|
||||||
};
|
};
|
||||||
|
|
||||||
static CStdStringFactory *stringFactory = nullptr;
|
static CStdStringFactory *stringFactory = 0;
|
||||||
|
|
||||||
// TODO: Make this public so the application can also use the string
|
// TODO: Make this public so the application can also use the string
|
||||||
// factory and share the string constants if so desired, or to
|
// factory and share the string constants if so desired, or to
|
||||||
// monitor the size of the string factory cache.
|
// monitor the size of the string factory cache.
|
||||||
CStdStringFactory *GetStdStringFactorySingleton()
|
CStdStringFactory *GetStdStringFactorySingleton()
|
||||||
{
|
{
|
||||||
if( stringFactory == nullptr )
|
if( stringFactory == 0 )
|
||||||
{
|
{
|
||||||
// The following instance will be destroyed by the global
|
// The following instance will be destroyed by the global
|
||||||
// CStdStringFactoryCleaner instance upon application shutdown
|
// CStdStringFactoryCleaner instance upon application shutdown
|
||||||
|
@ -122,7 +123,7 @@ class CStdStringFactoryCleaner
|
||||||
public:
|
public:
|
||||||
~CStdStringFactoryCleaner()
|
~CStdStringFactoryCleaner()
|
||||||
{
|
{
|
||||||
if (stringFactory != nullptr)
|
if (stringFactory)
|
||||||
{
|
{
|
||||||
// Only delete the string factory if the stringCache is empty
|
// Only delete the string factory if the stringCache is empty
|
||||||
// If it is not empty, it means that someone might still attempt
|
// If it is not empty, it means that someone might still attempt
|
||||||
|
@ -133,13 +134,13 @@ public:
|
||||||
if (stringFactory->stringCache.empty())
|
if (stringFactory->stringCache.empty())
|
||||||
{
|
{
|
||||||
delete stringFactory;
|
delete stringFactory;
|
||||||
stringFactory = nullptr;
|
stringFactory = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static CStdStringFactoryCleaner cleaner = {};
|
static CStdStringFactoryCleaner cleaner;
|
||||||
|
|
||||||
|
|
||||||
static void ConstructString(string *thisPointer)
|
static void ConstructString(string *thisPointer)
|
||||||
|
@ -461,12 +462,12 @@ static void StringResize(asUINT l, string &str)
|
||||||
// string formatInt(int64 val, const string &in options, uint width)
|
// string formatInt(int64 val, const string &in options, uint width)
|
||||||
static string formatInt(asINT64 value, const string &options, asUINT width)
|
static string formatInt(asINT64 value, const string &options, asUINT width)
|
||||||
{
|
{
|
||||||
bool leftJustify = options.find('l') != string::npos;
|
bool leftJustify = options.find("l") != string::npos;
|
||||||
bool padWithZero = options.find('0') != string::npos;
|
bool padWithZero = options.find("0") != string::npos;
|
||||||
bool alwaysSign = options.find('+') != string::npos;
|
bool alwaysSign = options.find("+") != string::npos;
|
||||||
bool spaceOnSign = options.find(' ') != string::npos;
|
bool spaceOnSign = options.find(" ") != string::npos;
|
||||||
bool hexSmall = options.find('h') != string::npos;
|
bool hexSmall = options.find("h") != string::npos;
|
||||||
bool hexLarge = options.find('H') != string::npos;
|
bool hexLarge = options.find("H") != string::npos;
|
||||||
|
|
||||||
string fmt = "%";
|
string fmt = "%";
|
||||||
if( leftJustify ) fmt += "-";
|
if( leftJustify ) fmt += "-";
|
||||||
|
@ -505,12 +506,12 @@ static string formatInt(asINT64 value, const string &options, asUINT width)
|
||||||
// string formatUInt(uint64 val, const string &in options, uint width)
|
// string formatUInt(uint64 val, const string &in options, uint width)
|
||||||
static string formatUInt(asQWORD value, const string &options, asUINT width)
|
static string formatUInt(asQWORD value, const string &options, asUINT width)
|
||||||
{
|
{
|
||||||
bool leftJustify = options.find('l') != string::npos;
|
bool leftJustify = options.find("l") != string::npos;
|
||||||
bool padWithZero = options.find('0') != string::npos;
|
bool padWithZero = options.find("0") != string::npos;
|
||||||
bool alwaysSign = options.find('+') != string::npos;
|
bool alwaysSign = options.find("+") != string::npos;
|
||||||
bool spaceOnSign = options.find(' ') != string::npos;
|
bool spaceOnSign = options.find(" ") != string::npos;
|
||||||
bool hexSmall = options.find('h') != string::npos;
|
bool hexSmall = options.find("h") != string::npos;
|
||||||
bool hexLarge = options.find('H') != string::npos;
|
bool hexLarge = options.find("H") != string::npos;
|
||||||
|
|
||||||
string fmt = "%";
|
string fmt = "%";
|
||||||
if( leftJustify ) fmt += "-";
|
if( leftJustify ) fmt += "-";
|
||||||
|
@ -549,12 +550,12 @@ static string formatUInt(asQWORD value, const string &options, asUINT width)
|
||||||
// string formatFloat(double val, const string &in options, uint width, uint precision)
|
// string formatFloat(double val, const string &in options, uint width, uint precision)
|
||||||
static string formatFloat(double value, const string &options, asUINT width, asUINT precision)
|
static string formatFloat(double value, const string &options, asUINT width, asUINT precision)
|
||||||
{
|
{
|
||||||
bool leftJustify = options.find('l') != string::npos;
|
bool leftJustify = options.find("l") != string::npos;
|
||||||
bool padWithZero = options.find('0') != string::npos;
|
bool padWithZero = options.find("0") != string::npos;
|
||||||
bool alwaysSign = options.find('+') != string::npos;
|
bool alwaysSign = options.find("+") != string::npos;
|
||||||
bool spaceOnSign = options.find(' ') != string::npos;
|
bool spaceOnSign = options.find(" ") != string::npos;
|
||||||
bool expSmall = options.find('e') != string::npos;
|
bool expSmall = options.find("e") != string::npos;
|
||||||
bool expLarge = options.find('E') != string::npos;
|
bool expLarge = options.find("E") != string::npos;
|
||||||
|
|
||||||
string fmt = "%";
|
string fmt = "%";
|
||||||
if( leftJustify ) fmt += "-";
|
if( leftJustify ) fmt += "-";
|
||||||
|
|
|
@ -112,7 +112,6 @@ static void StringJoin_Generic(asIScriptGeneric *gen)
|
||||||
// The string type must have been registered first.
|
// The string type must have been registered first.
|
||||||
void RegisterStdStringUtils(asIScriptEngine *engine)
|
void RegisterStdStringUtils(asIScriptEngine *engine)
|
||||||
{
|
{
|
||||||
[[maybe_unused]]
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
if( strstr(asGetLibraryOptions(), "AS_MAX_PORTABILITY") )
|
if( strstr(asGetLibraryOptions(), "AS_MAX_PORTABILITY") )
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "AngelScriptResolver.hpp"
|
#include "AngelScriptResolver.hpp"
|
||||||
#include <CreatureLib/Battling/Models/Creature.hpp>
|
#include <CreatureLib/Battling/Models/Creature.hpp>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
#include "../../../extern/angelscript_addons/scriptdictionary/scriptdictionary.h"
|
||||||
#include "../../../extern/angelscript_addons/scripthandle/scripthandle.h"
|
#include "../../../extern/angelscript_addons/scripthandle/scripthandle.h"
|
||||||
#include "../../../extern/angelscript_addons/scripthelper/scripthelper.h"
|
#include "../../../extern/angelscript_addons/scripthelper/scripthelper.h"
|
||||||
#include "../../../extern/angelscript_addons/scriptstdstring/scriptstdstring.h"
|
#include "../../../extern/angelscript_addons/scriptstdstring/scriptstdstring.h"
|
||||||
|
@ -76,6 +77,7 @@ void AngelScriptResolver::Initialize(CreatureLib::Battling::BattleLibrary* arg,
|
||||||
// Register Script Array type
|
// Register Script Array type
|
||||||
RegisterScriptArray(_engine, true);
|
RegisterScriptArray(_engine, true);
|
||||||
RegisterScriptHandle(_engine);
|
RegisterScriptHandle(_engine);
|
||||||
|
RegisterScriptDictionary(_engine);
|
||||||
r = _engine->RegisterGlobalFunction("void print(const string &in)", asFUNCTION(Print), asCALL_CDECL);
|
r = _engine->RegisterGlobalFunction("void print(const string &in)", asFUNCTION(Print), asCALL_CDECL);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
throw ArbUt::Exception("Registering print function failed.");
|
throw ArbUt::Exception("Registering print function failed.");
|
||||||
|
|
Loading…
Reference in New Issue