Fix bugs.

This commit is contained in:
Bruce
2026-04-10 13:58:15 +08:00
parent 0f7c4be17e
commit 200262d3ca
14 changed files with 334 additions and 44 deletions

BIN
PriFileFormat.backup.7z Normal file

Binary file not shown.

View File

@@ -17,15 +17,15 @@ namespace PriFileFormat
} }
public override bool CanRead public override bool CanRead
{ {
get { return comStream != null; } get { return true; }
} }
public override bool CanSeek public override bool CanSeek
{ {
get { return comStream != null; } get { return true; }
} }
public override bool CanWrite public override bool CanWrite
{ {
get { return comStream != null; } get { return true; }
} }
public override long Length public override long Length
{ {
@@ -144,8 +144,9 @@ namespace PriFileFormat
} }
public override void Close () public override void Close ()
{ {
base.Close ();
comStream = null; comStream = null;
} base.Close ();
}
~ComStreamWrapper () { comStream = null;}
} }
} }

View File

@@ -48,6 +48,7 @@ namespace PriFileFormat
_dataItems?.Clear (); _dataItems?.Clear ();
_dataItems = null; _dataItems = null;
} }
~DataItemSection () { ClearData (); }
} }
public struct DataItemRef public struct DataItemRef

213
PriFileFormat/DataTree.cs Normal file
View File

@@ -0,0 +1,213 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
namespace PriFileFormat
{
/// <summary>
/// 树节点接口
/// </summary>
public interface ITreeNode: IDisposable, IEnumerable<ITreeNode>
{
string Name { get; set; }
string Value { get; set; }
ITreeNode Parent { get; set; }
IList<ITreeNode> Children { get; }
ITreeNode AddChild (string name, string value);
IEnumerable<ITreeNode> DescendantsAndSelf ();
ITreeNode Find (string name);
IEnumerable<ITreeNode> FindAll (string name);
}
/// <summary>
/// 树节点实现
/// </summary>
[Serializable]
public class TreeNode: ITreeNode
{
// 节点基本属性
public string Name { get; set; }
public string Value { get; set; }
// 父节点引用
[XmlIgnore]
public TreeNode Parent { get; set; }
ITreeNode ITreeNode.Parent
{
get { return Parent; }
set { Parent = (TreeNode)value; }
}
// 子节点列表
[XmlArray ("Children")]
[XmlArrayItem ("Node")]
public IList<TreeNode> Children { get; set; }
IList<ITreeNode> ITreeNode.Children
{
get
{
List<ITreeNode> list = new List<ITreeNode> ();
foreach (TreeNode node in Children)
{
list.Add (node);
}
return list;
}
}
public TreeNode ()
{
Name = "";
Value = "";
Children = new List<TreeNode> ();
Parent = null;
}
public TreeNode (string name, string value)
{
Name = name;
Value = value;
Children = new List<TreeNode> ();
Parent = null;
}
/// <summary>
/// 添加子节点
/// </summary>
public TreeNode AddChild (string name, string value)
{
TreeNode child = new TreeNode (name, value);
child.Parent = this;
Children.Add (child);
return child;
}
ITreeNode ITreeNode.AddChild (string name, string value)
{
return AddChild (name, value);
}
/// <summary>
/// 深度优先遍历节点,包括自身
/// </summary>
public IEnumerable<TreeNode> DescendantsAndSelf ()
{
yield return this;
foreach (TreeNode child in Children)
{
foreach (TreeNode desc in child.DescendantsAndSelf ())
{
yield return desc;
}
}
}
IEnumerable<ITreeNode> ITreeNode.DescendantsAndSelf ()
{
foreach (TreeNode n in DescendantsAndSelf ())
{
yield return n;
}
}
/// <summary>
/// 查找第一个匹配节点
/// </summary>
public TreeNode Find (string name)
{
foreach (TreeNode n in DescendantsAndSelf ())
{
if (n.Name == name)
return n;
}
return null;
}
ITreeNode ITreeNode.Find (string name)
{
return Find (name);
}
/// <summary>
/// 查找所有匹配节点
/// </summary>
public IEnumerable<TreeNode> FindAll (string name)
{
foreach (TreeNode n in DescendantsAndSelf ())
{
if (n.Name == name)
yield return n;
}
}
IEnumerable<ITreeNode> ITreeNode.FindAll (string name)
{
foreach (TreeNode n in FindAll (name))
{
yield return n;
}
}
#region IEnumerable<TreeNode>
public IEnumerator<TreeNode> GetEnumerator ()
{
return Children.GetEnumerator ();
}
IEnumerator IEnumerable.GetEnumerator ()
{
return GetEnumerator ();
}
#endregion
#region IEnumerable<ITreeNode>
IEnumerator<ITreeNode> IEnumerable<ITreeNode>.GetEnumerator ()
{
foreach (TreeNode child in Children)
{
yield return child;
}
}
#endregion
#region XML
public void SaveToXml (string filePath)
{
XmlSerializer serializer = new XmlSerializer (typeof (TreeNode));
using (StreamWriter writer = new StreamWriter (filePath, false, System.Text.Encoding.UTF8))
{
serializer.Serialize (writer, this);
}
}
public static TreeNode LoadFromXml (string filePath)
{
XmlSerializer serializer = new XmlSerializer (typeof (TreeNode));
using (StreamReader reader = new StreamReader (filePath, System.Text.Encoding.UTF8))
{
TreeNode root = (TreeNode)serializer.Deserialize (reader);
SetParentRecursive (root, null);
return root;
}
}
private static void SetParentRecursive (TreeNode node, TreeNode parent)
{
node.Parent = parent;
foreach (TreeNode child in node.Children)
{
SetParentRecursive (child, node);
}
}
#endregion
#region IDisposable
public virtual void Dispose ()
{
foreach (TreeNode child in Children)
{
child.Dispose ();
}
Children.Clear ();
Parent = null;
Children = null;
}
#endregion
}
/// <summary>
/// 树根节点
/// </summary>
[Serializable]
public class TreeRoot: TreeNode
{
public TreeRoot ()
: base ("Root", "")
{
}
}
}

