/**
 * Copyright 2022-2025 Theai, Inc. dba Inworld AI
 */

#define CUDA

using System;
using System.IO;
using UnrealBuildTool;

public class InworldRuntimeLibrary : ModuleRules
{
    private string ThirdPartyLibrariesDirectory
    {
        get
        {
            return Path.Combine(ModuleDirectory, Target.Platform.ToString());
        }
    }

    private string ThirdPartyIncludesDirectory
    {
        get
        {
            return Path.Combine(ModuleDirectory, "Public");
        }
    }

    private string PluginExtrasDirectory
    {
        get
        {
            return Path.Combine(PluginDirectory, "Extras/...");
        }
    }

    public InworldRuntimeLibrary(ReadOnlyTargetRules Target) : base(Target)
	{
		Type = ModuleType.External;

        PublicIncludePaths.Add(ThirdPartyIncludesDirectory);

        if (!TargetPlatformSupported)
        {
            return;
        }

        PublicDefinitions.Add("INWORLD_IMPLEMENTATION=1");
        RuntimeDependencies.Add(PluginExtrasDirectory, StagedFileType.NonUFS);

        if (Target.Platform == UnrealTargetPlatform.Win64)
        {
            PublicAdditionalLibraries.Add(Path.Combine(ThirdPartyLibrariesDirectory, "inworld.if.lib"));
            PublicDelayLoadDLLs.Add("inworld.dll");

            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "inworld.dll"));
            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "onnxruntime.dll"));
            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "webrtc_aec_plugin.dll"));

            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "espeak_ng_data/..."), StagedFileType.NonUFS);
            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "icu_data/..."), StagedFileType.NonUFS);

            PublicDefinitions.Add("INWORLD_WITH_CUDA=1");
            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "CUDA", "inworld.dll"));
            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "CUDA", "onnxruntime.dll"));
            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "CUDA", "webrtc_aec_plugin.dll"));

            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "espeak_ng_data/..."), StagedFileType.NonUFS);
            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "icu_data/..."), StagedFileType.NonUFS);
        }
        else if(Target.Platform == UnrealTargetPlatform.Linux)
        {
            PublicAdditionalLibraries.Add(Path.Combine(ThirdPartyLibrariesDirectory, "libinworld.so"));
            PublicDelayLoadDLLs.Add("libinworld.so");

            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "libinworld.so"));
            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "libonnxruntime.so.1"));
            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "libonnxruntime_providers_shared.so"));
            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "libgomp.so.1"));
            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "libwebrtc_aec_plugin.so"));

            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "espeak_ng_data/..."), StagedFileType.NonUFS);
            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "icu_data/..."), StagedFileType.NonUFS);
        }
        else if(Target.Platform == UnrealTargetPlatform.Mac)
        {
            PublicAdditionalLibraries.Add(Path.Combine(ThirdPartyLibrariesDirectory, "libinworld.dylib"));
            PublicDelayLoadDLLs.Add(Path.Combine(ThirdPartyLibrariesDirectory, "libinworld.dylib"));

            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "libinworld.dylib"));
            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "libonnxruntime.dylib"));
            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "libwebrtc_aec_plugin.dylib"));

            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "espeak_ng_data/..."), StagedFileType.NonUFS);
            RuntimeDependencies.Add(Path.Combine(ThirdPartyLibrariesDirectory, "icu_data/..."), StagedFileType.NonUFS);
        }
    }

    internal bool TargetPlatformSupported
    {
        get
        {
            return Target.Platform == UnrealTargetPlatform.Win64
                || Target.Platform == UnrealTargetPlatform.Linux
                || Target.Platform == UnrealTargetPlatform.Mac;
        }
    }
}
