1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 |
|
---|
5 | namespace Oni.Game
|
---|
6 | {
|
---|
7 | internal class CharacterClass
|
---|
8 | {
|
---|
9 | public InstanceDescriptor Body;
|
---|
10 | public InstanceDescriptor[] Textures;
|
---|
11 | public IEnumerable<InstanceDescriptor> Animations;
|
---|
12 | public InstanceDescriptor Animation;
|
---|
13 |
|
---|
14 | public static CharacterClass Read(InstanceDescriptor descriptor)
|
---|
15 | {
|
---|
16 | return Read(descriptor, null);
|
---|
17 | }
|
---|
18 |
|
---|
19 | public static CharacterClass Read(InstanceDescriptor descriptor, string animationName)
|
---|
20 | {
|
---|
21 | if (descriptor.Template.Tag != TemplateTag.ONCC)
|
---|
22 | throw new ArgumentException(string.Format("The specified descriptor has a wrong template {0}", descriptor.Template.Tag), "descriptor");
|
---|
23 |
|
---|
24 | var character = new CharacterClass();
|
---|
25 |
|
---|
26 | InstanceDescriptor trma;
|
---|
27 | InstanceDescriptor trac;
|
---|
28 |
|
---|
29 | using (var reader = descriptor.OpenRead(0xC34))
|
---|
30 | {
|
---|
31 | character.Body = reader.ReadInstance();
|
---|
32 | trma = reader.ReadInstance();
|
---|
33 | reader.Skip(0x44);
|
---|
34 | trac = reader.ReadInstance();
|
---|
35 | }
|
---|
36 |
|
---|
37 | if (trma != null)
|
---|
38 | {
|
---|
39 | using (var reader = trma.OpenRead(22))
|
---|
40 | character.Textures = reader.ReadInstanceArray(reader.ReadUInt16());
|
---|
41 | }
|
---|
42 |
|
---|
43 | var animationList = new List<InstanceDescriptor>();
|
---|
44 |
|
---|
45 | while (trac != null)
|
---|
46 | {
|
---|
47 | InstanceDescriptor childCollection;
|
---|
48 |
|
---|
49 | using (var reader = trac.OpenRead(16))
|
---|
50 | {
|
---|
51 | childCollection = reader.ReadInstance();
|
---|
52 | reader.Skip(2);
|
---|
53 | int count = reader.ReadUInt16();
|
---|
54 |
|
---|
55 | for (int i = 0; i < count; i++)
|
---|
56 | {
|
---|
57 | reader.Skip(8);
|
---|
58 | var tram = reader.ReadInstance();
|
---|
59 |
|
---|
60 | if (tram != null)
|
---|
61 | animationList.Add(tram);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | trac = childCollection;
|
---|
66 | }
|
---|
67 |
|
---|
68 | character.Animations = animationList;
|
---|
69 |
|
---|
70 | if (string.Equals(Path.GetExtension(animationName), ".oni", StringComparison.OrdinalIgnoreCase))
|
---|
71 | {
|
---|
72 | var file = descriptor.File.FileManager.OpenFile(animationName);
|
---|
73 |
|
---|
74 | if (file != null && file.Descriptors[0].Template.Tag == TemplateTag.TRAM)
|
---|
75 | character.Animation = file.Descriptors[0];
|
---|
76 | }
|
---|
77 | else
|
---|
78 | {
|
---|
79 | if (!string.IsNullOrEmpty(animationName) && !animationName.StartsWith("TRAM", StringComparison.Ordinal))
|
---|
80 | animationName = "TRAM" + animationName;
|
---|
81 |
|
---|
82 | foreach (var tram in animationList)
|
---|
83 | {
|
---|
84 | using (var animReader = tram.OpenRead(0x15A))
|
---|
85 | {
|
---|
86 | int type = animReader.ReadInt16();
|
---|
87 | animReader.Skip(2);
|
---|
88 | int fromState = animReader.ReadInt16();
|
---|
89 | int toState = animReader.ReadInt16();
|
---|
90 | animReader.Skip(6);
|
---|
91 | int varient = animReader.ReadInt16();
|
---|
92 |
|
---|
93 | if (!string.IsNullOrEmpty(animationName))
|
---|
94 | {
|
---|
95 | if (tram.FullName == animationName)
|
---|
96 | {
|
---|
97 | character.Animation = tram;
|
---|
98 | break;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | else
|
---|
102 | {
|
---|
103 | if (type == 6
|
---|
104 | && fromState == 7
|
---|
105 | && toState == 7
|
---|
106 | && varient == 0)
|
---|
107 | {
|
---|
108 | character.Animation = tram;
|
---|
109 | break;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (!string.IsNullOrEmpty(animationName) && character.Animation == null)
|
---|
116 | Console.Error.WriteLine("Animation {0} was not found", animationName);
|
---|
117 | }
|
---|
118 |
|
---|
119 | return character;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|