Example #1
0
        public static void WriteUncompressedReplay(string filepath, string replay)
        {
            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);
            }

            try
            {
                var unpackedreplay = ReplayLoader.LoadReplay(new BinaryReader(new FileStream(replay, FileMode.Open)), true);
                using (var binarywriter = new BinaryWriter(File.OpenWrite(filepath + @"\" + replay.Substring(replay.LastIndexOf('\\') + 1))))
                {
                    binarywriter.Write(unpackedreplay.Identifier);
                    binarywriter.Write("Header");
                    binarywriter.Write(unpackedreplay.Header);
                    binarywriter.Write("Actions");
                    binarywriter.Write(unpackedreplay.Actions);
                    binarywriter.Write("Map");
                    binarywriter.Write(unpackedreplay.Map);
                    Console.WriteLine("Finished writing unpacked replay {0}.", replay);
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.GetInstance()?.LogError($"{DateTime.Now} - Something went wrong while writing the uncompressed replay: {ex.Message}", ex: ex);
            }
        }
Example #2
0
        public SpectatorService(Session session, ReplayLoader loader) : base("/observer-mode/rest/consumer")
        {
            this.session = session;
            this.loader  = loader;

            //Replay(2232035041);
        }
Example #3
0
    public void LoadReplay()
    {
        replayLoader = new ReplayLoader();

        string replayName = loader.gamepath + "/" + Globals.modname.Text + "/jumpdemo/" + Globals.map.Text + "_388.dj3";

        replay = replayLoader.LoadReplay(replayName, Globals.scale.Value);

        if (replay == null)
        {
            Console.DebugLog("Goblin replay not found! " + replayName);
        }
    }
Example #4
0
        static void Main(string[] args)
        {
            //var replay = ReplayLoader.LoadReplay("0022_PvT_Vanko_buralzzan.rep(281).rep");

            Stopwatch sw = new Stopwatch();

            sw.Start();
            var replay = ReplayLoader.LoadReplay("1.rep");

            sw.Stop();

            System.Console.WriteLine("Parse Time: " + sw.Elapsed.TotalSeconds);
            PrintActionType <BuildAction>(replay);
            PrintActionType <GameChatAction>(replay);
        }
Example #5
0
        static void Main(string[] args)
        {
            if (args == null || args.Length == 0 || String.IsNullOrEmpty(args[0]))
            {
                Console.WriteLine("You need to specify a folder containing the replays.");
                return;
            }

            // Set path of folder containing replay files
            string filepath = args[0];

            if (!Directory.Exists(filepath))
            {
                Console.WriteLine("Could not find the folder specified.");
                return;
            }

            int take = 0;

            if (args.Length > 1)
            {
                take = int.Parse(args[1]);
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            // Define building features.
            var buildingFeatures = new List <Entities.ObjectType>()
            {
                Entities.ObjectType.Barracks, Entities.ObjectType.SupplyDepot,
                Entities.ObjectType.CommandCenter, Entities.ObjectType.Refinery,
                Entities.ObjectType.EngineeringBay, Entities.ObjectType.Bunker,
                Entities.ObjectType.Academy, Entities.ObjectType.Factory,
                Entities.ObjectType.MissileTurret, Entities.ObjectType.ComsatStation,
                Entities.ObjectType.MachineShop, Entities.ObjectType.Starport,
                Entities.ObjectType.Armory, Entities.ObjectType.ScienceFacility,
                Entities.ObjectType.ControlTower, Entities.ObjectType.PhysicsLab,
                Entities.ObjectType.CovertOps, Entities.ObjectType.NuclearSilo
            };

            // Get list of replay filesnames
            var files = Directory.EnumerateFiles(filepath, "*.rep");

            if (take > 0)
            {
                files = files.Take(take);
            }

            // Initialize the streamwriter for the output comma-separated file.
            using (var terranOut = new StreamWriter("terran.csv"))
            {
                // Write the head of the output-file with the features.
                string featureString = "";
                featureString += "\"GAME\"" + separater;
                featureString += "\"PLAYER\"" + separater;
                foreach (var feature in buildingFeatures)
                {
                    featureString += "\"" + feature.ToString().ToUpper() + "\"" + separater;
                }
                terranOut.WriteLine(featureString.TrimEnd(separater));

                int count = 1;

                // Iterate each replay file.
                string aggregateString = "";
                foreach (var file in files)
                {
                    // Parse replay
                    var replay = ReplayLoader.LoadReplay(file);

                    // Filter out anything but terran, while getting players
                    var players = replay.Players.Where(x => x.RaceType == Entities.RaceType.Terran);
                    if (players == null || players.Count() == 0 || players.Count() > 2)
                    {
                        continue;
                    }

                    // Iterate through each terran player.
                    foreach (var player in players)
                    {
                        // Get the build actions of the given player
                        var playerActions = replay.Actions.Where(x => x.Player == player &&
                                                                 x.ActionType == Entities.ActionType.Build)
                                            .Cast <BuildAction>();

                        if (playerActions.Count() == 0)
                        {
                            continue;
                        }

                        // Begin building output string.
                        aggregateString  = "\"" + Path.GetFileName(file).Replace(separater, '_') + "\"" + separater;
                        aggregateString += "\"" + player.Name.Replace(separater, '_') + "\"" + separater;

                        // Get first build time for each feature building
                        foreach (var feature in buildingFeatures)
                        {
                            var buildTime = playerActions.Where(x => x.ObjectType == feature)
                                            .OrderBy(x => x.Frame)
                                            .Select(x => x.Frame)
                                            .FirstOrDefault() / 23; // The time is in "Ticks" - divide by 23 to approximate seconds.
                            aggregateString += buildTime.ToString() + separater;
                        }

                        // Write out the players features and flush
                        terranOut.WriteLine(aggregateString.TrimEnd(separater));
                        terranOut.Flush();
                    }

                    Console.WriteLine("File #" + count + ": (" + Path.GetFileName(file) + ") processed!");
                    count++;
                }

                terranOut.Flush();
            }   // Disposing streamwriter

            sw.Stop();

            Console.WriteLine("Done! Time: " + sw.Elapsed);
        }
Example #6
0
        static void Main(string[] args)
        {
            //if (args == null || args.Length == 0 || String.IsNullOrEmpty(args[0]))
            //{
            //    Console.WriteLine("You need to specify a folder containing the replays.");
            //    return;
            //}

            ////Set path of folder containing replay files
            //string filepath = args[0];
            string filepath = @"F:\reps";

            if (!Directory.Exists(filepath))
            {
                Console.WriteLine("Could not find the folder specified.");
                return;
            }
            int take = 0;

            if (args.Length > 1)
            {
                take = int.Parse(args[1]);
            }

            // Get list of replay filesnames
            var files = Directory.EnumerateFiles(filepath, "*.rep");

            if (take > 0)
            {
                files = files.Take(take);
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            List <IReplay> parsedReplays = new List <IReplay>();

            foreach (var file in files)
            {
                // Parse replay
                var replay = ReplayLoader.LoadReplay(file);
                parsedReplays.Add(replay);
            }

            TreeBuilder tb = new TreeBuilder(parsedReplays);

            tb.Build();

            string supbitches = tb.ToString();

            System.Console.WriteLine(supbitches);
            using (StreamWriter writer = new StreamWriter(new FileStream("graphviz.txt", FileMode.Create)))
            {
                writer.Write(supbitches);
            }

            Kmeans c = new Kmeans();

            c.Cluster(5, tb.AllGames);

            sw.Stop();
            System.Console.WriteLine("Time: " + sw.Elapsed.TotalSeconds);
            System.Console.ReadKey();
        }
Example #7
0
        static void Main(string[] args)
        {
            // Try writing resource to disk C:\testreplays2 => success
            //if (!Directory.Exists(@"C:\testreplays2"))
            //{
            //    Directory.CreateDirectory(@"C:\testreplays2");
            //}
            //File.WriteAllBytes(@"C:\testreplays2\_000204_Fighting_Spirit_1_3_.rep", ReplayParser.ReplaySorter.Replays._000204_Fighting_Spirit_1_3_);

            // Try writing all resources to disk C:\testreplays2 => success
            //if (!Directory.Exists(@"C:\testreplays2"))
            //{
            //    Directory.CreateDirectory(@"C:\testreplays2");
            //}

            //var Resources = Assembly.GetExecutingAssembly().GetManifestResourceNames();
            //foreach (var aResource in Resources)
            //{
            //    if (aResource.Substring(aResource.Length - 4) == ".rep")
            //    {
            //        string FilePath = @"C:\testreplays2\" + aResource.Substring(36);
            //        using (Stream replayStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(aResource))
            //        {
            //            using (var FileStream = new FileStream(FilePath, FileMode.OpenOrCreate))
            //            {
            //                replayStream.CopyTo(FileStream);
            //            }
            //        }
            //    }
            //}

            // ask directory, if none supplied, check the embedded resources directory

            //if (directory == string.Empty)
            //{
            //    directory = Directory.GetCurrentDirectory() + @"\" + "Resources";
            //}

            // parse all replays in directory


            // for your UI, you should be able to put these things in a separate class and methods so you can make use of them instead of having to rewrite this...

            IReplaySorterConfiguration replaySorterConfiguration = new ReplaySorterAppConfiguration();

            if (ErrorLogger.GetInstance(replaySorterConfiguration) == null)
            {
                Console.WriteLine("Issue intializing logger. Logging will be disabled.");
            }

            var searchDirectory = (SearchDirectory)User.AskDirectory(typeof(SearchDirectory));
            List <File <IReplay> > ListReplays = new List <File <IReplay> >();
            IEnumerable <string>   files       = Directory.EnumerateFiles(searchDirectory.Directory, "*.rep", searchDirectory.SearchOption);

            while (files.Count() == 0)
            {
                Console.WriteLine("No replays found in {0}", searchDirectory.Directory);
                searchDirectory = (SearchDirectory)User.AskDirectory(typeof(SearchDirectory));
                files           = Directory.EnumerateFiles(searchDirectory.Directory, "*.rep", searchDirectory.SearchOption);
            }
            // getting a lot of errors for replays, figure out why. Add possibility to rename the replays themselves.
            // Also figure out how to make this  1.16, 1.18, 1.19, ... compatible

            // could I put a try/catch inside a nother catch? Or should I just use a stringbuilder and write the entirety of it to a file in the end
            StringBuilder ErrorMessages             = new StringBuilder();
            List <string> ReplaysThrowingExceptions = new List <string>();
            //var BadReplays = searchDirectory.Directory + @"\BadReplays";
            Stopwatch sw = new Stopwatch();

            sw.Start();
            var ProgressBar     = new ProgressBar(Console.CursorLeft, Console.CursorTop);
            var numberOfFiles   = files.Count();
            int currentPosition = 0;

            //try
            //{
            foreach (var replay in files)
            {
                currentPosition++;
                ProgressBar.ShowAndUpdate(currentPosition, numberOfFiles);
                try
                {
                    var ParsedReplay = ReplayLoader.LoadReplay(replay);
                    ListReplays.Add(File <IReplay> .Create(ParsedReplay, replay));
                    //WriteUncompressedReplay(@"C:\testreplays\UncompressedReplays", replay);
                }
                catch (Exception ex)
                {
                    //Console.WriteLine(ex.Message);
                    Console.WriteLine("\nError with replay {0}", replay.ToString());
                    ErrorMessages.AppendLine(ex.Message + ":" + replay.ToString());
                    ReplaysThrowingExceptions.Add(replay);
                }
            }
            Console.WriteLine();
            //}
            //catch(Exception ex)
            //{
            //    Console.WriteLine(ex.Message);
            //}
            sw.Stop();
            //var ErrorMessagesFile = @"C:\testreplays\ErrorMessages.txt";
            var ErrorMessagesFile = searchDirectory.Directory + @"\ErrorMessages.txt";

            Console.WriteLine("It took {0} seconds to parse {1} replays. {2} replays encountered exceptions during parsing. Errors can be investigated in the textfile: {3}", sw.Elapsed, ListReplays.Count(), ReplaysThrowingExceptions.Count(), ErrorMessagesFile);
            try
            {
                //using (var StreamWriter = new StreamWriter(ErrorMessagesFile))
                //{

                //}
                File.WriteAllText(ErrorMessagesFile, ErrorMessages.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error writing error messages." + ex.Message);
            }

            if (ReplaysThrowingExceptions.Count() > 0)
            {
                Console.WriteLine("Move bad replays to a designated folder? Y(es)/N(o).");
                var MoveBadReplays = User.AskYesNo();
                if (MoveBadReplays.Yes != null)
                {
                    if ((bool)MoveBadReplays.Yes)
                    {
                        var  BadReplaysOutputDirectory = (OutputDirectory)User.AskDirectory(typeof(OutputDirectory), "Specify a directory to put the bad replays.");
                        bool CreateDirectorySuccess    = false;
                        while (!CreateDirectorySuccess)
                        {
                            try
                            {
                                Directory.CreateDirectory(BadReplaysOutputDirectory.Directory);
                                CreateDirectorySuccess = true;
                            }
                            catch (Exception)
                            {
                                Console.WriteLine("Could not create directory. Invalid directory name or not enough privileges.");
                                CreateDirectorySuccess = false;
                            }
                        }

                        foreach (var replay in ReplaysThrowingExceptions)
                        {
                            ReplayHandler.RemoveBadReplay(BadReplaysOutputDirectory.Directory + @"\BadReplays", replay);
                        }
                        ReplayHandler.LogBadReplays(ReplaysThrowingExceptions, replaySorterConfiguration.LogDirectory, $"{DateTime.Now} - Error while parsing replay: {{0}}");
                    }
                }
                else
                {
                    Console.WriteLine("Answer can not be null");
                    return;
                }
                // remove bad replay from files
                // not necessary anymore since I use ReplayFile now
                // files = files.Where(x => !ReplaysThrowingExceptions.Contains(x));
            }

            // you might want to extract information and put everything into some sort of data structure, so you don't have to go through the list of replays for each
            // sort but instead can lookup in the datastructure/table?


            //Ask criteria to sort on, let's start with something simple...
            //Player name

            // use a library to parse command line arguments => then return some sort of Enum
            // then pass this enum to a Sorter object
            OutputDirectory SortResultOutputDirectory = (OutputDirectory)User.AskDirectory(typeof(OutputDirectory), "Specify a directory for the sort result. If it does not exist, it will be created.");

            while (!Directory.Exists(SortResultOutputDirectory.Directory))
            {
                try
                {
                    Directory.CreateDirectory(SortResultOutputDirectory.Directory);
                }
                catch (Exception)
                {
                    Console.WriteLine("Could not create directory. Invalid directory name or not enough privileges.");
                    Console.WriteLine("Retry?");
                    var TryAgain = User.AskYesNo();
                    if (TryAgain.Yes != null)
                    {
                        if ((bool)TryAgain.Yes)
                        {
                            continue;
                        }
                        else
                        {
                            SortResultOutputDirectory = (OutputDirectory)User.AskDirectory(typeof(OutputDirectory), "Specify a different directory for the sort result.If it does not exist, it will be created.");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Answer can not be null.");
                        return;
                    }
                }
            }

            Console.WriteLine("Please enter criteria to sort replays on.");
            Console.WriteLine("Provide a space separated list of criteria.");
            var           SortCriteria = User.AskCriteria();
            Sorter        sorter       = new Sorter(SortResultOutputDirectory.Directory, ListReplays);
            List <string> replaysThrowingExceptions = new List <string>();

            while (SortCriteria.StopProgram != null)
            {
                if ((bool)!SortCriteria.StopProgram)
                {
                    var CriteriaParameters = User.AskAdditionalQuestionsSortCriteria(SortCriteria.ChosenCriteria);

                    Console.WriteLine("Keep original replay names?");
                    var KeepOriginalReplayNames = User.AskYesNo();
                    if (KeepOriginalReplayNames.Yes != null)
                    {
                        if ((bool)!KeepOriginalReplayNames.Yes)
                        {
                            CustomReplayFormat aCustomReplayFormat = null;
                            while (aCustomReplayFormat == null)
                            {
                                Console.WriteLine("Custom format?");
                                string customformat = Console.ReadLine();
                                try
                                {
                                    aCustomReplayFormat       = CustomReplayFormat.Create(customformat, ListReplays.Count, true);
                                    sorter.CustomReplayFormat = aCustomReplayFormat;
                                }
                                catch (Exception)
                                {
                                    Console.WriteLine("Invalid custom format.");
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Answer can not be null");
                        return;
                    }

                    sorter.CurrentDirectory    = SortResultOutputDirectory.Directory;
                    sorter.SortCriteria        = SortCriteria.ChosenCriteria;
                    sorter.CriteriaStringOrder = SortCriteria.CriteriaStringOrder;
                    try
                    {
                        // use SortCriteriaParameters
                        sorter.ExecuteSort(CriteriaParameters.SortCriteriaParameters, (bool)KeepOriginalReplayNames.Yes, replaysThrowingExceptions);
                        Console.WriteLine("Sort finished.");
                        ReplayHandler.LogBadReplays(replaysThrowingExceptions, replaySorterConfiguration.LogDirectory, $"{DateTime.Now} - Error while sorting replay: {{0}} using arguments: {sorter.ToString()}");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        Console.WriteLine("Please enter criteria to sort replays on.");
                        Console.WriteLine("Provide a space separated list of criteria.");
                        SortCriteria = User.AskCriteria();
                    }
                }
                else
                {
                    return;
                }
            }
            Console.WriteLine("Stop Program can not be null");
            return;

            //string consoleInput = Console.ReadLine();
            //Sorter sorter = new Sorter();
            //while (consoleInput.ToUpper() != "STOP")
            //{
            //    string[] Criteria = Array.ConvertAll(consoleInput.ToUpper().Split(new char[] { ' ' }), x => x.Trim());
            //    Criteria ChosenCriteria = new Criteria();
            //    foreach (var criteria in Criteria)
            //    {
            //        if (!Enum.TryParse(criteria, out ChosenCriteria))
            //        {
            //            Console.WriteLine("Invalid criteria: {0}", criteria);
            //        }
            //    }

            //    // again have sort of mapping structure to ask additional questions bsed on the criteria
            //    // for now keep it simple, just ask the questions
            //    Console.WriteLine("Keep original replay names?");
            //    var KeepOriginalReplayNames = User.AskYesNo();
            //    if (KeepOriginalReplayNames.Yes != null)
            //    {
            //        if ((bool)!KeepOriginalReplayNames.Yes)
            //        {
            //            CustomReplayFormat aCustomReplayFormat = new CustomReplayFormat();
            //            while (aCustomReplayFormat.CustomFormat == null)
            //            {
            //                Console.WriteLine("Custom format?");
            //                string customformat = Console.ReadLine();
            //                try
            //                {
            //                    aCustomReplayFormat.CustomFormat = customformat;
            //                    sorter.CustomReplayFormat = aCustomReplayFormat;
            //                }
            //                catch (Exception ex)
            //                {
            //                    Console.WriteLine(ex.Message);
            //                }
            //            }
            //        }
            //    }
            //    else
            //    {
            //        Console.WriteLine("Answer can not be null");
            //        return;
            //    }

            //    var CriteriaParameters = User.AskAdditionalQuestionsSortCriteria(ChosenCriteria);

            //    sorter.CurrentDirectory = searchDirectory.Directory;
            //    sorter.Files = files;
            //    sorter.ListReplays = ListReplays;
            //    sorter.SortCriteria = ChosenCriteria;
            //    try
            //    {
            //        sorter.ExecuteSort(CriteriaParameters.MakeFolderForWinner, CriteriaParameters.MakeFolderForLoser, (bool)KeepOriginalReplayNames.Yes, CriteriaParameters.ValidGameTypes, CriteriaParameters.Durations);
            //        Console.WriteLine("Sort finished.");
            //    }
            //    catch (Exception ex)
            //    {
            //        Console.WriteLine(ex.Message);
            //    }
            //    finally
            //    {
            //        Console.WriteLine("Please enter criteria to sort replays on.");
            //        Console.WriteLine("Provide a space separated list of criteria.");
            //        consoleInput = Console.ReadLine();
            //    }
            //}


            // switch not going to work unless you go through each value between 1 to 2^x and not just the values defined in the enum
            //switch((int)ChosenCriteria)
            //{
            //    case 1:
            //        break;
            //    case 2:
            //        break;
            //    default:
            //        break;
            //}

            // use hasflag instead

            //if (ChosenCriteria.HasFlag(ReplaySorter.Criteria.PLAYERNAME))
            //{
            //    List<string> PlayerNames = new List<string>();
            //    // sort on playername
            //    foreach (var replay in ListReplays)
            //    {
            //        //List<string> Players = new List<string>();
            //        var parseplayers = replay.Players.ToList();
            //        foreach (var aplayer in parseplayers)
            //        {
            //            //Players.Add(aplayer.Name);
            //            if (!PlayerNames.Contains(aplayer.Name))
            //            {
            //                PlayerNames.Add(aplayer.Name);
            //            }
            //        }
            //            //PlayerNames.AddRange(Players);
            //    }

            //    // create sort directory, and directories for each player
            //    string sortDirectory = @directory + @"\" + ChosenCriteria.ToString();
            //    Directory.CreateDirectory(sortDirectory);
            //    foreach(var player in PlayerNames.Distinct())
            //    {
            //        Directory.CreateDirectory(sortDirectory + @"\" + player);
            //    }

            //    // now add all replays associated with player into the folder

            //foreach (var replay in ListReplays)
            //    {
            //        // get players per replay
            //        var ParsePlayers = replay.Players.ToList();
            //        var index = ListReplays.IndexOf(replay);
            //        var FilePath = files.ElementAt(index);
            //        var DirectoryName = Directory.GetParent(FilePath);
            //        var FileName = FilePath.Substring(DirectoryName.ToString().Length);

            //        foreach (var aPlayer in ParsePlayers)
            //        {
            //            // for each player, get proper folder
            //            // find the corresponding replay file
            //            // add this file to that folder
            //            var PlayerName = aPlayer.Name;
            //            var DestinationFilePath = sortDirectory + @"\" + PlayerName + FileName;
            //            File.Copy(FilePath, DestinationFilePath);
            //        }
            //    }
            //}
        }