Skip to content

Commit

Permalink
Merge pull request #16 from dhcgn/Possible-null-reference-argument
Browse files Browse the repository at this point in the history
Remove null ref warnings
  • Loading branch information
dhcgn authored Apr 3, 2024
2 parents 2feed12 + fbf1ff6 commit 76afdbf
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 48 deletions.
14 changes: 8 additions & 6 deletions jxlgui.buildinfo/BuildInfos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ private static string GetStringFromFile(string resourceName)
var name = assembly.GetManifestResourceNames().First(n => n.EndsWith(resourceName));
using (var resource = assembly.GetManifestResourceStream(name))
{
using (var reader = new StreamReader(resource))
{
var text = reader.ReadToEnd();
text = text.Trim();
return text;
}
if (resource == null)
return String.Empty;

using var reader = new StreamReader(resource);
var text = reader.ReadToEnd();
text = text.Trim();
return text;
}

}
}
3 changes: 2 additions & 1 deletion jxlgui.converter/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public static Config LoadOrCreateNew()
{
if (File.Exists(Constants.ConfigPath))
{
return Load();
var c = Load();
return c ?? CreateEmpty();
}

var config = CreateEmpty();
Expand Down
21 changes: 11 additions & 10 deletions jxlgui.converter/ExternalJxlResourceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static JxlFileResult GetFileInformation(string path)
Result = JxlFileResultEnum.FileNotFound
};

(string version, string commit) = GetExecutableVersion(path);
(string? version, string? commit) = GetExecutableVersion(path);

if (version == null)
return new JxlFileResult
Expand All @@ -49,7 +49,7 @@ private static JxlFileResult GetFileInformation(string path)
}


private static (string version, string commit) GetExecutableVersion(string path)
private static (string? version, string? commit) GetExecutableVersion(string path)
{
var proc = new Process
{
Expand All @@ -70,15 +70,15 @@ private static (string version, string commit) GetExecutableVersion(string path)
return (ParseVersion(line), ParseCommit(line));
}

private static string ParseVersion(string input)
private static string? ParseVersion(string input)
{
var r = Regex.Match(input, @"v(\d{1,}.\d{1,}.\d{1,})");
if (r.Groups.Count != 2) return null;

return r.Groups[1].Value;
}

private static string ParseCommit(string input)
private static string? ParseCommit(string input)
{
var r = Regex.Match(input, @"([a-f0-9]{6,})");
if (r.Groups.Count != 2) return null;
Expand All @@ -100,16 +100,17 @@ private static void WriteResourceToFile(string resourceName, string fileName)
var name = assembly.GetManifestResourceNames().First(n => n.EndsWith(resourceName));

using var resource = assembly.GetManifestResourceStream(name);
using (var file = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
resource.CopyTo(file);
}
if (resource == null)
return;

using var file = new FileStream(fileName, FileMode.Create, FileAccess.Write);
resource.CopyTo(file);
}

public class JxlFileResult
{
public JxlFileResultEnum Result { get; init; }
public string Version { get; init; }
public string Commit { get; init; }
public string? Version { get; init; }
public string? Commit { get; init; }
}
}
39 changes: 28 additions & 11 deletions jxlgui.converter/JobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,25 @@ private static async Task<ExecuteImageOperationResult> ExecuteImageOperationAsyn

