From c31f2abfc9bdf50be7661ee2782215ab62fa256b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sat, 3 Jun 2023 14:14:31 +0200 Subject: [PATCH] Add sanity checks to the Lua script trait. --- OpenRA.Mods.Common/Lint/CheckLuaScript.cs | 42 +++++++++++++++++++++++ OpenRA.Mods.Common/Scripting/LuaScript.cs | 2 ++ 2 files changed, 44 insertions(+) create mode 100644 OpenRA.Mods.Common/Lint/CheckLuaScript.cs diff --git a/OpenRA.Mods.Common/Lint/CheckLuaScript.cs b/OpenRA.Mods.Common/Lint/CheckLuaScript.cs new file mode 100644 index 0000000000..d1b603302d --- /dev/null +++ b/OpenRA.Mods.Common/Lint/CheckLuaScript.cs @@ -0,0 +1,42 @@ +#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 OpenRA.FileSystem; +using OpenRA.Mods.Common.Scripting; +using OpenRA.Server; + +namespace OpenRA.Mods.Common.Lint +{ + public class CheckLuaScript : ILintMapPass, ILintServerMapPass + { + void ILintMapPass.Run(Action emitError, Action emitWarning, ModData modData, Map map) + { + CheckLuaScriptFileExistance(emitError, map.Package, modData.DefaultFileSystem, map.Rules); + } + + void ILintServerMapPass.Run(Action emitError, Action emitWarning, ModData modData, MapPreview map, Ruleset mapRules) + { + CheckLuaScriptFileExistance(emitError, map.Package, modData.DefaultFileSystem, mapRules); + } + + static void CheckLuaScriptFileExistance(Action emitError, IReadOnlyPackage package, IReadOnlyFileSystem fileSystem, Ruleset mapRules) + { + var luaScriptInfo = mapRules.Actors[SystemActors.World].TraitInfoOrDefault(); + if (luaScriptInfo == null) + return; + + foreach (var script in luaScriptInfo.Scripts) + if (!package.Contains(script) && !fileSystem.Exists(script)) + emitError($"Lua script `{script}` does not exist."); + } + } +} diff --git a/OpenRA.Mods.Common/Scripting/LuaScript.cs b/OpenRA.Mods.Common/Scripting/LuaScript.cs index 117d82c3be..c9a7a6833a 100644 --- a/OpenRA.Mods.Common/Scripting/LuaScript.cs +++ b/OpenRA.Mods.Common/Scripting/LuaScript.cs @@ -18,9 +18,11 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Scripting { + [TraitLocation(SystemActors.World)] [Desc("Part of the new Lua API.")] public class LuaScriptInfo : TraitInfo, Requires { + [Desc("File names with location relative to the map.")] public readonly HashSet Scripts = new(); public override object Create(ActorInitializer init) { return new LuaScript(this); }