Skip to content

Commit ce17051

Browse files
authored
Clear list, PermaStickies and Editable settings
Clear permasticky settings
2 parents 29ece36 + 7b70e6b commit ce17051

File tree

17 files changed

+305
-30
lines changed

17 files changed

+305
-30
lines changed
245 Bytes
Loading
542 Bytes
Loading
489 Bytes
Loading
559 Bytes
Loading

ShowKSP2Events/swinfo.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"name": "ShowKSP2Events",
55
"description": "Shows KSP2 events being triggered",
66
"source": "https://github.com/Falki-git/ShowKSP2Events",
7-
"version": "1.1.2",
7+
"version": "1.2.0",
88
"version_check": "https://raw.githubusercontent.com/Falki-git/ShowKSP2Events/master/ShowKSP2Events/swinfo.json",
99
"dependencies": [
1010
{
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using BepInEx.Logging;
2+
using Newtonsoft.Json;
3+
using System.Reflection;
4+
5+
namespace ShowKSP2Events
6+
{
7+
[JsonObject(MemberSerialization.OptIn)]
8+
internal class ExportMessages
9+
{
10+
[JsonProperty]
11+
internal string DateTimeCreated;
12+
[JsonProperty]
13+
internal List<MessageInfo> Messages;
14+
15+
private static ManualLogSource _logger = BepInEx.Logging.Logger.CreateLogSource("ShowKSP2Events.ExportMessages");
16+
private static int _fileNumber = 0;
17+
private static string _path => Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), $"ShowKSP2Events_export({_fileNumber}).json");
18+
19+
internal ExportMessages(List<MessageInfo> messages)
20+
{
21+
Messages = messages;
22+
DateTimeCreated = DateTime.Now.ToString();
23+
}
24+
25+
internal void Export()
26+
{
27+
try
28+
{
29+
while (File.Exists(_path))
30+
_fileNumber++;
31+
32+
var data = new SettingsData();
33+
File.WriteAllText(_path, JsonConvert.SerializeObject(this));
34+
_logger.LogInfo("Export successful.");
35+
}
36+
catch (Exception ex)
37+
{
38+
_logger.LogError("Error trying to export data. Error description: " + ex);
39+
}
40+
}
41+
}
42+
}

ShowKSP2EventsProject/MessageInfo.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
using UnityEngine;
1+
using Newtonsoft.Json;
2+
using UnityEngine;
23

34
namespace ShowKSP2Events
45
{
56
internal class MessageInfo
67
{
78
internal Type Type;
9+
[JsonProperty]
10+
internal string TypeName => Type.Name;
11+
[JsonProperty]
812
internal int Hits;
913
internal double TimeOfLastHit;
14+
[JsonProperty]
15+
internal string DateTimeOfLastHit;
1016
internal bool IsSticky;
11-
internal bool IsPermaSticky; // TODO: implement
17+
internal bool IsPermaSticky;
1218
internal bool IsStale;
1319
internal bool JustHit => Time.time - TimeOfLastHit < Settings.JustHit;
1420
}

ShowKSP2EventsProject/MessageListener.cs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,12 @@ private void MessageReceived(MessageCenterMessage messageReceived)
5858
var messageInfo = Messages.Find(m => m.Type == messageReceived.GetType());
5959
messageInfo.Hits++;
6060
messageInfo.TimeOfLastHit = Time.time;
61-
61+
messageInfo.DateTimeOfLastHit = DateTime.Now.ToString();
62+
messageInfo.IsSticky = true;
63+
messageInfo.IsStale = false;
64+
6265
if (!messageInfo.IsPermaSticky && !messageInfo.IsSticky)
63-
{
64-
messageInfo.IsSticky = true;
65-
messageInfo.IsStale = false;
6666
MoveToBelowLastSticky(messageInfo);
67-
}
68-
69-
//Logger.LogInfo($"Message Received: {messageReceived.GetType()}");
7067
}
7168

7269
private void MoveToBelowLastSticky(MessageInfo message)
@@ -75,20 +72,28 @@ private void MoveToBelowLastSticky(MessageInfo message)
7572
int lastStickyIndex = Messages.FindLastIndex(m => m.IsSticky || m.IsPermaSticky);
7673
Messages.Insert(lastStickyIndex == -1 ? 0 : lastStickyIndex + 1, message);
7774
}
78-
75+
76+
private void MoveToBelowLastPermaSticky(MessageInfo message)
77+
{
78+
Messages.Remove(message);
79+
int lastPermaStickyIndex = Messages.FindLastIndex(m => m.IsPermaSticky);
80+
Messages.Insert(lastPermaStickyIndex == -1 ? 0 : lastPermaStickyIndex + 1, message);
81+
}
82+
7983
internal void UnSticky(MessageInfo message)
8084
{
8185
var messageInfo = Messages.Find(m => m.Type == message.Type);
8286
messageInfo.IsSticky = false;
8387

84-
this.MoveToBelowLastSticky(messageInfo);
88+
if (!message.IsPermaSticky)
89+
this.MoveToBelowLastSticky(messageInfo);
8590
}
8691