View File

@@ -172,6 +172,18 @@ namespace PriFileFormat
FirstChildIndex = firstChildIndex; FirstChildIndex = firstChildIndex;
} }
} }
~HierarchicalSchemaSection ()
{
try
{
Version = null;
foreach (var item in Items) { item.Parent = null; }
foreach (var scope in Scopes) { scope.Parent = null; }
Scopes = null;
Items = null;
}
catch { }
}
// Checksum computation is buggy for some files // Checksum computation is buggy for some files
//private uint ComputeHierarchicalSchemaVersionInfoChecksum() //private uint ComputeHierarchicalSchemaVersionInfoChecksum()
@@ -258,6 +270,7 @@ namespace PriFileFormat
return fullName; return fullName;
} }
} }
~ResourceMapEntry () { Parent = null; }
} }
public class ResourceMapScope: ResourceMapEntry public class ResourceMapScope: ResourceMapEntry
{ {
@@ -267,6 +280,7 @@ namespace PriFileFormat
{ {
return $"Scope {Index} {FullName} ({Children.Count} children)"; return $"Scope {Index} {FullName} ({Children.Count} children)";
} }
~ResourceMapScope () { Children = null; }
} }
public class ResourceMapItem: ResourceMapEntry public class ResourceMapItem: ResourceMapEntry
{ {

View File

@@ -46,6 +46,15 @@ namespace PriFileFormat
DataItemSections = dataItemSections; DataItemSections = dataItemSections;
return true; return true;
} }
~PriDescriptorSection ()
{
HierarchicalSchemaSections = null;
DecisionInfoSections = null;
ResourceMapSections = null;
ReferencedFileSections = null;
DataItemSections = null;
PrimaryResourceMapSection = null;
}
} }
[Flags] [Flags]
public enum PriDescriptorFlags: ushort public enum PriDescriptorFlags: ushort

View File

