@@ -64,7 +64,8 @@ public async Task<XivImc> GetImcInfo(IItemModel item)
64
64
var info = await GetFullImcInfo ( item ) ;
65
65
var slot = item . GetItemSlotAbbreviation ( ) ;
66
66
67
- return info . GetEntry ( item . ModelInfo . ImcSubsetID , slot ) ;
67
+ var result = info . GetEntry ( item . ModelInfo . ImcSubsetID , slot ) ;
68
+ return result ;
68
69
}
69
70
70
71
public async Task < FullImcInfo > GetFullImcInfo ( IItemModel item )
@@ -79,14 +80,19 @@ public async Task<FullImcInfo> GetFullImcInfo(IItemModel item)
79
80
{
80
81
// Some dual wield items don't have a second IMC, and just default to the first.
81
82
var gear = ( XivGear ) item ;
82
- if ( gear != null && gear . PairedItem != null ) {
83
- return await ( GetFullImcInfo ( gear . PairedItem ) ) ;
83
+ if ( gear != null && gear . PairedItem != null )
84
+ {
85
+ var pair = gear . PairedItem ;
86
+ var imcPath = GetImcPath ( pair ) ;
87
+ var path = imcPath . Folder + "/" + imcPath . File ;
88
+ return await ( GetFullImcInfo ( path ) ) ;
84
89
}
85
90
}
86
91
87
92
return info ;
88
93
}
89
94
95
+
90
96
/// <summary>
91
97
/// Gets the full IMC information for a given item
92
98
/// </summary>
@@ -185,6 +191,87 @@ public async Task<FullImcInfo> GetFullImcInfo(string path)
185
191
} ) ;
186
192
}
187
193
194
+ public async Task SaveImcInfo ( XivImc info , IItemModel item )
195
+ {
196
+ var full = await GetFullImcInfo ( item ) ;
197
+ full . SetEntry ( info , item . ModelInfo . ImcSubsetID , item . GetItemSlotAbbreviation ( ) ) ;
198
+ await SaveFullImcInfo ( full , item ) ;
199
+ }
200
+
201
+ public async Task SaveImcInfo ( XivImc info , string path , int subsetId = - 1 , string slot = "" )
202
+ {
203
+ var full = await GetFullImcInfo ( path ) ;
204
+ full . SetEntry ( info , subsetId , slot ) ;
205
+ await SaveFullImcInfo ( full , path ) ;
206
+ }
207
+
208
+ public async Task SaveFullImcInfo ( FullImcInfo info , IItemModel item )
209
+ {
210
+ try
211
+ {
212
+ var imcPath = GetImcPath ( item ) ;
213
+ var path = imcPath . Folder + "/" + imcPath . File ;
214
+ await SaveFullImcInfo ( info , path ) ;
215
+ }
216
+ catch
217
+ {
218
+ // Some dual wield items don't have a second IMC, and just default to the first.
219
+ var gear = ( XivGear ) item ;
220
+ if ( gear != null && gear . PairedItem != null )
221
+ {
222
+ var pair = gear . PairedItem ;
223
+ var imcPath = GetImcPath ( pair ) ;
224
+ var path = imcPath . Folder + "/" + imcPath . File ;
225
+ await ( SaveFullImcInfo ( info , path ) ) ;
226
+ }
227
+ }
228
+ return ;
229
+
230
+ }
231
+
232
+ public async Task SaveFullImcInfo ( FullImcInfo info , string path , string itemName = null , string category = null , string source = null )
233
+ {
234
+ var index = new Index ( _gameDirectory ) ;
235
+ var dat = new Dat ( _gameDirectory ) ;
236
+
237
+
238
+ var imcOffset = await index . GetDataOffset ( path ) ;
239
+
240
+ // No writing new IMC files.
241
+ if ( imcOffset == 0 )
242
+ {
243
+ throw new InvalidDataException ( $ "Could not find offset for { path } ") ;
244
+ }
245
+
246
+ var data = new List < byte > ( ) ;
247
+
248
+ // 4 Header bytes.
249
+ data . AddRange ( BitConverter . GetBytes ( info . SubsetCount ) ) ;
250
+ data . AddRange ( BitConverter . GetBytes ( ( short ) info . TypeIdentifier ) ) ;
251
+
252
+ // The rest of this is easy, it's literally just post all the sets in order.
253
+ foreach ( var entry in info . DefaultSubset )
254
+ {
255
+ data . AddRange ( entry . GetBytes ( ) ) ;
256
+ }
257
+
258
+ foreach ( var set in info . SubsetList )
259
+ {
260
+ foreach ( var entry in set )
261
+ {
262
+ data . AddRange ( entry . GetBytes ( ) ) ;
263
+ }
264
+ }
265
+
266
+ // That's it.
267
+
268
+ itemName ??= Path . GetFileName ( path ) ;
269
+ category ??= "Meta" ;
270
+ source ??= "Internal" ;
271
+
272
+ await dat . ImportType2Data ( data . ToArray ( ) , itemName , path , category , source ) ;
273
+ }
274
+
188
275
/// <summary>
189
276
/// Gets the IMC internal path for the given model info
190
277
/// </summary>
@@ -320,13 +407,42 @@ public XivImc GetEntry(int subsetID = -1, string slot = "")
320
407
321
408
// Get which offset the slot uses.
322
409
var idx = 0 ;
323
- if ( _slotOffsetDictionary . ContainsKey ( slot ) )
410
+ if ( _slotOffsetDictionary . ContainsKey ( slot ) && _slotOffsetDictionary [ slot ] < subset . Count )
324
411
{
325
412
idx = _slotOffsetDictionary [ slot ] ;
326
413
}
327
414
328
415
return subset [ idx ] ;
329
416
}
417
+
418
+ public void SetEntry ( XivImc info , int subsetID = - 1 , string slot = "" )
419
+ {
420
+ // Variant IDs are 1 based, not 0 based.
421
+ var index = subsetID - 1 ;
422
+
423
+ // Invalid Index, return default.
424
+ if ( index >= SubsetCount || index < 0 )
425
+ {
426
+ index = - 1 ;
427
+ }
428
+
429
+ // Test for getting default set.
430
+ var subset = DefaultSubset ;
431
+ if ( index >= 0 )
432
+ {
433
+ subset = SubsetList [ index ] ;
434
+ }
435
+
436
+ // Get which offset the slot uses.
437
+ var idx = 0 ;
438
+ if ( _slotOffsetDictionary . ContainsKey ( slot ) && _slotOffsetDictionary [ slot ] < subset . Count )
439
+ {
440
+ idx = _slotOffsetDictionary [ slot ] ;
441
+ }
442
+
443
+ // Assign info.
444
+ subset [ idx ] = info ;
445
+ }
330
446
}
331
447
332
448
}
0 commit comments