mirror of
https://github.com/kwsch/NHSE.git
synced 2026-09-10 04:05:38 -05:00
Extract some logic
This commit is contained in:
@@ -77,19 +77,7 @@ public IEnumerable<FileHashRegion> InvalidHashes()
|
||||
}
|
||||
}
|
||||
|
||||
public string GetString(int offset, int maxLength)
|
||||
{
|
||||
var str = Encoding.Unicode.GetString(Data, offset, maxLength * 2);
|
||||
return StringUtil.TrimFromZero(str);
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(string value, int maxLength)
|
||||
{
|
||||
if (value.Length > maxLength)
|
||||
value = value.Substring(0, maxLength);
|
||||
else if (value.Length < maxLength)
|
||||
value = value.PadRight(maxLength, '\0');
|
||||
return Encoding.Unicode.GetBytes(value);
|
||||
}
|
||||
public string GetString(int offset, int maxLength) => StringUtil.GetString(Data, offset, maxLength);
|
||||
public static byte[] GetBytes(string value, int maxLength) => StringUtil.GetBytes(value, maxLength);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
namespace NHSE.Core
|
||||
{
|
||||
@@ -20,5 +22,25 @@ private static string TrimFromFirst(string input, char c)
|
||||
int index = input.IndexOf(c);
|
||||
return index < 0 ? input : input.Substring(0, index);
|
||||
}
|
||||
|
||||
public static string GetString(byte[] data, int offset, int maxLength)
|
||||
{
|
||||
var str = Encoding.Unicode.GetString(data, offset, maxLength * 2);
|
||||
return TrimFromZero(str);
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(string value, int maxLength)
|
||||
{
|
||||
if (value.Length > maxLength)
|
||||
value = value.Substring(0, maxLength);
|
||||
else if (value.Length < maxLength)
|
||||
value = value.PadRight(maxLength, '\0');
|
||||
return Encoding.Unicode.GetBytes(value);
|
||||
}
|
||||
|
||||
public static string CleanFileName(string fileName)
|
||||
{
|
||||
return string.Concat(fileName.Split(Path.GetInvalidFileNameChars()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,21 +63,30 @@ private void Menu_Open(object sender, EventArgs e)
|
||||
|
||||
private static void Open(string path)
|
||||
{
|
||||
#if !DEBUG
|
||||
try
|
||||
#endif
|
||||
{
|
||||
var file = new HorizonSave(path);
|
||||
Open(file);
|
||||
|
||||
var settings = Settings.Default;
|
||||
settings.LastFilePath = path;
|
||||
settings.Save();
|
||||
OpenSaveFile(path);
|
||||
}
|
||||
#if !DEBUG
|
||||
#pragma warning disable CA1031 // Do not catch general exception types
|
||||
catch (Exception ex)
|
||||
#pragma warning restore CA1031 // Do not catch general exception types
|
||||
{
|
||||
WinFormsUtil.Error(ex.Message);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
private static void OpenSaveFile(string path)
|
||||
{
|
||||
var file = new HorizonSave(path);
|
||||
Open(file);
|
||||
|
||||
var settings = Settings.Default;
|
||||
settings.LastFilePath = path;
|
||||
settings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user