Compile time version Cesium plugin detection

Hello,

I’m in situation where I need to support various Cesium Plugin version from UE5 (C++). Between versions, some function signature are updated, breaking the build.

Is there a way to detect the Cesium plugin version at compile time? (something similar to UE UE_5_4_OR_LATER definition).

Thanks

Hi @jplebel, we keep our version number in the plugin’s CesiumForUnreal.uplugin file.

Is that enough to work with?

Hello Brian,

Well, you stated the oblious but that got be thinking. Here is my solution to detect Cesium Version at build time.

  1. 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);
  1. 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)))
  1. 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!