Пример #1
0
        private static bool HEditDataSaveHook(bool __result, HEditData __instance, ref BinaryWriter _writer, ref YS_Node.NodeControl _nodeControl, ref bool _isInitUserID)
        {
            ExtendedSave.HEditDataWriteEvent(__instance);

            ExtendedSave.Logger.Log(LogLevel.Debug, "MapInfo hook!");

            var extendedData = ExtendedSave.GetAllExtendedData(__instance);

            if (extendedData == null || extendedData.Count == 0)
            {
                return(__result);
            }

            var originalLength   = _writer.BaseStream.Length;
            var originalPosition = _writer.BaseStream.Position;

            try
            {
                var bytes = ExtendedSave.MessagePackSerialize(extendedData);

                _writer.Write(Marker);
                _writer.Write(Version);
                _writer.Write(bytes.Length);
                _writer.Write(bytes);
            }
            catch (Exception e)
            {
                ExtendedSave.Logger.Log(LogLevel.Warning, $"Failed to save extended data in card. {e.Message}");
                _writer.BaseStream.Position = originalPosition;
                _writer.BaseStream.SetLength(originalLength);
            }

            return(__result);
        }
Пример #2
0
        public static void ChaFileSaveFileHook(ChaFile file, BlockHeader header, ref long[] array3)
        {
            Dictionary <string, PluginData> extendedData = ExtendedSave.GetAllExtendedData(file);

            if (extendedData == null)
            {
                currentlySavingData = null;
                return;
            }

            currentlySavingData = MessagePackSerializer.Serialize(extendedData);

            //get offset
            long offset = array3.Sum();
            long length = currentlySavingData.LongLength;

            //insert our custom data length at the end
            Array.Resize(ref array3, array3.Length + 1);
            array3[array3.Length - 1] = length;

            //add info about our data to the block header
            BlockHeader.Info info = new BlockHeader.Info
            {
                name    = Marker,
                version = Version.ToString(),
                pos     = offset,
                size    = length
            };

            header.lstInfo.Add(info);
        }
Пример #3
0
        public static void SaveFileHook(ChaFile __instance, bool __result, BinaryWriter bw, bool savePng)
        {
            if (!__result)
            {
                return;
            }

            ExtendedSave.writeEvent(__instance);

            Dictionary <string, PluginData> extendedData = ExtendedSave.GetAllExtendedData(__instance);

            if (extendedData == null)
            {
                return;
            }

            byte[] bytes = MessagePackSerializer.Serialize(extendedData);

            bw.Write(Marker);
            bw.Write(Version);
            foreach (KeyValuePair <string, PluginData> kv in extendedData)
            {
                PluginData dict = kv.Value as PluginData;
            }

            bw.Write((int)bytes.Length);
            bw.Write(bytes);
        }
Пример #4
0
        public static void ChaFileCoordinateLoadHook(ChaFileCoordinate coordinate, BinaryReader br)
        {
            try
            {
                string marker  = br.ReadString();
                int    version = br.ReadInt32();

                int length = br.ReadInt32();

                if (marker == Marker && version == Version && length > 0)
                {
                    byte[] bytes      = br.ReadBytes(length);
                    var    dictionary = MessagePackSerializer.Deserialize <Dictionary <string, PluginData> >(bytes);

                    ExtendedSave.internalCoordinateDictionary.Set(coordinate, dictionary);
                }
                else
                {
                    ExtendedSave.internalCoordinateDictionary.Set(coordinate, new Dictionary <string, PluginData>()); //Overriding with empty data just in case there is some remnant from former loads.
                }
            }
            catch (EndOfStreamException)
            {
                /* Incomplete/non-existant data */
                ExtendedSave.internalCoordinateDictionary.Set(coordinate, new Dictionary <string, PluginData>());
            }
            catch (InvalidOperationException)
            {
                /* Invalid/unexpected deserialized data */
                ExtendedSave.internalCoordinateDictionary.Set(coordinate, new Dictionary <string, PluginData>());
            }
            ExtendedSave.coordinateReadEvent(coordinate); //Firing the event in any case
        }
Пример #5
0
        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>());
                    BepInEx.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>());
            }
        }
Пример #6
0
        public static void SceneInfoLoadHook(string path, BinaryReader br)
        {
            ExtendedSave.internalSceneDictionary.Clear();

            try
            {
                br.ReadString(); //Reading that useless string at the end "【KStudio】"

                string marker  = br.ReadString();
                int    version = br.ReadInt32();

                int length = br.ReadInt32();

                if (marker.Equals(Marker) && length > 0)
                {
                    byte[] bytes = br.ReadBytes(length);
                    ExtendedSave.internalSceneDictionary = MessagePackSerializer.Deserialize <Dictionary <string, PluginData> >(bytes);
                }
            }
            catch (EndOfStreamException)
            {
                /* Incomplete/non-existant data */
            }
            catch (InvalidOperationException)
            {
                /* Invalid/unexpected deserialized data */
            }

            ExtendedSave.sceneReadEvent(path);
        }
Пример #7
0
        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;


                var dictionary = MessagePackSerializer.Deserialize <Dictionary <string, PluginData> >(data);

                cardReadEventCalled = true;
                ExtendedSave.internalCharaDictionary.Set(file, dictionary);

                ExtendedSave.cardReadEvent(file);
            }
            else
            {
                ExtendedSave.internalCharaDictionary.Set(file, new Dictionary <string, PluginData>());
            }
        }
Пример #8
0
        public static void ChaFileCoordinateLoadHook(ChaFileCoordinate coordinate, BinaryReader br)
        {
            try
            {
                string marker  = br.ReadString();
                int    version = br.ReadInt32();

                int length = br.ReadInt32();

                if (marker == Marker && version == Version && length > 0)
                {
                    byte[] bytes      = br.ReadBytes(length);
                    var    dictionary = MessagePackSerializer.Deserialize <Dictionary <string, PluginData> >(bytes);

                    ExtendedSave.internalCoordinateDictionary.Set(coordinate, dictionary);
                }

                ExtendedSave.coordinateReadEvent(coordinate);
            }
            catch (EndOfStreamException)
            {
                /* Incomplete/non-existant data */
            }
            catch (InvalidOperationException)
            {
                /* Invalid/unexpected deserialized data */
            }
        }
Пример #9
0
        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);
            }
        }
Пример #10
0
        public static void LoadFileHook(ChaFile __instance, bool __result, BinaryReader br, bool noLoadPNG, bool noLoadStatus)
        {
            Dictionary <string, PluginData> dictionary = null;

            if (!__result)
            {
                return;
            }

            if (br.BaseStream.Position != br.BaseStream.Length)
            {
                long originalPosition = br.BaseStream.Position;

                try
                {
                    string marker  = br.ReadString();
                    int    version = br.ReadInt32();

                    if (marker == Marker && version == Version)
                    {
                        int length = br.ReadInt32();

                        if (length > 0)
                        {
                            byte[] bytes = br.ReadBytes(length);
                            dictionary = MessagePackSerializer.Deserialize <Dictionary <string, PluginData> >(bytes);
                        }
                    }
                    else
                    {
                        br.BaseStream.Position = originalPosition;
                    }
                }
                catch (EndOfStreamException)
                {
                    /* Incomplete/non-existant data */
                }
                catch (InvalidOperationException)
                {
                    /* Invalid/unexpected deserialized data */
                }
            }

            if (dictionary == null)
            {
                //initialize a new dictionary since it doesn't exist
                dictionary = new Dictionary <string, PluginData>();
            }

            ExtendedSave.internalDictionary.Set(__instance, dictionary);
            ExtendedSave.readEvent(__instance);
        }
Пример #11
0
        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);

                            ExtendedSave.internalCharaDictionary.Set(__instance, dictionary);
                        }
                    }
                    else
                    {
                        br.BaseStream.Position = originalPosition;
                    }
                }
                catch (EndOfStreamException)
                {
                    /* Incomplete/non-existant data */
                }
                catch (InvalidOperationException)
                {
                    /* Invalid/unexpected deserialized data */
                }
            }

            ExtendedSave.cardReadEvent(__instance);
        }
Пример #12
0
        public static void SceneInfoSaveHook(string path, BinaryWriter bw)
        {
            ExtendedSave.sceneWriteEvent(path);

            Dictionary <string, PluginData> extendedData = ExtendedSave.internalSceneDictionary;

            if (extendedData == null)
            {
                return;
            }
            byte[] data = MessagePackSerializer.Serialize(extendedData);

            bw.Write(Marker);    //Not super useful
            bw.Write(Version);   //but kept for consistency
            bw.Write(data.Length);
            bw.Write(data);
        }
Пример #13
0
        public static void ChaFileCoordinateSaveHook(ChaFileCoordinate file, BinaryWriter bw)
        {
            ExtendedSave.coordinateWriteEvent(file);

            BepInEx.Logger.Log(BepInEx.Logging.LogLevel.Debug, "Coordinate hook!");

            Dictionary <string, PluginData> extendedData = ExtendedSave.GetAllExtendedData(file);

            if (extendedData == null)
            {
                return;
            }

            byte[] data = MessagePackSerializer.Serialize(extendedData);

            bw.Write(Marker);
            bw.Write(Version);
            bw.Write(data.Length);
            bw.Write(data);
        }
