Exemplo n.º 1
0
        public clsResult Save(sWrite_WZ_Args Args)
        {
            var returnResult =
                new clsResult("Compiling to \"{0}\"".Format2(Args.Path), false);
            logger.Info("Compiling to \"{0}\"".Format2(Args.Path));

            try
            {
                switch ( Args.CompileType )
                {
                    case sWrite_WZ_Args.enumCompileType.Multiplayer:
                        if ( Args.Multiplayer == null )
                        {
                            returnResult.ProblemAdd("Multiplayer arguments were not passed.");
                            return returnResult;
                        }
                        if ( Args.Multiplayer.PlayerCount < 2 | Args.Multiplayer.PlayerCount > Constants.PlayerCountMax )
                        {
                            returnResult.ProblemAdd(string.Format("Number of players was below 2 or above {0}.", Constants.PlayerCountMax));
                            return returnResult;
                        }
                        break;
                    case sWrite_WZ_Args.enumCompileType.Campaign:
                        if ( Args.Campaign == null )
                        {
                            returnResult.ProblemAdd("Campaign arguments were not passed.");
                            return returnResult;
                        }
                        break;
                    default:
                        returnResult.ProblemAdd("Unknown compile method.");
                        return returnResult;
                }

                if ( !Args.Overwrite )
                {
                    if ( File.Exists(Args.Path) )
                    {
                        returnResult.ProblemAdd("The selected file already exists.");
                        return returnResult;
                    }
                }

                if ( Args.CompileType == sWrite_WZ_Args.enumCompileType.Multiplayer )
                {
                    if ( !Args.Overwrite )
                    {
                        if ( File.Exists(Args.Path) )
                        {
                            returnResult.ProblemAdd(string.Format("A file already exists at: {0}", Args.Path));
                            return returnResult;
                        }
                    }

                    try
                    {
                        using ( var zip = new ZipOutputStream(Args.Path) )
                        {
                            // Set encoding
                            zip.AlternateEncoding = Encoding.GetEncoding("UTF-8");
                            zip.AlternateEncodingUsage = ZipOption.Always;

                            // Set compression
                            zip.CompressionLevel = CompressionLevel.BestCompression;

                            // .xplayers.lev
                            var zipPath = string.Format("{0}c-{1}.xplayers.lev", Args.Multiplayer.PlayerCount, Args.MapName);
                            if ( Args.CompileType == sWrite_WZ_Args.enumCompileType.Multiplayer )
                            {
                                zip.PutNextEntry(zipPath);
                                returnResult.Add(Serialize_WZ_LEV(zip, Args.Multiplayer.PlayerCount,
                                    Args.Multiplayer.AuthorName, Args.Multiplayer.License,
                                    Args.MapName));
                            }

                            var path = string.Format("multiplay/maps/{0}c-{1}", Args.Multiplayer.PlayerCount, Args.MapName);
                            zip.PutNextEntry(string.Format("{0}.gam", path));
                            returnResult.Add(Serialize_WZ_Gam(zip, 0U,
                                Args.CompileType, Args.ScrollMin, Args.ScrollMax));

                            zip.PutNextEntry(string.Format("{0}/struct.ini", path));
                            var iniStruct = new IniWriter(zip);
                            returnResult.Add(Serialize_WZ_StructuresINI(iniStruct, Args.Multiplayer.PlayerCount));
                            iniStruct.Flush();

                            zip.PutNextEntry(string.Format("{0}/droid.ini", path));
                            var iniDroid = new IniWriter(zip);
                            returnResult.Add(Serialize_WZ_DroidsINI(iniDroid, Args.Multiplayer.PlayerCount));
                            iniDroid.Flush();

                            zip.PutNextEntry(string.Format("{0}/labels.ini", path));
                            var iniLabels = new IniWriter(zip);
                            returnResult.Add(Serialize_WZ_LabelsINI(iniLabels, Args.Multiplayer.PlayerCount));
                            iniLabels.Flush();

                            zip.PutNextEntry(string.Format("{0}/feature.ini", path));
                            var iniFeature = new IniWriter(zip);
                            returnResult.Add(Serialize_WZ_FeaturesINI(iniFeature));
                            iniFeature.Flush();

                            zip.PutNextEntry(string.Format("{0}/game.map", path));
                            returnResult.Add(Serialize_WZ_Map(zip));

                            zip.PutNextEntry(string.Format("{0}/ttypes.ttp", path));
                            var ttpSaver = new TTP.TTP(map);
                            returnResult.Add(ttpSaver.Save(zip));
                        }
                    }
                    catch ( Exception ex )
                    {
                        Debugger.Break();
                        returnResult.ProblemAdd(ex.Message);
                        logger.ErrorException("Got an exception", ex);
                        return returnResult;
                    }

                    return returnResult;
                }
                if ( Args.CompileType == sWrite_WZ_Args.enumCompileType.Campaign )
                {
                    var CampDirectory = PathUtil.EndWithPathSeperator(Args.Path);

                    if ( !Directory.Exists(CampDirectory) )
                    {
                        returnResult.ProblemAdd(string.Format("Directory {0} does not exist.", CampDirectory));
                        return returnResult;
                    }

                    var filePath = string.Format("{0}{1}.gam", CampDirectory, Args.MapName);
                    using ( var file = File.Open(filePath, FileMode.Open | FileMode.CreateNew) )
                    {
                        returnResult.Add(Serialize_WZ_Gam(file, Args.Campaign.GAMType,
                            Args.CompileType, Args.ScrollMin, Args.ScrollMax));
                    }

                    CampDirectory += Args.MapName + Convert.ToString(App.PlatformPathSeparator);
                    try
                    {
                        Directory.CreateDirectory(CampDirectory);
                    }
                    catch ( Exception ex )
                    {
                        returnResult.ProblemAdd(string.Format("Unable to create directory {0}", CampDirectory));
                        logger.ErrorException("Got an exception", ex);
                        return returnResult;
                    }

                    filePath = CampDirectory + "droid.ini";
                    using ( var file = File.Open(filePath, FileMode.Open | FileMode.CreateNew) )
                    {
                        var iniDroid = new IniWriter(file);
                        returnResult.Add(Serialize_WZ_DroidsINI(iniDroid, -1));
                        iniDroid.Flush();
                    }

                    filePath = CampDirectory + "feature.ini";
                    using ( var file = File.Open(filePath, FileMode.Open | FileMode.CreateNew) )
                    {
                        var iniFeatures = new IniWriter(file);
                        returnResult.Add(Serialize_WZ_FeaturesINI(iniFeatures));
                        iniFeatures.Flush();
                    }

                    filePath = CampDirectory + "game.map";
                    using ( var file = File.Open(filePath, FileMode.Open | FileMode.CreateNew) )
                    {
                        returnResult.Add(Serialize_WZ_Map(file));
                    }

                    filePath = CampDirectory + "struct.ini";
                    using ( var file = File.Open(filePath, FileMode.Open | FileMode.CreateNew) )
                    {
                        var iniStruct = new IniWriter(file);
                        returnResult.Add(Serialize_WZ_StructuresINI(iniStruct, -1));
                        iniStruct.Flush();
                    }

                    filePath = CampDirectory + "ttypes.ttp";
                    var ttpSaver = new TTP.TTP(map);
                    returnResult.Add(ttpSaver.Save(filePath, false));

                    filePath = CampDirectory + "labels.ini";
                    using ( var file = File.Open(filePath, FileMode.Open | FileMode.CreateNew) )
                    {
                        var iniLabels = new IniWriter(file);
                        returnResult.Add(Serialize_WZ_LabelsINI(iniLabels, 0));
                        iniLabels.Flush();
                    }
                }
            }
            catch ( Exception ex )
            {
                Debugger.Break();
                returnResult.ProblemAdd(ex.Message);
                logger.ErrorException("Got an exception", ex);
                return returnResult;
            }

            return returnResult;
        }
Exemplo n.º 2
0
        public void btnCompileCampaign_Click(Object sender, EventArgs e)
        {
            var ReturnResult = new clsResult("Compile campaign", false);
            logger.Info("Compile campaign");
            var A = 0;

            SaveToMap();

            A = ValidateMap_WaterTris();
            if ( A > 0 )
            {
                ReturnResult.WarningAdd(A + " water tiles have an incorrect triangle direction. There might be in-game graphical glitches on those tiles.");
            }

            ReturnResult.Add(ValidateMap());
            ReturnResult.Add(ValidateMap_UnitPositions());

            var MapName = "";
            var TypeNum = 0;

            MapName = txtName.Text;
            if ( MapName.Length < 1 )
            {
                ReturnResult.ProblemAdd("Enter a name for the campaign files.");
            }
            TypeNum = cboCampType.SelectedIndex;
            if ( TypeNum < 0 | TypeNum > 2 )
            {
                ReturnResult.ProblemAdd("Select a campaign type.");
            }
            if ( ReturnResult.HasProblems )
            {
                App.ShowWarnings(ReturnResult);
                return;
            }
            var CompileCampDialog = new FolderBrowserDialog();
            if ( CompileCampDialog.ShowDialog(this) != DialogResult.OK )
            {
                return;
            }
            var WriteWZArgs = new sWrite_WZ_Args();
            WriteWZArgs.MapName = MapName;
            WriteWZArgs.Path = CompileCampDialog.SelectedPath;
            WriteWZArgs.Overwrite = false;
            SetScrollLimits(ref WriteWZArgs.ScrollMin, ref WriteWZArgs.ScrollMax);
            WriteWZArgs.Campaign = new sWrite_WZ_Args.clsCampaign();
            WriteWZArgs.Campaign.GAMType = (uint)TypeNum;
            WriteWZArgs.CompileType = sWrite_WZ_Args.enumCompileType.Campaign;

            var wzFormat = new Wz(Map);
            ReturnResult.Add(wzFormat.Save(WriteWZArgs));
            App.ShowWarnings(ReturnResult);
            if ( !ReturnResult.HasWarnings )
            {
                Close();
            }
        }
