using System; using System.IO; using System.Collections.Generic; using NW4RTools; using NW4RTools.Models; using NW4RTools.Models.Animation; using OpenTK; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL; namespace TestApp { class MainClass { public static void Main(string[] args) { ResFile shits = BrresReader.LoadFile(File.ReadAllBytes("Z:/stuff/Games/Newer/bros.brres")); ColladaExporter.WriteModel(File.OpenWrite("Z:/stuff/Games/Newer/bros.dae"), shits, "bros"); //ResFile shits = BrresReader.LoadFile(File.ReadAllBytes("/home/me/Games/Newer/bros.brres")); //ColladaExporter.WriteModel(File.OpenWrite("/home/me/Games/Newer/bros.dae"), shits, "bros"); return; striphammer(); return; fuckskawo(); return; ewater(); return; blah(args); return; string mdlPath = "/home/me/Games/Newer/ModelRev/"; //skawo(); return; oldBehaviour(); return; // Going to create a model! //string filename = "crapmap", resmdlname = "CrapMap"; /*string filename = "SMGoldwood"; ResFile rf = new ResFile(); ObjImporter.ImportModel(mdlPath, File.OpenText(mdlPath + "GoldwoodBase.obj"), rf, "GoldwoodBase", ObjImporter.LightmapType.Map); ObjImporter.ImportModel(mdlPath, File.OpenText(mdlPath + "BrownTree.obj"), rf, "BrownTree", ObjImporter.LightmapType.MapObj); ObjImporter.ImportModel(mdlPath, File.OpenText(mdlPath + "RedTree.obj"), rf, "RedTree", ObjImporter.LightmapType.MapObj); File.WriteAllBytes(mdlPath + "SMGoldwood.brres", BrresWriter.WriteFile(rf));*/ string filename = "DemoMap"; //string filename = "MMFullWorld"; string resmdlname = "WorldBase"; //string objname = "fullworld-tree-fbx"; string objname = "crapmap2"; ResFile rf = new ResFile(); ObjImporter.ImportModel(mdlPath, File.OpenText(mdlPath + objname + ".obj"), rf, resmdlname, ObjImporter.LightmapType.Map); File.WriteAllBytes(mdlPath + filename + ".brres", BrresWriter.WriteFile(rf)); ResFile rf2 = BrresReader.LoadFile(File.ReadAllBytes(mdlPath + filename + ".brres")); using (var gw = new RenderWindow()) { gw.Title = filename; gw.SetModel(rf2, resmdlname); gw.Run(1, 1); } //*/ /*var texs = rf.GetGroup("Textures(NW4R)"); // wtf C#?! foreach (var kv in (IEnumerable>)texs) { kv.Value.BaseImage.Save(mdlPath + "images/" + kv.Key + ".png"); } //var objFile = File.Open(mdlPath + mdlName + ".obj", FileMode.OpenOrCreate); var objFile = File.Open(mdlPath + mdlName + ".dae", FileMode.OpenOrCreate); //var sw = new StreamWriter(objFile); //ObjWriter.WriteModel(sw, rf, mdlName); ColladaWriter.WriteModel(objFile, rf, mdlName + whatever); objFile.Close();*/ } private static void striphammer() { string mdlPath = "/home/me/Games/Newer/ISO/files/Object/hammerM/g3d/"; ResFile rf = BrresReader.LoadFile(File.ReadAllBytes(mdlPath + "bros.brres")); var mgrp = rf.GetModelGroup(); var mremove = new List(); foreach (var k in mgrp.Keys) if (k != "bros_hammer") mremove.Add(k); foreach (var k in mremove) mgrp.Remove(k); var tgrp = rf.GetTextureGroup(); var texremove = new List(); var texkeep = new Dictionary(); texkeep.Add("bros_hammer", true); texkeep.Add("bros_hammer_env", true); texkeep.Add("bros_hammer_handle", true); texkeep.Add("lm_01e", true); texkeep.Add("lm_02e", true); foreach (var k in tgrp.Keys) if (!texkeep.ContainsKey(k)) texremove.Add(k); foreach (var k in texremove) tgrp.Remove(k); rf.Remove("AnmChr(NW4R)"); File.WriteAllBytes(mdlPath + "hammer.brres", BrresWriter.WriteFile(rf)); } private static void skawo() { string mdlPath = "/home/me/Games/Newer/ModelRev/"; string skawopath = "/home/me/Dropbox/NEWERsmbw/Nowy folder/obj/"; ResFile rf = new ResFile(); ObjImporter.ImportModel(skawopath, File.OpenText(skawopath + "edited.obj"), rf, "Skawo"); File.WriteAllBytes(skawopath + "skawo.brres", BrresWriter.WriteFile(rf)); ResFile rf2 = BrresReader.LoadFile(File.ReadAllBytes(skawopath + "skawo.brres")); using (var gw = new RenderWindow()) { gw.Title = "Skawo"; gw.SetModel(rf2, "Skawo"); gw.Run(1, 1); } } private static void oldBehaviour() { string mdlPath = "/home/me/Games/Newer/ModelRev/"; string mdlName = "CS_W1"; //string mdlName = "lift_han_wood"; //string mdlName = "bgB_4502"; //string mdlName = "cobKoopaCastle"; //string mdlName = "waterPlate_W4boss"; //mdlName = "test_lift"; string whatever = (mdlName == "CS_W2" || mdlName == "CS_W3" || mdlName == "CS_W6") ? "a" : ""; byte[] file = File.ReadAllBytes(mdlPath + mdlName + ".brres"); ResFile rf = BrresReader.LoadFile(file); using (var gw = new RenderWindow()) { gw.Title = mdlName; gw.SetModel(rf, mdlName); gw.Run(1, 1); } //File.WriteAllBytes(mdlPath + mdlName + "_rewritten.brres", BrresWriter.WriteFile(rf)); /*var texs = rf.GetGroup("Textures(NW4R)"); // wtf C#?! foreach (var kv in (IEnumerable>)texs) { kv.Value.Images[0].Save(mdlPath + "images/" + kv.Key + ".png"); }*/ //var objFile = File.Open(mdlPath + "lift_han_wood_M.dae", FileMode.OpenOrCreate); //ColladaExporter.WriteModel(objFile, rf, "lift_han_wood_M"); //objFile.Close(); } private static void blah(string[] args) { string mdlPath = getPath(); ResFile rf = BrresReader.LoadFile(File.ReadAllBytes(mdlPath + "CS_W4.brres")); File.WriteAllBytes(mdlPath + "CS_W4a.brres", BrresWriter.WriteFile(rf)); if (args.Length > 0 && args [0] == "a") { ResFile rf2 = BrresReader.LoadFile(File.ReadAllBytes(mdlPath + "CS_W4a.brres")); File.WriteAllBytes(mdlPath + "CS_W4b.brres", BrresWriter.WriteFile(rf2)); ResFile rf3 = BrresReader.LoadFile(File.ReadAllBytes(mdlPath + "CS_W4b.brres")); File.WriteAllBytes(mdlPath + "CS_W4c.brres", BrresWriter.WriteFile(rf3)); ResFile rf4 = BrresReader.LoadFile(File.ReadAllBytes(mdlPath + "CS_W4c.brres")); File.WriteAllBytes(mdlPath + "CS_W4d.brres", BrresWriter.WriteFile(rf4)); ResFile rf5 = BrresReader.LoadFile(File.ReadAllBytes(mdlPath + "CS_W4d.brres")); File.WriteAllBytes(mdlPath + "CS_W4e.brres", BrresWriter.WriteFile(rf5)); } } private static void ewater() { string mdlPath = getPath(); ResFile rf = BrresReader.LoadFile(File.ReadAllBytes(mdlPath + "CS_W4.brres")); Model m = rf.GetModelGroup()["CS_W4"]; foreach (var kv in m.Shapes) { var s = kv.Value; Console.WriteLine("{0} : {1}", kv.Key, m.VtxPosData.GetKeyForValue(s.PosData)); } var tgrp = rf.GetTextureGroup(); var texremove = new List(); var texkeep = new Dictionary(); texkeep.Add("W3a_water01", true); texkeep.Add("water_wave1mm", true); texkeep.Add("water01", true); texkeep.Add("texMask", true); foreach (var k in tgrp.Keys) if (!texkeep.ContainsKey(k)) texremove.Add(k); foreach (var k in texremove) tgrp.Remove(k); rf.Remove("AnmClr(NW4R)"); rf.Remove("AnmChr(NW4R)"); var tsanmg = rf.GetGroup("AnmTexSrt(NW4R)"); var tsanm = tsanmg["CS_W4"]; tsanmg.RemoveAt(0); tsanmg.Add("KpBG", tsanm); tsanm.Nodes.Remove("mtCloudA"); tsanm.Nodes.Remove("mtCloudB"); tsanm.Nodes.Remove("mtWater_nami"); rf.GetModelGroup().RemoveAt(0); rf.GetModelGroup()["KpBG"] = m; // Make bytecode m.MatrixIDtoNodeID = new int[1]; m.MatrixIDtoNodeID[0] = 0; // Now put together the NodeTree var nodeTreeInsn = new ByteCode.AssignNodeToParentMtxInstruction(); nodeTreeInsn.NodeID = 0; nodeTreeInsn.ParentMatrixID = 0; var done = new ByteCode.DoneInstruction(); var nodeTree = new ByteCode(); nodeTree.Instructions.Add(nodeTreeInsn); nodeTree.Instructions.Add(done); m.Bytecode["NodeTree"] = nodeTree; // Also, DrawOpa var drawOpa = new ByteCode(); var shapeInsn = new ByteCode.DrawShapeInstruction(); shapeInsn.ShapeID = 0; shapeInsn.NodeID = 0; shapeInsn.MaterialID = 0; drawOpa.Instructions.Add(shapeInsn); drawOpa.Instructions.Add(done); m.Bytecode["DrawOpa"] = drawOpa; var drawXlu = new ByteCode(); drawXlu.Instructions.Add(done); m.Bytecode["DrawXlu"] = drawXlu; // Clean up other shit var seashape = m.Shapes["polygon106"]; var posd = seashape.PosData; var nrmd = seashape.NrmData; var clrd = seashape.ClrData; var tcd = seashape.TexCoordData; var nnod = new ResDict(); var nshp = new ResDict(); var nshd = new ResDict(); var nmat = new ResDict(); var npos = new ResDict(); var nnrm = new ResDict(); var nclr = new ResDict(); var ntc = new ResDict(); Node rn = m.Nodes["CS_W4"]; rn.FirstChild = null; nnod["RootNode"] = rn; nshp["waterpoly"] = seashape; nshd["mtWater"] = m.Shaders["mtWater"]; nmat["mtWater"] = m.Materials["mtWater"]; if (posd != null) { npos[m.VtxPosData.GetKeyForValue(posd)] = posd; } if (nrmd != null) { nnrm[m.VtxNrmData.GetKeyForValue(nrmd)] = nrmd; } foreach (var t in clrd) { if (t != null) { nclr[m.VtxClrData.GetKeyForValue(t)] = t; } } foreach (var t in tcd) { if (t != null) { ntc[m.VtxTexCoordData.GetKeyForValue(t)] = t; } } m.Nodes = nnod; m.Shapes = nshp; m.Shaders = nshd; m.Materials = nmat; m.VtxPosData = npos; m.VtxNrmData = nnrm; m.VtxClrData = nclr; m.VtxTexCoordData = ntc; m.PairingLookupByTexture = new ResDict>(); m.PairingLookupByPalette = new ResDict>(); // NEW var dl = new DisplayListWriter(); dl.BeginPrimitives(PrimitiveType.Quads, 0, 4); dl.WriteByte(0); dl.WriteByte(0); dl.WriteByte(1); dl.WriteByte(1); dl.WriteByte(2); dl.WriteByte(2); dl.WriteByte(3); dl.WriteByte(3); dl.End(); seashape.DisplayList2 = dl.GetBuffer(); seashape.DLBufferSize2 = (uint)seashape.DisplayList2.Length; var shape = seashape; float w = 200000, h = 100000; float sw = 600, sh = 600; float ow = -480, oh = 480; posd.EntryCount = 4; posd.Data = new float[][] { new float[] { ow, (-h) + oh, -40 }, new float[] { ow, oh, -40 }, new float[] { ow + w, oh, -40 }, new float[] { ow + w, (-h) + oh, -40 } }; posd.EntrySize = 12; posd.Fraction = 0; posd.ComponentType = VertexSettings.CompType.Float32; posd.Save(); tcd[0].EntryCount = 4; tcd[0].Fraction = 0; tcd[0].Data = new float[][] { new float[] { 0, 0 }, new float[] { sw, 0 }, new float[] { sw, sh }, new float[] { 0, sh } }; tcd[0].Save(); /* posd.Data = new float[][] { new float[] { 0, -40, h }, new float[] { 0, -40, 0 }, new float[] { w, -40, 0 }, new float[] { w, -40, h } };*/ var vs = new VertexSettings(); vs.PositionDesc = VertexSettings.DescType.Index8; vs.PositionFormat = VertexSettings.CompType.Float32; vs.PositionCount = VertexSettings.CompCount.Position3; vs.TexCoordDesc[0] = VertexSettings.DescType.Index8; vs.TexCoordFormat[0] = VertexSettings.CompType.Int16; vs.TexCoordCount[0] = VertexSettings.CompCount.TexCoord2; vs.TexCoordFrac[0] = 0; uint vd1, vd2, vat1, vat2, vat3; vs.GetDesc(out vd1, out vd2); vs.GetAttrFmt(out vat1, out vat2, out vat3); byte vtxSpecs = 1 << 4; // setting up Unk in the hopes it will work, I think this is what it's for.. var unkStream = new OutputStream(); unkStream.WriteUInt32(vd1); unkStream.WriteUInt32(vd2); unkStream.WriteUInt32(vtxSpecs); shape.Unk = unkStream.GetBuffer(); // now make the display list var dl1 = new DisplayListWriter(); dl1.AddPadding(10); dl1.LoadCPReg(0x50, vd1); dl1.LoadCPReg(0x60, vd2); dl1.LoadXFReg(0x1008, new byte[] { 0x00, 0x00, 0x00, vtxSpecs }); dl1.Nop(); dl1.LoadCPReg(0x70, vat1); dl1.LoadCPReg(0x80, vat2); dl1.LoadCPReg(0x90, vat3); // extend it // should it be bigger if more texcoords are used? maybe... dl1.PadToSize(0x80); dl1.End(); shape.DLBufferSize1 = 0xE0; shape.DisplayList1 = dl1.GetBuffer(); var texmask = tgrp["texMask"].Images[0]; var g = System.Drawing.Graphics.FromImage(texmask); g.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White), 0, 0, texmask.Width, texmask.Height); g.Dispose(); // END NEW File.WriteAllBytes(mdlPath + "Water.brres", BrresWriter.WriteFile(rf)); } private static string getPath() { if (Directory.Exists("/home/me")) return "/home/me/Games/Newer/WaterL/"; else return "Z:/stuff/Games/Newer/WaterL/"; } private static void fuckskawo() { var rf = BrresReader.LoadFile(File.ReadAllBytes("/home/me/Games/Newer/Pakku/pakkun/g3d/pakkun.brres")); File.WriteAllBytes("/home/me/Games/Newer/Pakku/pakkun/g3d/pakkuRewrite.brres", BrresWriter.WriteFile(rf)); } } }