Skip to content

Commit

Permalink
pushed code.
Browse files Browse the repository at this point in the history
  • Loading branch information
r0neko committed Dec 3, 2020
0 parents commit a5a0f6b
Show file tree
Hide file tree
Showing 6 changed files with 430 additions and 0 deletions.
78 changes: 78 additions & 0 deletions osuCompanion/Companion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.IO;

namespace osuCompanion
{
class Companion
{
static string FILE_PATH = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "osuCompanion.txt");
const string NO_MAP_SELECTED = "no map selected";

private static MSN msn = new MSN();

static private void updateFile(string d)
{
using (StreamWriter sw = new StreamWriter(FILE_PATH))
{
sw.WriteLine(string.Format("- {0} -", d));
}
}

static int Main(string[] args)
{
Console.WriteLine("osu!companion - v1.0 by r0neko");
#if DEBUG
Console.WriteLine("[DEBUG BUILD]");
#endif
Console.WriteLine("https://github.com/r0neko/osuCompanion");
Console.WriteLine();

if (!File.Exists(FILE_PATH))
{
Console.WriteLine(string.Format("=> Creating '{0}'...", FILE_PATH));
try
{
File.Create(FILE_PATH);
updateFile("If you see this, your streaming software was configured correctly!");
Console.WriteLine(string.Format("Created a new file at {0}.\nPlease select '{0}' in your streaming software to show updates. In OBS, add a new Text component, then set it to 'Read from file'.", FILE_PATH));
Console.WriteLine("Press enter when ready.");
_ = Console.ReadLine();
}
catch (Exception)
{
Console.WriteLine("[x] An error has occured while creating the file.\nPlease check that you have proper rights(are you admin?) and try again.");
Console.WriteLine("Press enter to exit.");
_ = Console.ReadLine();
return 1;
}
}
else
{
Console.WriteLine(string.Format("=> File checks succeded! '{0}' exists.", FILE_PATH));
}

updateFile(NO_MAP_SELECTED);

Console.WriteLine("=> Initialising MSN Handler...");
msn.init();
msn.MessageReceived += OnMessage;

Console.WriteLine("=> Main updating loop starts.");

while (true)
{
msn.Update();
}
}

private static void OnMessage(object sender, MSN.OsuStatus status)
{
string template = status.status == MSN.Status.Listening ? "{0} to {1} - {2}" : "{1} - {2}";
if (status.difficulty.Length > 0) template += "[{3}]";
string finalString = status.status == MSN.Status.Listening ? NO_MAP_SELECTED : string.Format(template, status.status, status.title, status.artist, status.difficulty);

Console.WriteLine("=> Status update: " + finalString);
updateFile(finalString);
}
}
}
158 changes: 158 additions & 0 deletions osuCompanion/MSN.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using System;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace osuCompanion
{
class MSN
{
private const string lpClassName = "MsnMsgrUIManager";
private WNDCLASS lpWndClass;
private IntPtr m_hwnd;
private WndProc WndProcc;
private OsuStatus lastStatus;
public event EventHandler<OsuStatus> MessageReceived;

[DllImport("user32.dll")]
private static extern ushort RegisterClassW([In] ref WNDCLASS lpWndClass);

[DllImport("user32.dll")]
private static extern IntPtr CreateWindowExW(uint dwExStyle, [MarshalAs(UnmanagedType.LPWStr)] string lpClassName, [MarshalAs(UnmanagedType.LPWStr)] string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);

[DllImport("user32.dll")]
private static extern IntPtr DefWindowProcW(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
private static extern bool DestroyWindow(IntPtr hWnd);

[DllImport("user32.dll")]
static extern bool TranslateMessage([In] ref MSG lpMsg);

[DllImport("user32.dll")]
static extern IntPtr DispatchMessage([In] ref MSG lpmsg);

[DllImport("user32.dll")]
static extern sbyte GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin,
uint wMsgFilterMax);
private delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

private struct WNDCLASS
{
public uint style;
public WndProc lpfnWndProc;
public int cbClsExtra;
public int cbWndExtra;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpszMenuName;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpszClassName;
}

private struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
}

public enum Status
{
Null,
Listening,
Editing,
Playing,
Watching
};

public class OsuStatus : EventArgs
{
public Status status { get; set; }
public String artist { get; set; }
public String title { get; set; }
public String difficulty { get; set; }

}

private IntPtr CustomWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
if (msg == 0x4a)
{
COPYDATASTRUCT copydatastruct =
(COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT));

var ptr = copydatastruct.lpData;
if (ptr != IntPtr.Zero)
{
string str = Marshal.PtrToStringUni(ptr, copydatastruct.cbData / 2);
string[] separator = new string[] { @"\0" };
string[] sourceArray = str.Split(separator, StringSplitOptions.None);
if (sourceArray.Length > 8)
{
for (int i = 0; i < sourceArray.Length; i++)
{
String status = sourceArray[3].Split(new[] { ' ' }, 2)[0];
Status st = status == "Listening" ? Status.Listening
: status == "Playing" ? Status.Playing
: status == "Watching" ? Status.Watching
: status == "Editing" ? Status.Editing
: Status.Null;

OsuStatus stat = new OsuStatus();
stat.status = st;
stat.artist = sourceArray[5];
stat.title = sourceArray[4];
stat.difficulty = sourceArray[7];

if (this.lastStatus == null || this.lastStatus.status != stat.status || this.lastStatus.artist != stat.artist || this.lastStatus.title != stat.title)
{
if (this.lastStatus == null) this.lastStatus = new OsuStatus();
this.lastStatus.status = stat.status;
this.lastStatus.artist = stat.artist;
this.lastStatus.title = stat.title;
this.lastStatus.difficulty = stat.difficulty;
EventHandler<OsuStatus> handler = MessageReceived;
if (handler != null)
{
handler(this, stat);
}
}
}
}
}
}
return DefWindowProcW(hWnd, msg, wParam, lParam);
}

public void init()
{
WndProcc = CustomWndProc;
lpWndClass = new WNDCLASS
{
lpszClassName = lpClassName,
lpfnWndProc = WndProcc
};
ushort num = RegisterClassW(ref lpWndClass);
int num2 = Marshal.GetLastWin32Error();
if ((num == 0) && (num2 != 0x582))
{
throw new Exception("Could not register window class");
}
m_hwnd = CreateWindowExW(0, lpClassName, string.Empty, 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
}

public void Update()
{
MSG msg;
if (GetMessage(out msg, IntPtr.Zero, 0, 0) != 0)
{
TranslateMessage(ref msg);
DispatchMessage(ref msg);
}
}

}
}
36 changes: 36 additions & 0 deletions osuCompanion/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("osuCompanion")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("osuCompanion")]
[assembly: AssemblyCopyright("r0neko 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("25fd472b-c518-42f9-be5b-bde96297be18")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
75 changes: 75 additions & 0 deletions osuCompanion/app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
Specifying requestedExecutionLevel element will disable file and registry virtualization.
Remove this element if your application requires this virtualization for backwards
compatibility.
-->
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->

<!-- Windows Vista -->
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->

<!-- Windows 7 -->
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->

<!-- Windows 8 -->
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->

<!-- Windows 8.1 -->
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->

<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>

<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. -->
<!--
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
</windowsSettings>
</application>
-->

<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
<!--
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
-->

</assembly>
Loading

0 comments on commit a5a0f6b

Please sign in to comment.