feat: integrate ts2lua-unreal v0.2.1 and UnLua TypeScript (.d.ts) generator

This commit is contained in:
szy73711 committed 2026-11-29 03:08:03 +08:00
1 parent ebce41f347
commit ee2e62bb34
282 files changed
+141794 -261

No files matched your search

@@ -17,6 +17,7 @@
#include "Binding.h"
#include "Misc/FileHelper.h"
#include "UnLuaIntelliSenseGenerator.h"
#include "UnLuaTypeScriptGenerator.h"
UUnLuaIntelliSenseCommandlet::UUnLuaIntelliSenseCommandlet(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
@@ -104,12 +105,25 @@ int32 UUnLuaIntelliSenseCommandlet::Main(const FString &Params)
TMap<FString, FString> ParamsMap;
ParseCommandLine(*Params, Tokens, Switches, ParamsMap);
FString BPKey = TEXT("BP");
if (ParamsMap.Contains(BPKey) && ParamsMap[BPKey] == TEXT("1"))
FString TSKey = TEXT("TS");
const bool bGenBP = ParamsMap.Contains(BPKey) && ParamsMap[BPKey] == TEXT("1");
const bool bGenTS = ParamsMap.Contains(TSKey) && ParamsMap[TSKey] == TEXT("1");
if (bGenBP && bGenTS)
{
auto Generator = FUnLuaIntelliSenseGenerator::Get();
Generator->Initialize();
Generator->UpdateAllWithTS();
}
else if (bGenBP)
{
auto Generator = FUnLuaIntelliSenseGenerator::Get();
Generator->Initialize();
Generator->UpdateAll();
}
else if (bGenTS)
{
FUnLuaTypeScriptGenerator::Get()->GenerateAll();
}
return 0;
}
@@ -36,6 +36,11 @@ FMainMenuToolbar::FMainMenuToolbar()
FUnLuaIntelliSenseGenerator::Get()->UpdateAll();
}), FCanExecuteAction());
CommandList->MapAction(FUnLuaEditorCommands::Get().GenerateIntelliSenseAndTS, FExecuteAction::CreateLambda([]
{
FUnLuaIntelliSenseGenerator::Get()->UpdateAllWithTS();
}), FCanExecuteAction());
CommandList->MapAction(FUnLuaEditorCommands::Get().OpenRuntimeSettings, FExecuteAction::CreateLambda([]
{
if (ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings"))
@@ -123,6 +128,7 @@ TSharedRef<SWidget> FMainMenuToolbar::GenerateUnLuaSettingsMenu()
MenuBuilder.BeginSection(NAME_None, LOCTEXT("Section_Action", "Action"));
MenuBuilder.AddMenuEntry(Commands.HotReload, NAME_None, LOCTEXT("HotReload", "Hot Reload"));
MenuBuilder.AddMenuEntry(Commands.GenerateIntelliSense, NAME_None, LOCTEXT("GenerateIntelliSense", "Generate IntelliSense"));
MenuBuilder.AddMenuEntry(Commands.GenerateIntelliSenseAndTS, NAME_None, LOCTEXT("GenerateIntelliSenseAndTS", "Generate IntelliSense & TS"));
MenuBuilder.EndSection();
MenuBuilder.BeginSection(NAME_None, LOCTEXT("Section_Help", "Help"));
@@ -28,6 +28,7 @@ void FUnLuaEditorCommands::RegisterCommands()
UI_COMMAND(ReportIssue, "Report issue", "Create an issue on github", EUserInterfaceActionType::Button, FInputChord());
UI_COMMAND(About, "About", "About this version", EUserInterfaceActionType::Button, FInputChord());
UI_COMMAND(GenerateIntelliSense, "Generate IntelliSense", "Generate intelliSense files", EUserInterfaceActionType::Button, FInputChord());
UI_COMMAND(GenerateIntelliSenseAndTS, "Generate IntelliSense & TS", "Generate Lua IntelliSense and TypeScript (.d.ts) declaration files", EUserInterfaceActionType::Button, FInputChord());
UI_COMMAND(RevealInExplorer, "Reveal in Explorer", "Reveal lua file in explorer", EUserInterfaceActionType::Button, FInputChord());
}
@@ -36,5 +36,6 @@ public:
TSharedPtr<FUICommandInfo> ReportIssue;
TSharedPtr<FUICommandInfo> About;
TSharedPtr<FUICommandInfo> GenerateIntelliSense;
TSharedPtr<FUICommandInfo> GenerateIntelliSenseAndTS;
TSharedPtr<FUICommandInfo> RevealInExplorer;
};
@@ -50,6 +50,26 @@ public:
UPROPERTY(config, EditAnywhere, Category = "Coding")
bool bGenerateIntelliSense = true;
/** Output directory for generated TypeScript (.d.ts) declarations (relative to ProjectDir, or absolute path). */
UPROPERTY(config, EditAnywhere, Category = "Coding")
FString TypeScriptOutputDir = TEXT("Script/ts/types/ue");
/** Exclude Editor / UncookedOnly / Developer modules and Editor-only properties when generating TypeScript (.d.ts) declarations. */
UPROPERTY(config, EditAnywhere, Category = "Coding")
bool bTypeScriptExcludeEditorModules = true;
/** Only export project Blueprints under /Game/ (skip Engine/Plugin sample & editor-utility Blueprints). */
UPROPERTY(config, EditAnywhere, Category = "Coding")
bool bTypeScriptOnlyGameBlueprints = true;
/** Additional C++ module names (e.g. "PCG", "ControlRig") to exclude from TypeScript (.d.ts) generation. */
UPROPERTY(config, EditAnywhere, Category = "Coding")
TArray<FString> TypeScriptExcludedModules;
/** Optional whitelist of C++ module names to include (empty = include all non-excluded modules; CoreUObject/Engine and project Blueprints are always included). */
UPROPERTY(config, EditAnywhere, Category = "Coding")
TArray<FString> TypeScriptIncludedModules;
/** Whether or not startup UnLua module on game start. (Requires restart to take effect) */
UPROPERTY(config, EditAnywhere, Category = "Build")
bool bAutoStartup = true;
@@ -20,9 +20,11 @@
#include "AssetRegistryModule.h"
#endif
#include "CoreUObject.h"
#include "Binding.h"
#include "UnLua.h"
#include "UnLuaEditorSettings.h"
#include "UnLuaIntelliSense.h"
#include "UnLuaTypeScriptGenerator.h"
#include "WidgetBlueprint.h"
#include "Blueprint/WidgetTree.h"
#include "Engine/Blueprint.h"
@@ -57,6 +59,19 @@ void FUnLuaIntelliSenseGenerator::Initialize()
void FUnLuaIntelliSenseGenerator::UpdateAll()
{
UpdateAllInternal(false);
}
void FUnLuaIntelliSenseGenerator::UpdateAllWithTS()
{
UpdateAllInternal(true);
}
void FUnLuaIntelliSenseGenerator::UpdateAllInternal(bool bGenerateTS)
{
if (!bInitialized)
Initialize();
const FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>(TEXT("AssetRegistry"));
FARFilter Filter;
@@ -77,16 +92,43 @@ void FUnLuaIntelliSenseGenerator::UpdateAll()
if (TotalCount == 0)
return;
TotalCount++;
TotalCount += bGenerateTS ? 2 : 1;
FScopedSlowTask SlowTask(TotalCount, LOCTEXT("GeneratingBlueprintsIntelliSense", "Generating Blueprints InstelliSense"));
const FText TaskTitle = bGenerateTS
? LOCTEXT("GeneratingIntelliSenseAndTS", "Generating Lua IntelliSense & TypeScript Declarations")
: LOCTEXT("GeneratingBlueprintsIntelliSense", "Generating Blueprints InstelliSense");
FScopedSlowTask SlowTask(TotalCount, TaskTitle);
SlowTask.MakeDialog();
TArray<const UBlueprint*> LoadedBlueprints;
if (bGenerateTS)
LoadedBlueprints.Reserve(BlueprintAssets.Num());
for (int32 i = 0; i < BlueprintAssets.Num(); i++)
{
if (SlowTask.ShouldCancel())
break;
OnAssetUpdated(BlueprintAssets[i]);
if (ShouldExport(BlueprintAssets[i], true))
{
if (UBlueprint* Blueprint = LoadObject<UBlueprint>(nullptr, *BlueprintAssets[i].GetSoftObjectPath().ToString()))
{
Export(Blueprint);
}
}
if (bGenerateTS && IsBlueprint(BlueprintAssets[i]))
{
const UBlueprint* Blueprint = Cast<UBlueprint>(BlueprintAssets[i].FastGetAsset(false));
if (!Blueprint)
{
// Ensure Blueprint is loaded even when Settings.bGenerateIntelliSense is disabled
Blueprint = Cast<UBlueprint>(BlueprintAssets[i].GetAsset());
}
if (Blueprint && Blueprint->GeneratedClass)
{
LoadedBlueprints.Add(Blueprint);
}
}
SlowTask.EnterProgressFrame();
}
@@ -103,7 +145,71 @@ void FUnLuaIntelliSenseGenerator::UpdateAll()
return;
ExportUE(NativeTypes);
ExportUnLua();
ExportStaticallyExports();
SlowTask.EnterProgressFrame();
if (bGenerateTS && !SlowTask.ShouldCancel())
{
FUnLuaTypeScriptGenerator::Get()->GenerateAll(NativeTypes, LoadedBlueprints);
SlowTask.EnterProgressFrame();
}
}
void FUnLuaIntelliSenseGenerator::ExportStaticallyExports()
{
static const TSet<FString> ClassBlackList = {
TEXT("int8"), TEXT("int16"), TEXT("int32"), TEXT("int64"),
TEXT("uint8"), TEXT("uint16"), TEXT("uint32"), TEXT("uint64"),
TEXT("float"), TEXT("double"), TEXT("bool"),
TEXT("FName"), TEXT("FString"), TEXT("FText"),
};
const auto ExportedReflectedClasses = UnLua::GetExportedReflectedClasses();
const auto ExportedNonReflectedClasses = UnLua::GetExportedNonReflectedClasses();
const auto ExportedEnums = UnLua::GetExportedEnums();
const auto ExportedFunctions = UnLua::GetExportedFunctions();
const FString ModuleName(TEXT("StaticallyExports"));
FString GeneratedFileContent;
for (const auto& Pair : ExportedReflectedClasses)
{
if (ClassBlackList.Contains(Pair.Key) || !Pair.Value)
continue;
GeneratedFileContent.Reset();
Pair.Value->GenerateIntelliSense(GeneratedFileContent);
SaveFile(ModuleName, Pair.Key, GeneratedFileContent);
}
for (const auto& Pair : ExportedNonReflectedClasses)
{
if (ClassBlackList.Contains(Pair.Key) || !Pair.Value)
continue;
GeneratedFileContent.Reset();
Pair.Value->GenerateIntelliSense(GeneratedFileContent);
SaveFile(ModuleName, Pair.Key, GeneratedFileContent);
}
for (const auto* Enum : ExportedEnums)
{
if (!Enum)
continue;
GeneratedFileContent.Reset();
Enum->GenerateIntelliSense(GeneratedFileContent);
SaveFile(ModuleName, Enum->GetName(), GeneratedFileContent);
}
GeneratedFileContent.Reset();
for (const auto* Function : ExportedFunctions)
{
if (!Function || Function->GetName() == TEXT("OnModuleHotfixed"))
continue;
Function->GenerateIntelliSense(GeneratedFileContent);
}
if (!GeneratedFileContent.IsEmpty())
{
SaveFile(ModuleName, TEXT("GlobalFunctions"), GeneratedFileContent);
}
}
bool FUnLuaIntelliSenseGenerator::IsBlueprint(const FAssetData& AssetData)
@@ -112,8 +218,8 @@ bool FUnLuaIntelliSenseGenerator::IsBlueprint(const FAssetData& AssetData)
const FName AssetClass = AssetData.AssetClass;
return AssetClass == UBlueprint::StaticClass()->GetFName() || AssetClass == UWidgetBlueprint::StaticClass()->GetFName();
#else
const auto AssetClassPath = AssetData.AssetClassPath.ToString();
return AssetClassPath == UBlueprint::StaticClass()->GetName() || AssetClassPath == UWidgetBlueprint::StaticClass()->GetName();
return AssetData.AssetClassPath == UBlueprint::StaticClass()->GetClassPathName()
|| AssetData.AssetClassPath == UWidgetBlueprint::StaticClass()->GetClassPathName();
#endif
}
@@ -247,6 +353,14 @@ void FUnLuaIntelliSenseGenerator::DeleteFile(const FString& ModuleName, const FS
void FUnLuaIntelliSenseGenerator::OnAssetAdded(const FAssetData& AssetData)
{
if (IsBlueprint(AssetData))
{
if (const UBlueprint* Blueprint = Cast<UBlueprint>(AssetData.FastGetAsset(false)))
{
FUnLuaTypeScriptGenerator::Get()->UpdateBlueprint(Blueprint);
}
}
if (!ShouldExport(AssetData))
return;
@@ -259,6 +373,11 @@ void FUnLuaIntelliSenseGenerator::OnAssetAdded(const FAssetData& AssetData)
void FUnLuaIntelliSenseGenerator::OnAssetRemoved(const FAssetData& AssetData)
{
if (IsBlueprint(AssetData))
{
FUnLuaTypeScriptGenerator::Get()->OnBlueprintPackageChanged(AssetData.PackageName.ToString());
}
if (!ShouldExport(AssetData))
return;
@@ -267,6 +386,15 @@ void FUnLuaIntelliSenseGenerator::OnAssetRemoved(const FAssetData& AssetData)
void FUnLuaIntelliSenseGenerator::OnAssetRenamed(const FAssetData& AssetData, const FString& OldPath)
{
if (IsBlueprint(AssetData))
{
FUnLuaTypeScriptGenerator::Get()->OnBlueprintPackageChanged(OldPath);
if (const UBlueprint* Blueprint = Cast<UBlueprint>(AssetData.FastGetAsset(false)))
{
FUnLuaTypeScriptGenerator::Get()->UpdateBlueprint(Blueprint);
}
}
if (!ShouldExport(AssetData))
return;
@@ -280,6 +408,14 @@ void FUnLuaIntelliSenseGenerator::OnAssetRenamed(const FAssetData& AssetData, co
void FUnLuaIntelliSenseGenerator::OnAssetUpdated(const FAssetData& AssetData)
{
if (IsBlueprint(AssetData))
{
if (const UBlueprint* Blueprint = Cast<UBlueprint>(AssetData.FastGetAsset(false)))
{
FUnLuaTypeScriptGenerator::Get()->UpdateBlueprint(Blueprint);
}
}
if (!ShouldExport(AssetData, true))
return;
File diff suppressed because it is too large. Load diff
@@ -32,11 +32,18 @@ public:
// Update all Blueprints from command
void UpdateAll();
// Update all Lua IntelliSense and TypeScript (.d.ts) declarations
void UpdateAllWithTS();
void Initialize();
private:
static TSharedPtr<FUnLuaIntelliSenseGenerator> Singleton;
void UpdateAllInternal(bool bGenerateTS);
void ExportStaticallyExports();
static bool IsBlueprint(const FAssetData& AssetData);
static bool ShouldExport(const FAssetData& AssetData, bool bLoad = false);
@@ -0,0 +1,51 @@
// Tencent is pleased to support the open source community by making UnLua available.
//
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License. You may obtain a copy of the License at
//
// http://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and limitations under the License.
#pragma once
#include "CoreUObject.h"
class UBlueprint;
/**
* Generates TypeScript (.d.ts) declaration files for UE reflected types, Blueprints,
* and UnLua statically exported types (excluding runtime declarations owned by unlua.d.ts).
*/
class UNLUAEDITOR_API FUnLuaTypeScriptGenerator
{
public:
static TSharedRef<FUnLuaTypeScriptGenerator> Get();
/** Collect all reflected types & Blueprints and generate .d.ts files. */
void GenerateAll();
/** Generate .d.ts files using already-collected native types & loaded Blueprints. */
void GenerateAll(const TArray<const UField*>& NativeTypes, const TArray<const UBlueprint*>& Blueprints);
/** Incrementally regenerate the .d.ts file for the package directory containing a Blueprint (if output dir exists). */
void UpdateBlueprint(const UBlueprint* Blueprint);
/** Incrementally refresh/clean the .d.ts file for a Blueprint's package path after deletion or rename. */
void OnBlueprintPackageChanged(const FString& PackagePath);
/** Resolve target output directory for generated .d.ts files. */
FString GetOutputDir() const;
private:
static TSharedPtr<FUnLuaTypeScriptGenerator> Singleton;
bool SaveDtsFile(const FString& OutputDir, const FString& RelativeFilePath, const FString& Content, TSet<FString>& OutWrittenFiles);
void CleanStaleDtsFiles(const FString& OutputDir, const TSet<FString>& WrittenFiles);
void RegenerateBlueprintDirectory(const FString& PackageDir);
};