Skip to content
This repository has been archived by the owner on Sep 16, 2019. It is now read-only.

Commit

Permalink
0.1.0.1
Browse files Browse the repository at this point in the history
- Proper exception logging
- Update check on startup
  • Loading branch information
LogicAndTrick committed Jul 24, 2013
1 parent 1e629f2 commit cfa155a
Show file tree
Hide file tree
Showing 14 changed files with 805 additions and 20 deletions.
31 changes: 30 additions & 1 deletion Sledge.Common/Mediator/Mediator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;

Expand All @@ -10,6 +11,24 @@ namespace Sledge.Common.Mediator
/// </summary>
public static class Mediator
{
public delegate void MediatorExceptionEventHandler(string message, Exception exception);
public static event MediatorExceptionEventHandler MediatorException;
private static void OnMediatorException(string message, object parameter, Exception ex)
{
if (MediatorException != null)
{
var st = new StackTrace();
var frames = st.GetFrames() ?? new StackFrame[0];
var msg = "Mediator exception: " + message + "(" + parameter + ")";
foreach (var frame in frames)
{
var method = frame.GetMethod();
msg += "\r\n " + method.ReflectedType.FullName + "." + method.Name;
}
MediatorException(message, new Exception(msg, ex));
}
}

/// <summary>
/// Helper method to execute the a function with the same name as the message. Called by the listener if desired.
/// </summary>
Expand Down Expand Up @@ -99,7 +118,17 @@ public static void Publish(string message, object parameter = null)
else if (reference.Target != null)
{
var method = reference.Target.GetType().GetMethod("Notify", new[] { typeof(string), typeof(object) });
if (method != null) method.Invoke(reference.Target, new object[] { message, parameter });
if (method != null)
{
try
{
method.Invoke(reference.Target, new[] { message, parameter });
}
catch (Exception ex)
{
OnMediatorException(message, parameter, ex);
}
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sledge.Editor.Updater/UpdaterForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ private void DoUpdate()
{
InstallUpdate(directory, download);
Directory.Delete(updateDirectory, true);
Close();
Process.Start(Path.Combine(directory, "Sledge.Editor.exe"));
Application.Exit();
});
}
return;
Expand Down
19 changes: 18 additions & 1 deletion Sledge.Editor/Documents/Document.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using OpenTK;
using Sledge.Common.Mediator;
Expand Down Expand Up @@ -133,7 +134,23 @@ public Coordinate Snap(Coordinate c, decimal spacing = 0)
/// <param name="action">The action to perform</param>
public void PerformAction(string name, IAction action)
{
action.Perform(this);
try
{
action.Perform(this);
}
catch (Exception ex)
{
var st = new StackTrace();
var frames = st.GetFrames() ?? new StackFrame[0];
var msg = "Action exception: " + name + " (" + action + ")";
foreach (var frame in frames)
{
var method = frame.GetMethod();
msg += "\r\n " + method.ReflectedType.FullName + "." + method.Name;
}
Logging.Logger.ShowException(new Exception(msg, ex), "Error performing action");
}

var history = new HistoryAction(name, action);
History.AddHistoryItem(history);
}
Expand Down
5 changes: 5 additions & 0 deletions Sledge.Editor/Editor.Designer.cs

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

112 changes: 112 additions & 0 deletions Sledge.Editor/Editor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.WindowsAPICodePack.Taskbar;
Expand All @@ -21,6 +25,7 @@
using Sledge.Settings;
using Sledge.Settings.Models;
using Hotkeys = Sledge.Editor.UI.Hotkeys;
using Path = Sledge.DataStructures.MapObjects.Path;

namespace Sledge.Editor
{
Expand Down Expand Up @@ -110,8 +115,103 @@ private void EditorLoad(object sender, EventArgs e)
//TexturePackage.LoadTextureData(TexturePackage.GetLoadedItems().Select(x => x.Name));

Subscribe();

Mediator.MediatorException += (msg, ex) => Logging.Logger.ShowException(ex, "Mediator Error: " + msg);
}

#region Updates

private void CheckForUpdates()
{
#if DEBUG
return;
#endif

var sources = GetUpdateSources();
var version = GetCurrentVersion();
foreach (var source in sources)
{
var result = GetUpdateCheckResult(source, version);
if (result == null) continue;
if (!String.Equals(result.Version, version, StringComparison.InvariantCultureIgnoreCase))
{
if (MessageBox.Show("An update is available, would you like to update now?", "New version detected!", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Process.Start(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(typeof(Editor).Assembly.Location), "Sledge.Editor.Updater.exe"));
Application.Exit();
}
}
return;
}
}

private class UpdateSource
{
public string Name { get; set; }
public string Url { get; set; }

public string GetUrl(string version)
{
return String.Format(Url, version);
}
}

private class UpdateCheckResult
{
public string Version { get; set; }
public DateTime Date { get; set; }
public string DownloadUrl { get; set; }
}

private IEnumerable<UpdateSource> GetUpdateSources()
{
var dir = System.IO.Path.GetDirectoryName(typeof(Editor).Assembly.Location);
if (dir == null) yield break;
var file = System.IO.Path.Combine(dir, "UpdateSources.txt");
if (!File.Exists(file)) yield break;
var lines = File.ReadAllLines(file);
foreach (var line in lines)
{
if (String.IsNullOrWhiteSpace(line) || line.StartsWith("#")) continue;
var split = line.Split(':');
if (split.Length < 2) continue;
var us = new UpdateSource
{
Name = split[0],
Url = String.Join(":", split.Skip(1))
};
yield return us;
}
}

private String GetCurrentVersion()
{
var info = FileVersionInfo.GetVersionInfo(typeof(Editor).Assembly.Location);
return info.FileVersion;
}

private UpdateCheckResult GetUpdateCheckResult(UpdateSource source, string version)
{
try
{
using (var downloader = new WebClient())
{
var str = downloader.DownloadString(source.GetUrl(version)).Split('\n', '\r');
if (str.Length < 3 || String.IsNullOrWhiteSpace(str[0]))
{
return null;
}
return new UpdateCheckResult { Version = str[0], Date = DateTime.Parse(str[1]), DownloadUrl = str[2] };
}
}
catch
{
return null;
}
}

#endregion

private void EditorClosing(object sender, FormClosingEventArgs e)
{
if (DocumentManager.CurrentDocument != null)
Expand Down Expand Up @@ -164,6 +264,13 @@ private void Subscribe()

Mediator.Subscribe(EditorMediator.TextureSelected, this);
Mediator.Subscribe(EditorMediator.ToolSelected, this);

Mediator.Subscribe(EditorMediator.About, this);
}

private void About()
{
throw new Exception("THIS IS AN EXCEPTION!");
}

public static void FileNew()
Expand Down Expand Up @@ -480,5 +587,10 @@ private void MoveToEntityClicked(object sender, EventArgs e)
{
Mediator.Publish(HotkeysMediator.TieToEntity);
}

private void EditorShown(object sender, EventArgs e)
{
System.Threading.Tasks.Task.Factory.StartNew(CheckForUpdates);
}
}
}
Loading

0 comments on commit cfa155a

Please sign in to comment.