| 1 |
#include "SRPropertyLoader.h" |
| 2 |
|
| 3 |
#include "iregistry.h" |
| 4 |
#include "entitylib.h" |
| 5 |
#include "string/convert.h" |
| 6 |
#include "gamelib.h" |
| 7 |
#include <regex> |
| 8 |
|
| 9 |
SRPropertyLoader::SRPropertyLoader(SREntity::KeyList& keys, SREntity& srEntity, std::string& warnings) : |
| 10 |
_keys(keys), |
| 11 |
_srEntity(srEntity), |
| 12 |
_warnings(warnings), |
| 13 |
_prefix(game::current::getValue<std::string>(GKEY_STIM_RESPONSE_PREFIX)), |
| 14 |
_responseEffectPrefix(game::current::getValue<std::string>(GKEY_RESPONSE_EFFECT_PREFIX)) |
| 15 |
{} |
| 16 |
|
| 17 |
void SRPropertyLoader::visitKeyValue(const std::string& key, const std::string& value) |
| 18 |
{ |
| 19 |
parseAttribute(key, value, false); |
| 20 |
} |
| 21 |
|
| 22 |
void SRPropertyLoader::operator() (const EntityClassAttribute& attribute, bool) |
| 23 |
{ |
| 24 |
parseAttribute(attribute.getName(), attribute.getValue(), true); |
| 25 |
} |
| 26 |
|
| 27 |
StimResponse& SRPropertyLoader::findOrCreate(int index, bool inherited) |
| 28 |
{ |
| 29 |
auto& found = _srEntity.get(index); |
| 30 |
|
| 31 |
if (found.getIndex() == index) |
| 32 |
{ |
| 33 |
return found; |
| 34 |
} |
| 35 |
|
| 36 |
// Create the S/R object in case we got an empty S/R back |
| 37 |
// Insert a new SR object with the given index |
| 38 |
auto& sr = _srEntity.add(index); |
| 39 |
sr.setInherited(inherited); |
| 40 |
|
| 41 |
return sr; |
| 42 |
} |
| 43 |
|
| 44 |
void SRPropertyLoader::parseAttribute(const std::string& key, const std::string& value, bool inherited) |
| 45 |
{ |
| 46 |
// Cycle through the possible key names and see if we have a match |
| 47 |
for (const auto& srKey : _keys) |
| 48 |
{ |
| 49 |
// Construct a regex with the number as match variable |
| 50 |
std::string exprStr = "^" + _prefix + srKey.key + "_([0-9]+)$"; |
| 51 |
std::regex expr(exprStr); |
| 52 |
std::smatch matches; |
| 53 |
|
| 54 |
if (std::regex_match(key, matches, expr)) |
| 55 |
{ |
| 56 |
// Retrieve the S/R index number |
| 57 |
int index = string::convert<int>(matches[1].str()); |
| 58 |
|
| 59 |
// Ensure the S/R index exists in the list |
| 60 |
auto& sr = findOrCreate(index, inherited); |
| 61 |
|
| 62 |
// Check if the property already exists |
| 63 |
if (!sr.get(srKey.key).empty()) |
| 64 |
{ |
| 65 |
// already existing, add to the warnings |
| 66 |
_warnings += "Warning on StimResponse #" + string::to_string(index) + |
| 67 |
": property " + srKey.key + " defined more than once.\n"; |
| 68 |
} |
| 69 |
|
| 70 |
// Set the property value on the StimResponse object |
| 71 |
sr.set(srKey.key, value, inherited); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// Check the key for a Response Effect definition |
| 76 |
{ |
| 77 |
// This should search for something like "sr_effect_2_3_arg3" |
| 78 |
// (with the optional postfix "_argN" or "_state") |
| 79 |
std::string exprStr = |
| 80 |
"^" + _prefix + _responseEffectPrefix + "([0-9]+)_([0-9]+)(_arg[0-9]+|_state)*$"; |
| 81 |
std::regex expr(exprStr); |
| 82 |
std::smatch matches; |
| 83 |
|
| 84 |
if (std::regex_match(key, matches, expr)) |
| 85 |
{ |
| 86 |
// The response index |
| 87 |
int index = string::convert<int>(matches[1].str()); |
| 88 |
// The effect index |
| 89 |
int effectIndex = string::convert<int>(matches[2].str()); |
| 90 |
|
| 91 |
// Ensure the S/R index exists in the list |
| 92 |
auto& sr = findOrCreate(index, inherited); |
| 93 |
|
| 94 |
// Get the response effect (or create a new one) |
| 95 |
ResponseEffect& effect = sr.getResponseEffect(effectIndex); |
| 96 |
|
| 97 |
std::string postfix = matches[3]; |
| 98 |
|
| 99 |
if (postfix.empty()) |
| 100 |
{ |
| 101 |
// No "_arg1" found, the value is the effect name definition |
| 102 |
effect.setName(value, inherited); |
| 103 |
} |
| 104 |
else if (postfix == "_state") |
| 105 |
{ |
| 106 |
// This is a state variable |
| 107 |
effect.setActive(value != "0", inherited); |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
// Get the argument index from the tail |
| 112 |
int argIndex = string::convert<int>(postfix.substr(4)); |
| 113 |
|
| 114 |
// Load the value into argument with the index <argIndex> |
| 115 |
effect.setArgument(argIndex, value, inherited); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |