Skip to content

Commit 1f1126d

Browse files
authored
Merge pull request igorseabra4#36 from Suprnova/discord-rework
DiscordRPC rework and logging hotfix
2 parents 32451df + 20838a1 commit 1f1126d

File tree

2 files changed

+147
-43
lines changed

2 files changed

+147
-43
lines changed

IndustrialPark/MainForm/MainForm.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private void newToolStripMenuItem1_Click(object sender, EventArgs e)
262262
}
263263

264264
// updates presence so that it doesn't get stuck on the previously focused window name
265-
DiscordRPCController.setPresence("a project");
265+
DiscordRPCController.setPresence(idling: true);
266266

267267
currentProjectPath = null;
268268
ApplyProject(new ProjectJson());
@@ -283,7 +283,7 @@ private void openToolStripMenuItem_Click(object sender, EventArgs e)
283283
}
284284

285285
// updates presence so that it doesn't get stuck on the previously focused window name
286-
DiscordRPCController.setPresence("a project");
286+
DiscordRPCController.setPresence(idling: true);
287287

288288
OpenFileDialog openFile = new OpenFileDialog()
289289
{ Filter = "JSON files|*.json" };
+145-41
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,46 @@
1-
using DiscordRPC;
2-
using DiscordRPC.Logging;
3-
using System;
4-
using System.Runtime.InteropServices;
5-
using System.Text;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
using DiscordRPC;
6+
using HipHopFile;
67

78
namespace IndustrialPark
89
{
910
public static class DiscordRPCController
1011
{
11-
[DllImport("user32.dll")]
12-
private static extern IntPtr GetForegroundWindow();
13-
[DllImport("user32.dll")]
14-
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
15-
1612
public static DiscordRpcClient client;
1713

1814
public static System.Windows.Forms.Timer timer1;
1915

16+
public static List<ArchiveEditor> archiveEditors = Program.MainForm.archiveEditors;
17+
18+
public static string currentArchiveEditor = string.Empty;
19+
20+
public static bool alreadyIdling = false;
21+
22+
public static Timestamps currentTimestamp = null;
23+
2024
internal static void ToggleDiscordRichPresence(bool value)
2125
{
2226
if (value)
2327
{
24-
Initialize();
28+
try
29+
{
30+
Initialize();
31+
}
32+
catch { }
2533
}
2634
else
2735
{
2836
try
2937
{
3038
if (client != null)
3139
client.Dispose();
32-
if (timer1 != null)
33-
timer1.Enabled = false;
3440
}
3541
catch { }
42+
if (timer1 != null)
43+
timer1.Enabled = false;
3644
}
3745
}
3846

@@ -44,30 +52,106 @@ public static void Initialize()
4452
};
4553
timer1.Tick += timer1_Tick;
4654

47-
/*this application ID is assigned to Suprnova#0001 on Discord
48-
it can be replaced with any other application ID as long as the name is "Industrial Park" and the image of the logo has the name of "icon"*/
49-
client = new DiscordRpcClient("734104261876514836")
50-
{
51-
Logger = new ConsoleLogger() { Level = LogLevel.Warning }
52-
};
55+
// this application ID is assigned to Suprnova#0001 on Discord
56+
// it can be replaced with any other application ID as long as
57+
// the name is "Industrial Park" and the image of the logo has
58+
// the name of "icon"
59+
client = new DiscordRpcClient("734104261876514836");
5360
client.Initialize();
5461
timer1.Start();
55-
var activeWindowInit = "a project";
56-
setPresence(activeWindowInit);
62+
setPresence(idling: true);
5763
}
5864

59-
public static void setPresence(string activeWindow)
65+
public static void setPresence(ArchiveEditor archiveEditor = null, bool idling = false)
6066
{
6167
try
6268
{
69+
string details = "Idling";
70+
string state, largeImageKey, largeImageText, game;
71+
state = largeImageKey = largeImageText = game = string.Empty;
72+
string smallImageKey = "icon";
73+
string smallImageText = $"Industrial Park {new IPversion().version}";
74+
75+
if (!idling)
76+
{
77+
alreadyIdling = false;
78+
if (archiveEditor == null)
79+
// .SingleOrDefault() works best here but it's not worth throwing an
80+
// exception just to update the presence.
81+
archiveEditor = archiveEditors.Where(x => x.ContainsFocus).FirstOrDefault();
82+
83+
// no point updating if it wasn't changed
84+
if (currentArchiveEditor == archiveEditor.Text)
85+
return;
86+
// additionally checking to maintain timestamp if in same level but different file
87+
if (currentArchiveEditor.Substring(0, currentArchiveEditor.Length > 4 ? 4 : currentArchiveEditor.Length) != archiveEditor.Text.Substring(0, archiveEditor.Text.Length > 4 ? 4 : archiveEditor.Text.Length))
88+
{
89+
currentTimestamp = Timestamps.Now;
90+
}
91+
currentArchiveEditor = archiveEditor.Text;
92+
93+
game = gameHandler(archiveEditor);
94+
95+
switch (game)
96+
{
97+
case "Scooby":
98+
largeImageText = "Scooby-Doo: Night of 100 Frights";
99+
break;
100+
case "BFBB":
101+
largeImageText = "SpongeBob SquarePants: Battle for Bikini Bottom";
102+
break;
103+
case "TSSM":
104+
largeImageText = "The SpongeBob SquarePants Movie Game";
105+
break;
106+
case "Incredibles":
107+
largeImageText = "The Incredibles";
108+
break;
109+
case "ROTU":
110+
largeImageText = "The Incredibles: Rise of the Underminer";
111+
break;
112+
case "Ratatouille":
113+
largeImageText = "Ratatouille (Prototype)";
114+
break;
115+
case "Unknown":
116+
default:
117+
largeImageKey = smallImageKey;
118+
largeImageText = smallImageText;
119+
smallImageKey = "";
120+
smallImageText = "";
121+
break;
122+
}
123+
124+
// uses "a" or "an" depending on the game's first char.
125+
// (I = Incredibles, U = Unknown, RO = ROTU)
126+
// If no match, it's OK to prefix with "a".
127+
details = $"Editing {("IU".IndexOf(game.ElementAt(0)) >= 0 || game.StartsWith("RO") ? $"an {game}" : $"a {game}")} level";
128+
129+
state = $"{archiveEditor.Text}{(archiveEditor.archive.platform != Platform.Unknown ? $" - {archiveEditor.archive.platform}" : "")}";
130+
}
131+
else
132+
{
133+
if (alreadyIdling)
134+
return;
135+
currentArchiveEditor = string.Empty;
136+
alreadyIdling = true;
137+
currentTimestamp = Timestamps.Now;
138+
largeImageKey = smallImageKey;
139+
largeImageText = smallImageText;
140+
smallImageKey = "";
141+
smallImageText = "";
142+
}
143+
63144
client.SetPresence(new RichPresence()
64145
{
65-
Details = "Editing a Level",
66-
State = "Editing " + activeWindow,
146+
Details = details,
147+
State = state,
148+
Timestamps = currentTimestamp,
67149
Assets = new Assets()
68150
{
69-
LargeImageKey = "icon",
70-
LargeImageText = "Industrial Park level editor"
151+
LargeImageKey = (largeImageKey == "icon" ? largeImageKey : game.ToLower()),
152+
LargeImageText = largeImageText,
153+
SmallImageKey = smallImageKey,
154+
SmallImageText = smallImageText
71155
}
72156
});
73157
}
@@ -77,34 +161,54 @@ public static void setPresence(string activeWindow)
77161
}
78162
}
79163

164+
private static string gameHandler(ArchiveEditor archiveEditor)
165+
{
166+
Game game = archiveEditor.archive.game;
167+
switch (game)
168+
{
169+
case Game.Scooby:
170+
case Game.BFBB:
171+
return game.ToString();
172+
default:
173+
switch (ArchiveEditor.GetGameFromGameConfigIni(archiveEditor.GetCurrentlyOpenFileName()))
174+
{
175+
case 3:
176+
return "TSSM";
177+
case 4:
178+
return "Incredibles";
179+
case 5:
180+
return "ROTU";
181+
case 6:
182+
return "Ratatouille";
183+
default:
184+
return "Unknown";
185+
}
186+
}
187+
}
188+
80189
public static void timer1_Tick(object sender, EventArgs e)
81190
{
82-
//timer that elapses every second to check for active window updates and adjust presence if needed
191+
// timer that elapses every second to check for active window updates
192+
// and adjust presence if needed
83193
timer1.Stop();
84194
GetActiveWindow();
85195
timer1.Start();
86196
}
87197

88198
public static void GetActiveWindow()
89199
{
90-
var activeWindowTemp = "null";
91-
const int nChars = 256;
92-
IntPtr handle;
93-
StringBuilder Buff = new StringBuilder(nChars);
94-
handle = GetForegroundWindow();
95-
if (GetWindowText(handle, Buff, nChars) > 0)
96-
{
97-
activeWindowTemp = Buff.ToString();
98-
}
99-
if (activeWindowTemp != "null")
200+
var activeEditor = archiveEditors.Where(x => x.ContainsFocus).FirstOrDefault();
201+
var activeEditorName = activeEditor?.Text;
202+
if (activeEditorName != null)
100203
{
101-
//ensures that it only updates to archives
102-
if (activeWindowTemp.EndsWith(".hip") || (activeWindowTemp.EndsWith(".hop")) || (activeWindowTemp.EndsWith(".HIP")) || (activeWindowTemp.EndsWith(".HOP")))
204+
// ensures that it only updates to archives
205+
if (activeEditorName.EndsWith(".hip", StringComparison.InvariantCultureIgnoreCase) || activeEditorName.EndsWith(".hop", StringComparison.InvariantCultureIgnoreCase))
103206
{
104-
var activeWindow = activeWindowTemp;
105-
setPresence(activeWindow);
207+
setPresence(activeEditor);
106208
}
107209
}
210+
else if (archiveEditors.Count == 0)
211+
setPresence(idling: true);
108212
}
109213
}
110214
}

0 commit comments

Comments
 (0)