Skip to content

Commit

Permalink
New loading screen. Some audio improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanpmartell committed Jul 22, 2024
1 parent 4cb5762 commit eadf01d
Show file tree
Hide file tree
Showing 14 changed files with 251 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet-desktop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
uses: mathieudutour/[email protected]
with:
github_token: ${{ secrets.TOKEN }}
default_bump: minor
default_bump: patch

# Install the .NET Core workload
- name: Install .NET Core
Expand Down
17 changes: 17 additions & 0 deletions LittleWarGameClient/AddOns.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
addons.init = {
function(mouseLock, clientVersion) {
this.addExitButton();
this.changeQuitButtonText();
this.addClientVersion(clientVersion);
this.replaceMouseLockCheckbox(mouseLock);
var fullScreenButton = document.getElementById("optionsFullscreenButton");
Expand All @@ -135,6 +136,7 @@ addons.init = {
};
this.addClientMadeBy();
console.log("Addons loaded");
this.jsInitComplete();
},

addClientMadeBy: function () {
Expand Down Expand Up @@ -190,5 +192,20 @@ addons.init = {
addons.pressMouseLockCheckbox(this);
};
}
},

changeQuitButtonText: function () {
document.getElementById("optionsQuitButton").innerText = "Surrender";
addons.addCustomHotkeyToInnerText("optionsQuitButton", "N");
},

jsInitComplete: function () {
window.chrome.webview.postMessage(
JSON.stringify({
Id: "",
Value: "",
Type: "InitComplete"
})
);
}
};
155 changes: 155 additions & 0 deletions LittleWarGameClient/AudioManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
using NAudio.CoreAudioApi;
using NAudio.CoreAudioApi.Interfaces;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace LittleWarGameClient
{
internal class AudioManager : IAudioSessionEventsHandler
{
private MMDevice? mainDevice;
private AudioSessionControl? currentSession;
private readonly Form form;

public AudioManager(Form form)
{
this.form = form;
var etor = new MMDeviceEnumerator();
this.mainDevice = etor.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
if (mainDevice != null)
{
var sessions = mainDevice.AudioSessionManager.Sessions;
for (int i = 0; i < sessions.Count; i++)
{
var session = sessions[i];
ChangeTextAndIcon(session);
}
mainDevice.AudioSessionManager.OnSessionCreated += AudioSessionManager_OnSessionCreated;
}
}
internal void DestroySession()
{
var sess = this.currentSession;
this.currentSession = null;
this.UnregisterFromSession(sess);
if (mainDevice != null)
{
mainDevice.AudioSessionManager.OnSessionCreated -= this.AudioSessionManager_OnSessionCreated;
mainDevice.Dispose();
}
}

private void UnregisterFromSession(AudioSessionControl? session)
{
if (session != null)
{
session.UnRegisterEventClient(this);
session.Dispose();
}
}

private void ChangeTextAndIcon(AudioSessionControl session)
{
if (session.IsSystemSoundsSession)
return;

var proc = Process.GetProcessById((int)session.GetProcessID);
if (proc == null)
return;

var currentProc = Process.GetCurrentProcess();
Process? parentProc = proc;
bool isSubProc = false;
while (parentProc != null && parentProc.Id != 0)
{
parentProc = GetParentProcess(parentProc);
if (parentProc != null && parentProc.Id == currentProc.Id)
{
isSubProc = true;
break;
}
}
if (!isSubProc)
return;

session.DisplayName = form.Text;
session.IconPath = GetType().Assembly.Location;
this.currentSession = session;
this.currentSession.RegisterEventClient(this);
}

private void AudioSessionManager_OnSessionCreated(object sender, IAudioSessionControl newSession)
{
AudioSessionControl managedControl = new AudioSessionControl(newSession);
ChangeTextAndIcon(managedControl);
}

private Process? GetParentProcess(Process process)
{
try
{
using (var query = new ManagementObjectSearcher(
"SELECT * " +
"FROM Win32_Process " +
"WHERE ProcessId=" + process.Id))
{
using (var collection = query.Get())
{
var mo = collection.OfType<ManagementObject>().FirstOrDefault();
if (mo != null)
{
using (mo)
{
var p = Process.GetProcessById((int)(uint)mo["ParentProcessId"]);
return p;
}
}

}
return null;
}
}
catch
{
return null;
}
}

void IAudioSessionEventsHandler.OnVolumeChanged(float volume, bool isMuted) { }
void IAudioSessionEventsHandler.OnDisplayNameChanged(string displayName) { }
void IAudioSessionEventsHandler.OnChannelVolumeChanged(uint channelCount, nint newVolumes, uint channelIndex) { }
void IAudioSessionEventsHandler.OnGroupingParamChanged(ref Guid groupingId) { }
void IAudioSessionEventsHandler.OnIconPathChanged(string iconPath) { }

void IAudioSessionEventsHandler.OnStateChanged(AudioSessionState state)
{
if (state == AudioSessionState.AudioSessionStateExpired)
{
this.ReleaseSessionDelayed(this.currentSession);
}
}

void IAudioSessionEventsHandler.OnSessionDisconnected(AudioSessionDisconnectReason disconnectReason)
{
this.ReleaseSessionDelayed(this.currentSession);
}

private void ReleaseSessionDelayed(AudioSessionControl? session)
{
var timer = new System.Windows.Forms.Timer();
void TimerTick(object? sender, EventArgs e)
{
timer.Stop();
timer.Tick -= TimerTick;
UnregisterFromSession(session);
};
timer.Interval = 100;
timer.Tick += TimerTick;
}
}
}
44 changes: 38 additions & 6 deletions LittleWarGameClient/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 23 additions & 2 deletions LittleWarGameClient/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
using System.IO;
using System.Reflection.Metadata;
using System.Reflection;
using System.Data;
using System.Diagnostics;
using NAudio.CoreAudioApi;

namespace LittleWarGameClient
{
Expand All @@ -16,13 +19,17 @@ internal partial class Form1 : Form
private readonly Fullscreen fullScreen;
private readonly KeyboardHandler kbHandler;
private readonly VersionHandler vHandler;
private readonly AudioManager audioMngr;

private bool wasSmallWindow = false;
private bool gameHasLoaded = false;
private bool mouseLocked;

public Form1()
{
InitializeComponent();
InitWebView();
audioMngr = new AudioManager(this);
settings = new Settings();
this.Size = settings.GetWindowSize();
fullScreen = new Fullscreen(this, settings);
Expand Down Expand Up @@ -53,7 +60,6 @@ private void webView_NavigationCompleted(object sender, Microsoft.Web.WebView2.C
kbHandler.InitHotkeyNames(settings);
gameHasLoaded = true;
ResizeGameWindows();
this.Controls.Remove(loadingPanel);
}

private void webView_WebMessageReceived(object sender, Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs e)
Expand All @@ -78,7 +84,9 @@ private void webView_WebMessageReceived(object sender, Microsoft.Web.WebView2.Co
settings.SaveAsync();
CaptureCursor();
break;
default:
case ButtonType.InitComplete:
this.Controls.Remove(loadingPanel);
loadingTimer.Enabled = false;
break;
}
}
Expand Down Expand Up @@ -135,5 +143,18 @@ private void ResizeGameWindows()
}
}
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
audioMngr.DestroySession();
}

private void loadingTimer_Tick(object sender, EventArgs e)
{
if (loadingText.Visible)
loadingText.Visible = false;
else
loadingText.Visible = true;
}
}
}
3 changes: 3 additions & 0 deletions LittleWarGameClient/Form1.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="loadingTimer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
Expand Down
Loading

0 comments on commit eadf01d

Please sign in to comment.