/// <summary> /// LOADER THREAD /// loads the sprites of a given sector to the faceSprites Dictionary /// </summary> /// <param name="sectorID"></param> public static void loadSprites(int sectorID) { int totalStatics = 0; //Just for info int totalStaticsSingle = 0; //Just for info UOReader.FacetSector fs = UOFacetManager.getSector(sectorID); for (int x = 0; x < fs.tiles.Length; ++x) { for (int y = 0; y < fs.tiles[x].Length; ++y) { if (fs.tiles[x][y].staticsCount <= 0) { continue; } for (int i = 0; i < fs.tiles[x][y].statics.Length; ++i) { facetStatic_t st = fs.tiles[x][y].statics[i]; if (!facetSprites.ContainsKey(st.graphic)) { facetSprites.Add(st.graphic, new UOStatic(st.graphic)); totalStaticsSingle++; } totalStatics++; } } } UOConsole.Debug("LOADER: instanced {0} statics, based on {1} uniques.", totalStatics, totalStaticsSingle); return; }
/// <summary> /// Get a FacetSector given an ID. FacetSector contains tile information about current block. /// UOPs related. /// </summary> /// <param name="sector"></param> /// <returns></returns> public static FacetSector getSector(int sector) { //Fast search if (facetSectors.ContainsKey(sector)) { return(facetSectors[sector]); } int currentFacet = 0; //Look for the file in facet hashes string toHash = string.Format("build/sectors/facet_{0:D2}/{1:D8}.bin", currentFacet, sector); ulong hash = HashDictionary.HashFileName(toHash); if (!UOResourceManager.uopHashes.facet0Hashes.ContainsKey(hash)) { UOConsole.Fatal("Cannot find {0} in facet0Hashes", toHash); return(null); } UOResourceManager.uopMapping_t map = UOResourceManager.uopHashes.facet0Hashes[hash]; MythicPackage _uop = new MythicPackage(fileDirectory + "facet0.uop"); byte[] raw = _uop.Blocks[map.block].Files[map.file].Unpack(_uop.FileInfo.FullName); FacetSector fs; using (MemoryStream ms = new MemoryStream(raw)){ using (BinaryReader r = new BinaryReader(ms)) { fs = FacetSector.readSector(r); } } facetSectors[sector] = fs; return(fs); }
//Get a resource given an id // the id is coming from TILEART -> TEXTURES // or TERRAINDEF -> TEXTURES public static UOResource getResource(Tileart tileart) { UOResource resource = null; //WorldArt Texture if (tileart.textures[0].texturePresent == 1) { resource = getResource(tileart.textures[0].texturesArray[0], ShaderTypes.Sprite); } //LegacyTexture if (resource == null && tileart.textures[1].texturePresent == 1) { resource = getLegacyResource(tileart.textures[1].texturesArray[0]); } //EnhancedTexture //Is this really necessary? //Light Texture if (resource != null && tileart.textures[3].texturePresent == 1) { //TODO: light texture load } // if (resource == null) { UOConsole.Fatal("texture is not present {0}", tileart.id); tileart = UOResourceManager.getTileart(1); resource = getResource(tileart.textures[0].texturesArray[0], ShaderTypes.Sprite); } return(resource); }
public static UOResource getLegacyResource(TextureImageInfo tileartTextureInfo) { //FAST search if (legacyTextures.ContainsKey(tileartTextureInfo.textureIDX)) { //TODO: references++ return(legacyTextures[tileartTextureInfo.textureIDX]); } //Get the string from stringDictionary if (tileartTextureInfo.textureIDX >= stringDictionary.count) { UOConsole.Fatal("String {0} not found in dictionary.", tileartTextureInfo.textureIDX); return(null); } string tga = stringDictionary.values[tileartTextureInfo.textureIDX]; //Replace extension int start = (tga.LastIndexOf("\\") == -1) ? 0 : (tga.LastIndexOf("\\") + 1); int end = tga.IndexOf("_"); if (end == -1) { UOConsole.Fatal("no descr in: {0} .. trying with extension", tga); tga = tga.Replace(".tga", ""); end = tga.Length; } //UOConsole.Fatal("{0} {1} {2}", tga, start, end); string toHash = tga.Substring(start, end - start); while (toHash.Length < 8) //Filling the missing zeros { toHash = "0" + toHash; } toHash += ".dds"; toHash = toHash.ToLower(); toHash = "build/tileartlegacy/" + toHash; //Get the file from Texture.uop ulong tehHash = HashDictionary.HashFileName(toHash); if (!uopHashes.legacyTexturesHashes.ContainsKey(tehHash)) { UOConsole.Fatal("string {0} not found in legacyTextureHashes - tga: {1}", toHash, tga); return(null); } uopMapping_t map = uopHashes.legacyTexturesHashes[tehHash]; MythicPackage tex = new MythicPackage(fileDirectory + "LegacyTexture.uop"); byte[] raw = tex.Blocks[map.block].Files[map.file].Unpack(tex.FileInfo.FullName); UOResource res = new UOResource(raw, ShaderTypes.Sprite, true); legacyTextures.Add(tileartTextureInfo.textureIDX, res); return(res); }
//Land tiles does not have tileart TEXTURES section.. we need to extract them from terrain definition public static TextureImageInfo getLandtileTextureID(uint legacyLandtileID) { //Fast search if (landtiles.ContainsKey(legacyLandtileID)) { return(landtiles[legacyLandtileID]); } //Translate the legacy ID to the new pair newID-subtype using legacyterrainMap if (!legacyTerrainMap.ContainsKey(legacyLandtileID)) { UOConsole.Fatal("Cannot find {0} in legacyTerrainMap", legacyLandtileID); return(null); } legacyTerrainMap_t landtileID = legacyTerrainMap[legacyLandtileID]; //Get the file from terrain definition using the newID ulong hash = HashDictionary.HashFileName(string.Format("build/terraindefinition/{0}.bin", landtileID.newID)); if (!uopHashes.terrainHashes.ContainsKey(hash)) { UOConsole.Fatal("Cannot find {0} in terrainHashes", landtileID.newID); return(null); } uopMapping_t pos = uopHashes.terrainHashes[hash]; MythicPackage _uop = new MythicPackage(fileDirectory + "TerrainDefinition.uop"); byte[] raw = _uop.Blocks[pos.block].Files[pos.file].Unpack(_uop.FileInfo.FullName); //Read the loaded terrainDefinition file. TerrainDefinition td; using (MemoryStream ms = new MemoryStream(raw)) { using (BinaryReader r = new BinaryReader(ms)) { td = TerrainDefinition.readTerrainDefinition(r); } } if (td == null) { UOConsole.Fatal("Cannot read terrainDefinition file"); return(null); } landtiles[legacyLandtileID] = td.textures.texturesArray[landtileID.newSubtype]; //Returns the texture according to subtype return(td.textures.texturesArray[landtileID.newSubtype]); }
public static void loaderThread() { while (!isClosing) { if (_sectorsQueue.Count == 0) { UOConsole.Debug("LOADER: thread: waiting.."); sem.WaitOne(); //Halt the loader thread UOConsole.Debug("LOADER: thread: resuming.."); } int sector = _sectorsQueue.Dequeue(); UOConsole.Debug("LOADER: Loading {0}", sector); UOResources.UOFacetManager.loadSprites(sector); UOConsole.Debug("LOADER: Finished {0} - {1} left", sector, _sectorsQueue.Count); } }
public static void Enqueue(int sector) { //UOConsole.Debug("sector {0}", sector); if (alreadyKnown[sector]) { return; //TODO: Change after resource managing has been considered } alreadyKnown[sector] = true; if (!_sectorsQueue.Contains(sector)) { _sectorsQueue.Enqueue(sector); UOConsole.Debug("UPDATE: Queuing {0} ..", sector); if (_sectorsQueue.Count == 1) { sem.Release(); } } }
//Get a tileart given a graphic id public static Tileart getTileart(uint graphic) { //Fast search if (tileartCollection.ContainsKey(graphic)) { return(tileartCollection[graphic]); } //Get the file from tileart using the id ulong hash = HashDictionary.HashFileName(string.Format("build/tileart/{0:D8}.bin", graphic)); if (!uopHashes.tileartHashes.ContainsKey(hash)) { UOConsole.Fatal("Cannot find {0} in tileartHashes", graphic); return(null); } uopMapping_t pos = uopHashes.tileartHashes[hash]; MythicPackage _uop = new MythicPackage(fileDirectory + "tileart.uop"); byte[] raw = _uop.Blocks[pos.block].Files[pos.file].Unpack(_uop.FileInfo.FullName); //Read the loaded tileart file. Tileart t; using (MemoryStream ms = new MemoryStream(raw)) { using (BinaryReader r = new BinaryReader(ms)) { t = Tileart.readTileart(r); } } if (t == null) { UOConsole.Fatal("Cannot read tileart file"); return(t); } tileartCollection[graphic] = t; //Returns the texture according to subtype return(t); }
/// <summary> /// MAIN THREAD /// This function loads a sector and puts the resulting terrain mesh into a GameObject /// </summary> /// <param name="sectorID"></param> /// <returns></returns> public static GameObject buildTerrain(int sectorID) { GameObject terrain = new GameObject("terrain" + sectorID); MeshFilter mf = (MeshFilter)terrain.AddComponent(typeof(MeshFilter)); MeshRenderer mr = (MeshRenderer)terrain.AddComponent(typeof(MeshRenderer)); //MeshCollider mc = (MeshCollider)terrain.AddComponent(typeof(MeshCollider)); //Load a facet sector UOReader.FacetSector sec = UOFacetManager.getSector(sectorID); //Set the data to a mesh Mesh mesh = new Mesh(); UOFacetManager.facetSectorMesh m = UOFacetManager.buildTerrainMesh(sec); //Copy values from the specific class.... i tried using directly a mesh but didn't worked.. mesh.vertices = m.vertices; mesh.triangles = m.triangles; mesh.normals = m.normals; mesh.uv = m.uvs; int i = 0; mesh.subMeshCount = m.subMeshes.Values.Count; UOConsole.Fatal("terrain: {0} subMeshes", mesh.subMeshCount); foreach (uint k in m.subMeshes.Keys) { //UOConsole.Fatal(string.Format("idx {0} texture {1} count: {2}", i, k, m.subMeshes[k].Count)); mesh.SetTriangles(m.subMeshes[k].ToArray(), i++); } mf.mesh = mesh; mr.materials = m.materials.ToArray(); float sq = Mathf.Sqrt(2.0f); terrain.transform.Rotate(0, 0, -45.0f); terrain.transform.localScale *= 1 / sq; terrain.transform.localScale /= 1.6525f; //Moving from 100pixel based to 64pixel based return(terrain); }
/// <summary> /// MAIN THREAD /// load the already cached sprites into GameObjects. /// This function ignores not-yet loaded sprites. /// </summary> public void loadObjects(Position p) { UOReader.FacetSector fs = UOFacetManager.getSector(sectorID); int worldY = (fs.sectorID % 64) * 64; int worldX = (fs.sectorID / 64) * 64; //Check if the block does not exit yet if (statics[fs.sectorID] == null) { statics[fs.sectorID] = new GameObject(fs.sectorID.ToString() + " statics"); statics[fs.sectorID].transform.parent = theMap.transform; terrains[fs.sectorID] = buildTerrain(fs.sectorID); terrains[fs.sectorID].transform.parent = theMap.transform; } bool needsAnotherRun = false; //Wheter the SectorLoader thread has not finished yet. for (int x = 0; x < fs.tiles.Length; ++x) { for (int y = 0; y < fs.tiles[x].Length; ++y) { if (Mathf.Abs(worldX + x - p.x) > UPDATE_RANGE || Mathf.Abs(worldY + y - p.y) > UPDATE_RANGE) { needsAnotherRun = true; continue; } if (fs.tiles[x][y].staticsCount <= 0) { continue; } if (goArray[x, y] == null) { goArray[x, y] = new GameObject[fs.tiles[x][y].statics.Length]; } for (int i = 0; i < fs.tiles[x][y].statics.Length; ++i) { facetStatic_t st = fs.tiles[x][y].statics[i]; if (facetSprites.ContainsKey(st.graphic)) { if (goArray[x, y][i] == null) { UOStatic si = facetSprites[st.graphic] as UOStatic; if (si == null) { facetSprites.Remove(st.graphic); UOConsole.Fatal("UPDATE: Removing {0} cause it's null", st.graphic); } goArray[x, y][i] = si.getDrawItem(x, y, st.z, worldX, worldY); goArray[x, y][i].transform.parent = statics[fs.sectorID].transform; } //Else we already have the tile loaded! } else { //We need another run needsAnotherRun = true; } } //End statics } //End y } //End x if (!needsAnotherRun) { fullLoaded = true; UOConsole.Debug("UPDATE: Finished loading {0}", sectorID); } return; }
private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e) { graphicSector gs = (graphicSector)source; UOConsole.Debug("UPDATE: block {0} is being deallocated.. TODO", gs.sectorID); }
//Preload all hashes public static bool loadUOPs() { //Load the string dictionary data MythicPackage sd = new MythicPackage(fileDirectory + "string_dictionary.uop"); byte[] data = sd.Blocks[0].Files[0].Unpack(sd.FileInfo.FullName); using (MemoryStream fs = new MemoryStream(data)) { using (BinaryReader r = new BinaryReader(fs)) { stringDictionary.unk64 = r.ReadUInt64(); stringDictionary.count = r.ReadUInt32(); stringDictionary.unk16 = r.ReadInt16(); stringDictionary.values = new string[stringDictionary.count]; for (int i = 0; i < stringDictionary.count; ++i) { ushort len = r.ReadUInt16(); byte[] datas = r.ReadBytes(len); stringDictionary.values[i] = UTF8Encoding.ASCII.GetString(datas); } } } //Load the map conversion between new ids and legacy ones using (FileStream fs = new FileStream(fileDirectory + "legacyterrainmap.csv", FileMode.Open)) { using (StreamReader r = new StreamReader(fs)) { string line = r.ReadLine(); //legacy newid newsub while (true) { line = r.ReadLine(); if (line == null) { break; } string[] values = line.Split(','); if (values.Length != 3) { UOConsole.Fatal("cannot read legacyterrainmap.csv"); return(false); } legacyTerrainMap[uint.Parse(values[0])] = new legacyTerrainMap_t(uint.Parse(values[0]), uint.Parse(values[1]), uint.Parse(values[2])); } } } //Now build files dictionaries for fast searching uopHashes.tileartHashes = new Dictionary <ulong, uopMapping_t>(); uopHashes.textureHashes = new Dictionary <ulong, uopMapping_t>(); uopHashes.terrainHashes = new Dictionary <ulong, uopMapping_t>(); uopHashes.facet0Hashes = new Dictionary <ulong, uopMapping_t>(); uopHashes.legacyTexturesHashes = new Dictionary <ulong, uopMapping_t>(); MythicPackage _uop = new MythicPackage(fileDirectory + "tileart.uop"); for (int i = 0; i < _uop.Blocks.Count; ++i) { for (int j = 0; j < _uop.Blocks[i].Files.Count; ++j) { uopHashes.tileartHashes.Add(_uop.Blocks[i].Files[j].FileHash, new uopMapping_t(i, j)); } } _uop = new MythicPackage(fileDirectory + "Texture.uop"); for (int i = 0; i < _uop.Blocks.Count; ++i) { for (int j = 0; j < _uop.Blocks[i].Files.Count; ++j) { uopHashes.textureHashes.Add(_uop.Blocks[i].Files[j].FileHash, new uopMapping_t(i, j)); } } _uop = new MythicPackage(fileDirectory + "TerrainDefinition.uop"); for (int i = 0; i < _uop.Blocks.Count; ++i) { for (int j = 0; j < _uop.Blocks[i].Files.Count; ++j) { uopHashes.terrainHashes.Add(_uop.Blocks[i].Files[j].FileHash, new uopMapping_t(i, j)); } } _uop = new MythicPackage(fileDirectory + "facet0.uop"); for (int i = 0; i < _uop.Blocks.Count; ++i) { for (int j = 0; j < _uop.Blocks[i].Files.Count; ++j) { uopHashes.facet0Hashes.Add(_uop.Blocks[i].Files[j].FileHash, new uopMapping_t(i, j)); } } _uop = new MythicPackage(fileDirectory + "LegacyTexture.uop"); for (int i = 0; i < _uop.Blocks.Count; ++i) { for (int j = 0; j < _uop.Blocks[i].Files.Count; ++j) { uopHashes.legacyTexturesHashes.Add(_uop.Blocks[i].Files[j].FileHash, new uopMapping_t(i, j)); } } return(true); }
//This function should never return null public virtual GameObject getDrawItem(int x, int y, int z, int worldX, int worldY, int drawLayer) { if (resource == null) { return(new GameObject(tileart.id.ToString())); } float width, height; width = _imageOffset.Xend - _imageOffset.Xstart; height = _imageOffset.Yend; // _imageOffset.Ystart; bool flipped = false; //TEMP - to move at load if (width == 0) { string tow = "", tow2 = ""; for (int i = 0; i < tileart.textures[0].texturesCount; ++i) { tow += tileart.textures[0].texturesArray[i].unk6 + " "; tow2 += tileart.textures[0].texturesArray[i].unk7 + " "; } UOConsole.Debug("id {0}, width {1} height {2} unk6 {3} unk7 {4}", tileart.id, width, height, tow, tow2); width = height = 64; flipped = true; } drawSprite = Sprite.Create(resource.getTexture(), new Rect(_imageOffset.Xstart, resource.getTexture().height - _imageOffset.Yend, width, height), new Vector2(0, 0) ); float squareRoot = Mathf.Sqrt(2.0f); float realz = z * 6 / (UOEC_SIZE); x += worldX; y += worldY; float realx = (-y * 0.5f + x * 0.5f) - 0.5f + _drawOffsetX; float realy = (-y * 0.5f - x * 0.5f) - (flipped ? 0.5f : 1f) - _drawOffsetY + realz; GameObject toret = new GameObject(tileart.id.ToString()); //Create a visible object realx /= 1.6525f; //Moving from 100pixel based grid to 64pixel based realy /= 1.6525f; toret.transform.Translate(realx, realy, 0); if (flipped) { toret.transform.Rotate(0, 0, -45.0f); toret.transform.localScale /= squareRoot; } SpriteRenderer r = (SpriteRenderer)toret.AddComponent(typeof(SpriteRenderer)); r.sprite = drawSprite; r.sortingOrder = x + y + z /*- worldY - worldX*/ + drawLayer; return(toret); }