Skip to content

Commit 848d227

Browse files
refactor: UConfigPropertys are now directly given the json value from which they should deserialize
For `URawFormatValue::FromJson` of values capable of having children, if the `UConfigProperty` is avaliable, that `UConfigProperty` is now used to perform the deserialize of the child value.
1 parent 7cc979b commit 848d227

11 files changed

Lines changed: 77 additions & 7 deletions

File tree

Mods/SML/Source/SML/Private/Configuration/ConfigManager.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ void UConfigManager::LoadConfigurationInternal(const FConfigId& ConfigId, URootC
9696

9797
//Convert JSON tree into the raw value tree and feed it to root section value
9898
const TSharedRef<FJsonValue> RootValue = MakeShareable(new FJsonValueObject(JsonObject));
99-
URawFormatValue* RawFormatValue = URawFormatValueObject::FromJson(this, RootValue);
100-
RootConfigValueHolder->GetWrappedValue()->Deserialize(RawFormatValue);
99+
RootConfigValueHolder->GetWrappedValue()->DeserializeFromJson(RootValue);
101100

102101
UE_LOG(LogConfigManager, Display, TEXT("Successfully loaded configuration from %s"), *ConfigurationFilePath);
103102

Mods/SML/Source/SML/Public/Configuration/ConfigProperty.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#include "FGGameMode.h"
44
#include "UObject/Object.h"
55
#include "Configuration/CodeGeneration/ConfigVariableDescriptor.h"
6+
#include "Configuration/RawFileFormat/Json/JsonRawFormatConverter.h"
67
#include "Reflection/BlueprintReflectedObject.h"
8+
#include "SatisfactoryModLoader.h"
79
#include "ConfigProperty.generated.h"
810

911
class URawFormatValue;
@@ -59,6 +61,19 @@ class SML_API UConfigProperty : public UObject {
5961
UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
6062
void Deserialize(const URawFormatValue* Value);
6163

64+
/** Deserializes passed json value into this property state */
65+
void DeserializeFromJson(const TSharedPtr<FJsonValue>& JsonValue) {
66+
Deserialize(CreateRawFormatValue(this, JsonValue));
67+
}
68+
69+
/** Creates a raw format value from the passed json value */
70+
virtual URawFormatValue* CreateRawFormatValue(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue) {
71+
// This is a fallback. Derived classes should create a specific raw format value based on the expected type.
72+
UE_LOG(LogSatisfactoryModLoader, Warning, TEXT("Creating raw format value for property %s with fallback implementation."), *GetName());
73+
#pragma warning(suppress : 4996)
74+
return FJsonRawFormatConverter::ConvertToRawFormat(Outer, JsonValue);
75+
}
76+
6277
/** Marks this property directly, forcing file system synchronization to happen afterwards */
6378
UFUNCTION(BlueprintCallable)
6479
virtual void MarkDirty();

Mods/SML/Source/SML/Public/Configuration/Properties/ConfigPropertyArray.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "Configuration/ConfigProperty.h"
33
#include "Configuration/ConfigValueArrayInterface.h"
44
#include "Configuration/ConfigValueDirtyHandlerInterface.h"
5+
#include "Configuration/RawFileFormat/RawFormatValueArray.h"
56
#include "ConfigPropertyArray.generated.h"
67

78
/** Describes array configuration property with single nested element type */
@@ -58,6 +59,10 @@ class SML_API UConfigPropertyArray : public UConfigProperty, public IConfigValue
5859
virtual bool ResetToDefault_Implementation() override;
5960
virtual bool IsSetToDefaultValue_Implementation() const override;
6061
virtual FString GetDefaultValueAsString_Implementation() const override;
62+
63+
virtual URawFormatValueArray* CreateRawFormatValue(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue) override {
64+
return URawFormatValueArray::FromJson(Outer, JsonValue);
65+
}
6166
//End UConfigProperty
6267

6368
//Begin IConfigValueDirtyHandlerInterface

Mods/SML/Source/SML/Public/Configuration/Properties/ConfigPropertyBool.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include "Configuration/ConfigProperty.h"
3+
#include "Configuration/RawFileFormat/RawFormatValueBool.h"
34
#include "ConfigPropertyBool.generated.h"
45

56
UCLASS()
@@ -32,5 +33,9 @@ class SML_API UConfigPropertyBool : public UConfigProperty {
3233
virtual bool ResetToDefault_Implementation() override;
3334
virtual bool IsSetToDefaultValue_Implementation() const override;
3435
virtual FString GetDefaultValueAsString_Implementation() const override;
36+
37+
virtual URawFormatValueBool* CreateRawFormatValue(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue) override {
38+
return URawFormatValueBool::FromJson(Outer, JsonValue);
39+
}
3540
//End UConfigProperty
3641
};

Mods/SML/Source/SML/Public/Configuration/Properties/ConfigPropertyClass.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include "Configuration/ConfigProperty.h"
3+
#include "Configuration/RawFileFormat/RawFormatValueString.h"
34
#include "ConfigPropertyClass.generated.h"
45

56
UCLASS()
@@ -60,5 +61,9 @@ class SML_API UConfigPropertyClass : public UConfigProperty {
6061
virtual bool ResetToDefault_Implementation() override;
6162
virtual bool IsSetToDefaultValue_Implementation() const override;
6263
virtual FString GetDefaultValueAsString_Implementation() const override;
64+
65+
virtual URawFormatValueString* CreateRawFormatValue(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue) override {
66+
return URawFormatValueString::FromJson(Outer, JsonValue);
67+
}
6368
//End UConfigProperty
6469
};

Mods/SML/Source/SML/Public/Configuration/Properties/ConfigPropertyFloat.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include "Configuration/ConfigProperty.h"
3+
#include "Configuration/RawFileFormat/RawFormatValueNumber.h"
34
#include "ConfigPropertyFloat.generated.h"
45

56
UCLASS()
@@ -32,5 +33,9 @@ class SML_API UConfigPropertyFloat : public UConfigProperty {
3233
virtual bool ResetToDefault_Implementation() override;
3334
virtual bool IsSetToDefaultValue_Implementation() const override;
3435
virtual FString GetDefaultValueAsString_Implementation() const override;
36+
37+
virtual URawFormatValueNumber* CreateRawFormatValue(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue) override {
38+
return URawFormatValueNumber::FromJson(Outer, JsonValue);
39+
}
3540
//End UConfigProperty
3641
};

Mods/SML/Source/SML/Public/Configuration/Properties/ConfigPropertyInteger.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include "Configuration/ConfigProperty.h"
3+
#include "Configuration/RawFileFormat/RawFormatValueNumber.h"
34
#include "ConfigPropertyInteger.generated.h"
45

56
UCLASS()
@@ -32,5 +33,9 @@ class SML_API UConfigPropertyInteger : public UConfigProperty {
3233
virtual bool ResetToDefault_Implementation() override;
3334
virtual bool IsSetToDefaultValue_Implementation() const override;
3435
virtual FString GetDefaultValueAsString_Implementation() const override;
36+
37+
virtual URawFormatValueNumber* CreateRawFormatValue(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue) override {
38+
return URawFormatValueNumber::FromJson(Outer, JsonValue);
39+
}
3540
//End UConfigProperty
3641
};

Mods/SML/Source/SML/Public/Configuration/Properties/ConfigPropertySection.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "Configuration/ConfigProperty.h"
33
#include "Configuration/ConfigValueObjectInterface.h"
44
#include "Configuration/ConfigValueDirtyHandlerInterface.h"
5+
#include "Configuration/RawFileFormat/RawFormatValueObject.h"
56
#include "ConfigPropertySection.generated.h"
67

78
/** Describes a single configuration section with nested properties */
@@ -38,6 +39,10 @@ class SML_API UConfigPropertySection : public UConfigProperty, public IConfigVal
3839
virtual bool ResetToDefault_Implementation() override;
3940
virtual bool IsSetToDefaultValue_Implementation() const override;
4041
virtual FString GetDefaultValueAsString_Implementation() const override;
42+
43+
virtual URawFormatValueObject* CreateRawFormatValue(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue) override {
44+
return URawFormatValueObject::FromJson(Outer, JsonValue);
45+
}
4146
//End UConfigProperty
4247

4348
//Begin IConfigValueDirtyHandlerInterface

Mods/SML/Source/SML/Public/Configuration/Properties/ConfigPropertyString.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include "Configuration/ConfigProperty.h"
3+
#include "Configuration/RawFileFormat/RawFormatValueString.h"
34
#include "ConfigPropertyString.generated.h"
45

56
UCLASS()
@@ -32,5 +33,9 @@ class SML_API UConfigPropertyString : public UConfigProperty {
3233
virtual bool ResetToDefault_Implementation() override;
3334
virtual bool IsSetToDefaultValue_Implementation() const override;
3435
virtual FString GetDefaultValueAsString_Implementation() const override;
36+
37+
virtual URawFormatValueString* CreateRawFormatValue(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue) override {
38+
return URawFormatValueString::FromJson(Outer, JsonValue);
39+
}
3540
//End UConfigProperty
3641
};

Mods/SML/Source/SML/Public/Configuration/RawFileFormat/RawFormatValueArray.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include "Configuration/ConfigProperty.h"
3+
#include "Configuration/ConfigValueArrayInterface.h"
34
#include "Configuration/RawFileFormat/Json/JsonRawFormatConverter.h"
45
#include "Configuration/RawFileFormat/RawFormatValue.h"
56
#include "Configuration/RawFileFormat/RawFormatValueNumber.h"
@@ -128,11 +129,22 @@ FORCEINLINE TSharedPtr<FJsonValue> URawFormatValueArray::ToJson() const {
128129

129130
FORCEINLINE URawFormatValueArray* URawFormatValueArray::FromJson(UObject* Outer, const TSharedPtr<FJsonValue>& JsonValue) {
130131
URawFormatValueArray* Array = NewObject<URawFormatValueArray>(Outer);
131-
for (const TSharedPtr<FJsonValue>& ChildValue : JsonValue->AsArray()) {
132-
// TODO: Base child property deserialization on the child config property.
132+
const TArray<TSharedPtr<FJsonValue>>& JsonArray = JsonValue->AsArray();
133+
const int32 ChildNum = JsonArray.Num();
134+
for (int32 ChildIndex = 0; ChildIndex < ChildNum; ChildIndex++) {
135+
const TSharedPtr<FJsonValue>& ChildValue = JsonArray[ChildIndex];
136+
UConfigProperty* Owner = Cast<UConfigProperty>(Outer);
137+
if (IsValid(Owner) && Owner->Implements<UConfigValueArrayInterface>()) {
138+
UConfigProperty* ChildProperty = IConfigValueArrayInterface::Execute_GetChildProperty(Owner, ChildIndex);
139+
if (IsValid(ChildProperty)) {
140+
Array->AddValue(ChildProperty->CreateRawFormatValue(Array, ChildValue));
141+
continue;
142+
}
143+
}
144+
133145
UE_LOG(LogSatisfactoryModLoader, Warning, TEXT("Deserializing JSON array value for property with fallback implementation."));
134146
#pragma warning(suppress : 4996)
135147
Array->AddValue(FJsonRawFormatConverter::ConvertToRawFormat(Array, ChildValue));
136148
}
137149
return Array;
138-
}
150+
}

0 commit comments

Comments
 (0)