diff --git a/Commands.cs b/Commands.cs index a7333bd..128a650 100644 --- a/Commands.cs +++ b/Commands.cs @@ -31,15 +31,15 @@ private set } } - public Dictionary? CommandHandlers { get => commandHandlers; set => commandHandlers = value; } + public Dictionary? CommandHandlers { get => commandHandlers; set => commandHandlers = value; } public Dictionary>? GameVariableMethods { get => gameVariableMethods; set => gameVariableMethods = value; } - private Dictionary? commandHandlers; + private Dictionary? commandHandlers; private Dictionary>? gameVariableMethods; public void DiscoverHandlersAndVariables() { - CommandHandlers = new Dictionary(); + CommandHandlers = new Dictionary(); GameVariableMethods = new Dictionary>(); try @@ -59,7 +59,8 @@ public void DiscoverHandlersAndVariables() .Where(param => param.ParameterType != typeof(HttpListenerResponse)) .Select(param => param.Name) .ToArray(); - CommandHandlers[path] = (method, parameters, commandAttribute.Category ?? string.Empty, commandAttribute.Description ?? string.Empty); + // Initialize AutoCompleteOptions as an empty dictionary + CommandHandlers[path] = (method, parameters, commandAttribute.Category ?? string.Empty, commandAttribute.Description ?? string.Empty, commandAttribute.AutoCompleteMethodName ?? string.Empty); } // Discover Game Variables diff --git a/HandlerAttributes.cs b/HandlerAttributes.cs index 0ea8bf3..ac4cd29 100644 --- a/HandlerAttributes.cs +++ b/HandlerAttributes.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; @@ -12,16 +13,33 @@ public class CommandHandlerAttribute : Attribute public string Path { get; } public string Category { get; } public string Description { get; } + public string AutoCompleteMethodName { get; set; } // New property - // Adjust the constructor to accept category as an optional parameter - public CommandHandlerAttribute(string path, string category = "Miscellaneous", string description = "") + public CommandHandlerAttribute(string path, string category = "Miscellaneous", string description = "", string autoCompleteMethodName = "") { Path = "/" + path; Category = category; Description = description; + AutoCompleteMethodName = autoCompleteMethodName; } + + //Example usage with autocomplete + + //[CommandHandler("SpawnResource", "Resources", "desc", "SpawnResourceAutoComplete")] + //public static void SpawnResourceHttp(HttpListenerResponse response, string resource = "Gold", string isInfinite = "False") + + //public static Dictionary SpawnResourceAutoComplete() + //{ + // Dictionary responseOptions = new Dictionary(); + + // responseOptions["resource"] = Enum.GetNames(typeof(Minerals.MineralTypes)); + // responseOptions["isInfinite"] = new[] { "False", "True" }; + + // return responseOptions; + //} } + [AttributeUsage(AttributeTargets.Method)] public class GameVariableAttribute : Attribute { diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index c8b2636..664e451 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -1,4 +1,4 @@ using MelonLoader; -[assembly: MelonInfo(typeof(Wicker.WickerServer), "WickerREST", "0.93.0", "Skrip")] +[assembly: MelonInfo(typeof(Wicker.WickerServer), "WickerREST", "0.94.0", "Skrip")] [assembly: MelonGame("Crate Entertainment", "Farthest Frontier")] \ No newline at end of file diff --git a/WickerNetwork.cs b/WickerNetwork.cs index 1dbebf5..78f3846 100644 --- a/WickerNetwork.cs +++ b/WickerNetwork.cs @@ -166,16 +166,37 @@ private void ServeCommandHandlers(HttpListenerResponse response) { if (Commands.Instance.CommandHandlers != null && Commands.Instance.CommandHandlers.Count > 0) { - //await EnsureFileExists("/dwyl", DWYL_URL, true); var commandsInfo = Commands.Instance.CommandHandlers.Select(handler => new { Path = handler.Key, Parameters = handler.Value.Method.GetParameters() - .Select(p => new { - p.Name, - Type = p.ParameterType.Name, - DefaultValue = p.HasDefaultValue ? p.DefaultValue?.ToString() : null - }) - .ToArray(), + .Select(p => { + Dictionary autoCompleteOptions = new Dictionary(); + if (!string.IsNullOrEmpty(handler.Value.AutoCompleteMethodName)) + { + MethodInfo? autoCompleteMethod; + try + { + autoCompleteMethod = handler.Value.Method.DeclaringType.GetMethod(handler.Value.AutoCompleteMethodName, BindingFlags.Static | BindingFlags.Public); + if (autoCompleteMethod != null && autoCompleteMethod.ReturnType == typeof(Dictionary)) + { + autoCompleteOptions = (Dictionary)autoCompleteMethod.Invoke(null, null); + } + } + catch (Exception ex) + { + WickerServer.Instance.LogMessage($"Error invoking AutoComplete method: {ex.Message}"); + } + } + + return new + { + p.Name, + Type = p.ParameterType.Name, + DefaultValue = p.HasDefaultValue ? p.DefaultValue?.ToString() : null, + AutoCompleteOptions = autoCompleteOptions.ContainsKey(p.Name) ? autoCompleteOptions[p.Name] : Array.Empty() + }; + }) + .ToArray(), Category = handler.Value.Category, Description = handler.Value.Description }).ToArray(); diff --git a/WickerREST.csproj b/WickerREST.csproj index 5536014..f1ca44f 100644 --- a/WickerREST.csproj +++ b/WickerREST.csproj @@ -4,8 +4,8 @@ net6.0 enable enable - 0.93.0.0 - 0.93.0.0 + 0.94.0.0 + 0.94.0.0 diff --git a/WickerServer.cs b/WickerServer.cs index e4de1ab..3f31e4e 100644 --- a/WickerServer.cs +++ b/WickerServer.cs @@ -10,7 +10,7 @@ public static class BuildInfo public const string Name = "WickerREST"; public const string Description = "WickerREST"; public const string Author = "Skrip"; - public const string Version = "0.93.0"; + public const string Version = "0.94.0"; public const string DownloadLink = ""; } diff --git a/web/index.html b/web/index.html index 397d72c..5bd2bc0 100644 --- a/web/index.html +++ b/web/index.html @@ -94,7 +94,7 @@

Command Output

- +