Skip to content

Commit

Permalink
Enable saving settings to the disk
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 committed Feb 19, 2024
1 parent 5793ce8 commit f52342b
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 13 deletions.
1 change: 1 addition & 0 deletions Localizations/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"replace": "Replace",
"resources": "Resources",
"save": "Save",
"save_settings_to_disk": "Save Settings to Disk",
"scene": "Scene",
"script": "Script",
"script_content_level_0": "Level 0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public sealed record class ExportSettings
/// </summary>
public TextExportMode TextExportMode { get; set; } = TextExportMode.Parse;

public bool SaveSettingsToDisk { get; set; }

public void Log()
{
Logger.Info(LogCategory.General, $"{nameof(AudioExportFormat)}: {AudioExportFormat}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ public class LibraryConfiguration : CoreConfiguration
public ProcessingSettings ProcessingSettings
{
get => SingletonData.GetStoredValue<ProcessingSettings>(nameof(ProcessingSettings));
set => SingletonData.GetValue<JsonDataInstance<ProcessingSettings>>(nameof(ProcessingSettings)).Value = value;
}

public ExportSettings ExportSettings
{
get => SingletonData.GetStoredValue<ExportSettings>(nameof(ExportSettings));
set => SingletonData.GetValue<JsonDataInstance<ExportSettings>>(nameof(ExportSettings)).Value = value;
}

public bool SaveSettingsToDisk => ExportSettings.SaveSettingsToDisk;

public LibraryConfiguration()
{
SingletonData.Add(nameof(ProcessingSettings), new JsonDataInstance<ProcessingSettings>(ProcessingExportSettingsContext.Default.ProcessingSettings));
SingletonData.Add(nameof(ExportSettings), new JsonDataInstance<ExportSettings>(ProcessingExportSettingsContext.Default.ExportSettings));
SingletonData.Add(nameof(ProcessingSettings), new JsonDataInstance<ProcessingSettings>(SerializedSettingsContext.Default.ProcessingSettings));
SingletonData.Add(nameof(ExportSettings), new JsonDataInstance<ExportSettings>(SerializedSettingsContext.Default.ExportSettings));
SingletonData.Add(nameof(EngineResourceData), new JsonDataInstance<EngineResourceData?>(EngineResourceDataContext.Default.NullableEngineResourceData));
}

Expand All @@ -28,4 +32,19 @@ public override void LogConfigurationValues()
ProcessingSettings.Log();
ExportSettings.Log();
}

public void LoadFromDefaultPath()
{
if (SerializedSettings.TryLoadFromDefaultPath(out SerializedSettings settings))
{
ImportSettings = settings.Import;
ProcessingSettings = settings.Processing;
ExportSettings = settings.Export;
}
}

public void SaveToDefaultPath()
{
new SerializedSettings(ImportSettings, ProcessingSettings, ExportSettings).SaveToDefaultPath();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using AssetRipper.Import.Configuration;
using AssetRipper.Import.Utils;
using System.Text.Json;

namespace AssetRipper.Export.UnityProjects.Configuration;

public readonly record struct SerializedSettings(ImportSettings Import, ProcessingSettings Processing, ExportSettings Export)
{
public const string DefaultFileName = "AssetRipper.Settings.json";

public static string DefaultFilePath => ExecutingDirectory.Combine(DefaultFileName);

public static SerializedSettings Load(string path)
{
return JsonSerializer.Deserialize(File.ReadAllText(path), SerializedSettingsContext.Default.SerializedSettings);
}

public static bool TryLoadFromDefaultPath(out SerializedSettings settings)
{
string path = DefaultFilePath;
if (File.Exists(path))
{
settings = Load(path);
return true;
}
settings = default;
return false;
}

public void Save(string path)
{
using FileStream fileStream = File.Create(path);
JsonSerializer.Serialize(fileStream, this, SerializedSettingsContext.Default.SerializedSettings);
}

public void SaveToDefaultPath()
{
Save(DefaultFilePath);
}

public static void DeleteDefaultPath()
{
string path = DefaultFilePath;
if (File.Exists(path))
{
File.Delete(path);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace AssetRipper.Export.UnityProjects.Configuration;

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(SerializedSettings))]
internal sealed partial class SerializedSettingsContext : JsonSerializerContext
{
}
5 changes: 5 additions & 0 deletions Source/AssetRipper.GUI.Localizations/Localization.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,11 @@ partial class Localization
/// </summary>
public static string Save => Get("save");

/// <summary>
/// Save Settings to Disk
/// </summary>
public static string SaveSettingsToDisk => Get("save_settings_to_disk");

/// <summary>
/// Scene
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion Source/AssetRipper.GUI.Web/GameFileLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class GameFileLoader
public static bool IsLoaded => GameData is not null;
public static GameBundle GameBundle => GameData!.GameBundle;
public static IAssemblyManager AssemblyManager => GameData!.AssemblyManager;
public static LibraryConfiguration Settings { get; } = new();
public static LibraryConfiguration Settings { get; } = LoadSettings();
private static ExportHandler exportHandler = new(Settings);
public static ExportHandler ExportHandler
{
Expand Down Expand Up @@ -56,4 +56,11 @@ public static void Export(string path)
ExportHandler.Export(GameData, path);
}
}

private static LibraryConfiguration LoadSettings()
{
LibraryConfiguration settings = new();
settings.LoadFromDefaultPath();
return settings;
}
}
23 changes: 23 additions & 0 deletions Source/AssetRipper.GUI.Web/Pages/Settings/SettingsPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ public override void WriteInnerContent(TextWriter writer)
WriteDropDownForScriptExportMode(writer);
}
}