@@ -6,12 +6,14 @@ using System;
namespace PriFileFormat namespace PriFileFormat
{ {
public class PriFile public class PriFile: IDisposable
{ {
public string Version { get; private set; } public string Version { get; private set; }
public uint TotalFileSize { get; private set; } public uint TotalFileSize { get; private set; }
public IReadOnlyList <TocEntry> TableOfContents { get; private set; } public IReadOnlyList <TocEntry> TableOfContents { get; private set; }
public IReadOnlyList <Section> Sections { get; private set; } public IReadOnlyList <Section> Sections { get; private set; }
private bool _disposed = false;
private Stream _internalStream; // 跟踪内部流
private PriFile () private PriFile ()
{ {
} }
@@ -35,6 +37,7 @@ namespace PriFileFormat
private void ParseInternal (Stream stream, bool ownStream) private void ParseInternal (Stream stream, bool ownStream)
{ {
if (ownStream) { _internalStream = stream; }
using (BinaryReader binaryReader = new BinaryReader (stream, Encoding.ASCII, true)) using (BinaryReader binaryReader = new BinaryReader (stream, Encoding.ASCII, true))
{ {
long fileStartOffset = binaryReader.BaseStream.Position; long fileStartOffset = binaryReader.BaseStream.Position;
@@ -109,6 +112,48 @@ namespace PriFileFormat
} }
} }
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (_disposed) return;
if (disposing)
{
// 释放托管资源
if (_internalStream != null)
{
_internalStream.Dispose ();
_internalStream = null;
}
// 释放 section 内容
if (Sections != null)
{
foreach (var section in Sections)
{
var unknown = section as UnknownSection;
if (unknown != null)
unknown.ClearContent ();
var dataSection = section as DataItemSection;
if (dataSection != null)
dataSection.ClearData ();
}
Sections = null;
}
}
_disposed = true;
}
~PriFile ()
{
Dispose (false);
}
PriDescriptorSection priDescriptorSection; PriDescriptorSection priDescriptorSection;
public PriDescriptorSection PriDescriptorSection public PriDescriptorSection PriDescriptorSection

View File

@@ -121,6 +121,11 @@ namespace PriFileFormat
FileNameOffset = fileNameOffset; FileNameOffset = fileNameOffset;
} }
} }
~ReferencedFileSection ()
{
foreach (var file in ReferencedFiles) { file.Parent = null; }
ReferencedFiles = null;
}
} }
public class ReferencedEntry public class ReferencedEntry
{ {
@@ -145,11 +150,13 @@ namespace PriFileFormat
return fullName; return fullName;
} }
} }
~ReferencedEntry () { Parent = null; }
} }
public class ReferencedFolder: ReferencedEntry public class ReferencedFolder: ReferencedEntry
{ {
internal ReferencedFolder (ReferencedFolder parent, string name) : base (parent, name) {} internal ReferencedFolder (ReferencedFolder parent, string name) : base (parent, name) {}
public IReadOnlyList<ReferencedEntry> Children { get; internal set; } public IReadOnlyList<ReferencedEntry> Children { get; internal set; }
~ReferencedFolder () { Children = null; }
} }
public class ReferencedFile: ReferencedEntry public class ReferencedFile: ReferencedEntry
{ {

View File

@@ -296,6 +296,11 @@ namespace PriFileFormat
DataOffset = dataOffset; DataOffset = dataOffset;
} }
} }
~ResourceMapSection ()
{
HierarchicalSchemaReference = null;
CandidateSets = null;
}
} }
public enum ResourceValueType public enum ResourceValueType
{ {
@@ -318,6 +323,10 @@ namespace PriFileFormat
DecisionIndex = decisionIndex; DecisionIndex = decisionIndex;
Candidates = candidates; Candidates = candidates;
} }
~CandidateSet ()
{
Candidates = null;
}
} }
public class Candidate public class Candidate
@@ -343,6 +352,12 @@ namespace PriFileFormat
DataItem = null; DataItem = null;
Data = data; Data = data;
} }
~Candidate ()
{
SourceFile = null;
DataItem = null;
Data = null;
}
} }
public class HierarchicalSchemaReference public class HierarchicalSchemaReference
{ {
@@ -357,6 +372,10 @@ namespace PriFileFormat
Unknown2 = unknown2; Unknown2 = unknown2;
UniqueName = uniqueName; UniqueName = uniqueName;
} }
~HierarchicalSchemaReference ()
{
VersionInfo = null;
}
} }
public struct ResourceMapItemRef public struct ResourceMapItemRef
{ {

View File

@@ -148,6 +148,12 @@ namespace PriFileFormat
return true; return true;
} }
~ReverseMapSection ()
{
Mapping = null;
Scopes = null;
Items = null;
}
} }
} }

View File

@@ -81,5 +81,9 @@ namespace PriFileFormat
} }
} }
~Section ()
{
PriFile = null;
}
} }
} }

View File

@@ -71,10 +71,6 @@ namespace PriFileFormat
throw new NotSupportedException (); throw new NotSupportedException ();
} }
public override void Close () ~SubStream () { baseStream = null; }
{
base.Close ();
baseStream = null;
}
} }
} }

View File

@@ -18,5 +18,6 @@ namespace PriFileFormat
{ {
SectionContent = null; SectionContent = null;
} }
~UnknownSection () { ClearContent (); }
} }
} }

View File

@@ -20,32 +20,6 @@
#include <algorithm> #include <algorithm>
#include <set> #include <set>
LPWSTR AllocWideString (const std::wstring &str)
{
size_t size = (str.length () + 1) * sizeof (WCHAR);
LPWSTR buf = (LPWSTR)CoTaskMemAlloc (size);
if (!buf) return nullptr;
ZeroMemory (buf, size);
wcscpy (buf, str.c_str ());
return buf;
}
// 测试用
LPWSTR AllocWideString (LPCWSTR str)
{
if (!str) return nullptr;
size_t size = (wcslen (str) + 1) * sizeof (WCHAR);
LPWSTR buf = (LPWSTR)CoTaskMemAlloc (size);
if (!buf) return nullptr;
ZeroMemory (buf, size);
wcscpy (buf, str);
return buf;
}
#define _wcsdup AllocWideString
#define free CoTaskMemFree
#define malloc CoTaskMemAlloc
#define realloc CoTaskMemRealloc
#define calloc(_cnt_, _size_) CoTaskMemAlloc (_cnt_ * _size_)
const std::wstring g_swMsResUriProtocolName = L"ms-resource:"; const std::wstring g_swMsResUriProtocolName = L"ms-resource:";
const size_t g_cbMsResPNameLength = lstrlenW (g_swMsResUriProtocolName.c_str ()); const size_t g_cbMsResPNameLength = lstrlenW (g_swMsResUriProtocolName.c_str ());
std::wstring g_swExcept = L""; std::wstring g_swExcept = L"";