- Main Page
- Packages
- Classes
- Files

- File List
- File Members

# D:/guillaume/guillara/3D/DXGfx/Backup/DXGfxLib/Serialization/Manager.cs

Go to the documentation of this file.

    00001 using System;
    00002 using System.IO;
    00003 using System.Collections.Generic;
    00004 using System.Text;
    00005 using System.Runtime.Serialization;
    00006 using System.Runtime.Serialization.Formatters.Soap;
    00007
    00008 namespace DXGfxLib.Serialization
    00009 {
    00010     public class Manager
    00011     {
    00012         private static Manager globalInstance = null;
    00013
    00014         public Manager()
    00015         {
    00016             if (globalInstance != null)
    00017             {
    00018                 throw new Exception("There is already one instance of the Manager class in this AppDomain. It is improper to have several!");
    00019             }
    00020             globalInstance = this;
    00021         }
    00022
    00023         public void Dispose()
    00024         {
    00025             globalInstance = null;
    00026         }
    00027
    00028         public static Manager GetGlobalInstance()
    00029         {
    00030             return globalInstance;
    00031         }
    00032
    00033         public void SaveScene(Scene scene, string fileName)
    00034         {
    00035         }
    00036
    00037         public void SaveScene(OutDoorScene scene, string fileName)
    00038         {
    00039             // 1. Construct the desired formatter
    00040             IFormatter formatter = new SoapFormatter();
    00041
    00042             // 2. Construct a SurrogateSelector object
    00043             SurrogateSelector ss = new SurrogateSelector();
    00044
    00045             // 3. Construct an instances of our serialization surrogate types
    00046             OutDoorSceneSurrogate odss = new OutDoorSceneSurrogate();
    00047             TerrainSurrogate ts = new TerrainSurrogate();
    00048             SceneObjectSurrogate sos = new SceneObjectSurrogate();
    00049             CursorSurrogate cs = new CursorSurrogate();
    00050             SkyDomeSurrogate sds = new SkyDomeSurrogate();
    00051             Water2Surrogate ws = new Water2Surrogate();
    00052
    00053             // 4. Tell the surrogate selector to use our objects
    00054             ss.AddSurrogate(typeof(OutDoorScene),
    00055                new StreamingContext(StreamingContextStates.All),
    00056                odss);
    00057             ss.AddSurrogate(typeof(Terrain),
    00058                new StreamingContext(StreamingContextStates.All),
    00059                ts);
    00060             ss.AddSurrogate(typeof(SceneObject),
    00061                new StreamingContext(StreamingContextStates.All),
    00062                sos);
    00063             ss.AddSurrogate(typeof(SkyBox),
    00064                 new StreamingContext(StreamingContextStates.All),
    00065                 sos);
    00066             ss.AddSurrogate(typeof(Water),
    00067                 new StreamingContext(StreamingContextStates.All),
    00068                 sos);
    00069             ss.AddSurrogate(typeof(MeshObject),
    00070                 new StreamingContext(StreamingContextStates.All),
    00071                 sos);
    00072             ss.AddSurrogate(typeof(Cursor),
    00073                 new StreamingContext(StreamingContextStates.All),
    00074                 cs);
    00075             ss.AddSurrogate(typeof(SkyDome),
    00076                 new StreamingContext(StreamingContextStates.All),
    00077                 sds);
    00078             ss.AddSurrogate(typeof(Water2),
    00079                 new StreamingContext(StreamingContextStates.All),
    00080                 ws);
    00081             // 5. Have the formatter use our surrogate selector
    00082             formatter.SurrogateSelector = ss;
    00083
    00084             // Serialize our outdoorscene.
    00085             formatter.Serialize(new FileStream(fileName, FileMode.Create, FileAccess.Write), scene);
    00086
    00087
    00088
    00089         }
    00090
    00091         public Scene LoadScene(string fileName)
    00092         {
    00093             return null;
    00094         }
    00095
    00096         public OutDoorScene LoadOutdoor(string fileName)
    00097         {
    00098             // 1. Construct the desired formatter
    00099             IFormatter formatter = new SoapFormatter();
    00100
    00101             // 2. Construct a SurrogateSelector object
    00102             SurrogateSelector ss = new SurrogateSelector();
    00103
    00104             // 3. Construct an instances of our serialization surrogate types
    00105             OutDoorSceneSurrogate odss = new OutDoorSceneSurrogate();
    00106             TerrainSurrogate ts = new TerrainSurrogate();
    00107             SceneObjectSurrogate sos = new SceneObjectSurrogate();
    00108             CursorSurrogate cs = new CursorSurrogate();
    00109             SkyDomeSurrogate sds = new SkyDomeSurrogate();
    00110             Water2Surrogate ws = new Water2Surrogate();
    00111
    00112             // 4. Tell the surrogate selector to use our objects
    00113             ss.AddSurrogate(typeof(OutDoorScene),
    00114                new StreamingContext(StreamingContextStates.All),
    00115                odss);
    00116             ss.AddSurrogate(typeof(Terrain),
    00117                new StreamingContext(StreamingContextStates.All),
    00118                ts);
    00119             ss.AddSurrogate(typeof(SceneObject),
    00120                new StreamingContext(StreamingContextStates.All),
    00121                sos);
    00122             ss.AddSurrogate(typeof(SkyBox),
    00123                 new StreamingContext(StreamingContextStates.All),
    00124                 sos);
    00125             ss.AddSurrogate(typeof(Water),
    00126                 new StreamingContext(StreamingContextStates.All),
    00127                 sos);
    00128             ss.AddSurrogate(typeof(MeshObject),
    00129                 new StreamingContext(StreamingContextStates.All),
    00130                 sos);
    00131             ss.AddSurrogate(typeof(Cursor),
    00132                 new StreamingContext(StreamingContextStates.All),
    00133                 cs);
    00134             ss.AddSurrogate(typeof(SkyDome),
    00135                 new StreamingContext(StreamingContextStates.All),
    00136                 sds);
    00137             ss.AddSurrogate(typeof(Water2),
    00138                 new StreamingContext(StreamingContextStates.All),
    00139                 ws);
    00140
    00141             // 5. Have the formatter use our surrogate selector
    00142             formatter.SurrogateSelector = ss;
    00143
    00144             // 6. Deserialize our outdoorscene.
    00145             FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    00146             OutDoorScene outdoorScene = (OutDoorScene)formatter.Deserialize(fStream);
    00147             fStream.Close();
    00148
    00149             return outdoorScene;
    00150         }
    00151     }
    00152 }