RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Resource icon

[2.x] Static serialization 1.0

No permission to download

_Epila_

Sorceror
_Epila_ submitted a new resource:

Static serialization (version 1.0) - Static serialization

I've seen some posts of you guys asking how to serialize things to playermobile, store pvp/pvm/etc points or any other data that is not-really a game aspect.

So what Static Serialization (SS) is?
Sometimes I am very conservative about how to do some things and try to not modify the original code even further the change does not interact directly with the gameplay and/or you may want to remove in a near future
e.g.: You want to serialize PvP points, which does not affect the gameplay, it is...

Read more about this resource...
 

Arrrr

Wanderer
Hi, i find this solution very handy, just two questions:
- It is not inefficient to scan classes everytime you save the data? Could this information be stored on initialization?
- I have not much experience with c#, so i ask you: Could this be changed to scan for classes implementing an interface? So is safer than just scan for a name (though it has an uncommon name)
 

Arrrr

Wanderer
After some testing i found it increased saves time from 0.5 up to 2 seconds. I made some changes to your script (In fact i no longer recognize it) so it now resembles more how runUO handles these actions. The two main differences are data is now loaded on ServerStarted and you have to explicitly register those events (not as comfortable as your script, but i like to make those actions clear):

Code:
using System;
using System.Collections.Generic;
 
namespace Server.Engines.CustomDataHandler
{
    public class CustomDataHandler
    {
        private static string Base { get { return "Saves/CustomData/"; } }
        private static string Binary { get { return Path.Combine( Base, "binary.bin"); } }
 
        public static event StoreCustom StoreCustomHandler = null;
        public static event LoadCustom LoadCustomHandler = null;
 
        public static void Configure()
        {
            EventSink.WorldSave += new WorldSaveEventHandler( EventSink_WorldSave );
            EventSink.ServerStarted += new ServerStartedEventHandler( EventSink_ServerStarted );
        }
 
        public delegate void StoreCustom(GenericWriter w);
        public delegate void LoadCustom(GenericReader r);
 
        public static void Register(StoreCustom sc, LoadCustom lc)
        {
            StoreCustomHandler += sc;
            LoadCustomHandler += lc;
        }
 
        private static void EventSink_WorldSave( WorldSaveEventArgs e )
        {
            if (!Directory.Exists( Base ))
                Directory.CreateDirectory( Base );
 
            GenericWriter writer = new BinaryFileWriter( Binary, true );
 
            Console.WriteLine("CustomDataHandler: Saving custom data in {0}", Binary);
            StoreCustomHandler( writer );
 
            writer.Close();
        }
 
        private static void EventSink_ServerStarted()
        {
            if ( !Directory.Exists( Base ) )
                Directory.CreateDirectory( Base );
 
            if ( File.Exists( Binary ) )
            {
                using ( FileStream bin = new FileStream( Binary, FileMode.Open, FileAccess.Read, FileShare.Read ) )
                {
                    BinaryFileReader reader = new BinaryFileReader( new BinaryReader( bin ) );
                    Console.WriteLine("CustomDataHandler: Loading custom data saved ({0} Bytes) from {1}", bin.Length, Binary);
                    LoadCustomHandler( reader );
 
                    reader.Close();
                }
            }
        }
    }
}

EDIT: Missed to include some checks
Code:
private static void EventSink_WorldSave( WorldSaveEventArgs e )
{
    if (StoreCustomHandler == null)
        return;
[...]
private static void EventSink_ServerStarted()
{
    if (LoadCustomHandler == null)
        return;
 

Attachments

  • CustomDataHandler.cs
    2.1 KB · Views: 0

_Epila_

Sorceror
For sure my method wasn't the best, it is a copy-paste from the RunUO source, so things were not optimized to be used in another context (SS). My only objective was to release a safe way to serialize things without the need to change the original serialization for PlayerMobile/items/etc and avoid losing data. Unfortunately, making things easier require to consume more resources, that's why the save time increased :p

Your changes are perfectly acceptable, and I can say it is better than mine, even having to register the serialization process.
But I see it does work like a example-class, it does not allow you to have multiple serialization targets, it does not indexes the data that is being serialized, how it will know if the data is being loaded to the correct class?
 

phpjunkie

Traveler
RunUO already has everything you need to do all kinds of serialization. Plus you can get creative about hiding items by using custom packets so only one person on the server can see the item.
 
Last edited:

phpjunkie

Traveler
Here is some example code of a stone that only you will see if modified right. Everyone else will see a no draw tile. This can be set up as a game stone for one or more games, like bombing run and/or capture the flag.

What I'm trying to do is show that runuo already has everything it needs to do serialization with.
 

Attachments

  • CustomStone.cs
    2.5 KB · Views: 3
  • CustomSerializer.cs
    594 bytes · Views: 1
Top