Skip to content

Commit

Permalink
Inventory Item Blueprints Library
Browse files Browse the repository at this point in the history
Preparations for Export/Import
  • Loading branch information
pavlicekdominik committed Oct 11, 2024
1 parent b8313fc commit a529327
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ class MOUNTEAINVENTORYEQUIPMENT_API UMounteaInventoryItemsLibrary : public UData
TArray<UMounteaInventoryItemBlueprint*> GetInventoryItemBlueprints() const;

UFUNCTION(BlueprintCallable, BlueprintPure, Category="Mountea|Inventory&Equipment|ItemBlueprintsLibrary", meta=(CustomTag="MounteaK2Getter"))
TArray<FMounteaInventoryItemsLibraryRow> GetInventoryItemBlueprintRows() const;
FORCEINLINE TArray<FMounteaInventoryItemsLibraryRow> GetInventoryItemBlueprintRows() const {return InventoryItemBlueprints; };

protected:

UPROPERTY(SaveGame, EditDefaultsOnly, BlueprintReadOnly, Category="Required", meta=(NoResetToDefault, UIMin=0,ClampMin=0, TitleProperty="{InitialQuantity} x {InventoryItemBlueprintName}"))
TArray<FMounteaInventoryItemsLibraryRow> InventoryItemBlueprints;


#if WITH_EDITOR

virtual void PostEditChangeChainProperty(struct FPropertyChangedChainEvent& PropertyChangedEvent) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ FString UMounteaInventorySerializationLibrary::ToJson(const FMounteaInventoryIte
return OutputString;
}


FString UMounteaInventorySerializationLibrary::ToJson(const FMounteaInventoryItemsLibraryRow& ItemBlueprintRow)
{
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);
Expand Down Expand Up @@ -112,7 +111,6 @@ FString UMounteaInventorySerializationLibrary::ToJson(const UMounteaInventoryIte
return OutputString;
}


