-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c748932
commit 76de2c1
Showing
7 changed files
with
224 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using JiayiLauncher.Features.Game; | ||
using JiayiLauncher.Utils; | ||
using Microsoft.AspNetCore.Components.Forms; | ||
|
||
namespace JiayiLauncher.Features.Mods; | ||
|
||
public class ModConfigManager | ||
{ | ||
public readonly string ConfigPath; | ||
//private readonly string _configExtension; // maybe in the future | ||
|
||
public ModConfigManager(Mod mod) | ||
{ | ||
var modDataFolder = Path.Combine(PackageData.GetGameDataPath(), "RoamingState", mod.DataFolderName); | ||
|
||
// find config folder (could be Config, config, configs, configurations, etc) | ||
var configFolder = Directory.GetDirectories(modDataFolder) | ||
.FirstOrDefault(x => x.Contains("config", System.StringComparison.OrdinalIgnoreCase)); | ||
|
||
ConfigPath = configFolder ?? string.Empty; | ||
} | ||
|
||
public async Task AddConfig(IBrowserFile file) | ||
{ | ||
var fileName = Path.GetFileName(file.Name); | ||
var filePath = Path.Combine(ConfigPath, fileName); | ||
|
||
if (File.Exists(filePath)) File.Delete(filePath); | ||
|
||
await using var stream = File.Create(filePath); | ||
await file.OpenReadStream().CopyToAsync(stream); | ||
|
||
Log.Write(this, $"Added config {fileName}"); | ||
} | ||
|
||
public void RemoveConfig(string path) | ||
{ | ||
var configName = Path.GetFileName(path); | ||
var configPath = Path.Combine(ConfigPath, configName); | ||
|
||
if (!File.Exists(configPath)) return; | ||
|
||
File.Delete(configPath); | ||
|
||
Log.Write(this, $"Removed config {configName}"); | ||
} | ||
|
||
public List<string> GetConfigs() | ||
{ | ||
return Directory.GetFiles(ConfigPath).ToList(); | ||
} | ||
|
||
public void OpenConfig(string path) | ||
{ | ||
var configName = Path.GetFileName(path); | ||
var configPath = Path.Combine(ConfigPath, configName); | ||
|
||
if (!File.Exists(configPath)) return; | ||
|
||
Process.Start(new ProcessStartInfo | ||
{ | ||
FileName = "notepad.exe", | ||
Arguments = configPath, | ||
UseShellExecute = true | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
@using JiayiLauncher.Features.Mods | ||
@using System.IO | ||
|
||
<div class="config-manager"> | ||
@if (Mod!.DataFolderName == string.Empty) | ||
{ | ||
<p>This mod doesn't have a data folder name set. <br> | ||
To access the config manager, edit this mod and set the data folder name.</p> | ||
} | ||
else if (_configManager!.ConfigPath == string.Empty) | ||
{ | ||
<p>The data folder you set for this mod doesn't exist. <br> | ||
Make sure you have the correct data folder name set.</p> | ||
} | ||
else | ||
{ | ||
<ul class="config-list"> | ||
@foreach (var config in _configManager!.GetConfigs()) | ||
{ | ||
<li class="config-item"> | ||
<p style="margin: 0">@Path.GetFileNameWithoutExtension(config)</p> | ||
<div class="config-item-buttons"> | ||
<span class="material-symbols-sharp" title="Open and edit config" | ||
@onclick="() => _configManager!.OpenConfig(config)">edit</span> | ||
<span class="material-symbols-sharp" title="Delete config (irreversible)" style="color: darkred" | ||
@onclick="() => _configManager!.RemoveConfig(config)">delete</span> | ||
</div> | ||
</li> | ||
} | ||
</ul> | ||
|
||
<label class="drag-area"> | ||
Click here or drop files to add configs | ||
<InputFile OnChange="FileDropped" multiple /> | ||
</label> | ||
} | ||
|
||
<JiayiButton Size="JiayiButton.ButtonSize.Small" OnClick="Close">Okay</JiayiButton> | ||
</div> | ||
|
||
@code { | ||
[Parameter] | ||
public Mod? Mod { get; set; } | ||
|
||
[CascadingParameter] | ||
private BlazoredModalInstance Modal { get; set; } = default!; | ||
|
||
private ModConfigManager? _configManager; | ||
|
||
protected override void OnInitialized() | ||
{ | ||
_configManager = new ModConfigManager(Mod!); | ||
} | ||
|
||
private async Task Close() | ||
{ | ||
await Modal.CloseAsync(); | ||
} | ||
|
||
private async Task FileDropped(InputFileChangeEventArgs arg) | ||
{ | ||
foreach (var file in arg.GetMultipleFiles()) | ||
{ | ||
await _configManager!.AddConfig(file); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
.config-manager { | ||
width: 350px; | ||
max-height: 300px; | ||
overflow: auto; | ||
} | ||
|
||
.config-list { | ||
list-style: none; | ||
padding: 0; | ||
margin-right: 0.5rem; | ||
} | ||
|
||
.config-item { | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
padding: 0.5rem; | ||
|
||
border-left: 5px solid var(--accent); | ||
} | ||
|
||
.config-item:nth-child(even) { | ||
background-color: rgba(100, 100, 100, 0.1); | ||
} | ||
|
||
.config-item-buttons { | ||
display: flex; | ||
gap: 0.5rem; | ||
} | ||
|
||
.config-item-buttons .material-symbols-sharp { | ||
cursor: pointer; | ||
transition: var(--transition-speed); | ||
opacity: 0; | ||
} | ||
|
||
.config-item:hover .config-item-buttons .material-symbols-sharp { | ||
opacity: 100%; | ||
} | ||
|
||
.drag-area { | ||
display: block; | ||
border: 2px dashed var(--text-grayed); | ||
border-radius: var(--rounding); | ||
text-align: center; | ||
padding: 0.5rem; | ||
color: var(--text-grayed); | ||
cursor: pointer; | ||
margin-right: 0.5rem; | ||
margin-bottom: 0.5rem; | ||
|
||
transition: var(--transition-speed); | ||
} | ||
|
||
.drag-area:hover { | ||
filter: brightness(1.5); | ||
} | ||
|
||
::deep input[type="file"] { | ||
display: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters