Merge pull request #12818 from iwubcode/json_file_operations
Common: add Json helper utilities for loading or saving to a file
This commit is contained in:
commit
a95c3dbc97
|
@ -87,6 +87,7 @@ add_library(common
|
||||||
JitRegister.cpp
|
JitRegister.cpp
|
||||||
JitRegister.h
|
JitRegister.h
|
||||||
JsonUtil.h
|
JsonUtil.h
|
||||||
|
JsonUtil.cpp
|
||||||
Lazy.h
|
Lazy.h
|
||||||
LinearDiskCache.h
|
LinearDiskCache.h
|
||||||
Logging/ConsoleListener.h
|
Logging/ConsoleListener.h
|
||||||
|
|
|
@ -3,6 +3,10 @@
|
||||||
|
|
||||||
#include "Common/JsonUtil.h"
|
#include "Common/JsonUtil.h"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#include "Common/FileUtil.h"
|
||||||
|
|
||||||
picojson::object ToJsonObject(const Common::Vec3& vec)
|
picojson::object ToJsonObject(const Common::Vec3& vec)
|
||||||
{
|
{
|
||||||
picojson::object obj;
|
picojson::object obj;
|
||||||
|
@ -38,3 +42,27 @@ std::optional<bool> ReadBoolFromJson(const picojson::object& obj, const std::str
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
return it->second.get<bool>();
|
return it->second.get<bool>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool JsonToFile(const std::string& filename, const picojson::value& root, bool prettify)
|
||||||
|
{
|
||||||
|
std::ofstream json_stream;
|
||||||
|
File::OpenFStream(json_stream, filename, std::ios_base::out);
|
||||||
|
if (!json_stream.is_open())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
json_stream << root.serialize(prettify);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool JsonFromFile(const std::string& filename, picojson::value* root, std::string* error)
|
||||||
|
{
|
||||||
|
std::string json_data;
|
||||||
|
if (!File::ReadFileToString(filename, json_data))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
*error = picojson::parse(*root, json_data);
|
||||||
|
return error->empty();
|
||||||
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@
|
||||||
|
|
||||||
#include <picojson.h>
|
#include <picojson.h>
|
||||||
|
|
||||||
#include "Common/MathUtil.h"
|
|
||||||
#include "Common/Matrix.h"
|
#include "Common/Matrix.h"
|
||||||
|
|
||||||
// Ideally this would use a concept like, 'template <std::ranges::range Range>' to constrain it,
|
// Ideally this would use a concept like, 'template <std::ranges::range Range>' to constrain it,
|
||||||
|
@ -47,7 +46,7 @@ std::optional<Type> ReadNumericFromJson(const picojson::object& obj, const std::
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
if (!it->second.is<double>())
|
if (!it->second.is<double>())
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
return MathUtil::SaturatingCast<Type>(it->second.get<double>());
|
return static_cast<Type>(it->second.get<double>());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<std::string> ReadStringFromJson(const picojson::object& obj, const std::string& key);
|
std::optional<std::string> ReadStringFromJson(const picojson::object& obj, const std::string& key);
|
||||||
|
@ -56,3 +55,6 @@ std::optional<bool> ReadBoolFromJson(const picojson::object& obj, const std::str
|
||||||
|
|
||||||
picojson::object ToJsonObject(const Common::Vec3& vec);
|
picojson::object ToJsonObject(const Common::Vec3& vec);
|
||||||
void FromJson(const picojson::object& obj, Common::Vec3& vec);
|
void FromJson(const picojson::object& obj, Common::Vec3& vec);
|
||||||
|
|
||||||
|
bool JsonToFile(const std::string& filename, const picojson::value& root, bool prettify = false);
|
||||||
|
bool JsonFromFile(const std::string& filename, picojson::value* root, std::string* error);
|
||||||
|
|
|
@ -813,6 +813,7 @@
|
||||||
<ClCompile Include="Common\IniFile.cpp" />
|
<ClCompile Include="Common\IniFile.cpp" />
|
||||||
<ClCompile Include="Common\IOFile.cpp" />
|
<ClCompile Include="Common\IOFile.cpp" />
|
||||||
<ClCompile Include="Common\JitRegister.cpp" />
|
<ClCompile Include="Common\JitRegister.cpp" />
|
||||||
|
<ClCompile Include="Common\JsonUtil.cpp" />
|
||||||
<ClCompile Include="Common\LdrWatcher.cpp" />
|
<ClCompile Include="Common\LdrWatcher.cpp" />
|
||||||
<ClCompile Include="Common\Logging\ConsoleListenerWin.cpp" />
|
<ClCompile Include="Common\Logging\ConsoleListenerWin.cpp" />
|
||||||
<ClCompile Include="Common\Logging\LogManager.cpp" />
|
<ClCompile Include="Common\Logging\LogManager.cpp" />
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include "Common/CommonPaths.h"
|
#include "Common/CommonPaths.h"
|
||||||
#include "Common/FileUtil.h"
|
#include "Common/FileUtil.h"
|
||||||
#include "Common/IniFile.h"
|
#include "Common/IniFile.h"
|
||||||
|
#include "Common/JsonUtil.h"
|
||||||
#include "Common/Logging/Log.h"
|
#include "Common/Logging/Log.h"
|
||||||
#include "Common/StringUtil.h"
|
#include "Common/StringUtil.h"
|
||||||
#include "Core/ConfigManager.h"
|
#include "Core/ConfigManager.h"
|
||||||
|
@ -23,18 +24,9 @@ namespace InputCommon::DynamicInputTextures
|
||||||
{
|
{
|
||||||
Configuration::Configuration(const std::string& json_path)
|
Configuration::Configuration(const std::string& json_path)
|
||||||
{
|
{
|
||||||
std::string json_data;
|
|
||||||
if (!File::ReadFileToString(json_path, json_data))
|
|
||||||
{
|
|
||||||
ERROR_LOG_FMT(VIDEO, "Failed to load dynamic input json file '{}'", json_path);
|
|
||||||
m_valid = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
picojson::value root;
|
picojson::value root;
|
||||||
const auto error = picojson::parse(root, json_data);
|
std::string error;
|
||||||
|
if (!JsonFromFile(json_path, &root, &error))
|
||||||
if (!error.empty())
|
|
||||||
{
|
{
|
||||||
ERROR_LOG_FMT(VIDEO, "Failed to load dynamic input json file '{}' due to parse error: {}",
|
ERROR_LOG_FMT(VIDEO, "Failed to load dynamic input json file '{}' due to parse error: {}",
|
||||||
json_path, error);
|
json_path, error);
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
#include "Common/FileUtil.h"
|
#include "Common/FileUtil.h"
|
||||||
#include "Common/IOFile.h"
|
#include "Common/IOFile.h"
|
||||||
|
#include "Common/JsonUtil.h"
|
||||||
#include "Common/Logging/Log.h"
|
#include "Common/Logging/Log.h"
|
||||||
#include "Common/StringUtil.h"
|
#include "Common/StringUtil.h"
|
||||||
#include "VideoCommon/Assets/MaterialAsset.h"
|
#include "VideoCommon/Assets/MaterialAsset.h"
|
||||||
|
@ -133,24 +134,16 @@ CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadPixelShader(const
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string json_data;
|
|
||||||
if (!File::ReadFileToString(PathToString(metadata->second), json_data))
|
|
||||||
{
|
|
||||||
ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to load the json file '{}',", asset_id,
|
|
||||||
PathToString(metadata->second));
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
picojson::value root;
|
picojson::value root;
|
||||||
const auto error = picojson::parse(root, json_data);
|
std::string error;
|
||||||
|
if (!JsonFromFile(PathToString(metadata->second), &root, &error))
|
||||||
if (!error.empty())
|
|
||||||
{
|
{
|
||||||
ERROR_LOG_FMT(VIDEO,
|
ERROR_LOG_FMT(VIDEO,
|
||||||
"Asset '{}' error - failed to load the json file '{}', due to parse error: {}",
|
"Asset '{}' error - failed to load the json file '{}', due to parse error: {}",
|
||||||
asset_id, PathToString(metadata->second), error);
|
asset_id, PathToString(metadata->second), error);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!root.is<picojson::object>())
|
if (!root.is<picojson::object>())
|
||||||
{
|
{
|
||||||
ERROR_LOG_FMT(
|
ERROR_LOG_FMT(
|
||||||
|
@ -181,18 +174,21 @@ CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadMaterial(const As
|
||||||
}
|
}
|
||||||
const auto& asset_path = asset_map.begin()->second;
|
const auto& asset_path = asset_map.begin()->second;
|
||||||
|
|
||||||
std::string json_data;
|
std::size_t metadata_size;
|
||||||
if (!File::ReadFileToString(PathToString(asset_path), json_data))
|
|
||||||
{
|
{
|
||||||
ERROR_LOG_FMT(VIDEO, "Asset '{}' error - material failed to load the json file '{}',",
|
std::error_code ec;
|
||||||
asset_id, PathToString(asset_path));
|
metadata_size = std::filesystem::file_size(asset_path, ec);
|
||||||
return {};
|
if (ec)
|
||||||
|
{
|
||||||
|
ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to get material file size with error '{}'!",
|
||||||
|
asset_id, ec);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
picojson::value root;
|
picojson::value root;
|
||||||
const auto error = picojson::parse(root, json_data);
|
std::string error;
|
||||||
|
if (!JsonFromFile(PathToString(asset_path), &root, &error))
|
||||||
if (!error.empty())
|
|
||||||
{
|
{
|
||||||
ERROR_LOG_FMT(
|
ERROR_LOG_FMT(
|
||||||
VIDEO,
|
VIDEO,
|
||||||
|
@ -220,7 +216,7 @@ CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadMaterial(const As
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
return LoadInfo{json_data.size(), GetLastAssetWriteTime(asset_id)};
|
return LoadInfo{metadata_size, GetLastAssetWriteTime(asset_id)};
|
||||||
}
|
}
|
||||||
|
|
||||||
CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadMesh(const AssetID& asset_id,
|
CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadMesh(const AssetID& asset_id,
|
||||||
|
@ -292,18 +288,9 @@ CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadMesh(const AssetI
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string json_data;
|
|
||||||
if (!File::ReadFileToString(PathToString(metadata->second), json_data))
|
|
||||||
{
|
|
||||||
ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to load the json file '{}'!", asset_id,
|
|
||||||
PathToString(metadata->second));
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
picojson::value root;
|
picojson::value root;
|
||||||
const auto error = picojson::parse(root, json_data);
|
std::string error;
|
||||||
|
if (!JsonFromFile(PathToString(metadata->second), &root, &error))
|
||||||
if (!error.empty())
|
|
||||||
{
|
{
|
||||||
ERROR_LOG_FMT(VIDEO,
|
ERROR_LOG_FMT(VIDEO,
|
||||||
"Asset '{}' error - failed to load the json file '{}', due to parse error: {}",
|
"Asset '{}' error - failed to load the json file '{}', due to parse error: {}",
|
||||||
|
@ -362,18 +349,9 @@ CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadTexture(const Ass
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string json_data;
|
|
||||||
if (!File::ReadFileToString(PathToString(metadata->second), json_data))
|
|
||||||
{
|
|
||||||
ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to load the json file '{}',", asset_id,
|
|
||||||
PathToString(metadata->second));
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
picojson::value root;
|
picojson::value root;
|
||||||
const auto error = picojson::parse(root, json_data);
|
std::string error;
|
||||||
|
if (!JsonFromFile(PathToString(metadata->second), &root, &error))
|
||||||
if (!error.empty())
|
|
||||||
{
|
{
|
||||||
ERROR_LOG_FMT(VIDEO,
|
ERROR_LOG_FMT(VIDEO,
|
||||||
"Asset '{}' error - failed to load the json file '{}', due to parse error: {}",
|
"Asset '{}' error - failed to load the json file '{}', due to parse error: {}",
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include "Common/CommonPaths.h"
|
#include "Common/CommonPaths.h"
|
||||||
#include "Common/FileUtil.h"
|
#include "Common/FileUtil.h"
|
||||||
|
#include "Common/JsonUtil.h"
|
||||||
#include "Common/Logging/Log.h"
|
#include "Common/Logging/Log.h"
|
||||||
#include "Common/StringUtil.h"
|
#include "Common/StringUtil.h"
|
||||||
|
|
||||||
|
@ -15,17 +16,9 @@
|
||||||
std::optional<GraphicsModConfig> GraphicsModConfig::Create(const std::string& file_path,
|
std::optional<GraphicsModConfig> GraphicsModConfig::Create(const std::string& file_path,
|
||||||
Source source)
|
Source source)
|
||||||
{
|
{
|
||||||
std::string json_data;
|
|
||||||
if (!File::ReadFileToString(file_path, json_data))
|
|
||||||
{
|
|
||||||
ERROR_LOG_FMT(VIDEO, "Failed to load graphics mod json file '{}'", file_path);
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
picojson::value root;
|
picojson::value root;
|
||||||
const auto error = picojson::parse(root, json_data);
|
std::string error;
|
||||||
|
if (!JsonFromFile(file_path, &root, &error))
|
||||||
if (!error.empty())
|
|
||||||
{
|
{
|
||||||
ERROR_LOG_FMT(VIDEO, "Failed to load graphics mod json file '{}' due to parse error: {}",
|
ERROR_LOG_FMT(VIDEO, "Failed to load graphics mod json file '{}' due to parse error: {}",
|
||||||
file_path, error);
|
file_path, error);
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include "Common/CommonPaths.h"
|
#include "Common/CommonPaths.h"
|
||||||
#include "Common/FileSearch.h"
|
#include "Common/FileSearch.h"
|
||||||
#include "Common/FileUtil.h"
|
#include "Common/FileUtil.h"
|
||||||
|
#include "Common/JsonUtil.h"
|
||||||
#include "Common/Logging/Log.h"
|
#include "Common/Logging/Log.h"
|
||||||
#include "Common/StringUtil.h"
|
#include "Common/StringUtil.h"
|
||||||
#include "Core/ConfigManager.h"
|
#include "Core/ConfigManager.h"
|
||||||
|
@ -42,17 +43,9 @@ void GraphicsModGroupConfig::Load()
|
||||||
std::set<std::string> known_paths;
|
std::set<std::string> known_paths;
|
||||||
if (File::Exists(file_path))
|
if (File::Exists(file_path))
|
||||||
{
|
{
|
||||||
std::string json_data;
|
|
||||||
if (!File::ReadFileToString(file_path, json_data))
|
|
||||||
{
|
|
||||||
ERROR_LOG_FMT(VIDEO, "Failed to load graphics mod group json file '{}'", file_path);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
picojson::value root;
|
picojson::value root;
|
||||||
const auto error = picojson::parse(root, json_data);
|
std::string error;
|
||||||
|
if (!JsonFromFile(file_path, &root, &error))
|
||||||
if (!error.empty())
|
|
||||||
{
|
{
|
||||||
ERROR_LOG_FMT(VIDEO,
|
ERROR_LOG_FMT(VIDEO,
|
||||||
"Failed to load graphics mod group json file '{}' due to parse error: {}",
|
"Failed to load graphics mod group json file '{}' due to parse error: {}",
|
||||||
|
|
Loading…
Reference in New Issue