Skip to content

Commit

Permalink
Finally I did it!
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaniru Rajapakse committed Jun 23, 2022
1 parent eb7776f commit e6d8884
Show file tree
Hide file tree
Showing 19 changed files with 760 additions and 217 deletions.
2 changes: 1 addition & 1 deletion ClientHandler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,4 @@ public Task WaitForExitTaskAsync()
});
}
}
}
}
2 changes: 1 addition & 1 deletion Package/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Identity
Name="SeaDevs.Launcher.UWP"
Publisher="CN=SeaDev Team"
Version="0.4.3.0" />
Version="0.4.5.0" />

<Properties>
<DisplayName>SD Launcher</DisplayName>
Expand Down
13 changes: 0 additions & 13 deletions SDLauncher UWP/Dialogs/Login.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,26 +342,13 @@ private void UpdateAccounts()
}
private void btnChooseAcc_Click(object sender, RoutedEventArgs e)
{
for (int i = vars.Accounts.Count - 1; i >= 0; i--)
{
try
{
vars.Accounts[i].PropertyChanged -= Login_PropertyChanged;
}
catch { }
vars.Accounts[i].PropertyChanged += Login_PropertyChanged;
}
ShowSelect(false, false);
gridChoose.Visibility = Visibility.Visible;
gridNew.Visibility = Visibility.Collapsed;
gridSettingsOnline.Visibility = Visibility.Collapsed;
UpdateAccounts();
}

private void Login_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
// UpdateAccounts();
}