private static string GetArguments(Job job)
{
var targetFilePath = "";
if (job == null)
throw new ArgumentNullException(nameof(job));

string? targetFilePath;
FileInfo fileInfo = new FileInfo(job.FilePath);
if (!fileInfo.Exists)
throw new FileNotFoundException(job.FilePath);

var directoryName = fileInfo.DirectoryName;
if (directoryName == null)
throw new Exception("Directory name is null");

switch (job.Operation)
{
case Job.OperationEnum.Encode:
targetFilePath = $"{Path.Combine(new FileInfo(job.FilePath).DirectoryName, job.FileName)}.jxl";
targetFilePath = $"{Path.Combine(directoryName, job.FileName)}.jxl";
break;
case Job.OperationEnum.Decode:
targetFilePath = $"{Path.Combine(new FileInfo(job.FilePath).DirectoryName, job.FileName)}.png";
targetFilePath = $"{Path.Combine(directoryName, job.FileName)}.png";
break;
default:
throw new Exception($"{job.Operation} should be Encode or Decode");
Expand All @@ -70,6 +81,9 @@ private static string GetArguments(Job job)
switch (job.Operation)
{
case Job.OperationEnum.Encode:
if (job.Config == null)
throw new ArgumentNullException(nameof(job.Config));

var args = $"-e {job.Config.Effort} ";

if (job.Config.Quality.HasValue)
Expand Down Expand Up @@ -147,7 +161,7 @@ private static string GetFileName(Job job)
private class ExecuteImageOperationResult
{
public Job.JobStateEnum State { get; internal set; }
public string Output { get; set; }
public required string Output { get; set; }
}
}

Expand All @@ -172,11 +186,11 @@ public enum OperationEnum
}

private JobStateEnum state;
private string targetFileFormattedLength;
private string? targetFileFormattedLength;


public string FilePath { get; init; }
public string FileName { get; init; }
public required string FilePath { get; init; }
public required string FileName { get; init; }
public long Length { get; init; }

public JobStateEnum State
Expand All @@ -197,11 +211,11 @@ internal set
}
}

public FileInfo FileInfo { get; init; }
public required FileInfo FileInfo { get; init; }

public OperationEnum Operation => GetOperation(this.FileInfo);

public string FormattedLength { get; init; }
public required string FormattedLength { get; init; }

private Config? config;

Expand All @@ -211,10 +225,10 @@ public Config? Config
set => base.SetProperty(ref config, value);
}

public string TargetFilePath { get; internal set; }
public string? TargetFilePath { get; internal set; }
public long TargetFileLength { get; internal set; }

public string TargetFileFormattedLength
public string? TargetFileFormattedLength
{
get => this.targetFileFormattedLength;
internal set => this.SetProperty(ref this.targetFileFormattedLength, value);
Expand Down Expand Up @@ -275,6 +289,7 @@ public static Job GetDesignDate(JobStateEnum state)
FileName = "pic1.png",
FilePath = "C:\\Users\\User\\Pictures\\pic1.png",
TargetFilePath = "C:\\Users\\User\\Pictures\\pic1.png.avif",
FileInfo = new FileInfo("C:\\Users\\User\\Pictures\\pic1.png"),
State = state,
FormattedLength = "131 KB",
Config = null
Expand All @@ -288,6 +303,7 @@ public static Job GetDesignDate(JobStateEnum state)
FileName = "pic1.png",
FilePath = "C:\\Users\\User\\Pictures\\pic1.png",
TargetFilePath = "C:\\Users\\User\\Pictures\\pic1.png.avif",
FileInfo = new FileInfo("C:\\Users\\User\\Pictures\\pic1.png"),
State = state,
FormattedLength = "131 KB",
Config = new Config
Expand All @@ -303,6 +319,7 @@ public static Job GetDesignDate(JobStateEnum state)
FileName = "pic1.png",
FilePath = "C:\\Users\\User\\Pictures\\pic1.png",
TargetFilePath = "C:\\Users\\User\\Pictures\\pic1.png.avif",
FileInfo = new FileInfo("C:\\Users\\User\\Pictures\\pic1.png"),
State = state,
FormattedLength = "132 KB",
TargetFileFormattedLength = "80 KB",
Expand Down
24 changes: 12 additions & 12 deletions jxlgui.wpf/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,35 +69,35 @@ public string JxlEncVersion
set => this.SetProperty(ref this.jxlEncVersion, value);
}

private string jxlEncCommit = "0000000";
private string? jxlEncCommit = "0000000";

public string JxlEncCommit
public string? JxlEncCommit
{
get => this.jxlEncCommit;
set => this.SetProperty(ref this.jxlEncCommit, value);
}

private string jxlDecVersion = "UNDEF";
private string? jxlDecVersion = "UNDEF";

public string JxlDecVersion
public string? JxlDecVersion
{
get => this.jxlDecVersion;
set => this.SetProperty(ref this.jxlDecVersion, value);
}

