private void CompletedTask() { while (_run) { try { var activeThread = Active; if (activeThread == 0 && _runnerStatus == RunnerStatus.Stopped) { _run = false; _stopwatch.Stop(); _datapool.Clear(); _cts.Dispose(); try { OnStopped?.Invoke(this, new StopEventArgs() { WordList = _wordlist, Save = _save, Log = _log }); } catch (Exception ex) { OnException?.Invoke(this, new ExceptionEventArgs() { Location = "OnStopped", Exception = ex, Log = _log }); } } else if (activeThread == 0) { _run = false; _stopwatch.Stop(); _datapool.Clear(); _cts.Dispose(); _runnerStatus = RunnerStatus.Completed; try { OnCompleted?.Invoke(this, new EventArgs()); } catch (Exception ex) { OnException?.Invoke(this, new ExceptionEventArgs() { Location = "OnCompleted", Exception = ex, Log = _log }); } } } catch { } Thread.Sleep(100); } }
public void Dispose() { if (status != Status.End) { WriteEndTable(); } fields.Clear(); stringPool.Clear(); vldPool.Clear(); if (!settings.LeaveOpen) { destination.Close(); } }
public override void Write(Stream destination) { long headerPosition = destination.Position; uint headerLength = 16; for (uint i = 0; i < headerLength; i++) { destination.WriteByte(0); } while ((destination.Position % 32) != 0) { destination.WriteByte(0); } uint tableLength = (uint)(entries.Count * 16); uint tablePosition = (uint)destination.Position; DataPool vldPool = new DataPool(); foreach (HeroesPacEntry pacEntry in entries) { uint entryPosition = (uint)vldPool.Put(pacEntry.FilePath); DataStream.WriteUInt32(destination, pacEntry.Id); DataStream.WriteUInt32(destination, entryPosition); while ((destination.Position % 16) != 0) { destination.WriteByte(0); } pacEntry.Position = tablePosition + tableLength + entryPosition; } vldPool.Write(destination); vldPool.Clear(); destination.Seek(0, SeekOrigin.Begin); DataStream.WriteUInt32(destination, (uint)entries.Count); DataStream.WriteUInt32(destination, tablePosition); DataStream.WriteUInt32(destination, (uint)vldPool.Length); DataStream.WriteUInt32(destination, (uint)vldPool.Position); }
/// <summary> /// A* to generate path to nearest grey tile /// </summary> private void GetPathToGrey() { Point currentPos = CurrentPos; Fringe.Clear(); Fringe.Add(CurrentPos, null, 0, NearestGrey(currentPos)); Node bestNode = Fringe.GetBestNode(); Point pos = bestNode.Value; Point childPos; Tile[,] map = HugeMap; TileStates state; int width = map.GetLength(0); //while not goal state while ((map[pos.Row, pos.Col].State & GreyTileSign) == 0) { //adding neighbour tiles to Fringe if they are not walls if (pos.Row > 0) { state = map[pos.Row - 1, pos.Col].State; if ((state & TileStates.Wall) == 0 && (state & TileStates.Visited) != 0) { childPos = new Point(pos.Row - 1, pos.Col); Fringe.Add(childPos, bestNode, bestNode.RealCost + 1, NearestGrey(childPos)); } } if (pos.Col < width - 1) { state = map[pos.Row, pos.Col + 1].State; if ((state & TileStates.Wall) == 0 && (state & TileStates.Visited) != 0) { childPos = new Point(pos.Row, pos.Col + 1); Fringe.Add(childPos, bestNode, bestNode.RealCost + 1, NearestGrey(childPos)); } } if (pos.Row < width - 1) { state = map[pos.Row + 1, pos.Col].State; if ((state & TileStates.Wall) == 0 && (state & TileStates.Visited) != 0) { childPos = new Point(pos.Row + 1, pos.Col); Fringe.Add(childPos, bestNode, bestNode.RealCost + 1, NearestGrey(childPos)); } } if (pos.Col > 0) { state = map[pos.Row, pos.Col - 1].State; if ((state & TileStates.Wall) == 0 && (state & TileStates.Visited) != 0) { childPos = new Point(pos.Row, pos.Col - 1); Fringe.Add(childPos, bestNode, bestNode.RealCost + 1, NearestGrey(childPos)); } } bestNode = Fringe.GetBestNode(); pos = bestNode.Value; } //Reverse path Node temp1 = bestNode, temp2 = bestNode.Parent, temp3; while (temp2.Parent != null) { temp3 = temp2.Parent; temp2.Parent = temp1; temp1 = temp2; temp2 = temp3; } temp2.Parent = temp1; bestNode.Parent = null; GoingToGrey = temp1; }
public override void Write(Stream destination) { long GetHeaderLength(uint idFieldLen, uint positionFieldLen) { return(16 + (idFieldLen * entries.Count) + (positionFieldLen * entries.Count) + positionFieldLen); } long Calculate(out uint idFieldLen, out uint positionFieldLen) { // It's kind of impossible to have more than 65535 waveforms in one ACB, but just to make sure. idFieldLen = entries.Count <= ushort.MaxValue ? 2u : 4u; long dataLength = 0; foreach (CriAfs2Entry entry in entries) { dataLength = Helpers.Align(dataLength, Align); dataLength += entry.Length; } positionFieldLen = 2; long headerLen; // Check 2, 4 and 8 if (Helpers.Align((headerLen = GetHeaderLength(idFieldLen, positionFieldLen)), Align) + dataLength > ushort.MaxValue) { positionFieldLen = 4; if (Helpers.Align((headerLen = GetHeaderLength(idFieldLen, positionFieldLen)), Align) + dataLength > uint.MaxValue) { positionFieldLen = 8; } } return(headerLen); } var mDestination = new MemoryStream(); void WriteByLength(uint length, long value) { switch (length) { case 2: DataStream.WriteUInt16(mDestination, (ushort)value); break; case 4: DataStream.WriteUInt32(mDestination, (uint)value); break; case 8: DataStream.WriteInt64(mDestination, value); break; default: throw new ArgumentException($"Unimplemented field length ({length})", nameof(length)); } } long headerLength = Calculate(out uint idFieldLength, out uint positionFieldLength); DataStream.WriteCString(mDestination, "AFS2", 4); DataStream.WriteUInt32(mDestination, 1 | (idFieldLength << 16) | (positionFieldLength << 8)); DataStream.WriteUInt32(mDestination, (uint)entries.Count); DataStream.WriteUInt32(mDestination, Align); DataPool vldPool = new DataPool(Align, headerLength); vldPool.ProgressChanged += OnProgressChanged; var orderedEntries = entries.OrderBy(entry => entry.Id); foreach (CriAfs2Entry afs2Entry in orderedEntries) { WriteByLength(idFieldLength, afs2Entry.Id); } foreach (CriAfs2Entry afs2Entry in orderedEntries) { long entryPosition = vldPool.Length; vldPool.Put(afs2Entry.FilePath); WriteByLength(positionFieldLength, entryPosition); } WriteByLength(positionFieldLength, vldPool.Length); // Copy the header to Header property and save it to destination Header = mDestination.ToArray(); mDestination.Close(); destination.Write(Header, 0, Header.Length); vldPool.Write(destination); vldPool.Clear(); }