Friday, September 20, 2024 12:05:36 AM
> settings

Customize


Authenticate

> Converter.cs
/*
 * Bryan
 * Exile Server Manager
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace esm
{
    public static class Converter
    {
        // precompile our regex so its fast
        private static Regex deserialize = new Regex(@"{""c"":""(.*)"",""d"":(.*)}", RegexOptions.IgnoreCase | RegexOptions.Compiled);

        /// <summary>
        /// Serializes an ArmaReturn into a string
        /// </summary>
        /// <param name="returnCommand"></param>
        /// <returns></returns>
        public static string Serialize(ArmAReturn returnCommand)
        {
            string response = returnCommand.Response ? "true" : "false";
            string returns = "";
            foreach (string param in returnCommand.Returned)
            {
                if (param == "[")
                    returns += $"{param}";
                else if (param == "]")
                    returns += $"{param},";
                else
                    returns += $"\"{param}\",";
            }
            if (returns != "")
                returns = returns.Substring(0, returns.Length - 1);

            return $"[{response},[{returns}]]";
        }


        /// <summary>
        /// Converts a string into an ArmACommand
        /// </summary>
        /// <param name="inputCommand"></param>
        /// <returns></returns>
        public static ArmACommand Deserialize(string inputCommand)
        {
            ArmACommand command = new ArmACommand();

            // Match the string
            Match match = deserialize.Match(inputCommand);
            if (match.Success)
            {
                // Get the function
                command.Function = match.Groups[1].Value;

                // Get our params
                string param = match.Groups[2].Value.Replace("[", "").Replace("]", "").Replace("\"", "");

                // If we actually have params, split them on the comma
                if (param != "")
                    command.Parameters = param.Split(',').ToList();
            }
            else
            {
                command.Function = "Failed";
                command.Parameters.Add($"Failed to parse input string: {inputCommand}");
            }

            return command;
        }
    }
}
All opinions represented herein are my own
- © 2024 itsthedevman
- build 3c15a1b