private string jxlDecCommit = "0000000";
private string? jxlDecCommit = "0000000";

public string JxlDecCommit
public string? JxlDecCommit
{
get => this.jxlDecCommit;
set => this.SetProperty(ref this.jxlDecCommit, value);
}

public RelayCommand ShowSettingsCommand { get; set; }
public RelayCommand OpenHelpCommand { get; set; }
public required RelayCommand ShowSettingsCommand { get; set; }
public required RelayCommand OpenHelpCommand { get; set; }
public List<string> Configs { get; }
public string SelectedConfig { get; set; }
public IAsyncRelayCommand OnLoadCommand { get; }
public required IAsyncRelayCommand OnLoadCommand { get; set;}

public bool CanEncode
{
Expand Down Expand Up @@ -126,7 +126,7 @@ public static bool InDesignMode()

private async Task OnLoadCommandHandlingAsync()
{
void SetVersionCommit(Action<string> SetVersion, Action<string> SetCommit,
void SetVersionCommit(Action<string?> SetVersion, Action<string?> SetCommit,
ExternalJxlResourceHandler.JxlFileResult jxlFileResult)
{
if (jxlFileResult.Result == ExternalJxlResourceHandler.JxlFileResultEnum.FileNotFound)
Expand All @@ -153,9 +153,9 @@ void SetVersionCommit(Action<string> SetVersion, Action<string> SetCommit,

await Task.Factory.StartNew(() =>
{
SetVersionCommit(v => this.JxlEncVersion = v, c => this.JxlEncCommit = c,
SetVersionCommit(v => this.JxlEncVersion = v ?? String.Empty, c => this.JxlEncCommit = c ?? String.Empty,
ExternalJxlResourceHandler.GetEncoderInformation());
SetVersionCommit(v => this.JxlDecVersion = v, c => this.JxlDecCommit = c,
SetVersionCommit(v => this.JxlDecVersion = v ?? String.Empty, c => this.JxlDecCommit = c ?? String.Empty,
ExternalJxlResourceHandler.GetDecoderInformation());
});
}
Expand Down
34 changes: 26 additions & 8 deletions jxlgui.wpf/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ public class SettingsViewModel : ObservableRecipient
public SettingsViewModel()
{
this.SaveCommand =
new RelayCommand(SaveCommandHandling, () => jxlgui.converter.Config.IsJsonValid(this.Config));
new RelayCommand(SaveCommandHandling, () =>
{
if (this.Config == null)
return false;
return jxlgui.converter.Config.IsJsonValid(this.Config);
});
this.CancelCommand = new RelayCommand(() =>
{
this.Messenger.Send(new WindowMessage(WindowEnum.SettingsWindowsClose));
Expand All @@ -36,6 +41,12 @@ public static bool InDesignMode()

private void SaveCommandHandling()
{
if (this.Config == null)
{
System.Windows.MessageBox.Show("Config is null");
return;
}

if (!jxlgui.converter.Config.IsJsonValid(this.Config))
{
System.Windows.MessageBox.Show("Config is not valid");
Expand All @@ -44,22 +55,29 @@ private void SaveCommandHandling()

File.WriteAllText(Constants.ConfigPath, this.Config);

this.Config = jxlgui.converter.Config.Load()?.ToJson();
}
var c = jxlgui.converter.Config.Load();
if (c == null)
return;

public RelayCommand OnLoadCommand { get; set; }
public RelayCommand SaveCommand { get; set; }
public RelayCommand CancelCommand { get; set; }
this.Config = c.ToJson();
}

private string config;
public required RelayCommand OnLoadCommand { get; set; }
public required RelayCommand SaveCommand { get; set; }
public required RelayCommand CancelCommand { get; set; }

private string? config;

public string Config
public string? Config
{
get { return config; }
set
{
this.SetProperty(ref this.config, value);
if (this.Config == null){
ConfigError = "Config is null";
return;
}
ConfigError = converter.Config.IsJsonValid(this.Config) ? null : "Json is not valid";
}
}
Expand Down

0 comments on commit 76afdbf

Please sign in to comment.