forked from DMDTools/DOFConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
155 lines (146 loc) · 6.07 KB
/
Program.cs
File metadata and controls
155 lines (146 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// This file contains plugins for LaunchBox/BigBox to integrate with DOFLinx
// Reference documentation : https://pluginapi.launchbox-app.com/html/4cf923f7-940c-5735-83de-04107a6ae0e6.htm
namespace DOFConnect
{
using Unbroken.LaunchBox.Plugins;
using Unbroken.LaunchBox.Plugins.Data;
using System.IO.Pipes;
using System.Text.Json;
using System.Reflection;
// Class to handle system events in LaunchBox/BigBox
public partial class SystemEvents : ISystemEventsPlugin
{
string gCurrentGame = "";
string gCurrentPlatform = "platforms";
string gCurrentCategory = "";
Boolean UseEmulator = true;
public void OnEventRaised(string eventType)
{
// Event types : https://pluginapi.launchbox-app.com/html/3e3603e5-bab6-e510-689c-ee35c0f5f694.htm
switch (eventType)
{
case SystemEventTypes.PluginInitialized:
// Load the configuration
LoadConfig();
break;
case SystemEventTypes.LaunchBoxStartupCompleted:
// Play cabinet's marquee, per the configuration
sendMenuNav("STARTUP");
break;
case SystemEventTypes.LaunchBoxShutdownBeginning:
case SystemEventTypes.BigBoxShutdownBeginning:
sendMenuNav("BLANK");
break;
case SystemEventTypes.GameStarting:
// Play "launching" GIF
sendMenuNav("LAUNCH");
break;
case SystemEventTypes.GameExited:
// Restore Game Marquee
sendMenuRom(gCurrentPlatform, gCurrentGame);
break;
case SystemEventTypes.SelectionChanged:
HandleSelectionChanged();
break;
default:
break;
}
}
public class DOFConnectConfig
{
public string CategorySource { get; set; }
}
// Load up the configuration details
private void LoadConfig()
{
using (StreamReader r = new StreamReader(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)+"\\DOFConnect.json"))
{
string json = r.ReadToEnd();
DOFConnectConfig config = JsonSerializer.Deserialize<DOFConnectConfig>(json);
if (config.CategorySource == "Platform") UseEmulator = false;
sendMsg($"INFO=DOFConnect v3 category set to {config.CategorySource}");
}
}
// Helper method to handle selection changes
private void HandleSelectionChanged()
{
// Depending if this is a new platform, or a new game, show platform marquee or game marquee
// Generate a DOFLinx message MENU_ROM="MAME,qbert"
IPlatform selectedPlatform = PluginHelper.StateManager.GetSelectedPlatform();
IGame[] selectedGames = PluginHelper.StateManager.GetAllSelectedGames();
if (selectedGames != null && selectedGames.Length > 0)
{
IGame game = selectedGames[0];
IEmulator emulator = PluginHelper.DataManager.GetEmulatorById(game.EmulatorId);
if (gCurrentGame != CleanGameName(game.ApplicationPath))
{
gCurrentGame = CleanGameName(game.ApplicationPath);
if (UseEmulator)
{
gCurrentCategory = emulator.Title;
}
else
{
gCurrentCategory = game.Platform;
}
sendMenuRom(gCurrentCategory, gCurrentGame);
}
}
else
{
if (selectedPlatform != null)
{
// If platform is not null, then we just selected a platform
// If we changed platform
if (gCurrentPlatform != selectedPlatform.Name)
{
gCurrentPlatform = selectedPlatform.Name;
if (selectedGames != null)
{
sendMenuRom(gCurrentPlatform, gCurrentGame);
}
else
{
// No game selected? Then display platform's marquee
sendMenuRom("platforms", gCurrentPlatform);
}
}
}
}
}
// Helper method to clean up the game name
private string CleanGameName(string gamePath)
{
string cleanName = System.IO.Path.GetFileNameWithoutExtension(gamePath);
cleanName = System.Text.RegularExpressions.Regex.Replace(cleanName, @"\s*\([^)]*\)", "");
cleanName = cleanName.Trim();
cleanName = cleanName.Replace("'", "_");
return cleanName;
}
// Method to send menu ROM information to DOFLinx
public static void sendMenuRom(string platform, string game)
{
string romName = System.IO.Path.GetFileNameWithoutExtension(game);
sendMenuNav("MOVE");
sendMsg($"MENU_ROM={platform},{romName}");
}
// Method to send menu command to DOFLinx
public static void sendMenuNav(string cmd)
{
sendMsg($"MENU_NAVIGATION={cmd}");
}
// Send the required message to the DOFLinx pipe
public static void sendMsg(string msg)
{
// Connect to DOFLinx named pipe
using (var dofClient = new NamedPipeClientStream(".", "DOFLinx", PipeDirection.Out, PipeOptions.Asynchronous))
{
dofClient.Connect(1000);
using (var dofSw = new StreamWriter(dofClient))
{
dofSw.Write(msg);
}
}
}
}
}