1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
#include "ai/ailua.h"
#include "mission/missionmessage.h"
#include "parse/sexp/sexp_lookup.h"
#include "parse/parselo.h"
#include "parse/sexp.h"
#include "parse/sexp/LuaSEXP.h"
#include "parse/sexp/LuaAISEXP.h"
#include "scripting/scripting.h"
#include <memory>
namespace {
using namespace sexp;
struct global_state {
// Track if we already have called our init function
bool initialized = false;
// Before we are initialized it is not safe to add sexps to the system so we add them to a pending list which we
// process while initializing
SCP_vector<std::unique_ptr<DynamicSEXP>> pending_sexps;
SCP_unordered_map<int, std::unique_ptr<sexp::DynamicSEXP>> operator_const_mapping;
SCP_unordered_map<int, int> subcategory_to_category;
// These IDs are now just incremented
int next_free_operator_id = First_available_operator_id;
int next_free_category_id = First_available_category_id;
int next_free_subcategory_id = First_available_subcategory_id;
int next_free_enum_list_id = First_available_opf_id;
};
// Static initialization to avoid initialization order issues
global_state& globals()
{
static global_state state;
return state;
}
void parse_sexp_table(const char* filename) {
try {
read_file_text(filename, CF_TYPE_TABLES);
reset_parse();
if (optional_string("#Lua Enums")) {
while (optional_string("$Name:")) {
SCP_string name;
stuff_string(name, F_NAME);
if (get_dynamic_enum_position(name) >= 0) {
error_display(0, "Lua Sexp Enum %s already exists! Skipping!\n", name.c_str());
continue;
}
dynamic_sexp_enum_list thisList;
thisList.name = std::move(name);
while (optional_string("+Enum:")) {
SCP_string item;
stuff_string(item, F_NAME);
// These characters may not appear in an Enum item
constexpr const char* ENUM_INVALID_CHARS = "()\"'\\/";
if (std::strpbrk(item.c_str(), ENUM_INVALID_CHARS) != nullptr) {
error_display(0, "ENUM item '%s' cannot include these characters [(,),\",',\\,/]. Skipping!\n", item.c_str());
// Skip the invalid entry
continue;
}
if (item.length() >= NAME_LENGTH) {
error_display(0, "Enum item '%s' is longer than %i characters. Truncating!\n", item.c_str(), NAME_LENGTH);
item.resize(NAME_LENGTH - 1);
}
bool skip = false;
// Case insensitive check if the item already exists in the list
for (int i = 0; i < (int)thisList.list.size(); i++) {
if (lcase_equal(item, thisList.list[i])) {
error_display(0, "Enum item '%s' already exists in list %s. Skipping!\n", item.c_str(), thisList.name.c_str());
skip = true;
break;
}
}
if (skip)
continue;
thisList.list.push_back(item);
}
if (thisList.list.size() > 0) {
Dynamic_enums.push_back(thisList);
increment_enum_list_id();
} else {
error_display(0, "Parsed empty enum list '%s'. Ignoring!\n", thisList.name.c_str());
}
}
required_string("#End");
}
// These characters may not appear in a SEXP name
constexpr const char* INVALID_CHARS = "()\"'\t ";
if (optional_string("#Lua SEXPs")) {
while (optional_string("$Operator:")) {
SCP_string name;
stuff_string(name, F_NAME);
if (std::strpbrk(name.c_str(), INVALID_CHARS) != nullptr) {
error_display(0, "Found invalid SEXP name '%s'!", name.c_str());
// Skip the invalid entry
skip_to_start_of_string_either("$Operator:", "#End");
continue;
}
if (get_operator_index(name.c_str()) >= 0) {
error_display(0, "The SEXP '%s' is already defined!", name.c_str());
// Skip the invalid entry
skip_to_start_of_string_either("$Operator:", "#End");
continue;
}
std::unique_ptr<DynamicSEXP> instance(new LuaSEXP(name));
auto luaSexp = static_cast<LuaSEXP*>(instance.get());
luaSexp->parseTable();
add_dynamic_sexp(std::move(instance));
}
required_string("#End");
}
if (optional_string("#Lua AI")) {
while (optional_string("$Operator:")) {
SCP_string name;
stuff_string(name, F_NAME);
if (std::strpbrk(name.c_str(), INVALID_CHARS) != nullptr) {
error_display(0, "Found invalid AI-SEXP name '%s'!", name.c_str());
// Skip the invalid entry
skip_to_start_of_string_either("$Operator:", "#End");
continue;
}
if (get_operator_index(name.c_str()) >= 0) {
error_display(0, "The AI-SEXP '%s' is already defined!", name.c_str());
// Skip the invalid entry
skip_to_start_of_string_either("$Operator:", "#End");
continue;
}
std::unique_ptr<DynamicSEXP> instance(new LuaAISEXP(name));
auto luaSexp = static_cast<LuaAISEXP*>(instance.get());
luaSexp->parseTable();
int op = add_dynamic_sexp(std::move(instance), sexp_oper_type::GOAL);
if (op >= 0) {
luaSexp->registerAIMode(op);
luaSexp->maybeRegisterPlayerOrder(op);
}
}
required_string("#End");
}
} catch (const parse::ParseException& e) {
mprintf(("TABLES: Unable to parse '%s'! Error message = %s.\n", filename, e.what()));
return;
}
}
void free_lua_sexps(lua_State* /*L*/)
{
auto& global = globals();
// Remove all Lua sexps from our list so that there are no dangling pointers on the lua state
for (auto iter = global.operator_const_mapping.begin(); iter != global.operator_const_mapping.end();) {
auto lua_sexp = dynamic_cast<LuaSEXP*>(iter->second.get());
if (lua_sexp == nullptr) {
++iter;
continue;
}
iter = global.operator_const_mapping.erase(iter);
}
}
} // namespace
namespace sexp {
int operator_upper_bound()
{
return globals().next_free_operator_id;
}
int add_dynamic_sexp(std::unique_ptr<DynamicSEXP>&& sexp, sexp_oper_type type)
{
auto& global = globals();
if (!global.initialized) {
// Do nothing now and delay this until we know that we are properly initialized
global.pending_sexps.emplace_back(std::move(sexp));
return -1;
}
sexp->initialize();
sexp_oper new_op;
new_op.text = sexp->getName();
new_op.min = sexp->getMinimumArguments();
new_op.max = sexp->getMaximumArguments();
int free_op_index = global.next_free_operator_id++;
if (Operators.size() >= FIRST_OP) {
Warning(LOCATION, "There are too many total SEXPs. The SEXP %s will not be added.", new_op.text.c_str());
return -1;
}
// sanity check
int subcategory = sexp->getSubcategory();
if (subcategory != OP_SUBCATEGORY_NONE)
{
int category = sexp->getCategory();
int implied_category = category_of_subcategory(subcategory);
if (category != implied_category)
Warning(LOCATION, "Operator %s has a category that is not a parent of its subcategory!", new_op.text.c_str());
}
// For now, all dynamic SEXPS are only valid in missions
new_op.value = free_op_index;
new_op.type = type;
sexp_help_struct new_help;
new_help.id = new_op.value;
new_help.help = sexp->getHelpText();
global.operator_const_mapping.insert(std::make_pair(new_op.value, std::move(sexp)));
// Now actually add the operator to the SEXP containers
Operators.push_back(new_op);
Sexp_help.push_back(new_help);
return new_op.value;
}
DynamicSEXP* get_dynamic_sexp(int operator_const)
{
auto& global = globals();
auto iter = global.operator_const_mapping.find(operator_const);
if (iter == global.operator_const_mapping.end()) {
return nullptr;
}
return iter->second.get();
}
int get_category(const SCP_string& name)
{
for (auto& cat : op_menu) {
if (lcase_equal(cat.name, name)) {
return cat.id;
}
}
return OP_CATEGORY_NONE;
}
int get_subcategory(const SCP_string& name, int category)
{
for (auto& subcat : op_submenu) {
if (lcase_equal(subcat.name, name) && get_category_of_subcategory(subcat.id) == category) {
return subcat.id;
}
}
return OP_SUBCATEGORY_NONE;
}
int get_category_of_subcategory(int subcategory_id)
{
const auto& global = globals();
auto iter = global.subcategory_to_category.find(subcategory_id);
if (iter == global.subcategory_to_category.end()) {
return OP_CATEGORY_NONE;
}
return iter->second;
}
int increment_enum_list_id()
{
auto& global = globals();
return global.next_free_enum_list_id++;
}
void dynamic_sexp_init()
{
auto& global = globals();
global.initialized = true;
// Add built-in subcategories
for (auto& item : op_submenu) {
int category = category_of_subcategory(item.id);
global.subcategory_to_category.emplace(item.id, category);
}
// Add pending sexps now when it is safe to do so
for (auto&& pending : global.pending_sexps) {
add_dynamic_sexp(std::move(pending));
}
global.pending_sexps.clear();
message_types_init();
parse_modular_table("*-sexp.tbm", parse_sexp_table, CF_TYPE_TABLES);
Script_system.OnStateDestroy.add(free_lua_sexps);
}
void dynamic_sexp_shutdown()
{
auto& global = globals();
global.operator_const_mapping.clear();
global.subcategory_to_category.clear();
global.next_free_operator_id = First_available_operator_id;
global.next_free_category_id = First_available_category_id;
global.next_free_subcategory_id = First_available_subcategory_id;
global.initialized = false;
}
int add_category(const SCP_string& name)
{
auto& global = globals();
int category_id = global.next_free_category_id++;
op_menu.push_back({ name, category_id });
return category_id;
}
int add_subcategory(int parent_category, const SCP_string& name)
{
auto& global = globals();
int subcategory_id = global.next_free_subcategory_id++;
op_submenu.push_back({ name, subcategory_id });
global.subcategory_to_category.emplace(subcategory_id, parent_category);
return subcategory_id;
}
}
|