public void StartLevel(LevelAddress address) { #if UNITY_EDITOR // In editor? Noice. Reload all levels from file so we can update during runtime! dataManager.ReloadLevels(); #endif StartLevel(dataManager.GetLevelData(address)); }
// ---------------------------------------------------------------- // Initialize // ---------------------------------------------------------------- public PackData(LevelAddress myAddress, PackDataXML packDataXML) //PackCollectionData myCollectionData, // this.myCollectionData = myCollectionData; { this.MyAddress = myAddress; this.PackName = packDataXML.packName; LoadAllLevelDatas(packDataXML); }
// ---------------------------------------------------------------- // Events // ---------------------------------------------------------------- private void OnStartLevel(Level level) { this.currLevel = level; LevelAddress addr = this.currLevel.MyAddress; t_levelName.text = addr.pack + "-" + addr.level; t_packName.text = currLevel.MyPackData == null ? "null" : currLevel.MyPackData.PackName; }
// ---------------------------------------------------------------- // Initialize // ---------------------------------------------------------------- public LevelData(LevelAddress myAddress, LevelDataXML ldxml) { // Basic properties this.myAddress = myAddress; boardData = new BoardData(ldxml); startingZoom = ldxml.zoom; // LOAD up stats! DidCompleteLevel = SaveStorage.GetBool(SaveKeys.DidCompleteLevel(myAddress)); }
public LevelAddress GetLastPlayedLevelAddress() { // Save data? Use it! if (SaveStorage.HasKey(SaveKeys.LastPlayedLevelAddress)) { return(LevelAddress.FromString(SaveStorage.GetString(SaveKeys.LastPlayedLevelAddress))); } // No save data. Default to the first level, I guess. else { return(new LevelAddress(0, 0)); } }
// ---------------------------------------------------------------- // LevelDatas // ---------------------------------------------------------------- /** Makes a LevelData for every level file in our world's levels folder!! */ private void LoadAllLevelDatas(PackDataXML packDataXML) { // Convert the XML to LevelDatas! levelDatas = new List <LevelData>(); for (int i = 0; i < packDataXML.levelDataXMLs.Count; i++) { LevelAddress levelAddress = new LevelAddress(MyAddress.pack, i); LevelData newLD = new LevelData(levelAddress, packDataXML.levelDataXMLs[i]); levelDatas.Add(newLD); } // Update this value now that we've got our datas. UpdateNumLevelsCompleted(); }
public LevelAddress PrevLevelAddress(LevelAddress addr) { if (addr == LevelAddress.zero) { return(addr); } // Safety check; can't go before 0,0. addr.level--; if (addr.level < 0) // Wrap back to previous pack. { addr.pack--; addr.level = GetPackData(addr.pack).NumLevels - 1; } return(addr); }
// ---------------------------------------------------------------- // Doers // ---------------------------------------------------------------- public void OnCompleteLevel(LevelAddress levelAddress) { LevelData levelData = GetLevelData(levelAddress); if (levelData != null) { levelData.SetDidCompleteLevel(true); } else { Debug.LogError("LevelData is null for OnCompleteLevel. Hmm."); } // Hmm. UpdateNumLevelsCompleted(); }
public LevelAddress NextLevelAddress(LevelAddress addr) { PackData pack = GetPackData(addr.pack); if (pack == null) { return(LevelAddress.undefined); } // Safety check. addr.level++; if (addr.level >= pack.NumLevels) // Wrap over to next pack. { addr.pack++; addr.level = 0; } return(addr); }
public void ReadJumpTable(CancellationToken ct) { ct.ThrowIfCancellationRequested(); if (!m_hasHeader) { throw new InvalidOperationException("Cannot read the Jump Table without reading the Header first!"); } // an empty database will have at least 2 pages: the header and the JT if (m_file.Length < checked (m_pageSize << 1)) { throw ParseError("File size ({0}) is too small to be a valid snapshot", m_file.Length); } // the jumptable is always in the last page of the file and is expected to fit nicely // > file size MUST be evenly divible by page size // > then JT offset will be file.Length - pageSize if (m_file.Length % m_pageSize != 0) { throw ParseError("The file size ({0}) is not a multiple of the page size ({1}), which may be a symptom of truncation", m_file.Length, m_pageSize); } var jumpTableStart = m_file.Length - m_pageSize; Contract.Assert(jumpTableStart % m_pageSize == 0); m_dataEnd = jumpTableStart; var reader = m_file.CreateReader(jumpTableStart, m_pageSize); // "JMPT" var signature = reader.ReadFixed32(); // Page Size (repeated) var pageSizeRepeated = (int)reader.ReadFixed32(); // Sequence Number (repeated) var sequenceRepeated = reader.ReadFixed64(); // Database ID (repeated) var uidRepeated = new Uuid128(reader.ReadBytes(16).GetBytes()); // Header CRC (repeated) var headerChecksumRepeated = reader.ReadFixed32(); // Sanity checks if (signature != SnapshotFormat.JUMP_TABLE_MAGIC_NUMBER) { throw ParseError("Last page does not appear to be the Jump Table"); } if (pageSizeRepeated != m_pageSize) { throw ParseError("Page size in Jump Table does not match the header value"); } if (sequenceRepeated != m_sequence) { throw ParseError("Sequence in Jump Table does not match the header value"); } if (uidRepeated != m_uid) { throw ParseError("Database ID in Jump Table does not match the header value"); } if (headerChecksumRepeated != m_headerChecksum) { throw ParseError("Database ID in Jump Table does not match the header value"); } // read the table itself int levels = (int)reader.ReadFixed32(); if (levels < 0 || levels > 32) { throw ParseError("The number of levels in the snapshot does not appear to be valid"); } var table = new LevelAddress[levels]; for (int level = 0; level < levels; level++) { ulong offset = reader.ReadFixed64(); ulong size = reader.ReadFixed64(); // Offset and Size cannot be negative // Empty levels (size == 0) must have a zero offset // Non empty levels (size > 0) must have a non zero offset that is greater than the headerSize if ((size == 0 && offset != 0) || (size > 0 && offset < m_dataStart)) { throw ParseError("Level in Jump Table has invalid size ({0}) or offset ({1})", size, offset); } if (checked (offset + size) > m_dataEnd) { throw ParseError("Level in Jump Table would end after the end of the file"); } table[level].Offset = offset; table[level].Size = size; table[level].PaddedSize = RoundUp(size, m_pageSize); } // end attributes uint attributeCount = reader.ReadFixed32(); if (attributeCount != 0) { throw new NotImplementedException("Footer attributes not yet implemented!"); } // end marker if (reader.ReadFixed32() != uint.MaxValue) { throw ParseError("Jump Table end marker not found"); } // checksum uint actualChecksum = SnapshotFormat.ComputeChecksum(reader.Base, reader.Offset); uint checksum = reader.ReadFixed32(); if (actualChecksum != checksum) { throw ParseError("Jump Table checksum does not match ({0} != {1}). This may be an indication of data corruption", checksum, actualChecksum); } m_jumpTable = table; m_levels = levels; m_hasJumpTable = true; }
public LevelData GetLevelData(LevelAddress address) { return(GetLevelData(address.pack, address.level)); }
// ---------------------------------------------------------------- // Getters // ---------------------------------------------------------------- public PackData GetPackData(LevelAddress address) { return(GetPackData(address.pack)); }
public void ReadJumpTable(CancellationToken ct) { ct.ThrowIfCancellationRequested(); if (!m_hasHeader) { throw new InvalidOperationException("Cannot read the Jump Table without reading the Header first!"); } // an empty database will have at least 2 pages: the header and the JT if (m_file.Length < checked(m_pageSize << 1)) { throw ParseError("File size ({0}) is too small to be a valid snapshot", m_file.Length); } // the jumptable is always in the last page of the file and is expected to fit nicely // > file size MUST be evenly divible by page size // > then JT offset will be file.Length - pageSize if (m_file.Length % m_pageSize != 0) { throw ParseError("The file size ({0}) is not a multiple of the page size ({1}), which may be a symptom of truncation", m_file.Length, m_pageSize); } var jumpTableStart = m_file.Length - m_pageSize; Contract.Assert(jumpTableStart % m_pageSize == 0); m_dataEnd = jumpTableStart; var reader = m_file.CreateReader(jumpTableStart, m_pageSize); // "JMPT" var signature = reader.ReadFixed32(); // Page Size (repeated) var pageSizeRepeated = (int)reader.ReadFixed32(); // Sequence Number (repeated) var sequenceRepeated = reader.ReadFixed64(); // Database ID (repeated) var uidRepeated = new Uuid128(reader.ReadBytes(16).GetBytes()); // Header CRC (repeated) var headerChecksumRepeated = reader.ReadFixed32(); // Sanity checks if (signature != SnapshotFormat.JUMP_TABLE_MAGIC_NUMBER) throw ParseError("Last page does not appear to be the Jump Table"); if (pageSizeRepeated != m_pageSize) throw ParseError("Page size in Jump Table does not match the header value"); if (sequenceRepeated != m_sequence) throw ParseError("Sequence in Jump Table does not match the header value"); if (uidRepeated != m_uid) throw ParseError("Database ID in Jump Table does not match the header value"); if (headerChecksumRepeated != m_headerChecksum) throw ParseError("Database ID in Jump Table does not match the header value"); // read the table itself int levels = (int)reader.ReadFixed32(); if (levels < 0 || levels > 32) throw ParseError("The number of levels in the snapshot does not appear to be valid"); var table = new LevelAddress[levels]; for (int level = 0; level < levels; level++) { ulong offset = reader.ReadFixed64(); ulong size = reader.ReadFixed64(); // Offset and Size cannot be negative // Empty levels (size == 0) must have a zero offset // Non empty levels (size > 0) must have a non zero offset that is greater than the headerSize if ((size == 0 && offset != 0) || (size > 0 && offset < m_dataStart)) throw ParseError("Level in Jump Table has invalid size ({0}) or offset ({1})", size, offset); if (checked(offset + size) > m_dataEnd) throw ParseError("Level in Jump Table would end after the end of the file"); table[level].Offset = offset; table[level].Size = size; table[level].PaddedSize = RoundUp(size, m_pageSize); } // end attributes uint attributeCount = reader.ReadFixed32(); if (attributeCount != 0) throw new NotImplementedException("Footer attributes not yet implemented!"); // end marker if (reader.ReadFixed32() != uint.MaxValue) throw ParseError("Jump Table end marker not found"); // checksum uint actualChecksum = SnapshotFormat.ComputeChecksum(reader.Base, reader.Offset); uint checksum = reader.ReadFixed32(); if (actualChecksum != checksum) throw ParseError("Jump Table checksum does not match ({0} != {1}). This may be an indication of data corruption", checksum, actualChecksum); m_jumpTable = table; m_levels = levels; m_hasJumpTable = true; }
public LevelData GetLevelData(LevelAddress levelAddress) { return(GetLevelData(levelAddress.level)); }
public static string DidCompleteLevel(LevelAddress address) { return("DidCompleteLevel_" + address.ToString()); }