private bool ProcessCreatePacket(CreatePacket createPacket) { lock (processLockObject) { // todo return(true); } }
private void ProcessFile(string fileName, CancellationToken ct) { var fileLines = File.ReadAllLines(fileName); // "Timestamp","LandCell","RawCoordinates","JSON" if (fileLines.Length < 2 || fileLines[0] != "\"Timestamp\",\"LandCell\",\"RawCoordinates\",\"JSON\"") { return; } lock (totalLinesLockObject) totalLines += fileLines.Length; foreach (var line in fileLines) { if (ct.IsCancellationRequested) { return; } // "2017-01-23 15:25:42Z,"00000000",49.0488996505737 -29.9918003082275 -7.45058059692383E-09","{"RawData":"45F70000334020771100000003980100180001000C00000000003D00020000000C00000000000000B40104724BC8704135EFEFC1000000B2F70435BF0000000000000000F70435BF86000009220000202B000034DA0500020100020000000000000002000000000090010000300000000400446F6F720000A131171380000000141000002000000000000040"}" // "2017-01-23 15:25:42Z,"00000000",49.0488996505737 -29.9918003082275 -7.45058059692383E-09","{"Id":"1998602291","ObjectClass":"Door","BoolValues":{},"DoubleValues":{"167772167":"269.999963323784","167772168":"2"},"LongValues":{"19":" - 1","218103811":"1912865204","218103831":"2","218103835":"4116","218103843":"32","218103847":"104451","218103808":"12705","218103830":"33555930","218103832":"48","218103834":"128","218103809":"4887"},"StringValues":{"1":"Door"},"ActiveSpells":"","Spells":""}" try { var thirdComma = Util.IndexOfNth(line, ',', 3); if (thirdComma == -1) // Corrupt line { Interlocked.Increment(ref corruptLines); continue; } var firstPart = line.Substring(0, thirdComma); var firstPartSplit = firstPart.Split(','); if (firstPartSplit[0] == "\"Timestamp\"") // Header line { continue; } DateTime timestamp; if (!DateTime.TryParse(firstPartSplit[0].Substring(1, firstPartSplit[0].Length - 2), out timestamp)) // Corrupt line { Interlocked.Increment(ref corruptLines); continue; } int landcell; if (!int.TryParse(firstPartSplit[1].Substring(1, firstPartSplit[1].Length - 2), NumberStyles.HexNumber, null, out landcell)) // Corrupt line { Interlocked.Increment(ref corruptLines); continue; } double x; double y; double z; var rawCoordinatesSplit = firstPartSplit[2].Substring(1, firstPartSplit[2].Length - 2).Split(' '); if (rawCoordinatesSplit.Length != 3) // Corrupt line { Interlocked.Increment(ref corruptLines); continue; } if (!double.TryParse(rawCoordinatesSplit[0], out x)) // Corrupt line { Interlocked.Increment(ref corruptLines); continue; } if (!double.TryParse(rawCoordinatesSplit[1], out y)) // Corrupt line { Interlocked.Increment(ref corruptLines); continue; } if (!double.TryParse(rawCoordinatesSplit[2], out z)) // Corrupt line { Interlocked.Increment(ref corruptLines); continue; } var rawCoordinates = new Tuple <double, double, double>(x, y, z); var jsonPart = line.Substring(thirdComma + 1, line.Length - (thirdComma + 1)); if (jsonPart[0] != '"' || jsonPart[jsonPart.Length - 1] != '"') // Corrupt line { Interlocked.Increment(ref corruptLines); continue; } jsonPart = jsonPart.Substring(1, jsonPart.Length - 2); // Trim the quotes... why did I add them.. :( if (jsonPart.StartsWith("{\"Ra")) { var createPacket = new CreatePacket(); createPacket.Timestamp = timestamp; createPacket.Landcell = landcell; createPacket.RawCoordinates = rawCoordinates; Dictionary <string, object> result = (Dictionary <string, object>)jsonSerializer.DeserializeObject(jsonPart); if (result.Count != 1) { Interlocked.Increment(ref corruptLines); continue; } foreach (var kvp in result) { createPacket.RawData = Util.HexStringToByteArray((string)kvp.Value); } if (ct.IsCancellationRequested) { return; } if (!ProcessCreatePacket(createPacket)) { Interlocked.Increment(ref corruptLines); continue; } } else if (jsonPart.StartsWith("{\"Id")) { bool retried = false; retry: try { var identResponse = new IdentResponse(); identResponse.Timestamp = timestamp; identResponse.Landcell = landcell; identResponse.RawCoordinates = rawCoordinates; Dictionary <string, object> result = (Dictionary <string, object>)jsonSerializer.DeserializeObject(jsonPart); identResponse.ParseFromDictionary(result); if (ct.IsCancellationRequested) { return; } if (!ProcessIdentResponse(identResponse)) { Interlocked.Increment(ref corruptLines); continue; } } catch { // This hacky retry method is about 25% faster if (!retried) { retried = true; // This is because I forgot to encode strings... jsonPart = jsonPart.Replace(" \"Ruschk\" ", " \\\"Ruschk\\\" "); jsonPart = jsonPart.Replace(" \"giants\" ", " \\\"giants\\\" "); jsonPart = jsonPart.Replace(" \"guard dogs.\"", " \\\"guard dogs.\\\""); jsonPart = jsonPart.Replace(" \"Slayer of Hope\" ", " \\\"Slayer of Hope\\\" "); jsonPart = jsonPart.Replace(" \"The Haven\"", " \\\"The Haven\\\""); jsonPart = jsonPart.Replace(" \"intelligent portals\" ", " \\\"intelligent portals\\\" "); jsonPart = jsonPart.Replace(" \"anti-magic\" ", " \\\"anti-magic\\\" "); jsonPart = jsonPart.Replace(" \"slippage\"", " \\\"slippage\\\""); jsonPart = jsonPart.Replace(" \"spilled\"", " \\\"spilled\\\""); jsonPart = jsonPart.Replace(" \"small place\" ", " \\\"small place\\\" "); jsonPart = jsonPart.Replace(" \"Hea Arantah's\" ", " \\\"Hea Arantah's\\\" "); jsonPart = jsonPart.Replace(" \"Wharu\"", " \\\"Wharu\\\""); jsonPart = jsonPart.Replace(" \"infiltrators,\"", " \\\"infiltrators,\\\""); jsonPart = jsonPart.Replace(" \". . . the (Undead) believe the tentacled creatures are the spawn of the Great Ones.\" ", " \\\". . . the (Undead) believe the tentacled creatures are the spawn of the Great Ones.\\\" "); jsonPart = jsonPart.Replace(" \"Great Ones\" ", " \\\"Great Ones\\\" "); jsonPart = jsonPart.Replace(" \"fire that fell from the sky.\"", " \\\"fire that fell from the sky.\\\""); jsonPart = jsonPart.Replace(" \"Bloodless,\"", " \\\"Bloodless,\\\""); jsonPart = jsonPart.Replace(" \"it is best to let a sleeping Ursuin lie.\"", " \\\"it is best to let a sleeping Ursuin lie.\\\""); jsonPart = jsonPart.Replace(" \"pure\" ", " \\\"pure\\\" "); jsonPart = jsonPart.Replace(" \"The Story of Ben Ten and Yanshi.\" ", " \\\"The Story of Ben Ten and Yanshi.\\\" "); jsonPart = jsonPart.Replace(" \"Gria'venir,\" ", " \\\"Gria'venir,\\\" "); jsonPart = jsonPart.Replace(" \"Atual Arutoa\" ", " \\\"Atual Arutoa\\\" "); jsonPart = jsonPart.Replace(" \"wings.\" ", " \\\"wings.\\\" "); jsonPart = jsonPart.Replace(" \"Great Tukal\" ", " \\\"Great Tukal\\\" "); jsonPart = jsonPart.Replace(" \"snow sharks.\" ", " \\\"snow sharks.\\\" "); jsonPart = jsonPart.Replace("\"Lords of the World\" ", "\\\"Lords of the World\\\" "); jsonPart = jsonPart.Replace("\"Winds From Darkness\"", "\\\"Winds From Darkness\\\""); jsonPart = jsonPart.Replace(" \"Property of Celcynd\" ", " \\\"Property of Celcynd\\\" "); goto retry; } Interlocked.Increment(ref corruptLines); continue; } } else { Interlocked.Increment(ref corruptLines); continue; } } catch { Interlocked.Increment(ref corruptLines); continue; } } }