From f6c5e6f57642f3076961b48f307d7022fbd36dc9 Mon Sep 17 00:00:00 2001 From: chinosk <2248589280@qq.com> Date: Sun, 5 May 2024 11:38:55 +0800 Subject: [PATCH] save sway offset #19 --- src/hook.cpp | 72 +++++++++++++++++++++++++++++++++++++++++ src/scgui/scGUILoop.cpp | 5 +++ 2 files changed, 77 insertions(+) diff --git a/src/hook.cpp b/src/hook.cpp index 8a774b5..739affa 100644 --- a/src/hook.cpp +++ b/src/hook.cpp @@ -40,10 +40,82 @@ std::map swayTypes{ {0xd, "Max"}, }; +void loadGUIDataCache() { + try { + if (!std::filesystem::exists("scsp-gui-save.json")) return; + std::ifstream file("scsp-gui-save.json"); + if (!file.is_open()) return; + std::string fileContent((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + file.close(); + + auto fileData = nlohmann::json::parse(fileContent); + + if (fileData.contains("charaSwayStringOffset")) { + auto& swaySaveData = fileData["charaSwayStringOffset"]; + for (auto& sway : swaySaveData.items()) { + const std::string keyStr = sway.key(); + const int key = atoi(keyStr.c_str()); + + const auto& value = sway.value(); + const float rate = value["rate"]; + const float P_bendStrength = value["P_bendStrength"]; + const float P_baseGravity = value["P_baseGravity"]; + const float P_inertiaMoment = value["P_inertiaMoment"]; + const float P_airResistance = value["P_airResistance"]; + const float P_deformResistance = value["P_deformResistance"]; + + if (auto it = charaSwayStringOffset.find(key); it != charaSwayStringOffset.end()) { + it->second.rate = rate; + it->second.P_bendStrength = P_bendStrength; + it->second.P_baseGravity = P_baseGravity; + it->second.P_inertiaMoment = P_inertiaMoment; + it->second.P_airResistance = P_airResistance; + it->second.P_deformResistance = P_deformResistance; + } + else { + charaSwayStringOffset.emplace(key, CharaSwayStringParam_t(rate, P_bendStrength, P_baseGravity, + P_inertiaMoment, P_airResistance, P_deformResistance)); + } + } + } + } + catch (std::exception& e) { + printf("initcharaSwayStringOffset failed: %s\n\n", e.what()); + } +} + void initcharaSwayStringOffset() { for (auto& i : swayTypes) { charaSwayStringOffset.emplace(i.first, CharaSwayStringParam_t()); } + loadGUIDataCache(); +} + +void saveGUIDataCache() { + try { + auto saveData = nlohmann::json::object(); + saveData["charaSwayStringOffset"] = nlohmann::json::object(); + + auto& swaySaveData = saveData["charaSwayStringOffset"]; + for (const auto& pair : charaSwayStringOffset) { + const auto currKey = std::to_string(pair.first); + swaySaveData[currKey] = nlohmann::json::object(); + swaySaveData[currKey]["rate"] = pair.second.rate; + swaySaveData[currKey]["P_bendStrength"] = pair.second.P_bendStrength; + swaySaveData[currKey]["P_baseGravity"] = pair.second.P_baseGravity; + swaySaveData[currKey]["P_inertiaMoment"] = pair.second.P_inertiaMoment; + swaySaveData[currKey]["P_airResistance"] = pair.second.P_airResistance; + swaySaveData[currKey]["P_deformResistance"] = pair.second.P_deformResistance; + } + + const auto saveStr = saveData.dump(4); + std::ofstream out("scsp-gui-save.json"); + out << saveStr; + out.close(); + } + catch (std::exception& e) { + printf("saveGUIDataCache error: %s\n", e.what()); + } } template diff --git a/src/scgui/scGUILoop.cpp b/src/scgui/scGUILoop.cpp index 77107df..e0ff16c 100644 --- a/src/scgui/scGUILoop.cpp +++ b/src/scgui/scGUILoop.cpp @@ -6,6 +6,7 @@ extern void* SetResolution_orig; // extern std::vector, int>> replaceDressResIds; extern std::map charaParam; extern CharaParam_t baseParam; +extern void saveGUIDataCache(); #define INPUT_AND_SLIDER_FLOAT(label, data, min, max) \ @@ -68,6 +69,10 @@ namespace SCGUILoop { currEditData.P_airResistance = 0; currEditData.P_deformResistance = 0; } + ImGui::SameLine(); + if (ImGui::Button("Save##sway")) { + saveGUIDataCache(); + } ImGui::NewLine(); } if (ImGui::IsItemHovered()) {