ID:26605
 
Both Deadron's calendar lib and xml lib are amazing and very easy to use. Here's some code to get the calendar initial properties to load up using xml. This is really handy when you want to reuse the calendar in other games without changing code.

These functions should be placed in BaseCamp/Calendar


implementation.dm
LoadCalendarFromXml(filename)
world << "start [filename]"
// This loads the calendar properties from an html file allowing
// you to edit the initial settings of the calendar.
var/XML/Element/root = xmlRootFromFile(filename)
_startingTimeOffset = text2num(root.FirstChildText("startingTimeOffset"))
_baseYear = text2num(root.FirstChildText("baseYear"))
_timeZone = text2num(root.FirstChildText("timeZone"))
_alwaysConsiderLeapYears = text2num(root.FirstChildText("alwaysConsiderLeapYears"))
_timeFormat = root.FirstChildText("timeFormat")
_isCustomCalendar = text2num(root.FirstChildText("isCustomCalendar"))

_timeSpeed = text2num(root.FirstChildText("timeSpeed"))
_hoursPerDay = text2num(root.FirstChildText("hoursPerDay"))

for (var/XML/Element/e in root.Descendants("dayNames"))
for (var/XML/Element/d in e.ChildElements())
if (!_dayNames)
_dayNames = new/list()
_dayNames += d.Text()

for (var/XML/Element/e in root.Descendants("monthNames"))
for (var/XML/Element/m in e.ChildElements())
if (!_monthNames)
_monthNames = new/list()
_monthNames += m.Text()

for (var/XML/Element/e in root.Descendants("numDaysInMonths"))
for (var/XML/Element/m in e.ChildElements())
if (!_numDaysInMonths)
_numDaysInMonths = new/list()
_numDaysInMonths += text2num(m.Text())

for (var/XML/Element/e in root.Descendants("numDaysInLeapMonths"))
for (var/XML/Element/m in e.ChildElements())
if (!_numDaysInLeapMonths)
_numDaysInLeapMonths = new/list()
_numDaysInLeapMonths += text2num(m.Text())

return ..()


Calendar.dm
//////////////////////
// Loading Calendar //
//////////////////////
LoadCalendarFromXml(filename)
/* This loads the calendar properties from an html file allowing
you to edit the initial settings of the calendar.

XML example.
<calendar>
<startingTimeOffset>0</startingTimeOffset>
<baseYear>2000</baseYear>
<timeZone>0</timeZone>
<alwaysConsiderLeapYears>0</alwaysConsiderLeapYears>
<timeFormat>%D %M %d, %y %H:%i %s %p</timeFormat>
<isCustomCalendar>0</isCustomCalendar>

<timeSpeed>1</timeSpeed>
<hoursPerDay>24</hoursPerDay>

<dayNames>
<day>Sunday</day>
<day>Monday</day>
<day>Tuesday</day>
<day>Wedndesday</day>
<day>Thursday</day>
<day>Friday</day>
<day>Saturday</day>
</dayNames>

<monthNames>
<month>January</month>
<month>February</month>
<month>March</month>
<month>April</month>
<month>May</month>
<month>June</month>
<month>July</month>
<month>August</month>
<month>September</month>
<month>October</month>
<month>November</month>
<month>December</month>
</monthNames>

<numDaysInMonths>
<month>31</month>
<month>28</month>
<month>31</month>
<month>30</month>
<month>31</month>
<month>30</month>
<month>31</month>
<month>31</month>
<month>30</month>
<month>31</month>
<month>30</month>
<month>31</month>
</numDaysInMonths>

<numDaysInLeapMonths>
<month>31</month>
<month>29</month>
<month>31</month>
<month>30</month>
<month>31</month>
<month>30</month>
<month>31</month>
<month>31</month>
<month>30</month>
<month>31</month>
<month>30</month>
<month>31</month>
</numDaysInLeapMonths>
</calendar>

Code example.

var/BaseCamp/Calendar/my_calendar = new()
my_calendar.LoadCalendarFromXml("calendar.xml")

var/now = my_calendar.CurrentTime()
src << "The current date and time for this world is: [now]"
*/

return ..()


With a lib, you shouldn't modify the base lib files. Just define the procs (or modify the lib procs) in your own app. That why if/when there is an update, it won't screw everything up :-)
You have a very good point there my friend.
And there is an XML update coming up, and possibly a Calendar update...

Nonetheless, clever idea!
Nice! The XML lib is very useful and handy. Also, did you see my post about the Calendar using timeofday in the processTime function? Here's the thread http://developer.byond.com/forum/ index.cgi?action=message_read&id=521052&forum=8&view=0

Perhaps you can look that over and include it in your Calendar update? :)