static void ProcessExtract()
        {
            var version    = "0";
            var script_dir = Path.Combine(Directory.GetCurrentDirectory(), "script");
            var em         = new extract_model();

            em.Version = version;
            em.Scripts = new List <Tuple <string, SRCALAttribute, string, string> >();
            foreach (var file in Directory.GetFiles(script_dir))
            {
                string raw = File.ReadAllText(file);

                var raw_script = raw.Split(
                    new[] { Environment.NewLine },
                    StringSplitOptions.None
                    ).ToList();
                var parser = new SRCALParser();
                parser.Parse(raw_script);

                var attribute = new SRCALAttribute();
                attribute.ScriptName        = parser.attributes["$ScriptName"];
                attribute.ScriptVersion     = parser.attributes["$ScriptVersion"];
                attribute.ScriptAuthor      = parser.attributes["$ScriptAuthor"];
                attribute.ScriptFolderName  = parser.attributes["$ScriptFolderName"];
                attribute.ScriptRequestName = parser.attributes["$ScriptRequestName"];
                attribute.URLSpecifier      = parser.attributes["$URLSpecifier"];
                attribute.UsingDriver       = Convert.ToInt32(parser.attributes["$UsingDriver"]) == 0 ? false : true;

                em.Scripts.Add(Tuple.Create(Path.GetFileName(file), attribute, raw.ToBase64(), Hash.GetFileHash(file)));
            }

            string json = JsonConvert.SerializeObject(em, Formatting.Indented);

            using (var fs = new StreamWriter(new FileStream("scripts.json", FileMode.Create, FileAccess.Write)))
            {
                fs.Write(json);
            }
        }
Esempio n. 2
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var tag = (sender as Button).Tag.ToString();

            DebugMonitor.Text = "";
            var raw_script = textEditor.Text.Split(
                new[] { '\n' },
                StringSplitOptions.None
                ).ToList();

            if (tag == "Open")
            {
                var ofd = new OpenFileDialog();
                ofd.InitialDirectory = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "script");
                ofd.Filter           = "SRCAL 스크립트 파일 (*.srcal)|*.srcal";
                if (ofd.ShowDialog() == true)
                {
                    textEditor.Text = File.ReadAllText(ofd.FileName);
                }
            }
            else if (tag == "Save")
            {
                string default_filename = "";
                try
                {
                    var parser = new SRCALParser();
                    script           = parser.Parse(raw_script);
                    default_filename = parser.attributes["$ScriptName"];
                } catch { }
                var sfd = new SaveFileDialog();
                sfd.InitialDirectory = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "script");
                sfd.FileName         = default_filename;
                sfd.Filter           = "SRCAL 스크립트 파일 (*.srcal)|*.srcal";
                if (sfd.ShowDialog() == true)
                {
                    File.WriteAllText(sfd.FileName, string.Join("\r\n", raw_script));
                }
            }
            else if (tag == "Parse")
            {
                bool err    = false;
                var  parser = new SRCALParser();
                try
                {
                    script = parser.Parse(raw_script);

                    var attribute = new SRCALAttribute();
                    attribute.ScriptName        = parser.attributes["$ScriptName"];
                    attribute.ScriptVersion     = parser.attributes["$ScriptVersion"];
                    attribute.ScriptAuthor      = parser.attributes["$ScriptAuthor"];
                    attribute.ScriptFolderName  = parser.attributes["$ScriptFolderName"];
                    attribute.ScriptRequestName = parser.attributes["$ScriptRequestName"];
                    attribute.URLSpecifier      = parser.attributes["$URLSpecifier"];
                    int v;
                    if (int.TryParse(parser.attributes["$UsingDriver"], out v))
                    {
                        attribute.UsingDriver = v == 0 ? false : true;
                    }
                    else
                    {
                        err = true;
                        DebugMonitor.Text = "Using driver must be integer type.\r\n";
                    }
                    DebugMonitor.Text = Monitor.SerializeObject(attribute) + "\r\n";
                }
                catch (Exception ex)
                {
                    DebugMonitor.Text += $"Script parsing error. {ex.Message}\r\n{ex.StackTrace}\r\n";
                    err = true;
                }

                if (parser.errors.Count > 0)
                {
                    DebugMonitor.Text += $"Occurred some errors when parsing script ...\r\n";
                    for (int i = 0; i < parser.errors.Count; i++)
                    {
                        DebugMonitor.Text += $"[{parser.errors[i].Item1.Line + 1}, {parser.errors[i].Item1.Column + 1}] {parser.errors[i].Item2}\r\n";
                    }
                    err = true;
                }

                if (!err)
                {
                    DebugMonitor.Text += "Complete parsing.";
                }
                else
                {
                    DebugMonitor.Text += "Error occured when parse script.";
                }
                DebugMonitor.ScrollToEnd();
            }
            else if (tag == "Inject")
            {
                try
                {
                    if (!ScriptManager.Instance.Subscribe(string.Join("\r\n", raw_script)))
                    {
                        MessageBox.Show("인젝션에 성공했습니다!\r\n메인창에서 스크립트를 실행하고, 콘솔에서 상태를 점검하세요.\r\n인젝션을 재시도하기 전에 반드시 이젝트해야합니다.", Title, MessageBoxButton.OK, MessageBoxImage.Information);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Monitor.Instance.Push($"[Script Editor] Fail to inject. {ex.Message}\r\n{ex.StackTrace}");
                }
                MessageBox.Show("인젝션에 실패했습니다. 자세한 내용은 콘솔을 참고해주세요.", Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (tag == "Eject")
            {
                var parser = new SRCALParser();
                try
                {
                    script = parser.Parse(raw_script);
                    if (ScriptManager.Instance.Unsubscribe(parser.attributes["$ScriptName"]) >= 1)
                    {
                        MessageBox.Show("이젝션 완료!", Title, MessageBoxButton.OK, MessageBoxImage.Information);
                        return;
                    }
                    else
                    {
                        MessageBox.Show("이젝션할 내용이 없습니다.", Title, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    }
                }
                catch (Exception ex)
                {
                    Monitor.Instance.Push($"[Script Editor] Fail to eject. {ex.Message}\r\n{ex.StackTrace}");
                    MessageBox.Show("이젝션을 실패했습니다. 자세한 내용은 콘솔을 참고해주세요.", Title, MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else if (tag == "CC")
            {
                (new CustomCrawler()).Show();
            }
            else if (tag == "Help")
            {
                Process.Start("https://github.com/dc-koromo/koromo-copy/blob/master/Document/SRCAL.md");
            }
        }