Skip to content

Commit

Permalink
config manager done basically
Browse files Browse the repository at this point in the history
  • Loading branch information
notcarlton committed Jan 8, 2024
1 parent c748932 commit 76de2c1
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 4 deletions.
72 changes: 72 additions & 0 deletions JiayiLauncher/Features/Mods/ModConfigManager.cs
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
});
}
}
68 changes: 68 additions & 0 deletions JiayiLauncher/Modals/ConfigManager.razor
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);
}
}

}
61 changes: 61 additions & 0 deletions JiayiLauncher/Modals/ConfigManager.razor.css
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;
}
1 change: 1 addition & 0 deletions JiayiLauncher/Modals/PrepareThemePublish.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@using System.Diagnostics

@inject IToastService ToastService;

<div class="edit">
<div class="name">
<p>Theme name</p>
Expand Down
2 changes: 1 addition & 1 deletion JiayiLauncher/Pages/Mods.razor
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
_hovered = false;
}

// Support for importing a list of urls/folder (as dragging a folder just creates file)
// TODO: Support for importing a list of urls/folder (as dragging a folder just creates file)
private async Task FileDropped(InputFileChangeEventArgs obj)
{
_hovered = false;
Expand Down
18 changes: 17 additions & 1 deletion JiayiLauncher/Shared/Components/Mods/JiayiModCard.razor
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</div>
</div>
<p class="mod-version">Works on @(string.Join(", ", Mod.SupportedVersions).ToLower())</p>
<div class="mod-controls">
<div class="mod-controls" style="@(_expanded ? "margin-left: -7rem" : "")">
@if (_isValid)
{
if (Mod.FromInternet && InternetManager.OfflineMode)
Expand All @@ -73,6 +73,12 @@
}

<div class="right">
<span class="material-symbols-sharp" title="@(_expanded ? "Hide controls" : "Show controls")"
@onclick="() => _expanded = !_expanded">
@(_expanded ? "remove" : "more_horiz")
</span>

<span class="material-symbols-sharp" title="Manage configs" @onclick="ConfigManagerClicked">folder_managed</span>
<span class="material-symbols-sharp" title="Edit mod" @onclick="EditClicked">edit</span>
<span class="material-symbols-sharp" style="color: darkred"
title="Delete mod (irreversible)" @onclick="DeleteClicked">delete</span>
Expand All @@ -93,6 +99,7 @@

private bool _launching;
private bool _isValid = true;
private bool _expanded;
private string _showLoadingBar => _launching ? "0.2" : "0";

private JiayiButton? _launchButton;
Expand Down Expand Up @@ -231,4 +238,13 @@
Mod.Delete();
Mods.Instance?.Refresh();
}

private void ConfigManagerClicked()
{
var parameters = new ModalParameters()
.Add(nameof(ConfigManager.Mod), Mod);

ModalService.Show<ConfigManager>("Config manager", parameters);
}

}
6 changes: 4 additions & 2 deletions JiayiLauncher/Shared/Components/Mods/JiayiModCard.razor.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
border-radius: var(--rounding);
border: var(--border-thickness) solid var(--border-primary);
border-top: 5px solid var(--accent);
overflow: hidden;
}

.mod-controls {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
gap: 0.35rem;
gap: 2.8em;
transition: var(--transition-speed);
}

.mod-header {
Expand All @@ -25,7 +27,7 @@
}
.mod-header .right, .mod-controls .right {
display: flex;
gap: 0.5em;
gap: 0.6em;
}

.mod-version {
Expand Down

0 comments on commit 76de2c1

Please sign in to comment.