Skip to content

Commit 9a70998

Browse files
committed
make all AgsGetCore actions into Async tasks.
This prevents UI lock.
1 parent 2a6157c commit 9a70998

10 files changed

Lines changed: 188 additions & 50 deletions

File tree

agsget/agsget/EditorPlugin/AGS.Plugin.AgsGet/AgsGet.Designer.cs

Lines changed: 42 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agsget/agsget/EditorPlugin/AGS.Plugin.AgsGet/AgsGetComponent.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ public AgsGetComponent(IAGSEditor editor)
3030
_editor.GUIController.RegisterIcon("AgsGetPluginIcon", GetIcon("PluginIcon.ico"));
3131
_editor.GUIController.ProjectTree.AddTreeRoot(this, CONTROL_ID_ROOT_NODE, "AgsGet plugin", "AgsGetPluginIcon");
3232
_pane = new ContentDocument(new AgsGetPane(editor), "Plugin pane", this);
33-
34-
_mainMenuItems = new MenuCommands(NEW_MAIN_MENU_ID);
35-
_mainMenuItems.Commands.Add(new MenuCommand(CONTROL_ID_MAIN_MENU_OPTION, "Main menu option"));
36-
_editor.GUIController.AddMenu(this, NEW_MAIN_MENU_ID, "AgsGetPlugin", _editor.GUIController.FileMenuID);
37-
_editor.GUIController.AddMenuItems(this, _mainMenuItems);
3833
}
3934

4035
private Icon GetIcon(string fileName)
@@ -50,19 +45,15 @@ string IEditorComponent.ComponentID
5045
IList<MenuCommand> IEditorComponent.GetContextMenu(string controlID)
5146
{
5247
List<MenuCommand> contextMenu = new List<MenuCommand>();
53-
contextMenu.Add(new MenuCommand(CONTROL_ID_CONTEXT_MENU_OPTION, "AgsGet context menu option"));
48+
contextMenu.Add(new MenuCommand(CONTROL_ID_CONTEXT_MENU_OPTION, "About..."));
5449
return contextMenu;
5550
}
5651

