mirror of
https://github.com/sfall-team/sfall.git
synced 2026-07-27 16:52:34 -07:00
Fixed a crash bug when using sorting functions on an associative array(map) (from Mr.Stalin)
This commit is contained in:
+18
-2
@@ -2,6 +2,7 @@
|
||||
#include <algorithm>
|
||||
|
||||
#include "Arrays.h"
|
||||
#include "FalloutEngine.h"
|
||||
#include "ScriptExtender.h"
|
||||
|
||||
/*
|
||||
@@ -288,6 +289,7 @@ void SaveArrays(HANDLE h) {
|
||||
int GetNumArrays() {
|
||||
return arrays.size();
|
||||
}
|
||||
|
||||
void GetArrays(int* _arrays) {
|
||||
int pos = 0;
|
||||
array_citr itr = arrays.begin();
|
||||
@@ -298,11 +300,13 @@ void GetArrays(int* _arrays) {
|
||||
itr++;
|
||||
}
|
||||
}
|
||||
|
||||
// those too are not really used yet in FalloutClient (AFAIK) -- phobos2077
|
||||
void DEGetArray(int id, DWORD* types, void* data) {
|
||||
//memcpy(types, arrays[id].types, arrays[id].len*4);
|
||||
//memcpy(data, arrays[id].data, arrays[id].len*arrays[id].datalen);
|
||||
}
|
||||
|
||||
void DESetArray(int id, const DWORD* types, const void* data) {
|
||||
//if (types) memcpy(arrays[id].types, types, arrays[id].len * 4);
|
||||
//memcpy(arrays[id].data, data, arrays[id].len*arrays[id].datalen);
|
||||
@@ -372,11 +376,13 @@ DWORD _stdcall CreateArray(int len, DWORD nothing) {
|
||||
arrays[nextarrayid] = var;
|
||||
return nextarrayid++;
|
||||
}
|
||||
|
||||
DWORD _stdcall TempArray(DWORD len, DWORD nothing) {
|
||||
DWORD id = CreateArray(len, nothing);
|
||||
tempArrays.insert(id);
|
||||
return id;
|
||||
}
|
||||
|
||||
void _stdcall FreeArray(DWORD id) {
|
||||
array_itr it = arrays.find(id);
|
||||
if (it != arrays.end()) {
|
||||
@@ -486,16 +492,18 @@ void _stdcall SetArray(DWORD id, DWORD key, DWORD keyType, DWORD val, DWORD valT
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int _stdcall LenArray(DWORD id) {
|
||||
if (arrays.find(id)==arrays.end()) return -1;
|
||||
else return arrays[id].size();
|
||||
}
|
||||
|
||||
void _stdcall ResizeArray(DWORD id, int newlen) {
|
||||
if (arrays.find(id) == arrays.end() || arrays[id].size() == newlen) return;
|
||||
if (newlen == -1 || arrays.find(id) == arrays.end() || arrays[id].size() == newlen) return;
|
||||
sArrayVar &arr = arrays[id];
|
||||
if (arr.isAssoc()) {
|
||||
// only allow to reduce number of elements (adding range of elements is meaningless for maps)
|
||||
if (newlen < arrays[id].size()) {
|
||||
if (newlen >= 0 && newlen < arrays[id].size()) {
|
||||
ArrayKeysMap::iterator itHash;
|
||||
std::vector<sArrayElement>::iterator itVal;
|
||||
int actualLen = newlen * 2;
|
||||
@@ -505,6 +513,8 @@ void _stdcall ResizeArray(DWORD id, int newlen) {
|
||||
}
|
||||
arr.clearRange(actualLen);
|
||||
arr.val.resize(actualLen);
|
||||
} else if (newlen < 0) {
|
||||
DebugPrintf("\nOPCODE ERROR: resize_array() - array sorting error.");
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -515,6 +525,10 @@ void _stdcall ResizeArray(DWORD id, int newlen) {
|
||||
arr.clearRange(newlen);
|
||||
arr.val.resize(newlen);
|
||||
} else { // special functions for lists...
|
||||
if (newlen < ARRAY_ACTION_SHUFFLE) {
|
||||
DebugPrintf("\nOPCODE ERROR: resize_array() - array sorting error.");
|
||||
return;
|
||||
}
|
||||
switch (newlen) {
|
||||
case ARRAY_ACTION_SORT: // sort ascending
|
||||
std::sort(arr.val.begin(), arr.val.end());
|
||||
@@ -531,9 +545,11 @@ void _stdcall ResizeArray(DWORD id, int newlen) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _stdcall FixArray(DWORD id) {
|
||||
tempArrays.erase(id);
|
||||
}
|
||||
|
||||
int _stdcall ScanArray(DWORD id, DWORD val, DWORD datatype, DWORD* resultType) {
|
||||
*resultType = VAR_TYPE_INT;
|
||||
datatype = getSfallTypeByScriptType(datatype);
|
||||
|
||||
@@ -94,6 +94,7 @@ public:
|
||||
bool isAssoc() const {
|
||||
return (flags & ARRAYFLAG_ASSOC);
|
||||
}
|
||||
|
||||
// logical array size (number of elements for normal arrays; number of key=>value pairs for associative)
|
||||
int size() const {
|
||||
return isAssoc()
|
||||
@@ -109,6 +110,7 @@ public:
|
||||
}
|
||||
|
||||
sArrayVar() : flags(0), key() {}
|
||||
|
||||
// free memory used by strings
|
||||
void clear() {
|
||||
clearRange(0);
|
||||
|
||||
Reference in New Issue
Block a user