UE4编辑器在文件改变的时候会回调 ··· void FFileCache::OnDirectoryChanged(const TArray& FileChanges) ···
对改接口的阅读后在项目里编辑器修改模块中增加自己的代码
监听文件变化
void EditorModule::LobbyDataTableInit()
{
CollectionFolders[ECollectionShareType::CST_Shared] = FPaths::ProjectContentDir() / TEXT("GameData");
WatcherDelegate = FDelegateHandle();
FDirectoryWatcherModule& Module = FModuleManager::LoadModuleChecked<FDirectoryWatcherModule>(TEXT("DirectoryWatcher"));
if (IDirectoryWatcher* DirectoryWatcher = Module.Get())
{
for (int32 CacheIdx = 0; CacheIdx < ECollectionShareType::CST_All; ++CacheIdx)
{
auto Callback = IDirectoryWatcher::FDirectoryChanged::CreateRaw(this, &EditorModule::DataTableToJson);
const FString& CollectionFolder = CollectionFolders[CacheIdx];
if (CollectionFolder.IsEmpty())
{
continue;
}
if (!IFileManager::Get().MakeDirectory(*CollectionFolder, true))
{
continue;
}
DirectoryWatcher::FFileCacheConfig FileCacheConfig(FPaths::ConvertRelativePathToFull(CollectionFolder), FString());
FileCacheConfig.DetectMoves(false);
FileCacheConfig.RequireFileHashes(false);
if (!DirectoryWatcher->RegisterDirectoryChangedCallback_Handle(FileCacheConfig.Directory, Callback, WatcherDelegate))
{
UE_LOG(LogTemp, Error, TEXT("LobbyDataTableInit. Failed registering directory watcher for folder"));
}
}
}
}
文件变化的时候Json的转换
void EditorModule::DataTableToJson(const TArray<FFileChangeData>& FileChanges)
{
TArray<FString> LobbyDataTables = UDSGameSettings::Get()->LobbyDataTables;
TMap<FString, bool> AlreadyFile;
for (const FFileChangeData& ThisEntry : FileChanges)
{
FString FilePath = ThisEntry.Filename;
FString FileBaseName = FPaths::GetBaseFilename(FilePath);
if (LobbyDataTables.Find(FileBaseName) == INDEX_NONE)
{
continue;
}
if (!AlreadyFile.Contains(FilePath) && (ThisEntry.Action == FFileChangeData::FCA_Added || ThisEntry.Action == FFileChangeData::FCA_Removed))
{
AlreadyFile.Emplace(FilePath, true);
FString FileRef;
if (!DataTableGetObjectPath(FilePath, FileRef))
{
continue;
}
UDataTable* UserInfoDataTable = LoadObject<UDataTable>(nullptr, *FileRef);
if (UserInfoDataTable)
{
FString JsonString = UserInfoDataTable->GetTableAsJSON();
FString JsonSavePath = FPaths::ProjectContentDir() / TEXT("../TablesServer/" + FileBaseName + ".json");
FFileHelper::SaveStringToFile(JsonString, *JsonSavePath, FFileHelper::EEncodingOptions::ForceUTF8);
}
else
{
UE_LOG(LogTemp, Error, TEXT("DataTable To Json. Error %s"), *FileRef);
}
}
}
return;
}
ObjectPath路径的转换
bool EditorModule::DataTableGetObjectPath(FString& FilePath, FString& FileRef)
{
FString FrontPath = FPaths::GetPath(FilePath);
int32 Pos = FrontPath.Find(TEXT("/GameData"));
if (Pos == INDEX_NONE) {
return false;
}
FString MiddlePath = FrontPath.Right(FrontPath.Len() - Pos);
FString FileBaseName = FPaths::GetBaseFilename(FilePath);
FileRef = FString::Printf(TEXT("DataTable'/Game%s/%s.%s'"), *MiddlePath, *FileBaseName, *FileBaseName);
return true;
}
// 文件路径 D:/xxxx/项目/Content/GameData/DT_xxxx.uasset // 资源路径 “DataTable’/Game/GameData/DT_xxxx.DT_xxxx’” // 文件路径 D:/xxxx/项目/Content/GameData/tttttt/DT_xxxx.uasset // 资源路径 “DataTable’/Game/GameData/tttttt/DT_xxxx.DT_xxxx’”
|