private bool MapVersionCheck(string path) { if (!File.Exists(_HEIGHT_MAP_PATH)) { return(false); } var file = File.ReadAllBytes(path); BinaryReader reader = new BinaryReader(new MemoryStream(file)); MapVersion version = GameObject.Find("map").GetComponent <MapVersion>(); String final_version = version.GetVersionString(); string version_string = reader.ReadString(); char nl = reader.ReadChar(); if (version_string != final_version || nl != '\n') { reader.Close(); return(false); } return(true); }
/// <summary> /// Takes the sampled height from the terrain and packs/compresses it to a binary file with the format of: /// <height><number_of_times_it_occurs_consecutively>["\n"] /// <4bytes><4bytes><4bytes> /// This can possibly be compressed more by creating a range of height to be included when it writes /// the number of times the height occurs. For example, if height is 1.5 and 1.7 and our range is +- .2.. we /// would combine 1.5 and 1.7 to be 1.5 because they are so close together. /// /// </summary> /// <param name="path"></param> public IEnumerator WriteHeightMap(string path) { MapVersion version = GameObject.Find("map").GetComponent <MapVersion>(); String final_version = version.GetVersionString();; int nEntry = 2 * _mapSize + 2 * EXTENSION; BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read)); // write the version so at least we can verify writer.Write(final_version); writer.Write('\n'); for (int x = 0; x < nEntry; x++) { float temp = 0; float last = 0; int lastcnt = 0; for (int z = 0; z < nEntry; z++) { //Terrain terrain = GetTerrainAtPos(PositionOf(x, z)); temp = GetTerrainHeight(PositionOf(x, z));//terrain.SampleHeight(PositionOf(x, z)); //map[x, z] = (byte)(terrain.SampleHeight(PositionOf(x, z)) > waterHeight ? PLAIN : WATER); if (last == temp || lastcnt == 0) { lastcnt++; } else { writer.Write(last); writer.Write(lastcnt); lastcnt = 1; } last = temp; } writer.Write(temp); writer.Write(lastcnt); writer.Write((int)'\n'); SetPercentComplete(((double)x / (double)_map.GetLength(0)) * 100.0); if ((int)GetPercentComplete() % 2 == 0) { yield return(null); } } writer.Close(); }