Skip to content

Commit 1462199

Browse files
committed
Fix issue where some LoRA files cannot be read
1 parent 2b4c07d commit 1462199

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

Tiefsee/Lib/A1111Manager.cs

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class A1111Manager {
1212
private string _tempPath = null;
1313
// 判斷是否需要更新暫存檔
1414
private bool _isNeedUpdateTemp = false;
15+
// 當前版本
16+
private const int _version = 1;
1517

1618
public A1111Manager(string tempPath) {
1719
_tempPath = tempPath;
@@ -24,9 +26,12 @@ public A1111Manager(string tempPath) {
2426
try {
2527
if (File.Exists(_tempPath)) {
2628
var jsonText = File.ReadAllText(_tempPath);
27-
var json = JsonSerializer.Deserialize<Dictionary<string, SafetensorsData>>(jsonText);
28-
lock (_dicSafetensorsData) {
29-
_dicSafetensorsData = json;
29+
var json = JsonSerializer.Deserialize<A1111Model>(jsonText);
30+
// 暫存檔的版本必須與當前版本相同
31+
if (json.Version == _version) {
32+
lock (_dicSafetensorsData) {
33+
_dicSafetensorsData = json.Data;
34+
}
3035
}
3136
}
3237
}
@@ -80,7 +85,6 @@ public Dictionary<string, List<string>> GetA1111LoraResource(string[] searchDirs
8085
var data = (fileExtension == ".safetensors") ? GetSafetensorsData(filePath) : null;
8186

8287
foreach (var loraResourcs in loraResources.Keys) {
83-
8488
// 例如 lora 的名稱是 aa-3.2
8589
// 那麼 aa-3.2.png 跟 aa-3.2.private.png 都要加進去
8690
// 而 aa-3.21.png 則應該不加進去
@@ -100,11 +104,7 @@ public Dictionary<string, List<string>> GetA1111LoraResource(string[] searchDirs
100104

101105
if (_isNeedUpdateTemp) {
102106
_isNeedUpdateTemp = false;
103-
string jsongText = null;
104-
lock (_dicSafetensorsData) {
105-
jsongText = JsonSerializer.Serialize(_dicSafetensorsData, new JsonSerializerOptions());
106-
}
107-
File.WriteAllText(_tempPath, jsongText);
107+
UpdateTemp(); // 更新暫存
108108
}
109109

110110
return loraResources;
@@ -129,7 +129,7 @@ public SafetensorsData GetSafetensorsData(string path) {
129129
// 小於 3G 應該就是 LoRA,嘗試從裡面提取真實名稱
130130
if (fileinfo.Length < (long)1024 * 1024 * 1024 * 3) {
131131

132-
var text = GetFileHeader(path, 1000 * 50);
132+
var text = GetFileHeader(path, 1000 * 300);
133133

134134
/* var indexSshsModelHash = text.IndexOf("\"sshs_model_hash\"");
135135
if (indexSshsModelHash != -1)
@@ -190,6 +190,35 @@ public List<string> GetDirectoriesExcluding(string rootDir, string[] excludeDirs
190190
return result;
191191
}
192192

193+
/// <summary>
194+
/// 清理暫存
195+
/// </summary>
196+
public void ClearTemp() {
197+
var keys = _dicSafetensorsData.Keys.ToArray();
198+
foreach (var key in keys) {
199+
var item = _dicSafetensorsData[key];
200+
// 如果檔案不存在就移除
201+
if (File.Exists(key) == false) {
202+
_dicSafetensorsData.Remove(key);
203+
}
204+
}
205+
UpdateTemp(); // 更新暫存
206+
}
207+
208+
/// <summary>
209+
/// 更新暫存
210+
/// </summary>
211+
private void UpdateTemp() {
212+
string jsongText = null;
213+
lock (_dicSafetensorsData) {
214+
jsongText = JsonSerializer.Serialize(new A1111Model {
215+
Version = _version,
216+
Data = _dicSafetensorsData
217+
}, new JsonSerializerOptions());
218+
}
219+
File.WriteAllText(_tempPath, jsongText);
220+
}
221+
193222
/// <summary>
194223
/// 取得2進制檔案內容的前幾個字元
195224
/// </summary>
@@ -238,7 +267,17 @@ private string ComputeSha256Hash(string input) {
238267
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
239268
}
240269
}
270+
}
241271

272+
public class A1111Model {
273+
/// <summary>
274+
/// 版本
275+
/// </summary>
276+
public int Version { get; set; }
277+
/// <summary>
278+
/// 資料
279+
/// </summary>
280+
public Dictionary<string, SafetensorsData> Data { get; set; }
242281
}
243282

244283
public class SafetensorsData {

Tiefsee/VW/WV_System.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ public int[] GetScreenFromPoint(int x, int y) {
130130
public void DeleteAllTemp() {
131131
DeleteTemp(AppPath.tempDirImgProcessed, 0);
132132
DeleteTemp(AppPath.tempDirImgZoom, 0);
133+
134+
var a1111Manager = new A1111Manager(AppPath.appDataA1111ModelList);
135+
a1111Manager.ClearTemp();
133136
}
134137

135138
/// <summary>

0 commit comments

Comments
 (0)