2022-11-01 18:01:28 +01:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2022-11-24 14:18:27 +01:00
|
|
|
|
using System.Runtime.InteropServices;
|
2022-11-01 18:01:28 +01:00
|
|
|
|
|
|
|
|
|
namespace EDPlayerJournal.Entries;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Base class for a single entry within the player journal. If no specific sub class is available
|
|
|
|
|
/// this class gives basic information, such as EventType or date when it happened. It also allows
|
|
|
|
|
/// base classes access to the underlying JSON object. Base classes should be named after the event
|
|
|
|
|
/// type that they map + Entry. So "FSDJump" event is handled by FSDJumpEntry.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Entry {
|
|
|
|
|
private static readonly Dictionary<string, Type> classes = new Dictionary<string, Type> {
|
2023-08-26 10:53:25 +02:00
|
|
|
|
{ Events.ApproachSettlement, typeof(ApproachSettlementEntry) },
|
2022-11-01 18:01:28 +01:00
|
|
|
|
{ Events.Bounty, typeof(BountyEntry) },
|
2022-11-28 19:43:51 +01:00
|
|
|
|
{ Events.CapShipBond, typeof(CapShipBondEntry) },
|
2024-01-29 18:23:19 +01:00
|
|
|
|
{ Events.CarrierJump, typeof(CarrierJump) },
|
2022-11-01 18:01:28 +01:00
|
|
|
|
{ Events.Commander, typeof(CommanderEntry) },
|
|
|
|
|
{ Events.CommitCrime, typeof(CommitCrimeEntry) },
|
|
|
|
|
{ Events.Died, typeof(DiedEntry) },
|
2022-11-25 17:13:17 +01:00
|
|
|
|
{ Events.Disembark, typeof(DisembarkEntry) },
|
2022-11-01 18:01:28 +01:00
|
|
|
|
{ Events.Docked, typeof(DockedEntry) },
|
2022-12-09 17:36:26 +01:00
|
|
|
|
{ Events.DropshipDeploy, typeof(DropshipDeployEntry) },
|
2022-11-25 17:13:17 +01:00
|
|
|
|
{ Events.Embark, typeof(EmbarkEntry) },
|
2022-11-01 18:01:28 +01:00
|
|
|
|
{ Events.FactionKillBond, typeof(FactionKillBondEntry) },
|
2022-11-25 17:13:17 +01:00
|
|
|
|
{ Events.FileHeader, typeof(FileHeaderEntry) },
|
2022-11-01 18:01:28 +01:00
|
|
|
|
{ Events.FSDJump, typeof(FSDJumpEntry) },
|
|
|
|
|
{ Events.HullDamage, typeof(HullDamageEntry) },
|
|
|
|
|
{ Events.LoadGame, typeof(LoadGameEntry) },
|
|
|
|
|
{ Events.Location, typeof(LocationEntry) },
|
|
|
|
|
{ Events.MarketBuy, typeof(MarketBuyEntry) },
|
|
|
|
|
{ Events.MarketSell, typeof(MarketSellEntry) },
|
|
|
|
|
{ Events.MissionAbandoned, typeof(MissionAbandonedEntry) },
|
|
|
|
|
{ Events.MissionAccepted, typeof(MissionAcceptedEntry) },
|
|
|
|
|
{ Events.MissionCompleted, typeof(MissionCompletedEntry) },
|
|
|
|
|
{ Events.MissionFailed, typeof(MissionFailedEntry) },
|
|
|
|
|
{ Events.MissionRedirected, typeof(MissionRedirectedEntry) },
|
2022-11-24 16:28:35 +01:00
|
|
|
|
{ Events.Missions, typeof(MissionsEntry) },
|
2022-11-01 18:01:28 +01:00
|
|
|
|
{ Events.MultiSellExplorationData, typeof(MultiSellExplorationDataEntry) },
|
2024-01-29 19:24:53 +01:00
|
|
|
|
{ Events.Music, typeof(MusicEntry) },
|
2024-11-14 12:14:36 +01:00
|
|
|
|
{ Events.Powerplay, typeof(PowerplayEntry) },
|
2022-12-07 16:06:07 +01:00
|
|
|
|
{ Events.ReceiveText, typeof(ReceiveTextEntry) },
|
2022-11-01 18:01:28 +01:00
|
|
|
|
{ Events.RedeemVoucher, typeof(RedeemVoucherEntry) },
|
|
|
|
|
{ Events.SearchAndRescue, typeof(SearchAndRescueEntry) },
|
2024-11-14 11:31:46 +01:00
|
|
|
|
{ Events.SelfDestruct, typeof(SelfDestructEntry) },
|
2022-11-01 18:01:28 +01:00
|
|
|
|
{ Events.SellExplorationData, typeof(SellExplorationDataEntry) },
|
|
|
|
|
{ Events.SellMicroResources, typeof(SellMicroResourcesEntry) },
|
|
|
|
|
{ Events.SellOrganicData, typeof(SellOrganicDataEntry) },
|
|
|
|
|
{ Events.ShieldState, typeof(ShieldStateEntry) },
|
|
|
|
|
{ Events.ShipTargeted, typeof(ShipTargetedEntry) },
|
2023-05-11 20:02:53 +02:00
|
|
|
|
{ Events.SupercruiseDestinationDrop, typeof(SupercruiseDestinationDropEntry) },
|
2022-11-25 17:13:17 +01:00
|
|
|
|
{ Events.SupercruiseEntry, typeof(SupercruiseEntryEntry) },
|
|
|
|
|
{ Events.SupercruiseExit, typeof(SupercruiseExitEntry) },
|
2022-11-01 18:01:28 +01:00
|
|
|
|
{ Events.UnderAttack, typeof(UnderAttackEntry) },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private string? eventtype = null;
|
|
|
|
|
private string? datetime = null;
|
|
|
|
|
private DateTime timestamp;
|
|
|
|
|
|
|
|
|
|
private string? jsonstr = null;
|
|
|
|
|
protected JObject? json = null;
|
|
|
|
|
|
|
|
|
|
public Entry() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Entry? Parse(string journalline) {
|
2022-11-24 14:18:27 +01:00
|
|
|
|
using (JsonReader reader = new JsonTextReader(new StringReader(journalline))) {
|
|
|
|
|
reader.DateParseHandling = DateParseHandling.None;
|
2023-10-25 11:30:34 +02:00
|
|
|
|
JObject? json = null;
|
|
|
|
|
try {
|
|
|
|
|
json = JObject.Load(reader);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new InvalidJournalEntryException(
|
|
|
|
|
"invalid JSON journal entry: " + journalline,
|
|
|
|
|
e
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-11-24 14:18:27 +01:00
|
|
|
|
if (json == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return Parse(json);
|
2022-11-01 18:01:28 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Entry? Parse(JObject json) {
|
|
|
|
|
string? event_name = json.Value<string?>("event");
|
|
|
|
|
|
|
|
|
|
if (event_name == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
classes.TryGetValue(event_name, out Type? classhandler);
|
|
|
|
|
if (classhandler == null) {
|
|
|
|
|
// No specific handler available so use base class
|
|
|
|
|
classhandler = typeof(Entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Entry? obj = (Entry?)Activator.CreateInstance(classhandler);
|
|
|
|
|
if (obj == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
obj.InternalInitialise(json);
|
|
|
|
|
obj.Initialise();
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InternalInitialise(JObject jobject) {
|
|
|
|
|
this.json = jobject;
|
|
|
|
|
this.jsonstr = json.ToString(formatting: Formatting.None);
|
|
|
|
|
|
|
|
|
|
this.eventtype = json.Value<string>("event");
|
|
|
|
|
// Do not change this or JSON parser will conver the string to
|
|
|
|
|
// datetime object and call .ToString() on it which changes the
|
|
|
|
|
// date and time format
|
|
|
|
|
this.datetime = json.GetValue("timestamp")?.ToString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(this.datetime)) {
|
2022-11-25 17:36:41 +01:00
|
|
|
|
this.timestamp = DateTime.Parse(this.datetime, null, System.Globalization.DateTimeStyles.RoundtripKind);
|
2022-11-01 18:01:28 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void Initialise() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Is(string eventtype) {
|
|
|
|
|
if (eventtype == null || this.eventtype == null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String.Equals(this.eventtype, eventtype, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string? Event {
|
|
|
|
|
get { return eventtype; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DateTime Timestamp {
|
|
|
|
|
get { return timestamp; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JObject JSON {
|
|
|
|
|
get { return this.json ?? new JObject(); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
return jsonstr ?? "";
|
|
|
|
|
}
|
|
|
|
|
}
|