private void DoStatusUpdate(int percentDone, string description,bool async=true) { var action = new Action(() => { Status = new LoadStatus() { PercentDone = percentDone, OperationDescription = description }; }); if (async) { _dispatcher.BeginInvoke(action); } else { _dispatcher.Invoke(action); } }
public void OpenFile(string file,bool getObjects=false,bool useNewTextures=true) { if (File.Exists(file)) { Task.Factory.StartNew(() => { S3D s3d = null; int status = 0; status += 5; DoStatusUpdate(status, "Loading " + file); try { s3d = S3D.Load(file); } catch (Exception e) { DoStatusUpdate(0, "Failed loading s3d: " + e.Message, false); return; } _archive = s3d; var filename = System.IO.Path.GetFileName(file); int period = filename.IndexOf('.', 0); var zone = filename.Substring(0, period); string dir = Path.GetDirectoryName(file); //var archiveFile = s3d.Files.FirstOrDefault(x => x.Name.Contains(zone + ".wld")); //if (archiveFile == null) return; _wld = null; _objectLocations = null; List<WLD> wlds = new List<WLD>(); foreach (var archive in s3d.Files.Where(x => x.Name.Contains(".wld"))) { bool isObjects = archive.Name.Contains("objects.wld"); if (isObjects && !getObjects) continue; status += 20; DoStatusUpdate(status, "Loading " + archive.Name); using (var ms = new MemoryStream(archive.Bytes)) { WLD wld = null; try { wld = WLD.Load(ms,archive.Name); if (useNewTextures) wld.TexturingFormat = WLD.TextureFormat.HighResolution; else wld.TexturingFormat = WLD.TextureFormat.Original; } catch (Exception e) { DoStatusUpdate(0, "Failed loading: " + archive.Name + " " + e.Message, false); return; } wlds.Add(wld); if (archive.Name.Contains(zone + ".wld")) { _wld = wld; if (archive.Name.Contains("_obj") || archive.Name.Contains("_chr")) { _wld.ResolveMeshNames(); } } else if (isObjects) { _objectLocations = wld; if (_objectLocations != null) { _objectLocations.ResolveObjectLocationNames(); } } } } _wlds = wlds; //load up the _obj.s3d WLD string objFile = dir + "\\" + zone + "_obj.s3d"; if (getObjects && File.Exists(objFile)) { status += 10; DoStatusUpdate(status, "Loading " + zone + "_obj.s3d"); var objS3d = S3D.Load(objFile); var archive = objS3d.Files.FirstOrDefault(x => x.Name.Contains(".wld")); if (archive != null) { using (var ms = new MemoryStream(archive.Bytes)) { WLD wld = null; try { wld = WLD.Load(ms,archive.Name); } catch (Exception e) { DoStatusUpdate(0, "Failed loading: " + archive.Name + " " + e.Message, false); return; } wlds.Add(wld); _objects = wld; _objects.Files = objS3d; } if (_objects != null) { _objects.ResolveMeshNames(); } } } if (wlds.Count > 0) { status = 90; DoStatusUpdate(status, "Generating Geometry", false); _wld = _wld == null ? wlds.ElementAt(0) : _wld; } _dispatcher.BeginInvoke((Action)(() => { if (_wld != null) { _wld.Files = s3d; WLDObject = _wld; _wlds = wlds; } Status = new LoadStatus() { PercentDone = 100, OperationDescription = "Done" }; })); }); } }