1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using Oni.Metadata; |
4 |
|
5 |
namespace Oni.Objects |
6 |
{ |
7 |
internal class ObjcDatWriter : Importer |
8 |
{ |
9 |
private readonly TypeTag tag; |
10 |
private readonly string name; |
11 |
private readonly List<Objects.ObjectBase> objects; |
12 |
private readonly Type type; |
13 |
|
14 |
private ObjcDatWriter(TypeTag tag, string name, List<Objects.ObjectBase> objects) |
15 |
{ |
16 |
this.tag = tag; |
17 |
this.name = name; |
18 |
this.objects = objects; |
19 |
|
20 |
if (tag == TypeTag.CHAR) |
21 |
type = typeof(Character); |
22 |
else if (tag == TypeTag.WEAP) |
23 |
type = typeof(Weapon); |
24 |
else if (tag == TypeTag.PART) |
25 |
type = typeof(Particle); |
26 |
else if (tag == TypeTag.PWRU) |
27 |
type = typeof(PowerUp); |
28 |
else if (tag == TypeTag.FLAG) |
29 |
type = typeof(Flag); |
30 |
else if (tag == TypeTag.DOOR) |
31 |
type = typeof(Door); |
32 |
else if (tag == TypeTag.CONS) |
33 |
type = typeof(Objects.Console); |
34 |
else if (tag == TypeTag.FURN) |
35 |
type = typeof(Furniture); |
36 |
else if (tag == TypeTag.TRIG) |
37 |
type = typeof(Trigger); |
38 |
else if (tag == TypeTag.TRGV) |
39 |
type = typeof(TriggerVolume); |
40 |
else if (tag == TypeTag.SNDG) |
41 |
type = typeof(Objects.Sound); |
42 |
else if (tag == TypeTag.TURR) |
43 |
type = typeof(Turret); |
44 |
else if (tag == TypeTag.NEUT) |
45 |
type = typeof(Neutral); |
46 |
else if (tag == TypeTag.PATR) |
47 |
type = typeof(PatrolPath); |
48 |
} |
49 |
|
50 |
private enum TypeTag |
51 |
{ |
52 |
CHAR = 0x43484152, |
53 |
CMBT = 0x434d4254, |
54 |
CONS = 0x434f4e53, |
55 |
DOOR = 0x444f4f52, |
56 |
FLAG = 0x464c4147, |
57 |
FURN = 0x4655524e, |
58 |
MELE = 0x4d454c45, |
59 |
NEUT = 0x4e455554, |
60 |
PART = 0x50415254, |
61 |
PATR = 0x50415452, |
62 |
PWRU = 0x50575255, |
63 |
SNDG = 0x534e4447, |
64 |
TRGV = 0x54524756, |
65 |
TRIG = 0x54524947, |
66 |
TURR = 0x54555252, |
67 |
WEAP = 0x57454150 |
68 |
} |
69 |
|
70 |
public static void Write(List<Objects.ObjectBase> objects, string outputDirPath) |
71 |
{ |
72 |
System.Console.Error.WriteLine("Writing {0} objects...", objects.Count); |
73 |
|
74 |
Write(TypeTag.CHAR, "Character", objects, outputDirPath); |
75 |
Write(TypeTag.CONS, "Console", objects, outputDirPath); |
76 |
Write(TypeTag.DOOR, "Door", objects, outputDirPath); |
77 |
Write(TypeTag.FLAG, "Flag", objects, outputDirPath); |
78 |
Write(TypeTag.FURN, "Furniture", objects, outputDirPath); |
79 |
Write(TypeTag.NEUT, "Neutral", objects, outputDirPath); |
80 |
Write(TypeTag.PART, "Particle", objects, outputDirPath); |
81 |
Write(TypeTag.PATR, "Patrol Path", objects, outputDirPath); |
82 |
Write(TypeTag.PWRU, "PowerUp", objects, outputDirPath); |
83 |
Write(TypeTag.SNDG, "Sound", objects, outputDirPath); |
84 |
Write(TypeTag.TRIG, "Trigger", objects, outputDirPath); |
85 |
Write(TypeTag.TRGV, "Trigger Volume", objects, outputDirPath); |
86 |
Write(TypeTag.TURR, "Turret", objects, outputDirPath); |
87 |
Write(TypeTag.WEAP, "Weapon", objects, outputDirPath); |
88 |
} |
89 |
|
90 |
private static void Write(TypeTag tag, string name, List<Objects.ObjectBase> objects, string outputDirPath) |
91 |
{ |
92 |
var writer = new ObjcDatWriter(tag, name, objects); |
93 |
writer.Import(null, outputDirPath); |
94 |
} |
95 |
|
96 |
public override void Import(string filePath, string outputDirPath) |
97 |
{ |
98 |
BeginImport(); |
99 |
|
100 |
var bina = CreateInstance(TemplateTag.BINA, "CJBO" + name); |
101 |
|
102 |
int offset = RawWriter.Align32(); |
103 |
int size = WriteCollection(RawWriter); |
104 |
|
105 |
using (var writer = bina.OpenWrite()) |
106 |
{ |
107 |
writer.Write(size); |
108 |
writer.Write(offset); |
109 |
} |
110 |
|
111 |
Write(outputDirPath); |
112 |
} |
113 |
|
114 |
private int WriteCollection(BinaryWriter raw) |
115 |
{ |
116 |
int start = raw.Position; |
117 |
|
118 |
raw.Write((int)BinaryTag.OBJC); |
119 |
raw.Write(0); |
120 |
raw.Write(39); |
121 |
|
122 |
foreach (var obj in objects.Where(o => o.GetType() == type)) |
123 |
{ |
124 |
int objectStartPosition = raw.Position; |
125 |
raw.Write(0); |
126 |
raw.Write((int)tag); |
127 |
|
128 |
obj.Write(raw); |
129 |
|
130 |
raw.Position = Utils.Align4(raw.Position); |
131 |
int objectSize = raw.Position - objectStartPosition - 4; |
132 |
raw.WriteAt(objectStartPosition, objectSize); |
133 |
} |
134 |
|
135 |
raw.Write(0); |
136 |
int size = raw.Position - start; |
137 |
raw.WriteAt(start + 4, size - 8); |
138 |
return size; |
139 |
} |
140 |
} |
141 |
} |