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.
95 lines
2.8 KiB
C#
95 lines
2.8 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 System;
|
|
using Eluant;
|
|
using OpenRA.Mods.Common.Traits;
|
|
using OpenRA.Scripting;
|
|
|
|
namespace OpenRA.Mods.Common.Scripting
|
|
{
|
|
[ScriptGlobal("DateTime")]
|
|
public class DateGlobal : ScriptGlobal
|
|
{
|
|
readonly TimeLimitManager tlm;
|
|
readonly int ticksPerSecond;
|
|
|
|
public DateGlobal(ScriptContext context)
|
|
: base(context)
|
|
{
|
|
tlm = context.World.WorldActor.TraitOrDefault<TimeLimitManager>();
|
|
var gameSpeeds = Game.ModData.Manifest.Get<GameSpeeds>();
|
|
var defaultGameSpeed = gameSpeeds.Speeds[gameSpeeds.DefaultSpeed];
|
|
ticksPerSecond = 1000 / defaultGameSpeed.Timestep;
|
|
}
|
|
|
|
[Desc("True on the 31st of October.")]
|
|
[Obsolete("Use CurrentMonth and CurrentDay instead.")]
|
|
public bool IsHalloween => DateTime.Today.Month == 10 && DateTime.Today.Day == 31;
|
|
|
|
[Desc("Get the current game time (in ticks).")]
|
|
public int GameTime => Context.World.WorldTick;
|
|
|
|
[Desc("Converts the number of seconds into game time (ticks).")]
|
|
public int Seconds(int seconds)
|
|
{
|
|
return seconds * ticksPerSecond;
|
|
}
|
|
|
|
[Desc("Get the current year (1-9999).")]
|
|
public int CurrentYear => DateTime.Now.Year;
|
|
[Desc("Get the current month (1-12).")]
|
|
public int CurrentMonth => DateTime.Now.Month;
|
|
[Desc("Get the current day (1-31).")]
|
|
public int CurrentDay => DateTime.Now.Day;
|
|
[Desc("Get the current hour (0-23).")]
|
|
public int CurrentHour => DateTime.Now.Hour;
|
|
[Desc("Get the current minute (0-59).")]
|
|
public int CurrentMinute => DateTime.Now.Minute;
|
|
[Desc("Get the current second (0-59).")]
|
|
public int CurrentSecond => DateTime.Now.Second;
|
|
|
|
[Desc("Converts the number of minutes into game time (ticks).")]
|
|
public int Minutes(int minutes)
|
|
{
|
|
return Seconds(minutes * 60);
|
|
}
|
|
|
|
[Desc("Return or set the time limit (in ticks). When setting, the time limit will count from now. Setting the time limit to 0 will disable it.")]
|
|
public int TimeLimit
|
|
{
|
|
get => tlm?.TimeLimit ?? 0;
|
|
|
|
set
|
|
{
|
|
if (tlm != null)
|
|
tlm.TimeLimit = value == 0 ? 0 : value + GameTime;
|
|
else
|
|
throw new LuaException("Cannot set TimeLimit, TimeLimitManager trait is missing.");
|
|
}
|
|
}
|
|
|
|
[Desc("The notification string used for custom time limit warnings. See the TimeLimitManager trait documentation for details.")]
|
|
public string TimeLimitNotification
|
|
{
|
|
get => tlm?.Notification;
|
|
|
|
set
|
|
{
|
|
if (tlm != null)
|
|
tlm.Notification = value;
|
|
else
|
|
throw new LuaException("Cannot set TimeLimitNotification, TimeLimitManager trait is missing.");
|
|
}
|
|
}
|
|
}
|
|
}
|