Skip to content

Commit

Permalink
Working solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Leinnan committed May 16, 2024
1 parent 4fc0fae commit 59f7489
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 150 deletions.
149 changes: 0 additions & 149 deletions Plugins/BeamableCore/Source/BeamableCore/BeamableCore.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,153 +54,4 @@ public BeamableCore(ReadOnlyTargetRules Target) : base(Target)
}
);
}
}

public static class BeamableModuleUtils
{
public struct OssConfig
{
public bool IsEnabled;
public bool HooksEnabled;
public string HookSubsystemImplementation;
public string HookSubsystemIncludePath;
public string[] AdditionalHookModules;
}


public class BeamableAdditionalData
{
public string[] OssAdditionalModules = Array.Empty<string>();
}


public static void ConfigureEditor(TargetRules TargetRules, OssConfig OssConfig)
{
var additionalData = GetOrAddAdditionalData<BeamableAdditionalData>(TargetRules);
AddUtilityMacros(TargetRules);
ConfigOnlineSubsystem(TargetRules, OssConfig, additionalData);

TargetRules.ExtraModuleNames.AddRange(new[]
{
"BeamableCore",
"BeamableCoreBlueprintNodes",
"BeamableCoreRuntime",
"BeamableCoreEditor",

"Json",
"JsonUtilities",
});
}

public static void ConfigureServer(TargetRules TargetRules, OssConfig OssConfig)
{
var additionalData = GetOrAddAdditionalData<BeamableAdditionalData>(TargetRules);
AddUtilityMacros(TargetRules);
ConfigOnlineSubsystem(TargetRules, OssConfig, additionalData);

TargetRules.ExtraModuleNames.AddRange(new[]
{
"BeamableCore",
"BeamableCoreRuntime",

"Json",
"JsonUtilities",
});
}

public static void ConfigureGame(TargetRules TargetRules, OssConfig OssConfig)
{
var additionalData = GetOrAddAdditionalData<BeamableAdditionalData>(TargetRules);
AddUtilityMacros(TargetRules);
ConfigOnlineSubsystem(TargetRules, OssConfig, additionalData);

TargetRules.ExtraModuleNames.AddRange(new[]
{
"BeamableCore",
"BeamableCoreRuntime",

"Json",
"JsonUtilities",
});
}


/// <summary>
/// To be used in Target.cs files to add objects to <see cref="TargetRules.AdditionalData"/> in order to be used by various Build.cs files.
/// </summary>
public static T GetOrAddAdditionalData<T>(TargetRules TargetRules) where T : class, new()
{
TargetRules.AdditionalData ??= new Dictionary<Type, object>();

var dict = TargetRules.AdditionalData as Dictionary<Type, object>;
dict!.TryAdd(typeof(T), new T());
return dict[typeof(T)] as T;
}

/// <summary>
/// To be used in Build.cs files to read objects added to the <see cref="TargetRules.AdditionalData"/> by <see cref="GetOrAddAdditionalData{T}"/>.
/// </summary>
public static T GetAdditionalData<T>(this ReadOnlyTargetRules TargetRules) where T : class, new()
{
if (TargetRules.AdditionalData is Dictionary<Type, object> dictionary)
{
if (dictionary.TryGetValue(typeof(T), out var data))
return data as T;

throw new Exception(
$"Failed to find an additional data with the given type {typeof(T).Name}.\n" +
"Make sure you call GetOrAddAdditionalData<T> from your Target.cs file passing in the correct Type.\n");
}

throw new Exception(
"Using beamable requires the AdditionalData to be a Dictionary of Type/Object.\n" +
"We provide a utility function that allows you to add your own additional data next to beamable's own BeamableAdditionalData.\n" +
"We do this because the Beamable plugin needs some additional data but we didn't want to lock you out of passing in your own things.");
}


/// <summary>
/// Configures the Beamable SDK to work with UE's Online Subsystem stuff.
/// </summary>
private static void ConfigOnlineSubsystem(TargetRules TargetRules, OssConfig OssConfig, BeamableAdditionalData AdditionalData)
{
// Don't do anything to configure OSS if its not enabled
if (!OssConfig.IsEnabled) return;

// Include the OnlineSubsystem modules in the target.
TargetRules.ExtraModuleNames.AddRange(new[]
{
"OnlineSubsystem",
"OnlineSubsystemUtils",
"OnlineSubsystemBeamable"
});

// If we are building the editor, we also add the OnlineSubsystemBeamableBp module (which is where your BeamFlow nodes for Microservices live when OSS is enabled)
if (TargetRules.bBuildEditor)
{
TargetRules.ExtraModuleNames.AddRange(new[]
{
"OnlineSubsystemBeamableBp" // TODO: Make sure this module exists before doing this.
});
}

// Add the ENABLE OSS define so that the BeamableCore plugins can declare some utilities for when it is working with OSS
TargetRules.ProjectDefinitions.Add("BEAM_ENABLE_BEAMABLE_OSS=1");

// If OSS Hooks are enabled, the customer can pass in the Hook entry point implementation and we'll auto-configure it for them.
if (OssConfig.HooksEnabled)
{
TargetRules.ProjectDefinitions.Add("BEAM_ENABLE_OSS_HOOKS=1");
TargetRules.ProjectDefinitions.Add($"BEAM_OSS_SUBSYSTEM_IMPLEMENTATION={OssConfig.HookSubsystemImplementation}");
TargetRules.ProjectDefinitions.Add($"BEAM_OSS_SUBSYSTEM_INCLUDE=BEAM_STRINGIFY({OssConfig.HookSubsystemIncludePath})");

// Pass down, to the OnlineSubsystemBeamable plugin, a list of Module names that it'll also depend on
AdditionalData.OssAdditionalModules = OssConfig.AdditionalHookModules;
}
}

private static void AddUtilityMacros(TargetRules TargetRules)
{
TargetRules.ProjectDefinitions.Add("BEAM_STRINGIFY(X)=#X");
}
}
153 changes: 152 additions & 1 deletion Source/BeamableUnreal.Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,155 @@ public static void ConfigureIfHathoraDemo(TargetRules TargetRules, string beamPr
}
}
}
}
}

