feat: move Lua scripts to Script/lua and add binary UE bundle packing
This commit is contained in:
1 parent
256a60ac80
commit
ebce41f347
41 files changed
+1545
-57
No files matched your search
@@ -0,0 +1,301 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#include "LuaBundlePacker.h"
|
||||
#include "UnrealLuaDemoEditor.h"
|
||||
#include "LuaBundle/LuaBundleAsset.h"
|
||||
#include "LuaBundle/LuaScriptSettings.h"
|
||||
#include "AssetRegistry/AssetRegistryModule.h"
|
||||
#include "HAL/FileManager.h"
|
||||
#include "Interfaces/IPluginManager.h"
|
||||
#include "Misc/DateTime.h"
|
||||
#include "Misc/FileHelper.h"
|
||||
#include "Misc/PackageName.h"
|
||||
#include "Misc/Paths.h"
|
||||
#include "UObject/Package.h"
|
||||
#include "UObject/SavePackage.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
bool ShouldSkipRelativeLuaPath(const FString& RelativePath)
|
||||
{
|
||||
// Skip editor-only IntelliSense stubs and hidden directories (e.g. .git)
|
||||
if (RelativePath.StartsWith(TEXT("IntelliSense/"), ESearchCase::IgnoreCase) ||
|
||||
RelativePath.Contains(TEXT("/IntelliSense/"), ESearchCase::IgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
TArray<FString> Segments;
|
||||
RelativePath.ParseIntoArray(Segments, TEXT("/"), true);
|
||||
for (const FString& Segment : Segments)
|
||||
{
|
||||
if (Segment.StartsWith(TEXT(".")))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void FLuaBundlePacker::CollectDirectoryLuaFiles(
|
||||
const FString& RootDir,
|
||||
TMap<FString, FLuaBundleSourceItem>& InOutItemsByLowerPath,
|
||||
bool bAllowOverwrite)
|
||||
{
|
||||
if (!FPaths::DirectoryExists(RootDir))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
FString NormalizedRoot = FPaths::ConvertRelativePathToFull(RootDir);
|
||||
FPaths::NormalizeDirectoryName(NormalizedRoot);
|
||||
if (!NormalizedRoot.EndsWith(TEXT("/")))
|
||||
{
|
||||
NormalizedRoot += TEXT("/");
|
||||
}
|
||||
|
||||
TArray<FString> FoundFiles;
|
||||
IFileManager::Get().FindFilesRecursive(FoundFiles, *NormalizedRoot, TEXT("*.lua"), /*Files=*/true, /*Directories=*/false);
|
||||
FoundFiles.Sort();
|
||||
|
||||
for (const FString& FilePath : FoundFiles)
|
||||
{
|
||||
FString NormalizedFile = FPaths::ConvertRelativePathToFull(FilePath);
|
||||
FPaths::NormalizeFilename(NormalizedFile);
|
||||
|
||||
FString RelativePath = NormalizedFile;
|
||||
if (!FPaths::MakePathRelativeTo(RelativePath, *NormalizedRoot))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
RelativePath = FLuaBundleArchive::NormalizeRelativePath(RelativePath);
|
||||
if (RelativePath.IsEmpty() || ShouldSkipRelativeLuaPath(RelativePath))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
const FString LowerKey = RelativePath.ToLower();
|
||||
if (!bAllowOverwrite && InOutItemsByLowerPath.Contains(LowerKey))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
TArray<uint8> RawBytes;
|
||||
if (!FFileHelper::LoadFileToArray(RawBytes, *NormalizedFile))
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Warning, TEXT("[LuaBundlePacker] Failed to read Lua file: %s"), *NormalizedFile);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Strip UTF-8 BOM if present so runtime loading is consistent
|
||||
if (RawBytes.Num() >= 3 && RawBytes[0] == 0xEF && RawBytes[1] == 0xBB && RawBytes[2] == 0xBF)
|
||||
{
|
||||
RawBytes.RemoveAt(0, 3);
|
||||
}
|
||||
|
||||
FLuaBundleSourceItem Item;
|
||||
Item.RelativePath = RelativePath;
|
||||
Item.PlaintextBytes = MoveTemp(RawBytes);
|
||||
InOutItemsByLowerPath.Add(LowerKey, MoveTemp(Item));
|
||||
}
|
||||
}
|
||||
|
||||
bool FLuaBundlePacker::CollectLuaSources(TArray<FLuaBundleSourceItem>& OutItems, FString& OutError)
|
||||
{
|
||||
OutItems.Reset();
|
||||
|
||||
const ULuaScriptSettings* Settings = GetDefault<ULuaScriptSettings>();
|
||||
if (!Settings)
|
||||
{
|
||||
OutError = TEXT("ULuaScriptSettings CDO is unavailable.");
|
||||
return false;
|
||||
}
|
||||
|
||||
const FString ProjectLuaRoot = Settings->GetExternalScriptRootFullPath();
|
||||
if (!FPaths::DirectoryExists(ProjectLuaRoot))
|
||||
{
|
||||
OutError = FString::Printf(TEXT("External Lua script root directory does not exist: %s"), *ProjectLuaRoot);
|
||||
return false;
|
||||
}
|
||||
|
||||
TMap<FString, FLuaBundleSourceItem> ItemsByLowerPath;
|
||||
|
||||
// 1. Collect project scripts from <ProjectDir>/Script/lua (highest priority)
|
||||
CollectDirectoryLuaFiles(ProjectLuaRoot, ItemsByLowerPath, /*bAllowOverwrite=*/true);
|
||||
|
||||
// 2. Optionally include UnLua plugin runtime scripts (e.g. UnLua/HotReload.lua, UnLua/EnhancedInput.lua)
|
||||
// without overwriting any project-provided script of the same path.
|
||||
if (Settings->bIncludeUnLuaPluginScriptsInBundle)
|
||||
{
|
||||
if (TSharedPtr<IPlugin> UnLuaPlugin = IPluginManager::Get().FindPlugin(TEXT("UnLua")))
|
||||
{
|
||||
const FString PluginScriptRoot = FPaths::Combine(UnLuaPlugin->GetContentDir(), TEXT("Script"));
|
||||
CollectDirectoryLuaFiles(PluginScriptRoot, ItemsByLowerPath, /*bAllowOverwrite=*/false);
|
||||
}
|
||||
}
|
||||
|
||||
if (ItemsByLowerPath.Num() == 0)
|
||||
{
|
||||
OutError = FString::Printf(TEXT("No .lua files found under: %s"), *ProjectLuaRoot);
|
||||
return false;
|
||||
}
|
||||
|
||||
OutItems.Reserve(ItemsByLowerPath.Num());
|
||||
for (TPair<FString, FLuaBundleSourceItem>& Pair : ItemsByLowerPath)
|
||||
{
|
||||
OutItems.Add(MoveTemp(Pair.Value));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FLuaBundlePacker::PackToDefaultAsset(FLuaBundlePackResult& OutResult, TOptional<bool> bEncryptOverride)
|
||||
{
|
||||
const ULuaScriptSettings* Settings = GetDefault<ULuaScriptSettings>();
|
||||
if (!Settings)
|
||||
{
|
||||
OutResult.bSuccess = false;
|
||||
OutResult.ErrorMessage = TEXT("ULuaScriptSettings CDO is unavailable.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return PackToAsset(Settings->LuaBundleAssetPath, OutResult, bEncryptOverride);
|
||||
}
|
||||
|
||||
bool FLuaBundlePacker::PackToAsset(
|
||||
const FSoftObjectPath& TargetAssetPath,
|
||||
FLuaBundlePackResult& OutResult,
|
||||
TOptional<bool> bEncryptOverride)
|
||||
{
|
||||
OutResult = FLuaBundlePackResult{};
|
||||
|
||||
const ULuaScriptSettings* Settings = GetDefault<ULuaScriptSettings>();
|
||||
if (!Settings)
|
||||
{
|
||||
OutResult.ErrorMessage = TEXT("ULuaScriptSettings CDO is unavailable.");
|
||||
return false;
|
||||
}
|
||||
|
||||
const FString PackageName = TargetAssetPath.GetLongPackageName();
|
||||
const FString AssetName = TargetAssetPath.GetAssetName();
|
||||
if (PackageName.IsEmpty() || AssetName.IsEmpty())
|
||||
{
|
||||
OutResult.ErrorMessage = FString::Printf(TEXT("Invalid LuaBundleAssetPath: '%s'"), *TargetAssetPath.ToString());
|
||||
return false;
|
||||
}
|
||||
|
||||
TArray<FLuaBundleSourceItem> SourceItems;
|
||||
if (!CollectLuaSources(SourceItems, OutResult.ErrorMessage))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool bEncrypt = bEncryptOverride.Get(Settings->bEncryptBundle);
|
||||
TArray<uint8> ArchiveBytes;
|
||||
if (!FLuaBundleArchive::Build(SourceItems, ArchiveBytes, bEncrypt, Settings->BundleEncryptionKey))
|
||||
{
|
||||
OutResult.ErrorMessage = TEXT("FLuaBundleArchive::Build failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
UPackage* Package = CreatePackage(*PackageName);
|
||||
if (!Package)
|
||||
{
|
||||
OutResult.ErrorMessage = FString::Printf(TEXT("Failed to create package '%s'."), *PackageName);
|
||||
return false;
|
||||
}
|
||||
Package->FullyLoad();
|
||||
|
||||
ULuaBundleAsset* BundleAsset = FindObject<ULuaBundleAsset>(Package, *AssetName);
|
||||
const bool bNewlyCreated = (BundleAsset == nullptr);
|
||||
if (!BundleAsset)
|
||||
{
|
||||
BundleAsset = NewObject<ULuaBundleAsset>(Package, *AssetName, RF_Public | RF_Standalone);
|
||||
}
|
||||
|
||||
BundleAsset->FormatVersion = static_cast<int32>(FLuaBundleArchive::CurrentVersion);
|
||||
BundleAsset->ScriptCount = SourceItems.Num();
|
||||
BundleAsset->bEncrypted = bEncrypt;
|
||||
BundleAsset->BuildTimestamp = FDateTime::UtcNow().ToIso8601();
|
||||
BundleAsset->ArchiveBytes = MoveTemp(ArchiveBytes);
|
||||
BundleAsset->InvalidateCache();
|
||||
BundleAsset->MarkPackageDirty();
|
||||
|
||||
if (bNewlyCreated)
|
||||
{
|
||||
FAssetRegistryModule::AssetCreated(BundleAsset);
|
||||
}
|
||||
|
||||
const FString PackageFilename = FPackageName::LongPackageNameToFilename(
|
||||
PackageName, FPackageName::GetAssetPackageExtension());
|
||||
|
||||
const FString ParentDir = FPaths::GetPath(PackageFilename);
|
||||
IFileManager::Get().MakeDirectory(*ParentDir, /*Tree=*/true);
|
||||
|
||||
FSavePackageArgs SaveArgs;
|
||||
SaveArgs.TopLevelFlags = RF_Public | RF_Standalone;
|
||||
SaveArgs.Error = GError;
|
||||
SaveArgs.bWarnOfLongFilename = false;
|
||||
|
||||
const bool bSaved = UPackage::SavePackage(Package, BundleAsset, *PackageFilename, SaveArgs);
|
||||
if (!bSaved)
|
||||
{
|
||||
OutResult.ErrorMessage = FString::Printf(TEXT("UPackage::SavePackage failed for '%s'."), *PackageFilename);
|
||||
return false;
|
||||
}
|
||||
|
||||
OutResult.bSuccess = true;
|
||||
OutResult.ScriptCount = BundleAsset->ScriptCount;
|
||||
OutResult.ArchiveByteSize = BundleAsset->ArchiveBytes.Num();
|
||||
OutResult.bEncrypted = BundleAsset->bEncrypted;
|
||||
OutResult.OutputPath = PackageFilename;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FLuaBundlePacker::PackToStandaloneFile(
|
||||
const FString& OutputFilePath,
|
||||
FLuaBundlePackResult& OutResult,
|
||||
TOptional<bool> bEncryptOverride)
|
||||
{
|
||||
OutResult = FLuaBundlePackResult{};
|
||||
|
||||
const ULuaScriptSettings* Settings = GetDefault<ULuaScriptSettings>();
|
||||
if (!Settings)
|
||||
{
|
||||
OutResult.ErrorMessage = TEXT("ULuaScriptSettings CDO is unavailable.");
|
||||
return false;
|
||||
}
|
||||
|
||||
TArray<FLuaBundleSourceItem> SourceItems;
|
||||
if (!CollectLuaSources(SourceItems, OutResult.ErrorMessage))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool bEncrypt = bEncryptOverride.Get(Settings->bEncryptBundle);
|
||||
TArray<uint8> ArchiveBytes;
|
||||
if (!FLuaBundleArchive::Build(SourceItems, ArchiveBytes, bEncrypt, Settings->BundleEncryptionKey))
|
||||
{
|
||||
OutResult.ErrorMessage = TEXT("FLuaBundleArchive::Build failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
const FString FullOutputPath = FPaths::ConvertRelativePathToFull(OutputFilePath);
|
||||
IFileManager::Get().MakeDirectory(*FPaths::GetPath(FullOutputPath), /*Tree=*/true);
|
||||
|
||||
if (!FFileHelper::SaveArrayToFile(ArchiveBytes, *FullOutputPath))
|
||||
{
|
||||
OutResult.ErrorMessage = FString::Printf(TEXT("Failed to save standalone Lua bundle to '%s'."), *FullOutputPath);
|
||||
return false;
|
||||
}
|
||||
|
||||
OutResult.bSuccess = true;
|
||||
OutResult.ScriptCount = SourceItems.Num();
|
||||
OutResult.ArchiveByteSize = ArchiveBytes.Num();
|
||||
OutResult.bEncrypted = bEncrypt;
|
||||
OutResult.OutputPath = FullOutputPath;
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "LuaBundle/LuaBundleArchive.h"
|
||||
|
||||
struct UNREALLUADEMOEDITOR_API FLuaBundlePackResult
|
||||
{
|
||||
bool bSuccess = false;
|
||||
int32 ScriptCount = 0;
|
||||
int32 ArchiveByteSize = 0;
|
||||
bool bEncrypted = false;
|
||||
FString OutputPath;
|
||||
FString ErrorMessage;
|
||||
};
|
||||
|
||||
/**
|
||||
* Editor-only packer that collects external Lua scripts (Script/lua and optionally
|
||||
* Plugins/UnLua/Content/Script) into a single FLuaBundleArchive and writes it to either
|
||||
* a UE DataAsset (ULuaBundleAsset) for cooked builds or a standalone binary file.
|
||||
*/
|
||||
class UNREALLUADEMOEDITOR_API FLuaBundlePacker
|
||||
{
|
||||
public:
|
||||
/** Collects all .lua source files according to ULuaScriptSettings. */
|
||||
static bool CollectLuaSources(TArray<FLuaBundleSourceItem>& OutItems, FString& OutError);
|
||||
|
||||
/** Packs external Lua scripts into the default ULuaBundleAsset configured in ULuaScriptSettings. */
|
||||
static bool PackToDefaultAsset(FLuaBundlePackResult& OutResult, TOptional<bool> bEncryptOverride = {});
|
||||
|
||||
/** Packs external Lua scripts into a specific UE asset soft object path (e.g. /Game/Generated/DA_LuaBundle.DA_LuaBundle). */
|
||||
static bool PackToAsset(const FSoftObjectPath& TargetAssetPath, FLuaBundlePackResult& OutResult, TOptional<bool> bEncryptOverride = {});
|
||||
|
||||
/** Packs external Lua scripts into a standalone binary archive file (useful for CI/hot-update pipelines). */
|
||||
static bool PackToStandaloneFile(const FString& OutputFilePath, FLuaBundlePackResult& OutResult, TOptional<bool> bEncryptOverride = {});
|
||||
|
||||
private:
|
||||
static void CollectDirectoryLuaFiles(
|
||||
const FString& RootDir,
|
||||
TMap<FString, FLuaBundleSourceItem>& InOutItemsByLowerPath,
|
||||
bool bAllowOverwrite);
|
||||
};
|
||||
@@ -0,0 +1,106 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#include "PackLuaBundleCommandlet.h"
|
||||
#include "LuaBundlePacker.h"
|
||||
#include "UnrealLuaDemoEditor.h"
|
||||
#include "LuaBundle/LuaBundleAsset.h"
|
||||
#include "LuaBundle/LuaScriptSettings.h"
|
||||
#include "Misc/Parse.h"
|
||||
|
||||
UPackLuaBundleCommandlet::UPackLuaBundleCommandlet()
|
||||
{
|
||||
IsClient = false;
|
||||
IsEditor = true;
|
||||
IsServer = false;
|
||||
LogToConsole = true;
|
||||
ShowErrorCount = true;
|
||||
}
|
||||
|
||||
int32 UPackLuaBundleCommandlet::Main(const FString& Params)
|
||||
{
|
||||
TOptional<bool> bEncryptOverride;
|
||||
if (FParse::Param(*Params, TEXT("NoEncrypt")))
|
||||
{
|
||||
bEncryptOverride = false;
|
||||
}
|
||||
else if (FParse::Param(*Params, TEXT("Encrypt")))
|
||||
{
|
||||
bEncryptOverride = true;
|
||||
}
|
||||
|
||||
FLuaBundlePackResult AssetResult;
|
||||
if (!FLuaBundlePacker::PackToDefaultAsset(AssetResult, bEncryptOverride))
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[PackLuaBundleCommandlet] PackToDefaultAsset failed: %s"), *AssetResult.ErrorMessage);
|
||||
return 1;
|
||||
}
|
||||
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Display,
|
||||
TEXT("[PackLuaBundleCommandlet] Packed %d scripts (%d bytes, encrypted=%s) -> %s"),
|
||||
AssetResult.ScriptCount,
|
||||
AssetResult.ArchiveByteSize,
|
||||
AssetResult.bEncrypted ? TEXT("true") : TEXT("false"),
|
||||
*AssetResult.OutputPath);
|
||||
|
||||
FString StandaloneOutputFile;
|
||||
if (FParse::Value(*Params, TEXT("OutputFile="), StandaloneOutputFile) && !StandaloneOutputFile.IsEmpty())
|
||||
{
|
||||
FLuaBundlePackResult FileResult;
|
||||
if (!FLuaBundlePacker::PackToStandaloneFile(StandaloneOutputFile, FileResult, bEncryptOverride))
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[PackLuaBundleCommandlet] PackToStandaloneFile failed: %s"), *FileResult.ErrorMessage);
|
||||
return 1;
|
||||
}
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Display,
|
||||
TEXT("[PackLuaBundleCommandlet] Exported standalone bundle (%d bytes) -> %s"),
|
||||
FileResult.ArchiveByteSize, *FileResult.OutputPath);
|
||||
}
|
||||
|
||||
if (FParse::Param(*Params, TEXT("Verify")))
|
||||
{
|
||||
const ULuaScriptSettings* Settings = GetDefault<ULuaScriptSettings>();
|
||||
ULuaBundleAsset* LoadedAsset = Settings ? Cast<ULuaBundleAsset>(Settings->LuaBundleAssetPath.TryLoad()) : nullptr;
|
||||
if (!LoadedAsset)
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[PackLuaBundleCommandlet] Verify failed: could not load bundle asset."));
|
||||
return 1;
|
||||
}
|
||||
|
||||
TArray<FLuaBundleSourceItem> Sources;
|
||||
FString CollectError;
|
||||
if (!FLuaBundlePacker::CollectLuaSources(Sources, CollectError))
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[PackLuaBundleCommandlet] Verify failed: %s"), *CollectError);
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (const FLuaBundleSourceItem& Source : Sources)
|
||||
{
|
||||
FString ModulePath = Source.RelativePath;
|
||||
if (ModulePath.EndsWith(TEXT(".lua"), ESearchCase::IgnoreCase))
|
||||
{
|
||||
ModulePath.LeftChopInline(4);
|
||||
}
|
||||
|
||||
TArray<uint8> ReadBackBytes;
|
||||
FString ChunkName;
|
||||
if (!LoadedAsset->FindScript(ModulePath, ReadBackBytes, ChunkName))
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[PackLuaBundleCommandlet] Verify failed: missing module '%s'."), *ModulePath);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ReadBackBytes != Source.PlaintextBytes)
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[PackLuaBundleCommandlet] Verify failed: content mismatch for '%s'."), *ModulePath);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Display,
|
||||
TEXT("[PackLuaBundleCommandlet] Verified all %d scripts round-trip cleanly from ULuaBundleAsset."),
|
||||
Sources.Num());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "Commandlets/Commandlet.h"
|
||||
#include "PackLuaBundleCommandlet.generated.h"
|
||||
|
||||
/**
|
||||
* Commandlet for packing external Script/lua scripts into the binary ULuaBundleAsset UE resource
|
||||
* (and optionally exporting a standalone binary archive file).
|
||||
*
|
||||
* Usage:
|
||||
* UnrealEditor-Cmd <Project.uproject> -run=PackLuaBundle [-NoEncrypt] [-OutputFile=<path>] [-Verify]
|
||||
*/
|
||||
UCLASS()
|
||||
class UNREALLUADEMOEDITOR_API UPackLuaBundleCommandlet : public UCommandlet
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
UPackLuaBundleCommandlet();
|
||||
|
||||
virtual int32 Main(const FString& Params) override;
|
||||
};
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
using UnrealBuildTool;
|
||||
|
||||
public class UnrealLuaDemoEditor : ModuleRules
|
||||
{
|
||||
public UnrealLuaDemoEditor(ReadOnlyTargetRules Target) : base(Target)
|
||||
{
|
||||
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
|
||||
|
||||
PublicIncludePaths.Add(ModuleDirectory);
|
||||
|
||||
PublicDependencyModuleNames.AddRange(new string[]
|
||||
{
|
||||
"Core",
|
||||
"CoreUObject",
|
||||
"Engine",
|
||||
"UnrealLuaDemo"
|
||||
});
|
||||
|
||||
PrivateDependencyModuleNames.AddRange(new string[]
|
||||
{
|
||||
"UnrealEd",
|
||||
"AssetRegistry",
|
||||
"DeveloperSettings",
|
||||
"Projects",
|
||||
"Slate",
|
||||
"SlateCore",
|
||||
"ToolMenus"
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#include "UnrealLuaDemoEditor.h"
|
||||
#include "LuaBundlePacker.h"
|
||||
#include "LuaBundle/LuaScriptSettings.h"
|
||||
#include "GameDelegates.h"
|
||||
#include "HAL/IConsoleManager.h"
|
||||
#include "ToolMenus.h"
|
||||
|
||||
DEFINE_LOG_CATEGORY(LogUnrealLuaDemoEditor);
|
||||
|
||||
#define LOCTEXT_NAMESPACE "FUnrealLuaDemoEditorModule"
|
||||
|
||||
void FUnrealLuaDemoEditorModule::StartupModule()
|
||||
{
|
||||
ModifyCookHandle = FGameDelegates::Get().GetModifyCookDelegate().AddRaw(
|
||||
this, &FUnrealLuaDemoEditorModule::OnModifyCook);
|
||||
|
||||
PackBundleCmd = IConsoleManager::Get().RegisterConsoleCommand(
|
||||
TEXT("Lua.PackBundle"),
|
||||
TEXT("Pack external Script/lua directory into the configured ULuaBundleAsset UE resource."),
|
||||
FConsoleCommandDelegate::CreateLambda([]()
|
||||
{
|
||||
FLuaBundlePackResult Result;
|
||||
if (FLuaBundlePacker::PackToDefaultAsset(Result))
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Log,
|
||||
TEXT("[Lua.PackBundle] Packed %d scripts (%d bytes, encrypted=%s) -> %s"),
|
||||
Result.ScriptCount,
|
||||
Result.ArchiveByteSize,
|
||||
Result.bEncrypted ? TEXT("true") : TEXT("false"),
|
||||
*Result.OutputPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[Lua.PackBundle] Failed: %s"), *Result.ErrorMessage);
|
||||
}
|
||||
}),
|
||||
ECVF_Default);
|
||||
|
||||
if (!IsRunningCommandlet())
|
||||
{
|
||||
UToolMenus::RegisterStartupCallback(
|
||||
FSimpleMulticastDelegate::FDelegate::CreateRaw(this, &FUnrealLuaDemoEditorModule::RegisterMenus));
|
||||
}
|
||||
}
|
||||
|
||||
void FUnrealLuaDemoEditorModule::ShutdownModule()
|
||||
{
|
||||
if (ModifyCookHandle.IsValid())
|
||||
{
|
||||
FGameDelegates::Get().GetModifyCookDelegate().Remove(ModifyCookHandle);
|
||||
ModifyCookHandle.Reset();
|
||||
}
|
||||
|
||||
if (PackBundleCmd)
|
||||
{
|
||||
IConsoleManager::Get().UnregisterConsoleObject(PackBundleCmd);
|
||||
PackBundleCmd = nullptr;
|
||||
}
|
||||
|
||||
UToolMenus::UnRegisterStartupCallback(this);
|
||||
UToolMenus::UnregisterOwner(this);
|
||||
}
|
||||
|
||||
void FUnrealLuaDemoEditorModule::OnModifyCook(
|
||||
TConstArrayView<const ITargetPlatform*> InTargetPlatforms,
|
||||
TArray<FName>& InOutPackagesToCook,
|
||||
TArray<FName>& InOutPackagesToNeverCook)
|
||||
{
|
||||
const ULuaScriptSettings* Settings = GetDefault<ULuaScriptSettings>();
|
||||
if (!Settings || !Settings->bAutoPackOnCook)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Log, TEXT("[LuaBundle] Auto-packing external Lua scripts before Cook..."));
|
||||
|
||||
FLuaBundlePackResult Result;
|
||||
if (!FLuaBundlePacker::PackToDefaultAsset(Result))
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[LuaBundle] Pre-Cook Lua bundle packing FAILED: %s"), *Result.ErrorMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
const FString LongPackageName = Settings->LuaBundleAssetPath.GetLongPackageName();
|
||||
if (!LongPackageName.IsEmpty())
|
||||
{
|
||||
const FName PackageFName(*LongPackageName);
|
||||
InOutPackagesToNeverCook.Remove(PackageFName);
|
||||
InOutPackagesToCook.AddUnique(PackageFName);
|
||||
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Log,
|
||||
TEXT("[LuaBundle] Packed %d scripts (%d bytes) and queued '%s' for cooking."),
|
||||
Result.ScriptCount, Result.ArchiveByteSize, *LongPackageName);
|
||||
}
|
||||
}
|
||||
|
||||
void FUnrealLuaDemoEditorModule::RegisterMenus()
|
||||
{
|
||||
FToolMenuOwnerScoped OwnerScoped(this);
|
||||
|
||||
UToolMenu* ToolsMenu = UToolMenus::Get()->ExtendMenu(TEXT("LevelEditor.MainMenu.Tools"));
|
||||
if (!ToolsMenu)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
FToolMenuSection& Section = ToolsMenu->FindOrAddSection(TEXT("LuaScripts"));
|
||||
Section.Label = LOCTEXT("LuaScriptsSection", "Lua Scripts");
|
||||
|
||||
Section.AddMenuEntry(
|
||||
TEXT("PackLuaScriptBundle"),
|
||||
LOCTEXT("PackLuaBundleLabel", "Pack Lua Script Bundle"),
|
||||
LOCTEXT("PackLuaBundleTooltip", "Pack external Script/lua scripts into the binary ULuaBundleAsset UE resource."),
|
||||
FSlateIcon(),
|
||||
FUIAction(FExecuteAction::CreateLambda([]()
|
||||
{
|
||||
FLuaBundlePackResult Result;
|
||||
if (FLuaBundlePacker::PackToDefaultAsset(Result))
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Log,
|
||||
TEXT("[LuaBundle] Packed %d scripts (%d bytes) -> %s"),
|
||||
Result.ScriptCount, Result.ArchiveByteSize, *Result.OutputPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[LuaBundle] Pack failed: %s"), *Result.ErrorMessage);
|
||||
}
|
||||
})));
|
||||
}
|
||||
|
||||
#undef LOCTEXT_NAMESPACE
|
||||
|
||||
IMPLEMENT_MODULE(FUnrealLuaDemoEditorModule, UnrealLuaDemoEditor);
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "Modules/ModuleManager.h"
|
||||
|
||||
class ITargetPlatform;
|
||||
|
||||
DECLARE_LOG_CATEGORY_EXTERN(LogUnrealLuaDemoEditor, Log, All);
|
||||
|
||||
class FUnrealLuaDemoEditorModule : public IModuleInterface
|
||||
{
|
||||
public:
|
||||
virtual void StartupModule() override;
|
||||
virtual void ShutdownModule() override;
|
||||
|
||||
private:
|
||||
void OnModifyCook(
|
||||
TConstArrayView<const ITargetPlatform*> InTargetPlatforms,
|
||||
TArray<FName>& InOutPackagesToCook,
|
||||
TArray<FName>& InOutPackagesToNeverCook);
|
||||
|
||||
void RegisterMenus();
|
||||
|
||||
FDelegateHandle ModifyCookHandle;
|
||||
IConsoleObject* PackBundleCmd = nullptr;
|
||||
};
|
||||
Reference in new issue
Block a user