Skip to content

Commit

Permalink
Merge pull request #97 from pjf/96_wsfix
Browse files Browse the repository at this point in the history
Linux fixes by @AlexanderDzhoganov
  • Loading branch information
pjf committed Oct 20, 2014
2 parents 623d9b4 + 801dc37 commit f041d48
Show file tree
Hide file tree
Showing 19 changed files with 696 additions and 271 deletions.
34 changes: 25 additions & 9 deletions CKAN/CKAN/ModuleInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class ModuleInstaller
public ModuleInstallerReportModInstalled onReportModInstalled = null;
public ModuleInstallerReportProgress onReportProgress = null;

private bool m_LastDownloadSuccessful = false;

/// <summary>
/// Download the given mod. Returns the filename it was saved to.
/// If no filename is provided, the standard_name() will be used.
Expand Down Expand Up @@ -105,6 +107,18 @@ public string CachedOrDownload(string identifier, Version version, Uri url, stri
return Download(url, filename);
}

public static bool IsCached(CkanModule module)
{
var filename = CkanModule.StandardName(module.identifier, module.version);
var path = CachePath(filename);
if (File.Exists(path))
{
return true;
}

return false;
}

public bool IsCached(string filename, out string fullPath)
{
fullPath = CachePath(filename);
Expand Down Expand Up @@ -199,6 +213,16 @@ public void InstallList(List<string> modules, RelationshipResolverOptions option
{
Monitor.Wait(downloader);
}

if (m_LastDownloadSuccessful)
{
for (int i = 0; i < modulesToDownload.Length; i++)
{
Install(modulesToDownload[i], modulesToDownloadPaths[i]);
}
}

currentTransaction.Commit();
}

private void OnDownloadsComplete(Uri[] urls, string[] filenames, CkanModule[] modules, Exception[] errors)
Expand All @@ -214,15 +238,7 @@ private void OnDownloadsComplete(Uri[] urls, string[] filenames, CkanModule[] mo
}
}

if (noErrors)
{
for (int i = 0; i < urls.Length; i++)
{
Install(modules[i], filenames[i]);
}

currentTransaction.Commit();
}
m_LastDownloadSuccessful = noErrors;

