public void GetUserIdsYouRequestedToFollow_MultipleCursorResult_ContainsAllIds() { string query = Guid.NewGuid().ToString(); var ids = new long[] { new Random().Next(), new Random().Next() }; var ids2 = new long[] { new Random().Next(), new Random().Next() }; // Arrange var queryExecutor = CreateFrienshipQueryExecutor(); ArrangeGetUserIdsYouRequestedToFollowQuery(query); IEnumerable <IIdsCursorQueryResultDTO> userIds = new List <IIdsCursorQueryResultDTO> { GenerateIdsCursorQueryResult(ids), GenerateIdsCursorQueryResult(ids2) }; _fakeTwitterAccessor.ArrangeExecuteCursorGETQuery <long, IIdsCursorQueryResultDTO>(query, ids); // Act var result = queryExecutor.GetUserIdsYouRequestedToFollow(TestHelper.GenerateRandomInt()); // Assert Assert.IsTrue(result.All(x => ids.Contains(x) || ids2.Contains(x))); Assert.IsTrue(ids.All(result.Contains)); Assert.IsTrue(ids2.All(result.Contains)); }
public override object Part2() { var moons = Parse(Input).ToList(); var repeatIndexes = new long[3]; var repeats = new[] { new HashSet <string>(), new HashSet <string>(), new HashSet <string>() }; for (long i = 0; i < 100_000_000; i++) { if (repeatIndexes.All(x => x > 0)) { break; } SimulateStep(moons); for (int j = 0; j < 3; j++) { if (repeatIndexes[j] > 0) { continue; } var state = string.Join(";", moons.Select(m => m.GetState(j))); if (repeats[j].Contains(state)) { repeatIndexes[j] = i; } repeats[j].Add(state); } } return(Lcm(repeatIndexes[0], Lcm(repeatIndexes[1], repeatIndexes[2]))); }
public void No_Arg_All_Values_Are_Between_Zero_And_MaxValue() { var longs = new long[Assertion.Amount]; for (var i = 0; i < Assertion.Amount; i++) { longs[i] = LongProvider.Long(); } longs.AssertNotAllValuesAreTheSame(); Assert.True( longs.All(x => x > 0 && x < long.MaxValue), "longs.All(x => x > 0 && x < long.MaxValue)" ); }
public void Inclusive_Min_Arg() { var longs = new long[Assertion.Amount]; const long arg = 100; for (var i = 0; i < Assertion.Amount; i++) { longs[i] = LongProvider.Long(arg, arg); } Assert.True( longs.All(x => x == arg), "longs.All(x => x == arg)" ); }
public void All_Values_Are_Between_Zero_And_Max() { var longs = new long[Assertion.Amount]; const long max = 200; for (var i = 0; i < Assertion.Amount; i++) { longs[i] = LongProvider.Long(max); } longs.AssertNotAllValuesAreTheSame(); Assert.True( longs.All(x => x >= 0 && x < max), "longs.All(x => x > 0 && x < max)" ); }
public void Exclusive_Max_Arg() { var longs = new long[Assertion.Amount]; const long max = 100; const long min = max - 1; for (var i = 0; i < Assertion.Amount; i++) { longs[i] = LongProvider.Long(min, max); } Assert.True( longs.All(x => x == min), "longs.All(x => x == min)" ); }
public void PositiveLong_Max_ReturnBelow() { // # Arrange. var sut = new VacheTache(42); const long Max = 45; var results = new long[Max - 1]; // # Act and Assert. for (int i = 0; i < 1_000_000; i++) { var res = sut.PositiveLong(Max); Assert.IsTrue(1 <= res && res < Max, $"The value {res} was not greater than 0 and <= {Max} at index {i}."); // Keep a tab on how many times each long value was returned. results[res - 1] += 1; } Assert.IsTrue( results.All(r => r >= 1), "If we are iterating that many times it would be strange if not all longs was returns at least once."); }
public void GetUserIdsRequestingFriendship_ReturnsQueryExecutor() { string query = Guid.NewGuid().ToString(); var ids = new long[] { new Random().Next(), new Random().Next() }; // Arrange var queryExecutor = CreateFrienshipQueryExecutor(); ArrangeGetUserIdsRequestingFriendshipQuery(query); IEnumerable <IIdsCursorQueryResultDTO> userIds = new List <IIdsCursorQueryResultDTO> { GenerateIdsCursorQueryResult(ids) }; _fakeTwitterAccessor.ArrangeExecuteCursorGETQuery <long, IIdsCursorQueryResultDTO>(query, ids); // Act var result = queryExecutor.GetUserIdsRequestingFriendship(TestHelper.GenerateRandomInt()); // Assert Assert.IsTrue(result.All(ids.Contains)); Assert.IsTrue(ids.All(result.Contains)); }
private void FillGridMain() { string[] searchTokens = textSearch.Text.ToLower().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); long[] userNums = new long[0]; JobCategory[] jobCats = new JobCategory[0]; JobPhase[] jobPhases = new JobPhase[0]; long[] jobPriorities = new long[0]; if (listBoxUsers.SelectedIndices.Count > 0 && !listBoxUsers.SelectedIndices.Contains(0)) { userNums = listBoxUsers.SelectedIndices.Cast <int>().Select(x => _listUsers[x - 1].UserNum).ToArray(); } if (listBoxCategory.SelectedIndices.Count > 0 && !listBoxCategory.SelectedIndices.Contains(0)) { jobCats = listBoxCategory.SelectedIndices.Cast <int>().Select(x => (JobCategory)(x - 1)).ToArray(); } if (listBoxPhases.SelectedIndices.Count > 0 && !listBoxPhases.SelectedIndices.Contains(0)) { jobPhases = listBoxPhases.SelectedIndices.Cast <int>().Select(x => (JobPhase)(x - 1)).ToArray(); } if (listBoxPriorities.SelectedIndices.Count > 0 && !listBoxPriorities.SelectedIndices.Contains(0)) { jobPriorities = listBoxPriorities.GetListSelected <Def>().Select(x => x.DefNum).ToArray(); } Action actionCloseProgress = ODProgress.Show(ODEventType.Job, typeof(JobEvent), "Getting job data..."); #region Get Missing Data //This entire section will go out to the database and get any data that is unknown based on some of the filters. //The other filters will be applied later via the cached lists. try { List <Job> listJobs = Jobs.GetForSearch(dateFrom.Value, dateTo.Value, jobPhases.ToList(), jobPriorities.ToList(), _listJobsAll.Select(x => x.JobNum).ToList()); Jobs.FillInMemoryLists(listJobs, true); _listJobsAll.AddRange(listJobs); } catch (OutOfMemoryException oome) { actionCloseProgress(); oome.DoNothing(); MsgBox.Show(this, "Not enough memory to complete the search. Please refine search filters."); return; } //Only get the feature request entries that we care about. JobEvent.Fire(ODEventType.Job, "Getting feature request data..."); List <long> listFeatureRequestNums = _listJobsAll.SelectMany(x => x.ListJobLinks) .Where(x => x.LinkType == JobLinkType.Request) .Select(x => x.FKey) .Distinct() .ToList(); //Don't download any feature requests that we already know about. listFeatureRequestNums.RemoveAll(x => x.In(_listFeatureRequestsAll.Select(y => y.FeatReqNum))); if (!listFeatureRequestNums.IsNullOrEmpty()) { _listFeatureRequestsAll.AddRange(FeatureRequests.GetAll(listFeatureRequestNums)); } //Only get the bug entries that we care about. JobEvent.Fire(ODEventType.Job, "Getting bug data..."); List <long> listBugIds = _listJobsAll.SelectMany(x => x.ListJobLinks) .Where(x => x.LinkType == JobLinkType.Bug) .Select(x => x.FKey) .Distinct() .ToList(); //Don't download any bugs that we already know about. listBugIds.RemoveAll(x => x.In(_listBugsAll.Select(y => y.BugId))); if (!listBugIds.IsNullOrEmpty()) { _listBugsAll.AddRange(Bugs.GetMany(listBugIds)); } #endregion JobEvent.Fire(ODEventType.Job, "Filling grid..."); gridMain.BeginUpdate(); gridMain.ListGridColumns.Clear(); gridMain.ListGridColumns.Add(new GridColumn("Job\r\nNum", 50, GridSortingStrategy.AmountParse)); gridMain.ListGridColumns.Add(new GridColumn("Priority", 50, HorizontalAlignment.Center)); gridMain.ListGridColumns.Add(new GridColumn("Phase", 85)); gridMain.ListGridColumns.Add(new GridColumn("Category", 80)); gridMain.ListGridColumns.Add(new GridColumn("Job Title", -1)); gridMain.ListGridColumns.Add(new GridColumn("Version", 80)); gridMain.ListGridColumns.Add(new GridColumn("Est. Version", 80)); gridMain.ListGridColumns.Add(new GridColumn("Expert", 75)); gridMain.ListGridColumns.Add(new GridColumn("Engineer", 75)); gridMain.ListGridColumns.Add(new GridColumn("Est.\r\nHours", 60, GridSortingStrategy.AmountParse)); gridMain.ListGridColumns.Add(new GridColumn("Act.\r\nHours", 60, GridSortingStrategy.AmountParse)); gridMain.ListGridColumns.Add(new GridColumn("Job\r\nMatch", 45, HorizontalAlignment.Center)); gridMain.ListGridColumns.Add(new GridColumn("Bug\r\nMatch", 45, HorizontalAlignment.Center)); gridMain.ListGridColumns.Add(new GridColumn("FR\r\nMatch", 45, HorizontalAlignment.Center)); gridMain.ListGridRows.Clear(); _listJobsFiltered = new List <Job>(); foreach (Job jobCur in _listJobsAll) { if (jobCats.Length > 0 && !jobCats.Contains(jobCur.Category)) { continue; } if (jobPhases.Length > 0 && !jobPhases.Contains(jobCur.PhaseCur)) { continue; } if (jobPriorities.Length > 0 && !jobPriorities.Contains(jobCur.Priority)) { continue; } if (userNums.Length > 0 && !userNums.All(x => Jobs.GetUserNums(jobCur).Contains(x))) { continue; } if (!jobCur.DateTimeEntry.Between(dateFrom.Value, dateTo.Value)) { continue; } bool isJobMatch = false; bool isBugMatch = false; bool isFeatureReqMatch = false; if (searchTokens.Length > 0) { bool addRow = false; List <Bug> listBugs = jobCur.ListJobLinks.FindAll(x => x.LinkType == JobLinkType.Bug) .Select(x => _listBugsAll.FirstOrDefault(y => x.FKey == y.BugId)) .Where(x => x != null) .ToList(); List <FeatureRequest> listFeatures = jobCur.ListJobLinks.FindAll(x => x.LinkType == JobLinkType.Request) .Select(x => _listFeatureRequestsAll.FirstOrDefault(y => x.FKey == y.FeatReqNum)) .Where(x => x != null) .ToList(); foreach (string token in searchTokens.Distinct()) { bool isFound = false; //JOB MATCHES if (jobCur.Title.ToLower().Contains(token) || jobCur.Implementation.ToLower().Contains(token) || jobCur.Requirements.ToLower().Contains(token) || jobCur.Documentation.ToLower().Contains(token) || jobCur.JobNum.ToString().Contains(token)) { isFound = true; isJobMatch = true; } //BUG MATCHES if (!isFound || !isBugMatch) { if (listBugs.Any(x => x.Description.ToLower().Contains(token) || x.Discussion.ToLower().Contains(token))) { isFound = true; isBugMatch = true; } } //FEATURE REQUEST MATCHES if (!isFound || !isFeatureReqMatch) { if (listFeatures.Any(x => x.Description.Contains(token) || x.FeatReqNum.ToString().ToLower().Contains(token))) { isFound = true; isFeatureReqMatch = true; } } addRow = isFound; if (!isFound) { break; //stop looking for additional tokens, we didn't find this one. } } if (!addRow) { continue; //we did not find one of the search terms. } } _listJobsFiltered.Add(jobCur); Def jobPriority = _listJobPriorities.FirstOrDefault(y => y.DefNum == jobCur.Priority); GridRow row = new GridRow(); row.Cells.Add(jobCur.JobNum.ToString()); row.Cells.Add(new GridCell(jobPriority.ItemName) { ColorBackG = jobPriority.ItemColor, ColorText = (jobCur.Priority == _listJobPriorities.FirstOrDefault(y => y.ItemValue.Contains("Urgent")).DefNum) ? Color.White : Color.Black, }); row.Cells.Add(jobCur.PhaseCur.ToString()); row.Cells.Add(jobCur.Category.ToString()); row.Cells.Add(jobCur.Title); row.Cells.Add(jobCur.JobVersion.ToString()); row.Cells.Add(jobCur.ProposedVersion.ToString()); row.Cells.Add(Userods.GetName(jobCur.UserNumExpert)); row.Cells.Add(Userods.GetName(jobCur.UserNumEngineer)); row.Cells.Add(jobCur.HoursEstimate.ToString()); row.Cells.Add(jobCur.HoursActual.ToString()); row.Cells.Add(isJobMatch ? "X" : ""); row.Cells.Add(isBugMatch ? "X" : ""); row.Cells.Add(new GridCell(isFeatureReqMatch ? "X" : "") { ColorBackG = _listFeatureRequestsAll.Count == 0 ? Control.DefaultBackColor : Color.Empty }); row.Tag = jobCur; gridMain.ListGridRows.Add(row); } gridMain.EndUpdate(); actionCloseProgress(); }
public override void Solve(IOManager io) { var n = io.ReadInt(); long totalLength = 0; var sticks = new int[n]; var maxLength = 2 * n - 1; for (int i = 0; i < sticks.Length; i++) { sticks[i] = 2 * i + 1; totalLength += sticks[i]; } var rbtree = new RedBlackTree <Stick>(); if (n % 2 == 0 && n >= 4) { io.WriteLine(n / 2); var left = 0; var right = n - 1; for (int i = 0; i < n / 2; i++) { io.WriteLine($"2 {sticks[left++]} {sticks[right--]}"); } return; } foreach (var div in Divisiors.GetDivisiors(totalLength)) { var eachLength = totalLength / div; if (div == 1 || eachLength < maxLength || sticks.Length % div != 0) { continue; } var groupCount = sticks.Length / div; var lengths = new long[div]; var shift = 0; var results = Enumerable.Repeat(0, (int)div).Select(_ => new List <int>()).ToArray(); for (int group = 0; group < groupCount; group++) { for (int i = 0; i < lengths.Length; i++) { var next = sticks[group * lengths.Length + i]; var index = (i + shift) % lengths.Length; lengths[index] += next; results[index].Add(next); } shift++; } if (lengths.All(l => l == lengths[0])) { io.WriteLine(results.Length); for (int i = 0; i < results.Length; i++) { var line = new int[results[i].Count + 1]; line[0] = results[i].Count; results[i].CopyTo(line, 1); io.WriteLine(line, ' '); } return; } } io.WriteLine("impossible"); }
private void FillGridMain() { if (!_IsSearchReady) { return; } Cursor = Cursors.WaitCursor; gridMain.BeginUpdate(); gridMain.Columns.Clear(); //TODO: change columns gridMain.Columns.Add(new ODGridColumn("Job\r\nNum", 50)); gridMain.Columns.Add(new ODGridColumn("Phase", 85)); gridMain.Columns.Add(new ODGridColumn("Category", 80)); gridMain.Columns.Add(new ODGridColumn("Job Title", 300)); gridMain.Columns.Add(new ODGridColumn("Version", 80)); gridMain.Columns.Add(new ODGridColumn("Expert", 75)); gridMain.Columns.Add(new ODGridColumn("Engineer", 75)); gridMain.Columns.Add(new ODGridColumn("Job\r\nMatch", 45) { TextAlign = HorizontalAlignment.Center }); gridMain.Columns.Add(new ODGridColumn("Bug\r\nMatch", 45) { TextAlign = HorizontalAlignment.Center }); gridMain.Columns.Add(new ODGridColumn("Feature\r\nRequest\r\nMatch", 45) { TextAlign = HorizontalAlignment.Center }); gridMain.Rows.Clear(); string[] searchTokens = textSearch.Text.ToLower().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); _listJobsFiltered = new List <Job>(); long[] userNums = new long[0]; JobCategory[] jobCats = new JobCategory[0]; JobPhase[] jobPhases = new JobPhase[0]; if (listBoxUsers.SelectedIndices.Count > 0 && !listBoxUsers.SelectedIndices.Contains(0)) { userNums = listBoxUsers.SelectedIndices.Cast <int>().Select(x => _listUsers[x - 1].UserNum).ToArray(); } if (listBoxCategory.SelectedIndices.Count > 0 && !listBoxCategory.SelectedIndices.Contains(0)) { jobCats = listBoxCategory.SelectedIndices.Cast <int>().Select(x => (JobCategory)(x - 1)).ToArray(); } if (listBoxStatus.SelectedIndices.Count > 0 && !listBoxStatus.SelectedIndices.Contains(0)) { jobPhases = listBoxStatus.SelectedIndices.Cast <int>().Select(x => (JobPhase)(x - 1)).ToArray(); } foreach (Job jobCur in _listJobsAll) { if (jobCats.Length > 0 && !jobCats.Contains(jobCur.Category)) { continue; } if (jobPhases.Length > 0 && !jobPhases.Contains(jobCur.PhaseCur)) { continue; } if (userNums.Length > 0 && !userNums.All(x => Jobs.GetUserNums(jobCur).Contains(x))) { continue; } bool isJobMatch = false; bool isBugMatch = false; bool isFeatureReqMatch = false; if (searchTokens.Length > 0) { bool addRow = false; List <Bug> listBugs = jobCur.ListJobLinks.FindAll(x => x.LinkType == JobLinkType.Bug).Select(x => _listBugsAll.FirstOrDefault(y => x.FKey == y.BugId)).Where(x => x != null).ToList(); List <FeatureRequest> listFeatures = jobCur.ListJobLinks.FindAll(x => x.LinkType == JobLinkType.Request).Select(x => _listFeatureRequestsAll.FirstOrDefault(y => x.FKey == y.FeatReqNum)).Where(x => x != null).ToList(); foreach (string token in searchTokens.Distinct()) { bool isFound = false; //JOB MATCHES if (jobCur.Title.ToLower().Contains(token) || jobCur.Implementation.ToLower().Contains(token) || jobCur.Requirements.ToLower().Contains(token) || jobCur.Documentation.ToLower().Contains(token) || jobCur.JobNum.ToString().Contains(token)) { isFound = true; isJobMatch = true; } //BUG MATCHES if (!isFound || !isBugMatch) { if (listBugs.Any(x => x.Description.ToLower().Contains(token) || x.Discussion.ToLower().Contains(token))) { isFound = true; isBugMatch = true; } } //FEATURE REQUEST MATCHES if (!isFound || !isFeatureReqMatch) { if (listFeatures.Any(x => x.Description.Contains(token) || x.FeatReqNum.ToString().ToLower().Contains(token))) { isFound = true; isFeatureReqMatch = true; } } addRow = isFound; if (!isFound) { break; //stop looking for additional tokens, we didn't find this one. } } if (!addRow) { continue; //we did not find one of the search terms. } } _listJobsFiltered.Add(jobCur); ODGridRow row = new ODGridRow(); row.Cells.Add(jobCur.JobNum.ToString()); row.Cells.Add(jobCur.PhaseCur.ToString()); row.Cells.Add(jobCur.Category.ToString()); row.Cells.Add(jobCur.Title.Left(53, true)); row.Cells.Add(jobCur.JobVersion.ToString()); row.Cells.Add(Userods.GetName(jobCur.UserNumExpert)); row.Cells.Add(Userods.GetName(jobCur.UserNumEngineer)); row.Cells.Add(isJobMatch ? "X" : ""); row.Cells.Add(isBugMatch ? "X" : ""); row.Cells.Add(new ODGridCell(isFeatureReqMatch ? "X" : "") { CellColor = _listFeatureRequestsAll.Count == 0 ? Control.DefaultBackColor : Color.Empty }); row.Tag = jobCur; gridMain.Rows.Add(row); } gridMain.EndUpdate(); Cursor = Cursors.Default; }
/// <summary> /// Look for leaks. /// </summary> /// <returns>String with pass/fail message.</returns> public static string TestLeaks() { var locks = new Dictionary <IntPtr, int>(); int lockCount = 0; var badLocks = new List <int>(); int badUnlockCount = 0; SecureArrayCall secureArrayCall = new SecureArrayCall( SecureArray.DefaultCall.ZeroMemory, (m, l) => { string ret = SecureArray.DefaultCall.LockMemory(m, l); if (ret == null) { lock (locks) { ++lockCount; if (locks.ContainsKey(m)) { badLocks.Add(lockCount); } else { locks.Add(m, lockCount); } } } return(ret); }, (m, l) => { lock (locks) { if (locks.ContainsKey(m)) { locks.Remove(m); SecureArray.DefaultCall.UnlockMemory(m, l); } else { ++badUnlockCount; } } }); var hashString = "$argon2i$v=19$m=65536,t=3,p=1$M2f6+jnVc4dyL3BfMQRzoA==$jO/fOrgqxX90XDVhiYZgIVJJcw0lzIXtRFRCEggXYV8="; var password = "******"; const int maxIteration = 10; var memoryDiff = new long[maxIteration]; for (int i = 0; i < maxIteration; i++) { Console.WriteLine($"TestLeaks: Iteration {i + 1} of {maxIteration}"); var prevTotalMemory = GC.GetTotalMemory(true); Argon2.Verify(hashString, password, secureArrayCall); var postTotalMemory = GC.GetTotalMemory(true); memoryDiff[i] = postTotalMemory - prevTotalMemory; } var errs = new List <string>(); if (memoryDiff.All(v => v > 0)) { errs.Add($"Leaked {memoryDiff.Min()} bytes"); } if (badLocks.Any()) { errs.Add($"{badLocks.Count} bad locks: [{string.Join(", ", badLocks.Select(l => $"{l}"))}]."); } if (badUnlockCount > 0) { errs.Add($"{badUnlockCount} bad unlocks."); } if (locks.Any()) { errs.Add($"Leaked {locks.Count} locks: addresses=[{string.Join(", ", locks.Keys.Select(k => $"0x{k.ToInt64():x8}"))}], lock index=[{string.Join(", ", locks.Keys.Select(k => $"{locks[k]}"))}]."); } return(errs.Any() ? $"Leaks: FAILED: {string.Join(" ", errs)}" : "Leaks: Passed"); }
public Bsp(Stream stream, IEnumerable <Package.Wad> wads) { if (!stream.CanSeek || !stream.CanRead) { throw new ArgumentException("stream must be seekable and readable.", "stream"); } if (wads == null) { wads = System.Linq.Enumerable.Empty <Package.Wad>(); } Reader = new Ibasa.IO.BinaryReader(stream, Encoding.ASCII); if (Reader.ReadUInt32() != 30) { throw new InvalidDataException("Version is not 30."); } long entitiesOffset = Reader.ReadUInt32(); long entitiesCount = Reader.ReadUInt32(); long planesOffset = Reader.ReadUInt32(); long planesCount = Reader.ReadUInt32(); long texturesOffset = Reader.ReadUInt32(); long texturesCount = Reader.ReadUInt32(); long vertexesOffset = Reader.ReadUInt32(); long vertexesCount = Reader.ReadUInt32(); long visibilityOffset = Reader.ReadUInt32(); long visibilityCount = Reader.ReadUInt32(); long nodesOffset = Reader.ReadUInt32(); long nodesCount = Reader.ReadUInt32(); long texinfoOffset = Reader.ReadUInt32(); long texinfoCount = Reader.ReadUInt32(); long facesOffset = Reader.ReadUInt32(); long facesCount = Reader.ReadUInt32(); long lightingOffset = Reader.ReadUInt32(); long lightingCount = Reader.ReadUInt32(); long clipnodesOffset = Reader.ReadUInt32(); long clipnodesCount = Reader.ReadUInt32(); long leafsOffset = Reader.ReadUInt32(); long leafsCount = Reader.ReadUInt32(); long marksurfacesOffset = Reader.ReadUInt32(); long marksurfacesCount = Reader.ReadUInt32(); long edgesOffset = Reader.ReadUInt32(); long edgesCount = Reader.ReadUInt32(); long surfedgesOffset = Reader.ReadUInt32(); long surfedgesCount = Reader.ReadUInt32(); long modelsOffset = Reader.ReadUInt32(); long modelsCount = Reader.ReadUInt32(); //Entities Reader.Seek(entitiesOffset, SeekOrigin.Begin); Entities = Encoding.ASCII.GetString(Reader.ReadBytes((int)entitiesCount)); //Planes Reader.Seek(planesOffset, SeekOrigin.Begin); Planes = new Planef[planesCount / 20]; for (int i = 0; i < Planes.Length; ++i) { Planes[i] = new Planef( Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle()); uint type = Reader.ReadUInt32(); } //Textures { Reader.Seek(texturesOffset, SeekOrigin.Begin); long[] offsets = new long[Reader.ReadUInt32()]; for (int i = 0; i < offsets.Length; ++i) { offsets[i] = Reader.ReadUInt32(); } Textures = new Resource[offsets.Length]; for (int i = 0; i < offsets.Length; ++i) { Reader.Seek(texturesOffset + offsets[i], SeekOrigin.Begin); string name = Encoding.ASCII.GetString(Reader.ReadBytes(16)); int nullbyte = name.IndexOf('\0'); name = nullbyte == -1 ? name : name.Substring(0, nullbyte); int width = Reader.ReadInt32(); int height = Reader.ReadInt32(); long[] dataoffsets = new long[4]; dataoffsets[0] = Reader.ReadUInt32(); dataoffsets[1] = Reader.ReadUInt32(); dataoffsets[2] = Reader.ReadUInt32(); dataoffsets[3] = Reader.ReadUInt32(); Resource resource = null; if (dataoffsets.All(o => o == 0)) { foreach (var wad in wads) { resource = wad[name]; if (resource != null) { break; } } } else { resource = new Resource(new Size3i(width, height, 1), 4, 1, Format.R8G8B8A8UNorm); byte[][] images = new byte[4][]; for (int mipSlice = 0; mipSlice < 4; ++mipSlice) { Reader.Seek(texturesOffset + offsets[i] + dataoffsets[mipSlice], SeekOrigin.Begin); images[mipSlice] = Reader.ReadBytes(width * height); width >>= 1; height >>= 1; } Reader.Seek(2, SeekOrigin.Current); byte[] pallet = Reader.ReadBytes(256 * 3); for (int mipSlice = 0; mipSlice < 4; ++mipSlice) { byte[] data = resource[mipSlice, 0]; byte[] image = images[mipSlice]; for (int j = 0; j < image.Length; ++j) { int palletIndex = image[j] * 3; int dataIndex = j * 3; data[dataIndex + 0] = pallet[palletIndex + 0]; data[dataIndex + 1] = pallet[palletIndex + 1]; data[dataIndex + 2] = pallet[palletIndex + 2]; } } } Textures[i] = resource; } } //Vertices Reader.Seek(vertexesOffset, SeekOrigin.Begin); Vertices = new Vector3f[vertexesCount / 12]; for (int i = 0; i < Vertices.Length; ++i) { Vertices[i] = new Vector3f(Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle()); } //Visibility Reader.Seek(visibilityOffset, SeekOrigin.Begin); Visibility = Reader.ReadBytes((int)visibilityCount); //Nodes //Surfaces Reader.Seek(texinfoOffset, SeekOrigin.Begin); Surfaces = new Surface[texinfoCount / 24]; for (int i = 0; i < Surfaces.Length; ++i) { Surfaces[i] = new Surface( new Vector4f(Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle()), new Vector4f(Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle(), Reader.ReadSingle()), Reader.ReadInt32(), Reader.ReadInt32()); } //Faces Reader.Seek(facesOffset, SeekOrigin.Begin); Faces = new Face[facesCount / 20]; for (int i = 0; i < Faces.Length; ++i) { Faces[i] = new Face( Reader.ReadUInt16(), Reader.ReadInt16(), Reader.ReadInt32(), Reader.ReadInt16(), Reader.ReadInt16(), Reader.ReadByte(), Reader.ReadByte(), Reader.ReadByte(), Reader.ReadByte(), Reader.ReadInt32()); } //Lighting Reader.Seek(facesOffset, SeekOrigin.Begin); Lighting = Reader.ReadBytes((int)lightingCount); //#define clipnodes 9 //#define leafs 10 //#define marksurfaces 11 //#define edges 12 //#define surfedges 13 //#define models 14 //Edges Reader.Seek(edgesOffset, SeekOrigin.Begin); Edges = new Edge[edgesCount / 4]; for (int i = 0; i < Edges.Length; ++i) { Edges[i] = new Edge(Reader.ReadUInt16(), Reader.ReadUInt16()); } //SurfaceEdges Reader.Seek(surfedgesOffset, SeekOrigin.Begin); SurfaceEdges = new int[surfedgesCount / 4]; for (int i = 0; i < Planes.Length; ++i) { SurfaceEdges[i] = Reader.ReadInt32(); } }