/// <summary>
        /// Replace module binary in dat file.
        /// </summary>
        /// <param name="dataPath">Dat file path.</param>
        /// <param name="binStream">Binary file stream.</param>
        /// <param name="module">Module name defined in ModuleDataName class.</param>
        public static void ReplaceBinaryFile(string dataPath, MemoryStream binStream, string module)
        {
            using (LangDataFile dataFile = new LangDataFile())
            {
                string dataBakPath = Helper.NeutralFormat("{0}.bak", dataPath);
                File.Delete(dataBakPath);
                File.Move(dataPath, dataBakPath);
                dataFile.Load(dataBakPath);

                // get guid of module
                string guidStr = GetReservedGuid(module);
                Guid guid = new Guid(guidStr);

                // replace binary file
                LangDataObject tnObject = dataFile.GetDataObject(guid);
                tnObject.Data = binStream.ToArray();
                dataFile.AddDataObject(tnObject, true);

                SortLangData(dataFile);

                dataFile.Save(dataPath);
            }
        }
        /// <summary>
        /// Sort Langugae data objects by module data name.
        /// </summary>
        /// <param name="langData">Language data file.</param>
        private static void SortLangData(LangDataFile langData)
        {
            ArrayList sortedDataObjects = new ArrayList();
            foreach (var data in langData.DataObjects)
            {
                string moduleDataName = GetReservedDataName(data.Token.ToString());
                sortedDataObjects.Add(new KeyValuePair<string, LangDataObject>(moduleDataName, data));
            }

            sortedDataObjects.Sort(new CompareLangDataObject());

            foreach (KeyValuePair<string, LangDataObject> obj in sortedDataObjects)
            {
                langData.AddDataObject(obj.Value, true);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create language data file.
        /// </summary>
        /// <param name="fileName">Language data file name.</param>
        /// <param name="domain">Domain.</param>
        /// <returns>Errors.</returns>
        public ErrorSet CombineDataFile(string fileName, string domain)
        {
            if (string.IsNullOrEmpty(domain) || string.IsNullOrEmpty(domain.Trim()))
            {
                domain = DomainItem.GeneralDomain;
            }

            ErrorSet errorSet = new ErrorSet();

            if (domain.Equals(DomainItem.GeneralDomain, StringComparison.OrdinalIgnoreCase))
            {
                errorSet = EnsureNecessaryData(this._moduleDataSet);
            }
            else if (this._moduleDataSet.Count == 0)
            {
                errorSet.Add(new Error(DataCompilerError.DomainDataMissing, domain));
            }

            if (!errorSet.Contains(ErrorSeverity.MustFix))
            {
                using (LangDataFile langDataFile = new LangDataFile())
                {
                    // Set file property
                    FileProperty fileProperty = new FileProperty();
                    fileProperty.Version = 1;
                    fileProperty.Build = 0;
                    fileProperty.LangID = (uint)_language;
                    langDataFile.FileProperties = fileProperty;

                    ArrayList sortedDataObjects = new ArrayList();
                    foreach (KeyValuePair<string, LangDataObject> obj in _moduleDataSet)
                    {
                        sortedDataObjects.Add(obj);
                    }

                    sortedDataObjects.Sort(new CompareLangDataObject());

                    // Set data objects
                    foreach (KeyValuePair<string, LangDataObject> obj in sortedDataObjects)
                    {
                        if (obj.Value.Data == null)
                        {
                            continue;
                        }

                        langDataFile.AddDataObject(obj.Value);
                        string message = Helper.NeutralFormat("Added {{{0}}} ({1}) data.",
                            obj.Value.Token.ToString(), obj.Key);
                        errorSet.Add(new Error(DataCompilerError.CompilingLog, message));
                    }

                    // Save as binary file
                    Helper.EnsureFolderExistForFile(fileName);
                    langDataFile.Save(fileName);
                }
            }

            return errorSet;
        }