lock (downloader)
{
Expand Down
12 changes: 8 additions & 4 deletions CKAN/CKAN/Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace CKAN
public struct NetAsyncDownloaderDownloadPart
{
public WebClient agent;
public long bytesDownloaded;
public long bytesLeft;
public int bytesPerSecond;
public Exception error;
Expand Down Expand Up @@ -97,7 +98,7 @@ public string[] StartDownload()
return filePaths;
}

private void FileProgressReport(int index, int percent, long bytesDownloaded, long _bytesLeft)
private void FileProgressReport(int index, int percent, long bytesDownloaded, long bytesLeft)
{
NetAsyncDownloaderDownloadPart download = downloads[index];

Expand All @@ -113,23 +114,26 @@ private void FileProgressReport(int index, int percent, long bytesDownloaded, lo
download.bytesPerSecond = (int) bytesChange/timeSpan.Seconds;
}

download.bytesLeft = _bytesLeft;
download.bytesLeft = bytesLeft;
download.bytesDownloaded = bytesDownloaded;
downloads[index] = download;

if (onProgressReport != null)
{
int totalPercentage = 0;
int totalBytesPerSecond = 0;
long totalBytesLeft = 0;
long totalBytesDownloaded = 0;

for (int i = 0; i < downloads.Length; i++)
{
totalBytesPerSecond += downloads[i].bytesPerSecond;
totalBytesLeft += downloads[i].bytesLeft;
totalPercentage += downloads[i].percentComplete;
totalBytesDownloaded += downloads[i].bytesDownloaded;
totalBytesLeft += downloads[i].bytesLeft;
}

totalPercentage /= downloads.Length;
totalPercentage = (int)((totalBytesDownloaded * 100) / (totalBytesLeft + totalBytesDownloaded + 1));

onProgressReport(totalPercentage, totalBytesPerSecond, totalBytesLeft);
}
Expand Down
51 changes: 50 additions & 1 deletion CKAN/CKAN/Registry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using log4net;

Expand Down Expand Up @@ -113,7 +114,7 @@ public List<CkanModule> Available(KSPVersion ksp_version = null)
{
try
{
if (LatestAvailable(dependency.name.Value, ksp_version) == null)
if (LatestAvailableWithProvides(dependency.name.Value, ksp_version).Count == 0)
{
failedDepedency = true;
break;
Expand Down Expand Up @@ -191,6 +192,54 @@ public CkanModule LatestAvailable(string module, KSPVersion ksp_version = null)
}
}

/// <summary>
/// Returns the latest available version of a module that
/// satisifes the specified version. Takes into account module 'provides'
/// Throws a ModuleNotFoundException if asked for a non-existant module.
/// Returns null if there's simply no compatible version for this system.
/// </summary>
public List<CkanModule> LatestAvailableWithProvides(string module, KSPVersion ksp_version = null)
{
log.DebugFormat("Finding latest available for {0}", module);

// TODO: Check user's stability tolerance (stable, unstable, testing, etc)

List<CkanModule> modules = new List<CkanModule>();

try
{
var mod = LatestAvailable(module, ksp_version);
if (mod != null)
{
modules.Add(mod);
}
}
catch (ModuleNotFoundException)
{
foreach (var pair in available_modules)
{
if (pair.Value.Latest(ksp_version) == null)
{
continue;
}

var provides = pair.Value.Latest(ksp_version).provides;
if (provides != null)
{
foreach (var provided in provides)
{
if (provided == module)
{
modules.Add(pair.Value.Latest(ksp_version));
}
}
}
}
}

return modules;
}

/// <summary>
/// Register the supplied module as having been installed, thereby keeping
/// track of its metadata and files.
Expand Down
13 changes: 9 additions & 4 deletions CKAN/GUI/ApplyChangesDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ public ApplyChangesDialog()

public void ShowApplyChangesDialog(List<KeyValuePair<CkanModule, GUIModChangeType>> changeset,
BackgroundWorker installWorker)
{
Util.Invoke(ChangesListView, () => _ShowApplyChangesDialog(changeset, installWorker));
Util.Invoke(this, () => ShowDialog());
}

private void _ShowApplyChangesDialog(List<KeyValuePair<CkanModule, GUIModChangeType>> changeset,
BackgroundWorker installWorker)
{
m_Changeset = changeset;
m_InstallWorker = installWorker;

ChangesListView.Items.Clear();

foreach (var change in changeset)
Expand All @@ -39,8 +45,6 @@ public void ShowApplyChangesDialog(List<KeyValuePair<CkanModule, GUIModChangeTyp
item.SubItems.Add(subChangeType);
ChangesListView.Items.Add(item);
}

ShowDialog();
}

private void CancelButton_Click(object sender, EventArgs e)
Expand All @@ -54,7 +58,8 @@ private void CancelButton_Click(object sender, EventArgs e)
private void ConfirmButton_Click(object sender, EventArgs e)
{
RelationshipResolverOptions install_ops = RelationshipResolver.DefaultOpts();

install_ops.with_recommends = false;

m_InstallWorker.RunWorkerAsync(
new KeyValuePair<List<KeyValuePair<CkanModule, GUIModChangeType>>, RelationshipResolverOptions>(
m_Changeset, install_ops));
Expand Down
2 changes: 2 additions & 0 deletions CKAN/GUI/CKAN-GUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@
<DependentUpon>ApplyChangesDialog.cs</DependentUpon>
</Compile>
<Compile Include="Configuration.cs" />
<Compile Include="ControlFactory.cs" />
<Compile Include="ErrorDialog.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="ErrorDialog.Designer.cs">
<DependentUpon>ErrorDialog.cs</DependentUpon>
</Compile>
<Compile Include="Util.cs" />
<Compile Include="Main.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
4 changes: 3 additions & 1 deletion CKAN/GUI/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public static Configuration LoadConfiguration(string path)
public static void SaveConfiguration(Configuration configuration, string path)
{
var serializer = new XmlSerializer(typeof (Configuration));
serializer.Serialize(new StreamWriter(path), configuration);
var writer = new StreamWriter(path);
serializer.Serialize(writer, configuration);
writer.Close();
}
}
}
38 changes: 38 additions & 0 deletions CKAN/GUI/ControlFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;

namespace CKAN
{

// this class ensures that all controls are created from the same thread
// this is a mono limitation described here - http://www.mono-project.com/docs/faq/winforms/


public class ControlFactory
{

private int m_MainThreadID = 0;

public ControlFactory()
{
m_MainThreadID = Thread.CurrentThread.ManagedThreadId;
}

public T CreateControl<T>() where T : new()
{
if (Thread.CurrentThread.ManagedThreadId != m_MainThreadID)
{
Debugger.Break();
}

return new T();
}


}

}
16 changes: 4 additions & 12 deletions CKAN/GUI/ErrorDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,18 @@ public partial class ErrorDialog : Form
public ErrorDialog()
{
InitializeComponent();
StartPosition = FormStartPosition.CenterScreen;
}

public void ShowErrorDialog(string message)
{
if (ErrorMessage.InvokeRequired)
{
ErrorMessage.Invoke(new MethodInvoker(delegate { ErrorMessage.Text = message; }));
}
else
{
ErrorMessage.Text = message;
}

StartPosition = FormStartPosition.CenterScreen;
ShowDialog();
Util.Invoke(ErrorMessage, () => ErrorMessage.Text = message);
Util.Invoke(this, () => ShowDialog());
}

public void HideErrorDialog()
{
Close();
Util.Invoke(this, () => Close());
}

private void DismissButton_Click(object sender, EventArgs e)
Expand Down
Loading

0 comments on commit f041d48

Please sign in to comment.