void LoadERF(ERFObject obj, string resRef, ResourceType resType, Dictionary <string, ClassDefinition> defs, Compatibility compat, ExistsIn existsIn) { if (!gffTypes.Contains(resType)) { return; } string className = "Aurora" + resType.ToString().ToUpper().Replace(" ", ""); if (!defs.ContainsKey(className)) { defs[className] = new ClassDefinition(); } Stream stream = obj.GetResource(resRef, resType); if (resType == ResourceType.ERF || resType == ResourceType.SAV) { // ERFs can contain ERFs themselves! //Debug.Log("Loading nested ERF file " + resRef); LoadERFObj(new ERFObject(stream), defs, compat, existsIn); //Debug.Log("Finished loading nested ERF file " + resRef); } else { // Load the resource from the ERF GFFObject gffObject = new GFFLoader(stream).GetObject(); // Update the class definition LoadGFF(gffObject, defs, className, compat, existsIn); } }
void LoadBIF(BIFObject obj, Dictionary <string, ClassDefinition> defs, Compatibility compat, ExistsIn existsIn) { List <BIFObject.Resource> resources = obj.resources.ToList(); //Debug.Log(resources.Count()); for (int i = 0; i < resources.Count(); i++) { BIFObject.Resource resource = resources[i]; ResourceType resType = (ResourceType)resource.ResType; if (!gffTypes.Contains(resType)) { continue; } try { //Debug.Log("Loading resource " + resource.ResRef); string className = "Aurora" + resType.ToString().ToUpper().Replace(" ", ""); // Load the resource from the RIM Stream stream = obj.GetResourceData(resource); GFFObject gffObject = new GFFLoader(stream).GetObject(); LoadGFF(gffObject, defs, className, compat, existsIn); } catch { // Just keep going Debug.LogWarning("Failed to load " + resource.ID); } } }
void LoadRIM(RIMObject obj, RIMObject.Resource resource, Dictionary <string, ClassDefinition> defs, Compatibility compat, ExistsIn existsIn) { if (!gffTypes.Contains(resource.ResType)) { return; } try { //Debug.Log("Loading resource " + resource.ResRef); string className = "Aurora" + resource.ResType.ToString().ToUpper().Replace(" ", ""); // Load the resource from the RIM Stream stream = obj.GetResource(resource.ResRef, resource.ResType); GFFObject gffObject = new GFFLoader(stream).GetObject(); LoadGFF(gffObject, defs, className, compat, existsIn); } catch { // Just keep going Debug.LogWarning("Failed to load " + resource.ResRef); } }
void LoadERFObj(ERFObject obj, Dictionary <string, ClassDefinition> defs, Compatibility compat, ExistsIn existsIn) { foreach ((string resRef, ResourceType resType) in obj.resourceKeys.Keys) { //Debug.Log("Loading erf object " + resRef); LoadERF(obj, resRef, resType, defs, compat, existsIn); } }
void LoadGFFFromFile(string location, Dictionary <string, ClassDefinition> defs, string className, Compatibility compat, ExistsIn existsIn) { using (Stream stream = File.OpenRead(location)) { GFFObject gff = (new GFFLoader(stream)).GetObject(); LoadGFF(gff, defs, className, compat, existsIn); } }
void LoadGFF(GFFObject gff, Dictionary <string, ClassDefinition> defs, string className, Compatibility compat, ExistsIn existsIn) { if (!defs.ContainsKey(className)) { defs[className] = new ClassDefinition(); } // Update the class definition defs[className].AddToDefinition(gff, className, 0, compat, existsIn); }
void LoadGFFsFromBIFs(string dataDir, Dictionary <string, ClassDefinition> defs, Compatibility compat, ExistsIn existsIn) { // We load every BIF file in the game files // Find the list of module names // TODO: Implement this for KOTOR 2, which uses a different file structure foreach (string filename in Directory.GetFiles(dataDir)) { // We don't actually use the key here BIFObject obj = new BIFObject(filename, null); LoadBIF(obj, defs, compat, existsIn); } }
void LoadGFFsFromERF(string filename, Dictionary <string, ClassDefinition> defs, Compatibility compat, ExistsIn existsIn) { // We load every module in the game files // Find the list of module names // TODO: Implement this for KOTOR 2, which uses a different file structure //Debug.Log("Loading " + filename); ERFObject obj = new ERFObject(filename); LoadERFObj(obj, defs, compat, existsIn); }
void LoadGFFsFromRIMs(string moduleDir, Dictionary <string, ClassDefinition> defs, Compatibility compat, ExistsIn existsIn) { // We load every module in the game files // Find the list of module names // TODO: Implement this for KOTOR 2, which uses a different file structure foreach (string filename in Directory.GetFiles(moduleDir)) { //Debug.Log("Loading " + filename); RIMObject obj = new RIMObject(filename); List <RIMObject.Resource> resources = new List <RIMObject.Resource>(obj.resources.Values); //Debug.Log(resources.Count()); for (int i = 0; i < resources.Count(); i++) { RIMObject.Resource resource = resources[i]; LoadRIM(obj, resource, defs, compat, existsIn); } } }
public GFFAttribute(string name, Compatibility compat = Compatibility.BOTH, ExistsIn exists = ExistsIn.BOTH) { this.name = name; this.compatibility = compat; this.existsIn = exists; }
public string ToXML(string gffType, bool isRecursive = false, string structLabel = "") { // Writes this object in GFF format // Convert the object to XML string xml = !isRecursive ? "<gff3 type=\"" + gffType + "\">" : ""; bool hasStructID = false; uint structid = 0; List <string> values = new List <string>(); foreach (FieldInfo f in GetType().GetFields()) { GFFAttribute attr = f.GetCustomAttribute <GFFAttribute>(); string label = attr.name; Compatibility compat = attr.compatibility; ExistsIn existsIn = attr.existsIn; //Debug.Log("Compat: " + compat + "; exists: " + existsIn); // Make sure we only write fields compatible with the target game if (compat == Compatibility.KotOR && AuroraPrefs.TargetGame() == Game.TSL) { continue; } if (compat == Compatibility.TSL && AuroraPrefs.TargetGame() == Game.KotOR) { continue; } // Don't write save-only fields when writing modules // TODO: Allow an override for this in case mods need it? if (existsIn == ExistsIn.SAVE) { continue; } object value = f.GetValue(this); if (value == null) { continue; } if (label == "structid") { // The struct ID should be dealt with separately structid = (uint)f.GetValue(this); hasStructID = true; continue; } if (f.FieldType.IsSubclassOf(typeof(AuroraStruct))) { AuroraStruct structVal = (AuroraStruct)value; // This is a nested AuroraStruct values.Add(structVal.ToXML(gffType, true, label)); } else if (f.FieldType.IsGenericType && (f.FieldType.GetGenericTypeDefinition() == typeof(List <>))) { // This is a list string listXML = "<list label=\"" + label + "\""; IList list = (IList)value; if (list == null) { continue; } if (list.Count == 0) { listXML += "/>"; } else { listXML += ">"; foreach (AuroraStruct item in list) { listXML += item.ToXML(gffType, true); } listXML += "</list>"; } values.Add(listXML); } else if (f.FieldType == typeof(GFFObject.CExoLocString)) { values.Add(((GFFObject.CExoLocString)value).ToXML(f.Name)); } else if (f.FieldType == typeof(GFFObject.CExoString)) { values.Add(((GFFObject.CExoString)value).ToXML(f.Name)); } else if (f.FieldType == typeof(Quaternion)) { Quaternion q = (Quaternion)value; string quatXML = "<orientation label=\"" + f.Name + "\">"; quatXML += "<double>" + q.x + "</double>"; quatXML += "<double>" + q.y + "</double>"; quatXML += "<double>" + q.z + "</double>"; quatXML += "<double>" + q.w + "</double>"; quatXML += "</orientation>"; values.Add(quatXML); } else if (f.FieldType == typeof(Vector3)) { Vector3 v = (Vector3)value; string vecXML = "<vector label=\"" + f.Name + "\">"; vecXML += "<double>" + v.x + "</double>"; vecXML += "<double>" + v.y + "</double>"; vecXML += "<double>" + v.z + "</double>"; vecXML += "</vector>"; values.Add(vecXML); } else if (f.FieldType == typeof(Byte[])) { // Source: https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa string b64 = Convert.ToBase64String((byte[])value, Base64FormattingOptions.InsertLineBreaks); values.Add("<data label=\"" + label + "\">" + b64 + "</data>"); } else if (f.FieldType == typeof(Byte)) { byte b = (byte)f.GetValue(this); string byteXML = "<" + TypeNames[f.FieldType]; if (label != null) { byteXML += " label=\"" + label + "\""; } byteXML += ">" + b.ToString() + "</" + TypeNames[f.FieldType] + ">"; values.Add(byteXML); } else if (f.FieldType == typeof(Char)) { char c = (char)f.GetValue(this); string charXML = "<" + TypeNames[f.FieldType]; if (label != null) { charXML += " label=\"" + label + "\""; } if ((int)c == 0) { charXML += "/>"; } else { charXML += ">" + c.ToString() + "</" + TypeNames[f.FieldType] + ">"; } values.Add(charXML); } else { if (!TypeNames.ContainsKey(f.FieldType)) { Debug.Log("Could not find field type" + f.FieldType); } string valXML = "<" + TypeNames[f.FieldType]; if (label != null) { valXML += " label=\"" + label + "\""; } valXML += ">" + value.ToString() + "</" + TypeNames[f.FieldType] + ">"; values.Add(valXML); } } string structLabelXML = ""; if (structLabel != null && structLabel != "") { structLabelXML = " label=\"" + structLabel + "\""; } if (!isRecursive) { xml += "<struct id=\"4294967295\"" + structLabelXML + ">"; } else if (hasStructID) { xml += "<struct id=\"" + structid + "\"" + structLabelXML + ">"; } else { xml += "<struct id = \"0\"" + structLabelXML + ">"; } foreach (string s in values) { xml += s; } xml += "</struct>"; if (!isRecursive) { xml += "</gff3>"; } return(xml); }