Skip to content

Commit 27f960a

Browse files
committed
Some bug fixes and overall improvements
1 parent 8d11294 commit 27f960a

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

ModLoader/Classes/StorageHelper.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ public static void InitSettings()
5959

6060
public static void SaveSettings()
6161
{
62-
if (!settingsDidInit) InitSettings();
6362
string json = JsonSerializer.Serialize(Current, new JsonSerializerOptions { WriteIndented = true });
64-
File.WriteAllText(settingsDataPath, json);
63+
File.WriteAllText(settingsDataPath, json ?? "{}");
6564
}
6665

6766
public static void LoadSettings()
@@ -87,7 +86,6 @@ public static void LoadSettings()
8786

8887
public static T GetSetting<T>(string settingName)
8988
{
90-
if (!settingsDidInit) InitSettings();
9189
var prop = typeof(AppSettings).GetProperty(settingName);
9290
if (prop == null) return default!;
9391
object value = prop.GetValue(Current)!;
@@ -97,7 +95,6 @@ public static T GetSetting<T>(string settingName)
9795

9896
public static void SetSetting(string settingName, string value)
9997
{
100-
if (!settingsDidInit) InitSettings();
10198
var prop = typeof(AppSettings).GetProperty(settingName);
10299
if (prop != null && prop.CanWrite)
103100
{

ModLoader/Form1.cs

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public Form1(string uri = null)
8787
{
8888
isDebug = System.Diagnostics.Debugger.IsAttached;
8989

90-
9190
bridge = new JSBridge();
9291
storage = new StorageHelper();
9392
urihelper = new URIHelper();
@@ -109,7 +108,7 @@ public Form1(string uri = null)
109108
this.StartPosition = FormStartPosition.CenterScreen;
110109

111110
InitializeComponent();
112-
Task.Run(() => ListenForUris());
111+
Task.Run(() => ListenForUrisAsync());
113112

114113

115114
webView = new WebView2
@@ -182,7 +181,7 @@ private static async Task<string> CallJsFunctionInternal(string functionName, pa
182181

183182

184183

185-
private void ListenForUris()
184+
private async Task ListenForUrisAsync()
186185
{
187186
while (true)
188187
{
@@ -191,38 +190,75 @@ private void ListenForUris()
191190
using (var pipe = new NamedPipeServerStream("MySuperSickAppPipeForDCTS", PipeDirection.In))
192191
using (var reader = new StreamReader(pipe))
193192
{
194-
pipe.WaitForConnection();
195-
string uri = reader.ReadLine();
193+
await pipe.WaitForConnectionAsync();
194+
195+
string uri = await reader.ReadLineAsync();
196196
if (!string.IsNullOrWhiteSpace(uri))
197197
{
198-
this.Invoke(() => URIHelper.HandleCustomUri(uri));
198+
BeginInvoke(new Action(() =>
199+
{
200+
try
201+
{
202+
URIHelper.HandleCustomUri(uri);
203+
}
204+
catch (Exception ex)
205+
{
206+
Logger.Log(ex.Message);
207+
Debug.WriteLine("URI handling failed: " + ex.Message);
208+
}
209+
}));
199210
}
200211
}
201212
}
202213
catch (Exception ex)
203214
{
204-
Console.WriteLine("Pipe error: " + ex.Message);
215+
Logger.Log(ex.Message);
216+
Debug.WriteLine("Pipe error: " + ex.Message);
217+
await Task.Delay(1000);
205218
}
206219
}
207220
}
208221

209-
210222
async void InitializeAsync()
211223
{
212224
string userDataDir = Path.Combine(Form1.appPath, "webview-data");
213225
Directory.CreateDirectory(userDataDir);
214226

215-
CoreWebView2Environment env = await CoreWebView2Environment.CreateAsync(
216-
null, // can pick browser with this apparently
217-
userDataDir
227+
var options = new CoreWebView2EnvironmentOptions(
228+
"--enable-features=WebRTCHwEncoding " +
229+
"--autoplay-policy=no-user-gesture-required " +
230+
"--disable-background-timer-throttling " +
231+
"--disable-renderer-backgrounding " +
232+
"--disable-backgrounding-occluded-windows " +
233+
"--disable-features=CalculateNativeWinOcclusion,StopNonTimersInBackground,StopAllInBackground," +
234+
"ThrottleDisplayNoneAndVisibilityHiddenCrossOriginIframes,ComputePressure,BackForwardCache"
218235
);
219236

237+
var env = await CoreWebView2Environment.CreateAsync(
238+
null,
239+
userDataDir,
240+
options
241+
);
220242
await webView.EnsureCoreWebView2Async(env);
221243

244+
// nice
245+
webView.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Dark;
246+
247+
await webView.CoreWebView2.Profile.SetPermissionStateAsync(CoreWebView2PermissionKind.Microphone, "http://localhost:2051", CoreWebView2PermissionState.Allow);
248+
await webView.CoreWebView2.Profile.SetPermissionStateAsync(CoreWebView2PermissionKind.Camera, "http://localhost:2051", CoreWebView2PermissionState.Allow);
249+
await webView.CoreWebView2.Profile.SetPermissionStateAsync(CoreWebView2PermissionKind.Autoplay, "http://localhost:2051", CoreWebView2PermissionState.Allow);
250+
251+
webView.CoreWebView2.PermissionRequested += (sender, args) =>
252+
{
253+
Debug.WriteLine(sender);
254+
Debug.WriteLine(args.PermissionKind);
255+
Debug.WriteLine(args.PermissionKind);
256+
args.State = CoreWebView2PermissionState.Allow;
257+
};
258+
222259

223260
webView.CoreWebView2.Settings.AreHostObjectsAllowed = true;
224261
webView.CoreWebView2.AddHostObjectToScript("dcts", bridge);
225-
226262
webView.CoreWebView2.DOMContentLoaded += (s, e) =>
227263
{
228264
try
@@ -248,6 +284,7 @@ async void InitializeAsync()
248284
}
249285
};
250286

287+
251288
webView.CoreWebView2.NavigationCompleted += WebView_NavigationCompleted;
252289

253290
await webView.CoreWebView2.Profile.ClearBrowsingDataAsync(

ModLoader/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ static class Program
88
[STAThread]
99
static void Main(string[] args)
1010
{
11+
StorageHelper.InitSettings();
1112
if (args.Contains("--register-uri"))
1213
{
1314
URIHelper.RegisterUriScheme(true, args);
1415
return;
1516
}
1617

17-
bool isNewInstance;
18+
bool isNewInstance;
1819
using (Mutex mutex = new Mutex(true, "MySuperSickAppMutexForDCTS", out isNewInstance))
1920
{
2021
if (isNewInstance)

0 commit comments

Comments
 (0)