public static void ChaFileLoadFileHook(ChaFile file, BlockHeader header, BinaryReader reader) { var info = header.SearchInfo(Marker); if (info != null && info.version == Version.ToString()) { long originalPosition = reader.BaseStream.Position; long basePosition = originalPosition - header.lstInfo.Sum(x => x.size); reader.BaseStream.Position = basePosition + info.pos; byte[] data = reader.ReadBytes((int)info.size); reader.BaseStream.Position = originalPosition; cardReadEventCalled = true; try { var dictionary = MessagePackSerializer.Deserialize <Dictionary <string, PluginData> >(data); ExtendedSave.internalCharaDictionary.Set(file, dictionary); } catch (Exception e) { ExtendedSave.internalCharaDictionary.Set(file, new Dictionary <string, PluginData>()); ExtendedSave.Logger.Log(LogLevel.Warning, $"Invalid or corrupted extended data in card \"{file.charaFileName}\" - {e.Message}"); } ExtendedSave.CardReadEvent(file); } else { ExtendedSave.internalCharaDictionary.Set(file, new Dictionary <string, PluginData>()); } }
public static void ChaFileLoadFilePostHook(ChaFile __instance, bool __result, BinaryReader br, bool noLoadPNG, bool noLoadStatus) { if (!__result) { return; } //Compatibility for ver 1 and 2 ext save data if (br.BaseStream.Position != br.BaseStream.Length) { long originalPosition = br.BaseStream.Position; try { string marker = br.ReadString(); int version = br.ReadInt32(); if (marker == "KKEx" && version == 2) { int length = br.ReadInt32(); if (length > 0) { byte[] bytes = br.ReadBytes(length); var dictionary = MessagePackSerializer.Deserialize <Dictionary <string, PluginData> >(bytes); cardReadEventCalled = true; ExtendedSave.internalCharaDictionary.Set(__instance, dictionary); ExtendedSave.CardReadEvent(__instance); } } else { br.BaseStream.Position = originalPosition; } } catch (EndOfStreamException) { /* Incomplete/non-existant data */ } catch (SystemException) { /* Invalid/unexpected deserialized data */ } } //If the event wasn't called at this point, it means the card doesn't contain any data, but we still need to call the even for consistency. if (cardReadEventCalled == false) { ExtendedSave.internalCharaDictionary.Set(__instance, new Dictionary <string, PluginData>()); ExtendedSave.CardReadEvent(__instance); } }