示例#1
0
    static void CopyRandomObjectToRandomCell()
    {
        HexObject[]      hexObjects  = FindObjectsOfType <HexObject>();
        BuildInstruction instruction = null;

        foreach (HexObject hexObj in hexObjects)
        {
            if (hexObj.IsBuilt)
            {
                instruction = hexObj.BuildInstruction;
                break;
            }
        }

        if (instruction != null)
        {
            int randX = UnityEngine.Random.Range(0, GameManager.Instance.Grid.GridSizeX);
            int randY = UnityEngine.Random.Range(0, GameManager.Instance.Grid.GridSizeY);
            instruction.Cell = new Vector2Int(randX, randY);
            FindObjectOfType <ObjectPlacer>().AddObject(instruction);
        }
        else
        {
            Debug.LogError("Couldn't get a BuildInstruction");
        }
    }
示例#2
0
        protected void AddRelativeSource(BuildInstruction targetInstr, Identifier ident, FileInfo srcFile)
        {
            try {
                var rp      = Path.GetRelativePath(targetInstr.TargetPath, targetInstr.TargetPath);
                var absPath = Path.GetFullPath(Path.Combine(srcFile.Directory.FullName, rp));
                if (rp != "." && Directory.Exists(absPath))
                {
                    // assume they're being added elsewhere for '.'

                    targetInstr.SourceFiles.AddFiles(new DirectoryInfo(absPath), ident.RawValue + ".*");
                }
            }
            catch (System.Exception) {
                _logger?.LogDebug(
                    $"Error while searching for relative path: {targetInstr.TargetPath}/{targetInstr.TargetPath}");
            }
        }
        private IEnumerable <FileReverseInst> Work(List <FileReverseInst> instList, int oldFileLength)
        {
            //筛选两个文件共同部分
            List <FileReverseInst> temp = new List <FileReverseInst>(instList);

            //排序
            temp.Sort((a, b) =>
            {
                BuildInstruction a1 = a.Inst, b1 = b.Inst;
                int compare         = a1.OldFilePosition.CompareTo(b1.OldFilePosition);
                if (compare == 0)
                {
                    compare = -a1.Length.CompareTo(b1.Length);
                }
                return(compare);
            });

            //进链表
            LinkedList <FileReverseInst> reverseList = new LinkedList <FileReverseInst>();

            foreach (var reverse in temp)
            {
                if (reverseList.Count <= 0)
                {
                    reverseList.AddFirst(reverse);
                }
                else
                {
                    BuildInstruction prev = reverseList.Last.Value.Inst;
                    BuildInstruction cur  = reverse.Inst;

                    if (cur.OldFilePosition <= prev.OldFilePosition) //过滤相等
                    {
                        continue;
                    }
                    if (cur.OldFilePosition < prev.OldFilePosition + prev.Length) //有重叠部分
                    {
                        int newLength = (cur.OldFilePosition + cur.Length) - (prev.OldFilePosition + prev.Length);
                        if (newLength <= 0) //完全包含
                        {
                            continue;
                        }
                        //调整不重叠
                        reverse.NewFilePosition += cur.Length - newLength;
                        cur.OldFilePosition      = (cur.OldFilePosition + cur.Length) - newLength;
                        cur.Length = newLength;
                    }
                    reverseList.AddLast(reverse);
                }
            }

            //链表填充
            if (reverseList.Count <= 0)
            {
                //怎么可能呢闹呢新建文件吧
                return(null);
            }

            //填充不存在的区块
            int totalLength = 0;

            for (var reverse = reverseList.First; reverse != null; reverse = reverse.Next) //懒得写while
            {
                BuildInstruction prev = null;
                BuildInstruction cur  = reverse.Value.Inst;
                if (reverse.Previous == null) //如果是first 构造一个虚指令
                {
                    prev = new BuildInstruction(BuildType.FromOldFile);
                }
                else
                {
                    prev = reverse.Previous.Value.Inst;
                }
                int newLength = cur.OldFilePosition - (prev.OldFilePosition + prev.Length);
                if (newLength > 0) //如果中间缺失 添加一块原区段
                {
                    reverseList.AddBefore(reverse, new FileReverseInst()
                    {
                        Inst = new BuildInstruction(BuildType.FromPatcher)
                        {
                            Length = newLength
                        },
                        NewFilePosition = cur.OldFilePosition - newLength
                    });
                    totalLength += newLength;
                }
                else if (newLength < 0)
                {
                    throw new Exception("?????");
                }

                totalLength += cur.Length;
            }

            //补充尾部区块
            BuildInstruction last = reverseList.Last.Value.Inst;

            if (last.Type == BuildType.FromOldFile)
            {
                int newLength = oldFileLength - (last.OldFilePosition + last.Length);
                if (newLength > 0)
                {
                    reverseList.AddLast(new FileReverseInst()
                    {
                        Inst = new BuildInstruction(BuildType.FromPatcher)
                        {
                            Length = newLength
                        },
                        NewFilePosition = last.OldFilePosition + last.Length
                    });

                    totalLength += newLength;
                }
                else if (newLength < 0)
                {
                    throw new Exception("?????");
                }
            }

            return(reverseList);
        }