/* ** table.cpp ** ** This file is part of mkxp. ** ** Copyright (C) 2013 - 2021 Amaryllis Kulla ** ** mkxp is free software: you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by ** the Free Software Foundation, either version 2 of the License, or ** (at your option) any later version. ** ** mkxp is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with mkxp. If not, see . */ #include "table.h" #include #include #include "serial-util.h" #include "exception.h" #include "util.h" #ifdef MKXPZ_RETRO # include "sandbox-serial-util.h" static uint64_t next_id = 1; #endif // MKXPZ_RETRO /* Init normally */ Table::Table(int x, int y /*= 1*/, int z /*= 1*/) : #ifdef MKXPZ_RETRO id(next_id++), #endif // MKXPZ_RETRO xs(x), ys(y), zs(z), data(x*y*z) {} Table::Table(const Table &other) : #ifdef MKXPZ_RETRO id(next_id++), #endif // MKXPZ_RETRO xs(other.xs), ys(other.ys), zs(other.zs), data(other.data) {} int16_t Table::get(int x, int y, int z) const { return data[xs*ys*z + xs*y + x]; } void Table::set(int16_t value, int x, int y, int z) { if (x < 0 || x >= xs || y < 0 || y >= ys || z < 0 || z >= zs) { return; } data[xs*ys*z + xs*y + x] = value; modified(); } void Table::resize(int x, int y, int z) { if (x == xs && y == ys && z == zs) return; std::vector newData(x*y*z); for (int k = 0; k < std::min(z, zs); ++k) for (int j = 0; j < std::min(y, ys); ++j) for (int i = 0; i < std::min(x, xs); ++i) newData[x*y*k + x*j + i] = at(i, j, k); data.swap(newData); xs = x; ys = y; zs = z; return; } void Table::resize(int x, int y) { resize(x, y, zs); } void Table::resize(int x) { resize(x, ys, zs); } /* Serializable */ int Table::serialSize() const { /* header + data */ return 20 + (xs * ys * zs) * 2; } void Table::serialize(char *buffer) const { /* Table dimensions: we don't care * about them but RMXP needs them */ int dim = 1; int size = xs * ys * zs; if (ys > 1) dim = 2; if (zs > 1) dim = 3; writeInt32(&buffer, dim); writeInt32(&buffer, xs); writeInt32(&buffer, ys); writeInt32(&buffer, zs); writeInt32(&buffer, size); #if defined(MKXPZ_RETRO) && defined(MKXPZ_BIG_ENDIAN) for (const int16_t datum : data) *(int16_t *)(buffer -= sizeof(int16_t)) = datum; #else memcpy(buffer, dataPtr(data), sizeof(int16_t)*size); #endif } Table *Table::deserialize(Exception &exception, const char *data, int len) { if (len < 20) { exception = Exception(Exception::RGSSError, "Marshal: Table: bad file format"); return nullptr; } readInt32(&data); int x = readInt32(&data); int y = readInt32(&data); int z = readInt32(&data); int size = readInt32(&data); if (size != x*y*z) { exception = Exception(Exception::RGSSError, "Marshal: Table: bad file format"); return nullptr; } if (len != 20 + x*y*z*2) { exception = Exception(Exception::RGSSError, "Marshal: Table: bad file format"); return nullptr; } Table *t = new Table(x, y, z); #if defined(MKXPZ_RETRO) && defined(MKXPZ_BIG_ENDIAN) for (int16_t &datum : t->data) datum = *(int16_t *)(data -= sizeof(int16_t)); #else memcpy(dataPtr(t->data), data, sizeof(int16_t)*size); #endif return t; } #ifdef MKXPZ_RETRO bool Table::sandbox_serialize(void *&data, mkxp_sandbox::wasm_size_t &max_size) const { if (!mkxp_sandbox::sandbox_serialize((int32_t)xs, data, max_size)) return false; if (!mkxp_sandbox::sandbox_serialize((int32_t)ys, data, max_size)) return false; if (!mkxp_sandbox::sandbox_serialize((int32_t)zs, data, max_size)) return false; if ((uint32_t)xs * (uint32_t)ys * (uint32_t)zs != this->data.size()) std::abort(); if (max_size < this->data.size() * sizeof(int16_t)) return false; memcpy(data, this->data.data(), this->data.size() * sizeof(int16_t)); data = (uint8_t *)data + this->data.size() * sizeof(int16_t); max_size -= this->data.size() * sizeof(int16_t); return true; } bool Table::sandbox_deserialize(const void *&data, mkxp_sandbox::wasm_size_t &max_size) { if (!mkxp_sandbox::sandbox_deserialize((int32_t &)xs, data, max_size)) return false; if (!mkxp_sandbox::sandbox_deserialize((int32_t &)ys, data, max_size)) return false; if (!mkxp_sandbox::sandbox_deserialize((int32_t &)zs, data, max_size)) return false; if (this->data.size() != (uint32_t)xs * (uint32_t)ys * (uint32_t)zs) { this->data.clear(); this->data.resize((uint32_t)xs * (uint32_t)ys * (uint32_t)zs); deserModified = true; } if (max_size < this->data.size() * sizeof(int16_t)) return false; if (deserModified) { memcpy(this->data.data(), data, this->data.size() * sizeof(int16_t)); } else if (memcmp(this->data.data(), data, this->data.size() * sizeof(int16_t))) { memcpy(this->data.data(), data, this->data.size() * sizeof(int16_t)); deserModified = true; } data = (uint8_t *)data + this->data.size() * sizeof(int16_t); max_size -= this->data.size() * sizeof(int16_t); return true; } void Table::sandbox_deserialize_begin(bool is_new) { deserModified = is_new; } #endif // MKXPZ_RETRO