Skip to content

Commit

Permalink
Merge pull request #1 from Noevain/testing
Browse files Browse the repository at this point in the history
Merging testing for 0.3.0
  • Loading branch information
Noevain authored Oct 5, 2024
2 parents 79d2be3 + 5bb08e9 commit b679b54
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 34 deletions.
3 changes: 2 additions & 1 deletion ModArchiveBrowser/ImageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public ImageHandler(string downloadDirectory)
{
_downloadDirectory = downloadDirectory;
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Add("User-Agent", "DalamudPluginModBrowser");
_downloadedFilenames = new HashSet<string>();//maybe fill this with cache dir or load from config later

// Check if it exist first
Expand Down Expand Up @@ -66,7 +67,7 @@ public string DownloadImage(string imageUrl)
}
catch (Exception ex)

Check warning on line 68 in ModArchiveBrowser/ImageHandler.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'ex' is declared but never used
{
Plugin.Logger.Error($"Failed to download image: {imageUrl}. Error: {ex.Message}");
//Plugin.ReportError($"Failed to download image: {imageUrl}. Error: {ex.Message}",ex);
return string.Empty;
}
}
Expand Down
2 changes: 1 addition & 1 deletion ModArchiveBrowser/ModArchiveBrowser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="Dalamud.Plugin.Bootstrap.targets" />

<PropertyGroup>
<Version>0.0.1</Version>
<Version>0.3.0</Version>
<Description>ModArchiveBrowser</Description>
<PlatformTarget>x64</PlatformTarget>
<TargetFramework>net8.0-windows</TargetFramework>
Expand Down
3 changes: 2 additions & 1 deletion ModArchiveBrowser/ModArchiveBrowser.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Author": "Noevain",
"Name": "XIV Mod Archive Browser",
"InternalName": "ModArchiveBrowser",
"AssemblyVersion": "0.0.1.0",
"AssemblyVersion": "0.3.0",
"Description": "Lets you browse and install mods in 1 click from www.xivmodarchive.com and add the modthumbnail in Penumbra",
"ApplicableVersion": "any",
"Tags": [
Expand All @@ -12,6 +12,7 @@
],
"DalamudApiLevel": 10,
"LoadRequiredState": 0,
"IconUrl": "https://raw.githubusercontent.com/noevain/ModArchiveBrowser/master/images/icon.png",
"LoadSync": false,
"CanUnloadAsync": false,
"LoadPriority": 0,
Expand Down
12 changes: 7 additions & 5 deletions ModArchiveBrowser/ModHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public ModHandler(string downloadDirectory,string thumbnailsDirectory, Plugin pl
_downloadDirectory = downloadDirectory;
_thumbnailDirectory = thumbnailsDirectory;
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Add("User-Agent", "DalamudPluginModBrowser");
_downloadedFilenames = plugin.Configuration.CacheFiles;
_modNameToThumbnail = plugin.Configuration.modNameToThumbnail;
this.plugin = plugin;
Expand Down Expand Up @@ -62,7 +63,7 @@ public double CalculateFolderSizeInMB()
{
if (!Directory.Exists(_downloadDirectory))
{
Plugin.Logger.Error("Directory does not exist.");
//Plugin.Logger.Error("Directory does not exist.");
return 0;
}

Expand All @@ -87,6 +88,7 @@ public async Task<string> DownloadModAsync(string modUrl)
{
try
{
modUrl = modUrl.Replace("&#39;", "'");
string fileName = Path.GetFileName(new Uri(modUrl).AbsolutePath);

if (_downloadedFilenames.Contains(fileName))
Expand All @@ -107,7 +109,7 @@ public async Task<string> DownloadModAsync(string modUrl)
}
catch (Exception ex)
{
Plugin.Logger.Error($"Failed to download mod: {modUrl}. Error: {ex.Message}");
Plugin.ReportError($"Failed to download mod: {modUrl}. Check /xllog for details",ex);
return null;
}
}
Expand All @@ -133,7 +135,7 @@ public string DownloadMod(string modUrl)
}
catch (Exception ex)
{
Plugin.Logger.Error($"Failed to download mod: {modUrl}. Error: {ex.Message}");
Plugin.ReportError($"Failed to download mod: {modUrl}. Check /xllog for details", ex);
return null;
}
}
Expand All @@ -142,7 +144,7 @@ public void InstallMod(string filePath,string imagepath)
{
if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath))
{
Plugin.Logger.Error("Invalid file path or file does not exist.");
Plugin.ReportError("Invalid file path or file does not exist.",null);
return;
}

