Advertisement
Guest User

wurmdate

a guest
Dec 4th, 2011
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.72 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using System.IO;
  6. using Microsoft.Win32;
  7.  
  8. namespace WurmTime
  9. {
  10.     class Days
  11.     {
  12.         public static string[] days = { "Ant",
  13.                           "Luck",
  14.                           "Wurm",
  15.                           "Wrath",
  16.                           "Tears",
  17.                           "Sleep",
  18.                           "Awakening" };
  19.         public const int Ant = 0;
  20.         public const int Luck = 1;
  21.         public const int Wurm = 2;
  22.         public const int Wrath = 3;
  23.         public const int Tears = 4;
  24.         public const int Sleep = 5;
  25.         public const int Awakening = 6;
  26.  
  27.         public static int ByName(string day)
  28.         {
  29.             return Array.IndexOf(days, day);
  30.         }
  31.  
  32.         public static string ByNumber(int day)
  33.         {
  34.             return days[day];
  35.         }
  36.  
  37.         public int this[string day]
  38.         {
  39.             get { return ByName(day); }
  40.         }
  41.  
  42.         public string this[int day]
  43.         {
  44.             get { return ByNumber(day); }
  45.         }
  46.     }
  47.  
  48.     class Starfalls
  49.     {
  50.         public static string[] starfalls = { "Diamonds",
  51.                                "Saw",
  52.                                "Digging",
  53.                                "Leaf",
  54.                                "Bears",
  55.                                "Snakes",
  56.                                "White Shark",
  57.                                "Fires",
  58.                                "Raven",
  59.                                "Dancers",
  60.                                "Omens",
  61.                                "Silence" };
  62.  
  63.         public const int Diamonds = 0;
  64.         public const int Saw = 1;
  65.         public const int Digging = 2;
  66.         public const int Leaf = 3;
  67.         public const int Bears = 4;
  68.         public const int Snakes = 5;
  69.         public const int WhiteShark = 6;
  70.         public const int Fires = 7;
  71.         public const int Raven = 8;
  72.         public const int Dancers = 9;
  73.         public const int Omens = 10;
  74.         public const int Silence = 11;
  75.  
  76.         public static int ByName(string starfall)
  77.         {
  78.             return Array.IndexOf(starfalls, starfall);
  79.         }
  80.  
  81.         public static string ByNumber(int starfall)
  82.         {
  83.             return starfalls[starfall];
  84.         }
  85.  
  86.         public int this[string starfall]
  87.         {
  88.             get { return ByName(starfall); }
  89.         }
  90.  
  91.         public string this[int starfall]
  92.         {
  93.             get { return ByNumber(starfall); }
  94.         }
  95.     }
  96.  
  97.     public struct WurmDateTime
  98.     {
  99.         private int starfall;
  100.         private int day;
  101.         private double time;
  102.  
  103.         public int Starfall
  104.         {
  105.             get { return starfall; }
  106.             set { starfall = value; }
  107.         }
  108.  
  109.         public int Week
  110.         {
  111.             get { return (day / 7) + 1; }
  112.         }
  113.  
  114.         public int Day
  115.         {
  116.             get { return day; }
  117.             set { day = value; }
  118.         }
  119.  
  120.         public int DayOfWeek
  121.         {
  122.             get { return day % 7; }
  123.         }
  124.  
  125.         public double Time
  126.         {
  127.             get { return time; }
  128.             set { time = value; }
  129.         }
  130.  
  131.         public String StarfallName
  132.         {
  133.             get { return Starfalls.ByNumber(starfall % 12); }
  134.         }
  135.  
  136.         public String DayName
  137.         {
  138.             get { return Days.ByNumber(DayOfWeek); }
  139.         }
  140.  
  141.         public String Text
  142.         {
  143.             get { return new TimeSpan(0, 0, (int)time) + " on " + DayName + " in week " + Week + " of the " + StarfallName + " starfall"; }
  144.         }
  145.  
  146.         public void AddReal(TimeSpan span)
  147.         {
  148.             Add(new TimeSpan(0, 0, (int)span.TotalSeconds * 8));
  149.         }
  150.  
  151.         public void Add(TimeSpan span)
  152.         {
  153.             time += span.TotalSeconds;
  154.             while (time >= 86400)
  155.             {
  156.                 day++;
  157.                 time -= 86400;
  158.             }
  159.             while (day >= 28)
  160.             {
  161.                 starfall++;
  162.                 day -= 28;
  163.             }
  164.             while (starfall >= 12)
  165.             {
  166.                 starfall -= 12;
  167.             }
  168.         }
  169.     }
  170.  
  171.     struct WurmDateTimeStamp
  172.     {
  173.         public DateTime real;
  174.         public WurmDateTime wurm;
  175.     }
  176.  
  177.     class DateExtract
  178.     {
  179.         private static String RegistryKey = @"HKEY_CURRENT_USER\Software\JavaSoft\Prefs\com\wurmonline\client";
  180.  
  181.         public static String getLogPath()
  182.         {
  183.             Object wurmDir = Registry.GetValue(RegistryKey, "wurm_dir", null);
  184.             Object wurmUser = Registry.GetValue(RegistryKey, "wurm_user", null);
  185.  
  186.             if (wurmDir == null || wurmUser == null)
  187.                 return null;
  188.  
  189.             String logsDir = wurmDir + "//players/" + wurmUser + "//logs";
  190.             return logsDir.Replace("//", "\\");
  191.         }
  192.  
  193.         public static bool parseDateTime(String date, ref WurmDateTime cur)
  194.         {
  195.             string[] days = { "day of the Ant",
  196.                               "Luck day",
  197.                               "day of the Wurm",
  198.                               "Wrath day",
  199.                               "day of Tears",
  200.                               "day of Sleep",
  201.                               "day of Awakening" };
  202.             string[] starfalls = { "the starfall of Diamonds",
  203.                                    "the starfall of the Saw",
  204.                                    "the starfall of the Digging",
  205.                                    "the starfall of the Leaf",
  206.                                    "the Bear's starfall",
  207.                                    "the Snake's starfall",
  208.                                    "the White Shark starfall",
  209.                                    "the starfall of Fires",
  210.                                    "the Raven's starfall",
  211.                                    "the starfall of Dancers",
  212.                                    "the starfall of Omens",
  213.                                    "the starfall of Silence" };
  214.             Regex TimeEntry = new Regex(@"^It is (\d\d):(\d\d):(\d\d) on (.*) in week (\d) of (.*) in the year of (\d*).$");
  215.  
  216.             Match match = TimeEntry.Match(date);
  217.             if (match.Success)
  218.             {
  219.                 int day = Array.IndexOf(days, match.Groups[4].Value);
  220.                 int week = Int16.Parse(match.Groups[5].Value);
  221.                 int starfall = Array.IndexOf(starfalls, match.Groups[6].Value);
  222.                 int year = Int16.Parse(match.Groups[7].Value);
  223.  
  224.                 int hour = Int16.Parse(match.Groups[1].Value);
  225.                 int minute = Int16.Parse(match.Groups[2].Value);
  226.                 int second = Int16.Parse(match.Groups[3].Value);
  227.  
  228.                 if (day == -1 || week == -1 || starfall == -1 || year == -1 || hour == -1 || minute == -1 || second == -1)
  229.                 {
  230.                     System.Diagnostics.Debug.WriteLine("Parse Error " + match.Groups[0] + String.Format(" {0} {1} {2} {3}", day, week, starfall, year));
  231.                     return false;
  232.                 }
  233.  
  234.                 cur.Day = day + 7 * week - 7;
  235.                 cur.Starfall = starfall;
  236.                 cur.Time = hour * 60 * 60 + minute * 60 + second;
  237.                 return true;
  238.             }
  239.             return false;
  240.         }
  241.  
  242.  
  243.         public static bool scanLogFile(String logFile, ref WurmDateTime cur, ref WurmDateTimeStamp stamp, DateTime maxDate)
  244.         {
  245.  
  246.             bool foundMatch = false;
  247.  
  248.             System.Diagnostics.Debug.WriteLine(logFile);
  249.  
  250.             FileStream stream = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  251.             StreamReader reader = new StreamReader(stream);
  252.  
  253.             Regex LoggingStarted = new Regex(@"^Logging started (\d{4}-\d{2}-\d{2})$");
  254.             Regex LogEntry = new Regex(@"^\[(\d\d):(\d\d):(\d\d)] (.*)$");
  255.  
  256.             DateTime lastLogDate = new DateTime();
  257.             while (!reader.EndOfStream)
  258.             {
  259.                 String line = reader.ReadLine();
  260.                 Match match = LoggingStarted.Match(line);
  261.                 if (match.Success)
  262.                 {
  263.                     lastLogDate = DateTime.Parse(match.Groups[1].Value);
  264.                     continue;
  265.                 }
  266.                 match = LogEntry.Match(line);
  267.                 if (match.Success)
  268.                 {
  269.                     DateTime currentLogDate = new DateTime(
  270.                         lastLogDate.Year, lastLogDate.Month, lastLogDate.Day,
  271.                         Int16.Parse(match.Groups[1].Value),
  272.                         Int16.Parse(match.Groups[2].Value),
  273.                         Int16.Parse(match.Groups[3].Value),
  274.                         DateTimeKind.Local);
  275.                     while (currentLogDate < lastLogDate)
  276.                         currentLogDate = currentLogDate.AddDays(1); // Handle day wrap around
  277.                     lastLogDate = currentLogDate;
  278.  
  279.                     if (currentLogDate > maxDate)
  280.                     {
  281.                         System.Diagnostics.Debug.WriteLine(currentLogDate + " > " + maxDate + ". Exiting");
  282.                         break;
  283.                     }
  284.  
  285.                     if (parseDateTime(match.Groups[4].Value, ref cur))
  286.                     {
  287.                         System.Diagnostics.Debug.WriteLine(lastLogDate + ": new Date " + match.Groups[4].Value);
  288.                         stamp.wurm = cur;
  289.                         stamp.real = lastLogDate;
  290.  
  291.                         foundMatch = true;
  292.                     }
  293.                 }
  294.             }
  295.  
  296.             reader.Close();
  297.             stream.Close();
  298.  
  299.             return foundMatch;
  300.         }
  301.     }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement