Skip to content

Commit

Permalink
use awake. fixed warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
kianzarrin committed Oct 19, 2022
1 parent d77d1cb commit 99d6473
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 27 deletions.
29 changes: 19 additions & 10 deletions LoadOrder/CO/SingletonLite.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
namespace CO;
using LoadOrderTool;
using System;

namespace CO {
public abstract class SingletonLite<T> where T : new() {
protected static T sInstance;
private static object lockObject = new object();
public static T instance {
get {
public abstract class SingletonLite<T>
where T : SingletonLite<T>, new() {
protected static T sInstance;
private static object lockObject = new object(); // work around in case constructor was used by mistake.
public static T instance {
get {
try {
if (sInstance == null) {
lock (lockObject) {
sInstance = new T();
Log.Debug("Creating singleton of type " + typeof(T).Name);
Log.Debug("Created singleton of type " + typeof(T).Name + ". calling Awake() ...");
sInstance.Awake();
Log.Debug("Awake() finished for " + typeof(T).Name);
}
}
return sInstance;
} catch(Exception ex) {
ex.Log();
throw;
}
}
}

public static bool exists => sInstance != null;
public static bool exists => sInstance != null;

public static void Ensure() => _ = instance;
}
public static void Ensure() => _ = instance;
public virtual void Awake() { }
}

13 changes: 7 additions & 6 deletions LoadOrder/CO/settings/GameSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,17 @@ public class GameSettings : SingletonLite<GameSettings> {
}
}

public GameSettings() {
public override void Awake() {
base.Awake();
try {
sInstance = this;
Log.Info("Creating GameSettings Monitor ...");
Log.Debug(Environment.StackTrace);
GameSettings.m_SaveThread = new Thread(new ThreadStart(GameSettings.MonitorSave));
GameSettings.m_SaveThread.Name = "SaveSettingsThread";
GameSettings.m_SaveThread.IsBackground = true;
GameSettings.m_Run = true;
GameSettings.m_SaveThread.Start();
m_SaveThread = new Thread(new ThreadStart(GameSettings.MonitorSave));
m_SaveThread.Name = "SaveSettingsThread";
m_SaveThread.IsBackground = true;
m_Run = true;
m_SaveThread.Start();
} catch (Exception ex) { ex.Log(); }
}

Expand Down
4 changes: 2 additions & 2 deletions LoadOrder/Data/ConfigWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class ConfigWrapper : SingletonLite<ConfigWrapper> {
bool m_Run = true;
object m_LockObject = new object();

public ConfigWrapper() {
sInstance = this;
public override void Awake() {
base.Awake();
var sw = System.Diagnostics.Stopwatch.StartNew();
Config = LoadOrderConfig.Deserialize(DataLocation.LocalLOMData)
?? new LoadOrderConfig();
Expand Down
2 changes: 1 addition & 1 deletion LoadOrder/Data/List/ModList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public class ModList : List<PluginInfo> {

private int[] RandomNumber(int count) {
var orderedList = Enumerable.Range(0, count);
int randomSeed = RNGCryptoServiceProvider.GetInt32(0, int.MaxValue);
int randomSeed = RandomNumberGenerator.GetInt32(0, int.MaxValue);
var rng = new Random(randomSeed);
return orderedList.OrderBy(c => rng.Next()).ToArray();
}
Expand Down
2 changes: 1 addition & 1 deletion LoadOrder/LoadOrderTool.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop" DefaultTargets="Build;Publish">
<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="Build;Publish">
<Import Project="../Version.props" />
<PropertyGroup>
<Company>Kian Zarrin</Company>
Expand Down
5 changes: 3 additions & 2 deletions LoadOrder/UI/AboutBox.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace LoadOrderTool.UI {
namespace LoadOrderTool.UI {
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -28,7 +28,8 @@ partial class AboutBox : Form {
return titleAttribute.Title;
}
}
return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);

return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location);
}
}

Expand Down
2 changes: 1 addition & 1 deletion LoadOrder/UI/UIUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static class UIUtil {
return values[combo.SelectedIndex];
} catch (Exception ex) {
Log.Exception(ex, $"SelectedIndex={combo?.SelectedIndex} values={string.Join(", ", values)}");
throw ex;
throw;
}
}

Expand Down
2 changes: 1 addition & 1 deletion LoadOrder/Util/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private enum LogLevel {
float diff = seconds + 1;
if (id < 0) id = -id;
id = System.Math.Abs(id % MAX_WAIT_ID);
if (times_[id] != null) {
if (times_[id] != default) {
var diff0 = DateTime.Now - times_[id];
diff = diff0.Seconds;
}
Expand Down
2 changes: 1 addition & 1 deletion LoadOrder/Util/ResourceUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static class ResourceUtil {
?? throw new Exception(file + " not find");
} catch(Exception ex) {
Log.Exception(ex);
throw ex;
throw;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions LoadOrder/Util/SteamUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public static class SteamUtil {
}
public static bool CheckForInternetConnection() {
try {
using (var client = new WebClient())
using (var stream = client.OpenRead("https://steamcommunity.com/")) {
using (var client = new HttpClient())
using (var stream = client.GetStreamAsync("https://steamcommunity.com/").Result) {
return true;
}
} catch {
Expand Down

0 comments on commit 99d6473

Please sign in to comment.