Advertisement
expired6978

BodyMorph Parse

Mar 29th, 2015
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.28 KB | None | 0 0
  1.  
  2. void BodyMorphInterface::ReadBodyMorphs(BSFixedString filePath)
  3. {
  4.     BSResourceNiBinaryStream file(filePath.data);
  5.     if (!file.IsValid()) {
  6.         return;
  7.     }
  8.  
  9.     UInt32 lineCount = 0;
  10.     std::string str = "";
  11.  
  12.     while (BSReadLine(&file, &str))
  13.     {
  14.         lineCount++;
  15.         str = std::trim(str);
  16.         if (str.length() == 0)
  17.             continue;
  18.         if (str.at(0) == '#')
  19.             continue;
  20.  
  21.         std::vector<std::string> side = std::explode(str, '=');
  22.         if (side.size() < 2) {
  23.             _ERROR("%s - Error - Line (%d) loading a morph from %s has no left-hand side.", __FUNCTION__, lineCount, filePath.data);
  24.             continue;
  25.         }
  26.  
  27.         std::string lSide = std::trim(side[0]);
  28.         std::string rSide = std::trim(side[1]);
  29.  
  30.         std::vector<std::string> form = std::explode(lSide, '|');
  31.         if (form.size() < 2) {
  32.             _ERROR("%s - Error - Line (%d) morph left side from %s missing mod name or formID.", __FUNCTION__, lineCount, filePath.data);
  33.             continue;
  34.         }
  35.  
  36.         std::string modNameText = std::trim(form[0]);
  37.         BSFixedString modText(modNameText.c_str());
  38.         UInt8 modIndex = DataHandler::GetSingleton()->GetModIndex(modText.data);
  39.         if (modIndex == -1) {
  40.             _WARNING("%s - Warning - Line (%d) Mod %s from %s not a loaded mod.", __FUNCTION__, lineCount, modText.data, filePath.data);
  41.             continue;
  42.         }
  43.  
  44.         std::string formIdText = std::trim(form[1]);
  45.         UInt32 formLower = strtoul(formIdText.c_str(), NULL, 16);
  46.  
  47.         if (formLower == 0) {
  48.             _ERROR("%s - Error - Line (%d) from %s invalid formID.", __FUNCTION__, lineCount, filePath.data);
  49.             continue;
  50.         }
  51.  
  52.         UInt32 formId = modIndex << 24 | formLower & 0xFFFFFF;
  53.         TESForm * foundForm = LookupFormByID(formId);
  54.         if (!foundForm) {
  55.             _ERROR("%s - Error - Line (%d) from %s invalid form %08X.", __FUNCTION__, lineCount, filePath.data, formId);
  56.             continue;
  57.         }
  58.  
  59.         TESNPC * npc = DYNAMIC_CAST(foundForm, TESForm, TESNPC);
  60.         if (!npc) {
  61.             _ERROR("%s - Error - Line (%d) from %s invalid form %08X not an ActorBase.", __FUNCTION__, lineCount, filePath.data, formId);
  62.             continue;
  63.         }
  64.  
  65.         BodyGenSets bodyGenSets;
  66.  
  67.         std::string error = "";
  68.         std::vector<std::string> sets = std::explode(rSide, '/');
  69.         for (UInt32 i = 0; i < sets.size(); i++) {
  70.             sets[i] = std::trim(sets[i]);
  71.  
  72.             BodyGenMorphs bodyMorphs;
  73.  
  74.             std::vector<std::string> morphs = std::explode(sets[i], ',');
  75.             for (UInt32 j = 0; j < morphs.size(); j++) {
  76.                 morphs[j] = std::trim(morphs[j]);
  77.  
  78.                
  79.  
  80.                 std::vector<std::string> selectors = std::explode(morphs[j], '|');
  81.  
  82.                 BodyGenMorphSelector selector;
  83.  
  84.                 for (UInt32 k = 0; k < selectors.size(); k++) {
  85.                     selectors[k] = std::trim(selectors[k]);
  86.  
  87.                     std::vector<std::string> pairs = std::explode(selectors[k], '@');
  88.                     if (pairs.size() < 2) {
  89.                         error = "Must have value pair with @";
  90.                         break;
  91.                     }
  92.  
  93.                     std::string morphName = std::trim(pairs[0]);
  94.                     if (morphName.length() == 0) {
  95.                         error = "Empty morph name";
  96.                         break;
  97.                     }
  98.  
  99.                     std::string morphValues = std::trim(pairs[1]);
  100.                     if (morphValues.length() == 0) {
  101.                         error = "Empty values";
  102.                         break;
  103.                     }
  104.  
  105.                     float lowerValue = 0;
  106.                     float upperValue = 0;
  107.  
  108.                     std::vector<std::string> range = std::explode(morphValues, ':');
  109.                     if (range.size() > 1) {
  110.                         std::string lowerRange = std::trim(range[0]);
  111.                         if (lowerRange.length() == 0) {
  112.                             error = "Empty lower range";
  113.                             break;
  114.                         }
  115.  
  116.                         lowerValue = strtof(lowerRange.c_str(), NULL);
  117.  
  118.                         std::string upperRange = std::trim(range[1]);
  119.                         if (upperRange.length() == 0) {
  120.                             error = "Empty upper range";
  121.                             break;
  122.                         }
  123.  
  124.                         upperValue = strtof(upperRange.c_str(), NULL);
  125.                     }
  126.                     else {
  127.                         lowerValue = strtof(morphValues.c_str(), NULL);
  128.                         upperValue = lowerValue;
  129.                     }
  130.  
  131.                     BodyGenMorphData morphData;
  132.                     morphData.name = morphName.c_str();
  133.                     morphData.lower = lowerValue;
  134.                     morphData.upper = upperValue;
  135.                     selector.push_back(morphData);
  136.                 }
  137.  
  138.                 if (error.length() > 0)
  139.                     break;
  140.  
  141.                 bodyMorphs.push_back(selector);
  142.             }
  143.  
  144.             if (error.length() > 0)
  145.                 break;
  146.  
  147.             bodyGenSets.push_back(bodyMorphs);
  148.         }
  149.  
  150.         if (error.length() > 0) {
  151.             _ERROR("%s - Error - Line (%d) could not parse morphs from %s. (%s)", __FUNCTION__, lineCount, filePath.data, error.c_str());
  152.             continue;
  153.         }
  154.  
  155.         bodyGenData[npc] = bodyGenSets;
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement