Beispiel #1
0
        public static void LoadMemoryMap()
        {
            if (_memoryMap != null && _mapViewSteam != null)
            {
                return;
            }

            if (string.IsNullOrEmpty(TerrainFilePath))
            {
                SetTerrainDataPath();
            }

            string memoryMapFile = Path.Combine(TerrainFilePath, TERRAIN_FILE_NAME);

            if (!System.IO.File.Exists(memoryMapFile))
            {
                memoryMapFile = "C:\\terrain.bin"; //hack for unit tests!
            }

            try
            {
                GameManager.Instance.Log.LogDebug("TerrainReader->LoadMemoryMap initializing. Reading terrain file: <" + memoryMapFile + "> .");
                _memoryMap    = MemoryMappedFile.Create(memoryMapFile, MapProtection.PageReadOnly);
                _mapViewSteam = _memoryMap.MapAsStream();
            }
            catch (Exception ex)
            {
                GameManager.Instance.Log.LogError("TerrainReader->LoadMemoryMap failed to load terrain from <" + memoryMapFile + "> ." + ex.Message);
                GameManager.Instance.Log.LogError(ex.ToString());
            }
        }
Beispiel #2
0
            public void RemoveObject(string ObjName)
            {
                //get the main mmf table and remove the key
                MemoryMappedFile map = new MemoryMappedFile();

                if (map.OpenEx(ObjectNamesMMF + ".nat", MapProtection.PageReadWrite, ObjectNamesMMF, MapAccess.FileMapAllAccess))
                {
                    BinaryFormatter bf        = new BinaryFormatter();
                    MapViewStream   mmfStream = map.MapView(MapAccess.FileMapRead, 0, 0, "");
                    mmfStream.Position = 0;
                    Hashtable oFilesMap = bf.Deserialize(mmfStream) as Hashtable;
                    oFilesMap.Remove(ObjName);
                    mmfStream.Close();
                    //update the main file
                    bf = new BinaryFormatter();
                    MemoryStream ms = new MemoryStream();
                    bf.Serialize(ms, oFilesMap);
                    MapViewStream stream = map.MapView(MapAccess.FileMapAllAccess, 0, (int)0, "");
                    stream.Position = 0;
                    stream.Write(ms.GetBuffer(), 0, (int)ms.Length);
                    stream.Flush();
                    stream.Close();
                    //delete the map of the object
                    MemoryMappedFile oMMf = new MemoryMappedFile();
                    if (oMMf.Open(MapAccess.FileMapAllAccess, ObjName))
                    {
                        oMMf.Close();
                        oMMf.Dispose();
                    }
                    if (System.IO.File.Exists(map.GetMMFDir() + ObjName + ".nat"))
                    {
                        System.IO.File.Delete(map.GetMMFDir() + ObjName + ".nat");
                    }
                }
            }
 /// <summary>
 ///
 /// </summary>
 public virtual void Open()
 {
     if (m_IsOpen)
     {
         Close();
     }
     //
     m_File   = MemoryMappedFile.Create(null /*m_Address*/, MapProtection.PageReadWrite, m_MaxSize, m_Address);
     m_Stream = m_File.MapView(MapAccess.FileMapAllAccess, 0 /*m_Offset*/, m_MaxSize /*m_Count*/) as MapViewStream;
     //
     m_IsOpen = true;
 }
 public static void Initialize()
 {
     try
     {
         mappedFile = MemoryMappedFile.Open(MapAccess.FileMapAllAccess, "Local\\BROKENGEKI_SHARED_BUFFER");
         NekoClient.Logging.Log.Info("Brokengeki shared memory exists, opening...");
     }
     catch (FileMapIOException)
     {
         NekoClient.Logging.Log.Info("Brokengeki shared memory does not exist, creating...");
         mappedFile = MemoryMappedFile.Create(MapProtection.PageReadWrite, 256, "Local\\BROKENGEKI_SHARED_BUFFER");
     }
     mapViewStream = (MapViewStream)mappedFile.MapView(MapAccess.FileMapAllAccess, 0, 256);
 }
 /// <summary>
 ///
 /// </summary>
 public virtual void Close()
 {
     if (!m_IsOpen)
     {
         return;
     }
     //
     m_File.Close();
     m_Stream.Close();
     //
     m_File   = null;
     m_Stream = null;
     //
     m_IsOpen = false;
 }
Beispiel #6
0
            /// <summary>
            /// create a MMF and serialize the object in to.
            /// </summary>
            /// <param name="InObject"></param>
            /// <param name="obectName"></param>
            /// <param name="ObjectSize"></param>
            private void WriteObjectToMMF(object InObject, string obectName, int ObjectSize)
            {
                MemoryStream    ms = new MemoryStream();
                BinaryFormatter bf = new BinaryFormatter();

                bf.Serialize(ms, InObject);

                oMutex.WaitOne();
                MemoryMappedFile map    = new MemoryMappedFile(obectName + ".nat", MapProtection.PageReadWrite, MapAccess.FileMapAllAccess, ms.Length, obectName);
                MapViewStream    stream = map.MapView(MapAccess.FileMapAllAccess, 0, (int)ms.Length, "");

                stream.Write(ms.GetBuffer(), 0, (int)ms.Length);
                stream.Flush();
                stream.Close();
                oMutex.ReleaseMutex();
            }
Beispiel #7
0
 public static void CloseMemoryMap()
 {
     try
     {
         if (_mapViewSteam != null)
         {
             _mapViewSteam.Close();
             _mapViewSteam = null;
         }
         if (_memoryMap != null)
         {
             _memoryMap.Close();
             _memoryMap = null;
         }
     }
     catch (Exception)
     {
         //ignore
     }
 }
Beispiel #8
0
            /// <summary>
            /// this function will
            /// 1) open exsisting (if not create) MMF that hold all the MMf names for  each object
            /// 2) look if aname allready exist
            /// 3) if exist
            ///			-Delete the MMF
            ///			-create new MMF
            ///			-Enter the onject into
            ///		if not
            ///			-create new MMF
            ///			-Enter the onject into
            ///			-enter the new name and MMF name into MMF of object and MMF name
            /// </summary>
            /// <param name="objName"></param>
            /// <param name="inObject"></param>
            public void AddObject(string objName, object inObject, bool UpdateDomain)
            {
                MemoryMappedFile map = new MemoryMappedFile();

                System.Collections.Hashtable oFilesMap;
                System.IntPtr oAtom  = System.IntPtr.Zero;
                string        strIps = "";

                try
                {
                    if (!map.OpenEx(ObjectNamesMMF + ".nat", MapProtection.PageReadWrite, ObjectNamesMMF, MapAccess.FileMapAllAccess))
                    {
                        //Create MMF for the object and serialize it
                        WriteObjectToMMF(inObject, objName, 0);
                        //create hashtable
                        oFilesMap = new System.Collections.Hashtable();
                        //add object name and mmf name to hash
                        oFilesMap.Add(objName, objName);
                        //create main MMF
                        WriteObjectToMMF(oFilesMap, ObjectNamesMMF, 0);
                    }
                    else
                    {
                        BinaryFormatter bf        = new BinaryFormatter();
                        Stream          mmfStream = map.MapView(MapAccess.FileMapRead, 0, 0, "");
                        mmfStream.Position = 0;
                        oFilesMap          = bf.Deserialize(mmfStream) as Hashtable;
                        long StartPosition = mmfStream.Position;

                        if (oFilesMap.ContainsKey(objName))
                        {
                            //name exist so we need to
                            //	open the MMF of the existing and update it
                            MemoryMappedFile MemberMap = new MemoryMappedFile();
                            oMutex.WaitOne();
                            MemberMap.OpenEx(objName + ".nat", MapProtection.PageReadWrite, objName, MapAccess.FileMapAllAccess);   //(MapAccess.FileMapAllAccess ,objName);
                            MapViewStream stream = MemberMap.MapView(MapAccess.FileMapAllAccess, 0, (int)0, "");
                            bf = new BinaryFormatter();
                            MemoryStream ms = new MemoryStream();
                            bf.Serialize(ms, inObject);
                            stream.Position = 0;
                            stream.Write(ms.GetBuffer(), 0, (int)ms.Length);
                            stream.Flush();
                            stream.Close();
                            oMutex.ReleaseMutex();
                        }
                        else
                        {
                            //name not apear so we nedd to
                            //	craete new MMF file and serialize
                            WriteObjectToMMF(inObject, objName, 0);
                            oMutex.WaitOne();
                            MapViewStream stream = map.MapView(MapAccess.FileMapAllAccess, 0, (int)0, "");
                            // update the main HashTable
                            oFilesMap.Add(objName, objName);
                            // serialize new Hash
                            bf = new BinaryFormatter();
                            MemoryStream ms = new MemoryStream();
                            bf.Serialize(ms, oFilesMap);
                            stream.Position = 0;
                            stream.Write(ms.GetBuffer(), 0, (int)ms.Length);
                            stream.Flush();
                            stream.Close();
                            oMutex.ReleaseMutex();
                        }
                    }
                }

                catch (Exception e)
                {
                    throw new Exception("Cannot Open File " + objName, e);
                }
                finally
                {
                    Win32MapApis.GlobalDeleteAtom(oAtom);
                }
            }