Skip to content

Commit

Permalink
feat(cli): finalize Write action
Browse files Browse the repository at this point in the history
  • Loading branch information
cengelha committed Jan 30, 2025
1 parent c727df4 commit e73c43f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 55 deletions.
2 changes: 1 addition & 1 deletion libNOM.cli/Args/WriteArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public class WriteArgs
public required FormatEnum Format { get; set; }

[ArgRange(0, 29), ArgRequired, ArgDescription("Index of the save you want to write. The index is expected to be: 0 for Slot1Auto, 1 for Slot1Manual, 2 for Slot2Auto, etc)."), ArgPosition(3)]
public int? Index { get; set; }
public int Index { get; set; }
}
2 changes: 1 addition & 1 deletion libNOM.cli/Executor_Analyze.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private static void AnalyzeFile(FileInfo info, int indentionLevel)

WriteLine(info.Name, indentionLevel);

var container = io.Global.Analyze.AnalyzeFile(info.FullName);
var container = io.Global.Analyze.AnalyzeFile(info);
if (container is null)
WriteLine("Analysis failed.", indentionLevel + 1);
else
Expand Down
2 changes: 1 addition & 1 deletion libNOM.cli/Executor_Backup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private static void GetBackupDataFromDirectory(BackupArgs args, out IPlatform? p

private static void GetBackupDataFromFile(BackupArgs args, out IPlatform? platform, out IEnumerable<IContainer> containers)
{
var container = io.Global.Analyze.AnalyzeFile(args.Input, GetPlatformSettings());
var container = io.Global.Analyze.AnalyzeFile(new FileInfo(args.Input), GetPlatformSettings());
if (container is null)
{
WriteLine("Input file could not be successfully processed.", 1);
Expand Down
2 changes: 1 addition & 1 deletion libNOM.cli/Executor_Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void Convert(ConvertArgs args)
else
{
var platform = Enum.Parse<io.Enums.PlatformEnum>(args.Format.ToString());
io.Global.Convert.ToSaveFile(args.Input.FullName, platform, args.Output?.FullName);
io.Global.Convert.ToSaveFile(args.Input, platform, args.Output?.FullName);
}
}
}
11 changes: 3 additions & 8 deletions libNOM.cli/Executor_Write.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ public partial class Executor
]
public static void Write(WriteArgs args)
{
// 1. Read from stdin
// 2. Determine Platform/Location
// 3. Write

//var json = Console.In.ReadToEnd();
var json = io.Global.Convert.ToJson("C:\\Users\\Christian\\AppData\\Roaming\\HelloGames\\NMS\\st_76561198042453834\\save3.hg", false, false);

var json = Console.In.ReadToEnd();
var platform = Enum.Parse<io.Enums.PlatformEnum>(args.Format.ToString());
io.Global.Convert.ToSaveFile(json, platform, args.Output?.FullName); // TODO: change to take a file or a JSON string

io.Global.Convert.ToSaveFile(json, platform, args.Index, args.Output?.FullName);
}
}
2 changes: 1 addition & 1 deletion libNOM.cli/libNOM.cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