Пример #14
0
        private static bool HEditDataLoadHook(bool __result, HEditData __instance, ref BinaryReader _reader, ref int _loadKind, ref YS_Node.NodeControl _nodeControl, ref HEditData.InfoData _info, ref bool _isEdit)
        {
            var originalPosition = _reader.BaseStream.Position;

            try
            {
                var marker  = _reader.ReadString();
                var version = _reader.ReadInt32();
                var length  = _reader.ReadInt32();
                if (marker == Marker && version == Version && length > 0)
                {
                    var bytes      = _reader.ReadBytes(length);
                    var dictionary = ExtendedSave.MessagePackDeserialize <Dictionary <string, PluginData> >(bytes);
                    ExtendedSave._internalHEditDataDictionary.Set(__instance, dictionary);
                }
                else
                {
                    ExtendedSave._internalHEditDataDictionary.Set(__instance, new Dictionary <string, PluginData>());
                    _reader.BaseStream.Position = originalPosition;
                }
            }
            catch (EndOfStreamException)
            {
                /* Incomplete/non-existant data */
                ExtendedSave._internalHEditDataDictionary.Set(__instance, new Dictionary <string, PluginData>());
                _reader.BaseStream.Position = originalPosition;
            }
            catch (InvalidOperationException)
            {
                /* Invalid/unexpected deserialized data */
                ExtendedSave._internalHEditDataDictionary.Set(__instance, new Dictionary <string, PluginData>());
                _reader.BaseStream.Position = originalPosition;
            }

            ExtendedSave.HEditDataReadEvent(__instance);

            return(__result);
        }
Пример #15
0
        public static void SceneInfoImportHook(string path, BinaryReader br, Version version)
        {
            //Reading useless data
            br.ReadInt32();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadInt32();
            br.ReadBoolean();
            br.ReadInt32();
            if (version.CompareTo(new Version(0, 0, 2)) >= 0)
            {
                br.ReadSingle();
            }
            if (version.CompareTo(new Version(0, 0, 1)) <= 0)
            {
                br.ReadBoolean();
                br.ReadSingle();
                br.ReadString();
            }

            if (version.CompareTo(new Version(0, 0, 2)) >= 0)
            {
                br.ReadBoolean();
                br.ReadString();
                br.ReadSingle();
            }

            br.ReadBoolean();
            br.ReadSingle();
            br.ReadSingle();
            if (version.CompareTo(new Version(0, 0, 2)) >= 0)
            {
                br.ReadSingle();
            }
            if (version.CompareTo(new Version(0, 0, 1)) <= 0)
            {
                br.ReadBoolean();
            }
            br.ReadBoolean();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadBoolean();
            if (version.CompareTo(new Version(0, 0, 1)) <= 0)
            {
                br.ReadSingle();
            }
            br.ReadBoolean();
            if (version.CompareTo(new Version(0, 0, 2)) >= 0)
            {
                br.ReadString();
                br.ReadSingle();
                br.ReadSingle();
            }

            br.ReadBoolean();
            if (version.CompareTo(new Version(0, 0, 2)) >= 0)
            {
                br.ReadString();
                br.ReadString();
            }

            if (version.CompareTo(new Version(0, 0, 4)) >= 0)
            {
                br.ReadInt32();
            }
            if (version.CompareTo(new Version(0, 0, 2)) >= 0)
            {
                br.ReadBoolean();
            }
            if (version.CompareTo(new Version(0, 0, 4)) >= 0)
            {
                br.ReadBoolean();
                br.ReadBoolean();
                br.ReadSingle();
                br.ReadString();
            }

            if (version.CompareTo(new Version(0, 0, 5)) >= 0)
            {
                br.ReadSingle();
                br.ReadInt32();
                br.ReadSingle();
            }

            int num = br.ReadInt32();

            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            if (num == 1)
            {
                br.ReadSingle();
            }
            else
            {
                br.ReadSingle();
                br.ReadSingle();
                br.ReadSingle();
            }

            br.ReadSingle();
            for (int j = 0; j < 10; j++)
            {
                num = br.ReadInt32();
                br.ReadSingle();
                br.ReadSingle();
                br.ReadSingle();
                br.ReadSingle();
                br.ReadSingle();
                br.ReadSingle();
                if (num == 1)
                {
                    br.ReadSingle();
                }
                else
                {
                    br.ReadSingle();
                    br.ReadSingle();
                    br.ReadSingle();
                }

                br.ReadSingle();
            }

            br.ReadString();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadBoolean();

            br.ReadString();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadSingle();
            br.ReadBoolean();

            br.ReadInt32();
            br.ReadInt32();
            br.ReadBoolean();

            br.ReadInt32();
            br.ReadInt32();
            br.ReadBoolean();

            br.ReadInt32();
            br.ReadString();
            br.ReadBoolean();
            br.ReadString();
            br.ReadString();
            br.ReadString();
            br.ReadBytes(16);

            ExtendedSave.internalSceneDictionary.Clear();

            try
            {
                string marker = br.ReadString();
                int    ver    = br.ReadInt32();

                int length = br.ReadInt32();

                if (marker.Equals(Marker) && length > 0)
                {
                    byte[] bytes = br.ReadBytes(length);
                    ExtendedSave.internalSceneDictionary = MessagePackSerializer.Deserialize <Dictionary <string, PluginData> >(bytes);
                }
            }
            catch (EndOfStreamException)
            {
                /* Incomplete/non-existant data */
            }
            catch (InvalidOperationException)
            {
                /* Invalid/unexpected deserialized data */
            }

            ExtendedSave.sceneImportEvent(path);
        }
Пример #16
0
 public static void ChaFileSaveFilePreHook(ChaFile __instance, bool __result, BinaryWriter bw, bool savePng)
 {
     ExtendedSave.cardWriteEvent(__instance);
 }