· 5 years ago · Oct 23, 2020, 02:28 PM
1using System;
2using System.Collections.Generic;
3using System.Data.Linq;
4using System.Data.Linq.Mapping;
5using System.Data.SqlClient;
6using System.IO;
7using System.Linq;
8using System.Linq.Expressions;
9using System.Runtime.InteropServices.WindowsRuntime;
10using System.Text;
11using System.Text.RegularExpressions;
12using System.Threading.Tasks;
13
14namespace conan_log_parser
15{
16 class Program
17 {
18 static DataContext db;
19
20 static void Main(string[] args)
21 {
22 string filename = "ConanSandbox.log";
23 DateTime filetime = File.GetCreationTime(filename);
24 long filepos = 0;
25
26 string connectionString = @"Data Source=10.200.128.90,1433;Initial Catalog=Conex;User ID=conex-website;Password=Nisl6Non6Maximus6Non;";
27 db = new DataContext(connectionString);
28
29 if (!db.DatabaseExists()) Environment.Exit(2);
30
31 CreateTable();
32
33 while (true)
34 {
35 System.Threading.Thread.Sleep(500);
36
37 if (!File.Exists(filename)) continue;
38
39 try
40 {
41 List<ChatMessage> messages = new List<ChatMessage>();
42
43 DateTime newfiletime = File.GetCreationTime(filename);
44 if (newfiletime != filetime)
45 {
46 filetime = newfiletime;
47 filepos = 0;
48 Console.WriteLine();
49 Console.WriteLine("New log file opened....");
50 Console.WriteLine();
51 }
52
53 using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
54 {
55 fs.Seek(filepos, SeekOrigin.Current);
56 using (StreamReader sr = new StreamReader(fs))
57 {
58 while (!sr.EndOfStream)
59 {
60 var msg = ParseLine(sr.ReadLine());
61 if (msg != null) messages.Add(msg);
62 }
63 filepos = fs.Position;
64 }
65 }
66
67 if (messages.Count > 0)
68 {
69 var table = db.GetTable<ChatMessage>();
70 foreach (var msg in messages)
71 {
72 bool exist = table.FirstOrDefault(m => m.Hashcode == msg.Hashcode) != null;
73
74 if (!exist)
75 {
76 table.InsertOnSubmit(msg);
77 Console.WriteLine("{0} : {1} - {2}", msg.Timestamp, msg.Player, msg.Message);
78 }
79 }
80 db.SubmitChanges();
81 }
82 }
83 catch (System.IO.FileNotFoundException)
84 {
85 }
86 }
87 }
88
89 static ChatMessage ParseLine(string line)
90 {
91 bool ischat = line.IndexOf("ChatWindow:") > 0;
92 if (ischat)
93 {
94 Regex regex = new Regex(@"\[(.*?)\:.*\]\[.*?\]ChatWindow: Character (.*?) said: (.*)", RegexOptions.IgnoreCase);
95
96 Match match = regex.Match(line);
97 if (match.Success)
98 {
99 var groups = match.Groups;
100 if (groups.Count != 4) return null;
101
102 DateTime timestamp;
103 if (!DateTime.TryParseExact(groups[1].Value, "yyyy.MM.dd-HH.mm.ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out timestamp)) return null;
104
105 string player = groups[2].Value;
106
107 string message = groups[3].Value;
108
109 int hashcode = (timestamp.ToFileTime().ToString() + player + message).GetHashCode();
110
111 return new ChatMessage() { Hashcode = hashcode, Timestamp = timestamp, Player = player, Message = message };
112 }
113
114 return null;
115 }
116 return null;
117 }
118
119 static void CreateTable ()
120 {
121 string query =
122 @"IF NOT EXISTS (SELECT * FROM sys.objects
123 WHERE object_id = OBJECT_ID(N'[dbo].[Venera1_chat]') AND type in (N'U'))
124
125 BEGIN
126 CREATE TABLE Venera1_Chat (
127 Id int IDENTITY,
128 Hashcode int NOT NULL UNIQUE,
129 Timestamp datetime2 NOT NULL,
130 Player varchar(255) NOT NULL,
131 Message varchar(1024)
132 )
133 END";
134 db.ExecuteCommand(query);
135 }
136
137 [Table(Name = "Venera1_Chat")]
138 public class ChatMessage
139 {
140 [Column(IsPrimaryKey = true, IsDbGenerated = true)]
141 public int Id { get; set; }
142
143 [Column(Name = "Hashcode", CanBeNull = false)]
144 public int Hashcode { get; set; }
145
146 [Column(Name = "Timestamp", CanBeNull = false)]
147 public DateTime Timestamp { get; set; }
148
149 [Column(Name = "Player", CanBeNull = false)]
150 public string Player { get; set; }
151
152 [Column(Name = "Message")]
153 public string Message { get; set; }
154 }
155 }
156}
157