<!-- Package -->
<PropertyGroup Label="General">
<Version>1.1.0-beta.3</Version>
<Version>1.1.0-beta.4</Version>
<Authors>cengelha</Authors>
<Description>CLI for libNOM.io to analyze single files or whole directories and print information about it, convert between JSON and actual save formats and perform file operations.</Description>
<Copyright>Copyright (c) Christian Engelhardt 2024</Copyright>
Expand Down
82 changes: 41 additions & 41 deletions libNOM.io/Global/Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static string ToJson(IContainer input, string? output, bool indented, boo
if (!string.IsNullOrWhiteSpace(output))
{
var path1 = File.Exists(output) ? new FileInfo(output).Directory!.FullName : Directory.Exists(output) ? output : input.DataFile?.Directory?.FullName ?? Directory.GetCurrentDirectory(); // path where to write the new file

var name = File.Exists(output) ? Path.GetFileName(output) : input.DataFile?.Name ?? Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyTitleAttribute>()?.Title ?? "libNOM.io"; // actual filename without timestamp and new extension
var file = Path.Combine(path1, $"{name}.{DateTime.Now.ToString(Constants.FILE_TIMESTAMP_FORMAT)}.json"); // full path

Expand All @@ -111,35 +111,28 @@ public static string ToJson(IContainer input, string? output, bool indented, boo
/// <param name="platform"></param>
public static IContainer? ToSaveContainer(FileInfo? input, IPlatform platform) => GetContainer(input, platform);


#endregion

#region ToSaveFile







/// <summary>
/// Converts the input file to a save of the specified platform.
/// Converts the input to a save of the specified platform.
/// The result will be right next to the specified input file.
/// </summary>
/// <param name="input"></param>
/// <param name="targetPlatform"></param>
public static void ToSaveFile(FileInfo? input, PlatformEnum targetPlatform) => ToSaveFile(input, targetPlatform, null);

/// <summary>
/// Converts an input file to a save of the specified platform.
/// The result will be in the specified output path or next to the specified input file if the path is invalid.
/// Converts an input to a save of the specified platform.
/// The result will be in the specified output path or next to the specified input file if the path is invalid or in the current directory if that fails as well.
/// </summary>
/// <param name="input"></param>
/// <param name="targetPlatform"></param>
/// <param name="path"></param>
/// <param name="output"></param>
/// <exception cref="InvalidDataException"></exception>
// EXTERNAL RELEASE: If any, add the new platform here as well.
public static void ToSaveFile(FileInfo? input, PlatformEnum targetPlatform, string? path)
public static void ToSaveFile(FileInfo? input, PlatformEnum targetPlatform, string? output)
{
Platform platform = targetPlatform switch
{
Expand All @@ -154,66 +147,73 @@ public static void ToSaveFile(FileInfo? input, PlatformEnum targetPlatform, stri
// Method contains all relevant checks so just throw an exception if container is null.
var container = GetContainer(input, platform) ?? throw new InvalidDataException("Unable to read input file.");

if (string.IsNullOrWhiteSpace(path))
path = container.DataFile?.Directory?.FullName ?? Directory.GetCurrentDirectory();
if (string.IsNullOrWhiteSpace(output))
output = container.DataFile?.Directory?.FullName ?? Directory.GetCurrentDirectory();

var name = $"{container.DataFile?.Name ?? "libNOM.io"}.{platform}.{DateTime.Now.ToString(Constants.FILE_TIMESTAMP_FORMAT)}";

// Set new files the converted content will be written to.
container.DataFile = new FileInfo(Path.Combine(path, $"{name}.data"));
container.MetaFile = new FileInfo(Path.Combine(path, $"{name}.meta"));
container.DataFile = new FileInfo(Path.Combine(output, $"{name}.data"));
container.MetaFile = new FileInfo(Path.Combine(output, $"{name}.meta"));

container.Exists = true; // fake it be able to create the data
container.Extra = container.Extra with { MetaLength = 0 }; // reset to get the length of the target platform
container.IsSynced = true;
container.Platform = platform; // to get the right sizes

platform.PrepareWrite(container);
}

/// <summary>
/// Converts an input file to a save of the specified platform.
/// The result will be in the specified output path or next to the specified input file if the path is invalid.
/// Converts the input to a save of the specified platform.
/// The result will be in the current directory using the default naming pattern of the specified platform/index.
/// </summary>
/// <param name="input"></param>
/// <param name="targetPlatform"></param>
/// <param name="index"></param>
public static void ToSaveFile(string input, PlatformEnum targetPlatform, int index) => ToSaveFile(input, targetPlatform, index,null);

/// <summary>
/// Converts an input to a save of the specified platform.
/// The result will be in the specified output path and uses the default naming pattern of the specified platform/index.
/// </summary>
/// <param name="input"></param>
/// <param name="targetPlatform"></param>
/// <param name="index"></param>
/// <param name="output"></param>
/// <exception cref="InvalidDataException"></exception>
// EXTERNAL RELEASE: If any, add the new platform here as well.
public static void ToSaveFile(string input, PlatformEnum targetPlatform, string? output, int index)
public static void ToSaveFile(string input, PlatformEnum targetPlatform, int index, string? output)
{
// 1. Analyze output (to get platform)
// 2. Get container from index (if output is not file) -> add index param
// 3. Overwrite JSONObject in container
// 4. Write container
// 5. Else fallback below

Platform platform = targetPlatform switch
{
PlatformEnum.Gog => new PlatformGog(),
PlatformEnum.Microsoft => new PlatformMicrosoft(),
PlatformEnum.Playstation => new PlatformPlaystation(),
PlatformEnum.Steam => new PlatformSteam(),
PlatformEnum.Switch => new PlatformSwitch(),
PlatformEnum.Gog => new PlatformGog(output, new() { LoadingStrategy = LoadingStrategyEnum.Hollow }),
PlatformEnum.Microsoft => new PlatformMicrosoft(output, new() { LoadingStrategy = LoadingStrategyEnum.Hollow }),
PlatformEnum.Playstation => new PlatformPlaystation(output, new() { LoadingStrategy = LoadingStrategyEnum.Hollow }),
PlatformEnum.Steam => new PlatformSteam(output, new() { LoadingStrategy = LoadingStrategyEnum.Hollow }),
PlatformEnum.Switch => new PlatformSwitch(output, new() { LoadingStrategy = LoadingStrategyEnum.Hollow }),
_ => throw new InvalidDataException("The specified output platform is not yet supported."),
};

// Method contains all relevant checks so just throw an exception if container is null.
var container = GetContainer(input, platform) ?? throw new InvalidDataException("Unable to read input file.");
var container = platform.GetSaveContainer(index)?.ToContainer();
var name = $"{container?.DataFile?.Name ?? "libNOM.io"}.{platform}.{DateTime.Now.ToString(Constants.FILE_TIMESTAMP_FORMAT)}";

if (string.IsNullOrWhiteSpace(output))
output = container.DataFile?.Directory?.FullName ?? Directory.GetCurrentDirectory();
output = container?.DataFile?.Directory?.FullName ?? Directory.GetCurrentDirectory();

var name = $"{container.DataFile?.Name ?? "libNOM.io"}.{platform}.{DateTime.Now.ToString(Constants.FILE_TIMESTAMP_FORMAT)}";
if (container is null)
{
container = new Container(Constants.OFFSET_INDEX, platform) ?? throw new InvalidDataException("Unable to read input file.");

// Set new files the converted content will be written to.
container.DataFile = new FileInfo(Path.Combine(output, $"{name}.data"));
container.MetaFile = new FileInfo(Path.Combine(output, $"{name}.meta"));
// Set new files the converted content will be written to.
container.DataFile = new FileInfo(Path.Combine(output, $"{name}.data"));
container.MetaFile = new FileInfo(Path.Combine(output, $"{name}.meta"));
}

container.Exists = true; // fake it be able to create the data
container.Extra = container.Extra with { MetaLength = 0 }; // reset to get the length of the target platform
container.IsSynced = true;
container.Platform = platform; // to get the right sizes

container.ClearIncompatibility();
container.SetJsonObject(input.GetJson()); // set specified input as new JSON after faking and resetting other properties

platform.PrepareWrite(container);
}
Expand Down
3 changes: 2 additions & 1 deletion libNOM.io/Meta/GameVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ internal static GameVersionEnum Get(Platform platform, int length, uint format)
// EXTERNAL RELEASE: Add new game version.
internal static GameVersionEnum Get(int baseVersion) => baseVersion switch
{
// TODO: Add TheCursedWithStarbornPhoenix ? 5.29 / 4167 -> check 5.28 if possible
// >= 4168 => GameVersionEnum.WorldsPartII, // 5.50
// >= 4167 => GameVersionEnum.TheCursedWithStarbornPhoenix, // 5.29
>= 4164 => GameVersionEnum.TheCursedWithCrossSave, // 5.25
>= 4161 => GameVersionEnum.TheCursed, // 5.20
>= 4155 => GameVersionEnum.Aquarius, // 5.10
Expand Down

0 comments on commit e73c43f

Please sign in to comment.