|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.IO; |
4 | 4 | using System.Text.Json; |
5 | | -using Microsoft.Data.Sqlite; |
6 | 5 |
|
7 | 6 | namespace FFTColorCustomizer.Services |
8 | 7 | { |
9 | 8 | /// <summary> |
10 | 9 | /// Patches charclut.nxd files by updating CLUTData bytes in place. |
11 | | - /// This avoids the need to fully reimplement the NXD format - we just |
12 | | - /// read the SQLite, convert JSON to bytes, and patch the NXD template. |
| 10 | + /// Uses known byte offsets to directly modify the NXD binary. |
13 | 11 | /// </summary> |
14 | 12 | public class NxdPatcher |
15 | 13 | { |
@@ -43,79 +41,6 @@ public class NxdPatcher |
43 | 41 |
|
44 | 42 | private const int ClutDataSize = 48; // 16 colors × 3 RGB bytes |
45 | 43 |
|
46 | | - /// <summary> |
47 | | - /// Creates a patched NXD file from the SQLite database. |
48 | | - /// </summary> |
49 | | - /// <param name="templateNxdPath">Path to the template charclut.nxd</param> |
50 | | - /// <param name="sqlitePath">Path to the modified charclut.sqlite</param> |
51 | | - /// <param name="outputNxdPath">Path for the output NXD file</param> |
52 | | - /// <returns>True if patching was successful</returns> |
53 | | - public bool PatchNxdFromSqlite(string templateNxdPath, string sqlitePath, string outputNxdPath) |
54 | | - { |
55 | | - if (!File.Exists(templateNxdPath)) |
56 | | - throw new FileNotFoundException("Template NXD file not found", templateNxdPath); |
57 | | - |
58 | | - if (!File.Exists(sqlitePath)) |
59 | | - throw new FileNotFoundException("SQLite database not found", sqlitePath); |
60 | | - |
61 | | - // Read the template NXD |
62 | | - byte[] nxdBytes = File.ReadAllBytes(templateNxdPath); |
63 | | - |
64 | | - // Verify it's a valid NXD file |
65 | | - if (nxdBytes.Length < 4 || |
66 | | - nxdBytes[0] != 'N' || nxdBytes[1] != 'X' || |
67 | | - nxdBytes[2] != 'D' || nxdBytes[3] != 'F') |
68 | | - { |
69 | | - throw new InvalidDataException("Invalid NXD file - missing NXDF magic"); |
70 | | - } |
71 | | - |
72 | | - try |
73 | | - { |
74 | | - // Read CLUTData from SQLite and patch into NXD |
75 | | - using var connection = new SqliteConnection($"Data Source={sqlitePath}"); |
76 | | - connection.Open(); |
77 | | - |
78 | | - using var command = connection.CreateCommand(); |
79 | | - command.CommandText = "SELECT Key, Key2, CLUTData FROM CharCLUT"; |
80 | | - |
81 | | - using var reader = command.ExecuteReader(); |
82 | | - while (reader.Read()) |
83 | | - { |
84 | | - int key = reader.GetInt32(0); |
85 | | - int key2 = reader.GetInt32(1); |
86 | | - string clutDataJson = reader.GetString(2); |
87 | | - |
88 | | - if (ClutDataOffsets.TryGetValue((key, key2), out int offset)) |
89 | | - { |
90 | | - byte[] clutBytes = JsonToClutBytes(clutDataJson); |
91 | | - if (clutBytes.Length == ClutDataSize) |
92 | | - { |
93 | | - Array.Copy(clutBytes, 0, nxdBytes, offset, ClutDataSize); |
94 | | - } |
95 | | - } |
96 | | - } |
97 | | - |
98 | | - connection.Close(); |
99 | | - } |
100 | | - finally |
101 | | - { |
102 | | - // Clear connection pool to release file handles |
103 | | - SqliteConnection.ClearAllPools(); |
104 | | - } |
105 | | - |
106 | | - // Ensure output directory exists |
107 | | - var outputDir = Path.GetDirectoryName(outputNxdPath); |
108 | | - if (!string.IsNullOrEmpty(outputDir) && !Directory.Exists(outputDir)) |
109 | | - { |
110 | | - Directory.CreateDirectory(outputDir); |
111 | | - } |
112 | | - |
113 | | - // Write the patched NXD |
114 | | - File.WriteAllBytes(outputNxdPath, nxdBytes); |
115 | | - |
116 | | - return true; |
117 | | - } |
118 | | - |
119 | 44 | /// <summary> |
120 | 45 | /// Patches a single CLUTData entry in an NXD file. |
121 | 46 | /// </summary> |
|
0 commit comments