diff --git a/Mafia2Libs/Core/IO/FileFactory.cs b/Mafia2Libs/Core/IO/FileFactory.cs index 5a63c618..b55f3936 100644 --- a/Mafia2Libs/Core/IO/FileFactory.cs +++ b/Mafia2Libs/Core/IO/FileFactory.cs @@ -35,6 +35,8 @@ public static FileBase ConstructFromFileInfo(FileInfo info) break; case "FR": return new FileFrameResource(info); + case "MES": + return new MapEditorSession(info); case "MTL": return new FileMaterialLibrary(info); case "LUA": diff --git a/Mafia2Libs/Core/IO/FileMapEditorSession.cs b/Mafia2Libs/Core/IO/FileMapEditorSession.cs new file mode 100644 index 00000000..4833609b --- /dev/null +++ b/Mafia2Libs/Core/IO/FileMapEditorSession.cs @@ -0,0 +1,62 @@ +using Mafia2Tool; +using System; +using System.IO; + +namespace Core.IO +{ + public class MapEditorSession : FileBase + { + private bool bForceBigEndian; + + public MapEditorSession(FileInfo info) : base(info) + { + } + + public override bool Open() + { + //make sure to load materials. + MaterialData.Load(); + + //we now build scene data from GameExplorer rather than d3d viewer. + string[] scenepaths = ReadMESScenePaths(); + + MapEditor d3dForm = new MapEditor(file,scenepaths); + d3dForm.Dispose(); + return true; + } + + public override void Save() + { + throw new NotImplementedException(); + } + + public override string GetExtensionUpper() + { + return "MES"; + } + + private string[] ReadMESScenePaths() + { + // Otevření streamu pro čtení souboru + byte[] fileData = File.ReadAllBytes(file.FullName); + using (var ms = new MemoryStream(fileData)) + using (var reader = new BinaryReader(ms)) + { + int version = reader.ReadInt32(); + int numberOfScenes = reader.ReadInt32(); + + string[] scenePaths = new string[numberOfScenes]; + + for (int i = 0; i < numberOfScenes; i++) + { + int stringLength = reader.ReadInt32(); + + scenePaths[i] = new string(reader.ReadChars(stringLength)); + } + + return scenePaths; + } + } + + } +} diff --git a/Mafia2Libs/Forms/Docking/DockSceneTree.cs b/Mafia2Libs/Forms/Docking/DockSceneTree.cs index a59ec050..35aa57fb 100644 --- a/Mafia2Libs/Forms/Docking/DockSceneTree.cs +++ b/Mafia2Libs/Forms/Docking/DockSceneTree.cs @@ -556,6 +556,21 @@ public bool ObjectGroupHasObject(TreeNode ogNode, ulong frameRefHash) } return false; } + + public TreeNode GetSceneDataTreeNode(string ScenePath) + { + + for (int i = 0; i < TreeView_Explorer.Nodes.Count; i++) + { + if (TreeView_Explorer.Nodes[i].Text.Equals(ScenePath)) + { + return TreeView_Explorer.Nodes[i]; + } + } + + // We have failed, return null. + return null; + } } } public class TreeViewDragEventArgs : EventArgs diff --git a/Mafia2Libs/Forms/MapEditor.Designer.cs b/Mafia2Libs/Forms/MapEditor.Designer.cs index e160c605..78468576 100644 --- a/Mafia2Libs/Forms/MapEditor.Designer.cs +++ b/Mafia2Libs/Forms/MapEditor.Designer.cs @@ -45,9 +45,11 @@ private void InitializeComponent() ToolbarStrip = new System.Windows.Forms.ToolStrip(); FileButton = new System.Windows.Forms.ToolStripDropDownButton(); SaveButton = new System.Windows.Forms.ToolStripMenuItem(); + SaveMapEditorSessionButton = new System.Windows.Forms.ToolStripMenuItem(); ExitButton = new System.Windows.Forms.ToolStripMenuItem(); EditButton = new System.Windows.Forms.ToolStripDropDownButton(); AddButton = new System.Windows.Forms.ToolStripMenuItem(); + LoadFrameResource = new System.Windows.Forms.ToolStripMenuItem(); Button_ImportFrame = new System.Windows.Forms.ToolStripMenuItem(); Button_ImportBundle = new System.Windows.Forms.ToolStripMenuItem(); AddSceneFolderButton = new System.Windows.Forms.ToolStripMenuItem(); @@ -205,7 +207,7 @@ private void InitializeComponent() // FileButton // FileButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; - FileButton.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { SaveButton, ExitButton }); + FileButton.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { SaveButton,SaveMapEditorSessionButton, ExitButton }); FileButton.Image = (System.Drawing.Image)resources.GetObject("FileButton.Image"); FileButton.ImageTransparentColor = System.Drawing.Color.Magenta; FileButton.Name = "FileButton"; @@ -219,6 +221,13 @@ private void InitializeComponent() SaveButton.Text = "$SAVE"; SaveButton.Click += SaveButton_Click; // + // SaveMapEditorSessionButton + // + SaveMapEditorSessionButton.Name = "SaveMapEditorSessionButton"; + SaveMapEditorSessionButton.Size = new System.Drawing.Size(106, 22); + SaveMapEditorSessionButton.Text = "Save Map Editor Session"; + SaveMapEditorSessionButton.Click += SaveMapEditorSessionButton_Click; + // // ExitButton // ExitButton.Name = "ExitButton"; @@ -229,7 +238,7 @@ private void InitializeComponent() // EditButton // EditButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; - EditButton.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { AddButton, Button_ImportFrame, Button_ImportBundle, AddSceneFolderButton }); + EditButton.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { LoadFrameResource,AddButton, Button_ImportFrame, Button_ImportBundle, AddSceneFolderButton }); EditButton.Image = (System.Drawing.Image)resources.GetObject("EditButton.Image"); EditButton.ImageTransparentColor = System.Drawing.Color.Magenta; EditButton.Name = "EditButton"; @@ -243,6 +252,13 @@ private void InitializeComponent() AddButton.Text = "$ADD"; AddButton.Click += AddButtonOnClick; // + // LoadFrameResource + // + LoadFrameResource.Name = "LoadFrameResource"; + LoadFrameResource.Size = new System.Drawing.Size(191, 22); + LoadFrameResource.Text = "Load FrameResource"; + LoadFrameResource.Click += LoadFrameResource_Click; + // // Button_ImportFrame // Button_ImportFrame.Name = "Button_ImportFrame"; @@ -470,12 +486,14 @@ private void InitializeComponent() private System.Windows.Forms.ToolStrip ToolbarStrip; private System.Windows.Forms.ToolStripDropDownButton FileButton; private System.Windows.Forms.ToolStripMenuItem SaveButton; + private System.Windows.Forms.ToolStripMenuItem SaveMapEditorSessionButton; private System.Windows.Forms.ToolStripMenuItem ExitButton; private System.Windows.Forms.ToolStripDropDownButton WindowButton; private System.Windows.Forms.ToolStripDropDownButton OptionsButton; private System.Windows.Forms.Panel RenderPanel; private System.Windows.Forms.ToolStripDropDownButton EditButton; private System.Windows.Forms.ToolStripMenuItem AddButton; + private System.Windows.Forms.ToolStripMenuItem LoadFrameResource; private System.Windows.Forms.OpenFileDialog MeshBrowser; private System.Windows.Forms.ImageList imageList1; private System.Windows.Forms.ToolStripMenuItem SceneTreeButton; diff --git a/Mafia2Libs/Forms/MapEditor.cs b/Mafia2Libs/Forms/MapEditor.cs index 5a0b4177..054321e5 100644 --- a/Mafia2Libs/Forms/MapEditor.cs +++ b/Mafia2Libs/Forms/MapEditor.cs @@ -32,6 +32,7 @@ using Utils.VorticeUtils; using Vortice.Mathematics; using WeifenLuo.WinFormsUI.Docking; +using ZLibNet; using static ResourceTypes.Collisions.Collision; using static UnluacNET.TableLiteral; using Collision = ResourceTypes.Collisions.Collision; @@ -42,6 +43,7 @@ namespace Mafia2Tool public partial class MapEditor : Form { private SceneData SceneData = new SceneData(); + private List sceneDatas = new List(); private SceneData ImportedScene; private InputClass Input { get; set; } private GraphicsClass Graphics { get; set; } @@ -79,7 +81,10 @@ public partial class MapEditor : Form public MapEditor(FileInfo info,SceneData sceneData) { SceneData = sceneData; - TextureLoader.ScenePath = SceneData.ScenePath; + TextureLoader.ScenePaths.Add(SceneData.ScenePath);//todo: probably make list of scenepaths there + SceneDataContainer sdc = new SceneDataContainer(); + sdc.SceneData = sceneData; + sceneDatas.Add(sdc); InitializeComponent(); Localise(); @@ -101,6 +106,30 @@ public MapEditor(FileInfo info,SceneData sceneData) SwitchMode(true); StartD3DPanel(); } + + public MapEditor(FileInfo info,String[] ScenePaths) + { + + InitializeComponent(); + Localise(); + + //sceneData.FrameResource.OnFrameRemoved += OnFrameRemoved; ??? + + if (MaterialsManager.MaterialLibraries.Count == 0) + { + MessageBox.Show("No material libraries have loaded, make sure they are set up correctly in the options window!", "Warning!", MessageBoxButtons.OK); + } + + ToolkitSettings.UpdateRichPresence(string.Format("Editing '{0}'", info.Directory.Name)); + fileLocation = info; + InitDockingControls(); + NamesAndDuplicationStore = new Dictionary(); + CameraSpeedTool.Value = (decimal)ToolkitSettings.CameraSpeed; + KeyPreview = true; + Text += " -" + info.Directory.Name; + SwitchMode(true); + StartD3DPanel(ScenePaths); + } private void Localise() { @@ -238,7 +267,7 @@ private void LinkToActor_Click(object sender, EventArgs e) SceneData.Actors = new Actor[0]; SceneData.CreateNewActor(); - LoadActorFiles(); + LoadActorFiles(new SceneDataContainer(),null);//todo } // Should have atleast one file, try to link actors. @@ -370,6 +399,10 @@ private void UpdateAssetVisualisation(TreeNode node, TreeNode parent) Graphics.SetTranslokatorGridEnabled(trGridIndex, enabled); } } + else + { + Graphics.SetAssetVisibility(refID, node.Checked && node.CheckIfParentsAreValid()); + } } else { @@ -395,10 +428,15 @@ private void OutlinerAfterCheck(object sender, TreeViewEventArgs e) public void PopulateList() { - TreeNode tree = SceneData.FrameResource.BuildTree(SceneData.FrameNameTable); - tree.Tag = SceneData.FrameResource.Header; + SceneDataContainer sdc = sceneDatas[sceneDatas.Count - 1]; + TreeNode scenetree = new TreeNode(); + scenetree.Text = sdc.SceneData.ScenePath; + scenetree.Tag = sdc; + TreeNode tree = sdc.SceneData.FrameResource.BuildTree(SceneData.FrameNameTable); + tree.Tag = sdc.SceneData.FrameResource.Header; frameResourceRoot = tree; - dSceneTree.AddToTree(tree); + dSceneTree.AddToTree(scenetree); + dSceneTree.AddToTree(tree,scenetree); } public void PopulateImportedData(string ImportedFilename) @@ -425,11 +463,20 @@ public void InitImportTree() public void StartD3DPanel() { - Init(RenderPanel.Handle); + Init(RenderPanel.Handle,false); + Run(); + } + public void StartD3DPanel(string[] scenePaths) + { + Init(RenderPanel.Handle,true); + for (int i = 0; i < scenePaths.Length; i++) + { + LoadFrameResourceFilename(scenePaths[i]); + } Run(); } - public bool Init(IntPtr handle) + public bool Init(IntPtr handle, bool session) { bool result = false; @@ -437,7 +484,10 @@ public bool Init(IntPtr handle) { Graphics = new GraphicsClass(); Graphics.PreInit(handle); - BuildRenderObjects(); + if (!session) + { + BuildRenderObjects(); + } result = Graphics.InitScene(RenderPanel.Width, RenderPanel.Height); } @@ -844,11 +894,15 @@ private IRenderer BuildRenderObjectFromFrame(FrameObjectBase fObject,Dictionary< private void BuildRenderObjects() { + SceneDataContainer sdc = sceneDatas[sceneDatas.Count - 1]; + + TreeNode sceneNode = dSceneTree.GetSceneDataTreeNode(sdc.SceneData.ScenePath); + SceneData sceneData = sdc.SceneData; Dictionary assets = new Dictionary(); - if (SceneData.FrameResource != null && SceneData.FrameNameTable != null) + if (sceneData.FrameResource != null && sceneData.FrameNameTable != null) { - foreach(FrameObjectBase FrameObject in SceneData.FrameResource.FrameObjects.Values) + foreach(FrameObjectBase FrameObject in sceneData.FrameResource.FrameObjects.Values) { IRenderer NewAsset = BuildRenderObjectFromFrame(FrameObject,assets); if(NewAsset != null) @@ -857,23 +911,25 @@ private void BuildRenderObjects() } } } - if (SceneData.roadMap != null && ToolkitSettings.Experimental) + if (sceneData.roadMap != null && ToolkitSettings.Experimental) { TreeNode node = new TreeNode("Road Data"); TreeNode node2 = new TreeNode("Junction Data"); node.Tag = node2.Tag = "Folder"; + sdc.roadRoot = node; + sdc.junctionRoot = node2; roadRoot = node; junctionRoot = node2; - for (int i = 0; i < SceneData.roadMap.Roads.Count; i++) + for (int i = 0; i < sceneData.roadMap.Roads.Count; i++) { - IRoadDefinition RoadDef = SceneData.roadMap.Roads[i]; + IRoadDefinition RoadDef = sceneData.roadMap.Roads[i]; if(RoadDef.Direction == RoadDirection.Backwards) { continue; } - IRoadSpline RoadSpline = SceneData.roadMap.Splines[RoadDef.RoadSplineIndex]; + IRoadSpline RoadSpline = sceneData.roadMap.Splines[RoadDef.RoadSplineIndex]; RenderRoad road = new RenderRoad(); int generatedID = RefManager.GetNewRefID(); road.Init(RoadDef, RoadSpline); @@ -886,11 +942,11 @@ private void BuildRenderObjects() node.Nodes.Add(child); } - for (int i = 0; i < SceneData.roadMap.Crossroads.Count; i++) + for (int i = 0; i < sceneData.roadMap.Crossroads.Count; i++) { int generatedID = RefManager.GetNewRefID(); RenderJunction junction = new RenderJunction(); - junction.Init(SceneData.roadMap.Crossroads[i], Graphics); + junction.Init(sceneData.roadMap.Crossroads[i], Graphics); assets.Add(generatedID, junction); TreeNode child = new TreeNode(i.ToString()); child.Text = "Junction ID: " + i; @@ -899,23 +955,23 @@ private void BuildRenderObjects() junctionRoot.Nodes.Add(child); } - dSceneTree.AddToTree(node); - dSceneTree.AddToTree(node2); + dSceneTree.AddToTree(node,sceneNode); + dSceneTree.AddToTree(node2,sceneNode); } - if (SceneData.HPDData != null) + if (sceneData.HPDData != null) { int generatedID = RefManager.GetNewRefID(); TreeNode navNode = new TreeNode(); navNode.Text = string.Format("HPD"); navNode.Name = generatedID.ToString(); - for (int i = 0; i < SceneData.HPDData.HPDEntries.Length; i++) + for (int i = 0; i < sceneData.HPDData.HPDEntries.Length; i++) { generatedID = RefManager.GetNewRefID(); TreeNode hpdNode = new TreeNode(); hpdNode.Text = string.Format("NODE: {0}", i); hpdNode.Name = generatedID.ToString(); - var item = SceneData.HPDData.HPDEntries[i]; + var item = sceneData.HPDData.HPDEntries[i]; RenderBoundingBox bbox = new RenderBoundingBox(); BoundingBox box = new BoundingBox(item.BBoxMin, item.BBoxMax); bbox.Init(box); @@ -923,26 +979,27 @@ private void BuildRenderObjects() hpdNode.Tag = box; navNode.Nodes.Add(hpdNode); } - dSceneTree.AddToTree(navNode); + dSceneTree.AddToTree(navNode,sceneNode); } - if (SceneData.OBJData != null && SceneData.OBJData.Length > 0) + if (sceneData.OBJData != null && sceneData.OBJData.Length > 0) { OBJDataRoot = new TreeNode(); OBJDataRoot.Tag = "Folder"; OBJDataRoot.Name = OBJDataRoot.Text = "Navigation: OBJDATA"; + sdc.OBJDataRoot = OBJDataRoot; - var data = new OBJData[SceneData.OBJData.Length]; - for (int i = 0; i < SceneData.OBJData.Length; i++) + var data = new OBJData[sceneData.OBJData.Length]; + for (int i = 0; i < sceneData.OBJData.Length; i++) { - data[i] = (OBJData)SceneData.OBJData[i].Data; + data[i] = (OBJData)sceneData.OBJData[i].Data; } TreeNode Grids = Graphics.SetNavigationGrid(data); OBJDataRoot.Nodes.Add(Grids); - for (int i = 0; i < SceneData.OBJData.Length; i++) + for (int i = 0; i < sceneData.OBJData.Length; i++) { - var obj = (SceneData.OBJData[i].Data as OBJData); + var obj = (sceneData.OBJData[i].Data as OBJData); RenderNav navigationPoints = new RenderNav(Graphics); navigationPoints.Init(obj); @@ -963,38 +1020,40 @@ private void BuildRenderObjects() OBJDataRoot.Nodes.Add(navNode); } - dSceneTree.AddToTree(OBJDataRoot); + dSceneTree.AddToTree(OBJDataRoot,sceneNode); } - if (SceneData.AIWorlds != null && SceneData.AIWorlds.Length > 0) + if (sceneData.AIWorlds != null && sceneData.AIWorlds.Length > 0) { AIWorldRoot = new TreeNode(); AIWorldRoot.Tag = "Folder"; AIWorldRoot.Name = AIWorldRoot.Text = "Navigation: AIWORLD"; + sdc.AIWorldRoot = AIWorldRoot; - var data = new AIWorld[SceneData.AIWorlds.Length]; - for (int i = 0; i < SceneData.AIWorlds.Length; i++) + var data = new AIWorld[sceneData.AIWorlds.Length]; + for (int i = 0; i < sceneData.AIWorlds.Length; i++) { - data[i] = (AIWorld)SceneData.AIWorlds[i].Data; + data[i] = (AIWorld)sceneData.AIWorlds[i].Data; data[i].ConstructRenderable(Graphics); TreeNode AIWorldNode = data[i].PopulateTreeNode(); AIWorldRoot.Nodes.Add(AIWorldNode); } - dSceneTree.AddToTree(AIWorldRoot); + dSceneTree.AddToTree(AIWorldRoot,sceneNode); } if (SceneData.Collisions != null) { TreeNode node = new TreeNode("Collision Data"); node.Tag = "Folder"; collisionRoot = node; + sdc.collisionRoot = node; - for (int i = 0; i != SceneData.Collisions.Models.Count; i++) + for (int i = 0; i != sceneData.Collisions.Models.Count; i++) { - Collision.CollisionModel data = SceneData.Collisions.Models.ElementAt(i).Value; + Collision.CollisionModel data = sceneData.Collisions.Models.ElementAt(i).Value; RenderStaticCollision collision = new RenderStaticCollision(); collision.ConvertCollisionToRender(data.Hash, data.Mesh); - RenderStorageSingleton.Instance.StaticCollisions.Add(SceneData.Collisions.Models.ElementAt(i).Key, collision); + RenderStorageSingleton.Instance.StaticCollisions.Add((sceneData.Collisions.Hash, sceneData.Collisions.Models.ElementAt(i).Key), collision); TreeNode treeNode = new TreeNode(data.Hash.ToString()); treeNode.Text = data.Hash.ToString(); treeNode.Name = data.Hash.ToString(); @@ -1002,16 +1061,16 @@ private void BuildRenderObjects() dSceneTree.AddToTree(treeNode, collisionRoot); } - for (int i = 0; i != SceneData.Collisions.Placements.Count; i++) + for (int i = 0; i != sceneData.Collisions.Placements.Count; i++) { - Collision.Placement placement = SceneData.Collisions.Placements[i]; + Collision.Placement placement = sceneData.Collisions.Placements[i]; TreeNode[] nodes = collisionRoot.Nodes.Find(placement.Hash.ToString(), false); if (nodes.Length > 0) { int refID = RefManager.GetNewRefID(); RenderInstance instance = new RenderInstance(); - instance.Init(RenderStorageSingleton.Instance.StaticCollisions[placement.Hash]); + instance.Init(RenderStorageSingleton.Instance.StaticCollisions[(placement.ParentHash, placement.Hash)]); instance.SetTransform(placement.Transform); TreeNode child = new TreeNode(); child.Text = nodes[0].Nodes.Count.ToString(); @@ -1022,18 +1081,19 @@ private void BuildRenderObjects() } } - dSceneTree.AddToTree(node); + dSceneTree.AddToTree(node,sceneNode); collisionRoot.Collapse(false); } - if(SceneData.ATLoader != null) + if(sceneData.ATLoader != null) { animalTrafficRoot = new TreeNode("Animal Traffic Paths"); animalTrafficRoot.Tag = "Folder"; - for (int i = 0; i < SceneData.ATLoader.Paths.Length; i++) + sdc.animalTrafficRoot = animalTrafficRoot; + for (int i = 0; i < sceneData.ATLoader.Paths.Length; i++) { int refID = RefManager.GetNewRefID(); RenderATP atp = new RenderATP(); - atp.Init(SceneData.ATLoader.Paths[i]); + atp.Init(sceneData.ATLoader.Paths[i]); TreeNode child = new TreeNode(); child.Text = animalTrafficRoot.Nodes.Count.ToString(); child.Name = refID.ToString(); @@ -1041,20 +1101,20 @@ private void BuildRenderObjects() assets.Add(refID, atp); animalTrafficRoot.Nodes.Add(child); } - dSceneTree.AddToTree(animalTrafficRoot); + dSceneTree.AddToTree(animalTrafficRoot,sceneNode); } - if (SceneData.Actors.Length > 0 && ToolkitSettings.Experimental) + if (sceneData.Actors.Length > 0 && ToolkitSettings.Experimental) { - LoadActorFiles(); + LoadActorFiles(sdc,sceneNode); } - for(int i = 0; i < SceneData.FrameNameTable.FrameData.Length; i++) + for(int i = 0; i < sceneData.FrameNameTable.FrameData.Length; i++) { - FrameNameTable.Data data = SceneData.FrameNameTable.FrameData[i]; + FrameNameTable.Data data = sceneData.FrameNameTable.FrameData[i]; if (data.FrameIndex != -1) { - FrameObjectBase frame = (SceneData.FrameResource.FrameObjects.ElementAt(data.FrameIndex).Value as FrameObjectBase); + FrameObjectBase frame = (sceneData.FrameResource.FrameObjects.ElementAt(data.FrameIndex).Value as FrameObjectBase); if (frame != null) { frame.FrameNameTableFlags = data.Flags; @@ -1063,7 +1123,7 @@ private void BuildRenderObjects() } } - foreach (var pair in SceneData.FrameResource.FrameObjects) + foreach (var pair in sceneData.FrameResource.FrameObjects) { FrameObjectBase frame = (pair.Value as FrameObjectBase); if (assets.ContainsKey(frame.RefID)) @@ -1072,19 +1132,20 @@ private void BuildRenderObjects() } } - Graphics.InitObjectStack = assets; + Graphics.InitObjectStack.AddRange(assets); - if (SceneData.Translokator != null && ToolkitSettings.Experimental) + if (sceneData.Translokator != null && ToolkitSettings.Experimental) { ToggleTranslokatorTint.Enabled = true; dSceneTree.hasTranslokatorData = true; translokatorRoot = new TreeNode("Translokator Items"); translokatorRoot.Tag = "Folder"; + sdc.translokatorRoot = translokatorRoot; TreeNode ogNode = new TreeNode("Objects Groups"); ogNode.Tag = "Folder"; - for (int z = 0; z < SceneData.Translokator.ObjectGroups.Length; z++) + for (int z = 0; z < sceneData.Translokator.ObjectGroups.Length; z++) { - ObjectGroup objectGroup = SceneData.Translokator.ObjectGroups[z]; + ObjectGroup objectGroup = sceneData.Translokator.ObjectGroups[z]; TreeNode objectGroupNode = new TreeNode(String.Format("Object Group: [{0}]", objectGroup.ActorType)); objectGroupNode.Tag = objectGroup; for (int y = 0; y < objectGroup.Objects.Length; y++) @@ -1093,7 +1154,7 @@ private void BuildRenderObjects() TreeNode objNode = new TreeNode(obj.Name.ToString()); objNode.Tag = obj; objectGroupNode.Nodes.Add(objNode); - FrameObjectBase groupRef = SceneData.FrameResource.GetObjectByHash(obj.Name.Hash); + FrameObjectBase groupRef = sceneData.FrameResource.GetObjectByHash(obj.Name.Hash); bool hasMesh = false; @@ -1132,9 +1193,9 @@ private void BuildRenderObjects() TreeNode gridNode = new TreeNode("Grids"); gridNode.Tag = "Folder"; - for (int i = 0; i < SceneData.Translokator.Grids.Length; i++) + for (int i = 0; i < sceneData.Translokator.Grids.Length; i++) { - Grid grid = SceneData.Translokator.Grids[i]; + Grid grid = sceneData.Translokator.Grids[i]; TreeNode child = new TreeNode("Grid " + i); child.Tag = grid; child.Checked = false; @@ -1143,8 +1204,8 @@ private void BuildRenderObjects() translokatorRoot.Nodes.Add(gridNode); - dSceneTree.AddToTree(translokatorRoot); - Graphics.BuildTranslokatorGrid(SceneData.Translokator); + dSceneTree.AddToTree(translokatorRoot,sceneNode); + Graphics.BuildTranslokatorGrid(sceneData.Translokator); } } @@ -1259,13 +1320,15 @@ public Matrix4x4 ComputeWorldTransform(Matrix4x4 LocalTransform, Matrix4x4 Paren //ToolkitAssert.Ensure(!worldTransform.IsNaN(), string.Format("Frame: {0} caused NaN()!", name.ToString())); } - private void LoadActorFiles() + private void LoadActorFiles(SceneDataContainer sdc,TreeNode sceneNode) { + SceneData sceneData = sdc.SceneData; actorRoot = new TreeNode("Actor Items"); actorRoot.Tag = "Folder"; - for (int z = 0; z < SceneData.Actors.Length; z++) + sdc.actorRoot = actorRoot; + for (int z = 0; z < sceneData.Actors.Length; z++) { - Actor actor = SceneData.Actors[z]; + Actor actor = sceneData.Actors[z]; TreeNode actorFile = new TreeNode("Actor File " + z); actorFile.Tag = "Folder"; actorRoot.Nodes.Add(actorFile); @@ -1295,7 +1358,7 @@ private void LoadActorFiles() FixActorDefintions(actor); } - dSceneTree.AddToTree(actorRoot); + dSceneTree.AddToTree(actorRoot,sceneNode); } private void TreeViewUpdateSelected() @@ -1884,7 +1947,7 @@ private void DeleteButton_Click(object sender, EventArgs e) Collision.CollisionModel data = (node.Tag as Collision.CollisionModel); SceneData.Collisions.RemoveModel(data); - RenderStorageSingleton.Instance.StaticCollisions.TryRemove(data.Hash); + RenderStorageSingleton.Instance.StaticCollisions.TryRemove((data.ParentHash, data.Hash)); //TODO: Need to get the sceneDataContainer for this for (int i = 0; i != node.Nodes.Count; i++) { @@ -2041,7 +2104,7 @@ private void DuplicateButton_Click(object sender, EventArgs e) dSceneTree.AddToTree(child, node.Parent); RenderInstance instance = new RenderInstance(); - instance.Init(RenderStorageSingleton.Instance.StaticCollisions[placement.Hash]); + instance.Init(RenderStorageSingleton.Instance.StaticCollisions[(placement.ParentHash, placement.Hash)]); instance.SetTransform(placement.Transform); Graphics.InitObjectStack.Add(refID, instance); } @@ -2267,7 +2330,7 @@ private Collision.CollisionModel CreateCollision(Collision.CollisionModel collis // Create a new renderable for collision object RenderStaticCollision collision = new RenderStaticCollision(); collision.ConvertCollisionToRender(collisionModel.Hash, collisionModel.Mesh); - RenderStorageSingleton.Instance.StaticCollisions.TryAdd(collisionModel.Hash, collision); + //RenderStorageSingleton.Instance.StaticCollisions.TryAdd(collisionModel.Hash, collision); //TODO: Need to get the sceneDataContainer for this // Push it onto the collisions dictionary SceneData.Collisions.Models.Add(collisionModel.Hash, collisionModel); @@ -2321,7 +2384,7 @@ private Collision.Placement CreatePlacement(Collision.CollisionModel ColModel, V // Complete it RenderInstance instance = new RenderInstance(); - instance.Init(RenderStorageSingleton.Instance.StaticCollisions[placement.Hash]); + instance.Init(RenderStorageSingleton.Instance.StaticCollisions[(placement.ParentHash, placement.Hash)]); instance.SetTransform(placement.Transform); Graphics.InitObjectStack.Add(refID, instance); SceneData.Collisions.Placements.Add(placement); @@ -2883,6 +2946,99 @@ private void OnFrameRemoved(object sender, OnFrameRemovedArgs e) { Graphics.DeleteAsset(e.FrameRefID); } + + private void LoadFrameResource_Click(object sender, EventArgs e) + { + var Browser = new System.Windows.Forms.OpenFileDialog(); + Browser.Filter = "FrameResource|*.fr"; + if (Browser.ShowDialog() == DialogResult.OK) + { + string Filename = Browser.FileName; + LoadFrameResourceFilename(Filename); + } + } + + private void LoadFrameResourceFilename(string Filename) + { + SceneData sceneData = new SceneData(); + if (Path.GetExtension(Filename).Equals(".fr", StringComparison.OrdinalIgnoreCase)) + { + // loading from map + sceneData.ScenePath = Path.GetDirectoryName(Filename); + } + else + { + // loading from session + sceneData.ScenePath = Filename; + } + sceneData.BuildData(false); + SceneDataContainer sdc = new SceneDataContainer(); + sdc.SceneData = sceneData; + sceneDatas.Add(sdc); + + TextureLoader.ScenePaths.Add(sceneData.ScenePath); + + SceneData = sceneData;//todo wip + + PopulateList(); + BuildRenderObjects(); + } + + private void SaveMapEditorSessionButton_Click(object sender, EventArgs e) + { + if (SaveFileDialog != null) + { + SaveFileDialog.Reset(); + } + //SaveFileDialog.FileName = CurrentBundle.Objects[0].ObjectName; + SaveFileDialog.RestoreDirectory = true; + SaveFileDialog.Filter = "Map Editor Session (*.mes)|*.mes"; + + if (SaveFileDialog.ShowDialog() != DialogResult.OK) + { + return; + } + + // Uložení do zvoleného souboru + using (var fs = new FileStream(SaveFileDialog.FileName, FileMode.Create, FileAccess.Write)) + using (var writer = new BinaryWriter(fs)) + { + // Verze souboru (4 byty) + int version = 1; + writer.Write(version); + + // Počet scén (4 byty) + int scenesCount = sceneDatas.Count; + writer.Write(scenesCount); + + // Zápis jednotlivých cest + for (int i = 0; i < scenesCount; i++) + { + string scenePath = sceneDatas[i].SceneData.ScenePath; + + // Zápis délky stringu (4 byty) + writer.Write(scenePath.Length); + + // Zápis samotného stringu + writer.Write(scenePath.ToCharArray());//todo reword for russians + } + } + } + } + + + public class SceneDataContainer + { + public SceneData SceneData; + public TreeNode frameResourceRoot; + public TreeNode collisionRoot; + public TreeNode roadRoot; + public TreeNode junctionRoot; + public TreeNode animalTrafficRoot; + public TreeNode actorRoot; + public TreeNode AIWorldRoot; + public TreeNode OBJDataRoot; + public TreeNode translokatorRoot; } } diff --git a/Mafia2Libs/Rendering/Graphics/RenderSingleton.cs b/Mafia2Libs/Rendering/Graphics/RenderSingleton.cs index 6f6f6b07..ea57f6d7 100644 --- a/Mafia2Libs/Rendering/Graphics/RenderSingleton.cs +++ b/Mafia2Libs/Rendering/Graphics/RenderSingleton.cs @@ -8,7 +8,7 @@ namespace Rendering.Graphics public sealed class RenderStorageSingleton { public List SplineStorage; - public Dictionary StaticCollisions; + public Dictionary<(ulong colFileHash, ulong colHash), RenderStaticCollision> StaticCollisions; public Dictionary TextureCache; public Dictionary TextureThumbnails; public ShaderManager ShaderManager; @@ -18,7 +18,7 @@ public sealed class RenderStorageSingleton RenderStorageSingleton() { SplineStorage = new List(); - StaticCollisions = new Dictionary(); + StaticCollisions = new(); TextureCache = new Dictionary(); TextureThumbnails = new Dictionary(); ShaderManager = new ShaderManager(); @@ -61,7 +61,7 @@ public void Shutdown() line.Shutdown(); } - foreach (KeyValuePair col in StaticCollisions) + foreach (var col in StaticCollisions) { col.Value.Shutdown(); } diff --git a/Mafia2Libs/Rendering/Graphics/TextureClass.cs b/Mafia2Libs/Rendering/Graphics/TextureClass.cs index 799dcea8..611eed2e 100644 --- a/Mafia2Libs/Rendering/Graphics/TextureClass.cs +++ b/Mafia2Libs/Rendering/Graphics/TextureClass.cs @@ -2,6 +2,7 @@ using Mafia2Tool; using ResourceTypes.Materials; using System; +using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.IO; @@ -14,7 +15,7 @@ namespace Rendering.Graphics { public static class TextureLoader { - public static string ScenePath; + public static List ScenePaths = new List(); private static bool ThumbnailCallback() { return false; @@ -25,25 +26,30 @@ private static string GetTextureFromPath(string fileName, bool bAllowMIPs = true string path = ""; bool bUseMIPs = bAllowMIPs && ToolkitSettings.UseMIPS; - if (!fileName.Contains(".ifl")) + for (int i = 0; i < ScenePaths.Count; i++) { - path = Path.Combine(ScenePath, fileName); - if (File.Exists(path)) + string ScenePath = ScenePaths[i]; + if (!fileName.Contains(".ifl")) { - string mip = Path.Combine(ScenePath, "MIP_" + fileName); - return (File.Exists(mip) && bUseMIPs ? mip : path); - } - - if (!string.IsNullOrEmpty(ToolkitSettings.TexturePath) || Directory.Exists(ToolkitSettings.TexturePath)) - { - path = Path.Combine(ToolkitSettings.TexturePath, fileName); + path = Path.Combine(ScenePath, fileName); if (File.Exists(path)) { - string mip = Path.Combine(ToolkitSettings.TexturePath, "MIP_" + fileName); + string mip = Path.Combine(ScenePath, "MIP_" + fileName); return (File.Exists(mip) && bUseMIPs ? mip : path); } - } + + if (!string.IsNullOrEmpty(ToolkitSettings.TexturePath) || Directory.Exists(ToolkitSettings.TexturePath)) + { + path = Path.Combine(ToolkitSettings.TexturePath, fileName); + if (File.Exists(path)) + { + string mip = Path.Combine(ToolkitSettings.TexturePath, "MIP_" + fileName); + return (File.Exists(mip) && bUseMIPs ? mip : path); + } + } + } } + path = Path.Combine("Resources", "texture.dds"); if (File.Exists(path)) { diff --git a/Mafia2Libs/ResourceTypes/FileTypes/Collisions/Collisions.cs b/Mafia2Libs/ResourceTypes/FileTypes/Collisions/Collisions.cs index af8c2669..c58f6a03 100644 --- a/Mafia2Libs/ResourceTypes/FileTypes/Collisions/Collisions.cs +++ b/Mafia2Libs/ResourceTypes/FileTypes/Collisions/Collisions.cs @@ -1,10 +1,12 @@ -using ResourceTypes.Collisions; +using Gibbed.Illusion.FileFormats.Hashing; +using ResourceTypes.Collisions; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Numerics; +using UnluacNET; using Utils.Helpers; using Utils.Logging; using Utils.VorticeUtils; @@ -17,6 +19,7 @@ public class Collision private const int Version = 0x11; // 17 public string Name { get; set; } + public ulong Hash { get => FNV32.Hash(Name) | (((ulong)GetHashCode()) << 32); } /// /// Platform (== 0 on PC/Mac, == 1 on XBox360, == 2 on PS3) /// @@ -55,14 +58,14 @@ public void ReadFromFile(BinaryReader reader) int numPlacements = reader.ReadInt32(); Placements = new List(numPlacements); for (int i = 0; i < numPlacements; i++) - Placements.Add(new Placement(reader)); + Placements.Add(new Placement(reader, Hash)); int numModels = reader.ReadInt32(); Models = new SortedDictionary(); for (int i = 0; i < numModels; i++) { - CollisionModel model = new CollisionModel(reader); + CollisionModel model = new CollisionModel(reader, Hash); Models.Add(model.Hash, model); } } @@ -186,6 +189,7 @@ public class Placement public Vector3 Rotation { get; set; } + public ulong ParentHash { get; set; } public ulong Hash { get; set; } public int Unk4 { get; set; } public byte Unk5 { get; set; } @@ -231,13 +235,15 @@ public Matrix4x4 Transform { } - public Placement(BinaryReader reader) + public Placement(BinaryReader reader, ulong _ParentHash) { + ParentHash = _ParentHash; ReadFromFile(reader); } public Placement() { + ParentHash = 0; Unk5 = 128; Unk4 = -1; Position = new Vector3(0, 0, 0); @@ -246,6 +252,7 @@ public Placement() public Placement(Placement other) { + ParentHash = other.ParentHash; Position = other.Position; Rotation = other.Rotation; Hash = other.Hash; @@ -279,17 +286,20 @@ public override string ToString() public class CollisionModel { + public ulong ParentHash { get; set; } public ulong Hash { get; set; } public TriangleMesh Mesh { get; set; } public IList
Sections { get; set; } - public CollisionModel(BinaryReader reader) + public CollisionModel(BinaryReader reader, ulong _ParentHash) { + ParentHash = _ParentHash; ReadFromFile(reader); } public CollisionModel() { + ParentHash = 0; Hash = 0; Mesh = new TriangleMesh(); Sections = new List
(); diff --git a/Mafia2Libs/ResourceTypes/FileTypes/FrameResource/FrameResource.cs b/Mafia2Libs/ResourceTypes/FileTypes/FrameResource/FrameResource.cs index 6fd6206d..ad59e368 100644 --- a/Mafia2Libs/ResourceTypes/FileTypes/FrameResource/FrameResource.cs +++ b/Mafia2Libs/ResourceTypes/FileTypes/FrameResource/FrameResource.cs @@ -471,7 +471,7 @@ private void AddChildren(Dictionary parsedNodes, List