예제 #1
0
        static void Translate(FileInfo inFile, String outPath, XmlDictionary dict)
        {
            if (!File.Exists(inFile.FullName))
            {
                throw new FileNotFoundException(inFile.FullName);
            }

            String _outFile = outPath + "\\" + inFile.Name;

            if (!Directory.Exists(outPath))
            {
                Directory.CreateDirectory(outPath);
            }

            IPackage _package = Package.OpenPackage(-1, inFile.FullName);

            Byte _targetCode     = 0;
            var  _stblIndexEntry = new List <KeyValuePair <IResourceIndexEntry, IResourceIndexEntry> >();

            {
                List <IResourceIndexEntry> _resourceEntry = _package.GetResourceList;
                var _stblRes = _resourceEntry.Where((o) => { return(o.ResourceType == 0x220557DA); });

                Byte _sourceCode = dict.To == TranslateTo.B ? dict.LanguageA : dict.LanguageB;
                IEnumerable <IResourceIndexEntry> _stblScoure = _stblRes.Where((o) => { return(GetLanguageCode(o.Instance) == _sourceCode); });

                _targetCode = dict.To == TranslateTo.A ? dict.LanguageA : dict.LanguageB;
                IEnumerable <IResourceIndexEntry> _stblTarget = _stblRes.Where((o) => { return(GetLanguageCode(o.Instance) == _targetCode); });

                foreach (var s in _stblScoure)
                {
                    Boolean hasTarget = false;
                    UInt64  _is       = s.Instance << (4 * 2);
                    foreach (var t in _stblTarget)
                    {
                        UInt64 _it = t.Instance << (4 * 2);
                        if (s.ResourceType == t.ResourceType &&
                            s.ResourceGroup == t.ResourceGroup &&
                            _is == _it)
                        {
                            hasTarget = true;
                            _stblIndexEntry.Add(new KeyValuePair <IResourceIndexEntry, IResourceIndexEntry>(s, t));
                            break;
                        }
                    }

                    if (!hasTarget)
                    {
                        _stblIndexEntry.Add(new KeyValuePair <IResourceIndexEntry, IResourceIndexEntry>(s, null));
                    }
                }
            }

            if (_stblIndexEntry.Count() == 0)
            {
                _package.SaveAs(_outFile);
                throw new ApplicationException($"源语言不存在,文件未修改。文件:{_outFile}");
            }
            else
            {
                var _newDict = new XmlDictionary(dict.LanguageA, dict.LanguageB, dict.To);

                Int32 _changedCount = 0;
                foreach (var item in _stblIndexEntry)
                {
                    IResource res = WrapperDealer.GetResource(-1, _package, item.Key, false);

                    Stream             _fs         = res.Stream;
                    List <StringEntry> _stringList = ReadStbl(inFile.Name, _fs, item.Key);

                    foreach (var str in _stringList)
                    {
                        if (dict.TryFind(str.Text, inFile.Name, str.Ident, out String s))
                        {
                            str.Text = s;
                            _changedCount++;
                        }
                        else
                        {
                            Entry _entry = new Entry(str.Text, str.Text, str.Ident, inFile.Name);
                            _newDict.Add(_entry);
                        }
                    }

                    var _ms = new MemoryStream();
                    StringListToStbl(_ms, _stringList);

                    if (item.Value == null)
                    {
                        var _rk = new TranslateResourceIndexEntry();
                        _rk.ResourceGroup = item.Key.ResourceGroup;
                        _rk.ResourceType  = item.Key.ResourceType;
                        _rk.Instance      = item.Key.Instance;
                        _rk.Instance      = _rk.Instance << (4 * 2);
                        _rk.Instance      = _rk.Instance >> (4 * 2);
                        _rk.Instance      = _rk.Instance + ((( UInt64 )_targetCode) << (4 * 14));

                        _package.AddResource(_rk, _ms, true);
                    }
                    else
                    {
                        var _st = new StblResource.StblResource(0, _ms);
                        _package.ReplaceResource(item.Value, _st);
                    }

                    _package.SaveAs(_outFile);
                }

                if (_newDict.Count != 0)
                {
                    throw new EntryNotFoundException(_newDict, _changedCount);
                }
            }
        }
예제 #2
0
        public static XmlDictionary FromFile(String xmlDictionaryPath, TranslateTo to)
        {
            XmlDictionary xmlDictionary;
            var           _xe = XDocument.Load(xmlDictionaryPath)?.Element(DICTIONARY) ?? null;

            if (_xe == null)
            {
                throw new ApplicationException("Xml读取失败。");
            }

            String _as = _xe.Attribute(A)?.Value ?? String.Empty;
            Byte   _a  = Convert.ToByte(_as, 16);
            String _bs = _xe.Attribute(B)?.Value ?? String.Empty;
            Byte   _b  = Convert.ToByte(_bs, 16);

            if (String.IsNullOrEmpty(_as) || String.IsNullOrEmpty(_bs))
            {
                throw new ApplicationException("语言代码不能为空。");
            }

            xmlDictionary = new XmlDictionary(_a, _b, to);
            var _enumerable = _xe.Elements(ENTRY);

            foreach (var item in _enumerable)
            {
                String _ia = item.Attribute(A)?.Value ?? String.Empty;
                String _ib = item.Attribute(B)?.Value ?? String.Empty;

                if (String.IsNullOrEmpty(to == TranslateTo.A ? _ib : _ia))
                {
                    continue;
                }

                StringEntryIdent _ident   = null;
                String           _package = null;
                String           _ns      = item.Attribute(NAMESPACE)?.Value ?? String.Empty;
                if (!String.IsNullOrEmpty(_ns))
                {
                    String[] _sp = _ns.Split('|');
                    if (_sp.Length >= 1)
                    {
                        if (_sp[0].ToLower().IndexOf(".package") != -1)
                        {
                            _package = _sp[0];

                            if (_sp.Length == 2)
                            {
                                try
                                {
                                    _ident = _sp[1].ToStringEntryIdent();
                                }
                                catch (Exception)
                                {
                                    _ident = null;
                                }
                            }
                        }
                    }
                }

                Entry _entry = new Entry(_ia, _ib, _ident, _package);
                xmlDictionary.Add(_entry);
            }

            return(xmlDictionary);
        }
예제 #3
0
        static void Main(String[] args)
        {
            if (args.Length < 4)
            {
                throw new ArgumentException("参数错误。参数格式应为:[-a2b|-b2a] [dictionary path] [input path] [output path]");
            }

            TranslateTo _to       = args[0].Equals("-a2b", StringComparison.CurrentCultureIgnoreCase) ? TranslateTo.B : TranslateTo.A;
            String      _dictPath = Path.GetFullPath(args[1]);
            var         _xmlDict  = XmlDictionary.FromFile(_dictPath, _to);

            String _inputPath = Path.GetFullPath(args[2]);

            var _in_file_paths = new List <FileInfo>();

            ListFiles(new DirectoryInfo(_inputPath), _in_file_paths);

            String _outputPath = Path.GetFullPath(args[3]);

            var   _log          = new List <String>();
            var   _outDict      = new XmlDictionary(_xmlDict.LanguageA, _xmlDict.LanguageB, _xmlDict.To);
            Int32 _changedCount = 0;

            foreach (var path in _in_file_paths)
            {
                //String _outPath = path.DirectoryName.Replace( _inputPath, _outputPath );
                //Translate( path, _outPath, _xmlDict );

                try
                {
                    String _outPath = path.DirectoryName.Replace(_inputPath, _outputPath);
                    Translate(path, _outPath, _xmlDict);
                }
                catch (EntryNotFoundException ex)
                {
                    _outDict.AddRange(ex.Dict);
                    _changedCount += ex.ChangedCount;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    _log.Add(ex.Message);
                }
            }

            if (_log.Count != 0)
            {
                StreamWriter log = new StreamWriter(_outputPath + "\\TS4TSLog.txt", true);

                log.WriteLine("时间:" + System.DateTime.Now.ToLongTimeString());

                log.WriteLine("已汉化条目:" + _changedCount);
                log.WriteLine("未汉化条目:" + _outDict.Count);

                for (Int32 i = 0; i < _log.Count; i++)
                {
                    log.WriteLine(_log[i]);
                }

                log.Close();
            }

            if (_outDict.Count != 0)
            {
                _outDict.SaveAs(_outputPath + "\\XMLDictionary.xml");
            }

            Console.WriteLine("按任意键退出...");
            Console.ReadLine();
        }
 public EntryNotFoundException(XmlDictionary dict, Int32 changed)
 {
     m_dict    = dict;
     m_changed = changed;
 }