source: OniSplit/Objects/ObjectEvent.cs@ 1158

Last change on this file since 1158 was 1114, checked in by iritscen, 5 years ago

Adding OniSplit source code (v0.9.99.0). Many thanks to Neo for all his work over the years.

File size: 2.9 KB
RevLine 
[1114]1using System.Collections.Generic;
2using System.Xml;
3using Oni.Metadata;
4
5namespace Oni.Objects
6{
7 internal class ObjectEvent
8 {
9 private ObjectEventType action;
10 private int targetId;
11 private string script;
12
13 public ObjectEvent()
14 {
15 }
16
17 private ObjectEvent(BinaryReader reader)
18 {
19 action = (ObjectEventType)reader.ReadInt16();
20
21 if (action == ObjectEventType.Script)
22 script = reader.ReadString(32);
23 else if (action != ObjectEventType.None)
24 targetId = reader.ReadUInt16();
25 }
26
27 public ObjectEventType Action => action;
28 public int TargetId => targetId;
29 public string Script => script;
30
31 public static ObjectEvent[] ReadEventList(BinaryReader reader)
32 {
33 var events = new ObjectEvent[reader.ReadUInt16()];
34
35 for (int i = 0; i < events.Length; i++)
36 events[i] = new ObjectEvent(reader);
37
38 return events;
39 }
40
41 public static void WriteEventList(BinaryWriter writer, ObjectEvent[] events)
42 {
43 if (events == null)
44 {
45 writer.WriteUInt16(0);
46 return;
47 }
48
49 writer.WriteUInt16(events.Length);
50
51 for (int i = 0; i < events.Length; i++)
52 {
53 writer.WriteUInt16((ushort)events[i].action);
54
55 if (events[i].action == ObjectEventType.Script)
56 writer.Write(events[i].script, 32);
57 else if (events[i].action != ObjectEventType.None)
58 writer.WriteUInt16(events[i].targetId);
59 }
60 }
61
62 public static ObjectEvent[] ReadEventList(XmlReader xml)
63 {
64 var events = new List<ObjectEvent>();
65
66 if (xml.IsStartElement("Events") && xml.IsEmptyElement)
67 {
68 xml.ReadStartElement();
69 return events.ToArray();
70 }
71
72 xml.ReadStartElement("Events");
73
74 while (xml.IsStartElement())
75 {
76 var evt = new ObjectEvent();
77
78 evt.action = MetaEnum.Parse<ObjectEventType>(xml.LocalName);
79
80 switch (evt.action)
81 {
82 case ObjectEventType.None:
83 break;
84 case ObjectEventType.Script:
85 evt.script = xml.GetAttribute("Function");
86 break;
87 default:
88 evt.targetId = XmlConvert.ToInt16(xml.GetAttribute("TargetId"));
89 break;
90 }
91
92 events.Add(evt);
93 xml.Skip();
94 }
95
96 xml.ReadEndElement();
97
98 return events.ToArray();
99 }
100 }
101}
Note: See TracBrowser for help on using the repository browser.