Hello Brian,
Well, you stated the oblious but that got be thinking. Here is my solution to detect Cesium Version at build time.
- Parse the CesiumForUnreal.uplugin file and extract version. This can be done using UBT. When we are done extracting the version, we add new C++ definitions:
private (int Major, int Minor, int Revision) GetPluginVersion(string PluginName)
{
string[] PossiblePaths = new string[]
{
//Path.Combine(ModuleDirectory, "../..", PluginName + ".uplugin"), // Local to module
Path.Combine(Target.ProjectFile.Directory.ToString(), "Plugins", PluginName, PluginName + ".uplugin"), // Project level
Path.Combine(Target.RelativeEnginePath, "Plugins", PluginName, PluginName + ".uplugin"), // Engine level
Path.Combine(Target.RelativeEnginePath, "Plugins", "Marketplace", PluginName, PluginName + ".uplugin"), // Engine level, marketplace
};
foreach (string PluginPath in PossiblePaths)
{
if (File.Exists(PluginPath))
{
string PluginFileContent = File.ReadAllText(PluginPath);
// Simple regex to extract the version name
Match match = Regex.Match(PluginFileContent, "\"VersionName\"\\s*:\\s*\"([^\"]+)\"");
if (match.Success)
{
string versionString = match.Groups[1].Value;
string[] versionComponents = versionString.Split('.');
if (versionComponents.Length >= 3 &&
int.TryParse(versionComponents[0], out int major) &&
int.TryParse(versionComponents[1], out int minor) &&
int.TryParse(versionComponents[2], out int revision))
{
return (major, minor, revision);
}
}
}
}
// Return -1 for all components if version extraction fails
return (-1, -1, -1);
}
Then from your module.cs constructor you can call
(int Major, int Minor, int Revision) CesiumPluginVersion = GetPluginVersion("CesiumForUnreal");
string Err = string.Format("Building with CesiumForUnreal version: {0}", CesiumPluginVersion);
System.Console.WriteLine(Err);
PublicDefinitions.Add("CESIUM_VERSION_MAJOR =" + CesiumPluginVersion.Major);
PublicDefinitions.Add("CESIUM_VERSION_MINOR =" + CesiumPluginVersion.Minor);
PublicDefinitions.Add("CESIUM_VERSION_REVISION=" + CesiumPluginVersion.Revision);
- I then created “CesiumVersionChecker.h” with the following content:
#define CESIUM_VERSION_AT_LEAST(major, minor, revision) \
((CESIUM_VERSION_MAJOR > (major)) || \
(CESIUM_VERSION_MAJOR == (major) && CESIUM_VERSION_MINOR > (minor)) || \
(CESIUM_VERSION_MAJOR == (major) && CESIUM_VERSION_MINOR == (minor) && CESIUM_VERSION_REVISION >= (revision)))
- From the code that depends on the Cesium plugin, I include “CesiumVersionChecker.h” then I can version check like this
#include "CesiumVersionChecker.h"
void myCode()
{
#if CESIUM_VERSION_AT_LEAST(2 , 5, 0)
// Code for Cesium version 2.5.0 or later
SomeFunctionInVersion2_5_0();
#else
// Code for older versions
SomeFunctionInOlderVersion();
#endif
}
}
Enjoy!