mirror of
https://github.com/AssetRipper/AssetRipper.git
synced 2025-12-11 20:15:29 +01:00
138 lines
3.8 KiB
C#
138 lines
3.8 KiB
C#
using AssetRipper.Assets.Bundles;
|
|
using AssetRipper.Export.Configuration;
|
|
using AssetRipper.Export.PrimaryContent;
|
|
using AssetRipper.Export.UnityProjects;
|
|
using AssetRipper.Import.Logging;
|
|
using AssetRipper.Import.Structure.Assembly.Managers;
|
|
using AssetRipper.IO.Files;
|
|
using AssetRipper.NativeDialogs;
|
|
using AssetRipper.Processing;
|
|
|
|
namespace AssetRipper.GUI.Web;
|
|
|
|
public static class GameFileLoader
|
|
{
|
|
private static GameData? GameData { get; set; }
|
|
[MemberNotNullWhen(true, nameof(GameData))]
|
|
public static bool IsLoaded => GameData is not null;
|
|
public static GameBundle GameBundle => GameData!.GameBundle;
|
|
public static IAssemblyManager AssemblyManager => GameData!.AssemblyManager;
|
|
public static FullConfiguration Settings { get; } = LoadSettings();
|
|
|
|
public static ExportHandler ExportHandler
|
|
{
|
|
private get;
|
|
set
|
|
{
|
|
ArgumentNullException.ThrowIfNull(value);
|
|
value.ThrowIfSettingsDontMatch(Settings);
|
|
field = value;
|
|
}
|
|
} = new(Settings);
|
|
|
|
/// <summary>
|
|
/// Is this the premium edition?
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This is purely for UI functionality and has no direct effect on the presense of features.
|
|
/// </remarks>
|
|
public static bool Premium => ExportHandler.GetType() != typeof(ExportHandler);
|
|
|
|
public static void Reset()
|
|
{
|
|
if (GameData is not null)
|
|
{
|
|
GameData = null;
|
|
Logger.Info(LogCategory.General, "Data was reset.");
|
|
}
|
|
}
|
|
|
|
public static void LoadAndProcess(IReadOnlyList<string> paths)
|
|
{
|
|
Reset();
|
|
Settings.LogConfigurationValues();
|
|
GameData = ExportHandler.LoadAndProcess(paths, LocalFileSystem.Instance);
|
|
}
|
|
|
|
public static async Task ExportUnityProject(string path)
|
|
{
|
|
if (IsLoaded && IsValidExportDirectory(path))
|
|
{
|
|
if (IsNonEmptyDirectory(path))
|
|
{
|
|
if (!await UserConsentsToDeletion())
|
|
{
|
|
Logger.Info(LogCategory.Export, "User declined to delete existing export directory. Aborting export.");
|
|
return;
|
|
}
|
|
Directory.Delete(path, true);
|
|
}
|
|
|
|
Directory.CreateDirectory(path);
|
|
ExportHandler.Export(GameData, path, LocalFileSystem.Instance);
|
|
}
|
|
}
|
|
|
|
public static async Task ExportPrimaryContent(string path)
|
|
{
|
|
if (IsLoaded && IsValidExportDirectory(path))
|
|
{
|
|
if (IsNonEmptyDirectory(path))
|
|
{
|
|
if (!await UserConsentsToDeletion())
|
|
{
|
|
Logger.Info(LogCategory.Export, "User declined to delete existing export directory. Aborting export.");
|
|
return;
|
|
}
|
|
Directory.Delete(path, true);
|
|
}
|
|
|
|
Directory.CreateDirectory(path);
|
|
Logger.Info(LogCategory.Export, "Starting primary content export");
|
|
Logger.Info(LogCategory.Export, $"Attempting to export assets to {path}...");
|
|
Settings.ExportRootPath = path;
|
|
PrimaryContentExporter.CreateDefault(GameData, Settings).Export(GameBundle, Settings, LocalFileSystem.Instance);
|
|
Logger.Info(LogCategory.Export, "Finished exporting primary content.");
|
|
}
|
|
}
|
|
|
|
private static FullConfiguration LoadSettings()
|
|
{
|
|
FullConfiguration settings = new();
|
|
settings.LoadFromDefaultPath();
|
|
return settings;
|
|
}
|
|
|
|
private static bool IsValidExportDirectory(string path)
|
|
{
|
|
if (string.IsNullOrEmpty(path))
|
|
{
|
|
Logger.Error(LogCategory.Export, "Export path is empty");
|
|
return false;
|
|
}
|
|
string directoryName = Path.GetFileName(path);
|
|
if (directoryName is "Desktop" or "Documents" or "Downloads")
|
|
{
|
|
Logger.Error(LogCategory.Export, $"Export path '{path}' is a system directory");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private static bool IsNonEmptyDirectory(string path)
|
|
{
|
|
return Directory.Exists(path) && Directory.EnumerateFileSystemEntries(path).Any();
|
|
}
|
|
|
|
private static async Task<bool> UserConsentsToDeletion()
|
|
{
|
|
ConfirmationDialog.Options options = new()
|
|
{
|
|
Message = Localization.ExportDirectoryDeleteUserConfirmation,
|
|
Type = ConfirmationDialog.Type.YesNo,
|
|
};
|
|
bool? result = await ConfirmationDialog.Confirm(options);
|
|
return result ?? false;
|
|
}
|
|
}
|