Cogito, ergo sum

Legacy:EditPackagesCommandlet

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

This commandlet lets you modify EditPackages from OS commandline. Handy for install scripts.

Usage[edit]

ucc package.EditPackagesCommandlet +AddPackage +AddAnother -RemovePackage

Paramaters[edit]

Parameter Description
--dry-run Simulate the process. Changes will not be saved.
+Package Add a package with the name Package. If it already exists it will be removed first.
-Package- Remove the package with the name Package.
Advanced
+?Package Don't add the package when it already exists. '?' can also be used in the commands below.
+Package@index Add the package at the given index.
+Package<Pkg Add the package before Pkg. If Pkg isn't present it will be appended to the list.
+Package>Pkg Add the package after Pkg. If Pkg isn't present it will be appended to the list.

Note, you must use double quotes around the commands that use the before or after modifier when executing it from a commandline.

Examples[edit]

ucc package.EditPackagesCommandlet "+myPackage>Engine" "+MyOtherPackage<myPackage" -MyOldPackage

Source code[edit]

/*******************************************************************************
    EditPackagesCommandlet
    This commandlet lets you modify EditPackages from OS commandline. Handy for
    install scripts.
 
    Usage:
        ucc package.EditPackagesCommandlet +AddPackage +AddAnother -RemovePackage
 
    +?Package
    +Package@index
    +Package<BeforeThisPackage
    +Package>AfterThisPackage
 
    Otherwise it will be added after the list. If the before or after packages
    don't exist they will be added to the end of the list. Adding a package that
    already exists will first remove it from the old location.
 
    Credits:
        Switch -- initial code
        El Muerte -- major improvements
*******************************************************************************/
class EditPackagesCommandlet extends Commandlet;
 
/**
    The contents of this will be appended to the Parms list, this can be used
    is a subclass of the commandlet.
*/
var config string CmdArgs;
 
/** the classname of the object to load, that contains the ArrayName */
var string ObjectClass< SEMI >
/** the name of the array to read\write the data to */
var string ArrayName;
 
/** temporary storage of the array list */
var protected array<string> PackagesArray< SEMI >
 
/** add the package to the list */
function int AddPackage(string pkg, optional int idx)
{
    if (idx == -1) idx = PackagesArray.length;
    else idx = Clamp(idx, 0, PackagesArray.length);
    PackagesArray.insert(idx, 1);
    PackagesArray[idx] = pkg;
    return idx;
}
 
/** remove the package from the list, returns the number of offurances deleted */
function int RemovePackage(string pkg)
{
    local int i, deleted;
    deleted = 0;
    for (i = PackagesArray.Length-1; i >= 0; i--)
    {
        if (PackagesArray[i] ~= pkg)
        {
            PackagesArray.Remove(i, 1);
            deleted++;
        }
    }
    return deleted;
}
 
/** returns the index of a package */
function int IndexOf(string pkg)
{
    local int i;
    for (i = PackagesArray.Length-1; i >= 0; i--)
    {
        if (PackagesArray[i] ~= pkg)
        {
            return i;
        }
    }
    return -1;
}
 
final function string StripQuotes(string in)
{
    if (Left(in, 1) == "\"")
    {
        return mid(in, 1, len(in)-2);
    }
    return in;
}
 
