public static string GetObjModelExtra(SetObject obj, SetObjectType type, string startName = null) { // Get the correct extra's model name string mdlName = startName; foreach (var extra in type.Extras) { if (extra.Type.ToLower() != "model") { continue; } if (string.IsNullOrEmpty(extra.Condition) || LuaScript.EvaluateObjectCondition(obj, type, extra.Condition)) { mdlName = extra.Value; break; } } if (string.IsNullOrEmpty(mdlName)) { return(mdlName); } // If the model name is actually supposed to be the value of // another parameter (e.g. in Gismos), get the name from that instead. if (mdlName.IndexOf('.') == -1) { int mdlNameParamIndex = type.GetParameterIndex(mdlName); if (mdlNameParamIndex != -1) { mdlName = (obj.Parameters[ mdlNameParamIndex].Data as string); } } else { int openIndex = mdlName.IndexOf('{'); int closeIndex = mdlName.IndexOf('}'); if (openIndex != -1 && closeIndex > openIndex) { ++openIndex; if (int.TryParse(mdlName.Substring(openIndex, closeIndex - openIndex), out int index) && index >= 0 && index < type.Parameters.Count) { mdlName = mdlName.Replace($"{{{index}}}", (obj.Parameters[index].Data as string)); } } } return(mdlName); }
/// <summary> /// Initialises Sonic Colors Set Editor /// </summary> public void Init() { HasBeenInit = true; Config.LoadConfig("config.bin"); UpdateObjects(); if (!Directory.Exists("Templates")) { var result = MessageBox.Show(Resources.NoTemplatesText, ProgramName, MessageBoxButtons.OKCancel, MessageBoxIcon.Error); if (result == DialogResult.OK) { Process.Start(Resources.TemplatesURL); } Environment.Exit(-1); } else { // Loads the object templates TemplatesColors = SetObjectType.LoadObjectTemplates(TemplatesPath, SonicColorsShortName); foreach (string objName in TemplatesColors.Keys) { ComboBox_ObjectType.Items.Add(objName); } Console.WriteLine("Loaded {0} Templates.", TemplatesColors.Count); // Load the Modifiers file if (File.Exists("Templates/Colors/Modifiers-ColorsToGenerations.xml")) { var doc = XDocument.Load("Templates/Colors/Modifiers-ColorsToGenerations.xml"); var renameNodes = doc.Root.Element("Rename").DescendantNodes().OfType <XElement>(); var objPhysNodes = doc.Root.Element("MakeObjectPhysics").DescendantNodes().OfType <XElement>(); var posYNodes = doc.Root.Element("Position-Y").DescendantNodes().OfType <XElement>(); var rotateXNodes = doc.Root.Element("Rotation-X").DescendantNodes().OfType <XElement>(); var rotateYNodes = doc.Root.Element("Rotation-Y").DescendantNodes().OfType <XElement>(); var paramNodes = doc.Root.Element("Param-Divide").DescendantNodes().OfType <XElement>(); ColorstoGensRenamers = renameNodes.ToDictionary(n => n.Name.ToString(), n => n.Value); ColorstoGensObjPhys = objPhysNodes.ToDictionary(n => n.Name.ToString(), n => n.Value); ColorstoGensPosYMods = posYNodes.ToDictionary(n => n.Name.ToString(), n => n.Value); ColorstoGensRotateXMods = rotateXNodes.ToDictionary(n => n.Name.ToString(), n => n.Value); ColorstoGensRotateYMods = rotateYNodes.ToDictionary(n => n.Name.ToString(), n => n.Value); ColorstoGensParamMods = paramNodes.ToDictionary(n => n.Name.ToString(), n => n.Value); } } if (File.Exists("CpkMaker.dll")) { HasCPKMaker = true; ToolStripMenuItem_ExtractCPK.Enabled = true; } else { Message(Resources.CPKMakerNotFoundText); } }
private void ReloadTemplates_ToolStripMenuItem_Click(object sender, EventArgs e) { // Loads the object templates ComboBox_ObjectType.Items.Clear(); TemplatesColors = SetObjectType.LoadObjectTemplates(TemplatesPath, SonicColorsShortName); foreach (string objName in TemplatesColors.Keys) { ComboBox_ObjectType.Items.Add(objName); } Console.WriteLine("Loaded {0} Templates.", TemplatesColors.Count); }
// Methods public static void Load(string startupPath) { string filePath = Helpers.CombinePaths(startupPath, FilePath); string templatesDir = Helpers.CombinePaths(startupPath, TemplatesDir); using (var fileStream = File.OpenRead(filePath)) { var xml = XDocument.Load(fileStream); var versionAttr = xml.Root.Attribute("version"); if (versionAttr == null) { return; } if (float.TryParse(versionAttr.Value, out float v) && v > HighestSupportedVersion) { throw new FileLoadException(string.Format( "Could not load GameList. GameLists of version ({0}) are unsupported.", versionAttr.Value), filePath); } foreach (var element in xml.Root.Elements("Game")) { // Game Name var shortNameAttr = element.Attribute("shortName"); var nameAttr = element.Attribute("name"); var unitMultiplierAttr = element.Attribute("unitMultiplier"); if (shortNameAttr == null) { continue; } string shortName = shortNameAttr.Value; // Game Entry var templates = SetObjectType.LoadObjectTemplates(templatesDir, shortName); var game = new GameEntry() { Name = (nameAttr == null) ? shortName : nameAttr.Value, ShortName = shortName, ObjectTemplates = templates, UnitMultiplier = (unitMultiplierAttr == null) ? 1 : Convert.ToSingle(unitMultiplierAttr.Value) }; Games.Add(game); // TODO: Remove this line Console.WriteLine("Loaded {0} templates for {1}.", game.ObjectTemplates.Count, game.Name); } } }
/// <summary> /// Creates temporary templates from a XML file that contains setdata /// </summary> /// <param name="filePath">Path to the XML</param> /// <param name="save"></param> public void CreateObjectTemplateFromXMLSetData(string filePath, bool save = false) { var xml = XDocument.Load(filePath); foreach (var objElem in xml.Root.Elements("Object")) { var typeAttr = objElem.Attribute("type"); var parametersElem = objElem.Element("Parameters"); if (typeAttr == null) { continue; } if (TemplatesColors.ContainsKey(typeAttr.Value)) { continue; } var objType = new SetObjectType(); objType.Name = typeAttr.Value; if (parametersElem != null) { foreach (var paramElem in parametersElem.Elements()) { var param = new SetObjectTypeParam(); var dataTypeAttr = paramElem.Attribute("type"); if (dataTypeAttr == null) { continue; } var dataType = Types.GetTypeFromString(dataTypeAttr.Value); param.Name = paramElem.Name.ToString(); param.DataType = dataType; param.DefaultValue = paramElem.Value; param.Description = "TODO"; objType.Parameters.Add(param); } } TemplatesColors.Add(typeAttr.Value, objType); if (save) { objType.Save(typeAttr.Value + ".xml", true); } } }
public static VPModel GetObjectModelInfo(SetObject obj, SetObjectType type, out Vector3 offsetPos, bool loadModels = true) { // Get offset position/scale (if any) offsetPos = GetObjOffsetPos(type); obj.Transform.Scale = GetObjScale(type, obj); // Load Object Model string mdlName = null; if (loadModels && type != null) { mdlName = GetObjModelExtra(obj, type); mdlName = Path.GetFileNameWithoutExtension(mdlName); } return(GetObjectModel(mdlName)); }
public static void LoadObjectResources(GameEntry gameType, SetObject obj, bool loadModels = true) { // Get Object Template (if any) SetObjectType type = null; if (gameType.ObjectTemplates.ContainsKey(obj.ObjectType)) { type = gameType.ObjectTemplates[obj.ObjectType]; } // Update Object Info var mdl = GetObjectModelInfo(obj, type, out Vector3 offsetPos, loadModels); // Spawn Object in World SpawnObject(obj, mdl, offsetPos, gameType.UnitMultiplier); }
public static Vector3 GetObjScale(SetObjectType type, SetObject obj) { if (type == null) { return(new Vector3(1, 1, 1)); } var scaleExtra = type.GetExtra("scale"); if (scaleExtra != null && !string.IsNullOrEmpty(scaleExtra.Value)) { if (float.TryParse(scaleExtra.Value, out float s)) { return(new Vector3(s, s, s)); } // TODO: Maybe try to parse it as a Vector3 as well? else { int scaleParamIndex = type.GetParameterIndex(scaleExtra.Value); if (scaleParamIndex != -1) { var param = obj.Parameters[scaleParamIndex]; if (param != null) { if (param.DataType == typeof(Vector3)) { return((Vector3)param.Data); } else if (param.DataType == typeof(float)) { float f = (float)param.Data; return(new Vector3(f, f, f)); } } } } } return(new Vector3(1, 1, 1)); }
public static bool EvaluateObjectCondition(SetObject obj, SetObjectType template, string condition) { try { var s = new Script(); for (int i = 0; i < template.Parameters.Count; ++i) { var param = template.Parameters[i]; s.Globals[param.Name] = obj.Parameters[i].Data; } string txt = $"return ({condition.Replace("!=", "~=")})"; return(s.DoString(txt).Boolean); } catch (Exception ex) { LuaTerminal.LogError($"ERROR: {ex.Message}"); return(false); } }
public static Vector3 GetObjOffsetPos(SetObjectType type) { var offsetPos = new Vector3(0, 0, 0); if (type != null) { var offsetPosExtra = type.GetExtra("OffsetPosition"); if (offsetPosExtra == null) { offsetPos.X = GetFloatExtra("OffsetPositionX", "Offset_Position_X"); offsetPos.Y = GetFloatExtra("OffsetPositionY", "Offset_Position_Y"); offsetPos.Z = GetFloatExtra("OffsetPositionZ", "Offset_Position_Z"); } else if (!string.IsNullOrEmpty(offsetPosExtra.Value)) { offsetPos = (Vector3)Helpers.ChangeType( offsetPosExtra.Value, typeof(Vector3)); } // Sub-Methods float GetFloatExtra(string name, string altName) { var extra = type.GetExtra(name); if (extra == null) { extra = type.GetExtra(altName); } float.TryParse((string.IsNullOrEmpty(extra?.Value)) ? "0" : extra.Value, out float f); return(f); } } return(offsetPos); }
public static XElement WriteObject(SetObject obj_, SetObjectType type = null) { if (!(obj_ is GensSetObject)) { throw new Exception("Not a GensSetObject!"); } var obj = obj_ as GensSetObject; // Parameters var elem = new XElement(obj.ObjectType); for (int i = 0; i < obj.Parameters.Count; ++i) { elem.Add(new XElement((type == null) ? $"Parameter{i + 1}" : type.Parameters[i].Name, obj.Parameters[i].Data)); } // MultiSetTransforms if (obj.Children.Length > 0) { var multiSetParam = new XElement("MultiSetParam"); for (int i = 0; i < obj.Children.Length;) { var multiSetElem = new XElement("Element"); multiSetElem.AddElem("Index", ++i); WriteTransform(obj.Children[i], multiSetElem); multiSetParam.Add(multiSetElem); } elem.AddElem("BaseLine", 0); elem.AddElem("Count", obj.Children.Length + 1); // TODO: Is this right? elem.AddElem("Direction", 0); elem.AddElem("Interval", 1.5f); elem.AddElem("IntervalBase", 0); elem.AddElem("PositionBase", 0); elem.AddElem("RotationBase", 0); elem.Add(multiSetParam); } // Transform WriteTransform(obj.Transform, elem); // Special Parameters elem.AddElem("Range", obj.GetCustomDataValue <float>("Range", 100)); elem.AddElem("SetObjectID", obj.ObjectID); elem.AddElem("Target", obj.TargetID); elem.AddElem("TargetPosition", obj.TargetPosition); foreach (var customData in obj.CustomData) { if (customData.Key == "Range") { continue; } elem.Add(new XElement( customData.Key, customData.Value)); } return(elem); }