Advertisement
expired6978

SaveLoad

Aug 10th, 2015
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.80 KB | None | 0 0
  1. bool MorphHandler::SaveJsonPreset(const char * filePath)
  2. {
  3.     Json::StyledWriter writer;
  4.     Json::Value root;
  5.  
  6.     IFileStream     currentFile;
  7.     IFileStream::MakeAllDirs(filePath);
  8.     if (!currentFile.Create(filePath))
  9.     {
  10.         _ERROR("%s: couldn't create preset file (%s) Error (%d)", __FUNCTION__, filePath, GetLastError());
  11.         return true;
  12.     }
  13.  
  14.     Json::Value versionInfo;
  15.     versionInfo["signature"] = PresetHeader::kSignature;
  16.     versionInfo["formatVersion"] = PresetHeader::kVersion;
  17.     versionInfo["skseVersion"] = PACKED_SKSE_VERSION;
  18.     versionInfo["runtimeVersion"] = RUNTIME_VERSION_1_9_32_0;
  19.  
  20.  
  21.     PlayerCharacter * player = (*g_thePlayer);
  22.     TESNPC * npc = DYNAMIC_CAST(player->baseForm, TESForm, TESNPC);
  23.     DataHandler * dataHandler = DataHandler::GetSingleton();
  24.  
  25.     bool isFemale = false;
  26.     if (npc)
  27.         isFemale = CALL_MEMBER_FN(npc, GetSex)() == 1;
  28.  
  29.     std::map<UInt8, const char *> modList;
  30.     std::map<UInt8, BGSHeadPart*> partList;
  31.  
  32.     UInt32 numHeadParts = 0;
  33.     BGSHeadPart ** headParts = NULL;
  34.     if (CALL_MEMBER_FN(npc, HasOverlays)()) {
  35.         numHeadParts = GetNumActorBaseOverlays(npc);
  36.         headParts = GetActorBaseOverlays(npc);
  37.     }
  38.     else {
  39.         numHeadParts = npc->numHeadParts;
  40.         headParts = npc->headparts;
  41.     }
  42.  
  43.     for (UInt32 i = 0; i < numHeadParts; i++) // Acquire all unique parts
  44.     {
  45.         BGSHeadPart * headPart = headParts[i];
  46.         if (headPart && !headPart->IsExtraPart()) {
  47.             UInt8 modIndex = headPart->formID >> 24;
  48.             ModInfo* modInfo = dataHandler->modList.modInfoList.GetNthItem(modIndex);
  49.             if (modInfo) {
  50.                 modList.emplace(modInfo->modIndex, modInfo->name);
  51.                 partList.emplace(i, headPart);
  52.             }
  53.         }
  54.     }
  55.  
  56.     std::map<UInt8, std::pair<UInt32, const char*>> tintList;
  57.     for (UInt32 i = 0; i < player->tintMasks.count; i++)
  58.     {
  59.         TintMask * tintMask = NULL;
  60.         if (player->tintMasks.GetNthItem(i, tintMask))
  61.         {
  62.             UInt32 tintColor = ((UInt32)(tintMask->alpha * 255.0) << 24) | tintMask->color.red << 16 | tintMask->color.green << 8 | tintMask->color.blue;
  63.             if (tintMask->texture)
  64.                 tintList.emplace(i, std::make_pair(tintColor, tintMask->texture->str.data));
  65.         }
  66.     }
  67.  
  68.     Json::Value modInfo;
  69.     for (auto mIt = modList.begin(); mIt != modList.end(); ++mIt)
  70.     {
  71.         Json::Value mod;
  72.         mod["index"] = mIt->first;
  73.         mod["name"] = mIt->second;
  74.         modInfo.append(mod);
  75.     }
  76.  
  77.     Json::Value headPartInfo;
  78.     for (auto pIt = partList.begin(); pIt != partList.end(); ++pIt)
  79.     {
  80.         Json::Value partInfo;
  81.         partInfo["type"] = pIt->first;
  82.         partInfo["formId"] = (Json::UInt)pIt->second->formID;
  83.         headPartInfo.append(partInfo);
  84.     }
  85.  
  86.     Json::Value tintInfo;
  87.     for (auto tmIt = tintList.begin(); tmIt != tintList.end(); ++tmIt)
  88.     {
  89.         Json::Value tint;
  90.         tint["index"] = tmIt->first;
  91.         tint["color"] = (Json::UInt)tmIt->second.first;
  92.         tint["texture"] = tmIt->second.second;
  93.         tintInfo.append(tint);
  94.     }
  95.  
  96.     Json::Value morphInfo;
  97.     if (npc->faceMorph)
  98.     {
  99.         for (UInt8 p = 0; p < TESNPC::FaceMorphs::kNumPresets; p++) {
  100.             Json::Value morphValue = (Json::UInt)npc->faceMorph->presets[p];
  101.             morphInfo["presets"].append(morphValue);
  102.         }
  103.  
  104.         for (UInt8 o = 0; o < TESNPC::FaceMorphs::kNumOptions; o++) {
  105.             Json::Value morphValue = npc->faceMorph->option[o];
  106.             morphInfo["morphs"].append(morphValue);
  107.         }
  108.     }
  109.  
  110.     Json::Value customMorphInfo;
  111.     ValueSet * valueSet = m_valueMap.GetValueSet(npc);
  112.     if (valueSet)
  113.     {
  114.         for (auto it = valueSet->begin(); it != valueSet->end(); ++it)
  115.         {
  116.             if (it->second != 0.0) {
  117.                 Json::Value morphValue;
  118.                 morphValue["name"] = it->first.data;
  119.                 morphValue["value"] = it->second;
  120.                 customMorphInfo.append(morphValue);
  121.             }
  122.         }
  123.     }
  124.  
  125.    
  126.     Json::Value sculptData;
  127.     auto sculptTarget = GetSculptTarget(npc, false);
  128.     if (sculptTarget) {
  129.         for (UInt32 i = 0; i < numHeadParts; i++) // Acquire all unique parts
  130.         {
  131.             BGSHeadPart * headPart = headParts[i];
  132.             if (headPart) {
  133.                 BSFixedString morphPath = SculptData::GetHostByPart(headPart);
  134.                 auto sculptHost = sculptTarget->GetSculptHost(morphPath, false);
  135.                 if (sculptHost) {
  136.                     Json::Value hostData;
  137.                     hostData["host"] = morphPath.data;
  138.  
  139.                     TRIModelData data;
  140.                     GetModelTri(morphPath, data);
  141.                    
  142.                     hostData["vertices"] = (Json::UInt)data.vertexCount;
  143.  
  144.                     for (auto morph : *sculptHost) {
  145.                         Json::Value value;
  146.                         value.append(morph.first);
  147.                         value.append((Json::Int)(morph.second.x * VERTEX_MULTIPLIER));
  148.                         value.append((Json::Int)(morph.second.y * VERTEX_MULTIPLIER));
  149.                         value.append((Json::Int)(morph.second.z * VERTEX_MULTIPLIER));
  150.                         hostData["data"].append(value);
  151.                     }
  152.  
  153.                     sculptData.append(hostData);
  154.                 }
  155.             }
  156.         }
  157.     }
  158.  
  159.     Json::Value textureInfo;
  160.     BGSHeadPart * facePart = npc->GetCurrentHeadPartByType(BGSHeadPart::kTypeFace);
  161.     if (facePart) {
  162.         BGSTextureSet * textureSet = GetTextureSetForPart(npc, facePart);
  163.         if (textureSet) {
  164.             for (UInt8 i = 0; i < BSShaderTextureSet::kNumTextures; i++) {
  165.                 const char * texturePath = textureSet->textureSet.GetTexturePath(i);
  166.                 if (texturePath != NULL) {
  167.                     Json::Value textureValue;
  168.                     textureValue["index"] = i;
  169.                     textureValue["texture"] = texturePath;
  170.                     textureInfo.append(textureValue);
  171.                 }
  172.             }
  173.         }
  174.     }
  175.    
  176.     // Collect override data
  177.     PresetData::OverrideData overrideData;
  178.     if (g_overrideInterface) {
  179.         g_overrideInterface->VisitNodes(player, [&overrideData](BSFixedString node, OverrideVariant & value)
  180.         {
  181.             overrideData[node].push_back(value);
  182.         });
  183.     }
  184.    
  185.     // Collect transform data
  186.     PresetData::TransformData transformData[2];
  187.     if (g_transformInterface) {
  188.         for (UInt32 i = 0; i <= 1; i++) {
  189.             g_transformInterface->VisitNodes(player, i == 1, isFemale, [&i, &transformData](BSFixedString node, OverrideRegistration<BSFixedString> * keys)
  190.             {
  191.                 keys->Visit([&i, &node, &transformData](const BSFixedString & key, OverrideSet * set)
  192.                 {
  193.                     set->Visit([&i, &node, &transformData, &key](OverrideVariant * value)
  194.                     {
  195.                         transformData[i][node][key].push_back(*value);
  196.                         return false;
  197.                     });
  198.  
  199.                     return false;
  200.                 });
  201.  
  202.                 return false;
  203.             });
  204.         }
  205.     }
  206.  
  207.     // Collect body morph data
  208.     PresetData::BodyMorphData bodyMorphData;
  209.     if (g_bodyMorphInterface) {
  210.         g_bodyMorphInterface->VisitMorphs(player, [&](BSFixedString name, float value)
  211.         {
  212.             bodyMorphData[name] = value;
  213.         });
  214.     }
  215.  
  216.     for (UInt32 i = 0; i <= 1; i++) {
  217.         for (auto & data : transformData[i]) {
  218.             Json::Value transform;
  219.             transform["firstPerson"] = (bool)(i == 1);
  220.             transform["node"] = data.first.data;
  221.  
  222.             for (auto & key : data.second) {
  223.                 Json::Value transformKey;
  224.                 transformKey["name"] = key.first.data;
  225.  
  226.                 for (auto & value : key.second) {
  227.                     Json::Value jvalue;
  228.                     jvalue["key"] = value.key;
  229.                     jvalue["type"] = value.type;
  230.                     jvalue["index"] = value.index;
  231.                     switch (value.type) {
  232.                         case OverrideVariant::kType_Bool:
  233.                         jvalue["data"] = value.data.b;
  234.                         break;
  235.                         case OverrideVariant::kType_Int:
  236.                         jvalue["data"] = value.data.i;
  237.                         break;
  238.                         case OverrideVariant::kType_Float:
  239.                         jvalue["data"] = value.data.f;
  240.                         break;
  241.                         case OverrideVariant::kType_String:
  242.                         jvalue["data"] = value.data.GetStr()->data;
  243.                         break;
  244.                     }
  245.                     transformKey["values"].append(jvalue);
  246.                 }
  247.                 transform["keys"].append(transformKey);
  248.             }
  249.             root["transforms"].append(transform);
  250.         }
  251.     }
  252.     for (auto & data : overrideData) {
  253.         Json::Value ovr;
  254.         ovr["node"] = data.first.data;
  255.  
  256.         for (auto & value : data.second) {
  257.             Json::Value jvalue;
  258.             jvalue["key"] = value.key;
  259.             jvalue["type"] = value.type;
  260.             jvalue["index"] = value.index;
  261.             switch (value.type) {
  262.                 case OverrideVariant::kType_Bool:
  263.                 jvalue["data"] = value.data.b;
  264.                 break;
  265.                 case OverrideVariant::kType_Int:
  266.                 jvalue["data"] = value.data.i;
  267.                 break;
  268.                 case OverrideVariant::kType_Float:
  269.                 jvalue["data"] = value.data.f;
  270.                 break;
  271.                 case OverrideVariant::kType_String:
  272.                 jvalue["data"] = value.data.GetStr()->data;
  273.                 break;
  274.             }
  275.             ovr["values"].append(jvalue);
  276.         }
  277.         root["overrides"].append(ovr);
  278.     }
  279.  
  280.     for (auto & data : bodyMorphData) {
  281.         Json::Value bm;
  282.         bm["name"] = data.first.data;
  283.         bm["value"] = data.second;
  284.         root["bodyMorphs"].append(bm);
  285.     }
  286.    
  287.     root["version"] = versionInfo;
  288.     root["mods"] = modInfo;
  289.     root["headParts"] = headPartInfo;
  290.     root["actor"]["weight"] = npc->weight;
  291.  
  292.     if (npc->headData) {
  293.         auto hairColor = npc->headData->hairColor;
  294.         if (hairColor)
  295.             root["actor"]["hairColor"] = (Json::UInt)(hairColor->color.red << 16 | hairColor->color.green << 8 | hairColor->color.blue);
  296.     }
  297.    
  298.     root["tintInfo"] = tintInfo;
  299.     root["faceTextures"] = textureInfo;
  300.     root["morphs"]["default"] = morphInfo;
  301.     root["morphs"]["custom"] = customMorphInfo;
  302.     root["morphs"]["sculptDivisor"] = VERTEX_MULTIPLIER;
  303.     root["morphs"]["sculpt"] = sculptData;
  304.  
  305.     std::string data = writer.write(root);
  306.     currentFile.WriteBuf(data.c_str(), data.length());
  307.     currentFile.Close();
  308.     return false;
  309. }
  310.  
  311.  
  312. bool MorphHandler::LoadJsonPreset(const char * filePath, PresetDataPtr presetData)
  313. {
  314.     bool loadError = false;
  315.     BSResourceNiBinaryStream file(filePath);
  316.     if (!file.IsValid()) {
  317.         _ERROR("%s: File %s failed to open.", __FUNCTION__, filePath);
  318.         loadError = true;
  319.         return loadError;
  320.     }
  321.  
  322.     std::string in;
  323.     BSReadAll(&file, &in);
  324.  
  325.     Json::Features features;
  326.     features.all();
  327.  
  328.     Json::Value root;
  329.     Json::Reader reader(features);
  330.  
  331.     bool parseSuccess = reader.parse(in, root);
  332.     if (!parseSuccess) {
  333.         _ERROR("%s: Error occured parsing json for %s.", __FUNCTION__, filePath);
  334.         loadError = true;
  335.         return loadError;
  336.     }
  337.  
  338.     Json::Value defaultValue;
  339.     Json::Value version = root["version"];
  340.     if (version.empty()) {
  341.         _ERROR("%s: No version header.", __FUNCTION__);
  342.         loadError = true;
  343.         return loadError;
  344.     }
  345.  
  346.     UInt32 signature = version["signature"].asUInt();
  347.     if (signature != PresetHeader::kSignature)
  348.     {
  349.         _ERROR("%s: invalid file signature (found %08X expected %08X)", __FUNCTION__, signature, PresetHeader::kSignature);
  350.         loadError = true;
  351.         return loadError;
  352.     }
  353.  
  354.     UInt32 formatVersion = version["formatVersion"].asUInt();
  355.     if (formatVersion <= PresetHeader::kVersion_Invalid)
  356.     {
  357.         _ERROR("%s: version invalid (%08X)", __FUNCTION__, formatVersion);
  358.         loadError = true;
  359.         return loadError;
  360.     }
  361.  
  362.     Json::Value mods = root["mods"];
  363.     if (mods.empty()) {
  364.         _ERROR("%s: No mods header.", __FUNCTION__);
  365.         loadError = true;
  366.         return loadError;
  367.     }
  368.  
  369.     DataHandler * dataHandler = DataHandler::GetSingleton();
  370.  
  371.     std::map<UInt8, std::string> modList;
  372.     if (mods.type() == Json::arrayValue) {
  373.         for (auto & mod : mods) {
  374.             UInt8 modIndex = mod["index"].asUInt();
  375.             std::string modName = mod["name"].asString();
  376.  
  377.             modList.emplace(modIndex, modName);
  378.             presetData->modList.push_back(modName);
  379.         }
  380.     }
  381.  
  382.     Json::Value headParts = root["headParts"];
  383.     if (!headParts.empty() && headParts.type() == Json::arrayValue) {
  384.         for (auto & part : headParts) {
  385.  
  386.             UInt8 partType = part["type"].asUInt();
  387.             UInt32 formId = part["formId"].asUInt();
  388.  
  389.             UInt8 modIndex = formId >> 24;
  390.             auto it = modList.find(modIndex);
  391.             if (it != modList.end()) {
  392.                 UInt8 gameIndex = dataHandler->GetModIndex(it->second.c_str());
  393.                 if (gameIndex != 255) {
  394.                     formId = (formId & 0x00FFFFFF) | (gameIndex << 24);
  395.                     TESForm * headPartForm = LookupFormByID(formId);
  396.                     if (headPartForm) {
  397.                         BGSHeadPart * headPart = DYNAMIC_CAST(headPartForm, TESForm, BGSHeadPart);
  398.                         if (headPart) {
  399.                             presetData->headParts.push_back(headPart);
  400.                         }
  401.                     }
  402.                     else {
  403.                         _WARNING("Could not resolve part %08X", formId);
  404.                     }
  405.                 }
  406.                 else {
  407.                     _WARNING("Could not load part type %d from %s; mod not found.", partType, it->second.c_str());
  408.                 }
  409.             }
  410.         }
  411.     }
  412.  
  413.     Json::Value actor = root["actor"];
  414.     if (!actor.empty() && actor.type() == Json::objectValue) {
  415.         presetData->weight = actor["weight"].asFloat();
  416.         presetData->hairColor = actor["hairColor"].asUInt();
  417.     }
  418.  
  419.  
  420.     Json::Value tintInfo = root["tintInfo"];
  421.     if (!tintInfo.empty() && tintInfo.type() == Json::arrayValue) {
  422.         for (auto & tint : tintInfo) {
  423.             PresetData::Tint tintData;
  424.             tintData.color = tint["color"].asUInt();
  425.             tintData.index = tint["index"].asUInt();
  426.             tintData.name = tint["texture"].asString().c_str();
  427.             presetData->tints.push_back(tintData);
  428.         }
  429.     }
  430.  
  431.     Json::Value faceTextures = root["faceTextures"];
  432.     if (!faceTextures.empty() && faceTextures.type() == Json::arrayValue) {
  433.         for (auto & faceTexture : faceTextures) {
  434.             PresetData::Texture texture;
  435.             texture.index = faceTexture["index"].asUInt();
  436.             texture.name = faceTexture["texture"].asString().c_str();
  437.             presetData->faceTextures.push_back(texture);
  438.         }
  439.     }
  440.  
  441.     Json::Value morphs = root["morphs"];
  442.     if (!morphs.empty()) {
  443.         Json::Value defaultMorphs = morphs["default"];
  444.         if (!defaultMorphs.empty()) {
  445.             Json::Value presets = defaultMorphs["presets"];
  446.             for (auto & preset : presets) {
  447.                 UInt32 presetValue = preset.asUInt();
  448.                 if (presetValue == 255)
  449.                     presetValue = -1;
  450.  
  451.                 presetData->presets.push_back(presetValue);
  452.             }
  453.  
  454.             Json::Value morphs = defaultMorphs["morphs"];
  455.             for (auto & morph : morphs) {
  456.                 presetData->morphs.push_back(morph.asFloat());
  457.             }
  458.         }
  459.         Json::Value customMorphs = morphs["custom"];
  460.         if (!customMorphs.empty()) {
  461.             for (auto & customMorph : customMorphs) {
  462.                 PresetData::Morph morph;
  463.                 morph.name = customMorph["name"].asString().c_str();
  464.                 morph.value = customMorph["value"].asFloat();
  465.                 presetData->customMorphs.push_back(morph);
  466.             }
  467.         }
  468.  
  469.         SInt32 multiplier = -1;
  470.  
  471.         Json::Value sculptMult = morphs["sculptDivisor"];
  472.         if (!sculptMult.empty())
  473.             multiplier = sculptMult.asInt();
  474.  
  475.         Json::Value sculptData = morphs["sculpt"];
  476.         if (!sculptData.empty()) {
  477.             presetData->sculptData = std::make_shared<SculptData>();
  478.             for (auto & hostFile : sculptData) {
  479.                 BSFixedString host = hostFile["host"].asString().c_str();
  480.                 Json::Value data = hostFile["data"];
  481.  
  482.                 auto sculptedData = std::make_shared<MappedSculptData>();
  483.                 for (auto & morphData : data) {
  484.                     UInt16 index = morphData[0].asUInt();
  485.                     NiPoint3 pt;
  486.  
  487.                     if (multiplier > 0) {
  488.                         pt.x = (float)morphData[1].asInt() / (float)multiplier;
  489.                         pt.y = (float)morphData[2].asInt() / (float)multiplier;
  490.                         pt.z = (float)morphData[3].asInt() / (float)multiplier;
  491.                     } else {
  492.                         pt.x = morphData[1].asFloat();
  493.                         pt.y = morphData[2].asFloat();
  494.                         pt.z = morphData[3].asFloat();
  495.                     }
  496.  
  497.                     sculptedData->force_insert(std::make_pair(index, pt));
  498.                 }
  499.  
  500.                 presetData->sculptData->emplace(host, sculptedData);
  501.             }
  502.         }
  503.     }
  504.  
  505.     Json::Value transforms = root["transforms"];
  506.     if (!transforms.empty()) {
  507.         for (auto & xForm : transforms) {
  508.             bool isFirstPerson = xForm["firstPerson"].asBool();
  509.             BSFixedString nodeName = xForm["node"].asString().c_str();
  510.  
  511.             Json::Value keys = xForm["keys"];
  512.             for (auto & key : keys) {
  513.                 BSFixedString keyName = key["name"].asString().c_str();
  514.  
  515.                 Json::Value values = key["values"];
  516.                 for (auto & jvalue : values) {
  517.                     OverrideVariant value;
  518.                     value.key = jvalue["key"].asUInt();
  519.                     value.type = jvalue["type"].asInt();
  520.                     value.index = jvalue["index"].asInt();
  521.                     switch (value.type) {
  522.                         case OverrideVariant::kType_Bool:
  523.                         value.data.b = jvalue["data"].asBool();
  524.                         break;
  525.                         case OverrideVariant::kType_Int:
  526.                         value.data.i = jvalue["data"].asInt();
  527.                         break;
  528.                         case OverrideVariant::kType_Float:
  529.                         value.data.f = jvalue["data"].asFloat();
  530.                         break;
  531.                         case OverrideVariant::kType_String:
  532.                         value.data.str = BSFixedString(jvalue["data"].asString().c_str()).data;
  533.                         break;
  534.                     }
  535.  
  536.                     presetData->transformData[isFirstPerson ? 1 : 0][nodeName][keyName].push_back(value);
  537.                 }
  538.             }
  539.         }
  540.     }
  541.  
  542.     Json::Value overrides = root["overrides"];
  543.     if (!overrides.empty()) {
  544.         for (auto & ovr : overrides) {
  545.             BSFixedString node = ovr["node"].asString().c_str();
  546.             Json::Value values = ovr["values"];
  547.             for (auto & jvalue : values) {
  548.                 OverrideVariant value;
  549.                 value.key = jvalue["key"].asUInt();
  550.                 value.type = jvalue["type"].asInt();
  551.                 value.index = jvalue["index"].asInt();
  552.                 switch (value.type) {
  553.                     case OverrideVariant::kType_Bool:
  554.                     value.data.b = jvalue["data"].asBool();
  555.                     break;
  556.                     case OverrideVariant::kType_Int:
  557.                     value.data.i = jvalue["data"].asInt();
  558.                     break;
  559.                     case OverrideVariant::kType_Float:
  560.                     value.data.f = jvalue["data"].asFloat();
  561.                     break;
  562.                     case OverrideVariant::kType_String:
  563.                     value.data.str = BSFixedString(jvalue["data"].asString().c_str()).data;
  564.                     break;
  565.                 }
  566.                 presetData->overrideData[node].push_back(value);
  567.             }
  568.         }
  569.     }
  570.  
  571.     Json::Value bodyMorphs = root["bodyMorphs"];
  572.     if (!bodyMorphs.empty()) {
  573.         for (auto & bm : bodyMorphs) {
  574.             BSFixedString name = bm["name"].asString().c_str();
  575.             float value = bm["value"].asFloat();
  576.  
  577.             presetData->bodyMorphData[name] = value;
  578.         }
  579.     }
  580.  
  581.     return loadError;
  582. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement