Listens for log entries and writes them to registered ILogOutputs.
Esempio n. 1
0
        void IPreferencesBackend.Commit()
        {
            if (!dirty)
            {
                return;
            }

            dirty = false;

            using (MemoryStream s = new MemoryStream()) {
                using (BinaryWriter w = new BinaryWriter(s)) {
                    w.Write((ushort)data.Count);

                    foreach (var pair in data)
                    {
                        w.Write(pair.Key);

                        switch (pair.Value)
                        {
                        case string value: {
                            w.Write((byte)0);
                            w.Write(value);
                            break;
                        }

                        case bool value: {
                            w.Write((byte)1);
                            w.Write(value);
                            break;
                        }

                        case byte value: {
                            w.Write((byte)2);
                            w.Write(value);
                            break;
                        }

                        case int value: {
                            w.Write((byte)3);
                            w.Write(value);
                            break;
                        }

                        case long value: {
                            w.Write((byte)4);
                            w.Write(value);
                            break;
                        }

                        case short value: {
                            w.Write((byte)5);
                            w.Write(value);
                            break;
                        }

                        case uint value: {
                            w.Write((byte)6);
                            w.Write(value);
                            break;
                        }

                        case string[] value: {
                            w.Write((byte)10);
                            w.Write((byte)value.Length);
                            for (int j = 0; j < value.Length; j++)
                            {
                                w.Write(value[j]);
                            }
                            break;
                        }

                        case bool[] value: {
                            w.Write((byte)11);
                            w.Write((byte)value.Length);
                            for (int j = 0; j < value.Length; j++)
                            {
                                w.Write(value[j]);
                            }
                            break;
                        }

                        case byte[] value: {
                            w.Write((byte)12);
                            w.Write((byte)value.Length);
                            for (int j = 0; j < value.Length; j++)
                            {
                                w.Write(value[j]);
                            }
                            break;
                        }

                        case int[] value: {
                            w.Write((byte)13);
                            w.Write((byte)value.Length);
                            for (int j = 0; j < value.Length; j++)
                            {
                                w.Write(value[j]);
                            }
                            break;
                        }

                        case long[] value: {
                            w.Write((byte)14);
                            w.Write((byte)value.Length);
                            for (int j = 0; j < value.Length; j++)
                            {
                                w.Write(value[j]);
                            }
                            break;
                        }

                        case short[] value: {
                            w.Write((byte)15);
                            w.Write((byte)value.Length);
                            for (int j = 0; j < value.Length; j++)
                            {
                                w.Write(value[j]);
                            }
                            break;
                        }

                        case uint[] value: {
                            w.Write((byte)16);
                            w.Write((byte)value.Length);
                            for (int j = 0; j < value.Length; j++)
                            {
                                w.Write(value[j]);
                            }
                            break;
                        }

                        default:
                            Log.Write(LogType.Error, "Unknown preference type: " + pair.Value.GetType().FullName);
                            break;
                        }
                    }
                }

                ArraySegment <byte> buffer;
                if (s.TryGetBuffer(out buffer))
                {
                    string base64 = Base64.EncodeToString(buffer.Array, buffer.Offset, buffer.Count, Base64Flags.NoPadding | Base64Flags.NoWrap);

                    ISharedPreferencesEditor editor = sharedPrefs.Edit();

                    editor.PutString("Root", base64);

                    editor.Commit();
                }
                else
                {
                    Log.Write(LogType.Error, "Can't get memory buffer to save preferences.");
                }
            }
        }