Skip to content

Commit

Permalink
Improve msu part1
Browse files Browse the repository at this point in the history
  • Loading branch information
MycroftKang committed Dec 11, 2022
1 parent db2508c commit 2ca484b
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 123 deletions.
27 changes: 9 additions & 18 deletions src/bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
sys.path.append("..\\lib")
import msvcrt
import os
import traceback

from core.controllers.ipc_controller import IPCController
from mgylabs.db.database import run_migrations
from mgylabs.db.paths import DB_URL, SCRIPT_DIR
from mgylabs.services.telemetry_service import TelemetryReporter
from mgylabs.utils.version import VERSION
from mgylabs.utils import logger

from core.controllers.ipc_controller import IPCController
from mgylabs.utils.version import VERSION

os.chdir(os.path.dirname(os.path.abspath(__file__)))

Expand Down Expand Up @@ -72,17 +70,10 @@ def main():


if __name__ == "__main__":
error = 0

try:
TelemetryReporter.start()
main()
except SystemExit as e:
error = e.code
except Exception as e:
TelemetryReporter.Exception(e)
traceback.print_exc()
error = 1
finally:
TelemetryReporter.terminate()
sys.exit(error)
with TelemetryReporter():
try:
main()
except Exception as error:
TelemetryReporter.Exception(error)
log.error(error, exc_info=True)
sys.exit(1)
26 changes: 14 additions & 12 deletions src/console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ static void Main()

bool createnew;

Mutex mutex = new Mutex(true, Version.mutex_name, out createnew);

if (!createnew)
using (Mutex mutex = new Mutex(true, Version.mutex_name, out createnew))
{
MessageBox.Show("Mulgyeol MK Bot is already running.");
return;
if (!createnew)
{
MessageBox.Show("Mulgyeol MK Bot is already running.");
return;
}

DesktopNotificationManagerCompat.RegisterAumidAndComServer<MyNotificationActivator>(Version.dname);
DesktopNotificationManagerCompat.RegisterActivator<MyNotificationActivator>();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
TrayApp app = new TrayApp();
Application.ApplicationExit += new EventHandler(app.Application_Exit);
Application.Run(app);
}
DesktopNotificationManagerCompat.RegisterAumidAndComServer<MyNotificationActivator>(Version.dname);
DesktopNotificationManagerCompat.RegisterActivator<MyNotificationActivator>();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
TrayApp app = new TrayApp();
Application.ApplicationExit += new EventHandler(app.Application_Exit);
Application.Run(app);
}
}
}
102 changes: 63 additions & 39 deletions src/console/TrayApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Windows.UI.Notifications;

Expand All @@ -15,6 +17,7 @@ class TrayApp : ApplicationContext
private NotifyIcon notifyIcon1;
private ContextMenuStrip contextMenuStrip1;

private bool manual_checking_update = false;
private bool checking_update = false;
private bool can_update = false;

Expand Down Expand Up @@ -85,17 +88,10 @@ public TrayApp()
new ToolStripMenuItem("Exit", null, Click_Exit)
});

if (check_ready_to_update())
{
Run_setup(true);
}
else
{
msu_process.Exited += new EventHandler(ProcessExited_msu);
msu_process.EnableRaisingEvents = true;
notifyIcon1.Visible = true;
notifyIcon1.Icon = Properties.Resources.mkbot_off;
}
msu_process.Exited += new EventHandler(ProcessExited_msu);
msu_process.EnableRaisingEvents = true;
notifyIcon1.Visible = true;
notifyIcon1.Icon = Properties.Resources.mkbot_off;

bool auto_connect = false;
if (configjson["connectOnStart"] != null)
Expand Down Expand Up @@ -187,8 +183,8 @@ private void Click_Update(object sender, EventArgs e)
}
else
{
checking_update = true;
UpdateMenu.Enabled = false;
manual_checking_update = true;
Run_msu("/c");
}
}
Expand Down Expand Up @@ -332,11 +328,6 @@ private void Toast_Activated(ToastNotification sender, object arguments)
}
}

private bool check_ready_to_update()
{
return File.Exists("./Update.flag");
}

private void checkForTime_Elapsed_At_Startup(object sender, System.Timers.ElapsedEventArgs e)
{
checkForTime.Enabled = false;
Expand All @@ -351,9 +342,12 @@ private void checkForTime_Elapsed_At_Startup(object sender, System.Timers.Elapse

private void checkForTime_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (can_update && !MKBotCore.discord_online)
if (can_update)
{
Run_setup(true, false);
if (!MKBotCore.discord_online)
{
Run_setup(true);
}
}
else
{
Expand All @@ -365,18 +359,24 @@ private void CheckForUpdate()
{
if (!can_update)
{
Run_msu("/c");
Run_msu("/c /sch");
}
}

private void Run_msu(string argv = "/c")
{
if (argv == "/c")
if (checking_update)
{
UpdateMenu.Enabled = false;
UpdateMenu.Text = "Checking for Updates...";
return;
}

checking_update = true;

UpdateMenu.Enabled = false;
UpdateMenu.Text = "Checking for Updates...";

psi2.Arguments = argv;

try
{
if (msu_process.HasExited)
Expand Down Expand Up @@ -405,59 +405,83 @@ private void Install_update()
}
}

private void Run_setup(bool autorun, bool gui = true)
private void Run_setup(bool autorun)
{
string param;

param = "--start";

if (gui)
{
param += " --gui";
}
var flag = File.ReadAllText("bin\\msu_update.flag");

if (autorun)
{
param += " --auto-run";
if (File.Exists(flag))
{
File.Delete(flag);
}
}

notifyIcon1.Visible = false;
Process.Start("Update.exe", param);
Environment.Exit(0);
}

private async void poll_until()
{
while (!mutex_is_active($"{Version.mutex_name}-ready"))
{
await Task.Delay(1000);
}
}

private bool mutex_is_active(string name)
{
try
{
Mutex foundMutex = Mutex.OpenExisting(name);

return true;
}
catch (WaitHandleCannotBeOpenedException)
{
return false;
}
}

private void ProcessExited_msu(object sender, EventArgs e)
{
if (msu_process.ExitCode == 0)
{
Console.WriteLine("> msu exit: 0");

UpdateMenu.Text = "Installing Update...";

poll_until();

can_update = true;
UpdateMenu.Enabled = true;
UpdateMenu.Text = "Restart to Update";

if (!(checking_update || MKBotCore.discord_online))
if (!(manual_checking_update || MKBotCore.discord_online))
{
Run_setup(true, false);
Run_setup(true);
}
else
{
ShowUpdateToast();
}

checking_update = false;
manual_checking_update = false;
}
else if (msu_process.ExitCode == 1)
{
Console.WriteLine("> msu exit: 1");
UpdateMenu.Enabled = true;
UpdateMenu.Text = "Check for Updates...";

if (checking_update)
if (manual_checking_update)
{
ShowToast("Mulgyeol Software Update", "There are currently no updates available.");
checking_update = false;
manual_checking_update = false;
}
}

checking_update = false;
}

internal void Application_Exit(object sender, EventArgs e)
Expand Down
2 changes: 1 addition & 1 deletion src/console/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static public void load_version_info()
canary = true;
mutex_name = "MKBotCanary";
product_name = "Mulgyeol MK Bot Canary";
dname = "com.mgylabs.mkbot-can";
dname = "com.mgylabs.mkbot-canary";
}
else
{
Expand Down
7 changes: 7 additions & 0 deletions src/lib/mgylabs/services/telemetry_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
class TelemetryReporter:
reporter = None

def __enter__(self, callback_event_name=None):
TelemetryReporter.start(callback_event_name)
return self

def __exit__(self, type, value, traceback):
TelemetryReporter.terminate()

@classmethod
def _run_callback_(cls, callback):
if callback is not None:
Expand Down
Loading

0 comments on commit 2ca484b

Please sign in to comment.