HKI Core
Date.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Xml;
3 
4 namespace HKI.Core.Variables
5 {
6  [System.Serializable]
7  [HKIVar]
8  [CreateAssetMenu(fileName = "Date", menuName = "HKI/Variables/Date", order = 20)]
9  public class Date : HKIVarGeneric<DayMonthYear>
10  {
11  // Interface functions
12  public bool ValidateDay()
13  {
14  return Value.ValidateDay();
15  }
16 
17  public override void Save(XmlNode parentNode)
18  {
19  XmlNode node = parentNode.OwnerDocument.CreateElement(name.Replace(" ", ""));
20  parentNode.AppendChild(node);
21 
22  XmlAttribute valueDayAttribute = parentNode.OwnerDocument.CreateAttribute("Day");
23  valueDayAttribute.Value = Value.Day.ToString();
24  node.Attributes.Append(valueDayAttribute);
25 
26  XmlAttribute valueMonthAttribute = parentNode.OwnerDocument.CreateAttribute("Month");
27  valueMonthAttribute.Value = Value.Month.ToString();
28  node.Attributes.Append(valueMonthAttribute);
29 
30  XmlAttribute valueYearAttribute = parentNode.OwnerDocument.CreateAttribute("Year");
31  valueYearAttribute.Value = Value.Year.ToString();
32  node.Attributes.Append(valueYearAttribute);
33 
34  XmlAttribute valueBCADAttribute = parentNode.OwnerDocument.CreateAttribute("BCAD");
35  valueBCADAttribute.Value = Value.BCAD.ToString();
36  node.Attributes.Append(valueBCADAttribute);
37  }
38 
39  public override void Load(XmlNode node)
40  {
41  if (node.Attributes == null)
42  {
43  Debug.LogError("(Date) >" + name + "< Loading failed because this XmlNode has not attributes!");
44  return;
45  }
46 
47  XmlAttribute valueDayAttribute = node.Attributes["Day"];
48  if(valueDayAttribute == null)
49  {
50  Debug.LogError("(Date) >" + name + "< Loading failed because this XmlNode has not an attribute with the name >Day<!");
51  return;
52  }
53 
54  XmlAttribute valueMonthAttribute = node.Attributes["Month"];
55  if(valueMonthAttribute == null)
56  {
57  Debug.LogError("(Date) >" + name + "< Loading failed because this XmlNode has not an attribute with the name >Month<!");
58  return;
59  }
60 
61  XmlAttribute valueYearAttribute = node.Attributes["Year"];
62  if(valueYearAttribute == null)
63  {
64  Debug.LogError("(Date) >" + name + "< Loading failed because this XmlNode has not an attribute with the name >Year<!");
65  return;
66  }
67 
68  XmlAttribute valueBCADAttribute = node.Attributes["BCAD"];
69  if(valueBCADAttribute == null)
70  {
71  Debug.LogError("(Date) >" + name + "< Loading failed because this XmlNode has not an attribute with the name >BCAD<!");
72  return;
73  }
74 
75  int parsedDayValue;
76  if (!int.TryParse(valueDayAttribute.Value, out parsedDayValue))
77  Debug.LogError("(Date) >" + name + "< Loading failed because parsing of >" + valueDayAttribute.Value + "< failed!");
78 
79  DayMonthYear.Months parsedMonthValue;
80  if (!System.Enum.TryParse(valueMonthAttribute.Value, out parsedMonthValue))
81  Debug.LogError("(Date) >" + name + "< Loading failed because parsing of >" + valueMonthAttribute.Value + "< failed!");
82 
83  int parsedYearValue;
84  if (!int.TryParse(valueYearAttribute.Value, out parsedYearValue))
85  Debug.LogError("(Date) >" + name + "< Loading failed because parsing of >" + valueYearAttribute.Value + "< failed!");
86 
87  DayMonthYear.BeforeChristAnnoDomini parsedBCADValue;
88  if (!System.Enum.TryParse(valueBCADAttribute.Value, out parsedBCADValue))
89  Debug.LogError("(Date) >" + name + "< Loading failed because parsing of >" + valueBCADAttribute.Value + "< failed!");
90 
91  SetValue = new DayMonthYear(parsedDayValue, parsedMonthValue, parsedYearValue, parsedBCADValue);
92  }
93  }
94 }
override void Load(XmlNode node)
Definition: Date.cs:39
bool ValidateDay()
Definition: Date.cs:12
override void Save(XmlNode parentNode)
Definition: Date.cs:17
This class adds a generic implementation to HKIVar. This reduces the repetitive implementation of thi...