Skip to content

Commit

Permalink
Merge pull request #7 from Falki-git/1.3.0
Browse files Browse the repository at this point in the history
1.3.0
  • Loading branch information
Falki-git authored Aug 5, 2023
2 parents 494c7c9 + f4884e9 commit 383eeb3
Show file tree
Hide file tree
Showing 23 changed files with 347 additions and 116 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This is a mod that helps modders mod.
- **Yellow event** - event that was recently triggered (< 20 sec)
- **Gray event** - event that hasn't triggered for a while (< 60 sec)

![screenshot](https://i.imgur.com/Cb1D1P7.png)
![screenshot](https://i.imgur.com/oCfA2zs.png)

## Installation
Extract the contents of the .zip into your KSP2 installation folder.
Expand All @@ -34,5 +34,8 @@ private void YourMethodThatWillHandleTheEvent(MessageCenterMessage obj)

## Other features
- pin events to the top
- ignore events you're not interested in
- log each triggering of an event to the KSP log
- get a list of all events that KSP2 can trigger
- customize timings of colors and purges
- export list to a JSON file inside mod folder
4 changes: 2 additions & 2 deletions ShowKSP2Events.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{41FA02E6-F455-4B46-B838-3F5BC5A888E3}.Debug|Any CPU.ActiveCfg = Release|Any CPU
{41FA02E6-F455-4B46-B838-3F5BC5A888E3}.Debug|Any CPU.Build.0 = Release|Any CPU
{41FA02E6-F455-4B46-B838-3F5BC5A888E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{41FA02E6-F455-4B46-B838-3F5BC5A888E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41FA02E6-F455-4B46-B838-3F5BC5A888E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41FA02E6-F455-4B46-B838-3F5BC5A888E3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
Expand Down
Binary file added ShowKSP2Events/assets/images/cross.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShowKSP2Events/assets/images/plus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 21 additions & 20 deletions ShowKSP2Events/swinfo.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
{
"mod_id": "ShowKSP2Events",
"author": "Falki",
"name": "ShowKSP2Events",
"description": "Shows KSP2 events being triggered",
"source": "https://github.com/Falki-git/ShowKSP2Events",
"version": "1.2.0",
"version_check": "https://raw.githubusercontent.com/Falki-git/ShowKSP2Events/master/ShowKSP2Events/swinfo.json",
"dependencies": [
{
"id": "SpaceWarp",
"version": {
"min": "1.0.1",
"max": "*"
}
}
],
"ksp2_version": {
"min": "0.1.0",
"max": "*"
}
"spec": "1.3",
"mod_id": "com.github.falki.showksp2events",
"author": "Falki",
"name": "ShowKSP2Events",
"description": "Shows KSP2 events being triggered",
"source": "https://github.com/Falki-git/ShowKSP2Events",
"version": "1.3.0",
"version_check": "https://raw.githubusercontent.com/Falki-git/ShowKSP2Events/master/ShowKSP2Events/swinfo.json",
"dependencies": [
{
"id": "com.github.x606.spacewarp",
"version": {
"min": "1.0.1",
"max": "*"
}
}
],
"ksp2_version": {
"min": "0.1.0",
"max": "*"
}
}
19 changes: 13 additions & 6 deletions ShowKSP2EventsProject/ExportMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,30 @@
namespace ShowKSP2Events
{
[JsonObject(MemberSerialization.OptIn)]
internal class ExportMessages
public class ExportMessages
{
[JsonProperty]
internal string DateTimeCreated;
public string DateTimeCreated;
[JsonProperty]
internal List<MessageInfo> Messages;
public List<MessageInfo> Messages;

private static ManualLogSource _logger = BepInEx.Logging.Logger.CreateLogSource("ShowKSP2Events.ExportMessages");
private static int _fileNumber = 0;
private static string _path => Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), $"ShowKSP2Events_export({_fileNumber}).json");

internal ExportMessages(List<MessageInfo> messages)
public ExportMessages(List<MessageInfo> messages)
{
Messages = messages;
DateTimeCreated = DateTime.Now.ToString();
}

internal void Export()
public void Export()
{
try
{
while (File.Exists(_path))
_fileNumber++;

var data = new SettingsData();
File.WriteAllText(_path, JsonConvert.SerializeObject(this));
_logger.LogInfo("Export successful.");
}
Expand All @@ -38,5 +37,13 @@ internal void Export()
_logger.LogError("Error trying to export data. Error description: " + ex);
}
}

public void WriteAllToLog()
{
_logger.LogInfo($"Writing all {Messages.Count} messages to log...");

foreach (var message in Messages)
_logger.LogInfo($"{message.TypeName}");
}
}
}
22 changes: 12 additions & 10 deletions ShowKSP2EventsProject/MessageInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@

namespace ShowKSP2Events
{
internal class MessageInfo
public class MessageInfo
{
internal Type Type;
public Type Type;
[JsonProperty]
internal string TypeName => Type.Name;
public string TypeName => Type.Name;
[JsonProperty]
internal int Hits;
internal double TimeOfLastHit;
public int Hits;
public double TimeOfLastHit;
[JsonProperty]
internal string DateTimeOfLastHit;
internal bool IsSticky;
internal bool IsPermaSticky;
internal bool IsStale;
internal bool JustHit => Time.time - TimeOfLastHit < Settings.JustHit;
public string DateTimeOfLastHit;
public bool IsIgnored;
public bool IsLogging;
public bool IsSticky;
public bool IsPermaSticky;
public bool IsStale;
public bool JustHit => Time.time - TimeOfLastHit < Settings.JustHit;
}
}
83 changes: 65 additions & 18 deletions ShowKSP2EventsProject/MessageListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,25 @@

namespace ShowKSP2Events
{
internal class MessageListener
{
private ManualLogSource Logger = BepInEx.Logging.Logger.CreateLogSource("ShowKSP2Events.MessageListener");
public class MessageListener
{
private ManualLogSource _logger = BepInEx.Logging.Logger.CreateLogSource("ShowKSP2Events.MessageListener");
private static MessageListener _instance;

internal List<MessageInfo> Messages = new();
public List<MessageInfo> Messages = new();

public MessageListener ()
{ }

public static MessageListener Instance
{
get
{
if (_instance == null)
_instance = new MessageListener();
return _instance;
}
}

internal void InitializeSubscriptions()
{
Expand Down Expand Up @@ -41,11 +55,11 @@ internal void InitializeSubscriptions()
}
catch (Exception ex)
{
Logger.LogError(ex);
_logger.LogError(ex);
}
}

Logger.LogInfo($"Subscriptions initialized. No. of subscriptions: {Messages.Count}");
_logger.LogInfo($"Subscriptions initialized. No. of subscriptions: {Messages.Count}");
}

private void AddSubscription(Type messageType)
Expand All @@ -62,6 +76,9 @@ private void MessageReceived(MessageCenterMessage messageReceived)
messageInfo.IsSticky = true;
messageInfo.IsStale = false;

if (messageInfo.IsLogging)
_logger.LogInfo($"Message {messageInfo.TypeName} triggered at {messageInfo.DateTimeOfLastHit}. Hit number: {messageInfo.Hits}.");

if (!messageInfo.IsPermaSticky && !messageInfo.IsSticky)
MoveToBelowLastSticky(messageInfo);
}
Expand All @@ -73,14 +90,14 @@ private void MoveToBelowLastSticky(MessageInfo message)
Messages.Insert(lastStickyIndex == -1 ? 0 : lastStickyIndex + 1, message);
}

