Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

Commit

Permalink
Fixed possible nullreference #98
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertware committed Jan 16, 2016
1 parent 76d240d commit 93729ae
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions bukkitgui2/AddOn/Tasker/Tasker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using MetroFramework;
using MetroFramework.Controls;
using Net.Bertware.Bukkitgui2.Core.FileLocation;
using Net.Bertware.Bukkitgui2.Core.Logging;
Expand Down Expand Up @@ -94,7 +96,7 @@ public void SaveTask(Task oldTask, Task newTask)
{
DeleteTask(oldTask);
AddTask(newTask);
// events aLocale.Tready fired by delete & add operations
// events are fired by delete & add operations
}

/// <summary>
Expand All @@ -117,9 +119,13 @@ public void AddTask(Task task)
/// <param name="task">the task to delete</param>
public void DeleteTask(Task task)
{
if (task == null) return;

task.Disable();
task.TaskExecuted -= OnTaskExecuted;

if (Tasks != null && Tasks.ContainsKey(task.Name)) Tasks.Remove(task.Name);

task.Dispose(); // make sure it's removed
OnTaskListAltered();// list changed, invoke event
SaveConfig();
Expand All @@ -130,18 +136,32 @@ public void DeleteTask(Task task)
/// </summary>
private static void SaveConfig()
{
Logger.Log(LogLevel.Info, "Tasker", "Saving tasks...", _Configfile);

if (!File.Exists(_Configfile)) File.Create(_Configfile).Close();
using (StreamWriter sw = File.CreateText(_Configfile))
{
foreach (KeyValuePair<string, Task> pair in Tasks)
{
sw.WriteLine(pair.Value.Serialize());
Logger.Log(LogLevel.Info, "Tasker", "Saved task", pair.Value.ToString());
}
}
Logger.Log(LogLevel.Info, "Tasker", "Saved all tasks");
try
{


Logger.Log(LogLevel.Info, "Tasker", "Saving tasks...", _Configfile);

if (!File.Exists(_Configfile)) File.Create(_Configfile).Close();

using (StreamWriter sw = File.CreateText(_Configfile))
{
foreach (KeyValuePair<string, Task> pair in Tasks)
{
sw.WriteLine(pair.Value.Serialize());
Logger.Log(LogLevel.Info, "Tasker", "Saved task", pair.Value.ToString());
}
}
Logger.Log(LogLevel.Info, "Tasker", "Saved all tasks");
}
catch (Exception e)
{
Logger.Log(LogLevel.Severe, "Tasker", "Failed to save tasks", e.Message);
MetroMessageBox.Show(Form.ActiveForm,
"The task settings could not be saved for an unknown reason", "Tasks not saved", MessageBoxButtons.OK,
MessageBoxIcon.Error);

}
}

public void Dispose()
Expand Down

0 comments on commit 93729ae

Please sign in to comment.