fallout2-ce/src/sfall_arrays.cc

168 lines
3.5 KiB
C++
Raw Normal View History

2023-04-19 16:23:33 -07:00
#include "sfall_arrays.h"
2023-04-19 13:13:02 -07:00
#include "interpreter.h"
2023-04-19 16:23:33 -07:00
#include "sfall_script_value.h"
2023-04-19 13:13:02 -07:00
#include <cstdint>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace fallout {
static ArrayId nextArrayID = 1;
static ArrayId stackArrayId = 1;
#define ARRAY_MAX_STRING (255) // maximum length of string to be stored as array key or value
#define ARRAY_MAX_SIZE (100000) // maximum number of array elements,
class SFallArray {
private:
public:
uint32_t flags;
SFallArray() = delete;
SFallArray(unsigned int len, uint32_t flags)
: flags(flags)
{
data.resize(len);
}
2023-04-19 15:39:30 -07:00
std::vector<SFallScriptValue> data;
2023-04-19 15:20:05 -07:00
int size()
{
return data.size();
}
2023-04-19 13:13:02 -07:00
};
2023-04-19 14:58:30 -07:00
using ArraysMap = std::unordered_map<ArrayId, SFallArray>;
ArraysMap arrays;
std::unordered_set<ArrayId> temporaryArrays;
2023-04-19 13:13:02 -07:00
ArrayId CreateArray(int len, uint32_t flags)
{
flags = (flags & ~1); // reset 1 bit
if (len < 0) {
2023-04-19 16:23:33 -07:00
flags |= SFALL_ARRAYFLAG_ASSOC;
2023-04-19 16:51:20 -07:00
// TODO: Implement
2023-04-19 13:13:02 -07:00
throw(std::invalid_argument("Not implemented yet"));
};
if (len > ARRAY_MAX_SIZE) {
len = ARRAY_MAX_SIZE; // safecheck
}
ArrayId array_id = nextArrayID++;
stackArrayId = array_id;
2023-04-19 14:58:30 -07:00
arrays.emplace(std::make_pair(array_id, SFallArray { len, flags }));
2023-04-19 13:13:02 -07:00
return array_id;
}
ArrayId CreateTempArray(int len, uint32_t flags)
{
ArrayId array_id = CreateArray(len, flags);
temporaryArrays.insert(array_id);
return array_id;
}
2023-04-19 15:20:05 -07:00
static SFallArray* get_array_by_id(ArrayId array_id)
{
auto iter = arrays.find(array_id);
if (iter == arrays.end()) {
return nullptr;
};
return &iter->second;
}
ProgramValue GetArrayKey(ArrayId array_id, int index)
{
auto arr = get_array_by_id(array_id);
if (arr == nullptr || index < -1 || index > arr->size()) {
2023-04-19 15:39:30 -07:00
return SFallScriptValue(0);
2023-04-19 15:20:05 -07:00
};
if (index == -1) { // special index to indicate if array is associative
throw(std::invalid_argument("Not implemented yet"));
};
2023-04-19 16:51:20 -07:00
// TODO: assoc
2023-04-19 15:39:30 -07:00
return SFallScriptValue(index);
2023-04-19 15:20:05 -07:00
}
2023-04-19 15:21:56 -07:00
int LenArray(ArrayId array_id)
{
auto arr = get_array_by_id(array_id);
if (arr == nullptr) {
return -1;
};
2023-04-19 16:51:20 -07:00
// TODO: assoc
2023-04-19 15:21:56 -07:00
return arr->size();
}
2023-04-19 16:26:37 -07:00
ProgramValue GetArray(ArrayId array_id, const SFallScriptValue& key)
2023-04-19 15:39:30 -07:00
{
2023-04-19 16:49:22 -07:00
// TODO: If type is string then do substr
2023-04-19 15:39:30 -07:00
auto arr = get_array_by_id(array_id);
if (arr == nullptr) {
return SFallScriptValue(0);
};
// TODO assoc
2023-04-19 16:26:37 -07:00
auto element_index = key.asInt();
2023-04-19 15:39:30 -07:00
if (element_index < 0 || element_index >= arr->size()) {
return SFallScriptValue(0);
};
return arr->data[element_index];
}
2023-04-19 16:23:33 -07:00
void SetArray(ArrayId array_id, const SFallScriptValue& key, const SFallScriptValue& val, bool allowUnset)
{
auto arr = get_array_by_id(array_id);
if (arr == nullptr) {
return;
};
2023-04-19 16:51:20 -07:00
// TODO: assoc
2023-04-19 16:23:33 -07:00
if (key.isInt()) {
auto index = key.asInt();
if (index >= 0 && index < arr->size()) {
arr->data[index] = key;
}
}
}
2023-04-19 16:46:24 -07:00
void FreeArray(ArrayId array_id)
{
// TODO: remove from saved_arrays
arrays.erase(array_id);
}
2023-04-19 16:53:49 -07:00
void FixArray(ArrayId id)
{
temporaryArrays.erase(id);
}
2023-04-19 16:46:24 -07:00
void DeleteAllTempArrays()
{
for (auto it = temporaryArrays.begin(); it != temporaryArrays.end(); ++it) {
FreeArray(*it);
}
temporaryArrays.clear();
}
2023-04-19 23:34:53 -07:00
void sfallArraysReset()
{
temporaryArrays.clear();
arrays.clear();
nextArrayID = 1;
stackArrayId = 1;
}
2023-04-19 13:13:02 -07:00
}