private void MoveToBelowLastPermaSticky(MessageInfo message)
internal void MoveToBelowLastPermaSticky(MessageInfo message)
{
Messages.Remove(message);
int lastPermaStickyIndex = Messages.FindLastIndex(m => m.IsPermaSticky);
Messages.Insert(lastPermaStickyIndex == -1 ? 0 : lastPermaStickyIndex + 1, message);
}

internal void UnSticky(MessageInfo message)
public void UnSticky(MessageInfo message)
{
var messageInfo = Messages.Find(m => m.Type == message.Type);
messageInfo.IsSticky = false;
Expand Down Expand Up @@ -111,29 +128,59 @@ internal void CheckStales()
}
}

internal void OnPermaStickyClicked(Type messageType)
public void OnExportClicked()
{
var message = Messages.Find(m => m.Type == messageType);
message.IsPermaSticky = !message.IsPermaSticky;
MoveToBelowLastPermaSticky(message);
var x = new ExportMessages(Messages.FindAll(m => m.Hits > 0));
x.Export();
}

public void OnWriteAllToLogClicked()
{
var x = new ExportMessages(Messages);
x.WriteAllToLog();
}

internal void OnClearClicked()
public void OnClearClicked()
{
foreach (var message in Messages)
{
message.Hits = 0;
message.TimeOfLastHit = 0;
message.IsPermaSticky = false;
message.IsSticky = false;
message.IsStale = true;
}

_logger.LogInfo($"Cleared all messages.");
}

