Advertisement
waelwindows92

Untitled

Mar 8th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. public class TextureDatabaseSerial
  2.     {
  3.         [FieldOrder(0)]                                                          public int                      TextureCount;
  4.         [FieldOrder(1)]                                                          public int                      TextureOffest;
  5.         [FieldOrder(2)]                                                          public Int64                    Reserved;
  6.         [FieldOrder(3), SerializeUntil(0)]                                       public List<string>             TextureNames;
  7.         [FieldOrder(4), FieldCount("TextureCount"),FieldOffset("TextureOffest")] public List<TextureEntrySerial> TextureEntries;
  8.  
  9.         [Ignore] public List<TextureNameEntrySerial> NameEntries { get => TextureEntries.Zip(TextureNames, (e, n) => new TextureNameEntrySerial(e, n)).ToList(); set => ApplyChanges(value); }
  10.  
  11.         public void SetOffsets()
  12.         {
  13.             var offset = 16;
  14.             for (var i = 0; i < TextureEntries.Count(); ++i)
  15.             {
  16.                 offset += i != 0 ? TextureNames[i-1].Length+1 : 0;
  17.                 TextureEntries[i].Offset = offset;
  18.             }
  19.         }
  20.  
  21.         public void ApplyChanges(List<TextureNameEntrySerial> nameEntry = null)
  22.         {
  23.             nameEntry = nameEntry ?? NameEntries;
  24.             TextureNames = nameEntry.Select(entry => entry.Name).ToList();
  25.             TextureEntries = nameEntry.Select(entry => entry.ToEntrySerial()).ToList();
  26.            
  27.             TextureOffest = TextureNames.Sum(name => name.Length + 1) + 15;
  28.             TextureOffest += 4 - (TextureOffest % 4);
  29.         }
  30.     }
  31.  
  32.     public class TextureEntrySerial
  33.     {
  34.         [FieldOrder(0)] public int Id;
  35.         [FieldOrder(1)] public int Offset;
  36.  
  37.         [Ignore] public virtual int Size => 8;
  38.  
  39.         public TextureEntrySerial(int id, int offset)
  40.         {
  41.             Id = id;
  42.             Offset = offset;
  43.         }
  44.     }
  45.  
  46.     public class TextureNameEntrySerial : TextureEntrySerial
  47.     {
  48.         public string Name;
  49.  
  50.         [Ignore] public override int Size => 8 + Name.Count() + 1;
  51.  
  52.         public TextureNameEntrySerial(TextureEntrySerial entry, string name) : base(entry.Id, entry.Offset) => Name = name;
  53.  
  54.         public override string ToString() => $"Texture: \"{Name}\" ID {Id} at 0x{Offset:X}";
  55.  
  56.         public TextureEntrySerial ToEntrySerial() => new TextureEntrySerial(Id, Offset);
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement