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 ;
6
7
7
8
namespace IndustrialPark
8
9
{
9
10
public static class DiscordRPCController
10
11
{
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
-
16
12
public static DiscordRpcClient client ;
17
13
18
14
public static System . Windows . Forms . Timer timer1 ;
19
15
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
+
20
24
internal static void ToggleDiscordRichPresence ( bool value )
21
25
{
22
26
if ( value )
23
27
{
24
- Initialize ( ) ;
28
+ try
29
+ {
30
+ Initialize ( ) ;
31
+ }
32
+ catch { }
25
33
}
26
34
else
27
35
{
28
36
try
29
37
{
30
38
if ( client != null )
31
39
client . Dispose ( ) ;
32
- if ( timer1 != null )
33
- timer1 . Enabled = false ;
34
40
}
35
41
catch { }
42
+ if ( timer1 != null )
43
+ timer1 . Enabled = false ;
36
44
}
37
45
}
38
46
@@ -44,30 +52,106 @@ public static void Initialize()
44
52
} ;
45
53
timer1 . Tick += timer1_Tick ;
46
54
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" ) ;
53
60
client . Initialize ( ) ;
54
61
timer1 . Start ( ) ;
55
- var activeWindowInit = "a project" ;
56
- setPresence ( activeWindowInit ) ;
62
+ setPresence ( idling : true ) ;
57
63
}
58
64
59
- public static void setPresence ( string activeWindow )
65
+ public static void setPresence ( ArchiveEditor archiveEditor = null , bool idling = false )
60
66
{
61
67
try
62
68
{
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
+
63
144
client . SetPresence ( new RichPresence ( )
64
145
{
65
- Details = "Editing a Level" ,
66
- State = "Editing " + activeWindow ,
146
+ Details = details ,
147
+ State = state ,
148
+ Timestamps = currentTimestamp ,
67
149
Assets = new Assets ( )
68
150
{
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
71
155
}
72
156
} ) ;
73
157
}
@@ -77,34 +161,54 @@ public static void setPresence(string activeWindow)
77
161
}
78
162
}
79
163
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
+
80
189
public static void timer1_Tick ( object sender , EventArgs e )
81
190
{
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
83
193
timer1 . Stop ( ) ;
84
194
GetActiveWindow ( ) ;
85
195
timer1 . Start ( ) ;
86
196
}
87
197
88
198
public static void GetActiveWindow ( )
89
199
{
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 )
100
203
{
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 ) )
103
206
{
104
- var activeWindow = activeWindowTemp ;
105
- setPresence ( activeWindow ) ;
207
+ setPresence ( activeEditor ) ;
106
208
}
107
209
}
210
+ else if ( archiveEditors . Count == 0 )
211
+ setPresence ( idling : true ) ;
108
212
}
109
213
}
110
214
}
0 commit comments