event int Main( string Parms )
{
    local class C;
    local object O;
    local int i,j,idx;
    local array<string> Commands;
    local string cmd, altpkg;
    local bool bSimulate;
 
    bSimulate = false;
 
    if (CmdArgs != "") Parms = CmdArgs@Parms;
 
    if (Parms == "")
    {
        Log( "Nothing to do. See 'ucc help "$HelpCmd$"' for more information.", 'Error' );
        return 1;
    }
    split(Parms, " ", Commands);
 
    // Load class
    C = class(DynamicLoadObject( ObjectClass, class'Class', true ));
    if( C == None )
    {
        Log( "Failed to load class: ["$ ObjectClass $"]", 'Error' );
        return 1;
    }
    // Create object
    O = new C;
    if( O == None )
    {
        Log( "Failed to create object of class: ["$ C $"]", 'Error' );
        return 1;
    }
 
    SetPropertyText("PackagesArray",O.GetPropertyText(ArrayName));
 
    // process commands
    for (i = 0; i < Commands.length; i++)
    {
        cmd = StripQuotes(Commands[i]);
        if (cmd ~= "--dry-run") {
            bSimulate = true;
        }
        else if (left(cmd, 1) == "+")
        {
            idx = -1;
            cmd = mid(cmd, 1);
            j = InStr(cmd, "@"); // find @index
            if (j > 0)
            {
                idx = int(mid(cmd, j+1));
                if (idx == 0) // idx should never be 0, core or engine is always required
                {
                    log("Invalid index: "$mid(cmd, j+1), 'Error');
                    continue;
                }
                cmd = left(cmd, j);
            }
            else {
                j = InStr(cmd, "<"); // before
                if (j > 0)
                {
                    altpkg = mid(cmd, j+1);
                    cmd = left(cmd, j);
                    idx = IndexOf(altpkg);
                    if (idx == -1)
                    {
                        log("Package '"$altpkg$"' doesn't exists; it will be appended to the list.", 'Warning');
                        idx = AddPackage(altpkg, -1);
                    }
                }
                else {
                    j = InStr(cmd, ">"); // after
                    if (j > 0)
                    {
                        altpkg = mid(cmd, j+1);
                        cmd = left(cmd, j);
                        idx = IndexOf(altpkg);
                        if (idx == -1)
                        {
                            log("Package '"$altpkg$"' doesn't exists; it will be appended to the list.", 'Warning');
                            idx = AddPackage(altpkg, -1);
                        }
                        idx++;
                    }
                }
            }
            if (left(cmd, 1) == "?") // don't add if already present
            {
                cmd = mid(cmd, 1);
                if (IndexOf(cmd) > -1)
                {
                    log("Package '"$cmd$"' is already present.", 'Add');
                    continue;
                }
            }
            RemovePackage(cmd); // first remove old
            idx = AddPackage(cmd, idx);
            if (idx < 0)
            {
                log("Failed to add package '"$cmd$"'", 'Error');
            }
            else {
                Log("Package '"$cmd$"' added at index "$idx, 'Add');
            }
        }
        else if (left(cmd, 1) == "-")
        {
            cmd = mid(cmd, 1);
            j = RemovePackage(cmd);
            if (j > 0) Log("Package '"$cmd$"' was removed "$j$" time(s).", 'Remove');
            else log("Package '"$cmd$"' was not found, therefor not removed.", 'Warning');
        }
        else if (cmd != "") {
            Log( "Unsupported argument: "$cmd, 'Error' );
        }
    }
    if (!bSimulate)
    {
        O.SetPropertyText(ArrayName,GetPropertyText("PackagesArray"));
        O.SaveConfig();
    }
    else {
        log("Simulation; changes not saved.");
        log("New array content:");
        log(chr(9)$GetPropertyText("PackagesArray"));
    }
    return 0;
}
 
DefaultProperties
{
    HelpCmd="EditPackagesCommandlet"
    HelpOneLiner="Modify the EditPackages list."
    HelpUsage="EditPackagesCommandlet [--dry-run] package-action ..."
    HelpWebLink="http://wiki.beyondunreal.com/wiki/EditPackagesCommandlet"
    HelpParm[0]="--dry-run"
    HelpDesc[0]="Simulate the process. Changes will not be saved."
    HelpParm[1]="+Package"
    HelpDesc[1]="Add a package with the name Package. If it already exists it will be removed first."
    HelpParm[2]="-Package"
    HelpDesc[2]="Remove the package with the name Package."
    HelpParm[3]=" "
    HelpDesc[3]="Advanced commands:"
    HelpParm[4]="+?Package"
    HelpDesc[4]="Don't add the package when it already exists. '?' can also be used in the commands below."
    HelpParm[5]="+Package@index"
    HelpDesc[5]="Add the package at the given index."
    HelpParm[6]="+Package<Pkg"
    HelpDesc[6]="Add the package before Pkg. If Pkg isn't present it will be appended to the list."
    HelpParm[7]="+Package>Pkg"
    HelpDesc[7]="Add the package after Pkg. If Pkg isn't present it will be appended to the list."
    ShowBanner=false
    ShowErrorCount=true
 
    ObjectClass="Editor.EditorEngine"
    ArrayName="EditPackages"
}