private void btnBack_Click(object sender, RoutedEventArgs e)
{
Expand Down
169 changes: 160 additions & 9 deletions SDLauncher UWP/Helpers/Labrinth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Windows.Foundation;
using Windows.Networking.BackgroundTransfer;
using Windows.Storage;
using Windows.UI.Xaml;

namespace SDLauncher_UWP.Helpers
{
Expand All @@ -25,6 +29,8 @@ public class Labrinth
{
public event EventHandler StatusChanged = delegate { };
public event EventHandler<SDLauncher.UIChangeRequestedEventArgs> UIChangeRequested = delegate { };
public event EventHandler<SDLauncher.UIChangeRequestedEventArgs> MainUIChangeRequested = delegate { };
public event EventHandler<SDLauncher.ProgressChangedEventArgs> ProgressChanged = delegate { };
public HttpClient Client = new HttpClient();
//ModrinthClient c = new ModrinthClient();
public void Test()
Expand All @@ -38,19 +44,96 @@ private void UI(bool UI)
{
UIChangeRequested(this, new SDLauncher.UIChangeRequestedEventArgs(UI));
}
public async Task<LabrinthResults.SearchResult> Search(string name, int? limit = null)
int DownloadTaskID;
public async void DownloadMod(LabrinthResults.DownloadManager.File file,CmlLib.Core.MinecraftPath mcPath)
{
DownloadTaskID = LittleHelp.AddTask("Download " + file.filename);
MainUIChangeRequested(this, new SDLauncher.UIChangeRequestedEventArgs(false));
StorageFolder f = await StorageFolder.GetFolderFromPathAsync(mcPath.BasePath);
var m = await f.CreateFolderAsync("mods", CreationCollisionOption.OpenIfExists);
try
{
var mfile = await m.CreateFileAsync(file.filename, CreationCollisionOption.FailIfExists);
await mfile.DeleteAsync();
ModrinthDownload(file.url, mcPath.BasePath + @"\mods\", file.filename);
}
catch
{
var r = await MessageBox.Show("Information", "The file \"" + file.filename + "\" already exists in the mod folder.\nDo you want to download and replace it ?", MessageBoxButtons.YesNo);
if (r == MessageBoxResults.Yes)
{
ModrinthDownload(file.url, m.Path, file.filename);
}
else
{
this.MainUIChangeRequested(this, new SDLauncher.UIChangeRequestedEventArgs(true));
LittleHelp.CompleteTask(DownloadTaskID);
}
}
}
private async void ModrinthDownload(string link,string folderdir, string fileName)
{
try
{
Uri source = new Uri(link);

var folder = await StorageFolder.GetFolderFromPathAsync(folderdir);
var file = await folder.CreateFileAsync(
fileName,
CreationCollisionOption.ReplaceExisting);
string path = file.Path;
file = null;
using (var client = new HttpClientDownloadWithProgress(link, path))
{
client.ProgressChanged += (totalFileSize, totalBytesDownloaded, progressPercentage) => {
StatusChanged("Download " + fileName, new EventArgs());
this.ProgressChanged(this, new SDLauncher.ProgressChangedEventArgs(currentProg: Convert.ToInt32(progressPercentage)));
if(progressPercentage == 100)
{
this.DownloadFileCompleted();
client.Dispose();
}
};

await client.StartDownload();
}

}
catch
{

}
}
private void DownloadFileCompleted()
{
StatusChanged("Ready", new EventArgs());
ProgressChanged(this, new SDLauncher.ProgressChangedEventArgs(currentProg:0));
LittleHelp.CompleteTask(DownloadTaskID);
MainUIChangeRequested(this, new SDLauncher.UIChangeRequestedEventArgs(true));
}
public async Task<LabrinthResults.SearchResult> Search(string name, int? limit = null, LabrinthResults.SearchSortOptions sortOptions = LabrinthResults.SearchSortOptions.Relevance, LabrinthResults.SearchCategories[] categories = null)
{
int taskID = 0;
UI(false);
if (name == "")
{
StatusChanged("Getting Mods",new EventArgs());
taskID = LittleHelp.AddTask("Getting Mods");
taskID = LittleHelp.AddTask("Get Mods");
}
else
{
taskID = LittleHelp.AddTask("Searcging Store");
StatusChanged("Searcging Store", new EventArgs());
taskID = LittleHelp.AddTask("Search Store");
StatusChanged("Searching Store", new EventArgs());
}
string categouriesString = "";
if(categories != null)
{
if (categories.Count() > 0)
{
categouriesString = string.Join("\"],[\"categories:", categories);
categouriesString = ",[\"categories:" + categouriesString + "\"]";
categouriesString = categouriesString.ToLower();
}
}
LabrinthResults.SearchResult s = null;
try
Expand All @@ -65,7 +148,7 @@ private void UI(bool UI)
{
l = "limit=";
}
var json = await Util.DownloadText("https://api.modrinth.com/v2/search?query=" + q + "&facets=[[%22categories:fabric%22],[%22project_type:mod%22]]&" + l);
var json = await Util.DownloadText("https://api.modrinth.com/v2/search?query=" + q + "&index=" + sortOptions.ToString().ToLower() + "&facets=[[\"categories:fabric\"]" + categouriesString + ",[\"project_type:mod\"]]&" + l);
s = JSONConverter.ConvertToLabrinthSearchResult(json);
StatusChanged("Ready", new EventArgs());
UI(true);
Expand All @@ -80,17 +163,50 @@ private void UI(bool UI)
return null;
}
}
public async Task<LabrinthResults.ModrinthProject> GetProject(string id)
public async Task<LabrinthResults.ModrinthProject> GetProject(string id,bool UIChange = true)
{
int taskID = LittleHelp.AddTask("Loading Mod");
UI(false);
int taskID = LittleHelp.AddTask("Load Mod");
if (UIChange)
{
UI(false);
}
StatusChanged("Loading Mod",new EventArgs());
LabrinthResults.ModrinthProject s = null;
try
{
var json = await Util.DownloadText("https://api.modrinth.com/v2/project/" + id);
s = JSONConverter.ConvertToLabrinthProject(json);
StatusChanged("Ready", new EventArgs());
if (UIChange)
{
UI(true);
}
LittleHelp.CompleteTask(taskID);
return s;
}
catch
{
StatusChanged("Ready", new EventArgs());
if (UIChange)
{
UI(true);
}
LittleHelp.CompleteTask(taskID);
return null;
}
}
public async Task<List<LabrinthResults.DownloadManager.DownloadLink>> GetVersions(string id)
{
int taskID = LittleHelp.AddTask("Load Download versionss");
UI(false);
StatusChanged("Loading Mod downloads", new EventArgs());
List<LabrinthResults.DownloadManager.DownloadLink> s = null;
try
{
string link = "https://api.modrinth.com/v2/project/" + id + "/version";
var json = await Util.DownloadText(link);
s = JSONConverter.ConvertDownloadLinksToCS(json);
StatusChanged("Ready", new EventArgs());
UI(true);
LittleHelp.CompleteTask(taskID);
return s;
Expand All @@ -107,12 +223,47 @@ private void UI(bool UI)

public class LabrinthResults
{
public class DownloadManager
{
public class File
{
public Hashes hashes { get; set; }
public string url { get; set; }
public string filename { get; set; }
public bool primary { get; set; }
public int size { get; set; }
}
public class DownloadLink
{
public string id { get; set; }
public string project_id { get; set; }
public string author_id { get; set; }
public bool featured { get; set; }
public string name { get; set; }
public string version_number { get; set; }
public string changelog { get; set; }
public object changelog_url { get; set; }
public DateTime date_published { get; set; }
public int downloads { get; set; }
public string version_type { get; set; }
public List<File> files { get; set; }
public List<object> dependencies { get; set; }
public List<string> game_versions { get; set; }
public List<string> loaders { get; set; }
}
public class Hashes
{
public string sha512 { get; set; }
public string sha1 { get; set; }
}
}
public enum SearchSortOptions
{
Relevance,
Downloads,
Follows,
Updated
Updated,
Newest
}

public enum SearchCategories
Expand Down
59 changes: 6 additions & 53 deletions SDLauncher UWP/Helpers/ModManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public class StoreItem
public string Description { get; private set; }
public async Task<string> BigDescription()
{
var mod = await vars.Launcher.Labrinth.GetProject(ProjectID);
var mod = await vars.Launcher.Labrinth.GetProject(ProjectID,false);
return await Util.DownloadText(mod.body_url);
}
public string ProjectID { get; set; }
public string Author { get; set; }
public int Followers { get; set; }
public string FollowersString { get { return Followers.KiloFormat(); } }
public int TotalDownloads { get; set; }
public string TotalDownloadsString { get { return TotalDownloads.KiloFormat(); } }

public string[] SupportedVers { get; set; }

Expand All @@ -44,60 +46,11 @@ public List<BitmapImage> SampleImages
return b;
}
}
private List<string> features;
public List<string> Features
{
get
{
return features;
}
}
public BitmapImage Icon;
public List<StoreManager.StoreModDownloadLink> ModDownloadLinks { get; set; }
public List<StoreManager.StoreShaderDownloadLink> ShaderDownloadLinks { get; set; }
public List<LabrinthResults.DownloadManager.DownloadLink> DownloadLinks;
public StoreItem(object Item, int iD)
{
ModDownloadLinks = new List<StoreManager.StoreModDownloadLink>();
ShaderDownloadLinks = new List<StoreManager.StoreShaderDownloadLink>();
if (Item.GetType() == typeof(StoreManager.StoreShader))
{
Type = StoreManager.Type.Shader;
var i = (StoreManager.StoreShader)Item;
this.Description = i.Description;
this.Name = i.Name;
this.sampleImages = new List<string>();

if (i.SampleImages != null)
{
foreach (var item in i.SampleImages)
{
this.sampleImages.Add(item.Url);
}
}
try
{
this.Icon = new BitmapImage(new Uri(i.Icon));
}
catch { }


features = new List<string>();
if (i.Features != null)
{
foreach (var item in i.Features)
{
features.Add(item.Name);
}
}
if (i.DownloadLinks != null)
{
foreach (var item in i.DownloadLinks)
{
ShaderDownloadLinks.Add(item);
}
}
}
else if(Item.GetType() == typeof(LabrinthResults.Hit))
if(Item.GetType() == typeof(LabrinthResults.Hit))
{
var hit = (LabrinthResults.Hit)Item;
this.Name = hit.title;
Expand All @@ -113,7 +66,7 @@ public StoreItem(object Item, int iD)
{
sampleImages.Add(item);
}

DownloadLinks = new List<LabrinthResults.DownloadManager.DownloadLink>();
}
ID = iD;
}
Expand Down
12 changes: 12 additions & 0 deletions SDLauncher UWP/Helpers/SDLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,24 @@ public SDLauncher()
StoreManager = new StoreManager();
Labrinth = new Labrinth();
Labrinth.StatusChanged += Labrinth_StatusChanged;
Labrinth.MainUIChangeRequested += Labrinth_MainUIChangeRequested;
Labrinth.ProgressChanged += Labrinth_ProgressChanged;

this.TasksHelper = new TasksHelper();

this.FileOrProgressChanged += SDLauncher_FileOrProgressChanged;
}

private void Labrinth_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.FileOrProgressChanged(sender, e);
}

private void Labrinth_MainUIChangeRequested(object sender, UIChangeRequestedEventArgs e)
{
UI(e.UI);
}

private void SDLauncher_FileOrProgressChanged(object sender, ProgressChangedEventArgs e)
{
string stats = "";
Expand Down
Loading

0 comments on commit e6d8884

Please sign in to comment.