Last active
January 14, 2019 17:26
-
-
Save hyakugei/323ee1ca2daf5af3f00173d67d33e375 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public abstract class OptionsBase | |
{ | |
public abstract ushort typeId { get; } | |
public abstract void Serialize(BinaryWriter writer); | |
public abstract void Deserialize(BinaryReader reader); | |
public abstract bool Deserialize(); | |
} | |
public class OptionA : OptionsBase | |
{ | |
public override ushort typeId | |
{ | |
get | |
{ | |
return 1; | |
} | |
} | |
public string data = ""; | |
public override void Serialize(BinaryWriter writer) | |
{ | |
writer.Write(data); | |
} | |
public override void Deserialize(BinaryReader reader) | |
{ | |
data = reader.ReadString(); | |
Debug.Log("-- data: " + data); | |
} | |
public override string ToString() | |
{ | |
return "-data " + data; | |
} | |
public override bool Deserialize() | |
{ | |
// if you can re-create the instance from the cmdLineArgs | |
// do so here. | |
// List<string> args; | |
// if(InsightArgs.TryGetArgument("data", out args)) | |
// { | |
// this.data = args[0]; // | |
// return true; | |
// } | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class OptionsCollection | |
{ | |
Dictionary<System.Type, OptionsBase> options = new Dictionary<System.Type, OptionsBase>(); | |
Dictionary<ushort, System.Type> types = new Dictionary<ushort, System.Type>(); | |
public void RegisterType(ushort id, System.Type type) | |
{ | |
types[id] = type; | |
} | |
public void Add(OptionsBase option) | |
{ | |
RegisterType(option.typeId, option.GetType()); | |
options[option.GetType()] = option; | |
} | |
public T Get<T>() where T:OptionsBase | |
{ | |
if (options.ContainsKey(typeof(T))) return options[typeof(T)] as T; | |
return null; | |
} | |
public void Serialize(BinaryWriter writer) | |
{ | |
writer.Write(options.Count); | |
foreach (var item in options) | |
{ | |
writer.Write(item.Value.typeId); | |
item.Value.Serialize(writer); | |
} | |
} | |
public void Deserialize(BinaryReader reader) | |
{ | |
options.Clear(); | |
int numberOfOptions = reader.ReadInt32(); | |
ushort typeId = 0; | |
for(int i = 0; i < numberOfOptions; i++) | |
{ | |
typeId = reader.ReadUInt16(); | |
var inst = System.Activator.CreateInstance(types[typeId]) as OptionsBase; | |
inst.Deserialize(reader); | |
options[inst.GetType()] = inst; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class OptionsTester : MonoBehaviour { | |
// Use this for initialization | |
void Start () { | |
var optionsCollection = new OptionsCollection(); | |
var option = new OptionA(); | |
option.data = "Yeah! Worked suckers!"; | |
optionsCollection.RegisterType(option.typeId, option.GetType()); | |
optionsCollection.Add(option); | |
byte[] buffer = new byte[2048]; | |
var ms = new MemoryStream(buffer); | |
var writer = new BinaryWriter(ms); | |
optionsCollection.Serialize(writer); | |
writer.Flush(); | |
ms = new MemoryStream(buffer); | |
var reader = new BinaryReader(ms); | |
optionsCollection.Deserialize(reader); | |
var o = optionsCollection.Get<OptionA>(); | |
Debug.Log("o -- " + o.data, this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment