Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-format of namespace, update lang version & SDK and improve US852 IFF models #41

Merged
merged 3 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 112 additions & 113 deletions PangLib.DAT/DATFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,144 +3,143 @@
using System.IO;
using System.Text;

namespace PangLib.DAT
namespace PangLib.DAT;

/// <summary>
/// Main DAT file class
/// </summary>
public class DATFile
{
public List<string> Entries = new List<string>();

private Encoding FileEncoding;

/// <summary>
/// Main DAT file class
/// Parses the data from the DAT file
/// </summary>
public class DATFile
/// <param name="data">Stream containing DAT file data</param>
private void Parse(Stream data)
{
public List<string> Entries = new List<string>();

private Encoding FileEncoding;

/// <summary>
/// Parses the data from the DAT file
/// </summary>
/// <param name="data">Stream containing DAT file data</param>
private void Parse(Stream data)
using (BinaryReader reader = new BinaryReader(data, FileEncoding))
{
using (BinaryReader reader = new BinaryReader(data, FileEncoding))
{
List<char> stringChars = new List<char>();
List<char> stringChars = new List<char>();

while (reader.BaseStream.Position < reader.BaseStream.Length)
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
if (reader.PeekChar() != 0x00)
{
stringChars.Add(reader.ReadChar());
}
else
{
if (reader.PeekChar() != 0x00)
{
stringChars.Add(reader.ReadChar());
}
else
{
char[] chars = stringChars.ToArray();
byte[] bytes = FileEncoding.GetBytes(chars);

Entries.Add(FileEncoding.GetString(bytes));

reader.BaseStream.Seek(1L, SeekOrigin.Current);
stringChars = new List<char>();
}
char[] chars = stringChars.ToArray();
byte[] bytes = FileEncoding.GetBytes(chars);

Entries.Add(FileEncoding.GetString(bytes));

reader.BaseStream.Seek(1L, SeekOrigin.Current);
stringChars = new List<char>();
}
}
}
}

/// <summary>
/// Tries to get the file encoding based on
/// the naming scheme of the files
///
/// Falls back on UTF-8 as default
/// </summary>
/// <param name="filePath">File name of the DAT file</param>
/// <returns>Encoding of the DAT file</returns>
private Encoding GetEncodingFromFileName(string filePath)
/// <summary>
/// Tries to get the file encoding based on
/// the naming scheme of the files
///
/// Falls back on UTF-8 as default
/// </summary>
/// <param name="filePath">File name of the DAT file</param>
/// <returns>Encoding of the DAT file</returns>
private Encoding GetEncodingFromFileName(string filePath)
{
if (filePath == null)
{
if (filePath == null)
{
throw new InvalidOperationException("No file path given to get encoding from, use SetEncoding() method!");
}

string fileName = Path.GetFileNameWithoutExtension(filePath).ToLower();

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
throw new InvalidOperationException("No file path given to get encoding from, use SetEncoding() method!");
}

Encoding targetEncoding;
string fileName = Path.GetFileNameWithoutExtension(filePath).ToLower();

switch (fileName)
{
case "korea":
targetEncoding = Encoding.GetEncoding(51949);
break;
case "japan":
targetEncoding = Encoding.GetEncoding(932);
break;
case "english":
targetEncoding = Encoding.ASCII;
break;
case "thailand":
targetEncoding = Encoding.GetEncoding(874);
break;
case "indonesia":
targetEncoding = Encoding.GetEncoding(65001);
break;
case "brasil":
case "spanish":
case "german":
case "french":
targetEncoding = Encoding.GetEncoding(1252);
break;
default:
targetEncoding = Encoding.GetEncoding(65001);
break;
}
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

return targetEncoding;
}
Encoding targetEncoding;

/// <summary>
/// Sets the encoding to be used by the DATFile instance
/// </summary>
/// <param name="encoding">Encoding to set</param>
public void SetEncoding(Encoding encoding)
switch (fileName)
{
FileEncoding = encoding;
case "korea":
targetEncoding = Encoding.GetEncoding(51949);
break;
case "japan":
targetEncoding = Encoding.GetEncoding(932);
break;
case "english":
targetEncoding = Encoding.ASCII;
break;
case "thailand":
targetEncoding = Encoding.GetEncoding(874);
break;
case "indonesia":
targetEncoding = Encoding.GetEncoding(65001);
break;
case "brasil":
case "spanish":
case "german":
case "french":
targetEncoding = Encoding.GetEncoding(1252);
break;
default:
targetEncoding = Encoding.GetEncoding(65001);
break;
}

return targetEncoding;
}

/// <summary>
/// Sets the encoding to be used by the DATFile instance
/// </summary>
/// <param name="encoding">Encoding to set</param>
public void SetEncoding(Encoding encoding)
{
FileEncoding = encoding;
}

/// <summary>
/// Returns the encoding used by the DATFile instance
/// </summary>
public Encoding GetEncoding()
{
return FileEncoding;
}
/// <summary>
/// Returns the encoding used by the DATFile instance
/// </summary>
public Encoding GetEncoding()
{
return FileEncoding;
}

/// <summary>
/// Load a DAT file into a DATFile instance
/// </summary>
/// <param name="filePath">File path to load the DAT file from</param>
public static DATFile Load(string filePath)
{
DATFile DAT = new DATFile();
/// <summary>
/// Load a DAT file into a DATFile instance
/// </summary>
/// <param name="filePath">File path to load the DAT file from</param>
public static DATFile Load(string filePath)
{
DATFile DAT = new DATFile();

DAT.SetEncoding(DAT.GetEncodingFromFileName(filePath));
DAT.Parse(File.Open(filePath, FileMode.Open));
DAT.SetEncoding(DAT.GetEncodingFromFileName(filePath));
DAT.Parse(File.Open(filePath, FileMode.Open));

return DAT;
}
return DAT;
}

/// <summary>
/// Save a DATFile instance to a file
/// </summary>
/// <param name="filePath">File path to save the DAT file to</param>
public void Save(string filePath)
/// <summary>
/// Save a DATFile instance to a file
/// </summary>
/// <param name="filePath">File path to save the DAT file to</param>
public void Save(string filePath)
{
using (BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create, FileAccess.Write), FileEncoding))
{
using (BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create, FileAccess.Write), FileEncoding))
foreach (string entry in Entries)
{
foreach (string entry in Entries)
{
writer.Write(entry.ToCharArray());
writer.Write((byte)0);
}
writer.Write(entry.ToCharArray());
writer.Write((byte)0);
}
}
}
}
}
2 changes: 2 additions & 0 deletions PangLib.DAT/PangLib.DAT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<PackageIconUrl>https://raw.githubusercontent.com/pangyatools/PangLib/master/.github/Images/pang.png</PackageIconUrl>

<TargetFramework>netstandard2.0</TargetFramework>

<LangVersion>12</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
120 changes: 0 additions & 120 deletions PangLib.IFF/IFFFile.cs

This file was deleted.

Loading
Loading