Skip to content

Commit 97d5f42

Browse files
committed
package search is working!
1 parent 9a70998 commit 97d5f42

File tree

5 files changed

+125
-19
lines changed

5 files changed

+125
-19
lines changed

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

Lines changed: 38 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,73 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace AgsGetCore.Actions
45
{
56
public class SearchDo
67
{
7-
public static int Do(Action<string> writerMethod, string changeRunDir, string searchQuery)
8+
public static async System.Threading.Tasks.Task<List<Package>> DoAsync(Action<string> writerMethod, string changeRunDir, string searchQuery)
89
{
9-
writerMethod("NOT IMPLEMENTED YET");
10-
writerMethod(string.Format("Search query: '{0}'", searchQuery));
10+
// we need a way to show report messages, if we can't, we return an error
11+
if (writerMethod == null) throw new ArgumentNullException(nameof(writerMethod), "was null");
1112

13+
// Update the directory to run AgsGet
14+
BaseFiles.SetRunDirectory(changeRunDir);
15+
16+
//1. Check if the command is run from a folder containing a valid Game.agf project. If not, exit with error.
17+
if (!GameAgfIO.Valid())
18+
{
19+
writerMethod("Not an AGS Game root directory.");
20+
writerMethod("You can only search packages on an AGS Game project.");
21+
return null;
22+
}
23+
24+
//2. Search Query can't be empty
1225
if (string.IsNullOrEmpty(searchQuery) == true)
1326
{
1427
writerMethod("No query to use for search.");
15-
return 1;
28+
return null;
29+
}
30+
31+
if(searchQuery.Length < 3)
32+
{
33+
writerMethod("Your query has to be at least 3 characters long.");
34+
return null;
35+
}
36+
37+
writerMethod(string.Format("You searched for: '{0}'...", searchQuery));
38+
39+
//3. Check if a package index exists on `./ags_packages_cache/package_index`, if not, it runs the functionality from `agsget update`.
40+
if (!BaseFiles.ExistsIndexFile())
41+
{
42+
writerMethod("No package index found, we are going to download one.");
43+
44+
//Search.2.If it is, creates a folder `./ ags_packages_cache /` on the directory if it doesn't exist.
45+
BaseFiles.CreatePackageDirIfDoesntExist();
46+
47+
//Search.3.Downloads the index of packages to `./ ags_packages_cache / package_index`.
48+
//If it already exists, overwrites it.
49+
await PackageCacheIO.GetPackageIndexAsync(writerMethod, null);
50+
}
51+
52+
if (!BaseFiles.ExistsIndexFile())
53+
{
54+
writerMethod("No package index found, it's possible a download failed due to connection error.");
55+
return null;
1656
}
1757

18-
writerMethod("-" );
19-
return 0;
58+
59+
var searchResults = PackageCacheIO.QueryPackages(searchQuery);
60+
61+
if(searchResults.Count == 1) writerMethod(string.Format("Found {0} result.", searchResults.Count));
62+
else writerMethod(string.Format("Found {0} results.", searchResults.Count));
63+
64+
string resultsAsString = "";
65+
66+
searchResults.ForEach(p => resultsAsString += " " + p.id);
67+
68+
writerMethod(resultsAsString);
69+
70+
return searchResults;
2071
}
2172
}
2273
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public static int Pack(Action<string> writerMethod, string changeRunDir, string
2323
return PackDo.Do(writerMethod, changeRunDir, pairName);
2424
}
2525

26-
public static int Search(Action<string> writerMethod, string changeRunDir, string searchQuery)
26+
public static async System.Threading.Tasks.Task<List<Package>> SearchAsync(Action<string> writerMethod, string changeRunDir, string searchQuery)
2727
{
28-
return SearchDo.Do(writerMethod, changeRunDir, searchQuery);
28+
return await SearchDo.DoAsync(writerMethod, changeRunDir, searchQuery);
2929
}
3030

3131
public static async System.Threading.Tasks.Task<int> UpdateAsync(Action<string> writerMethod, string changeRunDir, string packageIndexURL)

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using Newtonsoft.Json;
66
using Newtonsoft.Json.Linq;
7+
using System.Linq;
78

89
namespace AgsGetCore
910
{
@@ -87,6 +88,31 @@ public static int PackagesCount()
8788
return packageList.Count;
8889
}
8990

91+
public static List<Package> AllPackages()
92+
{
93+
var packageIndexAsString = File.ReadAllText(BaseFiles.GetIndexFilePath());
94+
95+
return JsonConvert.DeserializeObject<List<Package>>(packageIndexAsString);
96+
}
97+
98+
99+
public static List<Package> QueryPackages(string searchQuery)
100+
{
101+
var packageIndexAsString = File.ReadAllText(BaseFiles.GetIndexFilePath());
102+
103+
var packageList = JsonConvert.DeserializeObject<List<Package>>(packageIndexAsString);
104+
105+
var searchResult = packageList.Where<Package>(p => {
106+
return p.name.Contains(searchQuery) ||
107+
p.id.Contains(searchQuery.ToLower()) ||
108+
p.author.ToLower().Contains(searchQuery.ToLower()) ||
109+
p.text.Contains(searchQuery) ||
110+
p.keywords.ToLower().Contains(searchQuery.ToLower());
111+
});
112+
113+
return searchResult.ToList();
114+
}
115+
90116
public static List<Package> PackagesPage(int pageNumber, int pageSize)
91117
{
92118
if (pageSize < 0 || pageNumber < 0) return new List<Package>();
@@ -118,5 +144,6 @@ public class Package
118144
public string forum { get; set; }
119145
public string author { get; set; }
120146
public string depends { get; set; }
147+
public string keywords { get; set; }
121148
}
122149
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ private void InitializeComponent()
296296
// button_GetPackage
297297
//
298298
this.button_GetPackage.AutoSize = true;
299+
this.button_GetPackage.Enabled = false;
299300
this.button_GetPackage.Location = new System.Drawing.Point(241, 3);
300301
this.button_GetPackage.Name = "button_GetPackage";
301302
this.button_GetPackage.Size = new System.Drawing.Size(80, 23);

0 commit comments

Comments
 (0)