Improve Lua type documentation and bindings.
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.
This commit is contained in:
@@ -63,8 +63,9 @@ namespace OpenRA.Mods.Common.Scripting
|
||||
|
||||
[ScriptActorPropertyActivity]
|
||||
[Desc("Patrol along a set of given waypoints until a condition becomes true. " +
|
||||
"The actor will wait for `wait` ticks at each waypoint.")]
|
||||
public void PatrolUntil(CPos[] waypoints, LuaFunction func, int wait = 0)
|
||||
"The actor will wait for `wait` ticks at each waypoint. " +
|
||||
"The callback function will be called as func(self: actor):boolean.")]
|
||||
public void PatrolUntil(CPos[] waypoints, [ScriptEmmyTypeOverride("fun(self: actor):boolean")] LuaFunction func, int wait = 0)
|
||||
{
|
||||
Patrol(waypoints, false, wait);
|
||||
|
||||
|
||||
@@ -137,7 +137,7 @@ namespace OpenRA.Mods.Common.Scripting
|
||||
|
||||
[ScriptActorPropertyActivity]
|
||||
[Desc("Run an arbitrary Lua function.")]
|
||||
public void CallFunc(LuaFunction func)
|
||||
public void CallFunc([ScriptEmmyTypeOverride("fun()")] LuaFunction func)
|
||||
{
|
||||
Self.QueueActivity(new CallLuaFunc(func, Context));
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace OpenRA.Mods.Common.Scripting
|
||||
public int MaxHealth => health.MaxHP;
|
||||
|
||||
[Desc("Kill the actor. damageTypes may be omitted, specified as a string, or as table of strings.")]
|
||||
public void Kill(object damageTypes = null)
|
||||
public void Kill([ScriptEmmyTypeOverride("string|{ [unknown]: string }")] object damageTypes = null)
|
||||
{
|
||||
Damage damage;
|
||||
if (damageTypes is string d)
|
||||
|
||||
@@ -26,6 +26,7 @@ namespace OpenRA.Mods.Common.Scripting
|
||||
exp = player.PlayerActor.Trait<PlayerExperience>();
|
||||
}
|
||||
|
||||
[Desc("Get or set the current experience.")]
|
||||
public int Experience
|
||||
{
|
||||
get => exp.Experience;
|
||||
|
||||
@@ -130,10 +130,10 @@ namespace OpenRA.Mods.Common.Scripting
|
||||
|
||||
[Desc("Build the specified set of actors using a TD-style (per building) production queue. " +
|
||||
"The function will return true if production could be started, false otherwise. " +
|
||||
"If an actionFunc is given, it will be called as actionFunc(Actor[] actors) once " +
|
||||
"If an actionFunc is given, it will be called as actionFunc(actors: actor[]) once " +
|
||||
"production of all actors has been completed. The actors array is guaranteed to " +
|
||||
"only contain alive actors.")]
|
||||
public bool Build(string[] actorTypes, LuaFunction actionFunc = null)
|
||||
public bool Build(string[] actorTypes, [ScriptEmmyTypeOverride("fun(actors: actor[])")] LuaFunction actionFunc = null)
|
||||
{
|
||||
if (triggers.HasAnyCallbacksFor(Trigger.OnProduction))
|
||||
return false;
|
||||
@@ -234,11 +234,11 @@ namespace OpenRA.Mods.Common.Scripting
|
||||
|
||||
[Desc("Build the specified set of actors using classic (RA-style) production queues. " +
|
||||
"The function will return true if production could be started, false otherwise. " +
|
||||
"If an actionFunc is given, it will be called as actionFunc(Actor[] actors) once " +
|
||||
"If an actionFunc is given, it will be called as actionFunc(actors: actor[]) once " +
|
||||
"production of all actors has been completed. The actors array is guaranteed to " +
|
||||
"only contain alive actors. Note: This function will fail to work when called " +
|
||||
"during the first tick.")]
|
||||
public bool Build(string[] actorTypes, LuaFunction actionFunc = null)
|
||||
public bool Build(string[] actorTypes, [ScriptEmmyTypeOverride("fun(actors: actor[])")] LuaFunction actionFunc = null)
|
||||
{
|
||||
var typeToQueueMap = new Dictionary<string, string>();
|
||||
foreach (var actorType in actorTypes.Distinct())
|
||||
|
||||
Reference in New Issue
Block a user