The ExtractEmmyLuaAPI utility command, invoked with `--emmy-lua-api`, produces a documentation file that is used by the [OpenRA Lua Language Extension](https://marketplace.visualstudio.com/items?itemName=openra.vscode-openra-lua) to provide documentation and type information is VSCode and VSCode compatible editors when editing the Lua scripts. We improve the documentation and types produced by this utility in a few ways: - Require descriptions to be provided for all items. - Fix the type definitions of the base engine types (cpos, wpos, wangle, wdist, wvec, cvec) to match with the actual bindings on the C# side. Add some extra bindings for these types to increase their utility. - Introduce ScriptEmmyTypeOverrideAttribute to allow the C# side of the bindings to provide a more specific type. The utility command now requires this to be used to avoid accidentally exporting poor type information. - Fix a handful of scripts where the new type information revealed warnings. The ability to ScriptEmmyTypeOverrideAttribute allows parameters and return types to provide a more specific type compared to the previous, weak, type definition. For example LuaValue mapped to `any`, LuaTable mapped to `table`, and LuaFunction mapped to `function`. These types are all non-specific. `any` can be anything, `table` is a table without known types for its keys or values, `function` is a function with an unknown signature. Now, we can provide specific types. , e.g. instead of `table`, ReinforcementsGlobal.ReinforceWithTransport is able to specify `{ [1]: actor, [2]: actor[] }` - a table with keys 1 and 2, whose values are an actor, and a table of actors respectively. The callback functions in MapGlobal now have signatures, e.g. instead of `function` we have `fun(a: actor):boolean`. In UtilsGlobal, we also make use of generic types. These work in a similar fashion to generics in C#. These methods operate on collections, we can introduce a generic parameter named `T` for the type of the items in those collections. Now the return type and callback parameters can also use that generic type. This means the return type or callback functions operate on the same type as whatever type is in the collection you pass in. e.g. Utils.Do accepts a collection typed as `T[]` with a callback function invoked on each item typed as `fun(item: T)`. If you pass in actors, the callback operates on an actor. If you pass in strings, the callback operates on a string, etc. Overall, these changes should result in an improved user experience for those editing OpenRA Lua scripts in a compatible IDE.
128 lines
4.1 KiB
C#
128 lines
4.1 KiB
C#
#region Copyright & License Information
|
|
/*
|
|
* Copyright (c) The OpenRA Developers and Contributors
|
|
* This file is part of OpenRA, which is free software. It is made
|
|
* available to you under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation, either version 3 of
|
|
* the License, or (at your option) any later version. For more
|
|
* information, see COPYING.
|
|
*/
|
|
#endregion
|
|
|
|
using Eluant;
|
|
using OpenRA.Primitives;
|
|
using OpenRA.Scripting;
|
|
|
|
namespace OpenRA.Mods.Common.Scripting.Global
|
|
{
|
|
// Kept as HSLColor for backwards compatibility
|
|
[ScriptGlobal("HSLColor")]
|
|
public class ColorGlobal : ScriptGlobal
|
|
{
|
|
public ColorGlobal(ScriptContext context)
|
|
: base(context) { }
|
|
|
|
[Desc("Create a new color with the specified hue/saturation/luminosity.")]
|
|
public Color New(int hue, int saturation, int luminosity)
|
|
{
|
|
var h = (byte)hue.Clamp(0, 255);
|
|
var s = (byte)saturation.Clamp(0, 255);
|
|
var l = (byte)luminosity.Clamp(0, 255);
|
|
|
|
return Color.FromAhsl(255, h / 255f, s / 255f, l / 255f);
|
|
}
|
|
|
|
[Desc("Create a new color with the specified red/green/blue/[alpha] values.")]
|
|
public Color FromRGB(int red, int green, int blue, int alpha = 255)
|
|
{
|
|
return Color.FromArgb(
|
|
alpha.Clamp(0, 255),
|
|
red.Clamp(0, 255),
|
|
green.Clamp(0, 255),
|
|
blue.Clamp(0, 255));
|
|
}
|
|
|
|
[Desc("Create a new color with the specified red/green/blue/[alpha] hex string (rrggbb[aa]).")]
|
|
public Color FromHex(string value)
|
|
{
|
|
if (Color.TryParse(value, out var color))
|
|
return color;
|
|
|
|
throw new LuaException("Invalid rrggbb[aa] hex string.");
|
|
}
|
|
|
|
[Desc("FromHex(\"00FFFF\")")]
|
|
public Color Aqua => Color.Aqua;
|
|
[Desc("FromHex(\"000000\")")]
|
|
public Color Black => Color.Black;
|
|
[Desc("FromHex(\"0000FF\")")]
|
|
public Color Blue => Color.Blue;
|
|
[Desc("FromHex(\"A52A2A\")")]
|
|
public Color Brown => Color.Brown;
|
|
[Desc("FromHex(\"00FFFF\")")]
|
|
public Color Cyan => Color.Cyan;
|
|
[Desc("FromHex(\"00008B\")")]
|
|
public Color DarkBlue => Color.DarkBlue;
|
|
[Desc("FromHex(\"008B8B\")")]
|
|
public Color DarkCyan => Color.DarkCyan;
|
|
[Desc("FromHex(\"A9A9A9\")")]
|
|
public Color DarkGray => Color.DarkGray;
|
|
[Desc("FromHex(\"006400\")")]
|
|
public Color DarkGreen => Color.DarkGreen;
|
|
[Desc("FromHex(\"FF8C00\")")]
|
|
public Color DarkOrange => Color.DarkOrange;
|
|
[Desc("FromHex(\"8B0000\")")]
|
|
public Color DarkRed => Color.DarkRed;
|
|
[Desc("FromHex(\"FF00FF\")")]
|
|
public Color Fuchsia => Color.Fuchsia;
|
|
[Desc("FromHex(\"FFD700\")")]
|
|
public Color Gold => Color.Gold;
|
|
[Desc("FromHex(\"808080\")")]
|
|
public Color Gray => Color.Gray;
|
|
[Desc("FromHex(\"008000\")")]
|
|
public Color Green => Color.Green;
|
|
[Desc("FromHex(\"7CFC00\")")]
|
|
public Color LawnGreen => Color.LawnGreen;
|
|
[Desc("FromHex(\"ADD8E6\")")]
|
|
public Color LightBlue => Color.LightBlue;
|
|
[Desc("FromHex(\"E0FFFF\")")]
|
|
public Color LightCyan => Color.LightCyan;
|
|
[Desc("FromHex(\"D3D3D3\")")]
|
|
public Color LightGray => Color.LightGray;
|
|
[Desc("FromHex(\"90EE90\")")]
|
|
public Color LightGreen => Color.LightGreen;
|
|
[Desc("FromHex(\"FFFFE0\")")]
|
|
public Color LightYellow => Color.LightYellow;
|
|
[Desc("FromHex(\"00FF00\")")]
|
|
public Color Lime => Color.Lime;
|
|
[Desc("FromHex(\"32CD32\")")]
|
|
public Color LimeGreen => Color.LimeGreen;
|
|
[Desc("FromHex(\"FF00FF\")")]
|
|
public Color Magenta => Color.Magenta;
|
|
[Desc("FromHex(\"800000\")")]
|
|
public Color Maroon => Color.Maroon;
|
|
[Desc("FromHex(\"000080\")")]
|
|
public Color Navy => Color.Navy;
|
|
[Desc("FromHex(\"808000\")")]
|
|
public Color Olive => Color.Olive;
|
|
[Desc("FromHex(\"FFA500\")")]
|
|
public Color Orange => Color.Orange;
|
|
[Desc("FromHex(\"FF4500\")")]
|
|
public Color OrangeRed => Color.OrangeRed;
|
|
[Desc("FromHex(\"800080\")")]
|
|
public Color Purple => Color.Purple;
|
|
[Desc("FromHex(\"FF0000\")")]
|
|
public Color Red => Color.Red;
|
|
[Desc("FromHex(\"FA8072\")")]
|
|
public Color Salmon => Color.Salmon;
|
|
[Desc("FromHex(\"87CEEB\")")]
|
|
public Color SkyBlue => Color.SkyBlue;
|
|
[Desc("FromHex(\"008080\")")]
|
|
public Color Teal => Color.Teal;
|
|
[Desc("FromHex(\"FFFF00\")")]
|
|
public Color Yellow => Color.Yellow;
|
|
[Desc("FromHex(\"FFFFFF\")")]
|
|
public Color White => Color.White;
|
|
}
|
|
}
|