Пример #1
0
        /// <summary>
        /// AddVariable
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="reference"></param>
        /// <returns>error string.</returns>
        public (VarDefine, bool, string) AddVariable(string name,
                                                     VarDefine.EType type = VarDefine.EType.Undecided, string reference = null)
        {
            if (GetVariable(name) != null)
            {
                return(null, false, "duplicate variable name");
            }

            VarDefine var = new VarDefine(this, name)
            {
                Type  = type,
                Value = reference,
                GridColumnValueWidth = 50,
            };

            bool create = false;

            switch (type)
            {
            case VarDefine.EType.List:
                if (string.IsNullOrEmpty(reference))
                {
                    var.Value = var.FullName();
                    if (_BeanDefines.TryGetValue(var.Name, out var exist))
                    {
                        if (false == exist.NamespaceOnly)
                        {
                            return(null, false, $"尝试为变量'{var.Name}'创建一个 BeanDefine 失败:已经存在。");
                        }
                    }
                    else
                    {
                        exist = new BeanDefine(Document, var.Name, this);
                        _BeanDefines.Add(var.Name, exist);
                    }
                    var.Reference = exist;
                    exist.AddReferenceFrom(var, ReferenceFrom.Reasons.List);
                    create = true;
                }
                else
                {
                    var r = FormMain.Instance.Documents.SearchReference(var.Value);
                    if (null == r)
                    {
                        return(null, false, "list reference bean not found.");
                    }
                    var.Reference = r;
                    r.AddReferenceFrom(var, ReferenceFrom.Reasons.List);
                }
                break;

            case VarDefine.EType.Enum:
                _EnumDefines.Add(var.Name, new EnumDefine(this, var.Name));
                break;
            }

            _Variables.Add(var);
            var.CreateXmlElementIfNeed(); // 调整变量顺序的时候需要这个创建好。
            Document.IsChanged = true;
            return(var, create, "");
        }
Пример #2
0
        private void Build()
        {
            try
            {
                FormMain.Instance.SaveAll();
                FormMain.Instance.Documents.LoadAllDocument(this);

                if (FormMain.Instance.FormError.GetErrorCount() > 0)
                {
                    FormMain.Instance.InvokeShowFormError();
                    this.AppendLine("当前打开的文档存在一些验证错误。停止Build。", Color.Red);
                    return;
                }

                // verify
                FormMain.Instance.Documents.ForEachFile((Documents.File file) =>
                {
                    var doc = file.Document;
                    if (doc.GridData != null)
                    {
                        return(Running); // 已经打开的文档,有即时验证。
                    }
                    this.AppendLine($"Verify {doc.RelateName}", Color.Black);
                    int ErrorCount = 0;
                    FormMain.Instance.FormError.OnAddError = (GridData.Cell cell, Property.IProperty p, Property.ErrorLevel level, string desc) =>
                    {
                        if (cell.Row.GridData == doc.GridData)
                        {
                            ++ErrorCount;
                        }
                    };
                    doc.BuildGridData();
                    doc.GridData.VerifyAll(false);
                    if (ErrorCount > 0)
                    {
                        FormMain.Instance.InvokeOpenGrid(doc, false);
                    }
                    else
                    {
                        // 等 FormError 处理了有 GridData 但是没有 View 的情况,可以不清除错误。
                        // FormMain.Instance.FormError.RemoveErrorByGrid(doc.GridData);
                    }
                    return(Running);
                });
                FormMain.Instance.FormError.OnAddError = null;

                if (false == Running)
                {
                    return;
                }

                // 输出服务器使用的配置数据。现在是xml格式。
                string serverDir = System.IO.Path.Combine(FormMain.Instance.ConfigProject.DataOutputDirectory, "Server");
                FormMain.Instance.Documents.ForEachFile((Documents.File file) =>
                {
                    this.AppendLine($"导出服务器配置. {file.Document.RelateName}", Color.Black);
                    string serverDocDir = System.IO.Path.Combine(serverDir, file.Parent.RelateName);
                    System.IO.Directory.CreateDirectory(serverDocDir);
                    string serverFileName = System.IO.Path.Combine(serverDocDir, file.Document.Name + ".xml");
                    file.Document.SaveAs(serverFileName, true, Property.DataOutputFlags.Server);
                    return(Running);
                });

                if (false == Running)
                {
                    return;
                }
                // check VarDefine.Default
                VarDefine hasDefaultError = null;
                FormMain.Instance.Documents.ForEachFile((Documents.File file) =>
                {
                    return(file.Document.BeanDefine.ForEach((BeanDefine beanDefine) =>
                    {
                        foreach (var varDefine in beanDefine.Variables)
                        {
                            if (false == varDefine.CheckType(varDefine.TypeNow, varDefine.Default))
                            {
                                hasDefaultError = varDefine;
                                return false;
                            }
                        }
                        return Running;
                    }));
                });
                if (hasDefaultError != null)
                {
                    this.AppendLine(hasDefaultError.FullName() + " 默认值和类型不匹配。", Color.Red);
                    return;
                }
                if (false == Running)
                {
                    return;
                }

                Gen.cs.Main.Gen(FormMain.Instance, Property.DataOutputFlags.Server, this);

                if (false == Running)
                {
                    return;
                }

                switch (string.IsNullOrEmpty(FormMain.Instance.ConfigProject.ClientLanguage)
                    ? "cs" : FormMain.Instance.ConfigProject.ClientLanguage)
                {
                case "cs":
                    Gen.cs.Main.Gen(FormMain.Instance, Property.DataOutputFlags.Client, this);
                    // 输出客户端使用的配置数据。xml格式。
                    string clientDir = System.IO.Path.Combine(FormMain.Instance.ConfigProject.DataOutputDirectory, "Client");
                    FormMain.Instance.Documents.ForEachFile((Documents.File file) =>
                    {
                        this.AppendLine($"导出cs客户端数据. {file.Document.RelateName}", Color.Black);
                        string clientDocDir = System.IO.Path.Combine(clientDir, file.Parent.RelateName);
                        System.IO.Directory.CreateDirectory(clientDocDir);
                        string clientFileName = System.IO.Path.Combine(clientDocDir, file.Document.Name + ".xml");
                        file.Document.SaveAs(clientFileName, true, Property.DataOutputFlags.Client);
                        return(Running);
                    });

                    break;

                case "ts":
                    // 生成代码,数据也嵌入在代码中。
                    Gen.ts.Main.Gen(FormMain.Instance, Property.DataOutputFlags.Client, this);
                    break;

                case "lua":
                    // 生成代码,数据也嵌入在代码中。
                    Gen.lua.Main.Gen(FormMain.Instance, Property.DataOutputFlags.Client, this);
                    break;

                default:
                    this.AppendLine("unkown client language: " + FormMain.Instance.ConfigProject.ClientLanguage, Color.Red);
                    break;
                }
            }
            catch (Exception ex)
            {
                this.AppendLine(ex.ToString(), Color.Red);
            }
            finally
            {
                WaitBuildExit.Set();
            }
        }