Mercurial > repos > IBBoard.ArmyBuilder.API
annotate Loaders/ArmyBuilderABFileLoader.cs @ 0:1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 19 Apr 2009 11:24:10 +0000 |
parents | |
children | d5ba733cd289 |
rev | line source |
---|---|
0
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 // This file (ArmyBuilderFileLoader.cs) is a part of the IBBoard.ArmyBuilder.API project and is copyright 2009 IBBoard |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 // |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 // |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 using System; |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 using System.IO; |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 namespace IBBoard.ArmyBuilder.API.Loaders |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 { |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 public class ArmyBuilderABFileLoader |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 { |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 private static byte HEADER_DELIMITER = 0x09; |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 public static ABFile LoadFile(FileInfo file) |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 { |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 ABFile abFile = new ABFile(file); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 LoadData(abFile); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 return abFile; |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 } |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 private static void LoadData(ABFile abFile) |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 { |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 BinaryReaderBigEndian reader = new BinaryReaderBigEndian(abFile.File.OpenRead()); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 try |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 { |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 ReadFileInfo(abFile, reader); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 ReadFileTable(abFile, reader); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 } |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 finally |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 { |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
33 reader.Close(); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
34 } |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
35 } |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
36 |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
37 private static void ReadFileInfo(ABFile abFile, BinaryReaderBigEndian reader) |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
38 { |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
39 int tableOffset = reader.ReadInt(); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
40 reader.Move(tableOffset); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
41 ushort fileTableOffset = reader.ReadUShort(); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
42 abFile.Header = reader.ReadDelimitedString(HEADER_DELIMITER); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
43 abFile.GameName = reader.ReadDelimitedString(HEADER_DELIMITER); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
44 abFile.FolderName = reader.ReadDelimitedString(HEADER_DELIMITER); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
45 abFile.ReleaseMajor = reader.ReadDelimitedString(HEADER_DELIMITER); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
46 abFile.ReleaseMinor = reader.ReadDelimitedString(HEADER_DELIMITER); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
47 abFile.RequiredMajorVersion = reader.ReadDelimitedString(HEADER_DELIMITER); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
48 abFile.RequiredMinorVersion = reader.ReadDelimitedString(HEADER_DELIMITER); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
49 abFile.UnknownNumber = reader.ReadDelimitedString(HEADER_DELIMITER); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
50 abFile.Exporter = reader.ReadDelimitedString(HEADER_DELIMITER); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
51 int bytesRead = CalculateFileInfoSize(abFile); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
52 |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
53 if (fileTableOffset > bytesRead) |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
54 { |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
55 abFile.Comment = reader.ReadString(fileTableOffset - bytesRead); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
56 } |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
57 } |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
58 |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
59 private static int CalculateFileInfoSize(ABFile file) |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
60 { |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
61 return file.Header.Length + file.GameName.Length + file.FolderName.Length + file.ReleaseMajor.Length + file.ReleaseMinor.Length + |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
62 file.RequiredMajorVersion.Length + file.RequiredMinorVersion.Length + file.UnknownNumber.Length + file.Exporter.Length + 9; |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
63 } |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
64 |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
65 private static void ReadFileTable(ABFile abFile, BinaryReaderBigEndian reader) |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
66 { |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
67 FileTableEntry entry = null; |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
68 |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
69 do |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
70 { |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
71 entry = ReadFileTableEntry(reader); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
72 abFile.AddFileTableEntry(entry); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
73 } |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
74 while (entry!=null); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
75 } |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
76 |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
77 private static FileTableEntry ReadFileTableEntry(BinaryReaderBigEndian reader) |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
78 { |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
79 FileTableEntry entry = null; |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
80 string name = reader.ReadUShortLengthString(); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
81 |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
82 if (name!="" && name!=null) |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
83 { |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
84 entry = new FileTableEntry(name); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
85 entry.FirstBytes = reader.ReadBytes(8); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
86 entry.FileSize = reader.ReadInt(); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
87 entry.CompressedSize = reader.ReadInt(); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
88 entry.MidBytes = reader.ReadBytes(4); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
89 entry.Location = reader.ReadInt(); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
90 entry.EndBytes = reader.ReadBytes(8); |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
91 } |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
92 |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
93 return entry; |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
94 } |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
95 } |
1a5205612b72
Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
96 } |