Advertisement
Guest User

D2IFILE.CS stump

a guest
Jul 13th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. using Stump.Core.IO;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.IO;
  6. using System.Linq;
  7. namespace Stump.DofusProtocol.Tools.D2i
  8. {
  9. public class D2IFile : IDisposable
  10. {
  11. //index stuff
  12. private readonly Dictionary<int, int> _indexes = new Dictionary<int, int>();
  13. private readonly Dictionary<int, int> _unDiacriticalIndexes = new Dictionary<int, int>();
  14. private readonly Dictionary<string, int> _textIndexes = new Dictionary<string, int>();
  15.  
  16. //sort ellements ??
  17. private readonly Dictionary<int, int> _textSortIndexes = new Dictionary<int, int>();
  18.  
  19. //caches
  20. private readonly Dictionary<int, string> _cache = new Dictionary<int, string>();
  21. private readonly Dictionary<int, string> _cacheUnDiacritical = new Dictionary<int, string>();
  22. private readonly Dictionary<string, string> _cacheTexts = new Dictionary<string, string>();
  23. private readonly Dictionary<int, string> _cachetextSortIndexes = new Dictionary<int, string>();
  24.  
  25. private readonly string _uri;
  26. private BigEndianReader _reader;
  27.  
  28. public string FilePath
  29. {
  30. get { return _uri; }
  31. }
  32.  
  33. public D2IFile(string uri)
  34. {
  35. _uri = uri;
  36. _reader = new BigEndianReader(File.OpenRead(_uri));
  37. Initialize();
  38. }
  39.  
  40. private void Initialize()
  41. {
  42. var indexesPointer = _reader.ReadInt();//index offset
  43. _reader.Seek(indexesPointer, SeekOrigin.Begin);
  44.  
  45. var indexesLength = _reader.ReadInt();
  46. int i = 0;
  47. while (i < indexesLength)//texts
  48. {
  49. var key = _reader.ReadInt();
  50. var dialectCriticalText = _reader.ReadBoolean();
  51. var pointer = _reader.ReadInt();
  52.  
  53. _indexes.Add(key, pointer);
  54.  
  55. if (dialectCriticalText)
  56. {
  57. i += 4;
  58. _unDiacriticalIndexes.Add(key, _reader.ReadInt());
  59. }
  60. else
  61. {
  62. _unDiacriticalIndexes.Add(key, pointer);
  63. }
  64. i += 9;
  65. }
  66.  
  67. indexesLength = _reader.ReadInt();
  68. while (indexesLength > 0)//ui texts
  69. {
  70. var position = _reader.Position;
  71. _textIndexes.Add(_reader.ReadUTF(), _reader.ReadInt());
  72. indexesLength = (indexesLength - ((int)_reader.Position - (int)position));
  73. }
  74.  
  75. indexesLength = _reader.ReadInt();
  76. i = 0;
  77. while (indexesLength > 0)//texts sorts
  78. {
  79. var position = _reader.Position;
  80. _textSortIndexes.Add(_reader.ReadInt(), i++);
  81. indexesLength = (indexesLength - ((int)_reader.Position - (int)position));
  82. }
  83.  
  84. foreach (var key in _indexes.Keys)
  85. {
  86. GetText(key);
  87. }
  88.  
  89. foreach (var textKey in _textIndexes.Keys)
  90. {
  91. GetText(textKey);
  92. }
  93. }
  94.  
  95. public int getOrderIndex(int index)
  96. {
  97. return _textSortIndexes[index];
  98. }
  99.  
  100. public string GetText(int key)
  101. {
  102. if (!_cache.ContainsKey(key))
  103. {
  104. if (_indexes.ContainsKey(key))
  105. {
  106. var pointer = _indexes[key];
  107. _reader.Seek(pointer, SeekOrigin.Begin);
  108. _cache.Add(key, _reader.ReadUTF());
  109. }
  110. else
  111. {
  112. _cache.Add(key, "{null}");
  113. }
  114. }
  115. return _cache[key];
  116. }
  117.  
  118. public string GetUnDiacriticalText(int key)
  119. {
  120. if (!_cacheUnDiacritical.ContainsKey(key))
  121. {
  122. if (_unDiacriticalIndexes.ContainsKey(key))
  123. {
  124. var pointer = _unDiacriticalIndexes[key];
  125. _reader.Seek(pointer, SeekOrigin.Begin);
  126. _cacheUnDiacritical.Add(key, _reader.ReadUTF());
  127. }
  128. else
  129. {
  130. _cacheUnDiacritical.Add(key, "{null}");
  131. }
  132. }
  133. return _cacheUnDiacritical[key];
  134. }
  135.  
  136. public string GetText(string key)
  137. {
  138. if (!_cacheTexts.ContainsKey(key))
  139. {
  140. if (_textIndexes.ContainsKey(key))
  141. {
  142. var pointer = _textIndexes[key];
  143. _reader.Seek(pointer, SeekOrigin.Begin);
  144. _cacheTexts.Add(key, _reader.ReadUTF());
  145. }
  146. else
  147. {
  148. _cacheTexts.Add(key, "{null}");
  149. }
  150. }
  151. return _cacheTexts[key];
  152. }
  153.  
  154. public void SetText(int key, string value)
  155. {
  156. if (_cache.ContainsKey(key))
  157. {
  158. _cache[key] = value;
  159. }
  160. }
  161.  
  162. public void SetText(string key, string value)
  163. {
  164. if (_cacheTexts.ContainsKey(key))
  165. {
  166. _cacheTexts[key] = value;
  167. }
  168. }
  169.  
  170. public bool DeleteText(int key)
  171. {
  172. return _cache.Remove(key);
  173. }
  174.  
  175. public bool DeleteText(string key)
  176. {
  177. return _cacheTexts.Remove(key);
  178. }
  179.  
  180. public IReadOnlyDictionary<int, string> GetAllText()
  181. {
  182. return new ReadOnlyDictionary<int, string>(_cache);
  183. }
  184.  
  185. public IReadOnlyDictionary<string, string> GetAllUiText()
  186. {
  187. return new ReadOnlyDictionary<string, string>(_cacheTexts);
  188. }
  189.  
  190. public int FindFreeId()
  191. {
  192. return _indexes.Keys.Max() + 1;
  193. }
  194.  
  195. public void Save()
  196. {
  197. Save(_uri);
  198. }
  199.  
  200. public void Save(string uri)
  201. {
  202. var writer = new BigEndianWriter(File.OpenWrite(uri));
  203. writer.Seek(4, SeekOrigin.Begin);
  204.  
  205. var newIndexes = new Dictionary<int, int>();
  206. var newTextIndexes = new Dictionary<string, int>();
  207. foreach (var oldIndex in _indexes)
  208. {
  209. newIndexes.Add(oldIndex.Key, (int)writer.Position);
  210. writer.WriteUTF(GetText(oldIndex.Key));
  211. }
  212. foreach (var oldIndex in _textIndexes)
  213. {
  214. newTextIndexes.Add(oldIndex.Key, (int)writer.Position);
  215. writer.WriteUTF(GetText(oldIndex.Key));
  216. }
  217.  
  218. int oldPos = (int)writer.Position;//end of the strings and start of the indexes
  219. writer.Seek(0, SeekOrigin.Begin);
  220. writer.WriteInt(oldPos);
  221. writer.Seek(oldPos, SeekOrigin.Begin);
  222.  
  223.  
  224. var subWriter = new BigEndianWriter();
  225. foreach (var newIndex in newIndexes)
  226. {
  227. var newUnDiacriticalIndex = -1;
  228. _unDiacriticalIndexes.TryGetValue(newIndex.Key, out newUnDiacriticalIndex);
  229.  
  230. subWriter.WriteInt(newIndex.Key);//id
  231. subWriter.WriteBoolean(newUnDiacriticalIndex != -1);//dialectCriticalText
  232. subWriter.WriteInt(newIndex.Value);//position
  233.  
  234. if (newUnDiacriticalIndex != -1)
  235. {
  236. subWriter.WriteInt(newUnDiacriticalIndex);
  237. }
  238. }
  239. var subData = subWriter.Data;
  240. writer.WriteInt(subData.Length);
  241. writer.WriteBytes(subData);
  242.  
  243.  
  244. subWriter.Clear();
  245. foreach (var textIndex in newTextIndexes)
  246. {
  247. subWriter.WriteUTF(textIndex.Key);
  248. subWriter.WriteInt(textIndex.Value);
  249. }
  250. subData = subWriter.Data;
  251. writer.WriteInt(subData.Length);
  252. writer.WriteBytes(subData);
  253.  
  254.  
  255. subWriter.Clear();
  256. foreach (var textSortIndex in newIndexes)
  257. {
  258. subWriter.WriteInt(textSortIndex.Key);
  259. }
  260. subData = subWriter.Data;
  261. writer.WriteInt(subData.Length);
  262. writer.WriteBytes(subData);
  263.  
  264.  
  265. writer.Dispose();
  266. }
  267.  
  268. public void Dispose()
  269. {
  270. _reader.Dispose();
  271. }
  272. }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement