Пример #1
0
        /// <summary>
        /// Saves the component to a .casset
        /// </summary>
        internal void Save()
        {
            if (!Directory.Exists("Library"))
            {
                Directory.CreateDirectory("Library");
            }

            if (!File.Exists("Library/COMP" + ID + ".casset"))
            {
                // create it
                using (BinaryWriter bw = new BinaryWriter(File.Open("Library/COMP" + ID + ".casset", FileMode.Create)))
                {
                    // header with id
                    bw.Write(ID);

                    // type
                    bw.Write(TypeString);

                    // systemtype
                    bw.Write(SystemType.ToString());

                    // name
                    bw.Write(Name);

                    // enabled
                    bw.Write(Enabled);

                    // parent id
                    bw.Write(Parent.ID);

                    FieldInfo[]    fields = ComponentUtils.GetFields(this);
                    PropertyInfo[] props  = ComponentUtils.GetProperties(this);

                    // Fields
                    int fieldCount = 0;
                    foreach (FieldInfo field in fields)
                    {
                        if (ComponentUtils.GetValue(this, field) is Resource)
                        {
                            fieldCount++;
                        }
                    }

                    bw.Write(fieldCount);
                    if (fieldCount > 0)
                    {
                        foreach (FieldInfo field in fields)
                        {
                            // write value
                            if (ComponentUtils.GetValue(this, field) is Resource)
                            {
                                // write name
                                bw.Write(ComponentUtils.GetName(field));

                                Resource r = (Resource)ComponentUtils.GetValue(this, field);
                                bw.Write(r.ID);
                                r.Save();
                            }
                        }
                    }

                    // Props
                    int propCount = 0;
                    foreach (PropertyInfo prop in props)
                    {
                        if (ComponentUtils.GetValue(this, prop) is Resource)
                        {
                            propCount++;
                        }
                    }

                    bw.Write(propCount);
                    if (propCount > 0)
                    {
                        foreach (PropertyInfo prop in props)
                        {
                            // write value
                            if (ComponentUtils.GetValue(this, prop) is Resource)
                            {
                                // write name
                                bw.Write(ComponentUtils.GetName(prop));

                                Resource r = (Resource)ComponentUtils.GetValue(this, prop);
                                bw.Write(r.ID);
                                r.Save();
                            }
                        }
                    }
                }
            }
            else
            {
                // TODO: Update existing
            }
        }