Skip to content

Commit 80ec29a

Browse files
committed
feat: install and update mods from DLL or ZIP packages
Use the Windows file dialog and Android in-app file browser. Apply matching mod updates on restart, replacing package files while preserving unrelated files.
1 parent 0593c86 commit 80ec29a

13 files changed

Lines changed: 933 additions & 28 deletions

File tree

‎StArray.ModManager.Android/Managed.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ public static int Entry(int argc, IntPtr argv)
7272

7373
_ = RuntimeManager.Detect();
7474
var loader = new ModLoader(modsPath, AssemblyPath is { } ap ? Path.GetDirectoryName(ap) : null);
75-
var ui = new ModManagerUI(loader, Path.GetDirectoryName(AssemblyPath)!);
75+
var picker = new AndroidModFilePicker(modsPath);
76+
var ui = new ModManagerUI(loader, Path.GetDirectoryName(AssemblyPath)!, picker.PickAsync);
7677
HookHelper.Instance = new DobbyHook();
7778
ImGuiEGLRender.OnRender += ui.Render;
79+
ImGuiEGLRender.OnRender += picker.Render;
7880
ImGuiEGLRender.Install();
7981

8082
Logger.Info($"{nameof(Managed)}-Benchmark", $"=== Startup total: {Benchmark.End():F3}s ===");
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
using System.Numerics;
2+
using ImGuiNET;
3+
using StArray.ModManager.Manager;
4+
using StArray.ModManager.Resources;
5+
6+
namespace StArray.ModManager.Android.UI;
7+
8+
/// <summary>Browses local files with the host application's existing storage access.</summary>
9+
internal sealed class AndroidModFilePicker
10+
{
11+
private sealed record DirectoryListing(string[] Folders, string[] Files);
12+
13+
private readonly string _imports;
14+
private readonly string _downloads;
15+
private TaskCompletionSource<string?>? _selection;
16+
private Task<DirectoryListing>? _listing;
17+
private string _directory;
18+
private string? _selectedFile;
19+
private bool _openPopup;
20+
private bool _resetListScroll;
21+
22+
public AndroidModFilePicker(string modsPath)
23+
{
24+
string mods = Path.GetFullPath(modsPath);
25+
_imports = Path.Combine(Path.GetDirectoryName(mods) ?? mods, "imports");
26+
string? storage = Environment.GetEnvironmentVariable("EXTERNAL_STORAGE");
27+
_downloads = Path.Combine(!string.IsNullOrEmpty(storage) && Path.IsPathFullyQualified(storage)
28+
? storage
29+
: "/sdcard", "Download");
30+
_directory = _imports;
31+
}
32+
33+
public async Task<ModInstallFile?> PickAsync()
34+
{
35+
if (_selection != null)
36+
throw new InvalidOperationException(L10n.Get("ModInstall_InProgress"));
37+
38+
var selection = new TaskCompletionSource<string?>(TaskCreationOptions.RunContinuationsAsynchronously);
39+
_selection = selection;
40+
_openPopup = true;
41+
Navigate(_directory);
42+
43+
string? path = await selection.Task.ConfigureAwait(false);
44+
return path == null ? null : new ModInstallFile(Path.GetFileName(path), File.OpenRead(path));
45+
}
46+
47+
public void Render()
48+
{
49+
if (_selection == null)
50+
return;
51+
52+
string title = L10n.Get("ModInstall_PickerTitle") + "###AndroidModFilePicker";
53+
if (_openPopup)
54+
{
55+
_openPopup = false;
56+
ImGui.OpenPopup(title);
57+
}
58+
59+
var viewport = ImGui.GetMainViewport();
60+
ImGui.SetNextWindowPos(viewport.GetCenter(), ImGuiCond.Appearing, new Vector2(0.5f));
61+
ImGui.SetNextWindowSize(viewport.Size * 0.9f, ImGuiCond.Appearing);
62+
bool open = true;
63+
if (ImGui.BeginPopupModal(title, ref open))
64+
{
65+
RenderDirectory();
66+
ImGui.BeginDisabled(_selectedFile == null);
67+
if (ImGui.Button(L10n.Get("ModInstall_Confirm")))
68+
{
69+
Finish(_selectedFile);
70+
ImGui.CloseCurrentPopup();
71+
}
72+
ImGui.EndDisabled();
73+
74+
ImGui.SameLine();
75+
if (ImGui.Button(L10n.Get("ModInstall_Cancel")))
76+
{
77+
Finish(null);
78+
ImGui.CloseCurrentPopup();
79+
}
80+
81+
ImGui.EndPopup();
82+
}
83+
84+
if (!open || !ImGui.IsPopupOpen(title))
85+
Finish(null);
86+
}
87+
88+
private void RenderDirectory()
89+
{
90+
if (ImGui.Button(L10n.Get("ModInstall_Downloads")))
91+
Navigate(_downloads);
92+
ImGui.SameLine();
93+
if (ImGui.Button(L10n.Get("ModInstall_ImportFolder")))
94+
Navigate(_imports);
95+
96+
string? parent = Path.GetDirectoryName(_directory);
97+
ImGui.BeginDisabled(parent == null);
98+
if (ImGui.Button(L10n.Get("ModInstall_ParentFolder")) && parent != null)
99+
Navigate(parent);
100+
ImGui.EndDisabled();
101+
102+
ImGui.SameLine();
103+
if (ImGui.Button(L10n.Get("ModInstall_Refresh")))
104+
Navigate(_directory);
105+
106+
ImGui.PushTextWrapPos(0);
107+
ImGui.TextUnformatted(_directory);
108+
ImGui.TextUnformatted(L10n.Get("ModInstall_ImportHint", _imports));
109+
ImGui.PopTextWrapPos();
110+
111+
float availableHeight = ImGui.GetContentRegionAvail().Y - ImGui.GetFrameHeightWithSpacing();
112+
if (ImGui.BeginChild("ModFiles", new Vector2(0, Math.Max(1, availableHeight)), ImGuiChildFlags.None))
113+
RenderListing();
114+
ImGui.EndChild();
115+
}
116+
117+
private void RenderListing()
118+
{
119+
if (_listing is not { IsCompleted: true } listing)
120+
{
121+
ImGui.TextUnformatted(L10n.Get("ModInstall_ReadingFolder"));
122+
return;
123+
}
124+
125+
if (!listing.IsCompletedSuccessfully)
126+
{
127+
ImGui.PushTextWrapPos(0);
128+
string detail = listing.Exception?.GetBaseException().Message ?? _directory;
129+
ImGui.TextUnformatted(L10n.Get("ModInstall_FolderReadError", detail));
130+
ImGui.PopTextWrapPos();
131+
return;
132+
}
133+
134+
if (_resetListScroll)
135+
{
136+
ImGui.SetScrollY(0);
137+
_resetListScroll = false;
138+
}
139+
140+
DirectoryListing entries = listing.Result;
141+
if (entries.Folders.Length == 0 && entries.Files.Length == 0)
142+
ImGui.TextUnformatted(L10n.Get("ModInstall_EmptyFolder"));
143+
foreach (string folder in entries.Folders)
144+
{
145+
ImGui.PushID(folder);
146+
bool entered = ImGui.Selectable(Path.GetFileName(folder) + "/");
147+
ImGui.PopID();
148+
if (entered)
149+
{
150+
Navigate(folder);
151+
return;
152+
}
153+
}
154+
155+
foreach (string file in entries.Files)
156+
{
157+
ImGui.PushID(file);
158+
if (ImGui.Selectable(Path.GetFileName(file), _selectedFile == file))
159+
_selectedFile = file;
160+
ImGui.PopID();
161+
}
162+
}
163+
164+
private void Navigate(string directory)
165+
{
166+
_directory = directory;
167+
_selectedFile = null;
168+
_resetListScroll = true;
169+
_listing = Task.Run(() => ReadDirectory(directory, directory == _imports));
170+
}
171+
172+
private static DirectoryListing ReadDirectory(string directory, bool create)
173+
{
174+
if (create)
175+
Directory.CreateDirectory(directory);
176+
177+
return new DirectoryListing(
178+
Directory.GetDirectories(directory).Order(StringComparer.OrdinalIgnoreCase).ToArray(),
179+
Directory.EnumerateFiles(directory)
180+
.Where(path => Path.GetExtension(path).Equals(".zip", StringComparison.OrdinalIgnoreCase)
181+
|| Path.GetExtension(path).Equals(".dll", StringComparison.OrdinalIgnoreCase))
182+
.Order(StringComparer.OrdinalIgnoreCase).ToArray());
183+
}
184+
185+
private void Finish(string? path)
186+
{
187+
_selection?.TrySetResult(path);
188+
_selection = null;
189+
_listing = null;
190+
_selectedFile = null;
191+
}
192+
}

