I am trying to deserialize a "Type" value from a serialized class, but in the core i have there are no Type for GenericReader.
Only these:
Code:
public abstract string ReadString();
public abstract DateTime ReadDateTime();
public abstract TimeSpan ReadTimeSpan();
public abstract DateTime ReadDeltaTime();
public abstract decimal ReadDecimal();
public abstract long ReadLong();
public abstract ulong ReadULong();
public abstract int ReadInt();
public abstract uint ReadUInt();
public abstract short ReadShort();
public abstract ushort ReadUShort();
public abstract double ReadDouble();
public abstract float ReadFloat();
public abstract char ReadChar();
public abstract byte ReadByte();
public abstract sbyte ReadSByte();
public abstract bool ReadBool();
public abstract int ReadEncodedInt();
public abstract IPAddress ReadIPAddress();
public abstract Point3D ReadPoint3D();
public abstract Point2D ReadPoint2D();
public abstract Rectangle2D ReadRect2D();
public abstract Rectangle3D ReadRect3D();
public abstract Map ReadMap();
public abstract Item ReadItem();
public abstract Mobile ReadMobile();
public abstract BaseGuild ReadGuild();
public abstract T ReadItem<T>() where T : Item;
public abstract T ReadMobile<T>() where T : Mobile;
public abstract T ReadGuild<T>() where T : BaseGuild;
public abstract ArrayList ReadItemList();
public abstract ArrayList ReadMobileList();
public abstract ArrayList ReadGuildList();
public abstract List<Item> ReadStrongItemList();
public abstract List<T> ReadStrongItemList<T>() where T : Item;
public abstract List<Mobile> ReadStrongMobileList();
public abstract List<T> ReadStrongMobileList<T>() where T : Mobile;
public abstract List<BaseGuild> ReadStrongGuildList();
public abstract List<T> ReadStrongGuildList<T>() where T : BaseGuild;
public abstract Race ReadRace();
public abstract bool End();

INFOS:
-class Stuff:
contains some stuffs, including a Type variable.

-listOfStuffs:
a List<Stuff>.

-Main class:
contains the list that contains stuffs, in it i serialize all the list, and deserialize all the list.

-To serialize my classes i do:
Code:
writer.Write( listOfStuffs.Get(i) );
in a for loop.

-To gather it back, i did this:
a for loop going from 0 to X, each time it finds a thing to read, it does:
Code:
Stuff s = new Stuff( reader );
listOfStuffs.Add( s );

-Which means i have a constructor in the class Stuff like so:
Code:
public Stuff( GenericReader reader )
{
	variableType = (Type)reader.ReadEncodedInt(); //wrong... no idea what i can use.
}

But the code above is wrong, as i don't have the appropriate way to obtain that desired Type.
At first i considered a string variable so i can easily save it, but things got complicated and well, yea.

Thanks.
 
don't know if this has anything to do with what you're asking, but taken from Talow's Mini Carousel

protected List<CarouselDoll> Dolls;

//Serialize

writer.Write( (int)Dolls.Count );
foreach( CarouselDoll doll in Dolls ) {
writer.Write( (Item)doll );
}

//deser

if ( Dolls == null ) {
Dolls = new List<CarouselDoll>();
}
int counter = reader.ReadInt();
for( int i = 0; i < counter; i++ ) {
CarouselDoll doll = (CarouselDoll)reader.ReadItem();
Dolls.Add(doll);
}
 
IF using VNc;

Types:
C#:
Type type = typeof(Longsword);

writer.WriteType( type );
C#:
Type type = reader.ReadType( );

Custom collections:
C#:
List<Type> types = new List<Type>( ) { typeof(Longsword), typeof(Bandage) };

writer.WriteList( types, (w, t) => w.WriteType( t ) ); // w is GenericWriter, t is Type
C#:
List<Type> types = reader.ReadList( r => r.ReadType( ) ); // r is GenericReader

WriteType supports any object, not just Type - it will defer the type from the provided object.
ReadType always returns a Type.

As with WriteList and ReadList, there is also WriteArray/ReadArray and WriteDictionary/ReadDictionary.

There are actually lots of additional serialization methods provided by VNc extension methods: Extensions/Server/SerializeExt.cs (SerializeExtUtility) - things such as WriteAccount/ReadAccount for IAccount persistence and WriteFlag/ReadFlag for enum persistence (without having to track and convert the enum value to int, etc).

In the context of OP; the class 'Stuff' can inherit the VNc class "PropertyObject", which is a base class for providing serializable custom object - the foundation of all service and module options, as well as many other generic usages, such as profiles that store data for players.

C#:
public class Stuff : PropertyObject
{
     [CommandProperty( AccessLevel.GameMaster )]
     public string HelloWorld { get; set; }

     public Stuff( )
     {
          HelloWorld = "Hello World!";
     }

     public Stuff( GenericReader reader ) : base( reader )
     { }

     public override void Reset( )
     { }

     public override void Clear( )
     { }

     public override void Serialize( GenericWriter writer )
     {
          base.Serialize( writer );

          writer.SetVersion( 0 );

          writer.Write( HelloWorld );
     }

     public override void Deserialize( GenericReader reader )
     {
          base.Deserialize( reader );

          var version = reader.GetVersion( );

          HelloWorld = reader.ReadString( );
     }
}
C#:
// Property declaration
[CommandProperty( AccessLevel.GameMaster )]
public Stuff MyStuff { get; set; }

// Constructor default (make sure it's not null when the containing class is constructed)
MyStuff = new MyStuff( );

// Serialize
MyStuff.Serialize( writer );

// Deserialize
MyStuff = new Stuff( reader );


Type serialization WITHOUT VNc:

C#:
Type type = typeof(Longsword);

writer.Write( type.FullName );
C#:
string typeName = reader.ReadString( );

Type type = Type.GetType( typeName, false ) ?? ScriptCompiler.FindTypeByFullName( typeName );
(The ?? operator is used to check for null and assign one side of the operator to the value)
 
Last edited:
Now i'm planning on taking the GetType from VNC and putting it directly in my core, would it require further edits anywhere else in the core?
 
No more edits other than to Serialization.cs should be needed to implement new members for the Generic[Writer/Reader] class; if you use the VNc code directly, all you need to do is remove the "this GenericWriter writer" and "this GenerciReader reader" arguments from the method signature and replace all instances of "writer" and "reader" with "this". :)
 
Just a headsup, i've found another solution to this:

serialize:
writer.Write(typestuff.ToString());

deserialize.
Type.GetType("string");

So far i didn't save items, mobiles, etc.
So can't really tell if it works number one.
Modifying the core would need a recompiling, and i lost my old core files so it would wipe out alot of progress xD
 
Type.GetType() may not always find the type you are looking for, depending on the assembly that the type exists in, this is why we need to use ScriptCompiler.FindTypeByFullName()

C#:
writer.Write( type.FullName );

C#:
string typeName = reader.ReadString( );
Type type = Type.GetType( typeName, false ) ?? ScriptCompiler.GetTypeByFullName( typeName );

Notes;
1. Type.GetType's second argument should be false to ensure it doesn't throw an exception if the type isn't found.
2. Using ?? is the same as doing this:
C#:
Type type = Type.GetType( typeName, false );

if( type == null )
{
    type = ScriptCompiler.FindTypeByFullName( typeName );
}
 
I was going to ask for some more help but i noticed you suggested "ScriptCompiler.FindTypeByFullName", i've been struggling for hours until i noticed i needed "ScriptCompiler.FindTypeByName". xD
Thanks for your help :p
 
Now i've got another thing i'm not sure about, i have a Type, and i want to know if this Type inherits from Item or Mobile, like:
if (type is Item)
but that always gives false.

i've got this too:
object o = Activator.CreateInstance( type );
then if (o is Item) etc.

But sadly, this creates an instance of it ingame and it will be nowhere.
I just feel like this would not be the right workaround for that.



EDIT: nevermind, got it.
if ( typeof( Sextant ).IsSubclassOf( typeof( Item ) ) )
 
Last edited:
Take a look at the source code for the various ways to compare Types, there may be more variables in future implementations you want to make, such as filtering child types;

http://core.vita-nex.com/svn/Extensions/System/TypeExt.cs

C#:
var type = typeof(Sextant);

if( type.IsEqual<Item>( ) ) // false
{
}

if( type.IsChildOf<Item>( ) ) // true
{
}

if( type.IsEqualOrChildOf<Item>( ) ) // true
{
}
 
Back