Beispiel #1
0
        public object readObject(TypeData expectedType)
        {
            if (expectedType.isValueType()) {
            return readValue(expectedType);
            }

            int objectId = readId();
            // see if it's one we've already seen
            if (objectId < _objects.Count) {
            return _objects[objectId];
            }

            // verify that objectId is as expected
            if (objectId != _objects.Count) {
            // TODO
            throw new System.Exception("Precondition failed: read bad id.");
            }

            // TODO: we need to pass the objectId to the TypeData so that they can call back to a
            // new method on this class for setting the typedata into the slot properly.
            // I tried to do this (Sep 10) but it causes f*****g unity to crash, crash, crash.
            // It could even log that I did it after the fact, and that line would log but then
            // unity would crash.
            // So: hooray for a shitty life. I already fixed this problem, but now it needs to
            // remain broken until I can fix it again...
            _objects.Add(null);
            object value = readValue(expectedType);
            _objects[objectId] = value;
            return value;
        }
 // TODO: sort out argument order, etc, with superclass
 public GenericExportingReflectiveTypeData(
 ExportContext ctx, Type fullType, TypeData baseType, TypeData[] args)
     : base(fullType, ctx)
 {
     _baseType = baseType;
     _args = args;
 }
        public override object readObject(ImportContext ctx, TypeData[] typeArgs = null)
        {
            object value = (_type == null) ? null : Activator.CreateInstance(_type);
            int fieldCount = ctx.readLength();
            this.logInfo("Reading " + fieldCount + " fields...");
            for (int ii = 0; ii < fieldCount; ii++) {
            int fieldId = ctx.readId();
            FieldData field;
            if (fieldId < _fields.Count) {
                field = _fields[fieldId];

            } else {
                // ASSERT the fieldId
                // TODO
                if (fieldId != _fields.Count) {
                    this.logWarning("Unexpected field id!",
                            "expected", _fields.Count,
                            "got", fieldId);
                    throw new Exception("Unexpected field id!");
                }

                // new field
                string name = ctx.readString();
                TypeData type = ctx.readType();
                FieldInfo fieldInfo;
                if (_fieldInfo != null) {
                    _fieldInfo.TryGetValue(name, out fieldInfo);
                    if (_fieldInfo == null) {
                        ctx.warn("Importing class no longer has field: " + name);
                    }
                } else {
                    fieldInfo = null;
                }
                // fieldInfo can now be null
                field = new FieldData(type, fieldInfo);
                _fields.Add(field);
            }
            field.readField(ctx, value);
            }
            return value;
        }
        public override void writeObject(ExportContext ctx, object value, TypeData[] typeArgs = null)
        {
            // make a list of all the fields that have changed, then send only those
            // TODO: Improve this.. Perhaps it writes them all to a new stream, a byte[], so that
            // we can insert the length afterwards... ugh...
            List<FieldData> fields = null;
            foreach (FieldData field in _fields) {
            if (field.hasChanged(value)) {
                (fields ?? (fields = new List<FieldData>())).Add(field);
            }
            }
            if (fields == null) {
            ctx.writeLength(0);
            return;
            }

            ctx.writeLength(fields.Count);
            foreach (FieldData field in fields) {
            field.writeField(ctx, this, value);
            }
        }
Beispiel #5
0
 public override void writeObject(ExportContext ctx, object value, TypeData[] typeArgs = null)
 {
     _baseType.writeObject(ctx, value, _args);
 }
Beispiel #6
0
 protected void readEntries(
     ImportContext ctx, Action<object> addAction, int size, TypeData expectedElementType)
 {
     for (int ii = 0; ii < size; ii++) {
     addAction(ctx.readObject(expectedElementType));
     }
 }
Beispiel #7
0
 public override object readObject(ImportContext ctx, TypeData[] typeArgs = null)
 {
     return _baseType.readObject(ctx, _args);
 }
 public FieldData(TypeData type, FieldInfo field)
 {
     _type = type;
     _field = field;
 }
 public override void writeObject(ExportContext ctx, object value, TypeData[] typeArgs = null)
 {
     throw new Exception();
 }
Beispiel #10
0
 public override object readObject(ImportContext ctx, TypeData[] typeArguments = null)
 {
     return ctx.readString();
 }
Beispiel #11
0
 public abstract object readObject(ImportContext ctx, TypeData[] typeArguments = null);
Beispiel #12
0
 public override void writeObject(ExportContext ctx, object value, TypeData[] typeArgs = null)
 {
     IList list = (IList)value;
     writeEntries(ctx, list, list.Count, typeArgs[0]);
 }
Beispiel #13
0
 public override object readObject(ImportContext ctx, TypeData[] typeArgs = null)
 {
     int size = ctx.readLength();
     TypeData elementType = typeArgs[0];
     Type fullType = typeof(HashMultiset<>).MakeGenericType(elementType.getType());
     object value = Activator.CreateInstance(fullType);
     if (size > 0) {
     MethodInfo method = fullType.GetMethod("Add",
             new Type[] { elementType.getType(), typeof(int) });
     object[] argArray = new object[2];
     for (int ii = 0; ii < size; ii++) {
         argArray[0] = ctx.readObject(elementType);
         argArray[1] = ctx.readLength();
         method.Invoke(value, argArray);
     }
     }
     return value;
 }
Beispiel #14
0
 public override void writeObject(ExportContext ctx, object value, TypeData[] typeArguments = null)
 {
     ctx.writeFloat((float)value);
 }
Beispiel #15
0
 public override object readObject(ImportContext ctx, TypeData[] typeArgs = null)
 {
     int size = ctx.readLength();
     TypeData elementType = typeArgs[0];
     Type listType = getType().MakeGenericType(elementType.getType());
     IList list = (IList)Activator.CreateInstance(listType);
     readEntries(ctx, o => list.Add(o), size, elementType);
     return list;
 }
Beispiel #16
0
 public override void writeObject(ExportContext ctx, object value, TypeData[] typeArgs = null)
 {
     string s = Enum.GetName(_type, value);
     if (s == null) {
     throw new ArgumentException("Enum value has no name: " + _type + ": " + value);
     }
     ctx.writeString(s);
 }
Beispiel #17
0
        public override object readObject(ImportContext ctx, TypeData[] typeArgs = null)
        {
            string s = ctx.readString();
            if (_type != null) {
            try {
                return Enum.Parse(_type, s);

            } catch (ArgumentException) {
                ctx.warn("could not find enum constant '" + s + "' in type " + _type);
            }
            }
            return null;
        }
Beispiel #18
0
 protected void writeEntries(
     ExportContext ctx, IEnumerable enumerable, int size, TypeData expectedElementType)
 {
     ctx.writeLength(size);
     foreach (object entry in enumerable) {
     ctx.writeObject(entry, expectedElementType);
     }
 }
Beispiel #19
0
 public override object readObject(ImportContext ctx, TypeData[] typeArgs = null)
 {
     int size = ctx.readLength();
     TypeData elementType = typeArgs[0];
     Type setType = getType().MakeGenericType(elementType.getType());
     object theSet = Activator.CreateInstance(setType);
     MethodInfo method = setType.GetMethod("Add");
     object[] argArray = new object[1];
     Action<object> addAction = obj => {
         argArray[0] = obj;
         method.Invoke(theSet, argArray);
     };
     readEntries(ctx, addAction, size, elementType);
     return theSet;
 }
Beispiel #20
0
        public override void writeObject(ExportContext ctx, object value, TypeData[] typeArgs = null)
        {
            // TODO: we should consider making a non-generic IMuliset interface for easy wildcarding
            TypeData elementType = typeArgs[0];
            Type fullType = value.GetType();
            int size = (int)fullType.GetProperty("ElementCount").GetValue(value, null);
            ctx.writeLength(size);
            if (size == 0) {
            return;
            }

            IEnumerable en = (IEnumerable)fullType.GetMethod("EntrySet").Invoke(value, null);
            PropertyInfo getKey = null, getVal = null;
            foreach (var entry in en) {
            if (getKey == null) {
                Type entryType = entry.GetType();
                getKey = entryType.GetProperty("Key");
                getVal = entryType.GetProperty("Value");
            }
            this.logInfo("Writing value of multiset",
                    "value", getKey.GetValue(entry, null));
            ctx.writeObject(getKey.GetValue(entry, null), elementType);
            ctx.writeLength((int)getVal.GetValue(entry, null));
            }
        }
Beispiel #21
0
 public override void writeObject(ExportContext ctx, object value, TypeData[] typeArgs = null)
 {
     // TODO: with a newer version of .NET we could do this better
     IEnumerable en = (IEnumerable)value;
     int size = en.Cast<object>().Count(); // TODO
     writeEntries(ctx, en, size, typeArgs[0]);
 }
Beispiel #22
0
 public override object readObject(ImportContext ctx, TypeData[] typeArguments = null)
 {
     // TODO: is this correct?
     return ctx.readObject(this);
 }
Beispiel #23
0
 public override void writeObject(ExportContext ctx, object value, TypeData[] typeArguments = null)
 {
     ctx.writeString((string)value);
 }
Beispiel #24
0
        public override void writeObject(ExportContext ctx, object value, TypeData[] typeArgs = null)
        {
            TypeData keyType = typeArgs[0];
            TypeData valType = typeArgs[1];
            Type fullType = value.GetType();
            int size = (int)fullType.GetProperty("Count").GetValue(value, null);
            ctx.writeLength(size);
            if (size == 0) {
            return;
            }

            Type entryType = typeof(KeyValuePair<,>).MakeGenericType(
                keyType.getType(), valType.getType());
            Type ienumerableType = typeof(IEnumerable<>).MakeGenericType(entryType);
            IEnumerator en = (IEnumerator)
                ienumerableType.GetMethod("GetEnumerator").Invoke(value, null);
            PropertyInfo getKey = entryType.GetProperty("Key");
            PropertyInfo getVal = entryType.GetProperty("Value");
            while (en.MoveNext()) {
            var entry = en.Current;
            ctx.writeObject(getKey.GetValue(entry, null), keyType);
            ctx.writeObject(getVal.GetValue(entry, null), valType);
            }
        }
Beispiel #25
0
 public abstract void writeObject(ExportContext ctx, object value, TypeData[] typeArguments = null);
Beispiel #26
0
 /**
  * Construct a ParameterizedTypeData during exporting.
  */
 public ParameterizedTypeData(Type fullType, TypeData baseType, TypeData[] args = null)
     : base(fullType)
 {
     _baseType = baseType;
     _args = args;
 }
Beispiel #27
0
 public override object readObject(ImportContext ctx, TypeData[] typeArgs = null)
 {
     int size = ctx.readLength();
     TypeData keyType = typeArgs[0];
     TypeData valType = typeArgs[1];
     Type dicType = getType().MakeGenericType(keyType.getType(), valType.getType());
     IDictionary dic = (IDictionary)Activator.CreateInstance(dicType);
     this.logInfo("Going to be reading in...",
         "dic", dic, "keyType", keyType.getType());
     for (int ii = 0; ii < size; ii++) {
     dic.Add(ctx.readObject(keyType), ctx.readObject(valType));
     }
     return dic;
 }
Beispiel #28
0
 // TODO: isFinal... maybe if the base class if final and all the parameter types are final?
 /**
  * Construct a ParameterizedTypeData during importing.
  */
 public ParameterizedTypeData(TypeData baseType, TypeData[] args = null)
     : this(baseType.getType().MakeGenericType(args.Select(arg => arg.getType()).ToArray()),
         baseType, args)
 {
 }
Beispiel #29
0
 public override void writeObject(ExportContext ctx, object value, TypeData[] typeArguments = null)
 {
     // TODO: is this correct?
     ctx.writeObject(value, this);
 }
Beispiel #30
0
 public override void writeObject(ExportContext ctx, object value, TypeData[] typeArguments = null)
 {
     ctx.writeDouble((double)value);
 }