Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Final answer:</p> <p>Alright, so, A few days ago I settled on a solution and I'm just updating this in case anyone else runs into the same issue.</p> <p>The whole problem stemmed from the fact that I didn't realize protobuf-net could support byte[]. So, here's my solution:</p> <pre><code>namespace Kannon.State { /// &lt;summary&gt; /// ReferenceDefinition describes the layout of the reference in general. /// It tells what states it should have, and stores the stream buffers for later serialization. /// &lt;/summary&gt; [ProtoBuf.ProtoContract] public class ReferenceDefinition { /// &lt;summary&gt; /// There are several built in state types, as well as rudimentary support for a "Generic" state. /// &lt;/summary&gt; public enum StateType { Graphics=0, Audio, Mind, Physics, Network, Generic } /// &lt;summary&gt; /// Represents what states should be present in the ReferenceDefinition /// &lt;/summary&gt; [ProtoBuf.ProtoMember(1)] List&lt;StateType&gt; m_StatesPresent = new List&lt;StateType&gt;(); /// &lt;summary&gt; /// Represent a list of StateDefinitions, which hold the buffers for each different type of state. /// &lt;/summary&gt; [ProtoBuf.ProtoMember(2)] List&lt;StateDefinition&gt; m_StateDefinition = new List&lt;StateDefinition&gt;(); /// &lt;summary&gt; /// Add a state, mapped to a type, to this reference definition. /// &lt;/summary&gt; /// &lt;param name="type"&gt;Type of state to add&lt;/param&gt; /// &lt;param name="def"&gt;State definition to add.&lt;/param&gt; public void AddState(StateType type, StateDefinition def) { // Enforce only 1 of each type, except for Generic, which can have as many as it wants. if (m_StatesPresent.Contains(type) &amp;&amp; type != StateType.Generic) return; m_StatesPresent.Add(type); m_StateDefinition.Add(def); } } /// &lt;summary&gt; /// Represents a definition of some gamestate, storing protobuffered data to be remapped to the state. /// &lt;/summary&gt; [ProtoBuf.ProtoContract] public class StateDefinition { /// &lt;summary&gt; /// Name of the state /// &lt;/summary&gt; [ProtoBuf.ProtoMember(1)] string m_StateName; /// &lt;summary&gt; /// Byte array to store the "data" for later serialization. /// &lt;/summary&gt; [ProtoBuf.ProtoMember(2)] byte[] m_Buffer; /// &lt;summary&gt; /// Constructor for the state definition, protected to enforce the Pack and Unpack functionality to keep things safe. /// &lt;/summary&gt; /// &lt;param name="name"&gt;Name of the state type.&lt;/param&gt; /// &lt;param name="buff"&gt;byte buffer to build state off of&lt;/param&gt; protected StateDefinition(String name, byte[] buff) { m_StateName = name; m_Buffer = buff; } /// &lt;summary&gt; /// Unpack a StateDefinition into a GameState /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;Gamestate type to unpack into. Must define Protobuf Contracts.&lt;/typeparam&gt; /// &lt;param name="def"&gt;State Definition to unpack.&lt;/param&gt; /// &lt;returns&gt;The unpacked state data.&lt;/returns&gt; public static T Unpack&lt;T&gt;(StateDefinition def) where T:GameState { // Make sure we're unpacking into the right state type. if (typeof(T).Name == def.m_StateName) return ProtoBuf.Serializer.Deserialize&lt;T&gt;(new MemoryStream(def.m_Buffer)); else // Otherwise, return the equivalent of Null. return default(T); } /// &lt;summary&gt; /// Pack a state type into a State Definition /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;Gamestate to package up. Upst define protobuf contracts.&lt;/typeparam&gt; /// &lt;param name="state"&gt;State to pack up.&lt;/param&gt; /// &lt;returns&gt;A state definition serialized from the passed in state.&lt;/returns&gt; public static StateDefinition Pack&lt;T&gt;(T state) where T:GameState { // Using a memory stream, to make sure Garbage Collection knows what's going on. using (MemoryStream s = new MemoryStream()) { ProtoBuf.Serializer.Serialize&lt;T&gt;(s, state); // Uses typeof(T).Name to do semi-enforcement of type safety. Not the best, but it works. return new StateDefinition(typeof(T).Name, s.ToArray()); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload