The three virtues of a programmer: Laziness, Impatience, and Hubris. – Larry Wall

Legacy:OS Detection

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search

Although Unrealscript is platform-independant, it can sometimes be useful for your code to determine what operating system it is running on. For instance, a Macintosh or Linux version of the Unreal Engine will not have Direct3D support and will only show white boxes if you try to use a ScriptedTexture. It may make sense if you use ScriptedTextures to have the script detect if the user is using Linux or Mac OS and replace the texture with something more attractive than a stark white box.

General OS Detection[edit]

This snippet works by attempting to pull a non-existing property from one of the drivers. An instance of the driver class will only exist, and therefore only reply with unrecognized property, if the driver is in fact loaded. WinDrv only being loaded on Windows, XDrv on Linux, and so on. This is not the most efficient implementation but should work on all Unreal Engine versions. (At least, Unreal Engine 2.5 and earlier. This is useable in any actor. The ConsoleCommand function of an Interaction does not return a string, and so will not work.

Note that "Unrecognized property" indicates that that is the CORRECT operating system. If you attempt to access XDrv.XClient from Windows, for instance, you get a different error message, "Unrecognized Class XDrv.XClient."

if(consolecommand("get WinDrv.WindowsClient")=="Unrecognized property")
// I'm windows
if(consolecommand("get XDrv.XClient")=="Unrecognized property")
// I'm linux
if(consolecommand("get MacDrv.MacClient")=="Unrecognized property")
// I'm mac

Posted by Fish HF on the Beyond Unreal coding forums.

UT2004[edit]

UT2004 has a few native functions that return what OS it is. They will also return whether the binary is 64-bit or 32-bit. A 32-bit binary running on a 64-bit system will return false to PlatformIs64Bit(). Note that although Mac OS is based on Unix, it will return false on PlatformIsUnix(), as it is already covered by PlatformIsMacOS(). These functions are declared in Object and so should be useable anywhere in script.

native final function bool PlatformIsMacOS();    // MacOS X.
native final function bool PlatformIsUnix();     // Linux, FreeBSD, etc (NOT OSX!)
native final function bool PlatformIsWindows();  // Win32, Win32.
native final function bool PlatformIs64Bit();    // Linux64, Win64.

Comments[edit]

El Muerte TDS: and what about "SDLDrv.SDLClient" that one is system independed, besides on dedicated servers the viewport setting doesn't matter. Also, what is the use, the Unreal engine is system independed in UScript, even things like filenames are corrected.

Foxpaw: The UT2004 one should be useable from any actor. The UT/UT2003 will need to be called on something that can execute console commands, and have the string value of lines written to the console during such time returned. (Some return only true or false instead of a string.) However, you could probrably do GetPropertyText instead.

Also, the above code was right before. You are attempting to access a non-existing property of an existing class. If you attempt to access XDrv.XClient it will give you "Unrecognized class XDrv.XClient" instead of "Unrecognized property"

Xian: XDrv is wrong, that is an experimental emulator for XWindows, not Linux necessarily.

The correct way is this (should work on UT400 - UTPG451b, but not sure about UTPG468):

function string CheckOS (PlayerPawn OSOwner)
{
    local string OSClass< SEMI >
    local string OS;
 
    OSClass = string(OSOwner.Player.Class);
 
    // string way
    switch (OSClass)
    {
        case "WinDrv.WindowsClient" :
            OS = "Windows";
            break;
        case "SDLDrv.SDLClient" :
            OS = "Linux";
            break;
        case "MacDrv.MacClient" :
            OS = "Mac";
            break;
    }
 
    // name way
    if (OSOwner.Player.IsA('WindowsViewport'))
        OS = "Windows";
    else if (OSOwner.Player.IsA('LinuxViewport'))
        OS = "Linux";
    else if (OSOwner.Player.IsA('MacViewport'))
        OS = "Mac";
 
    return OS;
}

FoxPawn: Also, the above code was right before. You are attempting to access a non-existing property of an existing class. If you attempt to access XDrv.XClient it will give you "Unrecognized class XDrv.XClient" instead of "Unrecognized property"

Wrong :) Well you're correct in what you said, but wrong in this case. That was not his point. It's true that if you attempt to recover a value of a property from a non existant class (we include typo-ed classes here too) it will return "Unrecognized Class" or a localized versioned text of that. However, his point was to check if the class is found, but property isn't, which proves the class exists, therefore, he can "guess" the OS. Although the Linux detection is wrong. The one I showed should be correct (both ways) for UE 1 games.