/// <summary> /// 保存 /// </summary> public void Save() { SaveFileDialog dialog = new SaveFileDialog(); dialog.Title = "保存生成的脚本"; dialog.DefaultExt = ".cs"; dialog.Filter = "C#文件|*.cs"; dialog.AddExtension = true; dialog.FileName = CurrentOption + ".cs"; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var path = dialog.FileName; var sourcepath = Path.Combine(OutputPath, CurrentOption + ".cs"); File.Copy(sourcepath, path, true); var finfo = new FileInfo(path); var dpath = finfo.Directory.FullName; var oppath = Path.Combine(dpath, CurrentOption.Replace("Message", "Opcode") + ".cs"); File.WriteAllText(oppath, OpcodeText.Text); System.Diagnostics.Process.Start("Explorer.exe", dpath); Log("保存生成脚本完成"); } }
/// <summary> /// 生成Opcode /// </summary> public void GenerateOpcode() { var Index = 0; var NameSpace = "Secode.Network"; var OpcodeClass = CurrentOption.Replace("Message", "Opcode"); var lines = File.ReadAllLines(Path.Combine(SourcesPath, CurrentOption + ".proto")); #region Get Index foreach (var line in lines) { var temp = line.Replace("/", "").Replace(" ", ""); if (temp.Length > 0 && Regex.IsMatch(temp, @"^\d*$")) { Index = int.Parse(temp); break; } } #endregion #region Get NameSpace var NameSpaceRegex = new Regex("package (.*?);"); foreach (var line in lines) { MatchCollection mc = NameSpaceRegex.Matches(line); if (mc != null && mc.Count > 0) { NameSpace = mc[0].Groups[1].Value; break; } } #endregion #region Append Message Start OpcodeText.Text = ""; OpcodeText.AppendText("namespace " + NameSpace); OpcodeText.AppendText(Environment.NewLine); OpcodeText.AppendText("{"); OpcodeText.AppendText(Environment.NewLine); #endregion #region Append Message Content var MessageRegex = new Regex(@"^message\s+(.*?)\s*//\s*(.*?)$"); var MessageRegex2 = new Regex(@"^message\s+(.*?)\s*$"); foreach (var line in lines) { MatchCollection mc = MessageRegex.Matches(line); if (mc == null || mc.Count == 0) { mc = MessageRegex2.Matches(line); } if (mc == null || mc.Count == 0) { continue; } var msgclass = mc[0].Groups[1].Value; var typeclass = mc[0].Groups[2].Value; OpcodeText.AppendText(" [Message(" + OpcodeClass + "." + msgclass + ")]"); OpcodeText.AppendText(Environment.NewLine); if (!string.IsNullOrWhiteSpace(typeclass)) { OpcodeText.AppendText(" public partial class " + msgclass + " : " + typeclass + " { }"); } else { OpcodeText.AppendText(" public partial class " + msgclass + " { }"); } OpcodeText.AppendText(Environment.NewLine); OpcodeText.AppendText(Environment.NewLine); } #endregion #region Append Message End OpcodeText.AppendText("}"); OpcodeText.AppendText(Environment.NewLine); #endregion #region Append Opcode Start OpcodeText.AppendText("namespace " + NameSpace); OpcodeText.AppendText(Environment.NewLine); OpcodeText.AppendText("{"); OpcodeText.AppendText(Environment.NewLine); OpcodeText.AppendText(" public static partial class " + OpcodeClass); OpcodeText.AppendText(Environment.NewLine); OpcodeText.AppendText(" {"); OpcodeText.AppendText(Environment.NewLine); #endregion #region Append Opcode Content foreach (var line in lines) { MatchCollection mc = MessageRegex.Matches(line); if (mc == null || mc.Count == 0) { mc = MessageRegex2.Matches(line); } if (mc == null || mc.Count == 0) { continue; } var msgclass = mc[0].Groups[1].Value; Index += 1; OpcodeText.AppendText(" public const ushort " + msgclass + " = " + Index + ";"); OpcodeText.AppendText(Environment.NewLine); } #endregion #region Append Opcode End OpcodeText.AppendText(" }"); OpcodeText.AppendText(Environment.NewLine); OpcodeText.AppendText("}"); OpcodeText.AppendText(Environment.NewLine); #endregion }