コード例 #1
0
        void DeSerialize()
        {
            string     scriptdata_filename = Path.GetTempPath() + "temp\\scriptdata.flavor";
            FileStream fs = null;

            try
            {
                fs = new FileStream(scriptdata_filename, FileMode.Open);
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: Failed to open " + scriptdata_filename + " no scripts will be serialized! " + e.Message);
                throw e;
            }
            BinaryFormatter bf = new BinaryFormatter();

            serializedScriptsDatabase.Clear();

            try
            {
                while (true)
                {
                    if (fs.Position >= fs.Length)   //eof register
                    {
                        break;
                    }
                    string register_id = (string)bf.Deserialize(fs);

                    SerializedScriptData ssd = new SerializedScriptData(register_id);
                    int number_of_props      = (int)bf.Deserialize(fs);
                    for (int i = 0; i < number_of_props; ++i)
                    {
                        SerializeScriptPropertyData script_data = new SerializeScriptPropertyData();
                        script_data.property_name      = (string)bf.Deserialize(fs);
                        script_data.property_type_name = (string)bf.Deserialize(fs);
                        object deserwrapper = bf.Deserialize(fs);
                        if (deserwrapper is SerializeScriptPropertyData.SerializeScriptValueWrapper)
                        {
                            //support new format without wrapped loading
                            SerializeScriptPropertyData.SerializeScriptValueWrapper wrapper = (SerializeScriptPropertyData.SerializeScriptValueWrapper)deserwrapper;
                            script_data.property_value = wrapper.property_value;
                        }
                        else if (deserwrapper is SerializeScriptPropertyData.SerializeScriptValueCustomWrapper)
                        {
                            //custom wrapped load
                            SerializeScriptPropertyData.SerializeScriptValueCustomWrapper wrapper = (SerializeScriptPropertyData.SerializeScriptValueCustomWrapper)deserwrapper;
                            MethodInfo load_wrapper = typeof(SerializeScriptPropertyData.SerializeCustomWrappers).GetMethod("WrapLoad_" + script_data.property_type_name);
                            if (load_wrapper == null)
                            {
                                Logger.Log("Attempt to load: " + script_data.property_name + " but no longer has wrapped load function.");
                                continue;
                            }
                            script_data.property_value = load_wrapper.Invoke(null, new object[] { wrapper.property_value });
                        }
                        else
                        {
                            script_data.property_value = deserwrapper;
                        }
                        ssd.property_store.Add(script_data);
                    }
                    try
                    {
                        serializedScriptsDatabase.Add(register_id, ssd);
                    }
                    catch (Exception)
                    {
                        Logger.Log("Repeated Script Register ID found: " + register_id);
                    }
                }
            }
            catch (System.Runtime.Serialization.SerializationException e)
            {
                Console.WriteLine("ERROR: Failed to serialize " + scriptdata_filename + " some scripts will NOT be serialized! " + e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: Failed to serialize " + scriptdata_filename + " some scripts will NOT be serialized! " + e.Message);
            }

            fs.Close();
        }
コード例 #2
0
        void Serialize()
        {
            BinaryFormatter bf = new BinaryFormatter();

            using (FileStream fs = new FileStream(Path.GetTempPath() + "temp\\scriptdata.flavor", FileMode.Create))
            {
                foreach (BoundScript bs in registeredScripts)
                {
                    if (String.IsNullOrWhiteSpace(bs.register_id))
                    {
                        Logger.Log("ELROY ERROR 2: THE ELROYENING: NO REGISTER ID SET FOR BOUNDSCRIPT");
                        continue;
                    }
                    //write header
                    bf.Serialize(fs, bs.register_id);
                    if (bs.scriptObject != null)
                    {
                        PropertyInfo[] properties = bs.scriptObject.GetType().GetProperties();
                        bf.Serialize(fs, properties.Count((PropertyInfo pi) => { return(pi.CanWrite && (pi.PropertyType.IsSerializable || IsPropertyWrapped(pi.PropertyType.Name))); }));
                        //write all properties
                        foreach (PropertyInfo property in properties)
                        {
                            if (property.CanWrite)
                            {
                                if (property.PropertyType.IsSerializable)
                                {
                                    //non wrapped value
                                    bf.Serialize(fs, property.Name);                        //property name
                                    bf.Serialize(fs, property.PropertyType.Name);           //property type name
                                    object value = property.GetValue(bs.scriptObject);
                                    SerializeScriptPropertyData.SerializeScriptValueWrapper wrapper = new SerializeScriptPropertyData.SerializeScriptValueWrapper();
                                    wrapper.property_value = value;
                                    bf.Serialize(fs, wrapper);
                                }
                                else
                                {
                                    MethodInfo wrapper_func = GetPropertyWrappedFunc(property.PropertyType.Name);
                                    if (wrapper_func != null)
                                    {
                                        //custom serialize wrapped value
                                        bf.Serialize(fs, property.Name);
                                        bf.Serialize(fs, property.PropertyType.Name);
                                        object value = property.GetValue(bs.scriptObject);
                                        SerializeScriptPropertyData.SerializeScriptValueCustomWrapper wrapper = new SerializeScriptPropertyData.SerializeScriptValueCustomWrapper();
                                        wrapper.property_value = wrapper_func.Invoke(null, new object[] { value });
                                        bf.Serialize(fs, wrapper);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        SetHasError(true);
                        bf.Serialize(fs, (int)0);
                        Logger.Log("No property info serialized for malformed script " + bs.register_id);
                    }
                }
                string eof_id = "";
                bf.Serialize(fs, eof_id);
            }
        }