Expand Down Expand Up @@ -175,7 +177,7 @@ public void InstallMod(string filePath,string imagepath)
}
else
{
Plugin.Logger.Error($"Unsupported file format: {extension}");
Plugin.ReportError($"Unsupported file format: {extension}",null);
}
}

Expand Down
31 changes: 31 additions & 0 deletions ModArchiveBrowser/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
using HtmlAgilityPack;
using ModArchiveBrowser.Interop.Penumbra;
using Dalamud.Interface.ImGuiFileDialog;
using Dalamud.Utility;
using Dalamud.Game.Text.SeStringHandling;
using System;

namespace ModArchiveBrowser;

Expand All @@ -18,6 +21,8 @@ public sealed class Plugin : IDalamudPlugin
[PluginService] internal static ICommandManager CommandManager { get; private set; } = null!;
[PluginService] internal static IPluginLog Logger { get; private set; } = null!;

[PluginService] internal static IChatGui ChatGui { get; private set; } = null!;

public Configuration Configuration { get; init; }

public readonly WindowSystem WindowSystem = new("ModArchiveBrowser");
Expand Down Expand Up @@ -62,6 +67,10 @@ public Plugin()
{
HelpMessage = "Display the config page"
});
CommandManager.AddHandler("/modid", new CommandInfo(OnCommand)
{
HelpMessage = "Manually display the corresponding mod in the mod window"
});
PluginInterface.UiBuilder.Draw += DrawUI;

// This adds a button to the plugin installer entry of this plugin which allows
Expand All @@ -72,6 +81,16 @@ public Plugin()
PluginInterface.UiBuilder.OpenMainUi += ToggleMainUI;
}

public static void ReportError(string msg,Exception? ex)
{
SeStringBuilder sb = new SeStringBuilder().AddText("[ModArchiveBrowser] Error:"+msg);
ChatGui.PrintError(sb.BuiltString);
if (ex is not null)
{
Plugin.Logger.Error(ex.ToString());
}
}

public void Dispose()
{
WindowSystem.RemoveAllWindows();
Expand All @@ -83,6 +102,7 @@ public void Dispose()
CommandManager.RemoveHandler("/archive");
CommandManager.RemoveHandler("/modsearch");
CommandManager.RemoveHandler("/archiveconfig");
CommandManager.RemoveHandler("/modid");
modHandler.Dispose();
penumbra.Dispose();
}
Expand All @@ -94,6 +114,17 @@ private void OnCommand(string command, string args)
case "/archive":MainWindow.Toggle();break;
case "/modsearch":searchWindow.Toggle();break;
case "/archiveconfig":ConfigWindow.Toggle();break;
case "/modId": if (!args.IsNullOrEmpty())
{
modWindow.ChangeMod(args);
modWindow.IsOpen = true;
modWindow.BringToFront();
}
else
{
ReportError("No argument",null);
}
break;
default:break;
}
}
Expand Down
11 changes: 9 additions & 2 deletions ModArchiveBrowser/Utils/ModStructs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ public struct ModMetadata//misc data about the mod,mostly so that the Mod ct isn
public string[] tags;
public string description;
public string affectReplace;
public DTCompatibility dTCompatibility;

public ModMetadata(string views, string downloads, string pins, string last_update, string release_date,
string[] races, string[] tags, string description, string affectReplace)
string[] races, string[] tags, string description, string affectReplace,DTCompatibility dT)
{
this.views = views;
this.downloads = downloads;
Expand All @@ -76,6 +77,7 @@ public ModMetadata(string views, string downloads, string pins, string last_upda
this.tags = tags;
this.description = description;
this.affectReplace = affectReplace;
this.dTCompatibility = dT;
}
}

Expand Down Expand Up @@ -124,7 +126,12 @@ public enum SortBy
{
Rank,
Time_posted,
Name
Name,
Time_edited,
Views,
Views_today,
Downloads,
Followers
}

