-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathweaponOffset.cpp
155 lines (129 loc) · 3.73 KB
/
weaponOffset.cpp
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
#include "weaponOffset.h"
#include <iostream>
#include <fstream>
#include <filesystem>
using json = nlohmann::json;
namespace F4VRBody {
WeaponOffset* g_weaponOffsets = nullptr;
std::string getSearchName(const std::string& name, const Mode mode) {
std::string searchName;
switch (mode) {
case powerArmor:
searchName = name + powerArmorSuffix;
break;
case offHand:
searchName = name + offHandSuffix;
break;
case offHandwithPowerArmor:
searchName = name + offHandSuffix + powerArmorSuffix;
break;
case normal:
default:
searchName = name;
break;
}
return searchName;
}
std::optional<NiTransform> WeaponOffset::getOffset(const std::string &name, const Mode mode) {
auto it = offsets.find(getSearchName(name, mode));
if (it == offsets.end()) {
switch (mode) { //check without PA
case powerArmor:
case offHandwithPowerArmor:
return getOffset(name);
default:
return { };
}
}
return it->second;
}
void WeaponOffset::addOffset(const std::string &name, NiTransform someData, const Mode mode) {
offsets[getSearchName(name, mode)] = someData;
}
void WeaponOffset::deleteOffset(const std::string& name, const Mode mode) {
offsets.erase(getSearchName(name, mode));
}
std::size_t WeaponOffset::getSize() {
return offsets.size();
}
void readOffsetJson() {
if (g_weaponOffsets) {
delete g_weaponOffsets;
}
g_weaponOffsets = new WeaponOffset();
loadOffsetJsonFile(); //load default list if it exists
if (!(std::filesystem::exists(offsetsPath) && std::filesystem::is_directory(offsetsPath)))
std::filesystem::create_directory(offsetsPath);
for (const auto& file : std::filesystem::directory_iterator(offsetsPath)) {
std::wstring path = L"";
try {
path = file.path().wstring();
}
catch (std::exception& e) {
_WARNING("Unable to convert path to string: %s", e.what());
}
if (file.exists() && !file.is_directory())
loadOffsetJsonFile(file.path().string());
}
}
void loadOffsetJsonFile(const std::string &file) {
json weaponJson;
std::ifstream inF;
inF.open(file, std::ios::in);
if (inF.fail()) {
_WARNING("cannot open %s", file.c_str());
inF.close();
return;
}
try
{
inF >> weaponJson;
}
catch (json::parse_error& ex)
{
_MESSAGE("cannot open %s: parse error at byte %d", file.c_str(), ex.byte);
inF.close();
return;
}
inF.close();
NiTransform data;
for (auto& [key, value] : weaponJson.items()) {
for (int i = 0; i < 12; i++) {
data.rot.arr[i] = value["rotation"][i].get<double>();
}
data.pos.x = value["x"].get<double>();
data.pos.y = value["y"].get<double>();
data.pos.z = value["z"].get<double>();
data.scale = value["scale"].get<double>();
g_weaponOffsets->addOffset(key, data);
}
_MESSAGE("Successfully loaded %d offsets from %s: total %d", weaponJson.size(), file.c_str(), g_weaponOffsets->getSize());
}
void saveOffsetJsonFile(const json& weaponJson, const std::string& file) {
std::ofstream outF;
outF.open(file, std::ios::out);
if (outF.fail()) {
_MESSAGE("cannot open %s for writing", file.c_str());
return;
}
try {
outF << std::setw(4) << weaponJson;
outF.close();
}catch (std::exception& e) {
outF.close();
_WARNING("Unable to save json %s: %s", file.c_str(), e.what());
}
}
void writeOffsetJson() {
std::string file;
for (auto& item : g_weaponOffsets->offsets) {
json weaponJson;
weaponJson[item.first]["rotation"] = item.second.rot.arr;
weaponJson[item.first]["x"] = item.second.pos.x;
weaponJson[item.first]["y"] = item.second.pos.y;
weaponJson[item.first]["z"] = item.second.pos.z;
weaponJson[item.first]["scale"] = item.second.scale;
saveOffsetJsonFile(weaponJson, offsetsPath + "\\" + item.first + ".json");
}
}
}