CharaOne.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Microsoft.Xna.Framework.Graphics;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. namespace OpenVIII.World
  6. {
  7. internal class CharaOne
  8. {
  9. private Debug_MCH[] mchInstances;
  10. private TextureHandler[] textures;
  11. private byte[] buffer;
  12. /// <summary>
  13. /// Reads chara.one buffer -&gt; chara one is a file packed with MCH files and TIM textures
  14. /// without pointers. File needs to be scanned
  15. /// </summary>
  16. /// <param name="buffer">Full chara.one file</param>
  17. public CharaOne(byte[] buffer)
  18. {
  19. this.buffer = buffer;
  20. var mchs = new List<Debug_MCH>();
  21. var texturesList = new List<TextureHandler>();
  22. var i = 0;
  23. MemoryStream ms = null;
  24. using (var br = new BinaryReader(ms = new MemoryStream(this.buffer)))
  25. {
  26. var eof = br.ReadUInt32();
  27. TIM2 tim;
  28. while (ms.CanRead)
  29. if (ms.Position >= ms.Length)
  30. break;
  31. else if (BitConverter.ToUInt16(this.buffer, (int)ms.Position) == 0)
  32. ms.Seek(2, SeekOrigin.Current);
  33. else if (br.ReadUInt64() == 0x0000000800000010)
  34. {
  35. ms.Seek(-8, SeekOrigin.Current);
  36. tim = new TIM2(this.buffer, (uint)ms.Position);
  37. ms.Seek(tim.GetHeight * tim.GetWidth / 2 + 64, SeekOrigin.Current); //i.e. 64*20=1280/2=640 + 64= 704 + eof
  38. texturesList.Add(TextureHandler.Create($"chara_tim{(i++).ToString("D2")}", tim, 0, null));
  39. }
  40. else //is geometry structure
  41. {
  42. ms.Seek(-8, SeekOrigin.Current);
  43. var mch = new Debug_MCH(ms, br);
  44. if (mch.bValid())
  45. mchs.Add(mch);
  46. }
  47. ms = null;
  48. }
  49. mchInstances = mchs.ToArray();
  50. textures = texturesList.ToArray();
  51. }
  52. public Debug_MCH GetMCH(int i) => i >= mchInstances.Length ? mchInstances[0] : mchInstances[i];
  53. public Texture2D GetCharaTexture(int i) => (Texture2D)textures[i];
  54. public void AssignTextureSizesForMchInstance(int mchInstanceIndex, int[] textureIndexes) =>
  55. mchInstances[mchInstanceIndex].AssignTextureSizes(textures, textureIndexes);
  56. }
  57. }