internal void OnExportClicked()
public void OnPermaStickyClicked(Type messageType)
{
var x = new ExportMessages(Messages.FindAll(m => m.Hits > 0));
x.Export();
var message = Messages.Find(m => m.Type == messageType);
message.IsPermaSticky = !message.IsPermaSticky;
MoveToBelowLastPermaSticky(message);
Settings.Save();
_logger.LogInfo($"Toggled pinning for {message.TypeName}.");
}

public void OnIgnoredClicked(MessageInfo message)
{
message.IsIgnored = true;
_logger.LogInfo($"Message {message.TypeName} ignored.");
Settings.Save();
}

public void OnLoggingClicked(MessageInfo message)
{
message.IsLogging = !message.IsLogging;
_logger.LogInfo($"Toggled logging for {message.TypeName}.");
Settings.Save();
}

public void OnUnignoreMessageClicked(MessageInfo message)
{
message.IsIgnored = false;
_logger.LogInfo($"Message {message.TypeName} returned to active messages.");
Settings.Save();
}
}
}
}
49 changes: 49 additions & 0 deletions ShowKSP2EventsProject/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ public static void Load()
Settings.DurationTillPruned = data.DurationTillPruned;
Settings.JustHit = data.JustHit;

// Load data for messages that have been modified (ignored, logging or stickied)
if (data.SavedMessages.Count() > 0)
{
var savedMessages = MessageListener.Instance.Messages
.FindAll(m => data.SavedMessages.Select(s => s.TypeName).Contains(m.TypeName));

foreach (var message in savedMessages)
{
var savedData = data.SavedMessages.Find(s => s.TypeName == message.TypeName);
message.IsIgnored = savedData.IsIgnored;
message.IsLogging = savedData.IsLogging;
if(savedData.IsPermaSticky)
{
message.IsPermaSticky = true;
MessageListener.Instance.MoveToBelowLastPermaSticky(message);
}
}
}

_logger.LogInfo("Settings loaded successfully.");
}
catch (FileNotFoundException ex)
Expand All @@ -61,12 +80,42 @@ internal class SettingsData
internal float DurationTillPruned;
[JsonProperty]
internal float JustHit;
[JsonProperty]
internal List<SettingsMessageData> SavedMessages;

internal SettingsData()
{
StickyDuration = Settings.StickyDuration;
DurationTillPruned = Settings.DurationTillPruned;
JustHit = Settings.JustHit;

var savedMessages = MessageListener.Instance.Messages
.Where(m => m.IsIgnored || m.IsLogging || m.IsPermaSticky);

if (savedMessages.Count() > 0)
{
SavedMessages = new();

foreach (var m in savedMessages)
SavedMessages.Add(new() {
TypeName = m.TypeName,
IsIgnored = m.IsIgnored,
IsLogging = m.IsLogging,
IsPermaSticky = m.IsPermaSticky
});
}
}
}

internal class SettingsMessageData
{
[JsonProperty]
internal string TypeName;
[JsonProperty]
internal bool IsIgnored;
[JsonProperty]
internal bool IsLogging;
[JsonProperty]
internal bool IsPermaSticky;
}
}
Loading

0 comments on commit 383eeb3

Please sign in to comment.