示例#1
0
        /// <summary>
        /// Import a Mod from cli arguments
        /// </summary>
        /// <param name="args"></param>
        private void ImportFromArgs(ReadOnlyCollection <string> args)
        {
            //There is no argument to attempt to import
            if (args.Count <= 1)
            {
                return;
            }

            var factorioBase64 = Regex.Match(args[1], @"factoriomods:\/\/(.*)");

            if (factorioBase64.Groups.Count <= 1)
            {
                return;
            }

            var base64String = factorioBase64.Groups[1].ToString().TrimEnd('/');

            byte[] jsonBytes;

            //If jsonstring is invalid base64 then stop import
            try
            {
                jsonBytes = Convert.FromBase64String(base64String);
            }
            catch (Exception)
            {
                return;
            }

            var     jsonString = System.Text.Encoding.Default.GetString(jsonBytes);
            JObject json;

            try
            {
                json = JsonConvert.DeserializeObject <JObject>(jsonString);
            }
            catch (Exception)
            {
                return;
            }
            var mod = new Mod(json);

            if (Mods.FindModInAvailable(mod.Name, mod.Version) != null)
            {
                MainForm.ShowMessage($"{mod.Title??mod.Name} has already been added");
                return;
            }

            if (!mod.HaveFiles)
            {
                MainForm.ShowForm(new ImportModForm(mod));
            }
        }