Skip to content

Commit

Permalink
fix: dynamically search for Terminal.ConsoleCommand constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
MSchmoecker committed Jul 3, 2023
1 parent fdc1b9f commit e60b606
Showing 1 changed file with 69 additions and 6 deletions.
75 changes: 69 additions & 6 deletions JotunnLib/Managers/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,81 @@ private void AddCustomCommands(Console self)
}

// Add to the vanilla system
new Terminal.ConsoleCommand(cmd.Name, cmd.Help, args =>
{
cmd.Run(args.Args.Skip(1).ToArray());
},
isCheat: cmd.IsCheat, isNetwork: cmd.IsNetwork, onlyServer: cmd.OnlyServer,
isSecret: cmd.IsSecret, optionsFetcher: cmd.CommandOptionList);
CreateVanillaCommand(cmd);
}

self.updateCommandList();
}
}

private Terminal.ConsoleCommand CreateVanillaCommand(ConsoleCommand command)
{
var constructor = AccessTools.Constructor(typeof(Terminal.ConsoleCommand), new[]
{
typeof(string),
typeof(string),
typeof(Terminal.ConsoleEvent),
typeof(bool),
typeof(bool),
typeof(bool),
typeof(bool),
typeof(bool),
typeof(Terminal.ConsoleOptionsFetcher),
});

// Valheim 0.216.9 and below
if (constructor != null)
{
return (Terminal.ConsoleCommand)constructor.Invoke(new object[]
{
command.Name,
command.Help,
(Terminal.ConsoleEvent)((args) => command.Run(args.Args.Skip(1).ToArray())),
command.IsCheat,
command.IsNetwork,
command.OnlyServer,
command.IsSecret,
false, // allowInDevBuild
(Terminal.ConsoleOptionsFetcher)command.CommandOptionList,
});
}

constructor = AccessTools.Constructor(typeof(Terminal.ConsoleCommand), new[]
{
typeof(string),
typeof(string),
typeof(Terminal.ConsoleEvent),
typeof(bool),
typeof(bool),
typeof(bool),
typeof(bool),
typeof(bool),
typeof(Terminal.ConsoleOptionsFetcher),
typeof(bool),
});

// Valheim 0.217.7 and above
if (constructor != null)
{
return (Terminal.ConsoleCommand)constructor.Invoke(new object[]
{
command.Name,
command.Help,
(Terminal.ConsoleEvent)((args) => command.Run(args.Args.Skip(1).ToArray())),
command.IsCheat,
command.IsNetwork,
command.OnlyServer,
command.IsSecret,
false, // allowInDevBuild
(Terminal.ConsoleOptionsFetcher)command.CommandOptionList,
false, // alwaysRefreshTabOptions
});
}

Logger.LogError("No suitable constructor for Terminal.ConsoleCommand found");
return null;
}

/// <summary>
/// Fire <see cref="OnGetTabOptions"/> for any ConsoleCommand when its tabOptions member
/// is first populated to add Jötunn entities to the option list
Expand Down

0 comments on commit e60b606

Please sign in to comment.