public enum SortOrder
Expand Down
2 changes: 1 addition & 1 deletion ModArchiveBrowser/Utils/StaticHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static double CalculateFolderSizeInMB(string path)
{
if (!Directory.Exists(path))
{
Plugin.Logger.Error("Directory does not exist.");
//Plugin.ReportError("Directory does not exist.",null);
return 0;
}

Expand Down
65 changes: 63 additions & 2 deletions ModArchiveBrowser/WebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ namespace ModArchiveBrowser
internal class WebClient
{
public const string xivmodarchiveRoot = "https://www.xivmodarchive.com";
public const string new_and_updated_from_patreon_subs = "search?nsfl=false&sponsored=true&dt_compat=1&sortby=time_edited&sortorder=desc";
public const string today_most_viewed = "search?nsfl=false&dt_compat=1&sortby=views_today&sortorder=desc";
public const string newest_mods_from_all_users = "search?nsfl=false&dt_compat=1&sortby=time_published&sortorder=desc";
private static HtmlWeb clientInstance = null;
public static HtmlWeb ClientInstance
{
Expand All @@ -25,6 +28,7 @@ public static HtmlWeb ClientInstance
clientInstance = new HtmlWeb();
clientInstance.CachePath = Path.Combine(System.IO.Path.GetTempPath(), "modarchivebrowser\\htmlcache");
clientInstance.UsingCache = true;
clientInstance.UserAgent = "DalamudPluginModBrowser";
return clientInstance;
}
else
Expand All @@ -46,11 +50,22 @@ public static (Mod,HtmlNodeCollection) GetModPage(ModThumb modThumb)
string url = xivmodarchiveRoot + modThumb.url;
Plugin.Logger.Debug($"{url}");
HtmlDocument page = ClientInstance.Load(url);
HtmlNodeCollection descriptionNodeStart = page.DocumentNode.SelectNodes("//div[@id='info']/div");
HtmlNodeCollection descriptionNodeStart = page.DocumentNode.SelectNodes("//div[@id='info']");
Plugin.Logger.Debug("Request made");
return (ParseModPage(page,modThumb),descriptionNodeStart);
}

public static (Mod,HtmlNodeCollection) GetModPage(string modId)
{
string url = xivmodarchiveRoot+"/modid/"+modId;
Plugin.Logger.Debug($"{url}");
HtmlDocument page = ClientInstance.Load(url);
HtmlNodeCollection descriptionNodeStart = page.DocumentNode.SelectNodes("//div[@id='info']");
Plugin.Logger.Debug("Request made");
ModThumb mdThumb = GetModThumbFromFullPage(page,modId);
return(ParseModPage(page,mdThumb),descriptionNodeStart);
}

public static List<ModThumb> DoSearch(string searchUrl)
{
string url = xivmodarchiveRoot + '/' + searchUrl;
Expand All @@ -59,6 +74,31 @@ public static List<ModThumb> DoSearch(string searchUrl)
return ParseSearchResults(page);
}

public static ModThumb GetModThumbFromFullPage(HtmlDocument page,string url)
{
string title;
string thumbUrl;
string authorName;
string type;
string gender;
string views;
HtmlNodeCollection titleNode = page.DocumentNode.SelectNodes("//h1[contains(@class, 'display-5')]");
HtmlNodeCollection imageNode = page.DocumentNode.SelectNodes("//img[contains(@class, 'mod-carousel-image')]/@src");
HtmlNodeCollection authorNode = page.DocumentNode.SelectNodes("//a[contains(@class, 'user-card-link')]");
HtmlNodeCollection typeNodes = page.DocumentNode.SelectNodes("//div[contains(@class, 'col-8')]//p[contains(@class, 'lead')]");
HtmlNodeCollection genderNodes = page.DocumentNode.SelectNodes("/html/body/div[2]/div[2]/div[2]/div[1]/div[3]/div[6]/code/a");
HtmlNodeCollection viewsNodes = page.DocumentNode.SelectNodes("/html/body/div[2]/div[2]/div[2]/div[1]/div[3]/div[1]/div/span[1]/div/span[2]");
title = titleNode[0].InnerText;
thumbUrl = imageNode[0].GetAttributeValue("src", "none");
authorName = authorNode[0].InnerText;
type = typeNodes[0].InnerText;
gender = genderNodes[0].InnerText;
views = viewsNodes[0].InnerText;
return new ModThumb(title, url, authorName,thumbUrl,"none",type,gender,views);


}

public static Mod ParseModPage(HtmlDocument page,ModThumb thumb)//I know,I know,this is ugly
{
string profile_pic;
Expand All @@ -80,6 +120,27 @@ public static Mod ParseModPage(HtmlDocument page,ModThumb thumb)//I know,I know,
HtmlNodeCollection downloadsNodes = page.DocumentNode.SelectNodes("//span[contains(@class, 'emoji-block') and contains(@title, 'Downloads')]//span[contains(@class, 'count')]");
HtmlNodeCollection pinsNodes = page.DocumentNode.SelectNodes("//span[contains(@class, 'emoji-block') and contains(@title, 'Followers')]//span[contains(@class, 'count')]");
HtmlNodeCollection lastVersionUpdateNodes = page.DocumentNode.SelectNodes("//div[contains(@class, 'mod-meta-block')]//code[contains(@class, 'server-date')][1]");
HtmlNode dtCompatible = page.DocumentNode.SelectSingleNode(".//div[contains(@class, 'alert-success')]");
DTCompatibility dTCompatibility = DTCompatibility.FullyCompatible;
if(dtCompatible is null)
{
HtmlNode dtTexTools = page.DocumentNode.SelectSingleNode(".//div[contains(@class, 'alert-info')]");
dTCompatibility = DTCompatibility.TexToolsCompatible;
if(dtTexTools is null)
{
HtmlNode dtPartial = page.DocumentNode.SelectSingleNode(".//div[contains(@class, 'alert-warning')]");
dTCompatibility = DTCompatibility.PartiallyCompatible;
if(dtPartial is null)
{
HtmlNode dtFucked = page.DocumentNode.SelectSingleNode(".//div[contains(@class, 'alert-danger')]");
dTCompatibility = DTCompatibility.NotCompatible;
}
else
{
dTCompatibility = DTCompatibility.PartiallyCompatible;//should never happen but you never know
}
}
}
profile_pic = authorProfilePictureNodes[0].GetAttributeValue("src", "none");
download_url = downloadModButtonNodes[0].GetAttributeValue("href", "none");
if (affectsReplacesNodes != null)
Expand Down Expand Up @@ -109,7 +170,7 @@ public static Mod ParseModPage(HtmlDocument page,ModThumb thumb)//I know,I know,
//originalReleaseDate = "N/A";
string description = "I will implement description parsing/rendering,later";
ModMetadata modMetadata = new ModMetadata(views,downloads,pins,lastVersionUpdate,originalReleaseDate,
races,tags,description,affectReplace);
races,tags,description,affectReplace,dTCompatibility);
return (new Mod(thumb, download_url,profile_pic, modMetadata));


Expand Down
10 changes: 5 additions & 5 deletions ModArchiveBrowser/Windows/ConfigWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private void FileDialogModCallBack(bool valid,string path)
}
else
{
Plugin.Logger.Error("Error from filedialog,invalid folder");
Plugin.ReportError("Error from filedialog,invalid folder", null);
}
ResetFileDialog();
}
Expand All @@ -57,7 +57,7 @@ private void FileDialogImageCallBack(bool valid,string path)
}
else
{
Plugin.Logger.Error("Error from filedialog,invalid folder");
Plugin.ReportError("Error from filedialog,invalid folder", null);
}
ResetFileDialog();
}
Expand All @@ -71,7 +71,7 @@ private void FileDialogThumbsCallBack(bool valid, string path)
}
else
{
Plugin.Logger.Error("Error from filedialog,invalid folder");
Plugin.ReportError("Error from filedialog,invalid folder", null);
}
ResetFileDialog();
}
Expand Down Expand Up @@ -109,11 +109,11 @@ public override void Draw()
_openFileDialog = true;
}
ImGui.Separator();
if(ImGui.InputInt("Cache Size", ref cacheSize))
/*if(ImGui.InputInt("Cache Size", ref cacheSize))
{
Configuration.CacheSize = cacheSize;
Configuration.Save();
}
}*/
var modCachePath = Configuration.CacheModPath;
if (ImGui.InputText("Mod cache path",ref modCachePath,300))
{
Expand Down
Loading

0 comments on commit b679b54

Please sign in to comment.