@@ -12,6 +12,8 @@ public class A1111Manager {
12
12
private string _tempPath = null ;
13
13
// 判斷是否需要更新暫存檔
14
14
private bool _isNeedUpdateTemp = false ;
15
+ // 當前版本
16
+ private const int _version = 1 ;
15
17
16
18
public A1111Manager ( string tempPath ) {
17
19
_tempPath = tempPath ;
@@ -24,9 +26,12 @@ public A1111Manager(string tempPath) {
24
26
try {
25
27
if ( File . Exists ( _tempPath ) ) {
26
28
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
+ }
30
35
}
31
36
}
32
37
}
@@ -80,7 +85,6 @@ public Dictionary<string, List<string>> GetA1111LoraResource(string[] searchDirs
80
85
var data = ( fileExtension == ".safetensors" ) ? GetSafetensorsData ( filePath ) : null ;
81
86
82
87
foreach ( var loraResourcs in loraResources . Keys ) {
83
-
84
88
// 例如 lora 的名稱是 aa-3.2
85
89
// 那麼 aa-3.2.png 跟 aa-3.2.private.png 都要加進去
86
90
// 而 aa-3.21.png 則應該不加進去
@@ -100,11 +104,7 @@ public Dictionary<string, List<string>> GetA1111LoraResource(string[] searchDirs
100
104
101
105
if ( _isNeedUpdateTemp ) {
102
106
_isNeedUpdateTemp = false ;
103
- string jsongText = null ;
104
- lock ( _dicSafetensorsData ) {
105
- jsongText = JsonSerializer . Serialize ( _dicSafetensorsData , new JsonSerializerOptions ( ) ) ;
106
- }
107
- File . WriteAllText ( _tempPath , jsongText ) ;
107
+ UpdateTemp ( ) ; // 更新暫存
108
108
}
109
109
110
110
return loraResources ;
@@ -129,7 +129,7 @@ public SafetensorsData GetSafetensorsData(string path) {
129
129
// 小於 3G 應該就是 LoRA,嘗試從裡面提取真實名稱
130
130
if ( fileinfo . Length < ( long ) 1024 * 1024 * 1024 * 3 ) {
131
131
132
- var text = GetFileHeader ( path , 1000 * 50 ) ;
132
+ var text = GetFileHeader ( path , 1000 * 300 ) ;
133
133
134
134
/* var indexSshsModelHash = text.IndexOf("\"sshs_model_hash\"");
135
135
if (indexSshsModelHash != -1)
@@ -190,6 +190,35 @@ public List<string> GetDirectoriesExcluding(string rootDir, string[] excludeDirs
190
190
return result ;
191
191
}
192
192
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
+
193
222
/// <summary>
194
223
/// 取得2進制檔案內容的前幾個字元
195
224
/// </summary>
@@ -238,7 +267,17 @@ private string ComputeSha256Hash(string input) {
238
267
return BitConverter . ToString ( hash ) . Replace ( "-" , "" ) . ToLowerInvariant ( ) ;
239
268
}
240
269
}
270
+ }
241
271
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 ; }
242
281
}
243
282
244
283
public class SafetensorsData {
0 commit comments