‎StArray.ModManager.Windows/Managed.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ public static int Entry(int argc, IntPtr argv)
8484
Il2CppFunctions.SetIl2CppLibraryPath(Path.Combine(DllParentPath, "..", "..", "GameAssembly.dll"));
8585
}
8686
HookHelper.Instance = new MinHook();
87-
ModManagerUI modManagerUI = new(new ModLoader(modsPath, DllParentPath), DllParentPath);
87+
ModManagerUI modManagerUI = new(new ModLoader(modsPath, DllParentPath),
88+
DllParentPath, WindowsModFilePicker.PickAsync);
8889

8990
NativeApi.SetBackend(1); // D3D11
9091
ImGuiDXRenderer.OnRender += modManagerUI.Render;
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System.ComponentModel;
2+
using System.Diagnostics;
3+
using System.Runtime.InteropServices;
4+
using StArray.ModManager.Manager;
5+
using StArray.ModManager.Resources;
6+
7+
namespace StArray.ModManager.Windows.UI;
8+
9+
internal static class WindowsModFilePicker
10+
{
11+
private const uint OfnNoChangeDir = 0x00000008;
12+
private const uint OfnPathMustExist = 0x00000800;
13+
private const uint OfnFileMustExist = 0x00001000;
14+
private const uint OfnExplorer = 0x00080000;
15+
16+
[StructLayout(LayoutKind.Sequential)]
17+
private struct OpenFileName
18+
{
19+
public uint Size;
20+
public nint Owner;
21+
public nint Instance;
22+
public nint Filter;
23+
public nint CustomFilter;
24+
public uint MaxCustomFilter;
25+
public uint FilterIndex;
26+
public nint File;
27+
public uint MaxFile;
28+
public nint FileTitle;
29+
public uint MaxFileTitle;
30+
public nint InitialDirectory;
31+
public nint Title;
32+
public uint Flags;
33+
public ushort FileOffset;
34+
public ushort ExtensionOffset;
35+
public nint DefaultExtension;
36+
public nint CustomData;
37+
public nint Hook;
38+
public nint TemplateName;
39+
public nint Reserved;
40+
public uint ReservedFlags;
41+
public uint FlagsEx;
42+
}
43+
44+
[DllImport("comdlg32.dll", ExactSpelling = true)]
45+
[return: MarshalAs(UnmanagedType.Bool)]
46+
private static extern bool GetOpenFileNameW(ref OpenFileName options);
47+
48+
[DllImport("comdlg32.dll", ExactSpelling = true)]
49+
private static extern uint CommDlgExtendedError();
50+
51+
public static Task<ModInstallFile?> PickAsync()
52+
{
53+
if (!OperatingSystem.IsWindows())
54+
throw new PlatformNotSupportedException();
55+
56+
var result = new TaskCompletionSource<ModInstallFile?>(TaskCreationOptions.RunContinuationsAsynchronously);
57+
var thread = new Thread(() =>
58+
{
59+
try
60+
{
61+
result.SetResult(Pick());
62+
}
63+
catch (Exception exception)
64+
{
65+
result.SetException(exception);
66+
}
67+
}) { IsBackground = true, Name = "Mod file picker" };
68+
thread.SetApartmentState(ApartmentState.STA);
69+
thread.Start();
70+
return result.Task;
71+
}
72+
73+
private static unsafe ModInstallFile? Pick()
74+
{
75+
string title = L10n.Get("ModInstall_PickerTitle");
76+
string filter = L10n.Get("ModInstall_FileFilter") + "\0*.zip;*.dll\0\0";
77+
var file = new char[32_768];
78+
using var process = Process.GetCurrentProcess();
79+
fixed (char* titlePointer = title, filterPointer = filter, filePointer = file)
80+
{
81+
var options = new OpenFileName
82+
{
83+
Size = (uint)sizeof(OpenFileName),
84+
Owner = process.MainWindowHandle,
85+
Title = (nint)titlePointer,
86+
Filter = (nint)filterPointer,
87+
FilterIndex = 1,
88+
File = (nint)filePointer,
89+
MaxFile = (uint)file.Length,
90+
Flags = OfnExplorer | OfnNoChangeDir | OfnFileMustExist | OfnPathMustExist
91+
};
92+
if (!GetOpenFileNameW(ref options))
93+
{
94+
uint error = CommDlgExtendedError();
95+
if (error == 0)
96+
return null;
97+
throw new Win32Exception((int)error, L10n.Get("ModInstall_WindowsPickerError", error));
98+
}
99+
}
100+
101+
string path = new(file, 0, Array.IndexOf(file, '\0'));
102+
return new ModInstallFile(Path.GetFileName(path), File.OpenRead(path));
103+
}
104+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace StArray.ModManager.Manager;
2+
3+
/// <summary>A file selected by the platform picker. The installer takes ownership of the stream.</summary>
4+
public sealed record ModInstallFile(string Name, Stream Content);

0 commit comments

Comments
 (0)