FString UMounteaInventorySerializationLibrary::ToJson(const UMounteaInventoryItemsLibrary* ItemBlueprintsLibrary)
{
if (!IsValid(ItemBlueprintsLibrary))
Expand Down Expand Up @@ -153,3 +151,102 @@ FString UMounteaInventorySerializationLibrary::ToJson(const UMounteaInventoryIte

return OutputString;
}

FMounteaInventoryItemRequiredData UMounteaInventorySerializationLibrary::RequiredDataFromJson(const TSharedPtr<FJsonObject>& JsonObject)
{
FMounteaInventoryItemRequiredData resultItemData = FMounteaInventoryItemRequiredData();
if (!JsonObject.IsValid()) return resultItemData;

resultItemData.ItemName = FText::FromString(JsonObject->GetStringField(TEXT("ItemName")));
resultItemData.ItemShortInfo = FText::FromString(JsonObject->GetStringField(TEXT("ItemShortInfo")));
resultItemData.ItemLongInfo = FText::FromString(JsonObject->GetStringField(TEXT("ItemLongInfo")));
resultItemData.ItemFlags = JsonObject->GetNumberField(TEXT("ItemFlags"));

return resultItemData;
}

FMounteaInventoryItemOptionalData UMounteaInventorySerializationLibrary::OptionalDataFromJson(const TSharedPtr<FJsonObject>& JsonObject)
{
FMounteaInventoryItemOptionalData resultItemData = FMounteaInventoryItemOptionalData();
if (!JsonObject.IsValid()) return resultItemData;

resultItemData.bHasDurability = JsonObject->GetBoolField(TEXT("bHasDurability"));
resultItemData.MaxDurability = JsonObject->GetNumberField(TEXT("MaxDurability"));
resultItemData.BaseDurability = JsonObject->GetNumberField(TEXT("BaseDurability"));
resultItemData.DurabilityPenalization = JsonObject->GetNumberField(TEXT("DurabilityPenalization"));
resultItemData.DurabilityToPriceCoefficient = JsonObject->GetNumberField(TEXT("DurabilityToPriceCoefficient"));

resultItemData.bHasPrice = JsonObject->GetBoolField(TEXT("bHasPrice"));
resultItemData.BasePrice = JsonObject->GetNumberField(TEXT("BasePrice"));
resultItemData.SellPriceCoefficient = JsonObject->GetNumberField(TEXT("SellPriceCoefficient"));
resultItemData.MaxStackSize = JsonObject->GetNumberField(TEXT("MaxStackSize"));
resultItemData.MaxQuantity = JsonObject->GetNumberField(TEXT("MaxQuantity"));
resultItemData.bHasWeight = JsonObject->GetBoolField(TEXT("bHasWeight"));
resultItemData.Weight = JsonObject->GetNumberField(TEXT("Weight"));

return resultItemData;
}

UMounteaInventoryItemBlueprint* UMounteaInventorySerializationLibrary::ItemBlueprintFromJson(const TSharedPtr<FJsonObject>& JsonObject)
{
if (!JsonObject.IsValid()) return nullptr;

UMounteaInventoryItemBlueprint* resultBlueprint = NewObject<UMounteaInventoryItemBlueprint>();
resultBlueprint->SetBlueprintGuid(FGuid::NewGuid());

const TArray<TSharedPtr<FJsonValue>>* TagsJsonArray;
FGameplayTagContainer TagContainer;
if (JsonObject->TryGetArrayField(TEXT("DefaultTags"), TagsJsonArray))
{
for (const TSharedPtr<FJsonValue>& TagValue : *TagsJsonArray)
{
const FGameplayTag newGameplayTag = FGameplayTag::RequestGameplayTag(FName(*TagValue->AsString()));
TagContainer.AddTag(newGameplayTag);
}
resultBlueprint->SetDefaultTags(TagContainer);
}

resultBlueprint->SetPrimaryData(RequiredDataFromJson(JsonObject->GetObjectField(TEXT("PrimaryData"))));
resultBlueprint->SetSecondaryData(OptionalDataFromJson(JsonObject->GetObjectField(TEXT("SecondaryData"))));

return resultBlueprint;
}

FMounteaInventoryItemsLibraryRow UMounteaInventorySerializationLibrary::ItemBlueprintRowFromJson(const TSharedPtr<FJsonObject>& JsonObject)
{
FMounteaInventoryItemsLibraryRow resultRow = FMounteaInventoryItemsLibraryRow();
if (!JsonObject.IsValid()) return resultRow;

resultRow.InitialQuantity = JsonObject->GetIntegerField(TEXT("InitialQuantity"));

FString BlueprintJson = JsonObject->GetStringField(TEXT("InventoryItemBlueprint"));
TSharedPtr<FJsonObject> BlueprintJsonObject;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(BlueprintJson);

if (FJsonSerializer::Deserialize(Reader, BlueprintJsonObject))
{
UMounteaInventoryItemBlueprint* Blueprint = NewObject<UMounteaInventoryItemBlueprint>();
resultRow.InventoryItemBlueprint = ItemBlueprintFromJson(BlueprintJsonObject);
}

return resultRow;
}

UMounteaInventoryItemsLibrary* UMounteaInventorySerializationLibrary::ItemBlueprintsLibraryFromJson(const TSharedPtr<FJsonObject>& JsonObject)
{
if (!JsonObject.IsValid()) return nullptr;

UMounteaInventoryItemsLibrary* resultLibrary = NewObject<UMounteaInventoryItemsLibrary>();

const TArray<TSharedPtr<FJsonValue>>* RowsJsonArray;
if (JsonObject->TryGetArrayField(TEXT("ItemBlueprintsRows"), RowsJsonArray))
{
for (const TSharedPtr<FJsonValue>& RowValue : *RowsJsonArray)
{
FMounteaInventoryItemsLibraryRow Row = ItemBlueprintRowFromJson(RowValue->AsObject());
resultLibrary->GetInventoryItemBlueprintRows().Add(Row);
}
}

return resultLibrary;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@ class MOUNTEAINVENTORYEQUIPMENTEDITOR_API UMounteaInventorySerializationLibrary
static FString ToJson(const class UMounteaInventoryItemBlueprint* ItemBlueprint);

static FString ToJson(const class UMounteaInventoryItemsLibrary* ItemBlueprintsLibrary);

static FMounteaInventoryItemRequiredData RequiredDataFromJson(const TSharedPtr<FJsonObject>& JsonObject);

static FMounteaInventoryItemOptionalData OptionalDataFromJson(const TSharedPtr<FJsonObject>& JsonObject);

static FMounteaInventoryItemsLibraryRow ItemBlueprintRowFromJson(const TSharedPtr<FJsonObject>& JsonObject);

static UMounteaInventoryItemBlueprint* ItemBlueprintFromJson(const TSharedPtr<FJsonObject>& JsonObject);

static UMounteaInventoryItemsLibrary* ItemBlueprintsLibraryFromJson(const TSharedPtr<FJsonObject>& JsonObject);
};

0 comments on commit a529327

Please sign in to comment.