5752
void IEditorComponent.CommandClick(string controlID)
5853
{
5954
if (controlID == CONTROL_ID_CONTEXT_MENU_OPTION)
6055
{
61-
_editor.GUIController.ShowMessage("You clicked the context menu option!", MessageBoxIconType.Information);
62-
}
63-
else if (controlID == CONTROL_ID_MAIN_MENU_OPTION)
64-
{
65-
_editor.GUIController.ShowMessage("You clicked the main menu option!", MessageBoxIconType.Information);
56+
_editor.GUIController.ShowMessage("AgsGet Editor Plugin made by eri0o", MessageBoxIconType.Information);
6657
}
6758
else if (controlID == CONTROL_ID_ROOT_NODE)
6859
{

agsget/agsget/EditorPlugin/AGS.Plugin.AgsGet/AgsGetCore/Actions/ApplyDo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace AgsGetCore.Actions
44
{
55
public class ApplyDo
66
{
7-
public static int Do(Action<string> writerMethod, string changeRunDir, string packageName)
7+
public static async System.Threading.Tasks.Task<int> DoAsync(Action<string> writerMethod, string changeRunDir, string packageName)
88
{
99
BaseFiles.SetRunDirectory(changeRunDir);
1010

@@ -41,7 +41,7 @@ public static int Do(Action<string> writerMethod, string changeRunDir, string pa
4141

4242
//Update.3.Downloads the index of packages to `./ ags_packages_cache / package_index`.
4343
//If it already exists, overwrites it.
44-
PackageCacheIO.GetPackageIndex(writerMethod, null);
44+
await PackageCacheIO.GetPackageIndexAsync(writerMethod, null);
4545
}
4646

4747
//5. Check if PACKAGE_NAME exists on `./ ags_packages_cache / PACKAGE_NAME`,
@@ -58,7 +58,7 @@ public static int Do(Action<string> writerMethod, string changeRunDir, string pa
5858
}
5959

6060
//Get.5. Download PACKAGE_NAME to `./ags_packages_cache/PACKAGE_NAME`.
61-
if (!PackageCacheIO.GetPackage(
61+
if (!await PackageCacheIO.GetPackageAsync(
6262
writerMethod,
6363
Configuration.PackageIndexURL,
6464
packageName))

agsget/agsget/EditorPlugin/AGS.Plugin.AgsGet/AgsGetCore/Actions/GetDo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace AgsGetCore.Actions
44
{
55
public class GetDo
66
{
7-
public static int Do(Action<string> writerMethod, string changeRunDir, string packageName)
7+
public static async System.Threading.Tasks.Task<int> DoAsync(Action<string> writerMethod, string changeRunDir, string packageName)
88
{
99
BaseFiles.SetRunDirectory(changeRunDir);
1010

@@ -32,7 +32,7 @@ public static int Do(Action<string> writerMethod, string changeRunDir, string pa
3232

3333
//Update.3.Downloads the index of packages to `./ ags_packages_cache / package_index`.
3434
//If it already exists, overwrites it.
35-
PackageCacheIO.GetPackageIndex(writerMethod, null);
35+
PackageCacheIO.GetPackageIndexAsync(writerMethod, null);
3636
}
3737

3838
//4.Check if PACKAGE_NAME exists on `./ ags_packages_cache / package_index`, if not, exit with error.
@@ -44,7 +44,7 @@ public static int Do(Action<string> writerMethod, string changeRunDir, string pa
4444
}
4545

4646
//5. Download PACKAGE_NAME to `./ags_packages_cache/PACKAGE_NAME`.
47-
if(!PackageCacheIO.GetPackage(
47+
if(!await PackageCacheIO.GetPackageAsync(
4848
writerMethod,
4949
Configuration.PackageIndexURL,
5050
packageName))

agsget/agsget/EditorPlugin/AGS.Plugin.AgsGet/AgsGetCore/Actions/ListDo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace AgsGetCore.Actions
55
{
66
public class ListDo
77
{
8-
public static List<Package> Do(Action<string> writerMethod, string changeRunDir, string packageIndexURL, int pageSize, int pageNumber)
8+
public static async System.Threading.Tasks.Task<List<Package>> DoAsync(Action<string> writerMethod, string changeRunDir, string packageIndexURL, int pageSize, int pageNumber)
99
{
1010
BaseFiles.SetRunDirectory(changeRunDir);
1111

@@ -27,7 +27,7 @@ public static List<Package> Do(Action<string> writerMethod, string changeRunDir,
2727

2828
//Update.3.Downloads the index of packages to `./ ags_packages_cache / package_index`.
2929
//If it already exists, overwrites it.
30-
PackageCacheIO.GetPackageIndex(writerMethod, packageIndexURL);
30+
await PackageCacheIO.GetPackageIndexAsync(writerMethod, packageIndexURL);
3131
}
3232

3333
if (!BaseFiles.ExistsIndexFile())

agsget/agsget/EditorPlugin/AGS.Plugin.AgsGet/AgsGetCore/Actions/UpdateDo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class UpdateDo
88
// Otherwise, use default package index url.
99
// I think maybe the url should be moved
1010
//to a general option available for all commands, but not sure yet
11-
public static int Do(Action<string> writerMethod, string changeRunDir, string packageIndexURL)
11+
public static async System.Threading.Tasks.Task<int> DoAsync(Action<string> writerMethod, string changeRunDir, string packageIndexURL)
1212
{
1313
BaseFiles.SetRunDirectory(changeRunDir);
1414
//1. Checks if the command is run from a folder containing a valid Game.agf project.
@@ -24,7 +24,7 @@ public static int Do(Action<string> writerMethod, string changeRunDir, string pa
2424

2525
//3.Downloads the index of packages to `./ ags_packages_cache / package_index`.
2626
//If it already exists, overwrites it.
27-
PackageCacheIO.GetPackageIndex(writerMethod, packageIndexURL);
27+
await PackageCacheIO.GetPackageIndexAsync(writerMethod, packageIndexURL);
2828

2929
writerMethod("Success.");
3030
return 0;

agsget/agsget/EditorPlugin/AGS.Plugin.AgsGet/AgsGetCore/AgsGetCore.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ namespace AgsGetCore
77
{
88
class AgsGetCore
99
{
10-
public static int Apply(Action<string> writerMethod, string changeRunDir, string packageName)
10+
public static async System.Threading.Tasks.Task<int> ApplyAsync(Action<string> writerMethod, string changeRunDir, string packageName)
1111
{
1212

13-
return ApplyDo.Do(writerMethod, changeRunDir, packageName);
13+
return await ApplyDo.DoAsync(writerMethod, changeRunDir, packageName);
1414
}
1515

16-
public static int Get(Action<string> writerMethod, string changeRunDir, string packageName)
16+
public static async System.Threading.Tasks.Task<int> GetAsync(Action<string> writerMethod, string changeRunDir, string packageName)
1717
{
18-
return GetDo.Do(writerMethod, changeRunDir, packageName);
18+
return await GetDo.DoAsync(writerMethod, changeRunDir, packageName);
1919
}
2020

2121
public static int Pack(Action<string> writerMethod, string changeRunDir, string pairName)
@@ -28,14 +28,14 @@ public static int Search(Action<string> writerMethod, string changeRunDir, strin
2828
return SearchDo.Do(writerMethod, changeRunDir, searchQuery);
2929
}
3030

31-
public static int Update(Action<string> writerMethod, string changeRunDir, string packageIndexURL)
31+
public static async System.Threading.Tasks.Task<int> UpdateAsync(Action<string> writerMethod, string changeRunDir, string packageIndexURL)
3232
{
33-
return UpdateDo.Do(writerMethod, changeRunDir, packageIndexURL);
33+
return await UpdateDo.DoAsync(writerMethod, changeRunDir, packageIndexURL);
3434
}
3535

36-
public static List<Package> ListAll(Action<string> writerMethod, string changeRunDir, string packageIndexURL)
36+
public static async System.Threading.Tasks.Task<List<Package>> ListAllAsync(Action<string> writerMethod, string changeRunDir, string packageIndexURL)
3737
{
38-
return ListDo.Do(writerMethod, changeRunDir, packageIndexURL, 0, 0);
38+
return await ListDo.DoAsync(writerMethod, changeRunDir, packageIndexURL, 0, 0);
3939
}
4040
}
4141
}

agsget/agsget/EditorPlugin/AGS.Plugin.AgsGet/AgsGetCore/PackageCacheIO.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ namespace AgsGetCore
1111
// some commands are distributed on BaseFiles, maybe they need to be here and not there?
1212
public class PackageCacheIO
1313
{
14-
public static void GetPackageIndex(Action<string> writerMethod, string packageIndexUrl)
14+
public static async System.Threading.Tasks.Task GetPackageIndexAsync(Action<string> writerMethod, string packageIndexUrl)
1515
{
1616
if (string.IsNullOrEmpty(packageIndexUrl))
1717
{
18-
DownloadPretty.File(writerMethod, Configuration.PackageIndexURL + "index/package_index.json", BaseFiles.GetIndexFilePath());
18+
await DownloadPretty.FileAsync(writerMethod, Configuration.PackageIndexURL + "index/package_index.json", BaseFiles.GetIndexFilePath());
1919
}
2020
else
2121
{
22-
DownloadPretty.File(writerMethod, packageIndexUrl + "index/package_index.json", BaseFiles.GetIndexFilePath());
22+
await DownloadPretty.FileAsync(writerMethod, packageIndexUrl + "index/package_index.json", BaseFiles.GetIndexFilePath());
2323
}
2424
}
2525

@@ -52,7 +52,7 @@ public static bool IsPackageOnLocalCache(string packageName)
5252
return File.Exists(scriptModuleFile);
5353
}
5454

55-
public static bool GetPackage(Action<string> writerMethod, string packageIndexUrl, string packageName)
55+
public static async System.Threading.Tasks.Task<bool> GetPackageAsync(Action<string> writerMethod, string packageIndexUrl, string packageName)
5656
{
5757
var packageDirPath = Path.Combine(BaseFiles.GetCacheDirectoryPath(), packageName);
5858

@@ -67,7 +67,7 @@ public static bool GetPackage(Action<string> writerMethod, string packageIndexUr
6767
// for the package.
6868
var destinationFile = Path.Combine(packageDirPath, packageName + ".scm");
6969

70-
if (!DownloadPretty.File(writerMethod,
70+
if (!await DownloadPretty.FileAsync(writerMethod,
7171
packageIndexUrl + "pkgs/" + packageName + "/" + packageName + ".scm",
7272
destinationFile))
7373
{

agsget/agsget/EditorPlugin/AGS.Plugin.AgsGet/AgsGetCore/_Utils/DownloadPretty.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,30 @@ namespace AgsGetCore
77
{
88
public class DownloadPretty
99
{
10+
public static async System.Threading.Tasks.Task<bool> FileAsync(Action<string> writerMethod, string url, string filename) {
11+
var webClient = new WebClient();
12+
13+
writerMethod("Will download from:"+ url);
14+
15+
bool is_async = true;
16+
17+
if (writerMethod == Console.WriteLine) is_async = false;
18+
19+
20+
var downloadTask = webClient.DownloadFileTaskAsync(
21+
new Uri(url),
22+
filename,
23+
new Progress<Tuple<long, int, long>>(t =>
24+
{
25+
writerMethod(string.Format($"\r({t.Item2,25:#,###}) Bytes received: {t.Item1,25:#,###}/{t.Item3,25:#,###}"));
26+
}));
27+
28+
await downloadTask;
29+
30+
return true;
31+
}
32+
33+
1034
public static bool File(Action<string> writerMethod, string url, string filename) {
1135
var webClient = new WebClient();
1236

@@ -27,5 +51,6 @@ public static bool File(Action<string> writerMethod, string url, string filename
2751
})).Wait();
2852
return true;
2953
}
54+
3055
}
3156
}

0 commit comments

Comments
 (0)