Пример #1
0
        /// <see cref="IMapLoader.SaveMap"/>
        public byte[] SaveMap(IMapAccess map)
        {
            if (!this.initThreadStarted)
            {
                throw new InvalidOperationException("Component has not yet been started!");
            }
            this.initThread.Join();

            if (map == null)
            {
                throw new ArgumentNullException("map");
            }

            RCPackage mapHeader      = this.CreateMapHeaderPackage(map);
            RCPackage isotileList    = this.CreateIsoTileListPackage(map);
            RCPackage terrainObjList = this.CreateTerrainObjListPackage(map);

            byte[] retArray = new byte[mapHeader.PackageLength + isotileList.PackageLength + terrainObjList.PackageLength];
            int    offset   = 0;

            offset += mapHeader.WritePackageToBuffer(retArray, offset);
            offset += isotileList.WritePackageToBuffer(retArray, offset);
            offset += terrainObjList.WritePackageToBuffer(retArray, offset);

            return(retArray);
        }
Пример #2
0
        /// <see cref="ITrace.WriteLine"/>
        public void WriteLine(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            string strToWrite = obj.ToString();

            if (strToWrite == null)
            {
                strToWrite = string.Empty;
            }

            RCPackage packageToWrite = RCPackage.CreateCustomDataPackage(RCL_EVENT_FORMAT);

            packageToWrite.WriteInt(0, RCThread.CurrentThread.WrappedThread.ManagedThreadId);
            packageToWrite.WriteString(1, RCThread.CurrentThread.Name);
            packageToWrite.WriteLong(2, timer.ElapsedMilliseconds);
            packageToWrite.WriteString(3, strToWrite);

            byte[] buffer = new byte[packageToWrite.PackageLength];
            packageToWrite.WritePackageToBuffer(buffer, 0);

            this.outputWriter.Write(buffer);
            this.outputWriter.Flush();
            this.outputStream.Flush();
        }
Пример #3
0
        /// <see cref="ITrace.WriteFork"/>
        public void WriteFork(RCThread newThread, RCThread parentThread)
        {
            if (newThread == null)
            {
                throw new ArgumentNullException("newThread");
            }
            if (parentThread == null)
            {
                throw new ArgumentNullException("parentThread");
            }

            RCPackage packageToWrite = RCPackage.CreateCustomDataPackage(RCL_FORK_FORMAT);

            packageToWrite.WriteInt(0, newThread.WrappedThread.ManagedThreadId);
            packageToWrite.WriteString(1, newThread.Name);
            packageToWrite.WriteInt(2, parentThread.WrappedThread.ManagedThreadId);
            packageToWrite.WriteString(3, parentThread.Name);
            packageToWrite.WriteLong(4, timer.ElapsedMilliseconds);

            byte[] buffer = new byte[packageToWrite.PackageLength];
            packageToWrite.WritePackageToBuffer(buffer, 0);

            this.outputWriter.Write(buffer);
            this.outputWriter.Flush();
            this.outputStream.Flush();
        }
Пример #4
0
        /// <see cref="ITrace.WriteLine"/>
        public void WriteException(Exception ex, bool isFatal)
        {
            if (ex == null)
            {
                throw new ArgumentNullException("ex");
            }

            string strToWrite = ex.ToString();

            if (strToWrite == null)
            {
                strToWrite = string.Empty;
            }

            RCPackage packageToWrite = RCPackage.CreateCustomDataPackage(RCL_EXCEPTION_FORMAT);

            packageToWrite.WriteInt(0, RCThread.CurrentThread.WrappedThread.ManagedThreadId);
            packageToWrite.WriteString(1, RCThread.CurrentThread.Name);
            packageToWrite.WriteLong(2, timer.ElapsedMilliseconds);
            packageToWrite.WriteByte(3, isFatal ? (byte)0x01 : (byte)0x00);
            packageToWrite.WriteString(4, strToWrite);

            byte[] buffer = new byte[packageToWrite.PackageLength];
            packageToWrite.WritePackageToBuffer(buffer, 0);

            this.outputWriter.Write(buffer);
            this.outputWriter.Flush();
            this.outputStream.Flush();
        }
Пример #5
0
        /// <see cref="ITrace.WriteJoin"/>
        public void WriteJoin(RCThread runningThread, RCThread waitingThread)
        {
            if (runningThread == null)
            {
                throw new ArgumentNullException("runningThread");
            }
            if (waitingThread == null)
            {
                throw new ArgumentNullException("waitingThread");
            }

            RCPackage packageToWrite = RCPackage.CreateCustomDataPackage(RCL_JOIN_FORMAT);

            packageToWrite.WriteInt(0, runningThread.WrappedThread.ManagedThreadId);
            packageToWrite.WriteString(1, runningThread.Name);
            packageToWrite.WriteInt(2, waitingThread.WrappedThread.ManagedThreadId);
            packageToWrite.WriteString(3, waitingThread.Name);
            packageToWrite.WriteLong(4, timer.ElapsedMilliseconds);

            byte[] buffer = new byte[packageToWrite.PackageLength];
            packageToWrite.WritePackageToBuffer(buffer, 0);

            this.outputWriter.Write(buffer);
            this.outputWriter.Flush();
            this.outputStream.Flush();
        }
Пример #6
0
        /// <see cref="IComponent.Start"/>
        public void Start()
        {
            this.tilesetLoader = ComponentManager.GetInterface <ITileSetLoader>();

            /// Load the tilesets from the configured directory
            DirectoryInfo rootDir = new DirectoryInfo(BizLogicConstants.TILESET_DIR);

            FileInfo[] tilesetFiles = rootDir.GetFiles("*.xml", SearchOption.AllDirectories);
            foreach (FileInfo tilesetFile in tilesetFiles)
            {
                /// TODO: this is a hack! Later we will have binary tileset format.
                string    xmlStr         = File.ReadAllText(tilesetFile.FullName);
                string    imageDir       = tilesetFile.DirectoryName;
                RCPackage tilesetPackage = RCPackage.CreateCustomDataPackage(PackageFormats.TILESET_FORMAT);
                tilesetPackage.WriteString(0, xmlStr);
                tilesetPackage.WriteString(1, imageDir);

                byte[] buffer = new byte[tilesetPackage.PackageLength];
                tilesetPackage.WritePackageToBuffer(buffer, 0);
                ITileSet tileset = this.tilesetLoader.LoadTileSet(buffer);

                if (this.loadedTilesets.ContainsKey(tileset.Name))
                {
                    throw new InvalidOperationException(string.Format("Tileset with name '{0}' already loaded!", tileset.Name));
                }

                this.loadedTilesets.Add(tileset.Name, tileset);
            }
        }
Пример #7
0
 protected override void BroadcastPackage_i(RCPackage package)
 {
     try
     {
         package.WritePackageToBuffer(this.buffer, 0);
         foreach (int port in this.wellKnownBroadcastPorts)
         {
             this.udpSocket.SendTo(buffer, NetworkingSystemConstants.ANNOUNCEMENT_UDP_POCKET_SIZE, SocketFlags.None,
                                   new IPEndPoint(IPAddress.Broadcast, port));
         }
     }
     catch (Exception ex)
     {
         TraceManager.WriteExceptionAllTrace(ex, false);
     }
 }
Пример #8
0
        /// <summary>
        /// Creates the package that contains the description of the terrain objects of the given map.
        /// </summary>
        /// <param name="map">Reference to the map.</param>
        /// <returns>The data package that contains the description of the terrain objects of the given map.</returns>
        private RCPackage CreateTerrainObjListPackage(IMapAccess map)
        {
            RCPackage terrainObjList = RCPackage.CreateCustomDataPackage(MapFileFormat.TERRAINOBJ_LIST);

            /// Create the terrain object type index table.
            List <string> terrainObjTypeList = new List <string>();
            Dictionary <ITerrainObjectType, int> terrainObjTypeIndexTable = new Dictionary <ITerrainObjectType, int>();
            int terrainObjTypeIndex = 0;

            foreach (ITerrainObjectType terrainObjType in map.Tileset.TerrainObjectTypes)
            {
                terrainObjTypeList.Add(terrainObjType.Name);
                terrainObjTypeIndexTable.Add(terrainObjType, terrainObjTypeIndex);
                terrainObjTypeIndex++;
            }
            terrainObjList.WriteStringArray(0, terrainObjTypeList.ToArray());

            /// Create the packages of the terrain objects.
            List <RCPackage> terrainObjPackages = new List <RCPackage>();
            int terrainObjInfoLength            = 0;

            foreach (ITerrainObject terrainObj in map.TerrainObjects)
            {
                RCPackage terrainObjPackage = RCPackage.CreateCustomDataPackage(MapFileFormat.TERRAINOBJ);
                terrainObjPackage.WriteShort(0, (short)terrainObj.MapCoords.X);
                terrainObjPackage.WriteShort(1, (short)terrainObj.MapCoords.Y);
                terrainObjPackage.WriteByte(2, (byte)terrainObjTypeIndexTable[terrainObj.Type]);

                terrainObjPackages.Add(terrainObjPackage);
                terrainObjInfoLength += terrainObjPackage.PackageLength;
            }

            /// Write the terrain object packages into the final package
            byte[] terrainObjInfoBytes = new byte[terrainObjInfoLength];
            int    offset = 0;

            foreach (RCPackage terrainObjPackage in terrainObjPackages)
            {
                offset += terrainObjPackage.WritePackageToBuffer(terrainObjInfoBytes, offset);
            }

            terrainObjList.WriteByteArray(1, terrainObjInfoBytes);
            return(terrainObjList);
        }
Пример #9
0
        /// <summary>
        /// Internal function to send a package to a network stream.
        /// </summary>
        private bool SendPackageToStream_i(RCPackage packageToSend, NetworkStream toWhichStream)
        {
            /// Extend the buffer if necessary.
            if (packageToSend.PackageLength > this.outBuffer.Length)
            {
                this.outBuffer = new byte[packageToSend.PackageLength];
            }
            int writtenBytes = packageToSend.WritePackageToBuffer(this.outBuffer, 0);

            try
            {
                toWhichStream.Write(this.outBuffer, 0, writtenBytes);
                return(true);
            }
            catch (Exception ex)
            {
                TraceManager.WriteExceptionAllTrace(ex, false);
            }
            return(false);
        }
Пример #10
0
        private void Form1_Load(object sender, EventArgs e)
        {
            ComponentManager.RegisterComponents("RC.Engine.Maps, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
                                                new string[3] {
                "RC.Engine.Maps.TileSetLoader", "RC.Engine.Maps.MapLoader", "RC.Engine.Maps.MapEditor"
            });
            ComponentManager.StartComponents();

            this.tilesetLoader = ComponentManager.GetInterface <ITileSetLoader>();
            this.mapLoader     = ComponentManager.GetInterface <IMapLoader>();
            this.mapEditor     = ComponentManager.GetInterface <IMapEditor>();

            /// TODO: this is a hack!
            FileInfo  tilesetFile    = new FileInfo("../../../../tilesets/test/test.xml");
            string    xmlStr         = File.ReadAllText(tilesetFile.FullName);
            string    imageDir       = tilesetFile.DirectoryName;
            RCPackage tilesetPackage = RCPackage.CreateCustomDataPackage(PackageFormats.TILESET_FORMAT);

            tilesetPackage.WriteString(0, xmlStr);
            tilesetPackage.WriteString(1, imageDir);

            byte[] buffer = new byte[tilesetPackage.PackageLength];
            tilesetPackage.WritePackageToBuffer(buffer, 0);
            ITileSet tileset = this.tilesetLoader.LoadTileSet(buffer);

            this.map = this.mapLoader.NewMap("TestMap", tileset, "Yellow", new RCIntVector(64, 32));

            this.draw = new IsoDraw();

            this.replacedTiles = new RCSet <IIsoTile>();
            this.terrainTypes  = new List <string>();
            foreach (ITerrainType terrainType in tileset.TerrainTypes)
            {
                this.terrainTypes.Add(terrainType.Name);
            }
            this.selectedTerrain = 0;
            this.Text            = this.terrainTypes[this.selectedTerrain];
        }
