コード例 #1
0
        /// <summary>
        /// Build the model of the specified type.
        /// </summary>
        /// <param name="model">The model to build.</param>
        /// <param name="output">The output type.</param>
        public void Build(Model model, string output)
        {
            if (string.IsNullOrEmpty(output))
            {
                output = @"CSharp";
            }

            var networkDirectory = Path.Combine(output, "Spike", "Network");

            if (!Directory.Exists(networkDirectory))
            {
                Directory.CreateDirectory(networkDirectory);
            }

            var packetsDirectory = Path.Combine(output, "Spike", "Network", "Packets");

            if (!Directory.Exists(packetsDirectory))
            {
                Directory.CreateDirectory(packetsDirectory);
            }

            var customTypesDirectory = Path.Combine(output, "Spike", "Network", "Entities");

            if (!Directory.Exists(customTypesDirectory))
            {
                Directory.CreateDirectory(customTypesDirectory);
            }

            Extentions.CopyFromRessources("Spike.Build.CSharp.StaticFiles.CLZF.cs", Path.Combine(networkDirectory, @"CLZF.cs"));
            Extentions.CopyFromRessources("Spike.Build.CSharp.StaticFiles.TcpChannelBase.cs", Path.Combine(networkDirectory, @"TcpChannelBase.cs"));

            var tcpChanneltemplate = new TcpChannelTemplate();
            var tcpChannelsession  = new Dictionary <string, object>();

            tcpChanneltemplate.Session = tcpChannelsession;

            tcpChannelsession["Model"] = model;
            tcpChanneltemplate.Initialize();

            var code = tcpChanneltemplate.TransformText();

            File.WriteAllText(Path.Combine(networkDirectory, @"TcpChannel.cs"), code);

            //Make packets
            var packetTemplate = new PacketTemplate();
            var packetSession  = new Dictionary <string, object>();

            packetTemplate.Session = packetSession;
            foreach (var receive in model.Receives)
            {
                packetTemplate.Clear();
                packetSession["Operation"] = receive;
                packetTemplate.Initialize();

                code = packetTemplate.TransformText();
                File.WriteAllText(Path.Combine(packetsDirectory, string.Format(@"{0}.cs", receive.Name)), code);
            }

            //Make CustomType
            var customTypeTemplate = new CustomTypeTemplate();
            var customTypeSession  = new Dictionary <string, object>();

            customTypeTemplate.Session = customTypeSession;
            foreach (var customType in model.CustomTypes)
            {
                customTypeTemplate.Clear();
                customTypeSession["CustomType"] = customType;
                customTypeTemplate.Initialize();
                code = customTypeTemplate.TransformText();
                File.WriteAllText(Path.Combine(customTypesDirectory, string.Format(@"{0}.cs", customType.Name)), code);
            }
        }