Пример #1
0
 private void Log(string log)
 {
     try
     {
         listener.OnLogAppeared(log);
     }
     catch { }
 }
Пример #2
0
        public void Run(string[] args)
        {
            try
            {
                if (args.Length < 2 || args.Length > 3)
                {
                    listener.OnFail("ERROR: args format should be {instructions} {completed instructions} {post update executable (optional)}");
                    return;
                }

                if (args.Length >= 3)
                {
                    postSelfPatcher = args[2];
                }

                string instructionsPath          = args[0];
                string completedInstructionsPath = args[1];

                if (!File.Exists(instructionsPath))
                {
                    listener.OnFail("ERROR: instructions file \"" + instructionsPath + "\" does not exist");
                    return;
                }

                string instructions = File.ReadAllText(instructionsPath);
                if (string.IsNullOrEmpty(instructions))
                {
                    listener.OnFail("ERROR: missing instructions");
                    return;
                }

                int completedInstructions = 0;
                if (File.Exists(completedInstructionsPath) && !int.TryParse(File.ReadAllText(completedInstructionsPath), out completedInstructions))
                {
                    completedInstructions = 0;
                }

                Op     currentOp = Op.Delete;
                string moveFrom = null;
                int    currentInstruction = 0;
                int    tokenStart = 0, tokenEnd = instructions.IndexOf(OP_SEPARATOR);
                if (tokenEnd < 0)
                {
                    listener.OnFail("ERROR: invalid instructions file");
                    return;
                }

                try
                {
                    listener.OnLogAppeared(string.Concat("Updating from v", instructions.Substring(0, tokenEnd), ", please don't close this window!"));
                }
                catch { }

                int numberOfInstructions = 0;
                while (tokenStart < instructions.Length)
                {
                    tokenStart = tokenEnd + OP_SEPARATOR.Length;
                    if (tokenStart >= instructions.Length)
                    {
                        break;
                    }

                    tokenEnd = instructions.IndexOf(OP_SEPARATOR, tokenStart);
                    if (tokenEnd < 0)
                    {
                        break;
                    }

                    string token = instructions.Substring(tokenStart, tokenEnd - tokenStart);
                    if (token.Length == 0)
                    {
                        continue;
                    }

                    if (token != DELETE_OP && token != MOVE_OP)
                    {
                        numberOfInstructions++;
                    }
                }

                if (numberOfInstructions > 0)
                {
                    tokenStart = 0;
                    tokenEnd   = instructions.IndexOf(OP_SEPARATOR);
                    while (tokenStart < instructions.Length)
                    {
                        try
                        {
                            listener.OnProgressChanged(currentInstruction, numberOfInstructions);
                        }
                        catch { }

                        tokenStart = tokenEnd + OP_SEPARATOR.Length;
                        if (tokenStart >= instructions.Length)
                        {
                            break;
                        }

                        tokenEnd = instructions.IndexOf(OP_SEPARATOR, tokenStart);
                        if (tokenEnd < 0)
                        {
                            break;
                        }

                        string token = instructions.Substring(tokenStart, tokenEnd - tokenStart);
                        if (token.Length == 0)
                        {
                            continue;
                        }

                        if (token == DELETE_OP)
                        {
                            currentOp = Op.Delete;
                        }
                        else if (token == MOVE_OP)
                        {
                            currentOp = Op.Move;
                            moveFrom  = null;
                        }
                        else
                        {
                            currentInstruction++;

                            if (currentOp == Op.Delete)
                            {
                                if (currentInstruction <= completedInstructions)
                                {
                                    continue;
                                }

                                File.WriteAllText(completedInstructionsPath, (currentInstruction - 1).ToString());
                                Delete(token);
                            }
                            else if (currentOp == Op.Move)
                            {
                                if (moveFrom == null)
                                {
                                    moveFrom = token;
                                }
                                else
                                {
                                    if (currentInstruction <= completedInstructions)
                                    {
                                        continue;
                                    }

                                    File.WriteAllText(completedInstructionsPath, (currentInstruction - 1).ToString());

                                    MoveFiles(moveFrom, token);
                                    moveFrom = null;
                                }
                            }
                        }
                    }
                }

                try
                {
                    listener.OnLogAppeared("Successful..!");
                }
                catch { }

                listener.OnSuccess();
            }
            catch (Exception e)
            {
                listener.OnFail("ERROR: " + e.ToString());
            }
        }