Unreal access to the PixoVR Platform
Loading...
Searching...
No Matches
PPlatformCoreSubsystem.cpp
Go to the documentation of this file.
1// Copyright 2023 PixoVR Corp. All Rights Reserved.
2
4#include "AssetRegistry/AssetRegistryModule.h"
5#include "Interfaces/IPluginManager.h"
6#include "Misc/FileHelper.h"
7#include "Serialization/JsonReader.h"
8#include "Serialization/JsonSerializer.h"
10
11#if WITH_EDITOR
12#include "Settings/ProjectPackagingSettings.h"
13#endif
14
15DEFINE_LOG_CATEGORY_STATIC(LogPPCoreSubsystem, Log, All);
16
17#define Log(pmt, ...) UE_LOG(LogPPCoreSubsystem, Log, TEXT(pmt), ##__VA_ARGS__)
18#define Warn(pmt, ...) UE_LOG(LogPPCoreSubsystem, Warning, TEXT(pmt), ##__VA_ARGS__)
19#define Error(pmt, ...) UE_LOG(LogPPCoreSubsystem, Error, TEXT(pmt), ##__VA_ARGS__)
20#define Fatal(pmt, ...) UE_LOG(LogPPCoreSubsystem, Fatal, TEXT(pmt), ##__VA_ARGS__)
21
23{
24 static FString ContentDir = IPluginManager::Get().FindPlugin(TEXT("PixoPlatformCore"))->GetContentDir();
25 return ContentDir;
26}
27
29{
30 static FString ContentDir = "";
31
32 if (ContentDir.IsEmpty())
33 {
34 ContentDir = IPluginManager::Get().FindPlugin(TEXT("PixoPlatformCore"))->GetContentDir();
35 FPaths::CollapseRelativeDirectories(ContentDir);
36 FPaths::MakePathRelativeTo(ContentDir, *FPaths::ProjectContentDir());
37 }
38
39 Log("Calling in content %s.", *ContentDir);
40 return ContentDir;
41}
42
47
48void UPPlatformCoreSubsystem::Initialize(FSubsystemCollectionBase& Collection)
49{
50 Super::Initialize(Collection);
51
52#if WITH_EDITOR
54#endif
56}
57
59{
60 Log("Finding all non-ufs assets.");
61
62 TArray<FString> JsonFiles;
63 FString PlatformConfig = PluginContent() / TEXT("PPlatformConfigurations/PlatformConfig.json");
64
65 if (IFileManager::Get().FileExists(*PlatformConfig))
66 {
67 Log("File exists!");
68 FString ConfigJsonString;
69 FFileHelper::LoadFileToString(ConfigJsonString, *PlatformConfig);
70
71 ConfigurationJsonObject = MakeShareable(new FJsonObject());
72 TSharedRef<TJsonReader<>> JsonReader = TJsonReaderFactory<>::Create(ConfigJsonString);
73
74 if (FJsonSerializer::Deserialize(JsonReader, ConfigurationJsonObject) && ConfigurationJsonObject.IsValid())
75 {
76 Log("Deserialized the JSON config.");
78
79 }
80 else
81 {
82 Log("Failed to deserialize the JSON config.");
83 }
84 }
85}
86
88{
89 if (!ConfigurationJsonObject.IsValid())
90 {
91 return;
92 }
93
94 if (UPPlatformCoreSettings* Settings = GetMutableDefault<UPPlatformCoreSettings>())
95 {
96 TArray<FString> ConfigKeys;
97 if (ConfigurationJsonObject->Values.GetKeys(ConfigKeys) > 0)
98 {
99 Settings->SelectableSections.Empty();
100 Settings->SelectableSections = ConfigKeys;
101
102 if (Settings->ConfigSection.IsEmpty())
103 {
104 Settings->ConfigSection = ConfigKeys[0];
105 }
106
107 GetSettingsFromRegion(Settings->ConfigSection);
108 }
109 }
110}
111
113{
114 return GetSettingsFromRegion(Section);
115}
116
118{
119 if (!ConfigurationJsonObject.IsValid())
120 {
121 Warn("No JSON loaded.");
122 return false;
123 }
124
125 if (UPPlatformCoreSettings* Settings = GetMutableDefault<UPPlatformCoreSettings>())
126 {
127 const TSharedPtr<FJsonObject>* RegionSettings;
128 if (ConfigurationJsonObject->TryGetObjectField(Region, RegionSettings))
129 {
130 Log("Found the region. Loading into settings.");
131 Settings->ProtocolVersion = (*RegionSettings)->GetStringField("protocol");
132 Settings->PlatformName = (*RegionSettings)->GetStringField("name");
133 Settings->IconURL = (*RegionSettings)->GetStringField("icon");
134 Settings->Region = (*RegionSettings)->GetStringField("region");
135 Settings->bDevMode = (*RegionSettings)->GetBoolField("dev");
136 const TSharedPtr<FJsonObject>* Services;
137 if ((*RegionSettings)->TryGetObjectField("services", Services))
138 {
139 const TSharedPtr<FJsonObject>* PlatformSetting;
140 if ((*Services)->TryGetObjectField("platform", PlatformSetting))
141 {
142 Settings->PlatformURL = (*PlatformSetting)->GetStringField("platform_url");
143 }
144
145 const TSharedPtr<FJsonObject>* ExtendedPlatformSetting;
146 if ((*Services)->TryGetObjectField("extended_platform", ExtendedPlatformSetting))
147 {
148 Settings->ModuleListURLFormat = (*ExtendedPlatformSetting)->GetStringField("module_list_url");
149 }
150
151 const TSharedPtr<FJsonObject>* MultiplayerSetting;
152 if ((*Services)->TryGetObjectField("multiplayer", MultiplayerSetting))
153 {
154 Settings->MatchmakingURL = (*MultiplayerSetting)->GetStringField("matchmaker_url");
155 }
156
157 const TSharedPtr<FJsonObject>* VoiceSetting;
158 if ((*Services)->TryGetObjectField("voice", VoiceSetting))
159 {
160 Settings->VoiceServerURL = (*VoiceSetting)->GetStringField("voice_url");
161 FString VoiceAccessKey;
162 if ((*VoiceSetting)->TryGetStringField("voice_access_key", VoiceAccessKey))
163 {
164 Settings->VoiceAccessKey = VoiceAccessKey;
165 }
166 FString VoiceTokenAudience;
167 if ((*VoiceSetting)->TryGetStringField("voice_token_audience", VoiceTokenAudience))
168 {
169 Settings->VoiceTokenAudience = VoiceTokenAudience;
170 }
171 }
172 }
173
174 Settings->OnSettingsChanged.Broadcast(Settings);
175 Settings->OnStaticSettingsChanged.Broadcast(Settings);
176 }
177
178 return true;
179 }
180
181 Log("Failed to find region %s.", *Region);
182 return false;
183}
184
186{
187#if WITH_EDITOR
188 static const FString DefaultGamePath = FString::Printf(TEXT("%sDefaultGame.ini"), *FPaths::SourceConfigDir());
189
190 //DirectoriesToAlwaysStageAsUFS - UProjectPackagingSettings
191 bool bDoesContainConfig = false;
192 Log("Checking in the package properties...");
193
194 if (UProjectPackagingSettings* Settings = GetMutableDefault<UProjectPackagingSettings>())
195 {
196 bDoesContainConfig = Settings->DirectoriesToAlwaysStageAsNonUFS.ContainsByPredicate([this](const FDirectoryPath& InValue) -> bool {
197 if (InValue.Path.Contains("PPlatformConfigurations"))
198 {
199 Log("Found the platform config already in the ini file.");
200 return true;
201 }
202 return false;
203 });
204
205 if (!bDoesContainConfig)
206 {
207 Log("Did not find the config file. Adding it to the packaging step.");
208 FString PlatformConfigPath = PluginContentRelativeGameContent() / TEXT("PPlatformConfigurations");
209 FString PlatformConfigPathAbs = PluginContent() / TEXT("PPlatformConfigurations/PlatformConfig.json");
210 if (!IFileManager::Get().FileExists(*PlatformConfigPathAbs))
211 {
212 Fatal("%s does not exist! Make sure you have all of the files and that they are in the correct place.", *PlatformConfigPath);
213 }
214 //PlatformConfigPath = "(Path=\"" + PlatformConfigPath + "\")";
215 Log("Inserting %s into the DirectoriesToAlwaysStageAsUFS array.");
216 FDirectoryPath AddedDirectoryPath;
217 AddedDirectoryPath.Path = PlatformConfigPath;
218
219 Settings->DirectoriesToAlwaysStageAsNonUFS.Add(AddedDirectoryPath);
220 Settings->UpdateSinglePropertyInConfigFile(
221 Settings->GetClass()->FindPropertyByName(GET_MEMBER_NAME_CHECKED(UProjectPackagingSettings, DirectoriesToAlwaysStageAsNonUFS)),
222 Settings->GetDefaultConfigFilename());
223 }
224 }
225#endif
226}
227
228#undef Fatal
229#undef Error
230#undef Warn
231#undef Log
FString PluginContent()
FString PluginContentRelativeGameContent()
DEFINE_LOG_CATEGORY_STATIC(LogPPCoreSubsystem, Log, All)
#define Log(pmt,...)
#define Fatal(pmt,...)
#define Warn(pmt,...)
#define Log(pmt,...)
UCLASS(config = Game, defaultconfig)
bool GetSettingsFromRegion(const FString &Region)
virtual void Initialize(FSubsystemCollectionBase &Collection) override
TSharedPtr< FJsonObject > ConfigurationJsonObject
bool TryChangeSettingsSection(const FString &Section)
UFUNCTION(BlueprintCallable, Category = "Pixo Platform")