Exemplo n.º 3
0
        private clsResult Serialize_WZ_Gam(Stream stream, UInt32 gamType, sWrite_WZ_Args.enumCompileType compileType, XYInt scrollMin, sXY_uint scrollMax)
        {
            var returnResult = new clsResult("Serializing .gam", false);
            logger.Info("Serializing .gam");

            var fileGAM = new BinaryWriter(stream, App.ASCIIEncoding);

            IOUtil.WriteText(fileGAM, false, "game");
            fileGAM.Write(8U);
            fileGAM.Write(0U); //Time
            if ( compileType == sWrite_WZ_Args.enumCompileType.Multiplayer )
            {
                fileGAM.Write(0U);
            }
            else if ( compileType == sWrite_WZ_Args.enumCompileType.Campaign )
            {
                fileGAM.Write(gamType);
            }
            fileGAM.Write(scrollMin.X);
            fileGAM.Write(scrollMin.Y);
            fileGAM.Write(scrollMax.X);
            fileGAM.Write(scrollMax.Y);
            fileGAM.Write(new byte[20]);
            fileGAM.Flush();

            return returnResult;
        }
Exemplo n.º 4
0
        public void btnCompile_Click(Object sender, EventArgs e)
        {
            var ReturnResult = new clsResult("Compile multiplayer", false);
            logger.Info("Compile multiplayer");
            var A = 0;

            SaveToMap();

            var MapName = "";
            var License = cboLicense.Text;
            var PlayerCount = 0;
            if ( !IOUtil.InvariantParse(txtMultiPlayers.Text, ref PlayerCount) )
            {
                PlayerCount = 0;
            }
            if ( PlayerCount < 2 | PlayerCount > Constants.PlayerCountMax )
            {
                ReturnResult.ProblemAdd(string.Format("The number of players must be from 2 to {0}", Constants.PlayerCountMax));
            }

            A = ValidateMap_WaterTris();
            if ( A > 0 )
            {
                ReturnResult.WarningAdd(string.Format("{0} water tiles have an incorrect triangle direction. There might be in-game graphical glitches on those tiles.", A));
            }

            ReturnResult.Add(ValidateMap());
            ReturnResult.Add(ValidateMap_UnitPositions());
            ReturnResult.Add(ValidateMap_Multiplayer(PlayerCount));

            MapName = txtName.Text;
            var CurrentChar = (char)0;
            for ( A = 0; A <= MapName.Length - 1; A++ )
            {
                CurrentChar = MapName[A];
                if (
                    !((CurrentChar >= 'a' && CurrentChar <= 'z') || (CurrentChar >= 'A' && CurrentChar <= 'Z') ||
                      (A >= 1 && ((CurrentChar >= '0' && CurrentChar <= '9') || CurrentChar == '-' || CurrentChar == '_'))) )
                {
                    break;
                }
            }
            if ( A < MapName.Length )
            {
                ReturnResult.ProblemAdd("The map\'s name must contain only letters, numbers, underscores and hyphens, and must begin with a letter.");
            }
            if ( MapName.Length < 1 | MapName.Length > 16 )
            {
                ReturnResult.ProblemAdd("Map name must be from 1 to 16 characters.");
            }
            if ( string.IsNullOrEmpty(License) )
            {
                ReturnResult.ProblemAdd("Enter a valid license.");
            }
            if ( ReturnResult.HasProblems )
            {
                App.ShowWarnings(ReturnResult);
                return;
            }
            var CompileMultiDialog = new SaveFileDialog();
            if ( Map.PathInfo != null )
            {
                CompileMultiDialog.InitialDirectory = Map.PathInfo.Path;
            }
            CompileMultiDialog.FileName = PlayerCount + "c-" + MapName;
            CompileMultiDialog.Filter = "WZ Files (*.wz)|*.wz";
            if ( CompileMultiDialog.ShowDialog(this) != DialogResult.OK )
            {
                return;
            }
            var WriteWZArgs = new sWrite_WZ_Args();
            WriteWZArgs.MapName = MapName;
            WriteWZArgs.Path = CompileMultiDialog.FileName;
            WriteWZArgs.Overwrite = true;
            SetScrollLimits(ref WriteWZArgs.ScrollMin, ref WriteWZArgs.ScrollMax);
            WriteWZArgs.Multiplayer = new sWrite_WZ_Args.clsMultiplayer();
            WriteWZArgs.Multiplayer.AuthorName = txtAuthor.Text;
            WriteWZArgs.Multiplayer.PlayerCount = PlayerCount;
            WriteWZArgs.Multiplayer.License = License;
            WriteWZArgs.CompileType = sWrite_WZ_Args.enumCompileType.Multiplayer;

            var wzFormat = new Wz(Map);
            ReturnResult.Add(wzFormat.Save(WriteWZArgs));
            App.ShowWarnings(ReturnResult);
            if ( !ReturnResult.HasWarnings )
            {
                Close();
            }
        }