/// BEAMABLE CODE TO COPY PASTE START
public static class BeamableModuleUtils
{
public struct OssConfig
{
public bool IsEnabled;
public bool HooksEnabled;
public string HookSubsystemImplementation;
public string HookSubsystemIncludePath;
public string[] AdditionalHookModules;
}


public class BeamableAdditionalData
{
public string[] OssAdditionalModules = Array.Empty<string>();
}


public static void ConfigureEditor(TargetRules TargetRules, OssConfig OssConfig)
{
var additionalData = GetOrAddAdditionalData<BeamableAdditionalData>(TargetRules);
AddUtilityMacros(TargetRules);
ConfigOnlineSubsystem(TargetRules, OssConfig, additionalData);

TargetRules.ExtraModuleNames.AddRange(new[]
{
"BeamableCore",
"BeamableCoreBlueprintNodes",
"BeamableCoreRuntime",
"BeamableCoreEditor",

"Json",
"JsonUtilities",
});
}

public static void ConfigureServer(TargetRules TargetRules, OssConfig OssConfig)
{
var additionalData = GetOrAddAdditionalData<BeamableAdditionalData>(TargetRules);
AddUtilityMacros(TargetRules);
ConfigOnlineSubsystem(TargetRules, OssConfig, additionalData);

TargetRules.ExtraModuleNames.AddRange(new[]
{
"BeamableCore",
"BeamableCoreRuntime",

"Json",
"JsonUtilities",
});
}

public static void ConfigureGame(TargetRules TargetRules, OssConfig OssConfig)
{
var additionalData = GetOrAddAdditionalData<BeamableAdditionalData>(TargetRules);
AddUtilityMacros(TargetRules);
ConfigOnlineSubsystem(TargetRules, OssConfig, additionalData);

TargetRules.ExtraModuleNames.AddRange(new[]
{
"BeamableCore",
"BeamableCoreRuntime",

"Json",
"JsonUtilities",
});
}


/// <summary>
/// To be used in Target.cs files to add objects to <see cref="TargetRules.AdditionalData"/> in order to be used by various Build.cs files.
/// </summary>
public static T GetOrAddAdditionalData<T>(TargetRules TargetRules) where T : class, new()
{
TargetRules.AdditionalData ??= new Dictionary<Type, object>();

var dict = TargetRules.AdditionalData as Dictionary<Type, object>;
dict!.TryAdd(typeof(T), new T());
return dict[typeof(T)] as T;
}

/// <summary>
/// To be used in Build.cs files to read objects added to the <see cref="TargetRules.AdditionalData"/> by <see cref="GetOrAddAdditionalData{T}"/>.
/// </summary>
public static T GetAdditionalData<T>(this ReadOnlyTargetRules TargetRules) where T : class, new()
{
if (TargetRules.AdditionalData is Dictionary<Type, object> dictionary)
{
if (dictionary.TryGetValue(typeof(T), out var data))
return data as T;

throw new Exception(
$"Failed to find an additional data with the given type {typeof(T).Name}.\n" +
"Make sure you call GetOrAddAdditionalData<T> from your Target.cs file passing in the correct Type.\n");
}

throw new Exception(
"Using beamable requires the AdditionalData to be a Dictionary of Type/Object.\n" +
"We provide a utility function that allows you to add your own additional data next to beamable's own BeamableAdditionalData.\n" +
"We do this because the Beamable plugin needs some additional data but we didn't want to lock you out of passing in your own things.");
}


/// <summary>
/// Configures the Beamable SDK to work with UE's Online Subsystem stuff.
/// </summary>
private static void ConfigOnlineSubsystem(TargetRules TargetRules, OssConfig OssConfig, BeamableAdditionalData AdditionalData)
{
// Don't do anything to configure OSS if its not enabled
if (!OssConfig.IsEnabled) return;

// Include the OnlineSubsystem modules in the target.
TargetRules.ExtraModuleNames.AddRange(new[]
{
"OnlineSubsystem",
"OnlineSubsystemUtils",
"OnlineSubsystemBeamable"
});

// If we are building the editor, we also add the OnlineSubsystemBeamableBp module (which is where your BeamFlow nodes for Microservices live when OSS is enabled)
if (TargetRules.bBuildEditor)
{
TargetRules.ExtraModuleNames.AddRange(new[]
{
"OnlineSubsystemBeamableBp" // TODO: Make sure this module exists before doing this.
});
}

// Add the ENABLE OSS define so that the BeamableCore plugins can declare some utilities for when it is working with OSS
TargetRules.ProjectDefinitions.Add("BEAM_ENABLE_BEAMABLE_OSS=1");

// If OSS Hooks are enabled, the customer can pass in the Hook entry point implementation and we'll auto-configure it for them.
if (OssConfig.HooksEnabled)
{
TargetRules.ProjectDefinitions.Add("BEAM_ENABLE_OSS_HOOKS=1");
TargetRules.ProjectDefinitions.Add($"BEAM_OSS_SUBSYSTEM_IMPLEMENTATION={OssConfig.HookSubsystemImplementation}");
TargetRules.ProjectDefinitions.Add($"BEAM_OSS_SUBSYSTEM_INCLUDE=BEAM_STRINGIFY({OssConfig.HookSubsystemIncludePath})");

// Pass down, to the OnlineSubsystemBeamable plugin, a list of Module names that it'll also depend on
AdditionalData.OssAdditionalModules = OssConfig.AdditionalHookModules;
}
}

private static void AddUtilityMacros(TargetRules TargetRules)
{
TargetRules.ProjectDefinitions.Add("BEAM_STRINGIFY(X)=#X");
}
}
/// BEAMABLE CODE TO COPY PASTE END

0 comments on commit 59f7489

Please sign in to comment.