Пример #11
0
        static void Main(string[] args)
        {
            ConfigurationManager.Initialize("../../../../config/RC.NetworkingSystem.TestConsole/RC.NetworkingSystem.TestConsole.root");

            RCPackageFormat f0 = new RCPackageFormat();
            RCPackageFormat f1 = new RCPackageFormat();
            RCPackageFormat f2 = new RCPackageFormat();

            f0.DefineField(RCPackageFieldType.SHORT);
            f0.DefineField(RCPackageFieldType.SHORT_ARRAY);
            f0.DefineField(RCPackageFieldType.STRING_ARRAY);
            f0.DefineField(RCPackageFieldType.STRING);
            f0.DefineField(RCPackageFieldType.INT);
            f0.DefineField(RCPackageFieldType.INT_ARRAY);

            f1.DefineField(RCPackageFieldType.LONG);
            f1.DefineField(RCPackageFieldType.LONG_ARRAY);
            f1.DefineField(RCPackageFieldType.BYTE_ARRAY);
            f1.DefineField(RCPackageFieldType.BYTE);
            f1.DefineField(RCPackageFieldType.STRING);

            f2.DefineField(RCPackageFieldType.BYTE);
            f2.DefineField(RCPackageFieldType.BYTE_ARRAY);
            f2.DefineField(RCPackageFieldType.LONG_ARRAY);
            f2.DefineField(RCPackageFieldType.LONG);
            f2.DefineField(RCPackageFieldType.INT);
            f2.DefineField(RCPackageFieldType.STRING_ARRAY);

            RCPackageFormat.RegisterFormat(f0, 65000);
            RCPackageFormat.RegisterFormat(f1, 65001);
            RCPackageFormat.RegisterFormat(f2, 65002);

            /// WRITING
            TextWriter writer = new StreamWriter("write.txt");

            byte[] buffer = new byte[10000000];
            int    pos    = 0;

            for (int i = 0; i < 10000; i++)
            {
                RCPackage newPackage = GenerateRandomPackage();
                TraceManager.WriteAllTrace(string.Format("{0}: {1}", i, newPackage.ToString()), TraceManager.GetTraceFilterID("RC.NetworkingSystem.TestConsole.Info"));
                writer.WriteLine(i + ": " + newPackage.ToString());
                pos += newPackage.WritePackageToBuffer(buffer, pos);
            }
            writer.Close();
            /// WRITING BINARY DATA0
            FileStream binStr = new FileStream("bin0.txt", FileMode.Create);

            binStr.Write(buffer, 0, pos);
            binStr.Close();

            /// READING
            pos = 0;
            int       parsedBytes = 0;
            RCPackage currPackage = null;
            int       count       = 0;

            writer = new StreamWriter("read.txt");
            byte[] buffer2 = new byte[10000000];
            int    pos2    = 0;

            while (true)
            {
                int burst = rnd.Next(1, 50);
                pos        += parsedBytes;
                currPackage = RCPackage.Parse(buffer, pos, burst, out parsedBytes);
                if (currPackage != null)
                {
                    bool error = false;
                    while (!currPackage.IsCommitted)
                    {
                        pos  += parsedBytes;
                        burst = rnd.Next(1, 50);
                        if (!currPackage.ContinueParse(buffer, pos, burst, out parsedBytes))
                        {
                            TraceManager.WriteAllTrace("Syntax error", TraceManager.GetTraceFilterID("RC.NetworkingSystem.TestConsole.Info"));
                            writer.WriteLine("Syntax error");
                            error = true;
                        }
                    }
                    if (!error)
                    {
                        TraceManager.WriteAllTrace(string.Format("{0}: {1}", count, currPackage.ToString()), TraceManager.GetTraceFilterID("RC.NetworkingSystem.TestConsole.Info"));
                        writer.WriteLine(count + ": " + currPackage.ToString());
                        pos2 += currPackage.WritePackageToBuffer(buffer2, pos2);
                        count++;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    TraceManager.WriteAllTrace("Syntax error", TraceManager.GetTraceFilterID("RC.NetworkingSystem.TestConsole.Info"));
                    writer.WriteLine("Syntax error");
                    break;
                }
            }
            writer.Close();

            /// WRITING BINARY DATA1
            binStr = new FileStream("bin1.txt", FileMode.Create);
            binStr.Write(buffer2, 0, pos2);
            binStr.Close();

            TraceManager.WriteAllTrace("READY", TraceManager.GetTraceFilterID("RC.NetworkingSystem.TestConsole.Info"));
            Console.ReadLine();
        }