BeatSaber-version-changer/ui/VersionChanger.cs

195 lines
6.3 KiB
C#
Raw Normal View History

2022-05-12 00:35:22 +02:00
using BeatSaber_Versions.controller;
2022-05-14 19:00:01 +02:00
using BeatSaber_Versions.controller.helper;
2022-05-12 00:35:22 +02:00
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace BeatSaber_Versions
{
public partial class VersionChanger : Form
{
[DllImport("kernel32.dll")]
static extern bool CreateSymbolicLink(string lpSymlinkFileName,
string lpTargetFileName, int dwFlags);
private string Path = $@"{AppDomain.CurrentDomain.BaseDirectory}config.yaml";
private VersionConfigDict Versionen = null;
private String beatsaberfolder = @"A:\BS";
public VersionChanger()
{
InitializeComponent();
var deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance) // see height_in_inches in sample yml
.Build();
string yml = File.ReadAllText(Path);
var p = deserializer.Deserialize<VersionConfigDict>(yml);
Versionen = p;
beatsaberfolder = p.beatsaberfolder;
foreach (var key in (p.versions).Keys)
{
versionListBox.Items.Add(key);
}
}
private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
{
}
private void setFolder_Click(object sender, EventArgs e)
{
DialogResult folder = selectFolderForVersion.ShowDialog();
if(folder == DialogResult.OK)
{
MessageBox.Show("" + selectFolderForVersion.SelectedPath);
folderPath.Text = selectFolderForVersion.SelectedPath;
}
else
{
}
}
private void versionListBox_SelectedIndexChanged(object sender, EventArgs e)
{
var Item = ((sender as ListBox).SelectedItem);
if(Item != null)
{
folderPath.Text = Versionen.versions[(String)Item].path;
versionName.Text = Versionen.versions[(String)Item].name;
versionVersion.Text = Versionen.versions[(String)Item].version;
}
}
private void playSelected_Click(object sender, EventArgs e)
{
String Target = folderPath.Text;
if (Directory.Exists(beatsaberfolder))
{
2022-05-14 19:00:01 +02:00
if (staticHelpers.IsSymbolic(beatsaberfolder))
2022-05-12 00:35:22 +02:00
{
Directory.Delete(beatsaberfolder);
if (Directory.Exists(Target))
{
CreateSymbolicLink(beatsaberfolder, Target, 0x1);
setSongFolder();
}
}
}
else
{
if (Directory.Exists(Target))
{
CreateSymbolicLink(beatsaberfolder, Target, 0x1);
setSongFolder();
}
}
}
private void setSongFolder()
{
String songFolderConf = Versionen.beatsabersongs;
String songFolder = beatsaberfolder+ "\\Beat Saber_Data\\CustomLevels";
if (Directory.Exists(songFolder))
{
2022-05-14 19:00:01 +02:00
if (staticHelpers.IsSymbolic(songFolder))
2022-05-12 00:35:22 +02:00
{
MessageBox.Show("Is a symbolic link <3");
}
else
{
MessageBox.Show("Is a normal folder.");
}
}
else
{
DialogResult dialogResult = MessageBox.Show("Create symbolic link, may deduplicate your song library", "Settings", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
CreateSymbolicLink(songFolder, songFolderConf, 0x1);
MessageBox.Show("Created symbolic link to song folder.");
}
else if (dialogResult == DialogResult.No)
{
//do something else
}
}
}
private void saveSetting_Click(object sender, EventArgs e)
{
VersionConfig tmp = new VersionConfig();
tmp.path = folderPath.Text;
tmp.name = versionName.Text;
tmp.version = versionVersion.Text;
if (Versionen.versions.ContainsKey((String)tmp.version))
{
2022-05-14 19:00:01 +02:00
Versionen.versions[tmp.version] = tmp;
2022-05-12 00:35:22 +02:00
}
else
{
2022-05-14 19:00:01 +02:00
Versionen.versions.Add(tmp.version,tmp);
}
2022-05-12 00:35:22 +02:00
2022-05-14 19:00:01 +02:00
versionListBox.Items.Clear();
foreach (var key in (Versionen.versions).Keys)
{
versionListBox.Items.Add(key);
2022-05-12 00:35:22 +02:00
}
2022-05-14 19:00:01 +02:00
var serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
var yaml = serializer.Serialize(Versionen);
File.WriteAllTextAsync(Path,yaml);
System.Console.WriteLine(yaml);
2022-05-12 00:35:22 +02:00
}
2022-05-14 19:00:01 +02:00
private void addVersion(object sender,EventArgs e){
folderPath.Text = "";
versionName.Text = "";
versionVersion.Text = "";
versionListBox.ClearSelected();
}
private void deleteVersion(object sender, EventArgs e){
var Item = versionVersion.Text;
if(Item != null)
{
Versionen.versions.Remove((String)Item);
}
versionListBox.Items.Clear();
foreach (var key in (Versionen.versions).Keys)
{
versionListBox.Items.Add(key);
}
2022-05-12 00:35:22 +02:00
2022-05-14 19:00:01 +02:00
var serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
var yaml = serializer.Serialize(Versionen);
File.WriteAllTextAsync(Path,yaml);
System.Console.WriteLine(yaml);
2022-05-12 00:35:22 +02:00
}
2022-05-14 19:00:01 +02:00
private void settingBSFolder_Click(object sender, EventArgs e)
{
BeatSaberFolders_Modal mod = new BeatSaberFolders_Modal();
mod.Show();
}
2022-05-12 00:35:22 +02:00
}
}