private void LoadDynamicXml(string path, bool debugVersion = false) { var tlkFile = new TlkFile(); var tlkStrings = new List <TlkString>(); using (var stream = new FileStream(path, FileMode.Open)) { dynamic tlkXml = DynamicXml.Load(stream); tlkXml.TlkFile(DynamicXml.GetElement(tlkfile => { if (tlkfile["id"] != null) { tlkFile.Id = tlkfile["id"]; } if (tlkfile["name"] != null) { tlkFile.Name = tlkfile["name"]; } if (tlkfile["source"] != null) { tlkFile.Source = tlkfile["source"]; } if (tlkfile.Includes != null) { tlkfile.Includes(DynamicXml.GetElement(includes => { var order = -1; foreach (var include in includes.Include) { if (include["order"] != null) { order = Convert.ToInt32(include["order"]); } var source = include["source"]; if (order >= 0) { tlkFile.Includes.Insert(order, source); } else { tlkFile.Includes.Add(source); } } })); } if (tlkfile.Strings != null) { tlkfile.Strings(DynamicXml.GetElement(strings => { if (strings.String == null) { return; } var i = 0; foreach (var str in strings.String) { var strId = InvalidIdRegex.Replace(str["id"], ""); var id = Convert.ToInt32(strId); var value = str.Value.Replace("\r\n", "\n"); var alwaysAdd = false; if (str["alwaysAdd"] != null) { alwaysAdd = Convert.ToBoolean(str["alwaysAdd"]); } if (id >= 0) { value += '\0'; } if (id >= 0 && debugVersion && (id & 0x8000000) != 0x8000000) { value = "(#" + id + ") " + value; } tlkStrings.Add(new TlkString(id, value, i++)); } })); } })); } var index = 0; foreach (var include in tlkFile.Includes) { var includePath = Path.GetDirectoryName(path); if (includePath == null) { continue; } includePath = Path.Combine(includePath, include); if (!File.Exists(includePath)) { continue; } using (var stream = new FileStream(includePath, FileMode.Open)) { dynamic tlkXml = DynamicXml.Load(stream); tlkXml.TlkFile(DynamicXml.GetElement(tlkfile => { if (tlkfile.Strings == null) { return; } tlkfile.Strings(DynamicXml.GetElement(strings => { if (strings.String == null) { return; } foreach (var str in strings.String) { var strId = InvalidIdRegex.Replace(str["id"], ""); var id = Convert.ToInt32(strId); var value = str.Value.Replace("\r\n", "\n"); var alwaysAdd = false; if (str["alwaysAdd"] != null) { alwaysAdd = Convert.ToBoolean(str["alwaysAdd"]); } if (id >= 0) { value += '\0'; } if (id >= 0 && debugVersion && (id & 0x8000000) != 0x8000000) { value = "(#" + id + ") " + value; } /*if (alwaysAdd || _inputData.Count(s => s.Id == id) == 0) * { * _inputData.Add(new TlkString(id, value, index++)); * } * else * { * var strIndex = _inputData.FindIndex(s => s.Id == id); * * if (strIndex >= 0) * { * _inputData[strIndex] = value; * } * }*/ _maleStrings.Add(new TlkString(id, value, index++)); } })); })); } } foreach (var tlkString in tlkStrings) { _maleStrings.Add(new TlkString(tlkString.Id, tlkString.Value, tlkString.Position + index++)); } }
private void SaveToDynamicXml(string path) { // To reduce size of XML files const int maxEntries = 10000; var tlkFile = new TlkFile { Id = GetId(Path.GetFileNameWithoutExtension(_path)), Name = Path.GetFileNameWithoutExtension(_path), Source = Path.GetFileName(_path) }; dynamic tlkXml = new DynamicXml(); var maleIncludeCount = MaleStringRefs.Count / maxEntries; var femaleIncludeCount = FemaleStringRefs.Count / maxEntries; var includeCount = Math.Max(maleIncludeCount, femaleIncludeCount); if ((MaleStringRefs.Count % maxEntries > 0) || (FemaleStringRefs.Count % maxEntries > 0)) { includeCount++; } MaleStringRefs.Sort((s1, s2) => (s1.Id & Int32.MaxValue).CompareTo(s2.Id & Int32.MaxValue)); FemaleStringRefs.Sort((s1, s2) => (s1.Id & Int32.MaxValue).CompareTo(s2.Id & Int32.MaxValue)); /*for (var i = 0; i < MaleStringRefs.Count; i++) * { * var s = MaleStringRefs[i]; * s.Position = i; * * tlkFile.MaleStrings.Add(s); * }*/ /*for (var i = 0; i < FemaleStringRefs.Count; i++) * { * var s = FemaleStringRefs[i]; * s.Position = i; * * tlkFile.FemaleStrings.Add(s); * }*/ tlkXml.TlkFile(DynamicXml.CreateElement(tlkfile => { tlkfile["id"] = tlkFile.Id; tlkfile["name"] = tlkFile.Name; tlkfile["source"] = tlkFile.Source; tlkfile.Includes(DynamicXml.CreateElement(includes => { for (var i = 0; i < includeCount; i++) { var item = string.Format("{0}/{0}{1}.xml", tlkFile.Name, i); includes.Include(DynamicXml.CreateElement(include => { include["source"] = item; })); tlkFile.Includes.Add(item); } })); })); tlkXml.Save(path); var index = 0; foreach (var include in tlkFile.Includes) { dynamic includeXml = new DynamicXml(); var include1 = include; includeXml.TlkFile(DynamicXml.CreateElement(tlkfile => { tlkfile["name"] = include1; tlkfile.MaleStrings(DynamicXml.CreateElement(strings => { var count = MaleStringRefs.Count; for (var x = 0; x < maxEntries && index < count; x++, index++) { var s = MaleStringRefs[index]; strings.String(DynamicXml.CreateElement(str => { str["id"] = s.Id; str.Value = s.Value; })); } })); tlkfile.FemaleStrings(DynamicXml.CreateElement(strings => { var count = FemaleStringRefs.Count; for (var x = 0; x < maxEntries && index < count; x++, index++) { var s = FemaleStringRefs[index]; strings.String(DynamicXml.CreateElement(str => { str["id"] = s.Id; str.Value = s.Value; })); } })); })); var destPath = Path.GetDirectoryName(path); if (destPath != null) { destPath = Path.Combine(destPath, include1); if (!Directory.Exists(Path.GetDirectoryName(destPath))) { Directory.CreateDirectory(Path.GetDirectoryName(destPath)); } includeXml.Save(destPath); } } }