Files
UnrealLuaDemo/Source/UnrealLuaDemo/HotUpdate/LuaHotUpdateSubsystem.h
T

77 lines
3.1 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Containers/Ticker.h"
#include "Subsystems/GameInstanceSubsystem.h"
#include "LuaEnv.h"
#include "LuaHotUpdateSubsystem.generated.h"
class ULuaBundleAsset;
/**
* Manages Lua script loading across development (external Script/lua) and release
* (packaged binary ULuaBundleAsset UE resource), as well as runtime hot-reloading.
*
* Script search priority:
* 1. Hot-update override directory: <Saved>/HotUpdate/Script/ (or PersistentDownloadDir on mobile)
* 2. Development (WITH_EDITOR, unless -UseLuaBundle is passed): external <ProjectDir>/Script/lua/
* Release (!WITH_EDITOR, or -UseLuaBundle): packaged UE asset ULuaBundleAsset (/Game/Generated/DA_LuaBundle)
* 3. Falls through to UnLua's default filesystem/plugin loaders (e.g. Plugins/UnLua/Content/Script/)
*/
UCLASS()
class UNREALLUADEMO_API ULuaHotUpdateSubsystem : public UGameInstanceSubsystem
{
GENERATED_BODY()
public:
virtual void Initialize(FSubsystemCollectionBase& Collection) override;
virtual void Deinitialize() override;
/** Registers the global custom Lua loader with UnLua. */
static void RegisterHotUpdateLoader();
/** Unregisters the global custom Lua loader from UnLua and releases cached bundle asset. */
static void UnregisterHotUpdateLoader();
/** Returns true when Lua scripts should be loaded from the packaged binary ULuaBundleAsset instead of external Script/lua. */
UFUNCTION(BlueprintPure, Category = "Lua|HotUpdate")
static bool ShouldUseLuaBundle();
/** Returns the writable directory where hot-updated Lua scripts are placed. */
UFUNCTION(BlueprintPure, Category = "Lua|HotUpdate")
static FString GetHotUpdateScriptDir();
/** Returns the external project Lua script directory (<ProjectDir>/Script/lua). */
UFUNCTION(BlueprintPure, Category = "Lua|HotUpdate")
static FString GetDefaultScriptDir();
/** Clears and reloads the cached ULuaBundleAsset (useful in Editor after re-packing). */
UFUNCTION(BlueprintCallable, Category = "Lua|HotUpdate")
static void ReloadLuaBundleAsset();
/** Triggers UnLua hot-reload across all active Lua environments. */
UFUNCTION(BlueprintCallable, Category = "Lua|HotUpdate")
static void ReloadAllLuaModules();
/** Hot-reloads a single dot-separated Lua module (e.g. "Game.LuaGameMode"). */
UFUNCTION(BlueprintCallable, Category = "Lua|HotUpdate")
static bool ReloadLuaModule(const FString& ModuleName);
/** Executes an arbitrary Lua code chunk in all active Lua environments. */
UFUNCTION(BlueprintCallable, Category = "Lua|HotUpdate")
static bool ExecuteLuaString(const FString& LuaCode);
private:
static ULuaBundleAsset* GetOrLoadLuaBundleAsset();
static void HandleLuaStateCreated(lua_State* L);
static bool HandleCustomLoadLuaFile(UnLua::FLuaEnv& Env, const FString& RelativeFilePath, TArray<uint8>& Data, FString& FullFilePath);
static bool TryLoadLuaFromRoot(const FString& RootDir, const FString& NormalizedModulePath, TArray<uint8>& OutData, FString& OutFullPath);
#if WITH_EDITOR
bool PollHotReloadTriggerFile(float DeltaTime);
FTSTicker::FDelegateHandle TriggerPollHandle;
#endif
};