using (new Div(writer).WithClass("row").End())
{
using (new Div(writer).WithClass("col").End())
{
}
using (new Div(writer).WithClass("col").End())
{
WriteCheckBoxForSaveSettingsToDisk(writer, Localization.SaveSettingsToDisk);
}
using (new Div(writer).WithClass("col").End())
{
}
}
}
}

Expand Down Expand Up @@ -233,6 +247,15 @@ public static Task HandlePostRequest(HttpContext context)
SetProperty(key, value);
}

if (Configuration.SaveSettingsToDisk)
{
Configuration.SaveToDefaultPath();
}
else
{
SerializedSettings.DeleteDefaultPath();
}

context.Response.Redirect("/Settings/Edit");
return Task.CompletedTask;
}
Expand Down
6 changes: 6 additions & 0 deletions Source/AssetRipper.GUI.Web/Pages/Settings/SettingsPage.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ private static void SetProperty(string key, string? value)
{
{ nameof(ImportSettings.IgnoreStreamingAssets), (value) => { Configuration.ImportSettings.IgnoreStreamingAssets = value; } },
{ nameof(ProcessingSettings.EnablePrefabOutlining), (value) => { Configuration.ProcessingSettings.EnablePrefabOutlining = value; } },
{ nameof(ExportSettings.SaveSettingsToDisk), (value) => { Configuration.ExportSettings.SaveSettingsToDisk = value; } },
};

private static void WriteDropDownForScriptContentLevel(TextWriter writer)
Expand Down Expand Up @@ -131,4 +132,9 @@ private static void WriteDropDownForTextExportMode(TextWriter writer)
{
WriteDropDown(writer, TextExportModeDropDownSetting.Instance, Configuration.ExportSettings.TextExportMode, nameof(ExportSettings.TextExportMode));
}

private static void WriteCheckBoxForSaveSettingsToDisk(TextWriter writer, string label)
{
WriteCheckBox(writer, label, Configuration.ExportSettings.SaveSettingsToDisk, nameof(ExportSettings.SaveSettingsToDisk));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class CoreConfiguration
public ImportSettings ImportSettings
{
get => SingletonData.GetStoredValue<ImportSettings>(nameof(ImportSettings));
set => SingletonData.GetValue<JsonDataInstance<ImportSettings>>(nameof(ImportSettings)).Value = value;
}

#endregion
Expand Down
12 changes: 12 additions & 0 deletions Source/AssetRipper.Import/Configuration/DataStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ public bool TryGetValue<TValue>(string key, [NotNullWhen(true)] out TValue? valu
}
}

public TValue GetValue<TValue>(string key) where TValue : T
{
if (TryGetValue(key, out TValue? storedValue))
{
return storedValue;
}
else
{
throw new KeyNotFoundException();
}
}

public void Add(string key, T value)
{
data.Add(key, value);
Expand Down

0 comments on commit f52342b

Please sign in to comment.