8792
internal void CheckStickies()
8893
{
8994
foreach (var message in Messages)
9095
{
91-
if (!message.IsPermaSticky && message.IsSticky && Time.time - message.TimeOfLastHit > Settings.StickyDuration)
96+
if (message.IsSticky && Time.time - message.TimeOfLastHit > Settings.StickyDuration)
9297
{
9398
UnSticky(message);
9499
}
@@ -97,13 +102,38 @@ internal void CheckStickies()
97102

98103
internal void CheckStales()
99104
{
100-
foreach (var message in Messages.Where(m => !m.IsStale))
105+
foreach (var message in Messages.Where(m => !m.IsStale && !m.IsPermaSticky))
101106
{
102107
if (Time.time - message.TimeOfLastHit > Settings.DurationTillPruned)
103108
{
104109
message.IsStale = true;
105110
}
106111
}
107112
}
113+
114+
internal void OnPermaStickyClicked(Type messageType)
115+
{
116+
var message = Messages.Find(m => m.Type == messageType);
117+
message.IsPermaSticky = !message.IsPermaSticky;
118+
MoveToBelowLastPermaSticky(message);
119+
}
120+
121+
internal void OnClearClicked()
122+
{
123+
foreach (var message in Messages)
124+
{
125+
message.Hits = 0;
126+
message.TimeOfLastHit = 0;
127+
message.IsPermaSticky = false;
128+
message.IsSticky = false;
129+
message.IsStale = true;
130+
}
131+
}
132+
133+
internal void OnExportClicked()
134+
{
135+
var x = new ExportMessages(Messages.FindAll(m => m.Hits > 0));
136+
x.Export();
137+
}
108138
}
109139
}

ShowKSP2EventsProject/Settings.cs

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,72 @@
1-
namespace ShowKSP2Events
1+
using BepInEx.Logging;
2+
using Newtonsoft.Json;
3+
using System.Reflection;
4+
5+
namespace ShowKSP2Events
26
{
3-
public static class Settings
7+
public class Settings
48
{
9+
public static float JustHit = 1.0f;
510
public static float StickyDuration = 20.0f;
611
public static float DurationTillPruned = 60.0f;
7-
public static float JustHit = 1.0f;
12+
13+
private static ManualLogSource _logger = Logger.CreateLogSource("ShowKSP2Events.Settings");
14+
15+
private static string _path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Settings.json");
16+
17+
public static void Save()
18+
{
19+
try
20+
{
21+
var data = new SettingsData();
22+
File.WriteAllText(_path, JsonConvert.SerializeObject(data));
23+
_logger.LogInfo("Settings saved successfully.");
24+
}
25+
catch (Exception ex)
26+
{
27+
_logger.LogError("Error trying to save settings. Error description: " + ex);
28+
}
29+
}
30+
31+
public static void Load()
32+
{
33+
try
34+
{
35+
var data = JsonConvert.DeserializeObject<SettingsData>(File.ReadAllText(_path));
36+
37+
Settings.StickyDuration = data.StickyDuration;
38+
Settings.DurationTillPruned = data.DurationTillPruned;
39+
Settings.JustHit = data.JustHit;
40+
41+
_logger.LogInfo("Settings loaded successfully.");
42+
}
43+
catch (FileNotFoundException ex)
44+
{
45+
_logger.LogWarning($"Settings file was not found at the expected location. Mod will continue with default settings. Full description:\n" + ex);
46+
47+
}
48+
catch (Exception ex)
49+
{
50+
_logger.LogError("Error trying to load settings. Full error description:\n" + ex);
51+
}
52+
}
53+
}
54+
55+
[JsonObject(MemberSerialization.OptIn)]
56+
internal class SettingsData
57+
{
58+
[JsonProperty]
59+
internal float StickyDuration;
60+
[JsonProperty]
61+
internal float DurationTillPruned;
62+
[JsonProperty]
63+
internal float JustHit;
64+
65+
internal SettingsData()
66+
{
67+
StickyDuration = Settings.StickyDuration;
68+
DurationTillPruned = Settings.DurationTillPruned;
69+
JustHit = Settings.JustHit;
70+
}
871
}
972
}

ShowKSP2EventsProject/ShowKSP2Events.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class ShowKSP2Events : BaseSpaceWarpPlugin
2323
private const string ToolbarFlightButtonID = "BTN-ShowKSP2EventsFlight";
2424
private const string ToolbarOABButtonID = "BTN-ShowKSP2EventsOAB";
2525

26-
private MessageListener _messageListener = new ();
26+
private MessageListener _messageListener = new();
2727
private UI _ui = new();
2828

2929
public override void OnInitialized()
@@ -51,9 +51,10 @@ public override void OnInitialized()
5151
GameObject.Find(ToolbarOABButtonID)?.GetComponent<UIValue_WriteBool_Toggle>()?.SetValue(isOpen);
5252
}
5353
);
54-
54+
Settings.Load();
55+
Styles.Initialize();
56+
Textures.Initialize(this);
5557
_messageListener.InitializeSubscriptions();
56-
Styles.InitializeStyles();
5758
}
5859

5960
private void OnGUI()

0 commit comments

Comments
 (0)